glyf: Glyph Data

class fontTools.ttLib.tables._g_l_y_f.table__g_l_y_f(tag=None)[source]

Bases: DefaultTable

Glyph Data Table

This class represents the glyf table, which contains outlines for glyphs in TrueType format. In many cases, it is easier to access and manipulate glyph outlines through the GlyphSet object returned from fontTools.ttLib.ttFont.getGlyphSet():

>> from fontTools.pens.boundsPen import BoundsPen
>> glyphset = font.getGlyphSet()
>> bp = BoundsPen(glyphset)
>> glyphset["A"].draw(bp)
>> bp.bounds
(19, 0, 633, 716)

However, this class can be used for low-level access to the glyf table data. Objects of this class support dictionary-like access, mapping glyph names to Glyph objects:

>> glyf = font["glyf"]
>> len(glyf["Aacute"].components)
2

Note that when adding glyphs to the font via low-level access to the glyf table, the new glyphs must also be added to the hmtx/vmtx table:

>> font["glyf"]["divisionslash"] = Glyph()
>> font["hmtx"]["divisionslash"] = (640, 0)
setGlyphOrder(glyphOrder)[source]

Sets the glyph order

Parameters:

glyphOrder ([str]) – List of glyph names in order.

getGlyphName(glyphID)[source]

Returns the name for the glyph with the given ID.

Raises a KeyError if the glyph name is not found in the font.

getGlyphID(glyphName)[source]

Returns the ID of the glyph with the given name.

Raises a ValueError if the glyph is not found in the font.

removeHinting()[source]

Removes TrueType hints from all glyphs in the glyphset.

See Glyph.removeHinting().

getPhantomPoints(glyphName, ttFont, defaultVerticalOrigin=None)[source]

Old public name for self._getPhantomPoints(). See: https://github.com/fonttools/fonttools/pull/2266

getCoordinatesAndControls(glyphName, ttFont, defaultVerticalOrigin=None)[source]

Old public name for self._getCoordinatesAndControls(). See: https://github.com/fonttools/fonttools/pull/2266

setCoordinates(glyphName, ttFont)[source]

Old public name for self._setCoordinates(). See: https://github.com/fonttools/fonttools/pull/2266

class fontTools.ttLib.tables._g_l_y_f.Glyph(data=b'')[source]

Bases: object

This class represents an individual TrueType glyph.

TrueType glyph objects come in two flavours: simple and composite. Simple glyph objects contain contours, represented via the .coordinates, .flags, .numberOfContours, and .endPtsOfContours attributes; composite glyphs contain components, available through the .components attributes.

Because the .coordinates attribute (and other simple glyph attributes mentioned above) is only set on simple glyphs and the .components attribute is only set on composite glyphs, it is necessary to use the isComposite() method to test whether a glyph is simple or composite before attempting to access its data.

For a composite glyph, the components can also be accessed via array-like access:

>> assert(font["glyf"]["Aacute"].isComposite())
>> font["glyf"]["Aacute"][0]
<fontTools.ttLib.tables._g_l_y_f.GlyphComponent at 0x1027b2ee0>
recalcBounds(glyfTable, *, boundsDone=None)[source]

Recalculates the bounds of the glyph.

Each glyph object stores its bounding box in the xMin/yMin/xMax/yMax attributes. These bounds must be recomputed when the coordinates change. The table__g_l_y_f bounds must be provided to resolve component bounds.

tryRecalcBoundsComposite(glyfTable, *, boundsDone=None)[source]

Try recalculating the bounds of a composite glyph that has certain constrained properties. Namely, none of the components have a transform other than an integer translate, and none uses the anchor points.

Each glyph object stores its bounding box in the xMin/yMin/xMax/yMax attributes. These bounds must be recomputed when the coordinates change. The table__g_l_y_f bounds must be provided to resolve component bounds.

Return True if bounds were calculated, False otherwise.

isComposite()[source]

Test whether a glyph has components

getCoordinates(glyfTable)[source]

Return the coordinates, end points and flags

This method returns three values: A GlyphCoordinates object, a list of the indexes of the final points of each contour (allowing you to split up the coordinates list into contours) and a list of flags.

On simple glyphs, this method returns information from the glyph’s own contours; on composite glyphs, it “flattens” all components recursively to return a list of coordinates representing all the components involved in the glyph.

To interpret the flags for each point, see the “Simple Glyph Flags” section of the glyf table specification <https://docs.microsoft.com/en-us/typography/opentype/spec/glyf#simple-glyph-description>.

getComponentNames(glyfTable)[source]

Returns a list of names of component glyphs used in this glyph

This method can be used on simple glyphs (in which case it returns an empty list) or composite glyphs.

trim(remove_hinting=False)[source]

Remove padding and, if requested, hinting, from a glyph. This works on both expanded and compacted glyphs, without expanding it.

removeHinting()[source]

Removes TrueType hinting instructions from the glyph.

draw(pen, glyfTable, offset=0)[source]

Draws the glyph using the supplied pen object.

Parameters:
  • pen – An object conforming to the pen protocol.

  • glyfTable – A table__g_l_y_f object, to resolve components.

  • offset (int) – A horizontal offset. If provided, all coordinates are translated by this offset.

drawPoints(pen, glyfTable, offset=0)[source]

Draw the glyph using the supplied pointPen. As opposed to Glyph.draw(), this will not change the point indices.

class fontTools.ttLib.tables._g_l_y_f.GlyphComponent[source]

Bases: object

Represents a component within a composite glyph.

The component is represented internally with four attributes: glyphName, x, y and transform. If there is no “two-by-two” matrix (i.e no scaling, reflection, or rotation; only translation), the transform attribute is not present.

getComponentInfo()[source]

Return information about the component

This method returns a tuple of two values: the glyph name of the component’s base glyph, and a transformation matrix. As opposed to accessing the attributes directly, getComponentInfo always returns a six-element tuple of the component’s transformation matrix, even when the two-by-two .transform matrix is not present.

class fontTools.ttLib.tables._g_l_y_f.GlyphCoordinates(iterable=[])[source]

Bases: object

A list of glyph coordinates.

Unlike an ordinary list, this is a numpy-like matrix object which supports matrix addition, scalar multiplication and other operations described below.

property array

Returns the underlying array of coordinates

static zeros(count)[source]

Creates a new GlyphCoordinates object with all coordinates set to (0,0)

copy()[source]

Creates a new GlyphCoordinates object which is a copy of the current one.

translate(p)[source]
>>> GlyphCoordinates([(1,2)]).translate((.5,0))
scale(p)[source]
>>> GlyphCoordinates([(1,2)]).scale((.5,0))
transform(t)[source]
>>> GlyphCoordinates([(1,2)]).transform(((.5,0),(.2,.5)))