def fromFontpartsGlyph(klass, glyph): """Returns an *array of BezierPaths* from a FontParts glyph object.""" paths = [] if hasattr(glyph, "contours"): contouriterator = glyph.contours else: contouriterator = glyph for c in contouriterator: path = BezierPath() path.closed = False nodeList = [] if hasattr(c, "points"): pointiterator = c.points else: pointiterator = c for p in pointiterator: if hasattr(p, "segmentType"): t = p.segmentType else: t = p.type nodeList.append(Node(p.x, p.y, t)) path.activeRepresentation = NodelistRepresentation(path, nodeList) if nodeList[0].point == nodeList[-1].point: path.closed = True paths.append(path) return paths
def test_corners(self): nl = [ Node(302.0, 492.0, "line"), Node(176.0, 432.0, "line"), Node(-51.0, 325.0, "offcurve"), Node(-74.0, 484.0, "offcurve"), Node(73.0, 570.0, "curve"), Node(85.0, 764.0, "offcurve"), Node(290.0, 748.0, "offcurve"), Node(418.0, 688.0, "curve"), ] path = BezierPath.fromNodelist(nl) path.closed = False for seg1, seg2 in path.segpairs(): print(seg1.endAngle * 57.2958, seg2.startAngle * 57.2958)
def fromFontpartsGlyph(klass, glyph): """Returns an *array of BezierPaths* from a FontParts glyph object.""" paths = [] for c in glyph.contours: path = BezierPath() path.closed = False nodeList = [] for p in c.points: nodeList.append(Node(p.x, p.y, p.type)) path.activeRepresentation = NodelistRepresentation(path, nodeList) if nodeList[0].point == nodeList[-1].point: path.closed = True paths.append(path) return paths
def test_inside(self): p = BezierPath.fromNodelist([ Node(329,320,"line"), Node(564,190,"line"), Node(622,332,"offcurve"), Node(495,471,"offcurve"), Node(329,471,"curve"), Node(164,471,"offcurve"), Node(34,334,"offcurve"), Node(93,190,"curve") ]) self.assertTrue(p.pointIsInside(Point(326,423))) self.assertFalse(p.pointIsInside(Point(326,123))) self.assertFalse(p.pointIsInside(Point(326,251))) self.assertTrue(p.pointIsInside(Point(526,251))) self.assertTrue(p.pointIsInside(Point(126,251)))
def toNodelist(self): first = self.segments[0][0] nodelist = [] if len(self.segments[0]) == 2: nodelist.append(Node(first.x, first.y, "line")) else: nodelist.append(Node(first.x, first.y, "curve")) for seg in self.segments: if len(seg) == 4: nodelist.append(Node(seg[1].x, seg[1].y, "offcurve")) nodelist.append(Node(seg[2].x, seg[2].y, "offcurve")) nodelist.append(Node(seg[3].x, seg[3].y, "curve")) elif len(seg) == 3: nodelist.append(Node(seg[1].x, seg[1].y, "offcurve")) nodelist.append(Node(seg[2].x, seg[2].y, "curve")) else: nodelist.append(Node(seg[1].x, seg[1].y, "line")) return nodelist
def toNodelist(self): return list( map(lambda n: Node(n.position.x, n.position.y, n.type), self.nodes))
def _qCurveToOne(self, p1, p2): self.nodeList.append(Node(p1[0], p1[1], "offcurve")) self.nodeList.append(Node(p2[0], p2[1], "curve"))
def _lineTo(self, p): self.nodeList.append(Node(p[0], p[1], "line"))
def _moveTo(self, p): self.nodeList = [Node(p[0], p[1], "move")]
def toNodelist(self): return map(lambda n: Node(n.x, n.y, n.type), self.nodes)
def test_overlap(self): nodes = [ Node(698.0,413.0,"offcurve"), Node(401.0,179.0,"offcurve"), Node(401.0,274.0,"curve"), Node(401.0,368.0,"offcurve"), Node(315.0,445.0,"offcurve"), Node(210.0,445.0,"curve"), Node(104.0,445.0,"offcurve"), Node(18.0,368.0,"offcurve"), Node(18.0,274.0,"curve"), Node(18.0,179.0,"offcurve"), Node(439.0,400.0,"offcurve"), Node(533.0,405.0,"curve") ] p = BezierPath.fromNodelist(nodes) p.closed = True i = p.getSelfIntersections() self.assertEqual(len(i),1) self.assertAlmostEqual(i[0].point.x, 377.71521068) # import matplotlib.pyplot as plt # fig, ax = plt.subplots() # p.plot(ax) # for n in i: # circle = plt.Circle((n.point.x, n.point.y), 2, fill=True, color="red") # ax.add_artist(circle) # plt.show() p = BezierPath.fromNodelist([ Node(310.0,389.0,"line"), Node(453.0,222.0,"line"), Node(289.0,251.0,"line"), Node(447.0,367.0,"line"), Node(578.0,222.0,"line"), Node(210.0,-8.0,"line"), ]) i = p.getSelfIntersections() self.assertEqual(len(i),1) self.assertEqual(i[0].point,Point(374.448829525,313.734583702))
def test_splitatpoints(self): p = BezierPath.fromNodelist([ Node(297.0,86.0,"offcurve"), Node(344.0,138.0,"offcurve"), Node(344.0,203.0,"curve"), Node(344.0,267.0,"offcurve"), Node(297.0,319.0,"offcurve"), Node(240.0,319.0,"curve"), Node(183.0,319.0,"offcurve"), Node(136.0,267.0,"offcurve"), Node(136.0,203.0,"curve"), Node(136.0,138.0,"offcurve"), Node(183.0,86.0,"offcurve"), Node(240.0,86.0,"curve"), ]) splitlist = [] for seg in p.asSegments(): for t in seg.regularSampleTValue(5): splitlist.append((seg,t)) p.splitAtPoints(splitlist) self.assertEqual(len(p.asSegments()),24)