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

TTGlyphPen: do not error with empty contours, simply ignore them #3145

Merged
merged 1 commit into from
May 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/fontTools/pens/ttGlyphPen.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def endPath(self) -> None:
if self._isClosed():
raise PenError("Contour is already closed.")
if self._currentContourStartIndex == len(self.points):
raise PenError("Tried to end an empty contour.")
# ignore empty contours
self._currentContourStartIndex = None
return

contourStart = self.endPts[-1] + 1 if self.endPts else 0
self.endPts.append(len(self.points) - 1)
Expand Down
15 changes: 9 additions & 6 deletions Tests/pens/ttGlyphPen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,6 @@ def test_glyph_errorOnUnendedContour(self):
with pytest.raises(PenError):
pen.glyph()

def test_glyph_errorOnEmptyContour(self):
pen = TTGlyphPointPen(None)
pen.beginPath()
with pytest.raises(PenError):
pen.endPath()

def test_glyph_decomposes(self):
componentName = "a"
glyphSet = {}
Expand Down Expand Up @@ -595,6 +589,15 @@ def test_open_path_starting_with_move(self):
assert pen1.points == pen2.points == [(0, 0), (10, 10), (20, 20), (20, 0)]
assert pen1.types == pen2.types == [1, 1, 0, 1]

def test_skip_empty_contours(self):
pen = TTGlyphPointPen(None)
pen.beginPath()
pen.endPath()
pen.beginPath()
pen.endPath()
glyph = pen.glyph()
assert glyph.numberOfContours == 0


class CubicGlyfTest:
def test_cubic_simple(self):
Expand Down