TrueType/OpenType Table Modules

This folder is a subpackage of fontTools.ttLib. Each module here is a specialized TT/OT table converter: they can convert raw data to Python objects and vice versa. Usually you don’t need to use the modules directly: they are imported and used automatically when needed by fontTools.ttLib. The tables currently supported are:

The Python modules representing the tables have pretty strange names: this is due to the fact that we need to map TT table tags (which are case sensitive) to filenames (which on Mac and Win aren’t case sensitive) as well as to Python identifiers. The latter means it can only contain [A-Za-z0-9_] and cannot start with a number.

fontTools.ttLib provides functions to expand a tag into the format used here:

>>> from fontTools import ttLib
>>> ttLib.tagToIdentifier("FOO ")
>>> ttLib.tagToIdentifier("cvt ")
>>> ttLib.tagToIdentifier("OS/2")
>>> ttLib.tagToIdentifier("glyf")

And vice versa:

>>> ttLib.identifierToTag("F_O_O_")
'FOO '
>>> ttLib.identifierToTag("_c_v_t")
'cvt '
>>> ttLib.identifierToTag("O_S_2f_2")
>>> ttLib.identifierToTag("_g_l_y_f")

Eg. the ‘glyf’ table converter lives in a Python file called:

The converter itself is a class, named table_ + expandedtag. Eg:

class table__g_l_y_f:

Note that if you _do_ need to use such modules or classes manually, there are two convenient API functions that let you find them by tag:

>>> ttLib.getTableModule('glyf')
<module 'ttLib.tables._g_l_y_f'>
>>> ttLib.getTableClass('glyf')
<class ttLib.tables._g_l_y_f.table__g_l_y_f at 645f400>

ttProgram: TrueType bytecode assembler/disassembler – Assembler/disassembler for TrueType bytecode programs.

class fontTools.ttLib.tables.ttProgram.Program[source]
fromAssembly(assembly: List[str] | str) None[source]
fromBytecode(bytecode: bytes) None[source]
fromXML(name, attrs, content, ttFont) None[source]
getAssembly(preserve=True) List[str][source]
getBytecode() bytes[source]
toXML(writer, ttFont) None[source]
fontTools.ttLib.tables.ttProgram.bitRepr(value, bits)[source]
exception fontTools.ttLib.tables.ttProgram.tt_instructions_error(error)[source]

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Contributing your own table convertors

To add support for a new font table that fontTools does not currently implement, you must subclass from fontTools.ttLib.tables.DefaultTable.DefaultTable. It provides some default behavior, as well as a constructor method (__init__) that you don’t need to override.

Your converter should minimally provide two methods:

class table_F_O_O_(DefaultTable.DefaultTable): # converter for table 'FOO '

    def decompile(self, data, ttFont):
        # 'data' is the raw table data. Unpack it into a
        # Python data structure.
        # 'ttFont' is a ttLib.TTfile instance, enabling you to
        # refer to other tables. Do ***not*** keep a reference to
        # it: it will cause a circular reference (ttFont saves
        # a reference to us), and that means we'll be leaking
        # memory. If you need to use it in other methods, just
        # pass it around as a method argument.

    def compile(self, ttFont):
        # Return the raw data, as converted from the Python
        # data structure.
        # Again, 'ttFont' is there so you can access other tables.
        # Same warning applies.

If you want to support TTX import/export as well, you need to provide two additional methods:

def toXML(self, writer, ttFont):
   # XXX

def fromXML(self, (name, attrs, content), ttFont):
   # XXX