# sfnt: Read and write the SFNT file format

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)

  Bases: `object`

  has_key(tag: str | bytes) → bool
  keys() → KeysView[Tag]
  close() → None

fontTools.ttLib.sfnt.compress(data, level=6)

  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)

  Bases: `object`

  setEntry(tag, entry)
  close()

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

  writeMasterChecksum(directory)
  reordersTables()

class fontTools.ttLib.sfnt.DirectoryEntry

  Bases: `object`

  fromFile(file)
  fromString(str)
  toString()
  loadData(file)
  saveData(file, data)
  decodeData(rawData)
  encodeData(data)

class fontTools.ttLib.sfnt.SFNTDirectoryEntry

  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

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

class fontTools.ttLib.sfnt.WOFFFlavorData(reader=None)

  Bases: `object`

  Flavor = 'woff'

fontTools.ttLib.sfnt.calcChecksum(data)

  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)

fontTools.ttLib.sfnt.writeTTCHeader(file, numFonts)