def _curve_to_quadratic(self, pt1, pt2, pt3): curve = (self.current_pt, pt1, pt2, pt3) quadratic = curve_to_quadratic(curve, self.max_err) if self.stats is not None: n = str(len(quadratic) - 2) self.stats[n] = self.stats.get(n, 0) + 1 self.qCurveTo(*quadratic[1:])
def _flushContour(self, segments): assert len(segments) >= 1 closed = segments[0][0] != "move" new_segments = [] prev_points = segments[-1][1] prev_on_curve = prev_points[-1][0] for segment_type, points in segments: if segment_type == 'curve': for sub_points in self._split_super_bezier_segments(points): on_curve, smooth, name, kwargs = sub_points[-1] bcp1, bcp2 = sub_points[0][0], sub_points[1][0] cubic = [prev_on_curve, bcp1, bcp2, on_curve] quad = curve_to_quadratic(cubic, self.max_err) if self.stats is not None: n = str(len(quad) - 2) self.stats[n] = self.stats.get(n, 0) + 1 new_points = [(pt, False, None, {}) for pt in quad[1:-1]] new_points.append((on_curve, smooth, name, kwargs)) new_segments.append(["qcurve", new_points]) prev_on_curve = sub_points[-1][0] else: new_segments.append([segment_type, points]) prev_on_curve = points[-1][0] if closed: # the BasePointToSegmentPen.endPath method that calls _flushContour # rotates the point list of closed contours so that they end with # the first on-curve point. We restore the original starting point. new_segments = new_segments[-1:] + new_segments[:-1] self._drawPoints(new_segments)
def setUpClass(cls): """Do the curve conversion ahead of time, and run tests on results.""" with open(os.path.join(DATADIR, "curves.json"), "r") as fp: curves = json.load(fp) cls.single_splines = [curve_to_quadratic(c, MAX_ERR) for c in curves] cls.single_errors = [ cls.curve_spline_dist(c, s) for c, s in zip(curves, cls.single_splines) ] curve_groups = [curves[i:i + 3] for i in range(0, 300, 3)] cls.compat_splines = [ curves_to_quadratic(c, [MAX_ERR] * 3) for c in curve_groups ] cls.compat_errors = [[ cls.curve_spline_dist(c, s) for c, s in zip(curve_group, splines) ] for curve_group, splines in zip(curve_groups, cls.compat_splines)] cls.results = []