annotate tools/svg2code.py @ 85:9b4a02bcaeb1

matrix() function in transform attribute of group and shapes
author Thinker K.F. Li <thinker@branda.to>
date Fri, 22 Aug 2008 15:47:03 +0800
parents 42698de1f653
children 7d0580f89468
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
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
4 import re
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 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
7 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
8
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
9 re_rgb = re.compile('rgb\\( *([0-9]+(\\.[0-9]+)?) *, *([0-9]+(\\.[0-9]+)?) *, *([0-9]+(\\.[0-9]+)?) *\\)')
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 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
11 stops = []
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 for node in parent.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 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
14 style = node.getAttribute('style')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 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
16 if prop.strip()]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 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
18 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
19 for prop in style_kvpairs]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 style_map = dict(style_kvpairs)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 color = style_map['stop-color'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 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
24 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
25 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
26 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
27 else:
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
28 mo = re_rgb.match(color)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
29 if mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
30 r = float(mo.group(1))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
31 g = float(mo.group(3))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
32 b = float(mo.group(5))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
33 else:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
34 raise ValueError, '\'%s\' is invalid color value.' % (color)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
35 pass
62
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 opacity = style_map['stop-opacity']
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 offset = node.getAttribute('offset')
64
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
39 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
40 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
41 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 pass
64
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
43 print >> codefo, '%sdnl' % (', '.join(stops))
62
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 def translate_linearGradient(linear, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 linear_id = linear.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 if linear.hasAttribute('x1'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 x1 = float(linear.getAttribute('x1'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 y1 = float(linear.getAttribute('y1'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 x2 = float(linear.getAttribute('x2'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 y2 = float(linear.getAttribute('y2'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 x1 = y1 = x2 = y2 = 0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 pass
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
56 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
57 linear_id, x1, y1, x2, y2)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 translate_stops(linear, codefo, linear_id)
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
59 print >> codefo, '])dnl'
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 href = linear.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 if href and href[0] == '#':
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
63 print >> codefo, 'REF_STOPS_LINEAR([%s], [%s])dnl' % (
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
64 linear_id, href[1:])
62
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 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 def translate_radialGradient(radial, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 radial_id = radial.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 cx = float(radial.getAttribute('cx'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 cy = float(radial.getAttribute('cy'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 cx = cy = 0
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 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 r = float(radial.getAttribute('r'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 r = 0.5
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 pass
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
81 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
82 radial_id, cx, cy, r)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 translate_stops(radial, codefo, radial_id)
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
84 print >>codefo, '])dnl'
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 href = radial.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 if href[0] == '#':
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
88 print >> codefo, 'REF_STOPS_RADIAL([%s], [%s])dnl' % (
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
89 radial_id, href[1:])
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 def translate_defs(defs, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 for node in defs.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 if node.localName == 'linearGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 translate_linearGradient(node, codefo, doc)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 elif node.localName == 'radialGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 translate_radialGradient(node, codefo, doc)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 def trans_color(code):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 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
108 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
109 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
110
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
111 def get_style_map(style_str):
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 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
113 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
114 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
115 prop_map = dict(prop_kvs)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
116 return prop_map
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
117
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
118 def translate_style(node, coord_id, codefo, doc, prefix):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
119 node_id = node.getAttribute('id')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
120 style_str = node.getAttribute('style')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
121 prop_map = get_style_map(style_str)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 opacity = float(node.getAttribute('opacity'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 if prop_map.has_key('fill'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 fill = prop_map['fill'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 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
132 r, g, b = trans_color(fill)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 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
134 node_id, r, g, b, opacity)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135 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
136 paint_id = fill[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 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
138 node_id, paint_id)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
139 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 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
141 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
143 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 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
145 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146 stroke_opacity = 1.0
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149 if prop_map.has_key('stroke'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150 stroke = prop_map['stroke'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
151 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
152 r, g, b = trans_color(stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153 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
154 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
155 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
156 paint_id = stroke[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
157 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
158 node_id, paint_id)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
159 elif stroke.lower() == 'none':
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
160 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
161 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
162 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
163 % (stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164 pass
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
165
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
166 if prop_map.has_key('stroke-width'):
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
167 if prop_map['stroke-width'].endswith('px'):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
168 stroke_width = float(prop_map['stroke-width'][:-2])
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
169 else:
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
170 stroke_width = float(prop_map['stroke-width'])
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
171 pass
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
172 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % (
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
173 node_id, stroke_width)
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
174 pass
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
175
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
176 if prop_map.has_key('display'):
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
177 display = prop_map['display'].strip().lower()
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
178 if display == 'none':
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
179 print >> codefo, '%sHIDE([%s])dnl' % (prefix, node_id)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
180 pass
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
181 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
182 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
183
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
184 def translate_shape_transform(shape, coord_id, codefo):
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
185 shape_id = shape.getAttribute('id')
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
186
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
187 if shape.hasAttribute('transform'):
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
188 shape_coord_id = shape_id + '_coord'
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
189 print >> codefo, 'dnl'
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
190 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
191 shape_coord_id, coord_id)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
192 transform = shape.getAttribute('transform')
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
193 translate_transform(shape_coord_id, transform, codefo, 'SHAPE_')
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
194 coord_id = shape_coord_id
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
195 pass
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
196 return coord_id
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
197
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
198 def translate_path(path, coord_id, codefo, doc):
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
199 coord_id = translate_shape_transform(path, coord_id, codefo)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
200
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
201 path_id = path.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202 d = path.getAttribute('d')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
203 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
204 print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id)
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
205
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
206 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
207 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
208
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 def translate_rect(rect, coord_id, codefo, doc):
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
210 coord_id = translate_shape_transform(rect, coord_id, codefo)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
211
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 rect_id = rect.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
213 x = float(rect.getAttribute('x'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
214 y = float(rect.getAttribute('y'))
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
215 rx = 0.0
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
216 if rect.hasAttribute('rx'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
217 rx = float(rect.getAttribute('rx'))
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
218 pass
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
219 ry = 0.0
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
220 if rect.hasAttribute('ry'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
221 ry = float(rect.getAttribute('ry'))
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
222 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
223 width = float(rect.getAttribute('width'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
224 height = float(rect.getAttribute('height'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
225 print >> codefo, 'dnl'
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
226 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
227 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
228 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
229 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
230
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
231 def translate_font_style(text, codefo):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
232 text_id = text.getAttribute('id')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
233 style_str = text.getAttribute('style')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
234 style_map = get_style_map(style_str)
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
235
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
236 font_sz = 10.0
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
237 if style_map.has_key('font-size'):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
238 if style_map['font-size'].endswith('px'):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
239 font_sz = float(style_map['font-size'][:-2])
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
240 print >> codefo, 'define([MB_FONT_SZ], %f)dnl' % (font_sz)
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
241 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
242 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
243
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
244 font_style = 'normal'
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
245 if style_map.has_key('font-style'):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
246 font_style = style_map['font-style'].lower()
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
247 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
248
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
249 font_family = 'Roman'
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
250 if style_map.has_key('font-family'):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
251 font_family = style_map['font-family'].lower()
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
252 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
253 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
254
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
255 def translate_text(text, coord_id, codefo, doc):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
256 translate_font_style(text, codefo)
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
257
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
258 txt_strs = []
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
259 for node in text.childNodes:
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
260 if node.localName == None:
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
261 txt_strs.append(node.data)
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
262 elif node.localName == 'tspan':
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
263 node.setAttribute('style', text.getAttribute('style'))
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
264 translate_text(node, coord_id, codefo, doc)
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
265 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
266 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
267 if txt_strs:
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
268 text_id = text.getAttribute('id')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
269 x = float(text.getAttribute('x'))
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
270 y = float(text.getAttribute('y'))
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
271 print >> codefo, 'dnl'
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
272 print >> codefo, \
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
273 'ADD_TEXT([%s], [%s], %f, %f, MB_FONT_SZ, [%s])dnl' % (
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
274 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'),
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
275 x, y, coord_id.encode('utf8'))
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
276 translate_style(text, coord_id, codefo, doc, 'TEXT_')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
277 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
278 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
279
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
280 reo_func = re.compile('([a-zA-Z]+)\\([^\\)]*\\)')
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
281 reo_translate = re.compile('translate\\(([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)\\)')
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
282 reo_matrix = re.compile('matrix\\(([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)\\)')
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
283 def translate_transform(coord_id, transform, codefo, prefix):
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
284 transform = transform.strip()
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
285 mo = reo_func.match(transform)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
286 if not mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
287 return
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
288 name = mo.group(1)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
289 print name
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
290 if name == 'translate':
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
291 mo = reo_translate.match(transform)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
292 if mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
293 x = float(mo.group(1))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
294 y = float(mo.group(3))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
295 print >> codefo, '%sTRANSLATE([%s], %f, %f)dnl' % (
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
296 prefix, coord_id, x, y)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
297 pass
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
298 elif name == 'matrix':
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
299 mo = reo_matrix.match(transform)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
300 if mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
301 r10, r11, r12 = \
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
302 float(mo.group(1)), float(mo.group(3)), float(mo.group(5))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
303 r20, r21, r22 = \
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
304 float(mo.group(7)), float(mo.group(9)), float(mo.group(11))
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
305 print >> codefo, \
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
306 '%sMATRIX([%s], %f, %f, %f, %f, %f, %f)dnl' % (
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
307 prefix, coord_id, r10, r11, r12, r20, r21, r22)
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
308 pass
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
309 pass
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
310 pass
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
311
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
312 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
313 group_id = group.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
314 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
315 print >> codefo, 'ADD_COORD([%s], [%s])dnl' % (group_id, parent_id)
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
316
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
317 if group.hasAttribute('transform'):
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
318 transform = group.getAttribute('transform')
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
319 translate_transform(group_id, transform, codefo, 'COORD_')
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
320 pass
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
321
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
322 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
323 for node in group.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
324 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
325 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
326 if node.localName == 'g':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
327 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
328 elif node.localName == 'path':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
329 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
330 elif node.localName == 'rect':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
331 translate_rect(node, group_id, codefo, doc)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
332 elif node.localName == 'text':
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
333 translate_text(node, group_id, codefo, doc)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
334 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
335 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
336 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
337
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
338 def svg_2_code(dom, codefo):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
339 for node in dom.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
340 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
341 break;
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
342 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
343 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
344 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
345
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
346 svg = node
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
347 for node in svg.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
348 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
349 translate_defs(node, codefo, dom)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
350 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
351 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
352 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
353 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
354 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
355 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
356
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
357 if __name__ == '__main__':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
358 from os import path
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
359 if len(sys.argv) == 3:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
360 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
361 codefn = sys.argv[2]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
362 elif len(sys.argv) == 2:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
363 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
364 codefn = 'out.mb'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
365 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
366 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
367 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
368
65
35c2b7ba140b Use file name of generated M4 script as name of header
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
369 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
370
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
371 dom = parse(svgfn)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
372 codefo = file(codefn, 'w+')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
373 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
374 svg_2_code(dom, codefo)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
375 print >> codefo, '])dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
376 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
377