From 78e10d8b42095b709cd4125e592d914d3ed1558e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 15:12:06 -0700 Subject: [PATCH] [ttGlyphPen] Add outputImpliedClosingLine argument Fixes https://github.com/fonttools/fonttools/issues/2913 --- Lib/fontTools/pens/ttGlyphPen.py | 24 +++++++++++++++++------- Tests/pens/ttGlyphPen_test.py | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Lib/fontTools/pens/ttGlyphPen.py b/Lib/fontTools/pens/ttGlyphPen.py index 5087e1589..bd97ab03f 100644 --- a/Lib/fontTools/pens/ttGlyphPen.py +++ b/Lib/fontTools/pens/ttGlyphPen.py @@ -164,6 +164,15 @@ class TTGlyphPen(_TTGlyphBasePen, LoggingPen): drawMethod = "draw" transformPen = TransformPen + def __init__( + self, + glyphSet: Optional[Dict[str, Any]] = None, + handleOverflowingTransforms: bool = True, + outputImpliedClosingLine: bool = False, + ) -> None: + super().__init__(glyphSet, handleOverflowingTransforms) + self.outputImpliedClosingLine = outputImpliedClosingLine + def _addPoint(self, pt: Tuple[float, float], onCurve: int) -> None: self.points.append(pt) self.types.append(onCurve) @@ -205,13 +214,14 @@ class TTGlyphPen(_TTGlyphBasePen, LoggingPen): self._popPoint() return - # if first and last point on this path are the same, remove last - startPt = 0 - if self.endPts: - startPt = self.endPts[-1] + 1 - if self.points[startPt] == self.points[endPt]: - self._popPoint() - endPt -= 1 + if not self.outputImpliedClosingLine: + # if first and last point on this path are the same, remove last + startPt = 0 + if self.endPts: + startPt = self.endPts[-1] + 1 + if self.points[startPt] == self.points[endPt]: + self._popPoint() + endPt -= 1 self.endPts.append(endPt) diff --git a/Tests/pens/ttGlyphPen_test.py b/Tests/pens/ttGlyphPen_test.py index ec5924ed5..de06931a6 100644 --- a/Tests/pens/ttGlyphPen_test.py +++ b/Tests/pens/ttGlyphPen_test.py @@ -294,6 +294,27 @@ class TTGlyphPenTest(TTGlyphPenTestBase): uni0302_uni0300.recalcBounds(glyphSet) self.assertGlyphBoundsEqual(uni0302_uni0300, (-278, 745, 148, 1025)) + def test_outputImpliedClosingLine(self): + glyphSet = {} + + pen = TTGlyphPen(glyphSet) + pen.moveTo((0, 0)) + pen.lineTo((10, 0)) + pen.lineTo((0, 10)) + pen.lineTo((0, 0)) + pen.closePath() + glyph = pen.glyph() + assert len(glyph.coordinates) == 3 + + pen = TTGlyphPen(glyphSet, outputImpliedClosingLine=True) + pen.moveTo((0, 0)) + pen.lineTo((10, 0)) + pen.lineTo((0, 10)) + pen.lineTo((0, 0)) + pen.closePath() + glyph = pen.glyph() + assert len(glyph.coordinates) == 4 + class TTGlyphPointPenTest(TTGlyphPenTestBase): penClass = TTGlyphPointPen @@ -574,7 +595,6 @@ class TTGlyphPointPenTest(TTGlyphPenTestBase): assert pen1.types == pen2.types == [1, 1, 0, 1] - class _TestGlyph(object): def __init__(self, glyph): self.coordinates = glyph.coordinates