annotate tools/svg2code.py @ 81:13fdf205047b

Hide shapes and groups
author Thinker K.F. Li <thinker@branda.to>
date Thu, 21 Aug 2008 13:52:23 +0800
parents e548221c04eb
children ea758bb3bbe2
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] == '#':
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
54 print >> codefo, 'REF_STOPS_LINEAR([%s], [%s])dnl' % (
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
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
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
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
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
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
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
79 print >> codefo, 'REF_STOPS_RADIAL([%s], [%s])dnl' % (
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
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
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
102 def translate_style(node, coord_id, codefo, doc, prefix):
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 node_id = node.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 style_str = node.getAttribute('style')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 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
106 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
107 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
108 prop_map = dict(prop_kvs)
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 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 opacity = float(node.getAttribute('opacity'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 if prop_map.has_key('fill'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 fill = prop_map['fill'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 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
119 r, g, b = trans_color(fill)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 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
121 node_id, r, g, b, opacity)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 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
123 paint_id = fill[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 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
125 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127 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
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 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 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
132 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 stroke_opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136 if prop_map.has_key('stroke'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 stroke = prop_map['stroke'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 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
139 r, g, b = trans_color(stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 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
141 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
142 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
143 paint_id = stroke[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 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
145 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
147 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
148 % (stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149 pass
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
150
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
151 if prop_map.has_key('stroke-width'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
152 stroke_width = float(prop_map['stroke-width'])
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
153 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % (
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
154 node_id, stroke_width)
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
155 pass
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
156
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
157 if prop_map.has_key('display'):
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
158 display = prop_map['display'].strip().lower()
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
159 if display == 'none':
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
160 print >> codefo, '%sHIDE([%s])dnl' % (prefix, node_id)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
161 pass
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
162 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
163 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165 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
166 path_id = path.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
167 d = path.getAttribute('d')
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_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id)
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
170 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
171 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 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
174 rect_id = rect.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
175 x = float(rect.getAttribute('x'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 y = float(rect.getAttribute('y'))
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
177 rx = 0.0
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
178 if rect.hasAttribute('rx'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
179 rx = float(rect.getAttribute('rx'))
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
180 pass
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
181 ry = 0.0
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
182 if rect.hasAttribute('ry'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
183 ry = float(rect.getAttribute('ry'))
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
184 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
185 width = float(rect.getAttribute('width'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
186 height = float(rect.getAttribute('height'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 print >> codefo, 'dnl'
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
188 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
189 rect_id, x, y, width, height, rx, ry, coord_id)
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
190 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
191 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
192
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
193 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
194 group_id = group.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
195 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
196 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (group_id, parent_id)
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
197 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
198 for node in group.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
199 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
200 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
201 if node.localName == 'g':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202 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
203 elif node.localName == 'path':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
204 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
205 elif node.localName == 'rect':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206 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
207 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
208 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
210
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 def svg_2_code(dom, codefo):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 for node in dom.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
213 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
214 break;
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
215 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
216 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
217 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
218
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
219 svg = node
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
220 for node in svg.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
221 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
222 translate_defs(node, codefo, dom)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
223 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
224 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
225 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
226 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
227 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
228 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
229
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
230 if __name__ == '__main__':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
231 from os import path
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
232 if len(sys.argv) == 3:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
233 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
234 codefn = sys.argv[2]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
235 elif len(sys.argv) == 2:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
236 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
237 codefn = 'out.mb'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
238 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
239 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
240 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
241
65
35c2b7ba140b Use file name of generated M4 script as name of header
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
242 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
243
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
244 dom = parse(svgfn)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
245 codefo = file(codefn, 'w+')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
246 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
247 svg_2_code(dom, codefo)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
248 print >> codefo, '])dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
249 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
250