Skip to content

Commit

Permalink
Merge pull request #361 from anthrotype/prune-mixed-bidi-kerns
Browse files Browse the repository at this point in the history
drop kern pairs with mixed bidi type
  • Loading branch information
anthrotype authored Feb 7, 2020
2 parents 6d89fe4 + 2c00cc8 commit 1c271d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
28 changes: 16 additions & 12 deletions Lib/ufo2ft/featureWriters/kernFeatureWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"Gong", # Gunjala Gondi
"Maka", # Makasar
# Unicode-12.0 additions
"Nand", # Nandinagari
"Nand", # Nandinagari
}


Expand All @@ -115,21 +115,20 @@ def unicodeScriptDirection(uv):
return unicodedata.script_horizontal_direction(sc)


STRONG_LTR_BIDI_TYPE = "L"
STRONG_RTL_BIDI_TYPES = {"R", "AL"}
LTR_NUMBER_BIDI_TYPES = {"AN", "EN"}
RTL_BIDI_TYPES = {"R", "AL"}
LTR_BIDI_TYPES = {"L", "AN", "EN"}


def unicodeBidiType(uv):
# return "R", "L", "N" (for numbers), or None for everything else
"""Return "R" for characters with RTL direction, or "L" for LTR (whether
'strong' or 'weak'), or None for neutral direction.
"""
char = unichr(uv)
bidiType = unicodedata.bidirectional(char)
if bidiType in STRONG_RTL_BIDI_TYPES:
if bidiType in RTL_BIDI_TYPES:
return "R"
elif bidiType == STRONG_LTR_BIDI_TYPE:
elif bidiType in LTR_BIDI_TYPES:
return "L"
elif bidiType in LTR_NUMBER_BIDI_TYPES:
return "N"
else:
return None

Expand Down Expand Up @@ -367,7 +366,7 @@ def _groupScriptsByTagAndDirection(feaScripts):
def _makePairPosRule(pair, rtl=False):
enumerated = pair.firstIsClass ^ pair.secondIsClass
value = otRound(pair.value)
if rtl and "N" in pair.bidiTypes:
if rtl and "L" in pair.bidiTypes:
# numbers are always shaped LTR even in RTL scripts
rtl = False
valuerecord = ast.ValueRecord(
Expand Down Expand Up @@ -424,10 +423,15 @@ def _makeKerningLookups(self):
if shouldSplit:
# make one DFLT lookup with script-agnostic characters, and two
# LTR/RTL lookups excluding pairs from the opposite group.
# We drop kerning pairs with ambiguous direction.
# We drop kerning pairs with ambiguous direction: i.e. those containing
# glyphs from scripts with different overall horizontal direction, or
# glyphs with incompatible bidirectional type (e.g. arabic letters vs
# arabic numerals).
pairs = []
for pair in self.context.kerning.pairs:
if "RTL" in pair.directions and "LTR" in pair.directions:
if ("RTL" in pair.directions and "LTR" in pair.directions) or (
"R" in pair.bidiTypes and "L" in pair.bidiTypes
):
self.log.warning(
"skipped kern pair with ambiguous direction: %r", pair
)
Expand Down
15 changes: 13 additions & 2 deletions tests/featureWriters/kernFeatureWriter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,20 @@ def test_skip_ambiguous_direction_pair(self, FontClass, caplog):

ufo = FontClass()
ufo.newGlyph("A").unicode = 0x41
ufo.newGlyph("one").unicode = 0x31
ufo.newGlyph("yod-hb").unicode = 0x5D9
ufo.newGlyph("reh-ar").unicode = 0x631
ufo.newGlyph("one-ar").unicode = 0x661
ufo.newGlyph("bar").unicodes = [0x73, 0x627]
ufo.kerning.update({("bar", "bar"): 1, ("bar", "A"): 2, ("reh-ar", "A"): 3})
ufo.kerning.update(
{
("bar", "bar"): 1,
("bar", "A"): 2,
("reh-ar", "A"): 3,
("reh-ar", "one-ar"): 4,
("yod-hb", "one"): 5,
}
)
ufo.features.text = dedent(
"""\
languagesystem DFLT dflt;
Expand All @@ -890,7 +901,7 @@ def test_skip_ambiguous_direction_pair(self, FontClass, caplog):
generated = self.writeFeatures(ufo)

assert not generated
assert len(caplog.records) == 3
assert len(caplog.records) == 5
assert "skipped kern pair with ambiguous direction" in caplog.text

def test_kern_RTL_and_DFLT_numbers(self, FontClass):
Expand Down

0 comments on commit 1c271d3

Please sign in to comment.