add option to skip glyphDataFormat check for speed

If one is certain that the glyph data has compatible format and prefers not to wait for each glyph flag to checked...
This commit is contained in:
Cosimo Lupo 2023-05-23 13:16:02 +01:00
parent 9bb3b652b0
commit 469c9ad963
No known key found for this signature in database
GPG Key ID: DF65A8A5A119C9A8
2 changed files with 8 additions and 5 deletions

View File

@ -648,7 +648,7 @@ class FontBuilder(object):
for fontDict in topDict.FDArray: for fontDict in topDict.FDArray:
fontDict.Private.vstore = vstore fontDict.Private.vstore = vstore
def setupGlyf(self, glyphs, calcGlyphBounds=True): def setupGlyf(self, glyphs, calcGlyphBounds=True, checkFormat=True):
"""Create the `glyf` table from a dict, that maps glyph names """Create the `glyf` table from a dict, that maps glyph names
to `fontTools.ttLib.tables._g_l_y_f.Glyph` objects, for example to `fontTools.ttLib.tables._g_l_y_f.Glyph` objects, for example
as made by `fontTools.pens.ttGlyphPen.TTGlyphPen`. as made by `fontTools.pens.ttGlyphPen.TTGlyphPen`.
@ -657,13 +657,12 @@ class FontBuilder(object):
calculated. Only pass False if your glyph objects already have calculated. Only pass False if your glyph objects already have
their bounding box values set. their bounding box values set.
Raises ValueError if any of the glyphs contains cubic curves or is a variable If `checkFormat` is True, raise ValueError if any of the glyphs contains
composite but the `head` table has `glyphDataFormat` set to 0 (instead of 1). cubic curves or is a variable composite but head.glyphDataFormat=0.
""" """
assert self.isTTF assert self.isTTF
glyphDataFormat = self.font["head"].glyphDataFormat if checkFormat and self.font["head"].glyphDataFormat == 0:
if glyphDataFormat == 0:
for name, g in glyphs.items(): for name, g in glyphs.items():
if g.isVarComposite(): if g.isVarComposite():
raise ValueError( raise ValueError(

View File

@ -145,12 +145,16 @@ def test_build_cubic_ttf(tmp_path):
glyph = pen.glyph() glyph = pen.glyph()
glyphs = {"A": glyph} glyphs = {"A": glyph}
# cubic outlines are not allowed in glyf table format 0
fb = FontBuilder(1000, isTTF=True, glyphDataFormat=0) fb = FontBuilder(1000, isTTF=True, glyphDataFormat=0)
with pytest.raises( with pytest.raises(
ValueError, match="Glyph 'A' has cubic Bezier outlines, but glyphDataFormat=0" ValueError, match="Glyph 'A' has cubic Bezier outlines, but glyphDataFormat=0"
): ):
fb.setupGlyf(glyphs) fb.setupGlyf(glyphs)
# can skip check if feeling adventurous
fb.setupGlyf(glyphs, checkFormat=False)
# cubics are (will be) allowed in glyf table format 1
fb = FontBuilder(1000, isTTF=True, glyphDataFormat=1) fb = FontBuilder(1000, isTTF=True, glyphDataFormat=1)
fb.setupGlyf(glyphs) fb.setupGlyf(glyphs)
assert "A" in fb.font["glyf"].glyphs assert "A" in fb.font["glyf"].glyphs