From 04e1269b41d654079992f94bb86d89e64bd6d7fa Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Sat, 3 Oct 2020 12:43:28 +0100 Subject: [PATCH] [glifLib] Hard-require x and y coordinates --- Lib/fontTools/ufoLib/glifLib.py | 8 ++++---- Tests/ufoLib/glifLib_test.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Lib/fontTools/ufoLib/glifLib.py b/Lib/fontTools/ufoLib/glifLib.py index 76fce771f..385f54bfe 100755 --- a/Lib/fontTools/ufoLib/glifLib.py +++ b/Lib/fontTools/ufoLib/glifLib.py @@ -1416,10 +1416,10 @@ def _validateAndMassagePointStructures(contour, pointAttributes, openContourOffC raise GlifLibError("Unknown child elements in point element.") # x and y are required for attr in ("x", "y"): - value = element.get(attr) - if validate and value is None: - raise GlifLibError("Required %s attribute is missing in point element." % attr) - point[attr] = _number(value) + try: + point[attr] = _number(point[attr]) + except KeyError as e: + raise GlifLibError(f"Required {attr} attribute is missing in point element.") from e # segment type pointType = point.pop("type", "offcurve") if validate and pointType not in pointTypeOptions: diff --git a/Tests/ufoLib/glifLib_test.py b/Tests/ufoLib/glifLib_test.py index 32c9f62a6..4c8d587c6 100644 --- a/Tests/ufoLib/glifLib_test.py +++ b/Tests/ufoLib/glifLib_test.py @@ -10,6 +10,7 @@ from fontTools.ufoLib.glifLib import ( ) from fontTools.ufoLib.errors import GlifLibError, UnsupportedGLIFFormat, UnsupportedUFOFormat from fontTools.misc.etree import XML_DECLARATION +from fontTools.pens.recordingPen import RecordingPointPen import pytest GLYPHSETDIR = getDemoFontGlyphSetPath() @@ -250,6 +251,27 @@ class ReadWriteFuncTest: with pytest.raises(GlifLibError, match="Forbidden GLIF format version"): readGlyphFromString(s, _Glyph(), formatVersions=[1]) + def test_read_ensure_x_y(self): + """Ensure that a proper GlifLibError is raised when point coordinates are + missing, regardless of validation setting.""" + + s = """ + + + + + + + + + """ + pen = RecordingPointPen() + + with pytest.raises(GlifLibError, match="Required y attribute"): + readGlyphFromString(s, _Glyph(), pen) + + with pytest.raises(GlifLibError, match="Required y attribute"): + readGlyphFromString(s, _Glyph(), pen, validate=False) def test_GlyphSet_unsupported_ufoFormatVersion(tmp_path, caplog): with pytest.raises(UnsupportedUFOFormat):