Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Glyph.appendContour method for defcon API compat #48

Merged
merged 3 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ufoLib2/objects/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/ufoLib2/objects/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
21 changes: 20 additions & 1 deletion tests/objects/test_glyph.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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())