sfnt

ttLib/sfnt.py – low-level module to deal with the sfnt file format.

Defines two public classes:

  • SFNTReader

  • SFNTWriter

(Normally you don’t have to use these classes explicitly; they are used automatically by ttLib.TTFont.)

The reading and writing of sfnt files is separated in two distinct classes, since whenever the number of tables changes or whenever a table’s length changes you need to rewrite the whole file anyway.

class fontTools.ttLib.sfnt.SFNTReader(*args, **kwargs)[source]

Bases: object

has_key(tag)[source]
keys()[source]
close()[source]
fontTools.ttLib.sfnt.compress(data, level=6)[source]

Compress ‘data’ to Zlib format. If ‘USE_ZOPFLI’ variable is True, zopfli is used instead of the zlib module. The compression ‘level’ must be between 0 and 9. 1 gives best speed, 9 gives best compression (0 gives no compression at all). The default value is a compromise between speed and compression (6).

class fontTools.ttLib.sfnt.SFNTWriter(*args, **kwargs)[source]

Bases: object

setEntry(tag, entry)[source]
close()[source]

All tables must have been written to disk. Now write the directory.

writeMasterChecksum(directory)[source]
reordersTables()[source]
class fontTools.ttLib.sfnt.DirectoryEntry[source]

Bases: object

fromFile(file)[source]
fromString(str)[source]
toString()[source]
loadData(file)[source]
saveData(file, data)[source]
decodeData(rawData)[source]
encodeData(data)[source]
class fontTools.ttLib.sfnt.SFNTDirectoryEntry[source]

Bases: DirectoryEntry

format = '\n\t\t> # big endian\n\t\ttag:            4s\n\t\tcheckSum:       L\n\t\toffset:         L\n\t\tlength:         L\n'
formatSize = 16
class fontTools.ttLib.sfnt.WOFFDirectoryEntry[source]

Bases: DirectoryEntry

format = '\n\t\t> # big endian\n\t\ttag:            4s\n\t\toffset:         L\n\t\tlength:         L    # compressed length\n\t\torigLength:     L    # original length\n\t\tcheckSum:       L    # original checksum\n'
formatSize = 20
decodeData(rawData)[source]
encodeData(data)[source]
class fontTools.ttLib.sfnt.WOFFFlavorData(reader=None)[source]

Bases: object

Flavor = 'woff'
fontTools.ttLib.sfnt.calcChecksum(data)[source]

Calculate the checksum for an arbitrary block of data.

If the data length is not a multiple of four, it assumes it is to be padded with null byte.

>>> print(calcChecksum(b"abcd"))
1633837924
>>> print(calcChecksum(b"abcdxyz"))
3655064932
fontTools.ttLib.sfnt.readTTCHeader(file)[source]
fontTools.ttLib.sfnt.writeTTCHeader(file, numFonts)[source]