[qu2cu] Add a test and fix a bug
This commit is contained in:
parent
41732b5904
commit
94ee47c6e4
@ -49,7 +49,7 @@ class Qu2CuPen(ContourFilterPen):
|
||||
self.stats = stats
|
||||
|
||||
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:
|
||||
n = str(len(curves))
|
||||
self.stats[n] = self.stats.get(n, 0) + 1
|
||||
@ -81,6 +81,7 @@ class Qu2CuPen(ContourFilterPen):
|
||||
if quadratics:
|
||||
newContour.extend(self._quadratics_to_curve(quadratics))
|
||||
|
||||
if not self.all_cubic:
|
||||
# Add back implicit oncurve points
|
||||
contour = newContour
|
||||
newContour = []
|
||||
@ -89,8 +90,10 @@ class Qu2CuPen(ContourFilterPen):
|
||||
pt0 = newContour[-1][1][-2]
|
||||
pt1 = newContour[-1][1][-1]
|
||||
pt2 = args[0]
|
||||
if math.isclose(pt2[0] - pt1[0], pt1[0] - pt0[0]) and math.isclose(
|
||||
pt2[1] - pt1[1], pt1[1] - pt0[1]
|
||||
if (
|
||||
pt1 is not None
|
||||
and math.isclose(pt2[0] - pt1[0], pt1[0] - pt0[0])
|
||||
and math.isclose(pt2[1] - pt1[1], pt1[1] - pt0[1])
|
||||
):
|
||||
newArgs = newContour[-1][1][:-1] + args
|
||||
newContour[-1] = (op, newArgs)
|
||||
|
@ -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.
|
||||
sols = [Solution(0, 0, 0, False)]
|
||||
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):
|
||||
|
||||
j_sol_count, j_sol_error = sols[j].num_points, sols[j].error
|
||||
|
@ -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__":
|
||||
unittest.main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user