From 5b28bafeb95c232ec579ed35b3b4d4ec103d47f9 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Mon, 7 Oct 2024 10:14:52 +0300 Subject: [PATCH] [cursFeatureWriter] Respect direction suffix when setting lookupflag By default we set RightToLeft lookupflag for RTL glyphs, which is a sane default. But, despite the flags name, it is not directly related to glyph directionality, rather it defines which glyphs stay at their natural position and which gets shifted. When the flag is set, glyphs to the right gets shifted, otherwise glyphs to the left. GlyphsApp supports controlling the flag by adding `.RTL` or `.LTR` suffix to the anchor names, so we now support this as well. See for examples of this: https://forum.glyphsapp.com/t/is-there-a-solution-for-vertical-kerning-in-arabic-script/26571/39 --- .../featureWriters/cursFeatureWriter.py | 9 +- .../featureWriters/cursFeatureWriter_test.py | 131 ++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/Lib/ufo2ft/featureWriters/cursFeatureWriter.py b/Lib/ufo2ft/featureWriters/cursFeatureWriter.py index f4e0b632..847b222d 100644 --- a/Lib/ufo2ft/featureWriters/cursFeatureWriter.py +++ b/Lib/ufo2ft/featureWriters/cursFeatureWriter.py @@ -51,7 +51,9 @@ def _makeCursiveFeature(self): orderedGlyphSet = self.getOrderedGlyphSet().items() cursiveAnchorsPairs = self._getCursiveAnchorPairs(orderedGlyphSet) for entryName, exitName in cursiveAnchorsPairs: - if shouldSplit: + # If the anchors have an explicit direction suffix, don’t set + # direction based on the script of the glyphs. + if not entryName.endswith((".LTR", ".RTL")) and shouldSplit: # Make LTR lookup LTRlookup = self._makeCursiveLookup( ( @@ -108,6 +110,11 @@ def _makeCursiveLookup(self, glyphs, entryName, exitName, direction=None): suffix += "_rtl" lookup = ast.LookupBlock(name=f"curs{suffix}") + if entryName.endswith(".RTL"): + direction = "RTL" + elif entryName.endswith(".LTR"): + direction = "LTR" + if direction != "LTR": lookup.statements.append(ast.makeLookupFlag(("IgnoreMarks", "RightToLeft"))) else: diff --git a/tests/featureWriters/cursFeatureWriter_test.py b/tests/featureWriters/cursFeatureWriter_test.py index 157bb783..0554756d 100644 --- a/tests/featureWriters/cursFeatureWriter_test.py +++ b/tests/featureWriters/cursFeatureWriter_test.py @@ -295,3 +295,134 @@ def test_curs_feature_multiple_anchors_mixed(self, testufo): } curs; """ ) + + def test_curs_feature_forced_RTL(self, testufo): + for c in ("a", "b", "c"): + g = testufo[c] + g.unicode = ord(c) + anchors = list(g.anchors) + g.anchors = [] + for a in anchors: + g.appendAnchor({"name": a.name + ".RTL", "x": a.x, "y": a.y}) + + generated = self.writeFeatures(testufo) + + assert str(generated) == dedent( + """\ + feature curs { + lookup curs_RTL { + lookupflag RightToLeft IgnoreMarks; + pos cursive a ; + pos cursive b ; + pos cursive c ; + } curs_RTL; + + } curs; + """ + ) + + def test_curs_feature_forced_LTR(self, testufo): + for n, u in (("a", 0x0627), ("b", 0x0628), ("c", 0x062C)): + g = testufo[n] + g.unicode = u + anchors = list(g.anchors) + g.anchors = [] + for a in anchors: + g.appendAnchor({"name": a.name + ".LTR", "x": a.x, "y": a.y}) + + generated = self.writeFeatures(testufo) + + assert str(generated) == dedent( + """\ + feature curs { + lookup curs_LTR { + lookupflag IgnoreMarks; + pos cursive a ; + pos cursive b ; + pos cursive c ; + } curs_LTR; + + } curs; + """ + ) + + def test_curs_feature_mixed_forced_direction(self, testufo): + testufo["a"].unicode = ord("a") + testufo["b"].unicode = ord("b") + testufo["c"].unicode = ord("c") + + glyph = testufo.newGlyph("d") + glyph.unicode = ord("d") + glyph.appendAnchor({"name": "exit.RTL", "x": 110, "y": 210}) + + glyph = testufo.newGlyph("e") + glyph.unicode = ord("e") + glyph.appendAnchor({"name": "entry.RTL", "x": 10, "y": 210}) + glyph.appendAnchor({"name": "exit.RTL", "x": 121, "y": 210}) + + glyph = testufo.newGlyph("f") + glyph.unicode = ord("f") + glyph.appendAnchor({"name": "entry.RTL", "x": 110, "y": 210}) + + glyph = testufo.newGlyph("alef") + glyph.unicode = 0x0627 + glyph.appendAnchor({"name": "entry", "x": 100, "y": 200}) + + glyph = testufo.newGlyph("beh") + glyph.unicode = 0x0628 + glyph.appendAnchor({"name": "entry", "x": 0, "y": 200}) + glyph.appendAnchor({"name": "exit", "x": 111, "y": 200}) + + glyph = testufo.newGlyph("jeem") + glyph.unicode = 0x062C + glyph.appendAnchor({"name": "entry", "x": 100, "y": 200}) + + glyph = testufo.newGlyph("heh") + glyph.unicode = 0x0647 + glyph.appendAnchor({"name": "entry.LTR", "x": 110, "y": 210}) + + glyph = testufo.newGlyph("waw") + glyph.unicode = 0x0648 + glyph.appendAnchor({"name": "exit.LTR", "x": 10, "y": 210}) + glyph.appendAnchor({"name": "exit.LTR", "x": 121, "y": 210}) + + glyph = testufo.newGlyph("zain") + glyph.unicode = 0x0632 + glyph.appendAnchor({"name": "entry.LTR", "x": 110, "y": 210}) + + generated = self.writeFeatures(testufo) + + assert str(generated) == dedent( + """\ + feature curs { + lookup curs_ltr { + lookupflag IgnoreMarks; + pos cursive a ; + pos cursive b ; + pos cursive c ; + } curs_ltr; + + lookup curs_rtl { + lookupflag RightToLeft IgnoreMarks; + pos cursive alef ; + pos cursive beh ; + pos cursive jeem ; + } curs_rtl; + + lookup curs_LTR { + lookupflag IgnoreMarks; + pos cursive heh ; + pos cursive waw ; + pos cursive zain ; + } curs_LTR; + + lookup curs_RTL { + lookupflag RightToLeft IgnoreMarks; + pos cursive d ; + pos cursive e ; + pos cursive f ; + } curs_RTL; + + } curs; + """ + )