Skip to content

Instantly share code, notes, and snippets.

@simoncozens
Created July 28, 2022 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simoncozens/0c9455d97951b268810e05ef19051898 to your computer and use it in GitHub Desktop.
Save simoncozens/0c9455d97951b268810e05ef19051898 to your computer and use it in GitHub Desktop.
ufo2ft rust precompilation patch
diff --git a/Lib/ufo2ft/__init__.py b/Lib/ufo2ft/__init__.py
index 4295caa..a98528a 100644
--- a/Lib/ufo2ft/__init__.py
+++ b/Lib/ufo2ft/__init__.py
@@ -1,12 +1,15 @@
import logging
import os
from enum import IntEnum
+import tempfile
from fontTools import varLib
from fontTools.designspaceLib import DesignSpaceDocument
from fontTools.designspaceLib.split import splitInterpolable, splitVariableFonts
from fontTools.otlLib.optimize.gpos import GPOS_COMPACT_MODE_ENV_KEY
+from glyphcompiler import compile as precompile_glyphs
+
from ufo2ft.constants import SPARSE_OTF_MASTER_TABLES, SPARSE_TTF_MASTER_TABLES
from ufo2ft.errors import InvalidDesignSpaceData
from ufo2ft.featureCompiler import (
@@ -14,9 +17,14 @@ from ufo2ft.featureCompiler import (
FeatureCompiler,
MtiFeatureCompiler,
)
-from ufo2ft.outlineCompiler import OutlineOTFCompiler, OutlineTTFCompiler
+from ufo2ft.outlineCompiler import (
+ OutlineOTFCompiler,
+ OutlineTTFCompiler,
+ PrecompiledTTFCompiler,
+)
from ufo2ft.postProcessor import PostProcessor
from ufo2ft.preProcessor import (
+ BasePreProcessor,
OTFPreProcessor,
TTFInterpolatablePreProcessor,
TTFPreProcessor,
@@ -26,6 +34,7 @@ from ufo2ft.util import (
ensure_all_sources_have_names,
init_kwargs,
prune_unknown_kwargs,
+ _GlyphSet,
)
try:
@@ -260,6 +269,7 @@ compileInterpolatableTTFs_args = {
flattenComponents=False,
layerNames=None,
colrLayerReuse=False,
+ precompiledGlyphs=None,
),
}
@@ -294,15 +304,36 @@ def compileInterpolatableTTFs(ufos, **kwargs):
kwargs["layerNames"] = [None] * len(ufos)
assert len(ufos) == len(kwargs["layerNames"])
- glyphSets = call_preprocessor(ufos, **kwargs)
+ if kwargs["precompiledGlyphs"]:
+ kwargs["outlineCompilerClass"] = PrecompiledTTFCompiler
+ kwargs["preProcessorClass"] = BasePreProcessor
+ precompiledGlyphs = kwargs["precompiledGlyphs"]
+ glyphSets = [
+ _GlyphSet.from_layer(
+ ufo,
+ layerName,
+ copy=not kwargs["inplace"],
+ skipExportGlyphs=kwargs["skipExportGlyphs"],
+ )
+ for ufo, layerName in zip(ufos, kwargs["layerNames"])
+ ]
+ else:
+ precompiledGlyphs = None
+ glyphSets = call_preprocessor(ufos, **kwargs)
- for ufo, glyphSet, layerName in zip(ufos, glyphSets, kwargs["layerNames"]):
+ for ix, (ufo, glyphSet, layerName) in enumerate(
+ zip(ufos, glyphSets, kwargs["layerNames"])
+ ):
fontName = _LazyFontName(ufo)
if layerName is not None:
logger.info("Building OpenType tables for %s-%s", fontName, layerName)
else:
logger.info("Building OpenType tables for %s", fontName)
+ if precompiledGlyphs:
+ kwargs["precompiledGlyphs"] = {
+ k: v[ix] for k, v in precompiledGlyphs.items()
+ }
ttf = call_outline_compiler(
ufo,
glyphSet,
@@ -375,6 +406,16 @@ def compileInterpolatableTTFsFromDS(designSpaceDoc, **kwargs):
if kwargs["notdefGlyph"] is None:
kwargs["notdefGlyph"] = _getDefaultNotdefGlyph(designSpaceDoc)
+ if True: # Basically if the filter set is "predictable"
+ # Oh, at this point, I don't actually know where it lives. It's just a text file, let's save it.
+ root = os.path.commonpath([s.path for s in designSpaceDoc.sources])
+ tmpname = root + "/tmp.designspace"
+ glyphset = (
+ designSpaceDoc.findDefault().font.keys()
+ ) # Forget skipExportGlyphs for now
+ designSpaceDoc.write(tmpname)
+ kwargs["precompiledGlyphs"] = precompile_glyphs(tmpname, list(glyphset))
+
ttfs = compileInterpolatableTTFs(ufos, **kwargs)
if kwargs["inplace"]:
diff --git a/Lib/ufo2ft/outlineCompiler.py b/Lib/ufo2ft/outlineCompiler.py
index 424f706..943255b 100644
--- a/Lib/ufo2ft/outlineCompiler.py
+++ b/Lib/ufo2ft/outlineCompiler.py
@@ -1508,6 +1508,28 @@ class OutlineTTFCompiler(BaseOutlineCompiler):
break
+class PrecompiledTTFCompiler(OutlineTTFCompiler):
+ def __init__(
+ self,
+ font,
+ glyphSet=None,
+ glyphOrder=None,
+ tables=None,
+ notdefGlyph=None,
+ colrLayerReuse=True,
+ precompiledGlyphs=None,
+ ):
+ super().__init__(
+ font,
+ glyphSet=glyphSet,
+ glyphOrder=glyphOrder,
+ tables=tables,
+ notdefGlyph=notdefGlyph,
+ colrLayerReuse=colrLayerReuse,
+ )
+ self._compiledGlyphs = precompiledGlyphs
+
+
class StubGlyph:
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment