annotate tools/svg2code.py @ 409:1e9b615a47e8

merge
author Thinker K.F. Li <thinker@branda.to>
date Tue, 07 Jul 2009 22:54:21 +0800
parents 07ca7681f43b
children c3faebaec0c4
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
86
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
27 elif color.lower() == 'white':
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
28 r, g, b = 1, 1, 1
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
29 elif color.lower() == 'black':
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
30 r, g, b = 0, 0, 0
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 else:
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
32 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
33 if mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
34 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
35 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
36 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
37 else:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
38 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
39 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40
86
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
41 try:
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
42 opacity = style_map['stop-opacity']
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
43 except:
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
44 opacity = 1
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
45 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 offset = node.getAttribute('offset')
64
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
47 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
48 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
49 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 pass
64
c668c5c3ceae M4 macro for C binding.
Thinker K.F. Li <thinker@branda.to>
parents: 63
diff changeset
51 print >> codefo, '%sdnl' % (', '.join(stops))
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 def translate_linearGradient(linear, codefo, doc):
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
55 linear_id = _get_id(linear)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 if linear.hasAttribute('x1'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 x1 = float(linear.getAttribute('x1'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 y1 = float(linear.getAttribute('y1'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 x2 = float(linear.getAttribute('x2'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 y2 = float(linear.getAttribute('y2'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 x1 = y1 = x2 = y2 = 0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 pass
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
64 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
65 linear_id, x1, y1, x2, y2)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 translate_stops(linear, codefo, linear_id)
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
67 print >> codefo, '])dnl'
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 href = linear.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 if href and href[0] == '#':
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
71 print >> codefo, 'REF_STOPS_LINEAR([%s], [%s])dnl' % (
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
72 linear_id, href[1:])
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 pass
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 def translate_radialGradient(radial, codefo, doc):
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
77 radial_id = _get_id(radial)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 cx = float(radial.getAttribute('cx'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 cy = float(radial.getAttribute('cy'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 cx = cy = 0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 try:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 r = float(radial.getAttribute('r'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 except:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 r = 0.5
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 pass
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
89 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
90 radial_id, cx, cy, r)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 translate_stops(radial, codefo, radial_id)
63
f4b792afa74e m4 translator
Thinker K.F. Li <thinker@branda.to>
parents: 62
diff changeset
92 print >>codefo, '])dnl'
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 href = radial.getAttributeNS(xlinkns, 'href').strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 if href[0] == '#':
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
96 print >> codefo, 'REF_STOPS_RADIAL([%s], [%s])dnl' % (
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 65
diff changeset
97 radial_id, href[1:])
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 pass
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
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 def translate_defs(defs, codefo, doc):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 for node in defs.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 if node.localName == 'linearGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 translate_linearGradient(node, codefo, doc)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 elif node.localName == 'radialGradient':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 translate_radialGradient(node, codefo, doc)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 pass
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 def trans_color(code):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 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
116 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
117 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
118
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
119 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
120 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
121 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
122 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
123 prop_map = dict(prop_kvs)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
124 return prop_map
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
125
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
126 def translate_style(node, coord_id, codefo, doc, prefix):
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
127 node_id = node.getAttribute('id')
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
128 try:
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
129 prop_map = node.style_map
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
130 except:
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
131 style_str = node.getAttribute('style')
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
132 prop_map = get_style_map(style_str)
313
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
133 pass
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
134
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
135 if node.hasAttribute('fill-opacity'):
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
136 opacity = float(node.getAttribute('fill-opacity'))
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
137 elif node.hasAttribute('opacity'):
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 opacity = float(node.getAttribute('opacity'))
313
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
139 else:
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
140 opacity = 1
62
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
300
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
143 try:
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
144 opacity = float(prop_map['opacity'])
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
145 except:
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
146 pass
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
147
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
148 print "# opacity of %s is %g" % (node_id, opacity)
1b69da2494e9 Use the opacity of the style to overwrite the default opacity property of the node itself.
wycc
parents: 296
diff changeset
149
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150 if prop_map.has_key('fill'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
151 fill = prop_map['fill'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
152 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
153 r, g, b = trans_color(fill)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154 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
155 node_id, r, g, b, opacity)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
156 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
157 paint_id = fill[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 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
159 node_id, paint_id)
86
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
160 elif fill.lower() == 'none':
7d0580f89468 Fix bug of dealing matrix().
Thinker K.F. Li <thinker@branda.to>
parents: 85
diff changeset
161 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
162 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
163 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
164 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165
313
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
166 if node.hasAttribute('stroke-opacity'):
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
167 stroke_opacity = float(node.getAttribute('stroke-opacity'))
313
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
168 elif node.hasAttribute('opacity'):
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
169 stroke_opacity = float(node.getAttribute('opacity'))
5737548e922f Fix bug that svg2code.py handle fill & stroke opacity in a wrong way.
Thinker K.F. Li <thinker@branda.to>
parents: 310
diff changeset
170 else:
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
171 stroke_opacity = 1.0
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
174 if prop_map.has_key('stroke'):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
175 stroke = prop_map['stroke'].strip()
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 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
177 r, g, b = trans_color(stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
178 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
179 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
180 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
181 paint_id = stroke[5:-1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
182 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
183 node_id, paint_id)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
184 elif stroke.lower() == 'none':
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
185 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
186 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 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
188 % (stroke)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
189 pass
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
190
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
191 if prop_map.has_key('stroke-width'):
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
192 if prop_map['stroke-width'].endswith('px'):
372
2212f515584c Adjust stroke width to more close to rendering result of Inkscape
Thinker K.F. Li <thinker@branda.to>
parents: 333
diff changeset
193 stroke_width = float(prop_map['stroke-width'][:-2]) / 2
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
194 else:
372
2212f515584c Adjust stroke width to more close to rendering result of Inkscape
Thinker K.F. Li <thinker@branda.to>
parents: 333
diff changeset
195 stroke_width = float(prop_map['stroke-width']) / 2
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
196 pass
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
197 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % (
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
198 node_id, stroke_width)
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
199 pass
373
07ca7681f43b Avoid set stroke width on shape with stroke=none
Thinker K.F. Li <thinker@branda.to>
parents: 372
diff changeset
200 elif prop_map.has_key('stroke') and prop_map['stroke'] != 'none':
372
2212f515584c Adjust stroke width to more close to rendering result of Inkscape
Thinker K.F. Li <thinker@branda.to>
parents: 333
diff changeset
201 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % (
2212f515584c Adjust stroke width to more close to rendering result of Inkscape
Thinker K.F. Li <thinker@branda.to>
parents: 333
diff changeset
202 node_id, 0.5)
2212f515584c Adjust stroke width to more close to rendering result of Inkscape
Thinker K.F. Li <thinker@branda.to>
parents: 333
diff changeset
203 pass
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
204
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
205 if prop_map.has_key('display'):
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
206 display = prop_map['display'].strip().lower()
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
207 if display == 'none':
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
208 print >> codefo, '%sHIDE([%s])dnl' % (prefix, node_id)
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
209 pass
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
210 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
213 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
214 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
215
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
216 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
217 shape_coord_id = shape_id + '_coord'
101
d6f9af55b0d0 More documentation.
Thinker K.F. Li <thinker@branda.to>
parents: 86
diff changeset
218 ## \page shape_coord Coordinate Transformation for Shapes.
d6f9af55b0d0 More documentation.
Thinker K.F. Li <thinker@branda.to>
parents: 86
diff changeset
219 #
d6f9af55b0d0 More documentation.
Thinker K.F. Li <thinker@branda.to>
parents: 86
diff changeset
220 # svg2code.py add a coord_t for a shape if transform attribute
d6f9af55b0d0 More documentation.
Thinker K.F. Li <thinker@branda.to>
parents: 86
diff changeset
221 # of it's tag is setted. The name of coord_t object is
d6f9af55b0d0 More documentation.
Thinker K.F. Li <thinker@branda.to>
parents: 86
diff changeset
222 # <shape_id> + "_coord".
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
223 print >> codefo, 'dnl'
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
224 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
225 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
226 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
227 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
228 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
229 pass
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
230 return coord_id
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
231
269
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
232 ## \brief Calculate geometry of ellipse where the arc is on.
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
233 #
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
234 # This function calculate the ellipse with information from SVG path data.
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
235 #
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
236 # \see calc_center_and_x_aix()
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
237 def _calc_ellipse_of_arc(x0, y0, rx, ry, x_rotate, large, sweep, x, y):
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
238 import math
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
239
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
240 _sin = math.sin(x_rotate)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
241 _cos = math.cos(x_rotate)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
242
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
243 nrx = x * _cos + y * _sin
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
244 nry = x * -_sin + y * _cos
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
245 nrx0 = x0 * _cos + y0 * _sin
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
246 nry0 = x0 * -_sin + y0 * _cos
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
247
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
248 udx = (nrx - nrx0) / 2 / rx # ux - umx
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
249 udy = (nry - nry0) / 2 / ry # uy - umy
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
250 umx = (nrx + nrx0) / 2 / rx
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
251 umy = (nry + nry0) / 2 / ry
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
252
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
253 udx2 = udx * udx
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
254 udy2 = udy * udy
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
255 udl2 = udx2 + udy2
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
256
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
257 if udy != 0:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
258 # center is at left-side of arc
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
259 udcx = -math.sqrt((1 - udl2) * udl2) / (udy + udx2 / udy)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
260 udcy = -udcx * udx / udy
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
261 else:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
262 # center is at down-side of arc
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
263 udcx = 0
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
264 udcy = math.sqrt((1 - udl2) * udl2) / udx
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
265 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
266
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
267 reflect = 0
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
268 if large:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
269 reflect ^= 1
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
270 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
271 if sweep != 1:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
272 reflect ^= 1
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
273 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
274 if reflect:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
275 udcx = -udcx
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
276 udcy = -udcy
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
277 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
278
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
279 nrcx = rx * (udcx + umx)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
280 nrcy = ry * (udcy + umy)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
281
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
282 cx = nrcx * _cos - nrcy * _sin
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
283 cy = nrcx * _sin + nrcy * _cos
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
284
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
285 xx = rx * _cos + cx
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
286 xy = rx * _sin + cy
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
287 return cx, cy, xx, xy
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
288
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
289 # M x y : Move to (x,y)
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
290 # Z : close path
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
291 # L x y : lineto (x,y)
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
292 # H x : horizontal line to x
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
293 # V y : Vertical line to y
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
294 # C x1 y1 x2 y2 x y : Draw a segment of bezier curve
241
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
295 # S x2 y2 x t : Draw a segment of bezier curve from the last
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
296 # control point
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
297 # Q x1 y1 x y : Draw a segment of quadratic curve
241
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
298 # T x y : Draw a segment of quadratic curve from the last
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
299 # control pint
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
300 # A x y r f s x y : Draw an arc
241
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
301 # translate the path data into two arrays. The first is an integer whose
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
302 # upper 8 bits are the command type The second array hold all arguments.
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
303
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
304 command_length={'M': 2, 'm':2,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
305 'Z': 0, 'z':0,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
306 'L': 2, 'l':2,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
307 'H': 1, 'h':1,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
308 'V': 1, 'v':1,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
309 'C': 6, 'c':6,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
310 'S': 4, 's':4,
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
311 'Q': 4, 'q':4,
269
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
312 'T': 2, 't':2,
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
313 'A': 7, 'a':7}
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
314
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
315 def translate_path_data(data,codefo):
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
316 temp = data.split()
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
317 fields=[]
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
318 for f in temp:
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
319 for s in f.split(','):
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
320 if s != '':
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
321 fields.append(s)
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
322 cmd = ''
280
c8b6ca46950b Add merged result
wycc
parents: 279 269
diff changeset
323 cmd_args=0
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
324 commands=''
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
325 args=[]
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
326 fix_args=[]
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
327 for f in fields:
269
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
328 if f in command_length:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
329 if cmd_args != 0 and (narg % cmd_args) != 0:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
330 raise ValueError, 'invalid path data %s' % (repr(fields))
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
331 cmd = f
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
332 cmd_args = command_length[f]
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
333 narg = 0
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
334 continue
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
335
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
336 if (narg % cmd_args) == 0:
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
337 commands = commands + cmd
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
338 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
339 arg = float(f)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
340 args.append(arg)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
341 narg = narg + 1
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
342
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
343 if (narg % cmd_args) == 0 and (cmd in 'Aa'):
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
344 x0, y0, rx, ry, x_rotate, large, sweep, x, y = \
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
345 tuple(args[-9:])
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
346 x_rotate = int(x_rotate)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
347 large = int(large)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
348 sweep = int(sweep)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
349 cx, cy, xx, xy = _calc_ellipse_of_arc(x0, y0, rx, ry,
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
350 x_rotate, large,
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
351 sweep, x, y)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
352 args[-7:] = [cx, cy, xx, xy, x, y]
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
353 fix_args.append(sweep)
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
354 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
355 pass
c96f38ad4bb6 Fix mis-behavior of translate_path_data() on arc.
Thinker K.F. Li <thinker@branda.to>
parents: 260
diff changeset
356 return commands, args, fix_args
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
357
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
358 _id_sn = 0
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
359
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
360 def _get_id(obj):
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
361 global _id_sn
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
362
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
363 if obj.hasAttribute('id'):
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
364 oid = obj.getAttribute('id')
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
365 else:
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
366 oid = '_MB_RAND_%s_' % (_id_sn)
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
367 obj.setAttribute('id', oid)
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
368 _id_sn = _id_sn + 1
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
369 pass
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
370 return oid
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
371
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
372 ##
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
373 # Decorator that check if objects have attribute 'mbname'.
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
374 #
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
375 def check_mbname(func):
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
376 def deco(obj, coord_id, codefo, doc):
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
377 if obj.hasAttribute('mbname'):
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
378 ## \note mbname declare that this node should be in the
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
379 # symbol table.
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
380 mbname = obj.getAttribute('mbname')
296
2e97e8082d83 * Fix the symbol definition code which does not assume the id is the same as the mbname.
wycc
parents: 287
diff changeset
381 id = obj.getAttribute('id')
2e97e8082d83 * Fix the symbol definition code which does not assume the id is the same as the mbname.
wycc
parents: 287
diff changeset
382 print >> codefo, 'ADD_SYMBOL([%s],[%s])dnl' % (id,mbname)
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
383 pass
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
384 func(obj, coord_id, codefo, doc)
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
385 pass
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
386 return deco
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
387
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
388 @check_mbname
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
389 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
390 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
391
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
392 path_id = path.getAttribute('id')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
393 d = path.getAttribute('d')
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
394 (commands,args,fix_args) = translate_path_data(d,codefo)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
395 print >> codefo, 'dnl'
197
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
396 #print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id)
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
397 sarg=''
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
398 for c in args:
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
399 sarg = sarg + "%f," % c
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
400 s_fix_arg=''
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
401 for c in fix_args:
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
402 s_fix_arg = s_fix_arg + ("%d," % c)
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
403
bcad1ccdf45c Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
wycc@wycc-desktop
parents: 101
diff changeset
404 print >> codefo, 'ADD_PATH([%s], [%s],[%s],[%s],[%d],[%s],[%d])dnl' % (path_id, coord_id,commands,sarg,len(args),s_fix_arg,len(fix_args))
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
405
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
406 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
407 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
408
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
409 @check_mbname
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
410 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
411 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
412
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
413 rect_id = _get_id(rect)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
414 x = float(rect.getAttribute('x'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
415 y = float(rect.getAttribute('y'))
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
416 rx = 0.0
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
417 if rect.hasAttribute('rx'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
418 rx = float(rect.getAttribute('rx'))
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
419 pass
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
420 ry = 0.0
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
421 if rect.hasAttribute('ry'):
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
422 ry = float(rect.getAttribute('ry'))
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
423 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
424 width = float(rect.getAttribute('width'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
425 height = float(rect.getAttribute('height'))
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
426 print >> codefo, 'dnl'
80
e548221c04eb svg2code.py support stroke
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
427 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
428 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
429 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
430 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
431
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
432 def translate_font_style(text, codefo):
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
433 text_id = _get_id(text)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
434 style_str = text.getAttribute('style')
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
435 style_map = get_style_map(style_str)
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
436
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
437 return style_map
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
438
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
439 def merge_style(tspan,text):
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
440 newmap = tspan.style_map
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
441 map = text.style_map
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
442 for k,v in text.style_map.items():
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
443 if not newmap.has_key(k):
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
444 newmap[k] = v
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
445 def translate_tspan(tspan, text,coord_id, codefo, doc,txt_strs,attrs):
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
446 try:
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
447 map = tspan.style_map
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
448 except:
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
449 map = translate_font_style(tspan, codefo)
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
450 tspan.style_map = map
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
451 if tspan.hasAttribute('x'):
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
452 # Render the tspan as an independent text if the x attribute is defined. All elements inside
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
453 # the tspan will be ignore by the outter text or tspan elements.
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
454 # FIXME: We need to apply the style map recursively.
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
455 merge_style(tspan,text)
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
456 translate_text(tspan,coord_id,codefo,doc)
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
457 return ''
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
458 attr = [len(txt_strs.encode('utf8'))-1,0, tspan]
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
459 attrs.append(attr)
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
460 for node in tspan.childNodes:
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
461 if node.localName == None:
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
462 txt_strs = txt_strs + node.data
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
463 elif node.localName == 'tspan':
287
5c066380a84e * Fix tspan inside tspan parsing issue
wycc
parents: 285
diff changeset
464 txt_strs = translate_tspan(node,tspan,coord_id, codefo, doc,txt_strs,attrs)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
465 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
466 pass
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
467 attr[1] = len(txt_strs.encode('utf8'))
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
468 return txt_strs
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
469
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
470
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
471
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
472 def generate_font_attributes(attrs,coord_id, codefo,doc):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
473 for a in attrs:
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
474 start = a[0]
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
475 end = a[1]
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
476 node = a[2]
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
477 #print "generate attributes from %d to %d" %(start,end)
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
478 style_map = node.style_map
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
479 #print [style_map]
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
480 if style_map.has_key('font-size'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
481 # FIXME: Implement all units here
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
482 if style_map['font-size'].endswith('px'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
483 font_sz = float(style_map['font-size'][:-2])
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
484 print >> codefo, 'PANGO_SIZE(%d,%d,%d)dnl' % (font_sz*1024,start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
485 else:
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
486 font_sz = float(style_map['font-size'])
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
487 print >> codefo, 'PANGO_SIZE(%d,%d,%d)dnl' % (font_sz*1024,start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
488 pass
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
489
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
490 if style_map.has_key('font-style'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
491 font_style = style_map['font-style'].lower()
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
492 if font_style == 'normal':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
493 print >> codefo, 'PANGO_STYLE(PANGO_STYLE_NORMAL,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
494 elif font_style == 'italic':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
495 print >> codefo, 'PANGO_STYLE(PANGO_STYLE_ITALIC,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
496 elif font_style == 'oblique':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
497 print >> codefo, 'PANGO_STYLE(PANGO_STYLE_OBLIQUE,%d,%d)dnl' % (start,end)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
498 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
499
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
500 if style_map.has_key('font-family'):
306
c981e561ac37 * Keep the font name as the original case.
wycc
parents: 300
diff changeset
501 font_family = style_map['font-family']
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
502 print >> codefo, 'PANGO_FAMILY(%s,%d,%d)dnl' % (font_family,start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
503 pass
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
504 if style_map.has_key('text-anchor'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
505 text_anchor = style_map['text-anchor'].lower()
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
506 # FIXME: We need to implement a mb_text_set_aligment to implement SVG-styled alignment.
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
507 print "The text-anchor is not implemented yet"
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
508 pass
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
509 if style_map.has_key('font-variant'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
510 font_variant = style_map['font-variant'].lower()
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
511 print "The font-variant is not implemented yet"
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
512 pass
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
513 if style_map.has_key('font-weight'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
514 font_weight = style_map['font-weight'].lower()
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
515 if font_weight == 'normal':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
516 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_NORMAL,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
517 elif font_weight == 'bold':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
518 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_BOLD,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
519 elif font_weight == 'bolder':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
520 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_HEAVY,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
521 elif font_weight == 'lighter':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
522 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRALIGHT,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
523 elif font_weight == '100':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
524 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRALIGHT,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
525 elif font_weight == '200':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
526 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRALIGHT,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
527 elif font_weight == '300':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
528 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_LIGHT,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
529 elif font_weight == '400':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
530 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_NORMAL,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
531 elif font_weight == '500':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
532 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_NORMAL,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
533 elif font_weight == '600':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
534 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_SEMIBOLD,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
535 elif font_weight == '700':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
536 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_BOLD,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
537 elif font_weight == '800':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
538 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRABOLD,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
539 elif font_weight == '900':
333
bdf36a26e420 Make generated code clean.
Thinker K.F. Li <thinker@branda.to>
parents: 313
diff changeset
540 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_HEAVY,%d,%d)dnl' % (start,end)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
541 else:
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
542 print "The font-weight %s is not supported" % font_weight
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
543 pass
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
544 if style_map.has_key('direction'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
545 direction = style_map['direction'].lower()
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
546 print "The direction is not implemented yet"
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
547 pass
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
548 if style_map.has_key('unicode-bidi'):
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
549 bidi = style_map['unicode-bidi'].lower()
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
550 print "The bidi is not implemented yet"
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
551 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
552 pass
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
553
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
554 def translate_text(text, coord_id, codefo, doc):
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
555 try:
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
556 map = text.style_map
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
557 except:
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
558 map = translate_font_style(text, codefo)
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
559 text.style_map = map
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
560 attrs = []
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
561 attr = [0,0, text]
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
562 attrs.append(attr)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
563
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
564 txt_strs = ''
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
565 for node in text.childNodes:
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
566 if node.localName == None:
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
567 txt_strs = txt_strs + node.data
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
568 elif node.localName == 'tspan':
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
569 txt_strs = translate_tspan(node,text,coord_id, codefo, doc,txt_strs,attrs)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
570 pass
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
571 pass
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
572 attr[1] = len(txt_strs.encode('utf8'))
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
573 if txt_strs:
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
574 text_id = _get_id(text)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
575 x = float(text.getAttribute('x'))
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
576 y = float(text.getAttribute('y'))
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
577 print >> codefo, 'dnl'
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
578 print >> codefo, \
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
579 'PANGO_BEGIN_TEXT([%s], [%s], %f, %f, 16, [%s])dnl' % (
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
580 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'),
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
581 x, y, coord_id.encode('utf8'))
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
582 generate_font_attributes(attrs, coord_id, codefo, doc)
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
583 print >> codefo, \
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
584 'PANGO_END_TEXT([%s], [%s], %f, %f, 16, [%s])dnl' % (
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
585 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'),
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
586 x, y, coord_id.encode('utf8'))
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
587 translate_style(text, coord_id, codefo, doc, 'TEXT_')
285
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
588 if text.hasAttribute('mbname'):
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
589 ## \note mbname declare that this node should be in the
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
590 # symbol table.
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
591 mbname = text.getAttribute('mbname')
296
2e97e8082d83 * Fix the symbol definition code which does not assume the id is the same as the mbname.
wycc
parents: 287
diff changeset
592 id = text.getAttribute('id')
2e97e8082d83 * Fix the symbol definition code which does not assume the id is the same as the mbname.
wycc
parents: 287
diff changeset
593 print >> codefo, 'ADD_SYMBOL([%s],[%s])dnl' % (id,mbname)
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents: 241
diff changeset
594 pass
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents: 241
diff changeset
595 pass
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents: 241
diff changeset
596
310
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
597 @check_mbname
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
598 def translate_image(image, coord_id, codefo, doc):
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
599 coord_id = translate_shape_transform(image, coord_id, codefo)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
600
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
601 image_id = _get_id(image)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
602 if not image.hasAttributeNS(xlinkns, 'href'):
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
603 raise ValueError, 'image %s must has a href attribute.' % (image_id)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
604 href = image.getAttributeNS(xlinkns, 'href')
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
605 if image.hasAttribute('x'):
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
606 x_str = image.getAttribute('x')
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
607 x = float(x_str)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
608 else:
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
609 x = 0
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
610 pass
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
611 if image.hasAttribute('y'):
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
612 y_str = image.getAttribute('y')
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
613 y = float(y_str)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
614 else:
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
615 y = 0
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
616 pass
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
617 if image.hasAttribute('width'):
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
618 width_str = image.getAttribute('width')
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
619 width = float(width_str)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
620 else:
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
621 width = -1
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
622 pass
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
623 if image.hasAttribute('height'):
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
624 height_str = image.getAttribute('height')
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
625 height = float(height_str)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
626 else:
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
627 height = -1
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
628 pass
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
629 print >> codefo, 'dnl'
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
630 print >> codefo, \
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
631 'ADD_IMAGE([%s], [%s], %f, %f, %f, %f, [%s])dnl' % (
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
632 image_id, href, x, y, width, height, coord_id)
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
633 pass
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
634
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
635
85
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
636 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
637 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
638 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
639 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
640 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
641 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
642 if not mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
643 return
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
644 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
645 if name == 'translate':
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
646 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
647 if mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
648 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
649 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
650 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
651 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
652 pass
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
653 elif name == 'matrix':
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
654 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
655 if mo:
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
656 r10, r11, r12 = \
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
657 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
658 r20, r21, r22 = \
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
659 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
660 print >> codefo, \
9b4a02bcaeb1 matrix() function in transform attribute of group and shapes
Thinker K.F. Li <thinker@branda.to>
parents: 84
diff changeset
661 '%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
662 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
663 pass
84
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
664 pass
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
665 pass
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
666
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
667 @check_mbname
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
668 def translate_group(group, parent_id, codefo, doc):
210
3fadd2f2742e M4 macros to generate code for dynamic loading.
Thinker K.F. Li <thinker@branda.to>
parents: 197
diff changeset
669 group_id = _get_id(group)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
670 print >> codefo, 'dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
671 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
672
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
673 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
674 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
675 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
676 pass
42698de1f653 Support translate() function for transform attribute of 'g' tag.
Thinker K.F. Li <thinker@branda.to>
parents: 83
diff changeset
677
81
13fdf205047b Hide shapes and groups
Thinker K.F. Li <thinker@branda.to>
parents: 80
diff changeset
678 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
679 for node in group.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
680 if node.namespaceURI != svgns:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
681 continue
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
682 if node.localName == 'g':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
683 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
684 elif node.localName == 'path':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
685 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
686 elif node.localName == 'rect':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
687 translate_rect(node, group_id, codefo, doc)
83
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
688 elif node.localName == 'text':
ea758bb3bbe2 example
Thinker K.F. Li <thinker@branda.to>
parents: 81
diff changeset
689 translate_text(node, group_id, codefo, doc)
310
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
690 elif node.localName == 'image':
25a68d15e92f Merge the image loader back
wycc
parents: 306
diff changeset
691 translate_image(node, group_id, codefo, doc)
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
692 elif node.localName == 'textarea':
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 241
diff changeset
693 translate_textarea(node, group_id, codefo, doc)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
694 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
695 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
696 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
697
241
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
698 ## \brief Translate "scenes" tag in "metadata" tag.
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
699 #
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
700 def translate_scenes(scenes_node, codefo, doc):
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
701 scenes = []
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
702 for scene in scenes_node.childNodes:
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
703 if scene.localName != 'scene' or \
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
704 not scene.hasAttribute('ref') or \
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
705 not scene.hasAttribute('start'):
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
706 continue
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
707
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
708 start_str = scene.getAttribute('start')
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
709 start = end = int(start_str)
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
710 if scene.hasAttribute('end'):
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
711 end_str = scene.getAttribute('end')
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
712 end = int(end_str)
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
713 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
714 ref = scene.getAttribute('ref')
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
715
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
716 while len(scenes) <= end:
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
717 scenes.append([])
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
718 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
719
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
720 for i in range(start, end + 1):
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
721 scenes[i].append(ref)
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
722 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
723 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
724
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
725 for scene_idx, groups_in_scene in enumerate(scenes):
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
726 if groups_in_scene:
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
727 groups_str = '[' + '],['.join(groups_in_scene) + ']'
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
728 else:
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
729 groups_str = ''
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
730 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
731 print >> codefo, \
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
732 'SCENE(%d, [%s])dnl' % (scene_idx, groups_str)
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
733 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
734 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
735
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
736 def svg_2_code(dom, codefo):
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
737 for node in dom.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
738 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
739 break;
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
740 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
741 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
742 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
743
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
744 svg = node
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
745 for node in svg.childNodes:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
746 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
747 translate_defs(node, codefo, dom)
241
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
748 elif node.localName == 'metadata' and node.namespaceURI == svgns:
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
749 for meta_node in node.childNodes:
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
750 if meta_node.localName == 'scenes':
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
751 translate_scenes(meta_node, codefo, dom)
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
752 pass
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
753 pass
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
754 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
755 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
756 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
757 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
758 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
759 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
760
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
761 if __name__ == '__main__':
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
762 from os import path
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
763 if len(sys.argv) == 3:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
764 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
765 codefn = sys.argv[2]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
766 elif len(sys.argv) == 2:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
767 svgfn = sys.argv[1]
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
768 codefn = 'out.mb'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
769 else:
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
770 print >> sys.stderr, '%s <SVG file> [<output>]' % (sys.argv[0])
241
104d83378582 Add scene support in svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 210
diff changeset
771 sys.exit(1)
62
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
772 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
773
65
35c2b7ba140b Use file name of generated M4 script as name of header
Thinker K.F. Li <thinker@branda.to>
parents: 64
diff changeset
774 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
775
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
776 dom = parse(svgfn)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
777 codefo = file(codefn, 'w+')
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
778 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
779 svg_2_code(dom, codefo)
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
780 print >> codefo, '])dnl'
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
781 pass
7d976d925431 Generate C header files for SVG files.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
782