annotate tools/svg2code.py @ 62:7d976d925431

Generate C header files for SVG files.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 12 Aug 2008 08:48:16 +0800
parents
children f4b792afa74e
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')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 print >> codefo, 'ADD_STOP([%s], [%f, %f, %f, %f], [%f])dnl' % (
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 print >> codefo, 'ADD_LINEAR_PAINT([%s], [%f, %f, %f, %f])dnl' % (
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)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 href = linear.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 if href and href[0] == '#':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 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
52 pass
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 def translate_radialGradient(radial, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 radial_id = radial.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 cx = float(radial.getAttribute('cx'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 cy = float(radial.getAttribute('cy'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 cx = cy = 0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 r = float(radial.getAttribute('r'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 r = 0.5
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 print >> codefo, 'ADD_RADIAL_PAINT([%s], [%f, %f], %f)dnl' % (
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 radial_id, cx, cy, r)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 translate_stops(radial, codefo, radial_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 href = radial.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 if href[0] == '#':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 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
75 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 def translate_defs(defs, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 for node in defs.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 if node.localName == 'linearGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 translate_linearGradient(node, codefo, doc)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 elif node.localName == 'radialGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 translate_radialGradient(node, codefo, doc)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 pass
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 def trans_color(code):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 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
93 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
94 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
95
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 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
97 node_id = node.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 style_str = node.getAttribute('style')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 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
100 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
101 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
102 prop_map = dict(prop_kvs)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 opacity = float(node.getAttribute('opacity'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 if prop_map.has_key('fill'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 fill = prop_map['fill'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 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
113 r, g, b = trans_color(fill)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 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
115 node_id, r, g, b, opacity)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 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
117 paint_id = fill[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 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
119 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 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
122 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 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
126 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127 stroke_opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 if prop_map.has_key('stroke'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 stroke = prop_map['stroke'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 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
133 r, g, b = trans_color(stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134 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
135 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
136 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
137 paint_id = stroke[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 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
139 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141 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
142 % (stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
143 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
145
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146 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
147 path_id = path.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
148 d = path.getAttribute('d')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150 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
151 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
152 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154 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
155 rect_id = rect.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
156 x = float(rect.getAttribute('x'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
157 y = float(rect.getAttribute('y'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 width = float(rect.getAttribute('width'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
159 height = float(rect.getAttribute('height'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
160 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
161 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
162 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
163 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
164 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
166 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
167 group_id = group.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
168 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
169 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
170 for node in group.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
171 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 if node.localName == 'g':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
174 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
175 elif node.localName == 'path':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 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
177 elif node.localName == 'rect':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
178 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
179 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
180 pass
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 svg_2_code(dom, codefo):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
184 for node in dom.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
185 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
186 break;
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
188 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
189 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
190
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
191 svg = node
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
192 for node in svg.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
193 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
194 translate_defs(node, codefo, dom)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
195 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
196 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
197 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
198 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
199 pass
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202 if __name__ == '__main__':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
203 from os import path
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
204 if len(sys.argv) == 3:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
205 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206 codefn = sys.argv[2]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
207 elif len(sys.argv) == 2:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
208 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 codefn = 'out.mb'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
210 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 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
212 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
213
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
214 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
215
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
216 dom = parse(svgfn)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
217 codefo = file(codefn, 'w+')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
218 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
219 svg_2_code(dom, codefo)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
220 print >> codefo, '])dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
221 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
222