From 469c9ad963294a3acc2f48f16171dfdb131d0af1 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Tue, 23 May 2023 13:16:02 +0100 Subject: [PATCH] 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... --- Lib/fontTools/fontBuilder.py | 9 ++++----- Tests/fontBuilder/fontBuilder_test.py | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/fontBuilder.py b/Lib/fontTools/fontBuilder.py index 58ecce43a..8bbcc6ac5 100644 --- a/Lib/fontTools/fontBuilder.py +++ b/Lib/fontTools/fontBuilder.py @@ -648,7 +648,7 @@ class FontBuilder(object): for fontDict in topDict.FDArray: 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 to `fontTools.ttLib.tables._g_l_y_f.Glyph` objects, for example as made by `fontTools.pens.ttGlyphPen.TTGlyphPen`. @@ -657,13 +657,12 @@ class FontBuilder(object): calculated. Only pass False if your glyph objects already have their bounding box values set. - Raises ValueError if any of the glyphs contains cubic curves or is a variable - composite but the `head` table has `glyphDataFormat` set to 0 (instead of 1). + If `checkFormat` is True, raise ValueError if any of the glyphs contains + cubic curves or is a variable composite but head.glyphDataFormat=0. """ assert self.isTTF - glyphDataFormat = self.font["head"].glyphDataFormat - if glyphDataFormat == 0: + if checkFormat and self.font["head"].glyphDataFormat == 0: for name, g in glyphs.items(): if g.isVarComposite(): raise ValueError( diff --git a/Tests/fontBuilder/fontBuilder_test.py b/Tests/fontBuilder/fontBuilder_test.py index 589bc676b..6131a972c 100644 --- a/Tests/fontBuilder/fontBuilder_test.py +++ b/Tests/fontBuilder/fontBuilder_test.py @@ -145,12 +145,16 @@ def test_build_cubic_ttf(tmp_path): glyph = pen.glyph() glyphs = {"A": glyph} + # cubic outlines are not allowed in glyf table format 0 fb = FontBuilder(1000, isTTF=True, glyphDataFormat=0) with pytest.raises( ValueError, match="Glyph 'A' has cubic Bezier outlines, but glyphDataFormat=0" ): 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.setupGlyf(glyphs) assert "A" in fb.font["glyf"].glyphs