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.

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]
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()