Mercurial > MadButterfly
annotate tools/svg2code.py @ 83:ea758bb3bbe2
example
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 22 Aug 2008 00:12:04 +0800 |
parents | 13fdf205047b |
children | 42698de1f653 |
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): |
64 | 9 stops = [] |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
10 for node in parent.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 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
|
12 style = node.getAttribute('style') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 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
|
14 if prop.strip()] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 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
|
16 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
|
17 for prop in style_kvpairs] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 style_map = dict(style_kvpairs) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 color = style_map['stop-color'].strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 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
|
27 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 opacity = style_map['stop-opacity'] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 offset = node.getAttribute('offset') |
64 | 30 stops.append('[COLOR_STOP([%s], %f, %f, %f, %f, %f)]' % ( |
31 parent_id, r, g, b, float(opacity), float(offset))) | |
62
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 |
64 | 34 print >> codefo, '%sdnl' % (', '.join(stops)) |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 def translate_linearGradient(linear, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 linear_id = linear.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 if linear.hasAttribute('x1'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 x1 = float(linear.getAttribute('x1')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 y1 = float(linear.getAttribute('y1')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 x2 = float(linear.getAttribute('x2')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 y2 = float(linear.getAttribute('y2')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
45 x1 = y1 = x2 = y2 = 0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 pass |
63 | 47 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
|
48 linear_id, x1, y1, x2, y2) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 translate_stops(linear, codefo, linear_id) |
63 | 50 print >> codefo, '])dnl' |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 href = linear.getAttributeNS(xlinkns, 'href').strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 if href and href[0] == '#': |
78 | 54 print >> codefo, 'REF_STOPS_LINEAR([%s], [%s])dnl' % ( |
55 linear_id, href[1:]) | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
59 def translate_radialGradient(radial, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 radial_id = radial.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
61 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
62 cx = float(radial.getAttribute('cx')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
63 cy = float(radial.getAttribute('cy')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 cx = cy = 0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
67 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 r = float(radial.getAttribute('r')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
69 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
70 r = 0.5 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
71 pass |
63 | 72 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
|
73 radial_id, cx, cy, r) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
74 translate_stops(radial, codefo, radial_id) |
63 | 75 print >>codefo, '])dnl' |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
76 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
77 href = radial.getAttributeNS(xlinkns, 'href').strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
78 if href[0] == '#': |
78 | 79 print >> codefo, 'REF_STOPS_RADIAL([%s], [%s])dnl' % ( |
80 radial_id, href[1:]) | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
81 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
82 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
83 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
84 def translate_defs(defs, codefo, doc): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
85 for node in defs.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
86 if node.namespaceURI != svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
87 continue |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
88 if node.localName == 'linearGradient': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
89 translate_linearGradient(node, codefo, doc) |
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 elif node.localName == 'radialGradient': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
92 translate_radialGradient(node, codefo, doc) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
93 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
95 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
96 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
97 def trans_color(code): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 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
|
99 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
|
100 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
|
101 |
83 | 102 def get_style_map(style_str): |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
103 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
|
104 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
|
105 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
|
106 prop_map = dict(prop_kvs) |
83 | 107 return prop_map |
108 | |
109 def translate_style(node, coord_id, codefo, doc, prefix): | |
110 node_id = node.getAttribute('id') | |
111 style_str = node.getAttribute('style') | |
112 prop_map = get_style_map(style_str) | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
113 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
114 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
115 opacity = float(node.getAttribute('opacity')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
116 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
117 opacity = 1.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
118 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
119 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
120 if prop_map.has_key('fill'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
121 fill = prop_map['fill'].strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
122 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
|
123 r, g, b = trans_color(fill) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
124 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
|
125 node_id, r, g, b, opacity) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
126 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
|
127 paint_id = fill[5:-1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
128 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
|
129 node_id, paint_id) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
130 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
131 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
|
132 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
133 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
134 try: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
135 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
|
136 except: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
137 stroke_opacity = 1.0 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
138 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
139 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
140 if prop_map.has_key('stroke'): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
141 stroke = prop_map['stroke'].strip() |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
142 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
|
143 r, g, b = trans_color(stroke) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 paint_id = stroke[5:-1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
148 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
|
149 node_id, paint_id) |
83 | 150 elif stroke.lower() == 'none': |
151 pass | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
152 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
153 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
|
154 % (stroke) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
155 pass |
80
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
156 |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
157 if prop_map.has_key('stroke-width'): |
83 | 158 if prop_map['stroke-width'].endswith('px'): |
159 stroke_width = float(prop_map['stroke-width'][:-2]) | |
160 else: | |
161 stroke_width = float(prop_map['stroke-width']) | |
162 pass | |
80
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
163 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % ( |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
164 node_id, stroke_width) |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
165 pass |
81 | 166 |
167 if prop_map.has_key('display'): | |
168 display = prop_map['display'].strip().lower() | |
169 if display == 'none': | |
170 print >> codefo, '%sHIDE([%s])dnl' % (prefix, node_id) | |
171 pass | |
172 pass | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
173 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
174 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
175 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
|
176 path_id = path.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
177 d = path.getAttribute('d') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
178 print >> codefo, 'dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
179 print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id) |
81 | 180 translate_style(path, coord_id, codefo, doc, 'PATH_') |
62
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 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
183 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
|
184 rect_id = rect.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
185 x = float(rect.getAttribute('x')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
186 y = float(rect.getAttribute('y')) |
80
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
187 rx = 0.0 |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
188 if rect.hasAttribute('rx'): |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
189 rx = float(rect.getAttribute('rx')) |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
190 pass |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
191 ry = 0.0 |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
192 if rect.hasAttribute('ry'): |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
193 ry = float(rect.getAttribute('ry')) |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
194 pass |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
195 width = float(rect.getAttribute('width')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
196 height = float(rect.getAttribute('height')) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
197 print >> codefo, 'dnl' |
80
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
198 print >> codefo, 'ADD_RECT([%s], %f, %f, %f, %f, %f, %f, [%s])dnl' % ( |
e548221c04eb
svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
199 rect_id, x, y, width, height, rx, ry, coord_id) |
81 | 200 translate_style(rect, coord_id, codefo, doc, 'RECT_') |
62
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 |
83 | 203 def translate_font_style(text, codefo): |
204 text_id = text.getAttribute('id') | |
205 style_str = text.getAttribute('style') | |
206 style_map = get_style_map(style_str) | |
207 | |
208 font_sz = 10.0 | |
209 if style_map.has_key('font-size'): | |
210 if style_map['font-size'].endswith('px'): | |
211 font_sz = float(style_map['font-size'][:-2]) | |
212 print >> codefo, 'define([MB_FONT_SZ], %f)dnl' % (font_sz) | |
213 pass | |
214 pass | |
215 | |
216 font_style = 'normal' | |
217 if style_map.has_key('font-style'): | |
218 font_style = style_map['font-style'].lower() | |
219 pass | |
220 | |
221 font_family = 'Roman' | |
222 if style_map.has_key('font-family'): | |
223 font_family = style_map['font-family'].lower() | |
224 pass | |
225 pass | |
226 | |
227 def translate_text(text, coord_id, codefo, doc): | |
228 translate_font_style(text, codefo) | |
229 | |
230 txt_strs = [] | |
231 for node in text.childNodes: | |
232 if node.localName == None: | |
233 txt_strs.append(node.data) | |
234 elif node.localName == 'tspan': | |
235 node.setAttribute('style', text.getAttribute('style')) | |
236 translate_text(node, coord_id, codefo, doc) | |
237 pass | |
238 pass | |
239 if txt_strs: | |
240 text_id = text.getAttribute('id') | |
241 x = float(text.getAttribute('x')) | |
242 y = float(text.getAttribute('y')) | |
243 print >> codefo, 'dnl' | |
244 print >> codefo, \ | |
245 'ADD_TEXT([%s], [%s], %f, %f, MB_FONT_SZ, [%s])dnl' % ( | |
246 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'), | |
247 x, y, coord_id.encode('utf8')) | |
248 translate_style(text, coord_id, codefo, doc, 'TEXT_') | |
249 pass | |
250 pass | |
251 | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
252 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
|
253 group_id = group.getAttribute('id') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
254 print >> codefo, 'dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
255 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (group_id, parent_id) |
81 | 256 translate_style(group, group_id, codefo, doc, 'GROUP_') |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
257 for node in group.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
258 if node.namespaceURI != svgns: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
259 continue |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
260 if node.localName == 'g': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
261 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
|
262 elif node.localName == 'path': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
263 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
|
264 elif node.localName == 'rect': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
265 translate_rect(node, group_id, codefo, doc) |
83 | 266 elif node.localName == 'text': |
267 translate_text(node, group_id, codefo, doc) | |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
268 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
269 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
270 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
271 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
272 def svg_2_code(dom, codefo): |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
273 for node in dom.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
274 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
|
275 break; |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
276 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
277 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
278 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
|
279 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
280 svg = node |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
281 for node in svg.childNodes: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
282 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
|
283 translate_defs(node, codefo, dom) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
284 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
285 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
|
286 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
|
287 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
288 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
289 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
290 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
291 if __name__ == '__main__': |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
292 from os import path |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
293 if len(sys.argv) == 3: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
294 svgfn = sys.argv[1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
295 codefn = sys.argv[2] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
296 elif len(sys.argv) == 2: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
297 svgfn = sys.argv[1] |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
298 codefn = 'out.mb' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
299 else: |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
300 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
|
301 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
302 |
65
35c2b7ba140b
Use file name of generated M4 script as name of header
Thinker K.F. Li <thinker@branda.to>
parents:
64
diff
changeset
|
303 struct_name = path.basename(codefn).split('.')[0] |
62
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
304 |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
305 dom = parse(svgfn) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
306 codefo = file(codefn, 'w+') |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
307 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
|
308 svg_2_code(dom, codefo) |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
309 print >> codefo, '])dnl' |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
310 pass |
7d976d925431
Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
311 |