From d89af35902f9eadf1515f3ccc3ddeee4268916ad Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 16 Dec 2019 10:30:57 +0000 Subject: [PATCH 1/3] add Glyph.appendContour method for compat with defcon API (#41) --- src/ufoLib2/objects/glyph.py | 8 ++++++++ 1 file changed, 8 insertions(+) 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. """ From 372fbb1c0280b1aba9ddebaf52a024f402086b89 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 16 Dec 2019 16:43:15 +0000 Subject: [PATCH 2/3] point: 'type' is Optional[str], default is None --- src/ufoLib2/objects/point.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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]) From f08f2095f8b4ed14f0120da06e7fd774437db89d Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 16 Dec 2019 16:44:00 +0000 Subject: [PATCH 3/3] test_glyph: test appendContour method --- tests/objects/test_glyph.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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())