woff2

class fontTools.ttLib.woff2.WOFF2DirectoryEntry[source]
decodeData(rawData)
encodeData(data)
fromFile(file)[source]
fromString(data)[source]
loadData(file)
saveData(file, data)
toString()[source]
property transformVersion

Return bits 6-7 of table entry’s flags, which indicate the preprocessing transformation version number (between 0 and 3).

property transformed

Return True if the table has any transformation, else return False.

class fontTools.ttLib.woff2.WOFF2FlavorData(reader=None, data=None, transformedTables=None)[source]
Flavor = 'woff2'
class fontTools.ttLib.woff2.WOFF2GlyfTable(tag=None)[source]

Decoder/Encoder for WOFF2 ‘glyf’ table transform.

closure_glyphs(s)
compile(ttFont)
decompile(data, ttFont)
dependencies = []
fromXML(name, attrs, content, ttFont)
getCoordinatesAndControls(glyphName, ttFont, defaultVerticalOrigin=None)

Return glyph coordinates and controls as expected by “gvar” table.

The coordinates includes four “phantom points” for the glyph metrics, as mandated by the “gvar” spec.

The glyph controls is a namedtuple with the following attributes:
  • numberOfContours: -1 for composite glyphs.

  • endPts: list of indices of end points for each contour in simple

glyphs, or component indices in composite glyphs (used for IUP optimization). - flags: array of contour point flags for simple glyphs (None for composite glyphs). - components: list of base glyph names (str) for each component in composite glyphs (None for simple glyphs).

The “ttFont” and “defaultVerticalOrigin” args are used to compute the “phantom points” (see “getPhantomPoints” method).

Return None if the requested glyphName is not present.

getGlyphID(glyphName)
getGlyphName(glyphID)
getPhantomPoints(glyphName, ttFont, defaultVerticalOrigin=None)

Compute the four “phantom points” for the given glyph from its bounding box and the horizontal and vertical advance widths and sidebearings stored in the ttFont’s “hmtx” and “vmtx” tables.

If the ttFont doesn’t contain a “vmtx” table, the hhea.ascent is used as the vertical origin, and the head.unitsPerEm as the vertical advance.

The “defaultVerticalOrigin” (Optional[int]) is needed when the ttFont contains neither a “vmtx” nor an “hhea” table, as may happen with ‘sparse’ masters. The value should be the hhea.ascent of the default master.

https://docs.microsoft.com/en-us/typography/opentype/spec/tt_instructing_glyphs#phantoms

has_key(glyphName)
keys()
merge(m, tables)
mergeMap = {'glyphOrder': <function sumLists>, 'glyphs': <function sumDicts>, 'tableTag': <function equal>}
padding = 1
prune_post_subset(font, options)
prune_pre_subset(font, options)
reconstruct(data, ttFont)[source]

Decompile transformed ‘glyf’ data.

removeHinting()
setCoordinates(glyphName, coord, ttFont)

Set coordinates and metrics for the given glyph.

“coord” is an array of GlyphCoordinates which must include the “phantom points” as the last four coordinates.

Both the horizontal/vertical advances and left/top sidebearings in “hmtx” and “vmtx” tables (if any) are updated from four phantom points and the glyph’s bounding boxes.

setGlyphOrder(glyphOrder)
subStreams = ('nContourStream', 'nPointsStream', 'flagStream', 'glyphStream', 'compositeStream', 'bboxStream', 'instructionStream')
subset_glyphs(s)
toXML(writer, ttFont, splitGlyphs=False)
transform(ttFont)[source]

Return transformed ‘glyf’ data

class fontTools.ttLib.woff2.WOFF2HmtxTable(tag=None)[source]
advanceName = 'width'
compile(ttFont)
decompile(data, ttFont)
dependencies = []
fromXML(name, attrs, content, ttFont)
headerTag = 'hhea'
longMetricFormat = 'Hh'
merge(m, tables)
mergeMap = {'metrics': <function sumDicts>, 'tableTag': <function equal>}
numberOfMetricsName = 'numberOfHMetrics'
reconstruct(data, ttFont)[source]
sideBearingName = 'lsb'
subset_glyphs(s)
toXML(writer, ttFont)
transform(ttFont)[source]
class fontTools.ttLib.woff2.WOFF2LocaTable(tag=None)[source]

Same as parent class. The only difference is that it attempts to preserve the ‘indexFormat’ as encoded in the WOFF2 glyf table.

