Merge pull request #3032 from fonttools/getGlyphID-KeyError

ttFont.getGlyphID should raise KeyError on missing glyphs
This commit is contained in:
Cosimo Lupo 2023-03-10 12:11:33 +00:00 committed by GitHub
commit 217742cc44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -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."""

View File

@ -262,3 +262,15 @@ 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")
with pytest.raises(KeyError):
font.getGlyphID("glyph_prefix_but_invalid_id")