[ttGlyphPen] Add outputImpliedClosingLine argument

Fixes https://github.com/fonttools/fonttools/issues/2913
This commit is contained in:
Behdad Esfahbod 2022-12-09 15:12:06 -07:00
parent ddbbef2257
commit 78e10d8b42
2 changed files with 38 additions and 8 deletions

View File

@ -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)

View File

@ -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