Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feaIncludeDir option to allow override the include search path #637

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
66 changes: 66 additions & 0 deletions tests/featureCompiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ def test_include_not_found(self, FontClass, tmpdir, caplog):
assert len(caplog.records) == 1
assert "change the file name in the include" in caplog.text

def test_include_dir(self, FontClass, tmp_path, caplog):
features_dir = tmp_path / "features"
features_dir.mkdir()
(features_dir / "test.fea").write_text(
dedent(
"""\
# hello world
"""
),
encoding="utf-8",
)
ufo = FontClass()
ufo.features.text = dedent(
"""\
include(test.fea)
"""
)
ufo.save(tmp_path / "Test.ufo")

fea = parseLayoutFeatures(ufo, features_dir)

assert "# hello world" in str(fea)


class DummyFeatureWriter:
tableTag = "GPOS"
Expand Down Expand Up @@ -277,3 +300,46 @@ def test_buildTables_FeatureLibError(self, FontClass, caplog):
finally:
if tmpfile is not None:
tmpfile.remove(ignore_errors=True)

def test_setupFeatures_custom_feaIncludeDir(self, FontClass, tmp_path):
(tmp_path / "family.fea").write_text(
"""\
feature liga {
sub f f by f_f;
} liga;
"""
)
ufo = FontClass()
ufo.newGlyph("a")
ufo.newGlyph("v")
ufo.newGlyph("f")
ufo.newGlyph("f_f")
ufo.kerning.update({("a", "v"): -40})
ufo.features.text = dedent(
"""\
include(family.fea);
"""
)
compiler = FeatureCompiler(ufo, feaIncludeDir=str(tmp_path))

compiler.setupFeatures()

assert compiler.features == dedent(
"""\
feature liga {
sub f f by f_f;
} liga;


lookup kern_Common {
lookupflag IgnoreMarks;
pos a v -40;
} kern_Common;

feature kern {
script DFLT;
language dflt;
lookup kern_Common;
} kern;
"""
)
12 changes: 12 additions & 0 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import os
import sys
from pathlib import Path

import pytest
from fontTools.pens.boundsPen import BoundsPen
Expand Down Expand Up @@ -99,6 +100,17 @@ def test_included_features(self, FontClass):
ttf = compileTTF(ufo)
expectTTX(ttf, "Bug108.ttx", tables=self._layoutTables)

def test_included_features_with_custom_include_dir(self, FontClass, tmp_path):
ufo = FontClass(getpath("Bug108.ufo"))
features_dir = tmp_path / "features"
features_dir.mkdir()
(features_dir / "foobarbaz.fea").write_text(
Path(getpath("Bug108_included.fea")).read_text()
)
ufo.features.text = "include(features/foobarbaz.fea);"
ttf = compileTTF(ufo, feaIncludeDir=tmp_path)
expectTTX(ttf, "Bug108.ttx", tables=self._layoutTables)

def test_mti_features(self, FontClass):
"""Checks handling of UFOs with embdedded MTI/Monotype feature files
https://github.com/googlei18n/fontmake/issues/289
Expand Down