chiark / gitweb /
Lots of updates on the unused SVG class. (Totally unrelated to Cura right now)
[cura.git] / Cura / util / svg.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import math
5 import re
6 import sys
7 import numpy
8 from xml.etree import ElementTree
9
10 def applyTransformString(matrix, transform):
11         s = transform.find('(')
12         e = transform.find(')')
13         if s < 0 or e < 0:
14                 print 'Unknown transform: %s' % (transform)
15                 return matrix
16         tag = transform[:s]
17         data = map(float, re.split('[ \t,]+', transform[s+1:e].strip()))
18         if tag == 'matrix' and len(data) == 6:
19                 return matrix * numpy.matrix([[data[0],data[1],0],[data[2],data[3],0],[data[4],data[5],1]], numpy.float64)
20         elif tag == 'translate' and len(data) == 1:
21                 return matrix * numpy.matrix([[1,0,data[0]],[0,1,0],[0,0,1]], numpy.float64)
22         elif tag == 'translate' and len(data) == 2:
23                 return matrix * numpy.matrix([[1,0,0],[0,1,0],[data[0],data[1],1]], numpy.float64)
24         elif tag == 'scale' and len(data) == 1:
25                 return matrix * numpy.matrix([[data[0],0,0],[0,data[0],0],[0,0,1]], numpy.float64)
26         elif tag == 'scale' and len(data) == 2:
27                 return matrix * numpy.matrix([[data[0],0,0],[0,data[1],0],[0,0,1]], numpy.float64)
28         elif tag == 'rotate' and len(data) == 1:
29                 r = math.radians(data[0])
30                 return matrix * numpy.matrix([[math.cos(r),math.sin(r),0],[-math.sin(r),math.cos(r),0],[0,0,1]], numpy.float64)
31         elif tag == 'skewX' and len(data) == 1:
32                 return matrix * numpy.matrix([[1,0,0],[math.tan(data[0]),1,0],[0,0,1]], numpy.float64)
33         elif tag == 'skewY' and len(data) == 1:
34                 return matrix * numpy.matrix([[1,math.tan(data[0]),0],[0,1,0],[0,0,1]], numpy.float64)
35         print 'Unknown transform: %s' % (transform)
36         return matrix
37
38 class Path(object):
39         LINE = 0
40         ARC = 1
41         CURVE = 2
42
43         def __init__(self, x, y, matrix):
44                 self._matrix = matrix
45                 self._relMatrix = numpy.matrix([[matrix[0,0],matrix[0,1]], [matrix[1,0],matrix[1,1]]])
46                 self._startPoint = complex(x, y)
47                 self._points = []
48
49         def addLineTo(self, x, y):
50                 self._points.append({'type': Path.LINE, 'p': complex(x, y)})
51
52         def addArcTo(self, x, y, rot, rx, ry, large, sweep):
53                 self._points.append({
54                         'type': Path.ARC,
55                         'p': complex(x, y),
56                         'rot': rot,
57                         'radius': complex(rx, ry),
58                         'large': large,
59                         'sweep': sweep
60                 })
61
62         def addCurveTo(self, x, y, cp1x, cp1y, cp2x, cp2y):
63                 self._points.append({
64                         'type': Path.CURVE,
65                         'p': complex(x, y),
66                         'cp1': complex(cp1x, cp1y),
67                         'cp2': complex(cp2x, cp2y)
68                 })
69
70         def closePath(self):
71                 self._points.append({'type': Path.LINE, 'p': self._startPoint})
72
73         def getPoints(self):
74                 pointList = [self._m(self._startPoint)]
75                 p1 = self._startPoint
76                 for p in self._points:
77                         if p['type'] == Path.LINE:
78                                 p1 = p['p']
79                                 pointList.append(self._m(p1))
80                         elif p['type'] == Path.ARC:
81                                 p2 = p['p']
82                                 rot = math.radians(p['rot'])
83                                 r = p['radius']
84
85                                 #http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
86                                 diff = (p1 - p2) / 2
87                                 p1alt = diff #TODO: apply rot
88                                 p2alt = -diff #TODO: apply rot
89                                 rx2 = r.real*r.real
90                                 ry2 = r.imag*r.imag
91                                 x1alt2 = p1alt.real*p1alt.real
92                                 y1alt2 = p1alt.imag*p1alt.imag
93
94                                 f = x1alt2 / rx2 + y1alt2 / ry2
95                                 if f >= 1.0:
96                                         r *= math.sqrt(f+0.000001)
97                                         rx2 = r.real*r.real
98                                         ry2 = r.imag*r.imag
99
100                                 f = math.sqrt((rx2*ry2 - rx2*y1alt2 - ry2*x1alt2) / (rx2*y1alt2+ry2*x1alt2))
101                                 if p['large'] == p['sweep']:
102                                         f = -f
103                                 cAlt = f * complex(r.real*p1alt.imag/r.imag, -r.imag*p1alt.real/r.real)
104
105                                 c = cAlt + (p1 + p2) / 2 #TODO: apply rot
106
107                                 a1 = math.atan2((p1alt.imag - cAlt.imag) / r.imag, (p1alt.real - cAlt.real) / r.real)
108                                 a2 = math.atan2((p2alt.imag - cAlt.imag) / r.imag, (p2alt.real - cAlt.real) / r.real)
109
110                                 #if a1 > a2:
111                                 #       a2 += math.pi * 2
112                                 large = abs(a2 - a1) > math.pi
113                                 if large != p['large']:
114                                         if a1 < a2:
115                                                 a1 += math.pi * 2
116                                         else:
117                                                 a2 += math.pi * 2
118
119                                 for n in xrange(0, 16):
120                                         pointList.append(self._m(c + complex(math.cos(a1 + n*(a2-a1)/16) * r.real, math.sin(a1 + n*(a2-a1)/16) * r.imag)))
121
122                                 pointList.append(self._m(p2))
123                                 p1 = p2
124                         elif p['type'] == Path.CURVE:
125                                 p1_ = self._m(p1)
126                                 p2 = self._m(p['p'])
127                                 cp1 = self._m(p['cp1'])
128                                 cp2 = self._m(p['cp2'])
129
130                                 for n in xrange(0, 10):
131                                         f = n / 10.0
132                                         g = 1.0-f
133                                         point = p1_*g*g*g + cp1*3.0*g*g*f + cp2*3.0*g*f*f + p2*f*f*f
134                                         pointList.append(point)
135
136                                 pointList.append(p2)
137                                 p1 = p['p']
138
139                 return pointList
140
141         def getSVGPath(self):
142                 p0 = self._m(self._startPoint)
143                 ret = 'M %f %f ' % (p0.real, p0.imag)
144                 for p in self._points:
145                         if p['type'] == Path.LINE:
146                                 p0 = self._m(p['p'])
147                                 ret += 'L %f %f' % (p0.real, p0.imag)
148                         elif p['type'] == Path.ARC:
149                                 p0 = self._m(p['p'])
150                                 radius = p['radius']
151                                 ret += 'A %f %f 0 %d %d %f %f' % (radius.real, radius.imag, p['large'], p['sweep'], p0.real, p0.imag)
152                         elif p['type'] == Path.CURVE:
153                                 p0 = self._m(p['p'])
154                                 cp1 = self._m(p['cp1'])
155                                 cp2 = self._m(p['cp2'])
156                                 ret += 'C %f %f %f %f %f %f' % (cp1.real, cp1.imag, cp2.real, cp2.imag, p0.real, p0.imag)
157
158                 return ret
159
160         def _m(self, p):
161                 tmp = numpy.matrix([p.real, p.imag, 1], numpy.float64) * self._matrix
162                 return complex(tmp[0,0], tmp[0,1])
163         def _r(self, p):
164                 tmp = numpy.matrix([p.real, p.imag], numpy.float64) * self._relMatrix
165                 return complex(tmp[0,0], tmp[0,1])
166
167 class SVG(object):
168         def __init__(self, filename):
169                 self.tagProcess = {}
170                 self.tagProcess['rect'] = self._processRectTag
171                 self.tagProcess['line'] = self._processLineTag
172                 self.tagProcess['polyline'] = self._processPolylineTag
173                 self.tagProcess['polygon'] = self._processPolygonTag
174                 self.tagProcess['elipse'] = self._processPolygonTag
175                 self.tagProcess['circle'] = self._processCircleTag
176                 self.tagProcess['ellipse'] = self._processEllipseTag
177                 self.tagProcess['path'] = self._processPathTag
178                 self.tagProcess['g'] = self._processGTag
179                 self.tagProcess['a'] = self._processGTag
180                 self.tagProcess['text'] = None #No text implementation yet
181                 self.tagProcess['image'] = None
182                 self.tagProcess['metadata'] = None
183                 self.tagProcess['defs'] = None
184                 self.tagProcess['title'] = None
185                 self.tagProcess['animate'] = None
186                 self.tagProcess['set'] = None
187                 self.tagProcess['script'] = None
188
189                 #From Inkscape
190                 self.tagProcess['namedview'] = None
191                 #From w3c testsuite
192                 self.tagProcess['SVGTestCase'] = None
193
194                 self.paths = []
195                 f = open(filename, "r")
196                 self._processGTag(ElementTree.parse(f).getroot(), numpy.matrix(numpy.identity(3, numpy.float64)))
197                 f.close()
198
199         def _processGTag(self, tag, baseMatrix):
200                 for e in tag:
201                         if e.get('transform') is None:
202                                 matrix = baseMatrix
203                         else:
204                                 matrix = applyTransformString(baseMatrix, e.get('transform'))
205                         tag = e.tag[e.tag.find('}')+1:]
206                         if not tag in self.tagProcess:
207                                 print 'unknown tag: %s' % (tag)
208                         elif self.tagProcess[tag] is not None:
209                                 self.tagProcess[tag](e, matrix)
210
211         def _processLineTag(self, tag, matrix):
212                 x1 = float(tag.get('x1', 0))
213                 y1 = float(tag.get('y1', 0))
214                 x2 = float(tag.get('x2', 0))
215                 y2 = float(tag.get('y2', 0))
216                 p = Path(x1, y1, matrix)
217                 p.addLineTo(x2, y2)
218                 self.paths.append(p)
219
220         def _processPolylineTag(self, tag, matrix):
221                 values = map(float, re.split('[, \t]+', tag.get('points', '').replace('-', ' -').strip()))
222                 p = Path(values[0], values[1], matrix)
223                 for n in xrange(2, len(values)-1, 2):
224                         p.addLineTo(values[n], values[n+1])
225                 self.paths.append(p)
226
227         def _processPolygonTag(self, tag, matrix):
228                 values = map(float, re.split('[, \t]+', tag.get('points', '').replace('-', ' -').strip()))
229                 p = Path(values[0], values[1], matrix)
230                 for n in xrange(2, len(values)-1, 2):
231                         p.addLineTo(values[n], values[n+1])
232                 p.closePath()
233                 self.paths.append(p)
234
235         def _processCircleTag(self, tag, matrix):
236                 cx = float(tag.get('cx', 0))
237                 cy = float(tag.get('cy', 0))
238                 r = float(tag.get('r', 0))
239                 p = Path(cx-r, cy, matrix)
240                 p.addArcTo(cx+r, cy, 0, r, r, False, False)
241                 p.addArcTo(cx-r, cy, 0, r, r, False, False)
242                 self.paths.append(p)
243
244         def _processEllipseTag(self, tag, matrix):
245                 cx = float(tag.get('cx', 0))
246                 cy = float(tag.get('cy', 0))
247                 rx = float(tag.get('rx', 0))
248                 ry = float(tag.get('rx', 0))
249                 p = Path(cx-rx, cy, matrix)
250                 p.addArcTo(cx+rx, cy, 0, rx, ry, False, False)
251                 p.addArcTo(cx-rx, cy, 0, rx, ry, False, False)
252                 self.paths.append(p)
253
254         def _processRectTag(self, tag, matrix):
255                 x = float(tag.get('x', 0))
256                 y = float(tag.get('y', 0))
257                 width = float(tag.get('width', 0))
258                 height = float(tag.get('height', 0))
259                 if width <= 0 or height <= 0:
260                         return
261                 rx = tag.get('rx')
262                 ry = tag.get('ry')
263                 if rx is not None or ry is not None:
264                         if ry is None:
265                                 ry = rx
266                         if rx is None:
267                                 rx = ry
268                         rx = float(rx)
269                         ry = float(ry)
270                         if rx > width / 2:
271                                 rx = width / 2
272                         if ry > height / 2:
273                                 ry = height / 2
274                 else:
275                         rx = 0.0
276                         ry = 0.0
277
278                 if rx > 0 and ry > 0:
279                         p = Path(x+rx, y, matrix)
280                         p.addLineTo(x+width-rx, y)
281                         p.addArcTo(x+width,y+ry, 0, rx, ry, False, True)
282                         p.addLineTo(x+width, y+height-ry)
283                         p.addArcTo(x+width-rx,y+height, 0, rx, ry, False, True)
284                         p.addLineTo(x+rx, y+height)
285                         p.addArcTo(x,y+height-ry, 0, rx, ry, False, True)
286                         p.addLineTo(x, y+ry)
287                         p.addArcTo(x+rx,y, 0, rx, ry, False, True)
288                         self.paths.append(p)
289                 else:
290                         p = Path(x, y, matrix)
291                         p.addLineTo(x,y+height)
292                         p.addLineTo(x+width,y+height)
293                         p.addLineTo(x+width,y)
294                         p.closePath()
295                         self.paths.append(p)
296
297         def _processPathTag(self, tag, matrix):
298                 pathString = tag.get('d', '').replace(',', ' ').replace('-', ' -')
299                 x = 0
300                 y = 0
301                 path = None
302                 for command in re.findall('[a-df-zA-DF-Z][^a-df-zA-DF-Z]*', pathString):
303                         params = re.split(' +', command[1:].strip())
304                         if len(params) > 0 and params[0] == '':
305                                 params = params[1:]
306                         if len(params) > 0 and params[-1] == '':
307                                 params = params[:-1]
308                         params = map(float, params)
309                         command = command[0]
310
311                         if command == 'm':
312                                 x += params[0]
313                                 y += params[1]
314                                 path = Path(x, y, matrix)
315                                 self.paths.append(path)
316                                 params = params[2:]
317                                 while len(params) > 1:
318                                         x += params[0]
319                                         y += params[1]
320                                         params = params[2:]
321                                         path.addLineTo(x, y)
322                         elif command == 'M':
323                                 x = params[0]
324                                 y = params[1]
325                                 path = Path(x, y, matrix)
326                                 self.paths.append(path)
327                                 params = params[2:]
328                                 while len(params) > 1:
329                                         x = params[0]
330                                         y = params[1]
331                                         params = params[2:]
332                                         path.addLineTo(x, y)
333                         elif command == 'l':
334                                 while len(params) > 1:
335                                         x += params[0]
336                                         y += params[1]
337                                         params = params[2:]
338                                         path.addLineTo(x, y)
339                         elif command == 'L':
340                                 while len(params) > 1:
341                                         x = params[0]
342                                         y = params[1]
343                                         params = params[2:]
344                                         path.addLineTo(x, y)
345                         elif command == 'h':
346                                 x += params[0]
347                                 path.addLineTo(x, y)
348                         elif command == 'H':
349                                 x = params[0]
350                                 path.addLineTo(x, y)
351                         elif command == 'v':
352                                 y += params[0]
353                                 path.addLineTo(x, y)
354                         elif command == 'V':
355                                 y = params[0]
356                                 path.addLineTo(x, y)
357                         elif command == 'a':
358                                 while len(params) > 6:
359                                         x += params[5]
360                                         y += params[6]
361                                         path.addArcTo(x, y, params[2], params[0], params[1], params[3] > 0, params[4] > 0)
362                                         params = params[7:]
363                         elif command == 'A':
364                                 while len(params) > 6:
365                                         x = params[5]
366                                         y = params[6]
367                                         path.addArcTo(x, y, params[2], params[0], params[1], params[3] > 0, params[4] > 0)
368                                         params = params[7:]
369                         elif command == 'c':
370                                 while len(params) > 5:
371                                         c1x = x + params[0]
372                                         c1y = y + params[1]
373                                         c2x = x + params[2]
374                                         c2y = y + params[3]
375                                         x += params[4]
376                                         y += params[5]
377                                         path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
378                                         params = params[6:]
379                         elif command == 'C':
380                                 while len(params) > 5:
381                                         c1x = params[0]
382                                         c1y = params[1]
383                                         c2x = params[2]
384                                         c2y = params[3]
385                                         x = params[4]
386                                         y = params[5]
387                                         path.addCurveTo(x, y, c1x, c1y, c2x, c2y)
388                                         params = params[6:]
389                         elif command == 'z' or command == 'Z':
390                                 path.closePath()
391                                 x = path._startPoint.real
392                                 y = path._startPoint.imag
393                         else:
394                                 print 'Unknown path command:', command, params
395
396
397 if __name__ == '__main__':
398         for n in xrange(1, len(sys.argv)):
399                 print 'File: %s' % (sys.argv[n])
400                 svg = SVG(sys.argv[n])
401
402         f = open("test_export.html", "w")
403
404         f.write("<!DOCTYPE html><html><body>\n")
405         f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" style='width:%dpx;height:%dpx'>\n" % (1000, 1000))
406         f.write("<g fill-rule='evenodd' style=\"fill: gray; stroke:black;stroke-width:2\">\n")
407         f.write("<path d=\"")
408         for path in svg.paths:
409                 points = path.getPoints()
410                 f.write("M %f %f " % (points[0].real, points[0].imag))
411                 for point in points[1:]:
412                         f.write("L %f %f " % (point.real, point.imag))
413         f.write("\"/>")
414         f.write("</g>\n")
415
416         f.write("<g style=\"fill: none; stroke:red;stroke-width:1\">\n")
417         f.write("<path d=\"")
418         for path in svg.paths:
419                 f.write(path.getSVGPath())
420         f.write("\"/>")
421         f.write("</g>\n")
422
423         f.write("</svg>\n")
424         f.write("</body></html>")
425         f.close()
426