annotate tools/svg2code.py @ 71:45c6ca5b3e80

start svg2code_ex example
author Thinker K.F. Li <thinker@branda.to>
date Wed, 13 Aug 2008 09:16:21 +0800
parents 35c2b7ba140b
children 3645e29e4986
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
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
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
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
30 stops.append('[COLOR_STOP([%s], %f, %f, %f, %f, %f)]' % (
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
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
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
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
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
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
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
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] == '#':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 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
55 pass
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 def translate_radialGradient(radial, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 radial_id = radial.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 cx = float(radial.getAttribute('cx'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 cy = float(radial.getAttribute('cy'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 cx = cy = 0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 r = float(radial.getAttribute('r'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 r = 0.5
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 pass
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
71 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
72 radial_id, cx, cy, r)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 translate_stops(radial, codefo, radial_id)
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
74 print >>codefo, '])dnl'
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 href = radial.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 if href[0] == '#':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 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
79 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 def translate_defs(defs, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 for node in defs.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 if node.localName == 'linearGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 translate_linearGradient(node, codefo, doc)
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 elif node.localName == 'radialGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 translate_radialGradient(node, codefo, doc)
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 pass
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 def trans_color(code):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 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
97 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
98 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
99
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 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
101 node_id = node.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 style_str = node.getAttribute('style')
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)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 opacity = float(node.getAttribute('opacity'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 pass
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 if prop_map.has_key('fill'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 fill = prop_map['fill'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 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
117 r, g, b = trans_color(fill)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 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
119 node_id, r, g, b, opacity)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 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
121 paint_id = fill[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 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
123 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 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
126 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 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
130 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 stroke_opacity = 1.0
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 if prop_map.has_key('stroke'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135 stroke = prop_map['stroke'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136 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
137 r, g, b = trans_color(stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 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
139 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
140 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
141 paint_id = stroke[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142 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
143 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
145 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
146 % (stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
147 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
148 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150 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
151 path_id = path.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
152 d = path.getAttribute('d')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154 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
155 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
156 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
157
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 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
159 rect_id = rect.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
160 x = float(rect.getAttribute('x'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
161 y = float(rect.getAttribute('y'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
162 width = float(rect.getAttribute('width'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
163 height = float(rect.getAttribute('height'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165 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
166 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
167 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
168 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
169
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
170 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
171 group_id = group.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 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
174 for node in group.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
175 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
177 if node.localName == 'g':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
178 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
179 elif node.localName == 'path':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
180 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
181 elif node.localName == 'rect':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
182 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
183 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
184 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
185 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
186
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 def svg_2_code(dom, codefo):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
188 for node in dom.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
189 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
190 break;
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
191 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
192 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
193 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
194
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
195 svg = node
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
196 for node in svg.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
197 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
198 translate_defs(node, codefo, dom)
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 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
201 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
202 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
203 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
204 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
205
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206 if __name__ == '__main__':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
207 from os import path
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
208 if len(sys.argv) == 3:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
210 codefn = sys.argv[2]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 elif len(sys.argv) == 2:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
213 codefn = 'out.mb'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
214 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
215 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
216 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
217
65
35c2b7ba140b Use file name of generated M4 script as name of header
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
218 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
219
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
220 dom = parse(svgfn)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
221 codefo = file(codefn, 'w+')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
222 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
223 svg_2_code(dom, codefo)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
224 print >> codefo, '])dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
225 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
226