Skip to content

Commit

Permalink
[interpolatable] WIP use uharfbuzz if available
Browse files Browse the repository at this point in the history
For faster drawing. However, I'm hitting a lot of
node-count-differs errors.  I have not tracked the
cause down yet.

I thought this line might be:

https://github.com/harfbuzz/harfbuzz/blob/df635ab78ae1d397396e3a0fe688b8a4674b2e8a/src/hb-draw.hh#L148

But disabling it in uharfbuzz didn't help.
  • Loading branch information
behdad committed Nov 17, 2023
1 parent 9c08d9b commit a97f6b4
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions Lib/fontTools/varLib/interpolatable.py
Expand Up @@ -425,13 +425,25 @@ def test(*args, **kwargs):
return problems


def recursivelyAddGlyph(glyphname, glyphset, ttGlyphSet, glyf):
def recursivelyAddGlyph(glyphname, glyphset, ttGlyphSet, hbFont, glyf, reverseGlyphMap):
if glyphname in glyphset:
return
glyphset[glyphname] = ttGlyphSet[glyphname]

for component in getattr(glyf[glyphname], "components", []):
recursivelyAddGlyph(component.glyphName, glyphset, ttGlyphSet, glyf)
glyph = glyf[glyphname]
components = getattr(glyph, "components", [])

# Can't use hb for composite glyphs, because it doesn't
# emit addComponent() calls.
if not components and hbFont is not None:
recording = RecordingPen()
hbFont.draw_glyph_with_pen(reverseGlyphMap[glyphname], recording)
recording.draw = recording.replay
glyphset[glyphname] = recording
else:
glyphset[glyphname] = ttGlyphSet[glyphname]

for component in components:
recursivelyAddGlyph(component.glyphName, glyphset, ttGlyphSet, hbFont, glyf, reverseGlyphMap)


def main(args=None):
Expand Down Expand Up @@ -543,6 +555,14 @@ def main(args=None):
if "gvar" in font:
# Is variable font

reverseGlyphMap = font.getReverseGlyphMap()

try:
import uharfbuzz as hb
hbFace = hb.Face(hb.Blob.from_file_path(args.inputs[0]))
except ImportError:
hbFace = None

axisMapping = {}
fvar = font["fvar"]
for axis in fvar.axes:
Expand All @@ -564,6 +584,7 @@ def main(args=None):
glyf = font["glyf"]
# Gather all glyphs at their "master" locations
ttGlyphSets = {}
hbFonts = {}
glyphsets = defaultdict(dict)

if glyphs is None:
Expand All @@ -581,9 +602,17 @@ def main(args=None):
ttGlyphSets[locTuple] = font.getGlyphSet(
location=locDict, normalized=True
)
if locTuple not in hbFonts:
if hbFace is not None:
hbFont = hb.Font(hbFace)
hbCoords = [var.axes.get(axis.axisTag, (0,0,0))[1] for axis in fvar.axes]
hbFont.set_var_coords_normalized(hbCoords)
hbFonts[locTuple] = hbFont
else:
hbFonts[locTuple] = None

recursivelyAddGlyph(
glyphname, glyphsets[locTuple], ttGlyphSets[locTuple], glyf
glyphname, glyphsets[locTuple], ttGlyphSets[locTuple], hbFonts[locTuple], glyf, reverseGlyphMap
)

names = ["''"]
Expand Down

0 comments on commit a97f6b4

Please sign in to comment.