Mercurial > MadButterfly
comparison tools/svg2code.py @ 85:9b4a02bcaeb1
matrix() function in transform attribute of group and shapes
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 22 Aug 2008 15:47:03 +0800 |
parents | 42698de1f653 |
children | 7d0580f89468 |
comparison
equal
deleted
inserted
replaced
84:42698de1f653 | 85:9b4a02bcaeb1 |
---|---|
4 import re | 4 import re |
5 | 5 |
6 svgns='http://www.w3.org/2000/svg' | 6 svgns='http://www.w3.org/2000/svg' |
7 xlinkns='http://www.w3.org/1999/xlink' | 7 xlinkns='http://www.w3.org/1999/xlink' |
8 | 8 |
9 re_rgb = re.compile('rgb\\( *([0-9]+(\\.[0-9]+)?) *, *([0-9]+(\\.[0-9]+)?) *, *([0-9]+(\\.[0-9]+)?) *\\)') | |
9 def translate_stops(parent, codefo, parent_id): | 10 def translate_stops(parent, codefo, parent_id): |
10 stops = [] | 11 stops = [] |
11 for node in parent.childNodes: | 12 for node in parent.childNodes: |
12 if node.localName == 'stop' and node.namespaceURI == svgns: | 13 if node.localName == 'stop' and node.namespaceURI == svgns: |
13 style = node.getAttribute('style') | 14 style = node.getAttribute('style') |
22 if len(color) == 7 and color[0] == '#': | 23 if len(color) == 7 and color[0] == '#': |
23 r = float(int(color[1:3], 16)) / 255.0 | 24 r = float(int(color[1:3], 16)) / 255.0 |
24 g = float(int(color[3:5], 16)) / 255.0 | 25 g = float(int(color[3:5], 16)) / 255.0 |
25 b = float(int(color[5:7], 16)) / 255.0 | 26 b = float(int(color[5:7], 16)) / 255.0 |
26 else: | 27 else: |
27 raise ValueError, '\'%s\' is invalid color value.' % (color) | 28 mo = re_rgb.match(color) |
29 if mo: | |
30 r = float(mo.group(1)) | |
31 g = float(mo.group(3)) | |
32 b = float(mo.group(5)) | |
33 else: | |
34 raise ValueError, '\'%s\' is invalid color value.' % (color) | |
35 pass | |
28 | 36 |
29 opacity = style_map['stop-opacity'] | 37 opacity = style_map['stop-opacity'] |
30 offset = node.getAttribute('offset') | 38 offset = node.getAttribute('offset') |
31 stops.append('[COLOR_STOP([%s], %f, %f, %f, %f, %f)]' % ( | 39 stops.append('[COLOR_STOP([%s], %f, %f, %f, %f, %f)]' % ( |
32 parent_id, r, g, b, float(opacity), float(offset))) | 40 parent_id, r, g, b, float(opacity), float(offset))) |
171 print >> codefo, '%sHIDE([%s])dnl' % (prefix, node_id) | 179 print >> codefo, '%sHIDE([%s])dnl' % (prefix, node_id) |
172 pass | 180 pass |
173 pass | 181 pass |
174 pass | 182 pass |
175 | 183 |
184 def translate_shape_transform(shape, coord_id, codefo): | |
185 shape_id = shape.getAttribute('id') | |
186 | |
187 if shape.hasAttribute('transform'): | |
188 shape_coord_id = shape_id + '_coord' | |
189 print >> codefo, 'dnl' | |
190 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % ( | |
191 shape_coord_id, coord_id) | |
192 transform = shape.getAttribute('transform') | |
193 translate_transform(shape_coord_id, transform, codefo, 'SHAPE_') | |
194 coord_id = shape_coord_id | |
195 pass | |
196 return coord_id | |
197 | |
176 def translate_path(path, coord_id, codefo, doc): | 198 def translate_path(path, coord_id, codefo, doc): |
199 coord_id = translate_shape_transform(path, coord_id, codefo) | |
200 | |
177 path_id = path.getAttribute('id') | 201 path_id = path.getAttribute('id') |
178 d = path.getAttribute('d') | 202 d = path.getAttribute('d') |
179 print >> codefo, 'dnl' | 203 print >> codefo, 'dnl' |
180 print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id) | 204 print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id) |
205 | |
181 translate_style(path, coord_id, codefo, doc, 'PATH_') | 206 translate_style(path, coord_id, codefo, doc, 'PATH_') |
182 pass | 207 pass |
183 | 208 |
184 def translate_rect(rect, coord_id, codefo, doc): | 209 def translate_rect(rect, coord_id, codefo, doc): |
210 coord_id = translate_shape_transform(rect, coord_id, codefo) | |
211 | |
185 rect_id = rect.getAttribute('id') | 212 rect_id = rect.getAttribute('id') |
186 x = float(rect.getAttribute('x')) | 213 x = float(rect.getAttribute('x')) |
187 y = float(rect.getAttribute('y')) | 214 y = float(rect.getAttribute('y')) |
188 rx = 0.0 | 215 rx = 0.0 |
189 if rect.hasAttribute('rx'): | 216 if rect.hasAttribute('rx'): |
248 x, y, coord_id.encode('utf8')) | 275 x, y, coord_id.encode('utf8')) |
249 translate_style(text, coord_id, codefo, doc, 'TEXT_') | 276 translate_style(text, coord_id, codefo, doc, 'TEXT_') |
250 pass | 277 pass |
251 pass | 278 pass |
252 | 279 |
253 reo_translate = re.compile('translate\\(([0-9]+),([0-9]+)\\)') | 280 reo_func = re.compile('([a-zA-Z]+)\\([^\\)]*\\)') |
254 def translate_transform(coord_id, transform, codefo): | 281 reo_translate = re.compile('translate\\(([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)\\)') |
282 reo_matrix = re.compile('matrix\\(([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)\\)') | |
283 def translate_transform(coord_id, transform, codefo, prefix): | |
255 transform = transform.strip() | 284 transform = transform.strip() |
256 mo = reo_translate.match(transform) | 285 mo = reo_func.match(transform) |
257 if mo: | 286 if not mo: |
258 x = float(mo.group(1)) | 287 return |
259 y = float(mo.group(2)) | 288 name = mo.group(1) |
260 print >> codefo, 'COORD_TRANSLATE([%s], %f, %f)dnl' % ( | 289 print name |
261 coord_id, x, y) | 290 if name == 'translate': |
291 mo = reo_translate.match(transform) | |
292 if mo: | |
293 x = float(mo.group(1)) | |
294 y = float(mo.group(3)) | |
295 print >> codefo, '%sTRANSLATE([%s], %f, %f)dnl' % ( | |
296 prefix, coord_id, x, y) | |
297 pass | |
298 elif name == 'matrix': | |
299 mo = reo_matrix.match(transform) | |
300 if mo: | |
301 r10, r11, r12 = \ | |
302 float(mo.group(1)), float(mo.group(3)), float(mo.group(5)) | |
303 r20, r21, r22 = \ | |
304 float(mo.group(7)), float(mo.group(9)), float(mo.group(11)) | |
305 print >> codefo, \ | |
306 '%sMATRIX([%s], %f, %f, %f, %f, %f, %f)dnl' % ( | |
307 prefix, coord_id, r10, r11, r12, r20, r21, r22) | |
308 pass | |
262 pass | 309 pass |
263 pass | 310 pass |
264 | 311 |
265 def translate_group(group, parent_id, codefo, doc): | 312 def translate_group(group, parent_id, codefo, doc): |
266 group_id = group.getAttribute('id') | 313 group_id = group.getAttribute('id') |
267 print >> codefo, 'dnl' | 314 print >> codefo, 'dnl' |
268 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (group_id, parent_id) | 315 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (group_id, parent_id) |
269 | 316 |
270 if group.hasAttribute('transform'): | 317 if group.hasAttribute('transform'): |
271 transform = group.getAttribute('transform') | 318 transform = group.getAttribute('transform') |
272 translate_transform(group_id, transform, codefo) | 319 translate_transform(group_id, transform, codefo, 'COORD_') |
273 pass | 320 pass |
274 | 321 |
275 translate_style(group, group_id, codefo, doc, 'GROUP_') | 322 translate_style(group, group_id, codefo, doc, 'GROUP_') |
276 for node in group.childNodes: | 323 for node in group.childNodes: |
277 if node.namespaceURI != svgns: | 324 if node.namespaceURI != svgns: |