Skip to content

Commit

Permalink
fontBuilder: add glyphDataFormat=0; error with accidentally cubic out…
Browse files Browse the repository at this point in the history
…lines

Fixes #3113
  • Loading branch information
anthrotype committed May 23, 2023
1 parent 65bc610 commit bf87632
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions Lib/fontTools/fontBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def drawTestGlyph(pen):

from .ttLib import TTFont, newTable
from .ttLib.tables._c_m_a_p import cmap_classes
from .ttLib.tables._g_l_y_f import flagCubic
from .misc.timeTools import timestampNow
import struct
from collections import OrderedDict
Expand Down Expand Up @@ -319,23 +320,39 @@ def drawTestGlyph(pen):


class FontBuilder(object):
def __init__(self, unitsPerEm=None, font=None, isTTF=True):
def __init__(self, unitsPerEm=None, font=None, isTTF=True, glyphDataFormat=0):
"""Initialize a FontBuilder instance.
If the `font` argument is not given, a new `TTFont` will be
constructed, and `unitsPerEm` must be given. If `isTTF` is True,
the font will be a glyf-based TTF; if `isTTF` is False it will be
a CFF-based OTF.
The `glyphDataFormat` argument corresponds to the `head` table field
that defines the format of the TrueType `glyf` table (default=0).
TrueType glyphs historically can only contain quadratic splines and static
components, but there's a proposal to add support for cubic Bezier curves as well
as variable composites/components at
https://github.com/harfbuzz/boring-expansion-spec/blob/main/glyf1.md
You can experiment with the new features by setting `glyphDataFormat` to 1.
A ValueError is raised if `glyphDataFormat` is left at 0 but glyphs are added
that contain cubic splines or varcomposites. This is to prevent accidentally
creating fonts that are incompatible with existing TrueType implementations.
If `font` is given, it must be a `TTFont` instance and `unitsPerEm`
must _not_ be given. The `isTTF` argument will be ignored.
must _not_ be given. The `isTTF` and `glyphDataFormat` arguments will be ignored.
"""
if font is None:
self.font = TTFont(recalcTimestamp=False)
self.isTTF = isTTF
now = timestampNow()
assert unitsPerEm is not None
self.setupHead(unitsPerEm=unitsPerEm, created=now, modified=now)
self.setupHead(
unitsPerEm=unitsPerEm,
create=now,
modified=now,
glyphDataForma=glyphDataFormat,
)
self.setupMaxp()
else:
assert unitsPerEm is None
Expand Down Expand Up @@ -639,8 +656,25 @@ def setupGlyf(self, glyphs, calcGlyphBounds=True):
If `calcGlyphBounds` is True, the bounds of all glyphs will be
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).
"""
assert self.isTTF

glyphDataFormat = self.font["head"].glyphDataFormat
if glyphDataFormat == 0:
for name, g in glyphs.items():
if g.isVarComposite():
raise ValueError(

Check warning on line 669 in Lib/fontTools/fontBuilder.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/fontBuilder.py#L669

Added line #L669 was not covered by tests
f"Glyph {name!r} is a variable composite, but glyphDataFormat=0"
)
elif g.numberOfContours > 0 and any(f & flagCubic for f in g.flags):
raise ValueError(

Check warning on line 673 in Lib/fontTools/fontBuilder.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/fontBuilder.py#L673

Added line #L673 was not covered by tests
f"Glyph {name!r} has cubic Bezier outlines, but glyphDataFormat=0; "
"either convert to quadratics with cu2qu or set glyphDataFormat=1."
)

self.font["loca"] = newTable("loca")
self.font["glyf"] = newTable("glyf")
self.font["glyf"].glyphs = glyphs
Expand Down

0 comments on commit bf87632

Please sign in to comment.