Skip to content

Commit

Permalink
Move the included font into the main package and update docs (celiagg#77
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jwiggins authored Feb 25, 2021
1 parent 82724b2 commit ca19dee
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 49 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include celiagg/*.h
include celiagg/*.pxd
include celiagg/*.pxi
include celiagg/*.pyx
include celiagg/tests/data/*
include celiagg/data/*
include LICENSE
include README.rst
graft agg-svn/agg-2.4/include
Expand Down
14 changes: 13 additions & 1 deletion celiagg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@
# Query the library
HAS_TEXT = _celiagg.has_text_rendering()


def example_font():
""" Returns the path to a TTF font which is included with the library for
testing purposes.
"""
import pkg_resources

return pkg_resources.resource_filename(
'celiagg', 'data/Montserrat-Regular.ttf'
)


# Be explicit
__all__ = [
'HAS_TEXT',
'HAS_TEXT', 'example_font',

'AggError', 'BlendMode', 'BSpline', 'DrawingMode', 'Font', 'FontCache',
'GradientSpread', 'GradientUnits', 'GraphicsState', 'Image', 'InnerJoin',
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion celiagg/tests/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_bad_method_args(self):

if agg.HAS_TEXT:
text = 'Hello!'
font = agg.Font('Times New Roman', 12.0)
font = agg.Font(agg.example_font(), 12.0)
with self.assertRaises(TypeError):
canvas.draw_text(text, None, transform, gs)
with self.assertRaises(TypeError):
Expand Down
13 changes: 4 additions & 9 deletions celiagg/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
import unittest

import numpy as np
import pkg_resources

import celiagg as agg

FONT_FILE = pkg_resources.resource_filename(
'celiagg.tests', 'data/Montserrat-Regular.ttf'
)


@unittest.skipIf(not agg.HAS_TEXT, 'Text support is not available')
class TestTextDrawing(unittest.TestCase):
Expand All @@ -20,9 +15,9 @@ def test_font_byte_string(self):
transform = agg.Transform()

text_unicode = 'Hello!'
font_unicode = agg.Font(FONT_FILE, 12.0, face_index=1)
font_unicode = agg.Font(agg.example_font(), 12.0, face_index=1)
text_byte = b'Hello!'
font_byte = agg.Font(FONT_FILE.encode('utf8'), 12.0)
font_byte = agg.Font(agg.example_font().encode('utf8'), 12.0)

canvas.draw_text(text_unicode, font_unicode, transform, gs)
canvas.draw_text(text_byte, font_unicode, transform, gs)
Expand All @@ -36,7 +31,7 @@ def test_text_rendering(self):
)
canvas.clear(1.0, 1.0, 1.0)

font = agg.Font(FONT_FILE, 24.0)
font = agg.Font(agg.example_font(), 24.0)
gs = agg.GraphicsState()
paint = agg.SolidPaint(1.0, 0.0, 0.0, 1.0)
transform = agg.Transform()
Expand All @@ -56,7 +51,7 @@ def test_text_measurement(self):
canvas = agg.CanvasRGB24(
np.zeros((100, 100, 3), dtype=np.uint8), font_cache=font_cache,
)
font = agg.Font(FONT_FILE, 12.0)
font = agg.Font(agg.example_font(), 12.0)
gs = agg.GraphicsState()
paint = agg.SolidPaint(1.0, 0.0, 0.0, 1.0)
text = 'Some appropriate string'
Expand Down
18 changes: 7 additions & 11 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ which can be drawn using the ``Canvas.draw_shape`` method.
:inherited-members:


Functions
---------

.. autofunction:: example_font


Enumerations
------------

Expand Down Expand Up @@ -136,17 +142,6 @@ fill variations use the even-odd rule when filling self-intersecting curves.
* ``DrawFillStroke``
* ``DrawEofFillStroke``

FontCacheType
~~~~~~~~~~~~~

Determines whether text is rendered with a raster or vector representation.
Raster text is faster, but *doesn't support different stroke and fill paints*;
only solid color. Vector text is slower but very powerful, and can make use of
gradients or patterned fills/strokes.

* ``RasterFontCache``
* ``VectorFontCache``

GradientSpread
~~~~~~~~~~~~~~

Expand Down Expand Up @@ -236,6 +231,7 @@ TextDrawingMode
* ``TextDrawFill``
* ``TextDrawStroke``
* ``TextDrawClip``
* ``TextDrawRaster``
* ``TextDrawFillStroke``
* ``TextDrawFillClip``
* ``TextDrawStrokeClip``
Expand Down
6 changes: 3 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@

# General information about the project.
project = 'celiagg'
copyright = '2015-2018, John Wiggins, Erik Hvatum'
copyright = '2015-2021, John Wiggins, Erik Hvatum'
author = 'John Wiggins, Erik Hvatum'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0.4'
version = '1.1.2'
# The full version, including alpha/beta/rc tags.
release = '1.0.4'
release = '1.1.2'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
Binary file modified docs/source/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 16 additions & 13 deletions docs/source/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Imports
-------

First, we include some libraries. ``numpy`` is needed for the memory buffer
which the canvas drawns into. ``scikit-image`` is used for simple image file I/O.
which the canvas drawns into. ``PIL`` is used for simple image file I/O.
Finally, ``celiagg`` is imported for obvious reasons:

.. literalinclude:: simple_ex.py
Expand All @@ -33,10 +33,10 @@ State setup
Next, some state objects are created. Most important is the ``GraphicsState``
object. Pay attention to the ``drawing_mode=agg.DrawingMode.DrawStroke`` keyword
argument. This means that all drawing calls using this ``state`` will only draw
the outline of the shapes or text being drawn.
the outline of the shapes being drawn.

The remaining state objects are ``transform`` which sets the affine
transformation of the drawing and ``red_paint``/``black_paint`` which determine
transformation of the drawing and ``red_paint``/``orange_paint`` which determine
the color of strokes (outlines) or fills when drawing.

.. literalinclude:: simple_ex.py
Expand All @@ -61,7 +61,7 @@ Draw a circle
We're now ready to do some drawing. A ``path`` object is created to describe the
shape that will be drawn. In this case, it's an ellipse with an equal width and
height of 190 pixels, ie: a circle. Because the canvas was instantiated with
``np.ones``, we call the ``canvas.clear`` method first to clear the background
``np.zeros``, we call the ``canvas.clear`` method first to clear the background
color to white. Finally, the circle is drawn with ``canvas.draw_shape``.

.. literalinclude:: simple_ex.py
Expand All @@ -72,26 +72,29 @@ color to white. Finally, the circle is drawn with ``canvas.draw_shape``.
Draw some text
--------------

Next up, some text. As a small caveat, this demo was written to run on macOS
where the path ``/Library/Fonts/Verdana.ttf`` points to a TTF font file. A raster
``font`` object is created with a point size of 96. The ``transform`` gets a
Next up, some text. We'll use the `Montserrat <https://github.com/JulietaUla/Montserrat>`_ font which is included with
celiagg, conveniently available via the ``example_font`` function. A ``font``
object is created with a point size of 96. The ``transform`` gets a
translation of (30, 220) set. This corresponds to a point which is 30 pixels
from the left side of the image and 220 pixels from the *top* of the image.
(celiagg defaults to a top-left origin, but also supports bottom-left origin)
The text will be drawn starting from that point.

.. literalinclude:: simple_ex.py
:lines: 18-22
:lines: 18-20
:linenos:
:lineno-match:

.. note::
Even though ``state`` specifies a ``drawing_mode`` of ``DrawStroke``, the
text will still be rendered as filled instead of drawing an outline. This
is because of the ``agg.FontCacheType.RasterFontCache`` argument given when
instantiating ``font``. To honor the ``drawing_mode``,
``agg.FontCacheType.VectorFontCache`` can be passed instead. Note that there
is a performance penalty paid for this flexibility.
is because text drawing mode is separate from the main [shape] drawing mode.
``text_drawing_mode`` in ``state`` defaults to
``agg.TextDrawingMode.TextDrawRaster``. To draw text as vector data,
``state.text_drawing_mode`` can be set to something like
``agg.TextDrawingMode.TextDrawStroke``. All modes except ``TextDrawRaster``
are drawn as vectors. Note that there is a performance penalty paid for this
flexibility, but it allows things like patterned fills.

Save
----
Expand All @@ -100,6 +103,6 @@ Last but not least, the image is written to a PNG file for enjoyment in your
favorite image viewer.

.. literalinclude:: simple_ex.py
:lines: 24
:lines: 22-23
:linenos:
:lineno-match:
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Welcome to celiagg's documentation!

celiagg is a Cython wrapper of the popular
`Anti-Grain Geometry library <http://www.antigrain.com>`_ which
supports both Python 3 (3.5+).
supports Python 3 (3.5+).

Keeping with the tradition of AGG, celiagg aims to have a minimal number of
dependencies. Currently only `numpy <http://www.numpy.org/>`_ is a required
Expand Down
2 changes: 2 additions & 0 deletions docs/source/philosophy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ These are the main objects used for drawing in celiagg.

* ``Canvas``: The object which has the main drawing methods. It also manages
the frame buffer which is being drawn into.
* ``GraphicsState``: The main state container to be passed to all ``Canvas``
drawing methods.
* ``VertexSource``: A family of classes which contain vector data to be
rasterized. Includes ``Path``, ``BSpline``, and ``ShapeAtPoints``
* ``Paint``: Another family of classes which describe the color and texture of
Expand Down
15 changes: 7 additions & 8 deletions docs/source/simple_ex.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import celiagg as agg
import numpy as np
from skimage.io import imsave
from PIL import Image

state = agg.GraphicsState(drawing_mode=agg.DrawingMode.DrawStroke,
line_width=10.0)
transform = agg.Transform()
red_paint = agg.SolidPaint(1.0, 0.0, 0.0)
black_paint = agg.SolidPaint(0.0, 0.0, 0.0)
orange_paint = agg.SolidPaint(0.99, 0.66, 0.0)

canvas = agg.CanvasRGB24(np.ones((400, 400, 3), dtype=np.uint8))
canvas = agg.CanvasRGB24(np.zeros((400, 400, 3), dtype=np.uint8))

path = agg.Path()
path.ellipse(200, 200, 190, 190)
canvas.clear(1.0, 1.0, 1.0)
canvas.draw_shape(path, transform, state, stroke=red_paint)

font = agg.Font("/Library/Fonts/Verdana.ttf", 96.0,
agg.FontCacheType.RasterFontCache)
font = agg.Font(agg.example_font(), 96.0)
transform.translate(30.0, 220.0)
canvas.draw_text("celiagg", font, transform, state,
stroke=black_paint)
canvas.draw_text("celiagg", font, transform, state, fill=orange_paint)

imsave("example.png", canvas.array)
image = Image.fromarray(canvas.array, "RGB")
image.save("example.png")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ def configuration(parent_package='', top_path=None):
requires=['numpy', 'freetype'],
packages=['celiagg', 'celiagg.tests'],
package_data={
'celiagg.tests': ['data/*'],
'celiagg': ['data/*'],
},
)

0 comments on commit ca19dee

Please sign in to comment.