Skip to content

Commit

Permalink
markFeatureWriter: Catch and skip contextual anchors with no context
Browse files Browse the repository at this point in the history
Instead of raising KeyError trying to access non existing context.
  • Loading branch information
khaledhosny committed Sep 5, 2024
1 parent eb73542 commit 930642a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Lib/ufo2ft/featureWriters/markFeatureWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,14 @@ def _makeContextualAttachments(self, glyphClass, liga=False):
# If we are building the mark2base lookup, skip anchors with a number
if not liga and anchor.number is not None:
continue
anchor_context = anchor.libData["GPOS_Context"].strip()
anchor_context = anchor.libData.get("GPOS_Context", "").strip()
if not anchor_context:
self.log.warning(
"contextual anchor '%s' in glyph '%s' has no context data; skipped",
anchor.name,
glyphName,
)
continue
result[anchor_context].append((glyphName, anchor))
return result

Expand Down
49 changes: 49 additions & 0 deletions tests/featureWriters/markFeatureWriter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,55 @@ def test_contextual_liga_anchors(self, testufo):
"""
)

def test_contextual_anchor_no_context(self, testufo, caplog):
a = testufo["a"]
a.appendAnchor({"name": "*top", "x": 200, "y": 200, "identifier": "*top"})
a.lib[OBJECT_LIBS_KEY] = {"*top": {"foo": "bar"}}

writer = MarkFeatureWriter()
feaFile = ast.FeatureFile()
assert str(feaFile) == ""

logger = "ufo2ft.featureWriters.markFeatureWriter.MarkFeatureWriter"
with caplog.at_level(logging.WARNING, logger=logger):
assert writer.write(testufo, feaFile)
assert len(caplog.records) == 1
assert (
"contextual anchor '*top' in glyph 'a' has no context data; skipped"
in caplog.text
)
assert str(feaFile) == dedent(
"""\
markClass acutecomb <anchor 100 200> @MC_top;
markClass tildecomb <anchor 100 200> @MC_top;
feature mark {
lookup mark2base {
pos base a
<anchor 100 200> mark @MC_top;
} mark2base;
lookup mark2liga {
pos ligature f_i
<anchor 100 500> mark @MC_top
ligComponent
<anchor 600 500> mark @MC_top;
} mark2liga;
} mark;
feature mkmk {
lookup mark2mark_top {
@MFS_mark2mark_top = [acutecomb tildecomb];
lookupflag UseMarkFilteringSet @MFS_mark2mark_top;
pos mark tildecomb
<anchor 100 300> mark @MC_top;
} mark2mark_top;
} mkmk;
"""
)

def test_ignorable_anchors(self, FontClass):
dirname = os.path.dirname(os.path.dirname(__file__))
fontPath = os.path.join(dirname, "data", "IgnoreAnchorsTest-Thin.ufo")
Expand Down

0 comments on commit 930642a

Please sign in to comment.