-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvg2can.py
More file actions
159 lines (119 loc) · 3.82 KB
/
Copy pathsvg2can.py
File metadata and controls
159 lines (119 loc) · 3.82 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import xml.etree.ElementTree as ET
class SvgPathNode:
def __init__(self, nodeType='M', x=0.0, y=0.0):
self.nodeType = nodeType
self.x = x
self.y = y
def __repr__(self):
return 'SvgPathNode(type={}, x={}, y={})'.format(self.nodeType, self.x, self.y)
SvgPathNode.PATH_NODE_TYPES = ['M', 'L', 'H', 'V', 'C', 'S', 'Q', 'T', 'A', 'Z']
class SvgPath:
def __init__(self, strPath=''):
self.nodeList = []
if strPath != None and len(strPath) > 0:
pieces = strPath.split()
self.add_nodes_from_string_pieces(pieces)
def add_nodes_from_string_pieces(self, strNode, startPoint=0):
nodeType = strNode[startPoint]
if(not nodeType in SvgPathNode.PATH_NODE_TYPES):
raise Exception('Invalid Path Node Type', 'Must be one of ' + str(SvgPathNode.PATH_NODE_TYPES))
if (nodeType == 'Z'):
self.nodeList.append(SvgPathNode(nodeType, None, None))
return True
if startPoint+2 > len(strNode):
raise Exception('Must have coordinates', '')
x = float(strNode[startPoint + 1])
y = float(strNode[startPoint + 2])
self.nodeList.append(SvgPathNode(nodeType, x, y))
self.add_nodes_from_string_pieces(strNode, startPoint + 3)
def add_node(self, node):
if type(node) is SvgPathNode:
self.nodeList.append(node)
else:
raise Exception('Invalid argument', 'node must be SvgPathNode')
@property
def nodes(self):
return self.nodeList
class Svg2Can:
def __init__(self):
self.min_x = 0
self.min_y = 0
def clean_tag(self, tag):
# Why is this here?
return tag.replace('{http://www.w3.org/2000/svg}', '')
def is_el_drawable(self, el):
return self.clean_tag(el.tag) in Svg2Can.DRAWABLE_ELEMENTS
def is_el_container(self, el):
return self.clean_tag(el.tag) in Svg2Can.CONTAINER_ELEMENTS
def draw_el(self, canvas, el):
if (self.clean_tag(el.tag) == 'rect'):
self.draw_rect(canvas, el)
if (self.clean_tag(el.tag) == 'path'):
self.draw_path(canvas, el)
if (self.clean_tag(el.tag) == 'ellipse'):
self.draw_ellipse(canvas, el)
def draw_path(self, canvas, el):
if (el.get('fill') == None):
return
path = SvgPath(el.get('d'))
coordinates = []
for node in path.nodes:
if (node.nodeType == 'Z'):
break
coordinates.append(node.x - self.min_x)
coordinates.append(node.y - self.min_y)
fillColour = el.get('fill')
canvas.create_polygon(coordinates, fill=fillColour)
def draw_rect(self, canvas, el):
if (el.get('fill') == None):
return
x1 = float(el.get('x')) - self.min_x
y1 = float(el.get('y')) - self.min_y
width = float(el.get('width'))
height = float(el.get('height'))
x2 = x1 + width
y2 = y1 + height
fillColour = el.get('fill')
canvas.create_rectangle(x1, y1, x2, y2, fill=fillColour, width=el.get('stroke-width'))
def draw_ellipse(self, canvas, el):
if (el.get('fill') == None):
return
radius_x = float(el.get('rx'))
radius_y = float(el.get('ry'))
x1 = float(el.get('cx')) - self.min_x - radius_x
y1 = float(el.get('cy')) - self.min_y - radius_y
x2 = x1 + radius_x * 2
y2 = y1 + radius_y * 2
fillColour = el.get('fill')
canvas.create_oval(x1, y1, x2, y2, fill=fillColour)
def draw_children(self, canvas, el):
for child in el:
if self.is_el_drawable(child):
self.draw_el(canvas, child)
if self.is_el_container(child):
self.draw_children(canvas, child)
def draw_svg_from_file_on_canvas(self, canvas, svg_file_path):
svg = ET.parse(svg_file_path)
root = svg.getroot()
self.min_x = float(root.get('viewBox').split()[0])
self.min_y = float(root.get('viewBox').split()[1])
self.draw_children(canvas, root)
Svg2Can.DRAWABLE_ELEMENTS = [
'rect',
'path',
'ellipse'
# This is incomplete; does what I need to for the time being
]
Svg2Can.CONTAINER_ELEMENTS = [
'a',
'defs',
'glyph',
'g',
'marker',
'mask',
'missing-glyph',
'pattern',
'svg',
'switch',
'symbol'
]