From d77c8901c8055b579d6cb0747551a8490100146a Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 10 Mar 2023 11:37:07 +0000 Subject: [PATCH 1/3] ttFont_test: add test for getGlyphID --- Tests/ttLib/ttFont_test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/ttLib/ttFont_test.py b/Tests/ttLib/ttFont_test.py index 38f2bc373..962196fa9 100644 --- a/Tests/ttLib/ttFont_test.py +++ b/Tests/ttLib/ttFont_test.py @@ -262,3 +262,13 @@ def test_font_normalizeLocation_no_VF(): ttf = TTFont() with pytest.raises(TTLibError, match="Not a variable font"): ttf.normalizeLocation({}) + + +def test_getGlyphID(): + font = TTFont() + font.importXML(os.path.join(DATA_DIR, "TestTTF-Regular.ttx")) + + assert font.getGlyphID("space") == 3 + assert font.getGlyphID("glyph12345") == 12345 # virtual glyph + with pytest.raises(KeyError): + font.getGlyphID("non_existent") From b137fd9616b8c8f3379829aff7fa9232c62a5097 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 10 Mar 2023 11:38:55 +0000 Subject: [PATCH 2/3] ttFont: raise KeyError from getGlyphID instead of returning None Fixes #3030 --- Lib/fontTools/ttLib/ttFont.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/fontTools/ttLib/ttFont.py b/Lib/fontTools/ttLib/ttFont.py index 5760e499e..3618d3f91 100644 --- a/Lib/fontTools/ttLib/ttFont.py +++ b/Lib/fontTools/ttLib/ttFont.py @@ -660,6 +660,7 @@ class TTFont(object): return int(glyphName[5:]) except (NameError, ValueError): raise KeyError(glyphName) + raise def getGlyphIDMany(self, lst): """Converts a list of glyph names into a list of glyph IDs.""" From d64ee127bea7ae0a1b00306289829761c8ad4d97 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 10 Mar 2023 11:44:52 +0000 Subject: [PATCH 3/3] ttFont_test: another test for missing glyph name that doesn't match 'glyph\d+' pattern --- Tests/ttLib/ttFont_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/ttLib/ttFont_test.py b/Tests/ttLib/ttFont_test.py index 962196fa9..a2a61baf2 100644 --- a/Tests/ttLib/ttFont_test.py +++ b/Tests/ttLib/ttFont_test.py @@ -272,3 +272,5 @@ def test_getGlyphID(): assert font.getGlyphID("glyph12345") == 12345 # virtual glyph with pytest.raises(KeyError): font.getGlyphID("non_existent") + with pytest.raises(KeyError): + font.getGlyphID("glyph_prefix_but_invalid_id")