1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
8 from xml.etree import ElementTree
10 from Cura.util.drawingLoader import drawing
12 def applyTransformString(matrix, transform):
13 while transform != '':
14 if transform[0] == ',':
15 transform = transform[1:].strip()
16 s = transform.find('(')
17 e = transform.find(')')
19 print 'Unknown transform: %s' % (transform)
22 data = map(float, re.split('[ \t,]+', transform[s+1:e].strip()))
23 if tag == 'matrix' and len(data) == 6:
24 matrix = numpy.matrix([[data[0],data[1],0],[data[2],data[3],0],[data[4],data[5],1]], numpy.float64) * matrix
25 elif tag == 'translate' and len(data) == 1:
26 matrix = numpy.matrix([[1,0,data[0]],[0,1,0],[0,0,1]], numpy.float64) * matrix
27 elif tag == 'translate' and len(data) == 2:
28 matrix = numpy.matrix([[1,0,0],[0,1,0],[data[0],data[1],1]], numpy.float64) * matrix
29 elif tag == 'scale' and len(data) == 1:
30 matrix = numpy.matrix([[data[0],0,0],[0,data[0],0],[0,0,1]], numpy.float64) * matrix
31 elif tag == 'scale' and len(data) == 2:
32 matrix = numpy.matrix([[data[0],0,0],[0,data[1],0],[0,0,1]], numpy.float64) * matrix
33 elif tag == 'rotate' and len(data) == 1:
34 r = math.radians(data[0])
35 matrix = numpy.matrix([[math.cos(r),math.sin(r),0],[-math.sin(r),math.cos(r),0],[0,0,1]], numpy.float64) * matrix
36 elif tag == 'rotate' and len(data) == 3:
37 matrix = numpy.matrix([[1,0,0],[0,1,0],[data[1],data[2],1]], numpy.float64) * matrix
38 r = math.radians(data[0])
39 matrix = numpy.matrix([[math.cos(r),math.sin(r),0],[-math.sin(r),math.cos(r),0],[0,0,1]], numpy.float64) * matrix
40 matrix = numpy.matrix([[1,0,0],[0,1,0],[-data[1],-data[2],1]], numpy.float64) * matrix
41 elif tag == 'skewX' and len(data) == 1:
42 matrix = numpy.matrix([[1,0,0],[math.tan(data[0]),1,0],[0,0,1]], numpy.float64) * matrix
43 elif tag == 'skewY' and len(data) == 1:
44 matrix = numpy.matrix([[1,math.tan(data[0]),0],[0,1,0],[0,0,1]], numpy.float64) * matrix
46 print 'Unknown transform: %s' % (transform)
48 transform = transform[e+1:].strip()
52 f = re.search('^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?', f).group(0)
56 def __init__(self, filename):
58 self.tagProcess['rect'] = self._processRectTag
59 self.tagProcess['line'] = self._processLineTag
60 self.tagProcess['polyline'] = self._processPolylineTag
61 self.tagProcess['polygon'] = self._processPolygonTag
62 self.tagProcess['elipse'] = self._processPolygonTag
63 self.tagProcess['circle'] = self._processCircleTag
64 self.tagProcess['ellipse'] = self._processEllipseTag
65 self.tagProcess['path'] = self._processPathTag
66 self.tagProcess['use'] = self._processUseTag
67 self.tagProcess['g'] = self._processGTag
68 self.tagProcess['a'] = self._processGTag
69 self.tagProcess['svg'] = self._processGTag
70 self.tagProcess['text'] = None #No text implementation yet
71 self.tagProcess['image'] = None
72 self.tagProcess['metadata'] = None
73 self.tagProcess['defs'] = None
74 self.tagProcess['style'] = None
75 self.tagProcess['marker'] = None
76 self.tagProcess['desc'] = None
77 self.tagProcess['filter'] = None
78 self.tagProcess['linearGradient'] = None
79 self.tagProcess['radialGradient'] = None
80 self.tagProcess['pattern'] = None
81 self.tagProcess['title'] = None
82 self.tagProcess['animate'] = None
83 self.tagProcess['animateColor'] = None
84 self.tagProcess['animateTransform'] = None
85 self.tagProcess['set'] = None
86 self.tagProcess['script'] = None
89 self.tagProcess['namedview'] = None
91 self.tagProcess['SVGTestCase'] = None
94 f = open(filename, "r")
95 self._xml = ElementTree.parse(f)
96 self._recursiveCount = 0
97 self._processGTag(self._xml.getroot(), numpy.matrix(numpy.identity(3, numpy.float64)))
101 for path in self.paths:
102 if not path.isClosed():
105 def _processGTag(self, tag, baseMatrix):
107 if e.get('transform') is None:
110 matrix = applyTransformString(baseMatrix, e.get('transform'))
111 tagName = e.tag[e.tag.find('}')+1:]
112 if not tagName in self.tagProcess:
113 print 'unknown tag: %s' % (tagName)
114 elif self.tagProcess[tagName] is not None:
115 self.tagProcess[tagName](e, matrix)
117 def _processUseTag(self, tag, baseMatrix):
118 if self._recursiveCount > 16:
120 self._recursiveCount += 1
121 id = tag.get('{http://www.w3.org/1999/xlink}href')
123 for e in self._xml.findall(".//*[@id='%s']" % (id[1:])):
124 if e.get('transform') is None:
127 matrix = applyTransformString(baseMatrix, e.get('transform'))
128 tagName = e.tag[e.tag.find('}')+1:]
129 if not tagName in self.tagProcess:
130 print 'unknown tag: %s' % (tagName)
131 elif self.tagProcess[tagName] is not None:
132 self.tagProcess[tagName](e, matrix)
133 self._recursiveCount -= 1
135 def _processLineTag(self, tag, matrix):
136 x1 = toFloat(tag.get('x1', '0'))
137 y1 = toFloat(tag.get('y1', '0'))
138 x2 = toFloat(tag.get('x2', '0'))
139 y2 = toFloat(tag.get('y2', '0'))
140 p = drawing.Path(x1, y1, matrix)
144 def _processPolylineTag(self, tag, matrix):
145 values = map(toFloat, re.split('[, \t]+', tag.get('points', '').strip()))
146 p = drawing.Path(values[0], values[1], matrix)
147 for n in xrange(2, len(values)-1, 2):
148 p.addLineTo(values[n], values[n+1])
151 def _processPolygonTag(self, tag, matrix):
152 values = map(toFloat, re.split('[, \t]+', tag.get('points', '').strip()))
153 p = drawing.Path(values[0], values[1], matrix)
154 for n in xrange(2, len(values)-1, 2):
155 p.addLineTo(values[n], values[n+1])
159 def _processCircleTag(self, tag, matrix):
160 cx = toFloat(tag.get('cx', '0'))
161 cy = toFloat(tag.get('cy', '0'))
162 r = toFloat(tag.get('r', '0'))
163 p = drawing.Path(cx-r, cy, matrix)
164 p.addArcTo(cx+r, cy, 0, r, r, False, False)
165 p.addArcTo(cx-r, cy, 0, r, r, False, False)
168 def _processEllipseTag(self, tag, matrix):
169 cx = toFloat(tag.get('cx', '0'))
170 cy = toFloat(tag.get('cy', '0'))
171 rx = toFloat(tag.get('rx', '0'))
172 ry = toFloat(tag.get('rx', '0'))
173 p = drawing.Path(cx-rx, cy, matrix)
174 p.addArcTo(cx+rx, cy, 0, rx, ry, False, False)
175 p.addArcTo(cx-rx, cy, 0, rx, ry, False, False)
178 def _processRectTag(self, tag, matrix):
179 x = toFloat(tag.get('x', '0'))
180 y = toFloat(tag.get('y', '0'))
181 width = toFloat(tag.get('width', '0'))
182 height = toFloat(tag.get('height', '0'))
183 if width <= 0 or height <= 0:
187 if rx is not None or ry is not None:
202 if rx > 0 and ry > 0:
203 p = drawing.Path(x+width-rx, y, matrix)
204 p.addArcTo(x+width,y+ry, 0, rx, ry, False, True)
205 p.addLineTo(x+width, y+height-ry)
206 p.addArcTo(x+width-rx,y+height, 0, rx, ry, False, True)
207 p.addLineTo(x+rx, y+height)
208 p.addArcTo(x,y+height-ry, 0, rx, ry, False, True)
210 p.addArcTo(x+rx,y, 0, rx, ry, False, True)
214 p = drawing.Path(x, y, matrix)
215 p.addLineTo(x,y+height)
216 p.addLineTo(x+width,y+height)
217 p.addLineTo(x+width,y)
221 def _processPathTag(self, tag, matrix):
222 pathString = tag.get('d', '').replace(',', ' ')
228 for command in re.findall('[a-df-zA-DF-Z][^a-df-zA-DF-Z]*', pathString):
229 params = re.split(' +', command[1:].strip())
230 if len(params) > 0 and params[0] == '':
232 if len(params) > 0 and params[-1] == '':
234 params = map(toFloat, params)
240 path = drawing.Path(x, y, matrix)
241 self.paths.append(path)
243 while len(params) > 1:
252 path = drawing.Path(x, y, matrix)
253 self.paths.append(path)
255 while len(params) > 1:
262 while len(params) > 1:
269 while len(params) > 1:
292 while len(params) > 6:
295 path.addArcTo(x, y, params[2], params[0], params[1], params[3] > 0, params[4] > 0)
299 while len(params) > 6:
302 path.addArcTo(x, y, params[2], params[0], params[1], params[3] > 0, params[4] > 0)
306 while len(params) > 5:
313 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
316 while len(params) > 5:
323 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
326 while len(params) > 3:
333 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
336 while len(params) > 3:
343 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
346 while len(params) > 3:
353 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
356 while len(params) > 3:
363 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
366 while len(params) > 1:
373 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
376 while len(params) > 1:
383 path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
385 elif command == 'z' or command == 'Z':
387 x = path._startPoint.real
388 y = path._startPoint.imag
390 print 'Unknown path command:', command, params
393 if __name__ == '__main__':
394 for n in xrange(1, len(sys.argv)):
395 print 'File: %s' % (sys.argv[n])
396 svg = SVG(sys.argv[n])
398 drawing.saveAsHtml(svg, "test_export.html")