comparison tools/svg2code.py @ 241:104d83378582

Add scene support in svg2code.py. - Add mb_sprite_t::goto_scene() - svg2code.py recoganize "scenes" tag in metadata of SVG file. - tranform scenes into SCENE() macro. - define SCENE macro in mb_c_*.m4
author Thinker K.F. Li <thinker@branda.to>
date Wed, 31 Dec 2008 02:08:40 +0800
parents 3fadd2f2742e
children 50d253d0fcba a90fd749af82
comparison
equal deleted inserted replaced
240:d347a577a232 241:104d83378582
214 # Z : close path 214 # Z : close path
215 # L x y : lineto (x,y) 215 # L x y : lineto (x,y)
216 # H x : horizontal line to x 216 # H x : horizontal line to x
217 # V y : Vertical line to y 217 # V y : Vertical line to y
218 # C x1 y1 x2 y2 x y : Draw a segment of bezier curve 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 219 # S x2 y2 x t : Draw a segment of bezier curve from the last
220 # control point
220 # Q x1 y1 x y : Draw a segment of quadratic curve 221 # 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 # T x y : Draw a segment of quadratic curve from the last
223 # control pint
222 # A x y r f s x y : Draw an arc 224 # 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 225 # translate the path data into two arrays. The first is an integer whose
224 # bits are the command type The second array hold all arguments. 226 # upper 8 bits are the command type The second array hold all arguments.
225 227
226 command_length={'M': 2, 'm':2, 228 command_length={'M': 2, 'm':2,
227 'Z': 0, 'z':0, 229 'Z': 0, 'z':0,
228 'L': 2, 'l':2, 230 'L': 2, 'l':2,
229 'H': 1, 'h':1, 231 'H': 1, 'h':1,
448 translate_text(node, group_id, codefo, doc) 450 translate_text(node, group_id, codefo, doc)
449 pass 451 pass
450 pass 452 pass
451 pass 453 pass
452 454
455 ## \brief Translate "scenes" tag in "metadata" tag.
456 #
457 def translate_scenes(scenes_node, codefo, doc):
458 scenes = []
459 for scene in scenes_node.childNodes:
460 if scene.localName != 'scene' or \
461 not scene.hasAttribute('ref') or \
462 not scene.hasAttribute('start'):
463 continue
464
465 start_str = scene.getAttribute('start')
466 start = end = int(start_str)
467 if scene.hasAttribute('end'):
468 end_str = scene.getAttribute('end')
469 end = int(end_str)
470 pass
471 ref = scene.getAttribute('ref')
472
473 while len(scenes) <= end:
474 scenes.append([])
475 pass
476
477 for i in range(start, end + 1):
478 scenes[i].append(ref)
479 pass
480 pass
481
482 for scene_idx, groups_in_scene in enumerate(scenes):
483 if groups_in_scene:
484 groups_str = '[' + '],['.join(groups_in_scene) + ']'
485 else:
486 groups_str = ''
487 pass
488 print >> codefo, \
489 'SCENE(%d, [%s])dnl' % (scene_idx, groups_str)
490 pass
491 pass
492
453 def svg_2_code(dom, codefo): 493 def svg_2_code(dom, codefo):
454 for node in dom.childNodes: 494 for node in dom.childNodes:
455 if node.localName == 'svg' and node.namespaceURI == svgns: 495 if node.localName == 'svg' and node.namespaceURI == svgns:
456 break; 496 break;
457 pass 497 pass
460 500
461 svg = node 501 svg = node
462 for node in svg.childNodes: 502 for node in svg.childNodes:
463 if node.localName == 'defs' and node.namespaceURI == svgns: 503 if node.localName == 'defs' and node.namespaceURI == svgns:
464 translate_defs(node, codefo, dom) 504 translate_defs(node, codefo, dom)
505 elif node.localName == 'metadata' and node.namespaceURI == svgns:
506 for meta_node in node.childNodes:
507 if meta_node.localName == 'scenes':
508 translate_scenes(meta_node, codefo, dom)
509 pass
510 pass
465 pass 511 pass
466 elif node.localName == 'g' and node.namespaceURI == svgns: 512 elif node.localName == 'g' and node.namespaceURI == svgns:
467 translate_group(node, 'root_coord', codefo, dom) 513 translate_group(node, 'root_coord', codefo, dom)
468 pass 514 pass
469 pass 515 pass
477 elif len(sys.argv) == 2: 523 elif len(sys.argv) == 2:
478 svgfn = sys.argv[1] 524 svgfn = sys.argv[1]
479 codefn = 'out.mb' 525 codefn = 'out.mb'
480 else: 526 else:
481 print >> sys.stderr, '%s <SVG file> [<output>]' % (sys.argv[0]) 527 print >> sys.stderr, '%s <SVG file> [<output>]' % (sys.argv[0])
528 sys.exit(1)
482 pass 529 pass
483 530
484 struct_name = path.basename(codefn).split('.')[0] 531 struct_name = path.basename(codefn).split('.')[0]
485 532
486 dom = parse(svgfn) 533 dom = parse(svgfn)