glyf: Glyph Data
- class fontTools.ttLib.tables._g_l_y_f.table__g_l_y_f(tag=None)[source]
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
GlyphSetobject returned fromfontTools.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
glyftable data. Objects of this class support dictionary-like access, mapping glyph names toGlyphobjects:>> glyf = font["glyf"] >> len(glyf["Aacute"].components) 2
Note that when adding glyphs to the font via low-level access to the
glyftable, the new glyphs must also be added to thehmtx/vmtxtable:>> font["glyf"]["divisionslash"] = Glyph() >> font["hmtx"]["divisionslash"] = (640, 0)
- getCoordinatesAndControls(glyphName, ttFont, defaultVerticalOrigin=None)[source]
Old public name for self._getCoordinatesAndControls(). See: https://github.com/fonttools/fonttools/pull/2266
- getGlyphID(glyphName)[source]
Returns the ID of the glyph with the given name.
Raises a
ValueErrorif the glyph is not found in the font.
- getGlyphName(glyphID)[source]
Returns the name for the glyph with the given ID.
Raises a
KeyErrorif the glyph name is not found in the font.
- getPhantomPoints(glyphName, ttFont, defaultVerticalOrigin=None)[source]
Old public name for self._getPhantomPoints(). 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]
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.endPtsOfContoursattributes; composite glyphs contain components, available through the.componentsattributes.Because the
.coordinatesattribute (and other simple glyph attributes mentioned above) is only set on simple glyphs and the.componentsattribute is only set on composite glyphs, it is necessary to use theisComposite()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>
- 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_fobject, 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.
- 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.
- getCoordinates(glyfTable)[source]
Return the coordinates, end points and flags
This method returns three values: A
GlyphCoordinatesobject, 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>.
- class fontTools.ttLib.tables._g_l_y_f.GlyphComponent[source]
Represents a component within a composite glyph.
The component is represented internally with four attributes:
glyphName,x,yandtransform. If there is no “two-by-two” matrix (i.e no scaling, reflection, or rotation; only translation), thetransformattribute 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,
getComponentInfoalways returns a six-element tuple of the component’s transformation matrix, even when the two-by-two.transformmatrix is not present.
- class fontTools.ttLib.tables._g_l_y_f.GlyphCoordinates(iterable=[])[source]
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