Skip to content

Commit

Permalink
Add feaIncludeDir option to allow override the include search path
Browse files Browse the repository at this point in the history
This is intended to fix googlefonts/glyphsLib#797, by allowing fontmake to override the default include directory used for parsing a UFO features.fea; useful when the UFOs where generated from a .glyphs source whose features contain some `include` statements and they are saved in a different directory (or not saved to disk at all).

fontmake will need to be modified to call ufo2ft with this new option when starting to build from a .glyphs input.
  • Loading branch information
anthrotype committed Jul 22, 2022
1 parent 85def2b commit 1cfb673
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions Lib/ufo2ft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def call_postprocessor(otf, ufo, glyphSet, *, postProcessorClass, **kwargs):
debugFeatureFile=None,
notdefGlyph=None,
colrLayerReuse=True,
feaIncludeDir=None,
)

compileOTF_args = {
Expand Down
27 changes: 22 additions & 5 deletions Lib/ufo2ft/featureCompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
logger = logging.getLogger(__name__)


def parseLayoutFeatures(font):
def parseLayoutFeatures(font, includeDir=None):
"""Parse OpenType layout features in the UFO and return a
feaLib.ast.FeatureFile instance.
includeDir is an optional directory path to search for included
feature files, if omitted the font.path is used. If the latter
is also not set, the feaLib Lexer uses the current working directory.
"""
featxt = font.features.text or ""
if not featxt:
return ast.FeatureFile()
buf = StringIO(featxt)
ufoPath = font.path
includeDir = None
if ufoPath is not None:
if includeDir is None and ufoPath is not None:
# The UFO v3 specification says "Any include() statements must be relative to
# the UFO path, not to the features.fea file itself". We set the `name`
# attribute on the buffer to the actual feature file path, which feaLib will
Expand All @@ -44,6 +47,7 @@ def parseLayoutFeatures(font):
buf.name = os.path.join(ufoPath, "features.fea")
includeDir = os.path.dirname(ufoPath)
glyphNames = set(font.keys())
includeDir = os.path.normpath(includeDir) if includeDir else None
try:
parser = Parser(buf, glyphNames, includeDir=includeDir)
doc = parser.parse()
Expand Down Expand Up @@ -157,7 +161,15 @@ class FeatureCompiler(BaseFeatureCompiler):
CursFeatureWriter,
]

def __init__(self, ufo, ttFont=None, glyphSet=None, featureWriters=None, **kwargs):
def __init__(
self,
ufo,
ttFont=None,
glyphSet=None,
featureWriters=None,
feaIncludeDir=None,
**kwargs,
):
"""
Args:
featureWriters: a list of BaseFeatureWriter subclasses or
Expand All @@ -177,9 +189,14 @@ def __init__(self, ufo, ttFont=None, glyphSet=None, featureWriters=None, **kwarg
which gets replaced by either the UFO.lib writers or the default
ones; thus one can insert additional writers either before or after
these.
feaIncludeDir: a directory to be used as the include directory for
the feature file. If None, the include directory is set to the
parent directory of the UFO, provided the UFO has a path.
"""
BaseFeatureCompiler.__init__(self, ufo, ttFont, glyphSet)

self.feaIncludeDir = feaIncludeDir

self.initFeatureWriters(featureWriters)

if kwargs.get("mtiFeatures") is not None:
Expand Down Expand Up @@ -259,7 +276,7 @@ def setupFeatures(self):
in a different way if desired.
"""
if self.featureWriters:
featureFile = parseLayoutFeatures(self.ufo)
featureFile = parseLayoutFeatures(self.ufo, self.feaIncludeDir)

for writer in self.featureWriters:
writer.write(self.ufo, featureFile, compiler=self)
Expand Down

0 comments on commit 1cfb673

Please sign in to comment.