From 8697f91cdc3f8e5fed67cdee3b45635b662711ac Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 11 Nov 2022 12:20:37 +0000 Subject: [PATCH] [unicodedata] map Zmth<->math in ot_tag_{to,from}_script Fixes https://github.com/fonttools/fonttools/issues/1737 --- Lib/fontTools/unicodedata/OTTags.py | 5 +++++ Lib/fontTools/unicodedata/__init__.py | 13 +++++++------ Tests/unicodedata_test.py | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Lib/fontTools/unicodedata/OTTags.py b/Lib/fontTools/unicodedata/OTTags.py index a9d8cd1ce..859a3bcdc 100644 --- a/Lib/fontTools/unicodedata/OTTags.py +++ b/Lib/fontTools/unicodedata/OTTags.py @@ -22,11 +22,16 @@ SCRIPT_EXCEPTIONS = { "Yiii": "yi ", "Nkoo": "nko ", "Vaii": "vai ", + "Zmth": "math", "Zinh": DEFAULT_SCRIPT, "Zyyy": DEFAULT_SCRIPT, "Zzzz": DEFAULT_SCRIPT, } +SCRIPT_EXCEPTIONS_REVERSED = { + "math": "Zmth", +} + NEW_SCRIPT_TAGS = { "Beng": ("bng2",), "Deva": ("dev2",), diff --git a/Lib/fontTools/unicodedata/__init__.py b/Lib/fontTools/unicodedata/__init__.py index 4546ef3f0..29f7252b6 100644 --- a/Lib/fontTools/unicodedata/__init__.py +++ b/Lib/fontTools/unicodedata/__init__.py @@ -239,15 +239,13 @@ def ot_tags_from_script(script_code): Unicode script code. Return ['DFLT'] script tag for invalid/unknown script codes. """ + if script_code in OTTags.SCRIPT_EXCEPTIONS: + return [OTTags.SCRIPT_EXCEPTIONS[script_code]] + if script_code not in Scripts.NAMES: return [OTTags.DEFAULT_SCRIPT] - script_tags = [ - OTTags.SCRIPT_EXCEPTIONS.get( - script_code, - script_code[0].lower() + script_code[1:] - ) - ] + script_tags = [script_code[0].lower() + script_code[1:]] if script_code in OTTags.NEW_SCRIPT_TAGS: script_tags.extend(OTTags.NEW_SCRIPT_TAGS[script_code]) script_tags.reverse() # last in, first out @@ -278,6 +276,9 @@ def ot_tag_to_script(tag): if tag in OTTags.NEW_SCRIPT_TAGS_REVERSED: return OTTags.NEW_SCRIPT_TAGS_REVERSED[tag] + if tag in OTTags.SCRIPT_EXCEPTIONS_REVERSED: + return OTTags.SCRIPT_EXCEPTIONS_REVERSED[tag] + # This side of the conversion is fully algorithmic # Any spaces at the end of the tag are replaced by repeating the last diff --git a/Tests/unicodedata_test.py b/Tests/unicodedata_test.py index 92995b4a1..bc950e550 100644 --- a/Tests/unicodedata_test.py +++ b/Tests/unicodedata_test.py @@ -210,6 +210,7 @@ def test_ot_tags_from_script(): assert unicodedata.ot_tags_from_script("Deva") == ["dev2", "deva"] # exceptions assert unicodedata.ot_tags_from_script("Hira") == ["kana"] + assert unicodedata.ot_tags_from_script("Zmth") == ["math"] # special script codes map to DFLT assert unicodedata.ot_tags_from_script("Zinh") == ["DFLT"] assert unicodedata.ot_tags_from_script("Zyyy") == ["DFLT"] @@ -232,6 +233,7 @@ def test_ot_tag_to_script(): assert unicodedata.ot_tag_to_script("vai ") == "Vaii" assert unicodedata.ot_tag_to_script("lao ") == "Laoo" assert unicodedata.ot_tag_to_script("yi") == "Yiii" + assert unicodedata.ot_tag_to_script("math") == "Zmth" # both 'hang' and 'jamo' tags map to the Hangul script assert unicodedata.ot_tag_to_script("hang") == "Hang" assert unicodedata.ot_tag_to_script("jamo") == "Hang"