diff --git a/src/ufoLib2/objects/glyph.py b/src/ufoLib2/objects/glyph.py index 6b9e84ff..5ff8bce5 100644 --- a/src/ufoLib2/objects/glyph.py +++ b/src/ufoLib2/objects/glyph.py @@ -6,6 +6,7 @@ from fontTools.pens.pointPen import PointToSegmentPen, SegmentToPointPen from ufoLib2.objects.anchor import Anchor +from ufoLib2.objects.contour import Contour from ufoLib2.objects.guideline import Guideline from ufoLib2.objects.image import Image from ufoLib2.pointPens.glyphPointPen import GlyphPointPen @@ -148,6 +149,13 @@ def appendGuideline(self, guideline): guideline = Guideline(**guideline) self._guidelines.append(guideline) + def appendContour(self, contour): + if not isinstance(contour, Contour): + raise TypeError( + f"Expected {Contour.__name__}, found {type(contour).__name__}" + ) + self.contours.append(contour) + def copy(self, name=None): """Return a new Glyph (deep) copy, optionally override the new glyph name. """ diff --git a/src/ufoLib2/objects/point.py b/src/ufoLib2/objects/point.py index f9539b24..a8d08c84 100644 --- a/src/ufoLib2/objects/point.py +++ b/src/ufoLib2/objects/point.py @@ -7,7 +7,7 @@ class Point: x = attr.ib(type=Union[int, float]) y = attr.ib(type=Union[int, float]) - type = attr.ib(type=Optional[str]) + type = attr.ib(default=None, type=Optional[str]) smooth = attr.ib(default=False, type=bool) name = attr.ib(default=None, type=Optional[str]) identifier = attr.ib(default=None, type=Optional[str]) diff --git a/tests/objects/test_glyph.py b/tests/objects/test_glyph.py index 44cee46c..62361280 100644 --- a/tests/objects/test_glyph.py +++ b/tests/objects/test_glyph.py @@ -1,4 +1,6 @@ -from ufoLib2.objects import Anchor, Component, Glyph, Guideline, Image +from ufoLib2.objects import Anchor, Contour, Component, Glyph, Guideline, Image, Point + +import pytest def test_copyDataFromGlyph(ufo_UbuTestData): @@ -70,3 +72,20 @@ def _assert_equal_but_distinct_objects(glyph1, glyph2): d = a.copy(name="d") assert d.name == "d" _assert_equal_but_distinct_objects(d, a) + + +def test_appendContour(ufo_UbuTestData): + font = ufo_UbuTestData + + A = font["A"] + n = len(A.contours) + + c = Contour(points=[Point(0, 0), Point(1, 1)]) + + A.appendContour(c) + + assert len(A.contours) == n + 1 + assert A.contours[-1] is c + + with pytest.raises(TypeError, match="Expected Contour, found object"): + A.appendContour(object())