comparison tools/svg2code.py @ 273:0cadeb9fdfc3

Merged
author wycc
date Mon, 26 Jan 2009 01:37:04 +0800
parents c96f38ad4bb6
children c8b6ca46950b
comparison
equal deleted inserted replaced
272:01439f28d0bd 273:0cadeb9fdfc3
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 ## \brief Calculate geometry of ellipse where the arc is on.
214 #
215 # This function calculate the ellipse with information from SVG path data.
216 #
217 # \see calc_center_and_x_aix()
218 def _calc_ellipse_of_arc(x0, y0, rx, ry, x_rotate, large, sweep, x, y):
219 import math
220
221 _sin = math.sin(x_rotate)
222 _cos = math.cos(x_rotate)
223
224 nrx = x * _cos + y * _sin
225 nry = x * -_sin + y * _cos
226 nrx0 = x0 * _cos + y0 * _sin
227 nry0 = x0 * -_sin + y0 * _cos
228
229 udx = (nrx - nrx0) / 2 / rx # ux - umx
230 udy = (nry - nry0) / 2 / ry # uy - umy
231 umx = (nrx + nrx0) / 2 / rx
232 umy = (nry + nry0) / 2 / ry
233
234 udx2 = udx * udx
235 udy2 = udy * udy
236 udl2 = udx2 + udy2
237
238 if udy != 0:
239 # center is at left-side of arc
240 udcx = -math.sqrt((1 - udl2) * udl2) / (udy + udx2 / udy)
241 udcy = -udcx * udx / udy
242 else:
243 # center is at down-side of arc
244 udcx = 0
245 udcy = math.sqrt((1 - udl2) * udl2) / udx
246 pass
247
248 reflect = 0
249 if large:
250 reflect ^= 1
251 pass
252 if sweep != 1:
253 reflect ^= 1
254 pass
255 if reflect:
256 udcx = -udcx
257 udcy = -udcy
258 pass
259
260 nrcx = rx * (udcx + umx)
261 nrcy = ry * (udcy + umy)
262
263 cx = nrcx * _cos - nrcy * _sin
264 cy = nrcx * _sin + nrcy * _cos
265
266 xx = rx * _cos + cx
267 xy = rx * _sin + cy
268 return cx, cy, xx, xy
269
213 # M x y : Move to (x,y) 270 # M x y : Move to (x,y)
214 # Z : close path 271 # Z : close path
215 # L x y : lineto (x,y) 272 # L x y : lineto (x,y)
216 # H x : horizontal line to x 273 # H x : horizontal line to x
217 # V y : Vertical line to y 274 # V y : Vertical line to y
231 'H': 1, 'h':1, 288 'H': 1, 'h':1,
232 'V': 1, 'v':1, 289 'V': 1, 'v':1,
233 'C': 6, 'c':6, 290 'C': 6, 'c':6,
234 'S': 4, 's':4, 291 'S': 4, 's':4,
235 'Q': 4, 'q':4, 292 'Q': 4, 'q':4,
236 'T': 2, 't':2} 293 'T': 2, 't':2,
237 294 'A': 7, 'a':7}
238 295
239 def translate_path_data(data,codefo): 296 def translate_path_data(data,codefo):
240 temp = data.split() 297 temp = data.split()
241 fields=[] 298 fields=[]
242 for f in temp: 299 for f in temp:
243 for s in f.split(','): 300 for s in f.split(','):
244 if s != '': 301 if s != '':
245 fields.append(s) 302 fields.append(s)
246 cmd = '' 303 cmd = ''
304 cmd_args = 0
247 commands='' 305 commands=''
248 args=[] 306 args=[]
307 narg = 0
249 fix_args=[] 308 fix_args=[]
250 for f in fields: 309 for f in fields:
251 if cmd == 'A' or cmd == 'a': 310 if f in command_length:
252 try: 311 if cmd_args != 0 and (narg % cmd_args) != 0:
253 d = int(f) 312 raise ValueError, 'invalid path data %s' % (repr(fields))
254 fix_args.append(d) 313 cmd = f
255 if (narg % 7) == 0: 314 cmd_args = command_length[f]
256 commands = commands + cmd 315 narg = 0
257 narg = narg + 1 316 continue
258 except: 317
259 pass 318 if (narg % cmd_args) == 0:
260 else: 319 commands = commands + cmd
261 try: 320 pass
262 d = float(f) 321 arg = float(f)
263 args.append(d) 322 args.append(arg)
264 if (narg % command_length[cmd]) == 0: 323 narg = narg + 1
265 commands = commands + cmd 324
266 narg = narg + 1 325 if (narg % cmd_args) == 0 and (cmd in 'Aa'):
267 continue 326 x0, y0, rx, ry, x_rotate, large, sweep, x, y = \
268 except: 327 tuple(args[-9:])
269 pass 328 x_rotate = int(x_rotate)
270 cmd = f 329 large = int(large)
271 narg=0 330 sweep = int(sweep)
272 pass 331 cx, cy, xx, xy = _calc_ellipse_of_arc(x0, y0, rx, ry,
273 return [commands,args,fix_args] 332 x_rotate, large,
333 sweep, x, y)
334 args[-7:] = [cx, cy, xx, xy, x, y]
335 fix_args.append(sweep)
336 pass
337 pass
338 return commands, args, fix_args
274 339
275 _id_sn = 0 340 _id_sn = 0
276 341
277 def _get_id(obj): 342 def _get_id(obj):
278 global _id_sn 343 global _id_sn
305 def translate_path(path, coord_id, codefo, doc): 370 def translate_path(path, coord_id, codefo, doc):
306 coord_id = translate_shape_transform(path, coord_id, codefo) 371 coord_id = translate_shape_transform(path, coord_id, codefo)
307 372
308 path_id = path.getAttribute('id') 373 path_id = path.getAttribute('id')
309 d = path.getAttribute('d') 374 d = path.getAttribute('d')
310 (commands,args,fix_args) = translate_path_data(d,codefo) 375 commands, args, fix_args = translate_path_data(d,codefo)
311 print >> codefo, 'dnl' 376 print >> codefo, 'dnl'
312 #print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id) 377 #print >> codefo, 'ADD_PATH([%s], [%s], [%s])dnl' % (path_id, d, coord_id)
313 sarg='' 378 sarg=''
314 for c in args: 379 for c in args:
315 sarg = sarg + "%f," % c 380 sarg = sarg + "%f," % c
534 pass 599 pass
535 600
536 def svg_2_code(dom, codefo): 601 def svg_2_code(dom, codefo):
537 for node in dom.childNodes: 602 for node in dom.childNodes:
538 if node.localName == 'svg' and node.namespaceURI == svgns: 603 if node.localName == 'svg' and node.namespaceURI == svgns:
539 break; 604 break
540 pass 605 pass
541 else: 606 else:
542 raise ValueErr, 'no any svg tag node.' 607 raise ValueErr, 'no any svg tag node.'
543 608
544 svg = node 609 svg = node