[qu2cu] Add a test and fix a bug

This commit is contained in:
Behdad Esfahbod 2023-02-20 12:22:16 -07:00
parent 41732b5904
commit 94ee47c6e4
3 changed files with 45 additions and 17 deletions

View File

@ -49,7 +49,7 @@ class Qu2CuPen(ContourFilterPen):
self.stats = stats self.stats = stats
def _quadratics_to_curve(self, q): def _quadratics_to_curve(self, q):
curves = quadratic_to_curves(q, self.max_err, self.all_cubic) curves = quadratic_to_curves(q, self.max_err, all_cubic=self.all_cubic)
if self.stats is not None: if self.stats is not None:
n = str(len(curves)) n = str(len(curves))
self.stats[n] = self.stats.get(n, 0) + 1 self.stats[n] = self.stats.get(n, 0) + 1
@ -81,21 +81,24 @@ class Qu2CuPen(ContourFilterPen):
if quadratics: if quadratics:
newContour.extend(self._quadratics_to_curve(quadratics)) newContour.extend(self._quadratics_to_curve(quadratics))
# Add back implicit oncurve points if not self.all_cubic:
contour = newContour # Add back implicit oncurve points
newContour = [] contour = newContour
for op, args in contour: newContour = []
if op == "qCurveTo" and newContour and newContour[-1][0] == "qCurveTo": for op, args in contour:
pt0 = newContour[-1][1][-2] if op == "qCurveTo" and newContour and newContour[-1][0] == "qCurveTo":
pt1 = newContour[-1][1][-1] pt0 = newContour[-1][1][-2]
pt2 = args[0] pt1 = newContour[-1][1][-1]
if math.isclose(pt2[0] - pt1[0], pt1[0] - pt0[0]) and math.isclose( pt2 = args[0]
pt2[1] - pt1[1], pt1[1] - pt0[1] if (
): pt1 is not None
newArgs = newContour[-1][1][:-1] + args and math.isclose(pt2[0] - pt1[0], pt1[0] - pt0[0])
newContour[-1] = (op, newArgs) and math.isclose(pt2[1] - pt1[1], pt1[1] - pt0[1])
continue ):
newArgs = newContour[-1][1][:-1] + args
newContour[-1] = (op, newArgs)
continue
newContour.append((op, args)) newContour.append((op, args))
return newContour return newContour

View File

@ -253,7 +253,7 @@ def spline_to_curves(q, costs, tolerance=0.5, all_cubic=False):
# cubic curves, and within those the one with smallest error. # cubic curves, and within those the one with smallest error.
sols = [Solution(0, 0, 0, False)] sols = [Solution(0, 0, 0, False)]
for i in range(1, len(elevated_quadratics) + 1): for i in range(1, len(elevated_quadratics) + 1):
best_sol = Solution(len(q) + 2, 0, 1, False) best_sol = Solution(len(q) + 4, 0, 1, False) # Impossible
for j in range(0, i): for j in range(0, i):
j_sol_count, j_sol_error = sols[j].num_points, sols[j].error j_sol_count, j_sol_error = sols[j].num_points, sols[j].error

View File

@ -219,6 +219,31 @@ class TestQu2CuPen(unittest.TestCase, _TestPenMixin):
], ],
) )
def test_all_cubic(self):
inPen = RecordingPen()
inPen.value = [
("moveTo", ((1204, 347),)),
("qCurveTo", ((1255, 347), (1323, 433), (1323, 467))),
("qCurveTo", ((1323, 478), (1310, 492), (1302, 492))),
("qCurveTo", ((1295, 492), (1289, 484))),
("lineTo", ((1272, 461),)),
("qCurveTo", ((1256, 439), (1221, 416), (1200, 416))),
("qCurveTo", ((1181, 416), (1141, 440), (1141, 462))),
("qCurveTo", ((1141, 484), (1190, 565), (1190, 594))),
("qCurveTo", ((1190, 607), (1181, 634), (1168, 634))),
("qCurveTo", ((1149, 634), (1146, 583), (1081, 496), (1081, 463))),
("qCurveTo", ((1081, 417), (1164, 347), (1204, 347))),
("closePath", ()),
]
outPen = RecordingPen()
q2cPen = Qu2CuPen(outPen, 1.0, all_cubic=True)
inPen.replay(q2cPen)
print(outPen.value)
assert not any(typ == "qCurveTo" for typ, _ in outPen.value)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()