Mercurial > MadButterfly
annotate tools/svg2code.py @ 63:f4b792afa74e
m4 translator
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 12 Aug 2008 14:21:09 +0800 |
parents | 7d976d925431 |
children | c668c5c3ceae |
rev | line source |
---|---|
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
2 from xml.dom.minidom import parse |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 import sys |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 svgns='http://www.w3.org/2000/svg' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
6 xlinkns='http://www.w3.org/1999/xlink' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
8 def translate_stops(parent, codefo, parent_id): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 for node in parent.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
10 if node.localName == 'stop' and node.namespaceURI == svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 style = node.getAttribute('style') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 style_props = [prop.strip() for prop in style.split(';') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 if prop.strip()] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 style_kvpairs = [prop.split(':') for prop in style_props] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 style_kvpairs = [(prop[0].strip(), prop[1].strip()) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 for prop in style_kvpairs] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
17 style_map = dict(style_kvpairs) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 color = style_map['stop-color'].strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 if len(color) == 7 and color[0] == '#': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 r = float(int(color[1:3], 16)) / 255.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 g = float(int(color[3:5], 16)) / 255.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
23 b = float(int(color[5:7], 16)) / 255.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
25 raise ValueError, '\'%s\' is invalid color value.' % (color) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 opacity = style_map['stop-opacity'] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 offset = node.getAttribute('offset') |
63 | 29 print >> codefo, '[COLOR_STOP([%s], %f, %f, %f, %f, %f)],' % ( |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
30 parent_id, r, g, b, float(opacity), float(offset)) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
31 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 def translate_linearGradient(linear, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 linear_id = linear.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 if linear.hasAttribute('x1'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 x1 = float(linear.getAttribute('x1')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 y1 = float(linear.getAttribute('y1')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 x2 = float(linear.getAttribute('x2')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 y2 = float(linear.getAttribute('y2')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 x1 = y1 = x2 = y2 = 0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 pass |
63 | 45 print >> codefo, 'ADD_LINEAR_PAINT([%s], %f, %f, %f, %f, [' % ( |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 linear_id, x1, y1, x2, y2) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
47 translate_stops(linear, codefo, linear_id) |
63 | 48 print >> codefo, '])dnl' |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 href = linear.getAttributeNS(xlinkns, 'href').strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 if href and href[0] == '#': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 print >> codefo, 'REF_STOPS([%s], [%s])dnl' % (linear_id, href[1:]) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 def translate_radialGradient(radial, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 radial_id = radial.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
59 cx = float(radial.getAttribute('cx')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 cy = float(radial.getAttribute('cy')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
61 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
62 cx = cy = 0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
63 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 r = float(radial.getAttribute('r')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
67 r = 0.5 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 pass |
63 | 69 print >> codefo, 'ADD_RADIAL_PAINT([%s], %f, %f, %f, [' % ( |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
70 radial_id, cx, cy, r) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
71 translate_stops(radial, codefo, radial_id) |
63 | 72 print >>codefo, '])dnl' |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
73 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
74 href = radial.getAttributeNS(xlinkns, 'href').strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
75 if href[0] == '#': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
76 print >> codefo, 'REF_STOPS([%s], [%s])dnl' % (radial_id, href[1:]) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
77 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
78 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
79 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
80 def translate_defs(defs, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
81 for node in defs.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
82 if node.namespaceURI != svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
83 continue |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
84 if node.localName == 'linearGradient': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
85 translate_linearGradient(node, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
86 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
87 elif node.localName == 'radialGradient': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
88 translate_radialGradient(node, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
89 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
90 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
91 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
92 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
93 def trans_color(code): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 return int(code[1:3], 16) / 255.0, \ |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
95 int(code[3:5], 16) / 255.0, \ |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
96 int(code[5:7], 16) / 255.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
97 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 def translate_style(node, coord_id, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
99 node_id = node.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
100 style_str = node.getAttribute('style') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
101 prop_strs = [s.strip() for s in style_str.split(';')] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
102 prop_kvs = [s.split(':') for s in prop_strs if s] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
103 prop_kvs = [(k.strip(), v.strip()) for k, v in prop_kvs] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
104 prop_map = dict(prop_kvs) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
105 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
106 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
107 opacity = float(node.getAttribute('opacity')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
108 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
109 opacity = 1.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
110 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
111 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
112 if prop_map.has_key('fill'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
113 fill = prop_map['fill'].strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
114 if fill.startswith('#') and len(fill) == 7: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
115 r, g, b = trans_color(fill) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
116 print >> codefo, 'FILL_SHAPE([%s], %f, %f, %f, %f)dnl' % ( |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
117 node_id, r, g, b, opacity) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
118 elif fill.startswith('url(') and fill.endswith(')'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
119 paint_id = fill[5:-1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
120 print >> codefo, 'FILL_SHAPE_WITH_PAINT([%s], [%s])dnl' % ( |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
121 node_id, paint_id) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
122 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
123 raise ValueError, '\'%s\' is an invalid value for fill.' % (fill) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
124 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
125 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
126 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
127 stroke_opacity = float(node.getAttribute('stroke-opacity')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
128 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
129 stroke_opacity = 1.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
130 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
131 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
132 if prop_map.has_key('stroke'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
133 stroke = prop_map['stroke'].strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
134 if stroke.startswith('#') and len(stroke) == 7: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
135 r, g, b = trans_color(stroke) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
136 print >> codefo, 'STROKE_SHAPE([%s], %f, %f, %f, %f)dnl' % ( |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
137 node_id, r, g, b, stroke_opacity) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
138 elif stroke.startswith('url(') and stroke.endswith(')'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
139 paint_id = stroke[5:-1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
140 print >> codefo, 'STROKE_SHAPE_WITH_PAINT([%s], [%s])dnl' % ( |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
141 node_id, paint_id) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
142 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
143 raise ValueError, '\'%s\' is an invalid value for stroke.' \ |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
144 % (stroke) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
145 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
146 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
147 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
148 def translate_path(path, coord_id, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
149 path_id = path.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
150 d = path.getAttribute('d') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
151 print >> codefo, 'dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
152 print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
153 translate_style(path, coord_id, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
154 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
155 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
156 def translate_rect(rect, coord_id, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
157 rect_id = rect.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
158 x = float(rect.getAttribute('x')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
159 y = float(rect.getAttribute('y')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
160 width = float(rect.getAttribute('width')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
161 height = float(rect.getAttribute('height')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
162 print >> codefo, 'dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
163 print >> codefo, 'ADD_RECT([%s], %f, %f, %f, %f, [%s])dnl' % ( |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
164 rect_id, x, y, width, height, coord_id) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
165 translate_style(rect, coord_id, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
166 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
167 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
168 def translate_group(group, parent_id, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
169 group_id = group.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
170 print >> codefo, 'dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
171 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (group_id, parent_id) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
172 for node in group.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
173 if node.namespaceURI != svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
174 continue |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
175 if node.localName == 'g': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
176 translate_group(node, group_id, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
177 elif node.localName == 'path': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
178 translate_path(node, group_id, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
179 elif node.localName == 'rect': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
180 translate_rect(node, group_id, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
181 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
182 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
183 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
184 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
185 def svg_2_code(dom, codefo): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
186 for node in dom.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
187 if node.localName == 'svg' and node.namespaceURI == svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
188 break; |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
189 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
190 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
191 raise ValueErr, 'no any svg tag node.' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
192 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
193 svg = node |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
194 for node in svg.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
195 if node.localName == 'defs' and node.namespaceURI == svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
196 translate_defs(node, codefo, dom) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
197 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
198 elif node.localName == 'g' and node.namespaceURI == svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
199 translate_group(node, 'root_coord', codefo, dom) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
200 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
201 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
202 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
203 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
204 if __name__ == '__main__': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
205 from os import path |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
206 if len(sys.argv) == 3: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
207 svgfn = sys.argv[1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
208 codefn = sys.argv[2] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
209 elif len(sys.argv) == 2: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
210 svgfn = sys.argv[1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
211 codefn = 'out.mb' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
212 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
213 print >> sys.stderr, '%s <SVG file> [<output>]' % (sys.argv[0]) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
214 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
215 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
216 struct_name = path.basename(svgfn).split('.')[0] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
217 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
218 dom = parse(svgfn) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
219 codefo = file(codefn, 'w+') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
220 print >> codefo, 'MADBUTTERFLY([%s],[dnl' % (struct_name) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
221 svg_2_code(dom, codefo) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
222 print >> codefo, '])dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
223 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
224 |