Merge pull request #3561 from drj11/drj11/unicode-hex

Check unicode elements have required hex attribute
This commit is contained in:
Cosimo Lupo 2024-10-15 16:39:12 +02:00 committed by GitHub
commit dd8d80a4f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 2 deletions

View File

@ -1191,8 +1191,12 @@ def _readGlyphFromTreeFormat1(
haveSeenAdvance = True
_readAdvance(glyphObject, element)
elif element.tag == "unicode":
v = element.get("hex")
if v is None:
raise GlifLibError(
"A unicode element is missing its required hex attribute."
)
try:
v = element.get("hex")
v = int(v, 16)
if v not in unicodes:
unicodes.append(v)
@ -1254,8 +1258,12 @@ def _readGlyphFromTreeFormat2(
haveSeenAdvance = True
_readAdvance(glyphObject, element)
elif element.tag == "unicode":
v = element.get("hex")
if v is None:
raise GlifLibError(
"A unicode element is missing its required hex attribute."
)
try:
v = element.get("hex")
v = int(v, 16)
if v not in unicodes:
unicodes.append(v)

View File

@ -300,6 +300,22 @@ class TestGLIF1(unittest.TestCase):
self.assertRaises(GlifLibError, self.pyToGLIF, py)
self.assertRaises(GlifLibError, self.glifToPy, glif)
def testUnicodes_hex_present(self):
"""Test that a present <unicode> element must have a
'hex' attribute; by testing that an invalid <unicode>
element raises an appropriate error.
"""
# illegal
glif = """
<glyph name="a" format="1">
<unicode />
<outline>
</outline>
</glyph>
"""
self.assertRaises(GlifLibError, self.glifToPy, glif)
def testNote(self):
glif = """
<glyph name="a" format="1">

View File

@ -300,6 +300,22 @@ class TestGLIF2(unittest.TestCase):
self.assertRaises(GlifLibError, self.pyToGLIF, py)
self.assertRaises(GlifLibError, self.glifToPy, glif)
def testUnicodes_hex_present(self):
"""Test that a present <unicode> element must have a
'hex' attribute; by testing that an invalid <unicode>
element raises an appropriate error.
"""
# illegal
glif = """
<glyph name="a" format="2">
<unicode />
<outline>
</outline>
</glyph>
"""
self.assertRaises(GlifLibError, self.glifToPy, glif)
def testNote(self):
glif = """
<glyph name="a" format="2">