comparison tools/svg2code.py @ 83:ea758bb3bbe2

example
author Thinker K.F. Li <thinker@branda.to>
date Fri, 22 Aug 2008 00:12:04 +0800
parents 13fdf205047b
children 42698de1f653
comparison
equal deleted inserted replaced
82:4bb6451ef036 83:ea758bb3bbe2
97 def trans_color(code): 97 def trans_color(code):
98 return int(code[1:3], 16) / 255.0, \ 98 return int(code[1:3], 16) / 255.0, \
99 int(code[3:5], 16) / 255.0, \ 99 int(code[3:5], 16) / 255.0, \
100 int(code[5:7], 16) / 255.0 100 int(code[5:7], 16) / 255.0
101 101
102 def translate_style(node, coord_id, codefo, doc, prefix): 102 def get_style_map(style_str):
103 node_id = node.getAttribute('id')
104 style_str = node.getAttribute('style')
105 prop_strs = [s.strip() for s in style_str.split(';')] 103 prop_strs = [s.strip() for s in style_str.split(';')]
106 prop_kvs = [s.split(':') for s in prop_strs if s] 104 prop_kvs = [s.split(':') for s in prop_strs if s]
107 prop_kvs = [(k.strip(), v.strip()) for k, v in prop_kvs] 105 prop_kvs = [(k.strip(), v.strip()) for k, v in prop_kvs]
108 prop_map = dict(prop_kvs) 106 prop_map = dict(prop_kvs)
107 return prop_map
108
109 def translate_style(node, coord_id, codefo, doc, prefix):
110 node_id = node.getAttribute('id')
111 style_str = node.getAttribute('style')
112 prop_map = get_style_map(style_str)
109 113
110 try: 114 try:
111 opacity = float(node.getAttribute('opacity')) 115 opacity = float(node.getAttribute('opacity'))
112 except: 116 except:
113 opacity = 1.0 117 opacity = 1.0
141 node_id, r, g, b, stroke_opacity) 145 node_id, r, g, b, stroke_opacity)
142 elif stroke.startswith('url(') and stroke.endswith(')'): 146 elif stroke.startswith('url(') and stroke.endswith(')'):
143 paint_id = stroke[5:-1] 147 paint_id = stroke[5:-1]
144 print >> codefo, 'STROKE_SHAPE_WITH_PAINT([%s], [%s])dnl' % ( 148 print >> codefo, 'STROKE_SHAPE_WITH_PAINT([%s], [%s])dnl' % (
145 node_id, paint_id) 149 node_id, paint_id)
150 elif stroke.lower() == 'none':
151 pass
146 else: 152 else:
147 raise ValueError, '\'%s\' is an invalid value for stroke.' \ 153 raise ValueError, '\'%s\' is an invalid value for stroke.' \
148 % (stroke) 154 % (stroke)
149 pass 155 pass
150 156
151 if prop_map.has_key('stroke-width'): 157 if prop_map.has_key('stroke-width'):
152 stroke_width = float(prop_map['stroke-width']) 158 if prop_map['stroke-width'].endswith('px'):
159 stroke_width = float(prop_map['stroke-width'][:-2])
160 else:
161 stroke_width = float(prop_map['stroke-width'])
162 pass
153 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % ( 163 print >> codefo, 'STROKE_WIDTH([%s], %f)dnl' % (
154 node_id, stroke_width) 164 node_id, stroke_width)
155 pass 165 pass
156 166
157 if prop_map.has_key('display'): 167 if prop_map.has_key('display'):
186 height = float(rect.getAttribute('height')) 196 height = float(rect.getAttribute('height'))
187 print >> codefo, 'dnl' 197 print >> codefo, 'dnl'
188 print >> codefo, 'ADD_RECT([%s], %f, %f, %f, %f, %f, %f, [%s])dnl' % ( 198 print >> codefo, 'ADD_RECT([%s], %f, %f, %f, %f, %f, %f, [%s])dnl' % (
189 rect_id, x, y, width, height, rx, ry, coord_id) 199 rect_id, x, y, width, height, rx, ry, coord_id)
190 translate_style(rect, coord_id, codefo, doc, 'RECT_') 200 translate_style(rect, coord_id, codefo, doc, 'RECT_')
201 pass
202
203 def translate_font_style(text, codefo):
204 text_id = text.getAttribute('id')
205 style_str = text.getAttribute('style')
206 style_map = get_style_map(style_str)
207
208 font_sz = 10.0
209 if style_map.has_key('font-size'):
210 if style_map['font-size'].endswith('px'):
211 font_sz = float(style_map['font-size'][:-2])
212 print >> codefo, 'define([MB_FONT_SZ], %f)dnl' % (font_sz)
213 pass
214 pass
215
216 font_style = 'normal'
217 if style_map.has_key('font-style'):
218 font_style = style_map['font-style'].lower()
219 pass
220
221 font_family = 'Roman'
222 if style_map.has_key('font-family'):
223 font_family = style_map['font-family'].lower()
224 pass
225 pass
226
227 def translate_text(text, coord_id, codefo, doc):
228 translate_font_style(text, codefo)
229
230 txt_strs = []
231 for node in text.childNodes:
232 if node.localName == None:
233 txt_strs.append(node.data)
234 elif node.localName == 'tspan':
235 node.setAttribute('style', text.getAttribute('style'))
236 translate_text(node, coord_id, codefo, doc)
237 pass
238 pass
239 if txt_strs:
240 text_id = text.getAttribute('id')
241 x = float(text.getAttribute('x'))
242 y = float(text.getAttribute('y'))
243 print >> codefo, 'dnl'
244 print >> codefo, \
245 'ADD_TEXT([%s], [%s], %f, %f, MB_FONT_SZ, [%s])dnl' % (
246 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'),
247 x, y, coord_id.encode('utf8'))
248 translate_style(text, coord_id, codefo, doc, 'TEXT_')
249 pass
191 pass 250 pass
192 251
193 def translate_group(group, parent_id, codefo, doc): 252 def translate_group(group, parent_id, codefo, doc):
194 group_id = group.getAttribute('id') 253 group_id = group.getAttribute('id')
195 print >> codefo, 'dnl' 254 print >> codefo, 'dnl'
202 translate_group(node, group_id, codefo, doc) 261 translate_group(node, group_id, codefo, doc)
203 elif node.localName == 'path': 262 elif node.localName == 'path':
204 translate_path(node, group_id, codefo, doc) 263 translate_path(node, group_id, codefo, doc)
205 elif node.localName == 'rect': 264 elif node.localName == 'rect':
206 translate_rect(node, group_id, codefo, doc) 265 translate_rect(node, group_id, codefo, doc)
266 elif node.localName == 'text':
267 translate_text(node, group_id, codefo, doc)
207 pass 268 pass
208 pass 269 pass
209 pass 270 pass
210 271
211 def svg_2_code(dom, codefo): 272 def svg_2_code(dom, codefo):