Skip to content

Commit

Permalink
[glyphdata] Improve ligature splitting
Browse files Browse the repository at this point in the history
Handle names like “moMa_underscore-thai” where it should be split into
“moMa-thai” and plain “underscore” not “underscore-thai”. I updated the
test expectation because Glyphs is just bing silly building production
names inconsistently.
  • Loading branch information
khaledhosny committed Jul 23, 2023
1 parent 6ab810f commit 5dced5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
31 changes: 22 additions & 9 deletions Lib/glyphsLib/glyphdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,29 @@ def _translate_category(glyph_name, unicode_category):


def _split_ligature_glyph_name(name, data):
# Split name to ligature parts
parts = name.split("_")
if len(parts) > 1 and "-" in parts[-1]:
script = parts[-1].rsplit("-", 1)[-1]
for i, part in enumerate(parts):
new = f"{part}-{script}"
if "-" in part:
continue
if _lookup_attributes(part, data) and not _lookup_attributes(new, data):
continue
parts[i] = new

# If the last part has a script suffix, strip it and re-split the name.
if "-" in parts[-1]:
base, script = name.rsplit("-", 1)
parts = base.split("_")

# If there is more than one part, try adding the script suffix to each
# part, if this results in a known glyph name, use it as the part name.
if len(parts) > 1:
for i, part in enumerate(parts):
new = f"{part}-{script}"
# If the part already has a script suffix, keep it unchanged.
if "-" in part:
continue
# If the non suffixed name exists and the suffixed name does
# not exist, keep the part name unchanged.
if _lookup_attributes(part, data) and not _lookup_attributes(new, data):
continue
parts[i] = new
else:
parts = name.split("_")
return parts


Expand Down
8 changes: 4 additions & 4 deletions tests/glyphdata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ def test_glyphdata_no_duplicates(self):
"ma_uMatra-tamil": "uni0BAE0BC1",
"ma_uuMatra-tamil": "uni0BAE0BC2",
"mo-khmer.below.ro": "uni17D21798.ro",
"moMa_underscore-thai": "uni0E21_uni005F",
"moMa_underscore-thai": "uni0E21005F", # uni0E21_uni005F
"na_iMatra-tamil": "uni0BA80BBF",
"na_uMatra-tamil": "uni0BA80BC1",
"na_uuMatra-tamil": "uni0BA80BC2",
"ng_ya-deva": "uni0919094D092F",
"nga_uMatra-tamil": "uni0B990BC1",
"nga_uuMatra-tamil": "uni0B990BC2",
"ngoNgu_underscore-thai": "uni0E07_uni005F",
"ngoNgu_underscore-thai": "uni0E07005F", # uni0E07_uni005F
"niggahita_maiCatawa-lao": "uni0ECD0ECB",
"niggahita_maiCatawa-lao.right": "uni0ECD0ECB.right",
"niggahita_maiEk-lao": "uni0ECD0EC8",
Expand All @@ -345,7 +345,7 @@ def test_glyphdata_no_duplicates(self):
"nnna_uuMatra-tamil": "uni0BA90BC2",
"nno-khmer.below.narrow1": "uni17D2178E.narrow1",
"nno-khmer.below.narrow2": "uni17D2178E.narrow2",
"noNu_underscore-thai": "uni0E19_uni005F",
"noNu_underscore-thai": "uni0E19005F", # uni0E19_uni005F
"nya_iMatra-tamil": "uni0B9E0BBF",
"nya_uMatra-tamil": "uni0B9E0BC1",
"nya_uuMatra-tamil": "uni0B9E0BC2",
Expand Down Expand Up @@ -397,7 +397,7 @@ def test_glyphdata_no_duplicates(self):
"va_uuMatra-tamil": "uni0BB50BC2",
"ya_uMatra-tamil": "uni0BAF0BC1",
"ya_uuMatra-tamil": "uni0BAF0BC2",
"yoYing_underscore-thai": "uni0E0D_uni005F",
"yoYing_underscore-thai": "uni0E0D005F", # uni0E0D_uni005F
}


Expand Down

0 comments on commit 5dced5c

Please sign in to comment.