Skip to content

Commit

Permalink
[cubic-glyf] Fix drawPoints() contour ending with cubic offCurve
Browse files Browse the repository at this point in the history
Fixes #3189
  • Loading branch information
behdad committed Jun 27, 2023
1 parent 1138e5b commit c8f2cdf
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Lib/fontTools/ttLib/tables/_g_l_y_f.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,9 +1528,15 @@ def drawPoints(self, pen, glyfTable, offset=0):
start = end
pen.beginPath()
# Start with the appropriate segment type based on the final segment
segmentType = "line" if cFlags[-1] == 1 else "qcurve"

if cFlags[-1] & flagOnCurve:
segmentType = "line"
elif cFlags[-1] & flagCubic:
segmentType = "curve"
else:
segmentType = "qcurve"
for i, pt in enumerate(contour):
if cFlags[i] & flagOnCurve == 1:
if cFlags[i] & flagOnCurve:
pen.addPoint(pt, segmentType=segmentType)
segmentType = "line"
else:
Expand Down
72 changes: 72 additions & 0 deletions Tests/ttLib/ttGlyphSet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,68 @@ def test_cubic_glyf(self):
glyphset["one"].draw(pen)
assert pen.value == expected

expectedPoints = [
("beginPath", (), {}),
("addPoint", ((76, 181), "curve", False, None), {}),
("addPoint", ((103, 181), None, False, None), {}),
("addPoint", ((125, 158), None, False, None), {}),
("addPoint", ((125, 104), None, False, None), {}),
("addPoint", ((103, 82), None, False, None), {}),
("addPoint", ((76, 82), "curve", False, None), {}),
("addPoint", ((48, 82), None, False, None), {}),
("addPoint", ((26, 104), None, False, None), {}),
("addPoint", ((26, 158), None, False, None), {}),
("addPoint", ((48, 181), None, False, None), {}),
("endPath", (), {}),
]
pen = RecordingPointPen()
glyphset["one"].drawPoints(pen)
assert pen.value == expectedPoints

pen = RecordingPen()
glyphset["two"].draw(pen)
assert pen.value == expected

expectedPoints = [
("beginPath", (), {}),
("addPoint", ((26, 158), None, False, None), {}),
("addPoint", ((48, 181), None, False, None), {}),
("addPoint", ((76, 181), "curve", False, None), {}),
("addPoint", ((103, 181), None, False, None), {}),
("addPoint", ((125, 158), None, False, None), {}),
("addPoint", ((125, 104), None, False, None), {}),
("addPoint", ((103, 82), None, False, None), {}),
("addPoint", ((76, 82), "curve", False, None), {}),
("addPoint", ((48, 82), None, False, None), {}),
("addPoint", ((26, 104), None, False, None), {}),
("endPath", (), {}),
]
pen = RecordingPointPen()
glyphset["two"].drawPoints(pen)
assert pen.value == expectedPoints

pen = RecordingPen()
glyphset["three"].draw(pen)
assert pen.value == expected

expectedPoints = [
("beginPath", (), {}),
("addPoint", ((48, 82), None, False, None), {}),
("addPoint", ((26, 104), None, False, None), {}),
("addPoint", ((26, 158), None, False, None), {}),
("addPoint", ((48, 181), None, False, None), {}),
("addPoint", ((76, 181), "curve", False, None), {}),
("addPoint", ((103, 181), None, False, None), {}),
("addPoint", ((125, 158), None, False, None), {}),
("addPoint", ((125, 104), None, False, None), {}),
("addPoint", ((103, 82), None, False, None), {}),
("addPoint", ((76, 82), "curve", False, None), {}),
("endPath", (), {}),
]
pen = RecordingPointPen()
glyphset["three"].drawPoints(pen)
assert pen.value == expectedPoints

pen = RecordingPen()
glyphset["four"].draw(pen)
assert pen.value == [
Expand All @@ -581,3 +635,21 @@ def test_cubic_glyf(self):
("curveTo", ((26, 158), (48, 181), (75.5, 181))),
("closePath", ()),
]

# Ouch! We can't represent all-cubic-offcurves in pointPen!
expectedPoints = [
("beginPath", (), {}),
("addPoint", ((103, 181), None, False, None), {}),
("addPoint", ((125, 158), None, False, None), {}),
("addPoint", ((125, 104), None, False, None), {}),
("addPoint", ((103, 82), None, False, None), {}),
("addPoint", ((48, 82), None, False, None), {}),
("addPoint", ((26, 104), None, False, None), {}),
("addPoint", ((26, 158), None, False, None), {}),
("addPoint", ((48, 181), None, False, None), {}),
("endPath", (), {}),
]
pen = RecordingPointPen()
glyphset["four"].drawPoints(pen)
print(pen.value)
assert pen.value == expectedPoints

0 comments on commit c8f2cdf

Please sign in to comment.