See pull request: https://github.com/fonttools/fonttools/pull/2326 The new module `otlLib.optimize.gpos` provides `compact` functions that can reduce the file size of GPOS PairPos lookups by splitting subtables in a smart way to avoid storing zero-valued pairs. The compaction methods are called from `otlLib.builder` and `varLib.merger` so that static and variable fonts can benefit from the optimization at compile time. The new module `otlLib.optimize` is also executable, to allow running the optimization on existing fonts. The optimization is a trade-off because on the one hand it can reduce significantly the byte size of the GPOS table (up to 50% in random Google Fonts) but on the other hand it adds to the compilation time and may make fonts very slightly bigger once compressed to WOFF2 (because WOFF2 doesn't mind about zero values and compresses them very well). As such, the optimization is off by default, and you can activate it by providing the environment variable `FONTTOOLS_GPOS_COMPACT_MODE=5` for example (values from 0 = off to 9 = max file size savings, but many more subtables).
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
from pathlib import Path
|
|
from subprocess import run
|
|
|
|
from fontTools.ttLib import TTFont, newTable
|
|
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
|
|
|
|
|
|
def test_main(tmpdir: Path):
|
|
"""Check that calling the main function on an input TTF works."""
|
|
glyphs = ".notdef space A B C a b c".split()
|
|
features = """
|
|
lookup GPOS_EXT useExtension {
|
|
pos a b -10;
|
|
} GPOS_EXT;
|
|
|
|
feature kern {
|
|
pos A 20;
|
|
pos A B -50;
|
|
pos A B' 10 C;
|
|
lookup GPOS_EXT;
|
|
} kern;
|
|
"""
|
|
font = TTFont()
|
|
font.setGlyphOrder(glyphs)
|
|
addOpenTypeFeaturesFromString(font, features)
|
|
font["maxp"] = maxp = newTable("maxp")
|
|
maxp.tableVersion = 0x00010000
|
|
maxp.maxZones = 1
|
|
maxp.maxTwilightPoints = 0
|
|
maxp.maxStorage = 0
|
|
maxp.maxFunctionDefs = 0
|
|
maxp.maxInstructionDefs = 0
|
|
maxp.maxStackElements = 0
|
|
maxp.maxSizeOfInstructions = 0
|
|
maxp.maxComponentElements = 0
|
|
maxp.maxPoints = 0
|
|
maxp.maxContours = 0
|
|
maxp.maxCompositePoints = 0
|
|
maxp.maxCompositeContours = 0
|
|
maxp.maxComponentDepth = 0
|
|
maxp.compile(font)
|
|
input = tmpdir / "in.ttf"
|
|
font.save(str(input))
|
|
output = tmpdir / "out.ttf"
|
|
run(["fonttools", "otlLib.optimize", str(input), "-o", str(output)], check=True)
|
|
assert output.exists()
|