ttLib

fontTools.ttLib – a package for dealing with TrueType fonts.

This package offers translators to convert TrueType fonts to Python objects and vice versa, and additionally from Python to TTX (an XML-based text format) and vice versa.

Example interactive session:

Python 1.5.2c1 (#43, Mar 9 1999, 13:06:43) [CW PPC w/GUSI w/MSL] Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> from fontTools import ttLib >>> tt = ttLib.TTFont(“afont.ttf”) >>> tt[‘maxp’].numGlyphs 242 >>> tt[‘OS/2’].achVendID ‘B&H’ >>> tt[‘head’].unitsPerEm 2048 >>> tt.saveXML(“afont.ttx”) Dumping ‘LTSH’ table... Dumping ‘OS/2’ table... Dumping ‘VDMX’ table... Dumping ‘cmap’ table... Dumping ‘cvt ‘ table... Dumping ‘fpgm’ table... Dumping ‘glyf’ table... Dumping ‘hdmx’ table... Dumping ‘head’ table... Dumping ‘hhea’ table... Dumping ‘hmtx’ table... Dumping ‘loca’ table... Dumping ‘maxp’ table... Dumping ‘name’ table... Dumping ‘post’ table... Dumping ‘prep’ table... >>> tt2 = ttLib.TTFont() >>> tt2.importXML(“afont.ttx”) >>> tt2[‘maxp’].numGlyphs 242 >>>

exception fontTools.ttLib.TTLibError

Bases: exceptions.Exception

class fontTools.ttLib.TTFont(file=None, res_name_or_index=None, sfntVersion='x00x01x00x00', checkChecksums=0, verbose=0, recalcBBoxes=1, allowVID=0, ignoreDecompileErrors=False, fontNumber=-1)

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

If we still have a reader object, close it.

save(file, makeSuitcase=0, reorderTables=1)

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

On the Mac, if makeSuitcase is true, a suitcase (resource fork) file will we made instead of a flat .ttf file.

saveXML(fileOrPath, progress=None, tables=None, skipTables=None, splitTables=0, disassembleInstructions=1)

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.

_tableToXML(writer, tag, progress)
importXML(file, progress=None)

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

isLoaded(tag)

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

has_key(tag)
keys()
setGlyphOrder(glyphOrder)
getGlyphOrder()
_getGlyphNamesFromCmap()
getGlyphNames()

Get a list of glyph names, sorted alphabetically.

getGlyphNames2()

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

getGlyphName(glyphID, requireReal=0)
getGlyphID(glyphName, requireReal=0)
getReverseGlyphMap(rebuild=0)
_buildReverseGlyphOrderDict()
_writeTable(tag, writer, done)

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

getTableData(tag)

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

getGlyphSet(preferCFF=1)

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’, but only after the .draw() method has been called.

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

class fontTools.ttLib._TTGlyphSet(ttFont)

Generic dict-like GlyphSet class, meant as a TrueType counterpart to CFF’s CharString dict. See TTFont.getGlyphSet().

keys()
has_key(glyphName)
get(glyphName, default=None)
class fontTools.ttLib._TTGlyph(glyphName, ttFont)

Wrapper for a TrueType glyph that supports the Pen protocol, meaning that it has a .draw() method that takes a pen object as its only argument. Additionally there is a ‘width’ attribute.

draw(pen)

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

class fontTools.ttLib.GlyphOrder(tag)

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.

toXML(writer, ttFont)
fromXML((name, attrs, content), ttFont)
fontTools.ttLib.getTableModule(tag)

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

fontTools.ttLib.getTableClass(tag)

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

fontTools.ttLib.newTable(tag)

Return a new instance of a table.

fontTools.ttLib._escapechar(c)

Helper function for tagToIdentifier()

fontTools.ttLib.tagToIdentifier(tag)

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.identifierToTag(ident)

the opposite of tagToIdentifier()

fontTools.ttLib.tagToXML(tag)

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.xmlToTag(tag)

The opposite of tagToXML()

fontTools.ttLib.debugmsg(msg)
fontTools.ttLib.sortedTagList(tagList, tableOrder=None)

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.reorderFontTables(inFile, outFile, tableOrder=None, checkChecksums=0)

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