comparison tools/svg2code.py @ 197:bcad1ccdf45c

Translate the path string into binary array to save the parsing in the runtime. It can reduce the size as well.
author wycc@wycc-desktop
date Wed, 19 Nov 2008 00:27:20 +0800
parents d6f9af55b0d0
children 3fadd2f2742e
comparison
equal deleted inserted replaced
192:54fdc2a65242 197:bcad1ccdf45c
208 translate_transform(shape_coord_id, transform, codefo, 'SHAPE_') 208 translate_transform(shape_coord_id, transform, codefo, 'SHAPE_')
209 coord_id = shape_coord_id 209 coord_id = shape_coord_id
210 pass 210 pass
211 return coord_id 211 return coord_id
212 212
213 # M x y : Move to (x,y)
214 # Z : close path
215 # L x y : lineto (x,y)
216 # H x : horizontal line to x
217 # V y : Vertical line to y
218 # C x1 y1 x2 y2 x y : Draw a segment of bezier curve
219 # S x2 y2 x t : Draw a segment of bezier curve from the last control point
220 # Q x1 y1 x y : Draw a segment of quadratic curve
221 # T x y : Draw a segment of quadratic curve from the last control pint
222 # A x y r f s x y : Draw an arc
223 # translate the path data into two arrays. The first is an integer whose upper 8
224 # bits are the command type The second array hold all arguments.
225
226 command_length={'M': 2, 'm':2,
227 'Z': 0, 'z':0,
228 'L': 2, 'l':2,
229 'H': 1, 'h':1,
230 'V': 1, 'v':1,
231 'C': 6, 'c':6,
232 'S': 4, 's':4,
233 'Q': 4, 'q':4,
234 'T': 2, 't':2}
235
236
237 def translate_path_data(data,codefo):
238 temp = data.split()
239 fields=[]
240 for f in temp:
241 for s in f.split(','):
242 if s != '':
243 fields.append(s)
244 cmd = ''
245 commands=''
246 args=[]
247 fix_args=[]
248 for f in fields:
249 if cmd == 'A' or cmd == 'a':
250 try:
251 d = int(f)
252 fix_args.append(d)
253 if (narg % 7) == 0:
254 commands = commands + cmd
255 narg = narg + 1
256 except:
257 pass
258 else:
259 try:
260 d = float(f)
261 args.append(d)
262 if (narg % command_length[cmd]) == 0:
263 commands = commands + cmd
264 narg = narg + 1
265 continue
266 except:
267 pass
268 cmd = f
269 narg=0
270 pass
271 return [commands,args,fix_args]
272
213 def translate_path(path, coord_id, codefo, doc): 273 def translate_path(path, coord_id, codefo, doc):
214 coord_id = translate_shape_transform(path, coord_id, codefo) 274 coord_id = translate_shape_transform(path, coord_id, codefo)
215 275
216 path_id = path.getAttribute('id') 276 path_id = path.getAttribute('id')
217 d = path.getAttribute('d') 277 d = path.getAttribute('d')
278 (commands,args,fix_args) = translate_path_data(d,codefo)
218 print >> codefo, 'dnl' 279 print >> codefo, 'dnl'
219 print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id) 280 #print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id)
281 sarg=''
282 for c in args:
283 sarg = sarg + "%f," % c
284 s_fix_arg=''
285 for c in fix_args:
286 s_fix_arg = s_fix_arg + ("%d," % c)
287
288 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))
220 289
221 translate_style(path, coord_id, codefo, doc, 'PATH_') 290 translate_style(path, coord_id, codefo, doc, 'PATH_')
222 pass 291 pass
223 292
224 def translate_rect(rect, coord_id, codefo, doc): 293 def translate_rect(rect, coord_id, codefo, doc):