From c7be064ef6f55e5ce1021550f80e08343ac55269 Mon Sep 17 00:00:00 2001 From: David Jones Date: Mon, 3 Jun 2024 17:01:20 +0100 Subject: [PATCH 1/4] Test absent hex attribute on unicode element (currently fails) --- Tests/ufoLib/GLIF1_test.py | 16 ++++++++++++++++ Tests/ufoLib/GLIF2_test.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/Tests/ufoLib/GLIF1_test.py b/Tests/ufoLib/GLIF1_test.py index c4991ca3e..64af3f6a2 100644 --- a/Tests/ufoLib/GLIF1_test.py +++ b/Tests/ufoLib/GLIF1_test.py @@ -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 element must have a + 'hex' attribute; by testing that an invalid + element raises an appropriate error. + """ + + # illegal + glif = """ + + + + + + """ + self.assertRaises(GlifLibError, self.glifToPy, glif) + def testNote(self): glif = """ diff --git a/Tests/ufoLib/GLIF2_test.py b/Tests/ufoLib/GLIF2_test.py index d8c96d653..f4ee4271f 100644 --- a/Tests/ufoLib/GLIF2_test.py +++ b/Tests/ufoLib/GLIF2_test.py @@ -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 element must have a + 'hex' attribute; by testing that an invalid + element raises an appropriate error. + """ + + # illegal + glif = """ + + + + + + """ + self.assertRaises(GlifLibError, self.glifToPy, glif) + def testNote(self): glif = """ From fb30c9822b3827486ea86a3f394103f3c50d42e9 Mon Sep 17 00:00:00 2001 From: David Jones Date: Mon, 3 Jun 2024 17:02:34 +0100 Subject: [PATCH 2/4] Verify that unicode elements have hex attribute --- Lib/fontTools/ufoLib/glifLib.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/fontTools/ufoLib/glifLib.py b/Lib/fontTools/ufoLib/glifLib.py index 62e87db0d..24ab284e4 100755 --- a/Lib/fontTools/ufoLib/glifLib.py +++ b/Lib/fontTools/ufoLib/glifLib.py @@ -1191,6 +1191,10 @@ def _readGlyphFromTreeFormat1( haveSeenAdvance = True _readAdvance(glyphObject, element) elif element.tag == "unicode": + if element.get("hex") is None: + raise GlifLibError( + "A unicode element is missing its required hex attribute." + ) try: v = element.get("hex") v = int(v, 16) @@ -1254,6 +1258,10 @@ def _readGlyphFromTreeFormat2( haveSeenAdvance = True _readAdvance(glyphObject, element) elif element.tag == "unicode": + if element.get("hex") is None: + raise GlifLibError( + "A unicode element is missing its required hex attribute." + ) try: v = element.get("hex") v = int(v, 16) From 7d39064a3674e413ed24577c7ddabb71ad47af6b Mon Sep 17 00:00:00 2001 From: David Jones Date: Mon, 3 Jun 2024 17:09:54 +0100 Subject: [PATCH 3/4] Fix oops: GLIF2 test should have format=2 --- Tests/ufoLib/GLIF2_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/ufoLib/GLIF2_test.py b/Tests/ufoLib/GLIF2_test.py index f4ee4271f..a193ab5c5 100644 --- a/Tests/ufoLib/GLIF2_test.py +++ b/Tests/ufoLib/GLIF2_test.py @@ -308,7 +308,7 @@ class TestGLIF2(unittest.TestCase): # illegal glif = """ - + From aa2d9196c0fa9dd42c4b430d244cad6e1e4b8a32 Mon Sep 17 00:00:00 2001 From: David Jones Date: Thu, 18 Jul 2024 19:29:41 +0100 Subject: [PATCH 4/4] Lift .get("hex") out of try:; and avoid re-evaluating it --- Lib/fontTools/ufoLib/glifLib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/fontTools/ufoLib/glifLib.py b/Lib/fontTools/ufoLib/glifLib.py index 24ab284e4..8f5cba064 100755 --- a/Lib/fontTools/ufoLib/glifLib.py +++ b/Lib/fontTools/ufoLib/glifLib.py @@ -1191,12 +1191,12 @@ def _readGlyphFromTreeFormat1( haveSeenAdvance = True _readAdvance(glyphObject, element) elif element.tag == "unicode": - if element.get("hex") is None: + 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) @@ -1258,12 +1258,12 @@ def _readGlyphFromTreeFormat2( haveSeenAdvance = True _readAdvance(glyphObject, element) elif element.tag == "unicode": - if element.get("hex") is None: + 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)