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)) 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 _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)) self.stats[n] = self.stats.get(n, 0) + 1 self.qCurveTo(*quadratic[1:])
def points_to_quadratic(p0, p1, p2, p3, max_err): """Return a quadratic spline approximating the cubic bezier defined by these points (or collections of points). """ if hasattr(p0, 'x'): curve = [(float(i.x), float(i.y)) for i in [p0, p1, p2, p3]] return curve_to_quadratic(curve, max_err)[0] curves = [[(float(i.x), float(i.y)) for i in p] for p in zip(p0, p1, p2, p3)] return curves_to_quadratic(curves, max_err)[0]
def setUpClass(cls): """Do the curve conversion ahead of time, and run tests on results.""" random.seed(1) curves = [generate_curve() for i in range(1000)] cls.single_splines, cls.single_errors = zip(*[ curve_to_quadratic(c, MAX_ERR) for c in curves]) cls.compat_splines, cls.compat_errors = zip(*[ curves_to_quadratic(curves[i:i + 3], [MAX_ERR] * 3) for i in range(0, 300, 3)]) cls.results = []
def setUpClass(cls): """Do the curve conversion ahead of time, and run tests on results.""" random.seed(1) curves = [generate_curve() for i in range(1000)] cls.single_splines, cls.single_errors = zip( *[curve_to_quadratic(c, MAX_ERR) for c in curves]) cls.compat_splines, cls.compat_errors = zip(*[ curves_to_quadratic(curves[i:i + 3], [MAX_ERR] * 3) for i in range(0, 300, 3) ]) cls.results = []
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 = []