comparison tools/svg2code.py @ 431:bf1addb037b7

Add -s option to svg2code.py. Option -s is for sh_stext type. With this option, svg2code.py will generate code to using sh_stext type instead of sh_text for text nodes.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 29 Jul 2009 15:03:26 +0800
parents c3faebaec0c4
children b5c7670b524b
comparison
equal deleted inserted replaced
430:bec538d361e9 431:bf1addb037b7
572 print >> codefo, \ 572 print >> codefo, \
573 'PANGO_END_TEXT([%s], [%s], %f, %f, 16, [%s])dnl' % ( 573 'PANGO_END_TEXT([%s], [%s], %f, %f, 16, [%s])dnl' % (
574 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'), 574 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'),
575 x, y, coord_id.encode('utf8')) 575 x, y, coord_id.encode('utf8'))
576 translate_style(text, coord_id, codefo, doc, 'TEXT_') 576 translate_style(text, coord_id, codefo, doc, 'TEXT_')
577 if text.hasAttribute('mbname'): 577 pass
578 ## \note mbname declare that this node should be in the 578 pass
579 # symbol table. 579
580 mbname = text.getAttribute('mbname') 580 def stext_generate_font_attributes(text, attrs, coord_id, codefo, doc):
581 id = text.getAttribute('id') 581 text_id = _get_id(text)
582 print >> codefo, 'ADD_SYMBOL([%s],[%s])dnl' % (id,mbname) 582
583 pass 583 for start, end, node in attrs:
584 style_map = node.style_map
585
586 font_sz = 10
587 if style_map.has_key('font-size'):
588 fsz = style_map['font-size']
589 if fsz.endswith('px'):
590 font_sz = float(fsz[:-2])
591 else:
592 font_sz = float(fsz)
593 pass
594 pass
595
596 if style_map.has_key('font-family'):
597 font_family = style_map['font-family']
598 else:
599 font_family = 'serif'
600 pass
601
602 font_slant = 0
603 if style_map.has_key('font-style'):
604 fn_style = style_map['font-style']
605 if fn_style == 'normal':
606 font_slant = 0
607 elif fn_style == 'italic':
608 font_slant = 100
609 elif fn_style == 'oblique':
610 font_slant = 110
611 else:
612 raise ValueError, '%s is not a valid font-style' % (fn_style)
613 pass
614
615 font_weight = 80
616 if style_map.has_key('font-weight'):
617 fn_weight = style_map['font-weight']
618 if fn_weight == 'normal':
619 font_weight = 80
620 elif fn_weight == 'medium':
621 font_weight = 100
622 elif fn_weight == 'bold':
623 font_weight = 200
624 elif fn_weight == 'bolder':
625 font_weight = 150
626 elif fn_weight == 'light':
627 font_weight = 50
628 elif fn_weight == 'lighter':
629 font_weight = 70
630 else:
631 font_weight = int(fn_weight)
632 pass
633 pass
634
635 print >> codefo, 'STYLE_BLOCK([%s], %d, [%s], %f, %d, %d)dnl' % (
636 text_id, end - start, font_family, font_sz,
637 font_slant, font_weight)
584 pass 638 pass
585 pass 639 pass
586 640
587 def stext_gen_text(text, coord_id, codefo, doc, txt_strs, attrs): 641 def stext_gen_text(text, coord_id, codefo, doc, txt_strs, attrs):
642 if not txt_strs:
643 return
644
645 text_id = _get_id(text)
646 x = float(text.getAttribute('x'))
647 y = float(text.getAttribute('y'))
648 print >> codefo, 'dnl'
649 print >> codefo, \
650 'ADD_STEXT([%s], [%s], %f, %f, [%s])dnl' % \
651 (text_id, txt_strs.encode('utf8'), x, y, coord_id)
652 translate_style(text, coord_id, codefo, doc, 'STEXT_')
653 stext_generate_font_attributes(text, attrs, coord_id, codefo, doc)
588 pass 654 pass
589 655
590 def gen_text(text, coord_id, codefo, doc, txt_strs, attrs): 656 def gen_text(text, coord_id, codefo, doc, txt_strs, attrs):
591 raise NotImplementedError, \ 657 raise NotImplementedError, \
592 'gen_text should be assigned to an implementation' 658 'gen_text should be assigned to an implementation'
606 txt_strs = '' 672 txt_strs = ''
607 for node in text.childNodes: 673 for node in text.childNodes:
608 if node.localName == None: 674 if node.localName == None:
609 txt_strs = txt_strs + node.data 675 txt_strs = txt_strs + node.data
610 elif node.localName == 'tspan': 676 elif node.localName == 'tspan':
611 txt_strs = translate_tspan(node, text,coord_id, codefo, 677 txt_strs = translate_tspan(node, text, coord_id, codefo,
612 doc,txt_strs, attrs) 678 doc,txt_strs, attrs)
613 pass 679 pass
614 pass 680 pass
615 attr[1] = len(txt_strs.encode('utf8')) 681 attr[1] = len(txt_strs.encode('utf8'))
616 gen_text(text, coord_id, codefo, doc, txt_strs, attrs) 682 gen_text(text, coord_id, codefo, doc, txt_strs, attrs)