ttFont

class fontTools.ttLib.ttFont.GlyphOrder(tag=None)[source]

A pseudo table. The glyph order isn’t in the font as a separate table, but it’s nice to present it as such in the TTX format.

fromXML(name, attrs, content, ttFont)[source]
toXML(writer, ttFont)[source]
class fontTools.ttLib.ttFont.TTFont(file=None, res_name_or_index=None, sfntVersion='\x00\x01\x00\x00', flavor=None, checkChecksums=0, verbose=None, recalcBBoxes=True, allowVID=False, ignoreDecompileErrors=False, recalcTimestamp=True, fontNumber=- 1, lazy=None, quiet=None, _tableCache=None)[source]

The main font object. It manages file input and output, and offers a convenient way of accessing tables. Tables will be only decompiled when necessary, ie. when they’re actually accessed. This means that simple operations can be extremely fast.

_buildReverseGlyphOrderDict()[source]
_getGlyphNamesFromCmap()[source]
static _makeGlyphName(codepoint)[source]
_save(file, tableCache=None)[source]

Internal function, to be shared by save() and TTCollection.save()

_saveXML(writer, writeVersion=True, quiet=None, tables=None, skipTables=None, splitTables=False, splitGlyphs=False, disassembleInstructions=True, bitmapGlyphDataFormat='raw')[source]
_tableToXML(writer, tag, quiet=None, splitGlyphs=False)[source]
_writeTable(tag, writer, done, tableCache=None)[source]

Internal helper function for self.save(). Keeps track of inter-table dependencies.

close()[source]

If we still have a reader object, close it.

get(tag, default=None)[source]
getBestCmap(cmapPreferences=((3, 10), (0, 6), (0, 4), (3, 1), (0, 3), (0, 2), (0, 1), (0, 0)))[source]

Return the ‘best’ unicode cmap dictionary available in the font, or None, if no unicode cmap subtable is available.

By default it will search for the following (platformID, platEncID) pairs:

(3, 10), (0, 6), (0, 4), (3, 1), (0, 3), (0, 2), (0, 1), (0, 0)

This can be customized via the cmapPreferences argument.

getGlyphID(glyphName, requireReal=False)[source]
getGlyphName(glyphID, requireReal=False)[source]
getGlyphNames()[source]

Get a list of glyph names, sorted alphabetically.

getGlyphNames2()[source]

Get a list of glyph names, sorted alphabetically, but not case sensitive.

getGlyphOrder()[source]
getGlyphSet(preferCFF=True)[source]

Return a generic GlyphSet, which is a dict-like object mapping glyph names to glyph objects. The returned glyph objects have a .draw() method that supports the Pen protocol, and will have an attribute named ‘width’.

If the font is CFF-based, the outlines will be taken from the ‘CFF ‘ or ‘CFF2’ tables. Otherwise the outlines will be taken from the ‘glyf’ table. If the font contains both a ‘CFF ‘/’CFF2’ and a ‘glyf’ table, you can use the ‘preferCFF’ argument to specify which one should be taken. If the font contains both a ‘CFF ‘ and a ‘CFF2’ table, the latter is taken.

getReverseGlyphMap(rebuild=False)[source]
getTableData(tag)[source]

Returns raw table data, whether compiled or directly read from disk.

has_key(tag)[source]
importXML(fileOrPath, quiet=None)[source]

Import a TTX file (an XML-based text format), so as to recreate a font object.

isLoaded(tag)[source]

Return true if the table identified by ‘tag’ has been decompiled and loaded into memory.

keys()[source]
save(file, reorderTables=True)[source]

Save the font to disk. Similarly to the constructor, the ‘file’ argument can be either a pathname or a writable file object.

saveXML(fileOrPath, newlinestr=None, **kwargs)[source]

Export the font as TTX (an XML-based text file), or as a series of text files when splitTables is true. In the latter case, the ‘fileOrPath’ argument should be a path to a directory. The ‘tables’ argument must either be false (dump all tables) or a list of tables to dump. The ‘skipTables’ argument may be a list of tables to skip, but only when the ‘tables’ argument is false.

setGlyphOrder(glyphOrder)[source]
class fontTools.ttLib.ttFont._TTGlyph(glyphset, glyph, horizontalMetrics, verticalMetrics=None)[source]

Wrapper for a TrueType glyph that supports the Pen protocol, meaning that it has .draw() and .drawPoints() methods that take a pen object as their only argument. Additionally there are ‘width’ and ‘lsb’ attributes, read from the ‘hmtx’ table.

