Skip to content

Commit

Permalink
Merge pull request #3145 from fonttools/ttglyphpen-ignore-empty
Browse files Browse the repository at this point in the history
TTGlyphPen: do not error with empty contours, simply ignore them
  • Loading branch information
anthrotype committed May 31, 2023
2 parents 0bf84f9 + 3bbc19a commit 02a0636
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Lib/fontTools/pens/ttGlyphPen.py
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
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

0 comments on commit 02a0636

Please sign in to comment.