compile(ttFont)[source]
decompile(data, ttFont)
dependencies = ['glyf']
fromXML(name, attrs, content, ttFont)
merge(m, tables)
mergeMap = {'*': <function recalculate>, 'tableTag': <function equal>}
set(locations)
toXML(writer, ttFont)
class fontTools.ttLib.woff2.WOFF2Reader(*args, **kwargs)[source]
close()
flavor = 'woff2'
has_key(tag)
keys()
reconstructTable(tag)[source]

Reconstruct table named ‘tag’ from transformed data.

class fontTools.ttLib.woff2.WOFF2Writer(*args, **kwargs)[source]
close()[source]

All tags must have been specified. Now write the table data and directory.

flavor = 'woff2'
reordersTables()[source]
setEntry(tag, entry)
transformTable(tag)[source]

Return transformed table data, or None if some pre-conditions aren’t met – in which case, the non-transformed table data will be used.

writeMasterChecksum()[source]

Write checkSumAdjustment to the transformBuffer.

fontTools.ttLib.woff2.base128Size(n)[source]

Return the length in bytes of a UIntBase128-encoded sequence with value n.

>>> base128Size(0)
1
>>> base128Size(24567)
3
>>> base128Size(2**32-1)
5
fontTools.ttLib.woff2.compress(input_file, output_file, transform_tables=None)[source]

Compress OpenType font to WOFF2.

Parameters
  • input_file – a file path, file or file-like object (open in binary mode) containing an OpenType font (either CFF- or TrueType-flavored).

  • output_file – a file path, file or file-like object where to save the compressed WOFF2 font.

  • transform_tables – Optional[Iterable[str]]: a set of table tags for which to enable preprocessing transformations. By default, only ‘glyf’ and ‘loca’ tables are transformed. An empty set means disable all transformations.

fontTools.ttLib.woff2.decompress(input_file, output_file)[source]

Decompress WOFF2 font to OpenType font.

Parameters
  • input_file – a file path, file or file-like object (open in binary mode) containing a compressed WOFF2 font.

  • output_file – a file path, file or file-like object where to save the decompressed OpenType font.

fontTools.ttLib.woff2.getKnownTagIndex(tag)[source]

Return index of ‘tag’ in woff2KnownTags list. Return 63 if not found.

fontTools.ttLib.woff2.main(args=None)[source]

Compress and decompress WOFF2 fonts

fontTools.ttLib.woff2.pack255UShort(value)[source]

Encode unsigned integer in range 0 to 65535 (inclusive) to a bytestring using 255UInt16 variable-length encoding.

>>> pack255UShort(252) == b'\xfc'
True
>>> pack255UShort(506) == b'\xfe\x00'
True
>>> pack255UShort(762) == b'\xfd\x02\xfa'
True
fontTools.ttLib.woff2.packBase128(n)[source]

Encode unsigned integer in range 0 to 2**32-1 (inclusive) to a string of bytes using UIntBase128 variable-length encoding. Produce the shortest possible encoding.

>>> packBase128(63) == b"\x3f"
True
>>> packBase128(2**32-1) == b'\x8f\xff\xff\xff\x7f'
True
fontTools.ttLib.woff2.unpack255UShort(data)[source]

Read one to three bytes from 255UInt16-encoded input string, and return a tuple containing the decoded integer plus any leftover data.

>>> unpack255UShort(bytechr(252))[0]
252

Note that some numbers (e.g. 506) can have multiple encodings: >>> unpack255UShort(struct.pack(“BB”, 254, 0))[0] 506 >>> unpack255UShort(struct.pack(“BB”, 255, 253))[0] 506 >>> unpack255UShort(struct.pack(“BBB”, 253, 1, 250))[0] 506

fontTools.ttLib.woff2.unpackBase128(data)[source]

Read one to five bytes from UIntBase128-encoded input string, and return a tuple containing the decoded integer plus any leftover data.

>>> unpackBase128(b'\x3f\x00\x00') == (63, b"\x00\x00")
True
>>> unpackBase128(b'\x8f\xff\xff\xff\x7f')[0] == 4294967295
True
>>> unpackBase128(b'\x80\x80\x3f')  
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TTLibError: UIntBase128 value must not start with leading zeros
>>> unpackBase128(b'\x8f\xff\xff\xff\xff\x7f')[0]  
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TTLibError: UIntBase128-encoded sequence is longer than 5 bytes
>>> unpackBase128(b'\x90\x80\x80\x80\x00')[0]  
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TTLibError: UIntBase128 value exceeds 2**32-1