-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeconverter.py
67 lines (58 loc) · 2.45 KB
/
nodeconverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import nodevisitor
from geometry import *
import inkex
from inkex import Transform
from inkex import paths
from inkex import bezier
class NodeToPolylines(nodevisitor.NodeVisitor):
def __init__(self, bbox):
self.flatness = 0.2
self.drawing = Drawing(bbox)
def visit_node(self, node, transform):
if node.tag == '{http://www.w3.org/2000/svg}path':
d = node.get('d')
if not d:
return
elif node.tag == '{http://www.w3.org/2000/svg}rect':
x = float(node.get('x', 0))
y = float(node.get('y', 0))
width = float(node.get('width'))
height = float(node.get('height'))
d = "m %s,%s %s,%s %s,%s %s,%s z" % (x, y, width, 0, 0, height, -width, 0)
elif node.tag == '{http://www.w3.org/2000/svg}line':
x1 = float(node.get('x1', 0))
x2 = float(node.get('x2', 0))
y1 = float(node.get('y1', 0))
y2 = float(node.get('y2', 0))
d = "M %s,%s L %s,%s" % (x1, y1, x2, y2)
elif node.tag == '{http://www.w3.org/2000/svg}circle':
cx = float(node.get('cx', 0))
cy = float(node.get('cy', 0))
r = float(node.get('r'))
d = "m %s,%s a %s,%s 0 0 1 %s,%s %s,%s 0 0 1 %s,%s z" % (cx + r, cy, r, r, -2*r, 0, r, r, 2*r, 0)
elif node.tag == '{http://www.w3.org/2000/svg}ellipse':
cx = float(node.get('cx', 0))
cy = float(node.get('cy', 0))
rx = float(node.get('rx'))
ry = float(node.get('ry'))
d = "m %s,%s a %s,%s 0 0 1 %s,%s %s,%s 0 0 1 %s,%s z" % (cx + rx, cy, rx, ry, -2*rx, 0, rx, ry, 2*rx, 0)
else:
return
p = paths.CubicSuperPath(paths.Path(d))
p.transform(transform)
bezier.cspsubdiv(p, self.flatness)
for sub in p:
polyline = Drawing.Polyline(self.drawing)
prev=None
for s in sub:
point=Vec2(s[1][0],s[1][1])
# filter out zero length segments
if prev and prev.equals(point):
continue
polyline.add_point(point)
prev=point
if polyline.valid():
self.drawing.polylines.append(polyline)
def accept(self, node, transform = Transform([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])):
self.polylines = []
super(NodeToPolylines, self).accept(node, transform)