From 94ee47c6e45f90acf560e472b04fef5f28317037 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 20 Feb 2023 12:22:16 -0700 Subject: [PATCH] [qu2cu] Add a test and fix a bug --- Lib/fontTools/pens/qu2cuPen.py | 35 ++++++++++++++++++---------------- Lib/fontTools/qu2cu/qu2cu.py | 2 +- Tests/pens/qu2cuPen_test.py | 25 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Lib/fontTools/pens/qu2cuPen.py b/Lib/fontTools/pens/qu2cuPen.py index d0b42c42c..caf49d41c 100644 --- a/Lib/fontTools/pens/qu2cuPen.py +++ b/Lib/fontTools/pens/qu2cuPen.py @@ -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,21 +81,24 @@ class Qu2CuPen(ContourFilterPen): if quadratics: newContour.extend(self._quadratics_to_curve(quadratics)) - # Add back implicit oncurve points - contour = newContour - newContour = [] - for op, args in contour: - if op == "qCurveTo" and newContour and newContour[-1][0] == "qCurveTo": - 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] - ): - newArgs = newContour[-1][1][:-1] + args - newContour[-1] = (op, newArgs) - continue + if not self.all_cubic: + # Add back implicit oncurve points + contour = newContour + newContour = [] + for op, args in contour: + if op == "qCurveTo" and newContour and newContour[-1][0] == "qCurveTo": + pt0 = newContour[-1][1][-2] + pt1 = newContour[-1][1][-1] + pt2 = args[0] + 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) + continue - newContour.append((op, args)) + newContour.append((op, args)) return newContour diff --git a/Lib/fontTools/qu2cu/qu2cu.py b/Lib/fontTools/qu2cu/qu2cu.py index 20bfaa93f..ffe9d9402 100644 --- a/Lib/fontTools/qu2cu/qu2cu.py +++ b/Lib/fontTools/qu2cu/qu2cu.py @@ -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 diff --git a/Tests/pens/qu2cuPen_test.py b/Tests/pens/qu2cuPen_test.py index f3f8a3f7c..acb09db5e 100644 --- a/Tests/pens/qu2cuPen_test.py +++ b/Tests/pens/qu2cuPen_test.py @@ -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()