If the font contains a ‘vmtx’ table, there will also be ‘height’ and ‘tsb’ attributes.

draw(pen)[source]

Draw the glyph onto pen. See fontTools.pens.basePen for details how that works.

drawPoints(pen)[source]
class fontTools.ttLib.ttFont._TTGlyphCFF(glyphset, glyph, horizontalMetrics, verticalMetrics=None)[source]
draw(pen)

Draw the glyph onto pen. See fontTools.pens.basePen for details how that works.

drawPoints(pen)
class fontTools.ttLib.ttFont._TTGlyphGlyf(glyphset, glyph, horizontalMetrics, verticalMetrics=None)[source]
draw(pen)[source]

Draw the glyph onto Pen. See fontTools.pens.basePen for details how that works.

drawPoints(pen)[source]

Draw the glyph onto PointPen. See fontTools.pens.pointPen for details how that works.

class fontTools.ttLib.ttFont._TTGlyphSet(ttFont, glyphs, glyphType)[source]

Generic dict-like GlyphSet class that pulls metrics from hmtx and glyph shape from TrueType or CFF.

get(glyphName, default=None)[source]
has_key(glyphName)[source]
keys()[source]
fontTools.ttLib.ttFont._escapechar(c)[source]

Helper function for tagToIdentifier()

fontTools.ttLib.ttFont.getClassTag(klass)[source]

Fetch the table tag for a class object.

fontTools.ttLib.ttFont.getCustomTableClass(tag)[source]

Return the custom table class for tag, if one has been registered with ‘registerCustomTableClass()’. Else return None.

fontTools.ttLib.ttFont.getSearchRange(n, itemSize=16)[source]

Calculate searchRange, entrySelector, rangeShift.

fontTools.ttLib.ttFont.getTableClass(tag)[source]

Fetch the packer/unpacker class for a table.

fontTools.ttLib.ttFont.getTableModule(tag)[source]

Fetch the packer/unpacker module for a table. Return None when no module is found.

fontTools.ttLib.ttFont.identifierToTag(ident)[source]

the opposite of tagToIdentifier()

fontTools.ttLib.ttFont.maxPowerOfTwo(x)[source]

Return the highest exponent of two, so that (2 ** exponent) <= x. Return 0 if x is 0.

fontTools.ttLib.ttFont.newTable(tag)[source]

Return a new instance of a table.

fontTools.ttLib.ttFont.registerCustomTableClass(tag, moduleName, className=None)[source]

Register a custom packer/unpacker class for a table. The ‘moduleName’ must be an importable module. If no ‘className’ is given, it is derived from the tag, for example it will be table_C_U_S_T_ for a ‘CUST’ tag.

The registered table class should be a subclass of fontTools.ttLib.tables.DefaultTable.DefaultTable

fontTools.ttLib.ttFont.reorderFontTables(inFile, outFile, tableOrder=None, checkChecksums=False)[source]

Rewrite a font file, ordering the tables as recommended by the OpenType specification 1.4.

fontTools.ttLib.ttFont.sortedTagList(tagList, tableOrder=None)[source]

Return a sorted copy of tagList, sorted according to the OpenType specification, or according to a custom tableOrder. If given and not None, tableOrder needs to be a list of tag names.

fontTools.ttLib.ttFont.tagToIdentifier(tag)[source]

Convert a table tag to a valid (but UGLY) python identifier, as well as a filename that’s guaranteed to be unique even on a caseless file system. Each character is mapped to two characters. Lowercase letters get an underscore before the letter, uppercase letters get an underscore after the letter. Trailing spaces are trimmed. Illegal characters are escaped as two hex bytes. If the result starts with a number (as the result of a hex escape), an extra underscore is prepended. Examples:

‘glyf’ -> ‘_g_l_y_f’ ‘cvt ‘ -> ‘_c_v_t’ ‘OS/2’ -> ‘O_S_2f_2’

fontTools.ttLib.ttFont.tagToXML(tag)[source]

Similarly to tagToIdentifier(), this converts a TT tag to a valid XML element name. Since XML element names are case sensitive, this is a fairly simple/readable translation.

fontTools.ttLib.ttFont.unregisterCustomTableClass(tag)[source]

Unregister the custom packer/unpacker class for a table.

fontTools.ttLib.ttFont.xmlToTag(tag)[source]

The opposite of tagToXML()