Mercurial > MadButterfly
comparison tools/svg2code.py @ 278:a90fd749af82 mbtext
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
author | wycc |
---|---|
date | Sat, 31 Jan 2009 09:41:04 +0800 |
parents | 104d83378582 |
children | 86a5ae82ccf2 |
comparison
equal
deleted
inserted
replaced
277:5006e4abdda5 | 278:a90fd749af82 |
---|---|
126 def translate_style(node, coord_id, codefo, doc, prefix): | 126 def translate_style(node, coord_id, codefo, doc, prefix): |
127 node_id = node.getAttribute('id') | 127 node_id = node.getAttribute('id') |
128 style_str = node.getAttribute('style') | 128 style_str = node.getAttribute('style') |
129 prop_map = get_style_map(style_str) | 129 prop_map = get_style_map(style_str) |
130 | 130 |
131 print node_id,style_str | |
131 try: | 132 try: |
132 opacity = float(node.getAttribute('opacity')) | 133 opacity = float(node.getAttribute('opacity')) |
133 except: | 134 except: |
134 opacity = 1.0 | 135 opacity = 1.0 |
135 pass | 136 pass |
348 def translate_font_style(text, codefo): | 349 def translate_font_style(text, codefo): |
349 text_id = _get_id(text) | 350 text_id = _get_id(text) |
350 style_str = text.getAttribute('style') | 351 style_str = text.getAttribute('style') |
351 style_map = get_style_map(style_str) | 352 style_map = get_style_map(style_str) |
352 | 353 |
353 font_sz = 10.0 | 354 return style_map |
354 if style_map.has_key('font-size'): | 355 |
355 if style_map['font-size'].endswith('px'): | 356 |
356 font_sz = float(style_map['font-size'][:-2]) | 357 def translate_tspan(tspan, coord_id, codefo, doc,txt_strs,attrs): |
357 print >> codefo, 'define([MB_FONT_SZ], %f)dnl' % (font_sz) | 358 map = translate_font_style(tspan, codefo) |
358 pass | 359 tspan.style_map = map |
359 pass | 360 attr = [len(txt_strs),0, tspan] |
360 | 361 attrs.append(attr) |
361 font_style = 'normal' | 362 for node in tspan.childNodes: |
362 if style_map.has_key('font-style'): | 363 if node.localName == None: |
363 font_style = style_map['font-style'].lower() | 364 txt_strs = txt_strs + node.data |
364 pass | 365 elif node.localName == 'tspan': |
365 | 366 txt_strs = translate_tspan(node,coord_id, codefo, doc,txt_strs,attrs) |
366 font_family = 'Roman' | 367 pass |
367 if style_map.has_key('font-family'): | 368 pass |
368 font_family = style_map['font-family'].lower() | 369 attr[1] = len(txt_strs)-1 |
369 pass | 370 return txt_strs |
370 pass | 371 |
371 | 372 |
373 | |
374 def generate_font_attributes(attrs,coord_id, codefo,doc): | |
375 for a in attrs: | |
376 start = a[0] | |
377 end = a[1] | |
378 node = a[2] | |
379 #print "generate attributes from %d to %d" %(start,end) | |
380 style_map = node.style_map | |
381 #print [style_map] | |
382 if style_map.has_key('font-size'): | |
383 # FIXME: Implement all units here | |
384 if style_map['font-size'].endswith('px'): | |
385 font_sz = float(style_map['font-size'][:-2]) | |
386 print >> codefo, 'PANGO_SIZE(%d,%d,%d)' % (font_sz,start,end) | |
387 else: | |
388 font_sz = float(style_map['font-size']) | |
389 print >> codefo, 'PANGO_SIZE(%d,%d,%d)' % (font_sz,start,end) | |
390 pass | |
391 | |
392 if style_map.has_key('font-style'): | |
393 font_style = style_map['font-style'].lower() | |
394 if font_style == 'normal': | |
395 print >> codefo, 'PANGO_STYLE(PANGO_STYLE_NORMAL,%d,%d)' % (start,end) | |
396 elif font_style == 'italic': | |
397 print >> codefo, 'PANGO_STYLE(PANGO_STYLE_ITALIC,%d,%d)' % (start,end) | |
398 elif font_style == 'oblique': | |
399 print >> codefo, 'PANGO_STYLE(PANGO_STYLE_OBLIQUE,%d,%d)' % (start,end) | |
400 pass | |
401 | |
402 if style_map.has_key('font-family'): | |
403 font_family = style_map['font-family'].lower() | |
404 print >> codefo, 'PANGO_FAMILY(%s,%d,%d)' % (font_family,start,end) | |
405 pass | |
406 if style_map.has_key('text-anchor'): | |
407 text_anchor = style_map['text-anchor'].lower() | |
408 # FIXME: We need to implement a mb_text_set_aligment to implement SVG-styled alignment. | |
409 print "The text-anchor is not implemented yet" | |
410 pass | |
411 if style_map.has_key('font-variant'): | |
412 font_variant = style_map['font-variant'].lower() | |
413 print "The font-variant is not implemented yet" | |
414 pass | |
415 if style_map.has_key('font-weight'): | |
416 font_weight = style_map['font-weight'].lower() | |
417 if font_weight == 'normal': | |
418 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_NORMAL,%d,%d)' % (start,end) | |
419 elif font_weight == 'bold': | |
420 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_BOLD,%d,%d)' % (start,end) | |
421 elif font_weight == 'bolder': | |
422 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_HEAVY,%d,%d)' % (start,end) | |
423 elif font_weight == 'lighter': | |
424 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRALIGHT,%d,%d)' % (start,end) | |
425 elif font_weight == '100': | |
426 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRALIGHT,%d,%d)' % (start,end) | |
427 elif font_weight == '200': | |
428 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRALIGHT,%d,%d)' % (start,end) | |
429 elif font_weight == '300': | |
430 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_LIGHT,%d,%d)' % (start,end) | |
431 elif font_weight == '400': | |
432 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_NORMAL,%d,%d)' % (start,end) | |
433 elif font_weight == '500': | |
434 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_NORMAL,%d,%d)' % (start,end) | |
435 elif font_weight == '600': | |
436 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_SEMIBOLD,%d,%d)' % (start,end) | |
437 elif font_weight == '700': | |
438 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_BOLD,%d,%d)' % (start,end) | |
439 elif font_weight == '800': | |
440 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_ULTRABOLD,%d,%d)' % (start,end) | |
441 elif font_weight == '900': | |
442 print >> codefo, 'PANGO_STYLE(PANGO_WEIGHT_HEAVY,%d,%d)' % (start,end) | |
443 else: | |
444 print "The font-weight %s is not supported" % font_weight | |
445 pass | |
446 if style_map.has_key('direction'): | |
447 direction = style_map['direction'].lower() | |
448 print "The direction is not implemented yet" | |
449 pass | |
450 if style_map.has_key('unicode-bidi'): | |
451 bidi = style_map['unicode-bidi'].lower() | |
452 print "The bidi is not implemented yet" | |
453 pass | |
454 pass | |
455 | |
372 def translate_text(text, coord_id, codefo, doc): | 456 def translate_text(text, coord_id, codefo, doc): |
373 translate_font_style(text, codefo) | 457 map = translate_font_style(text, codefo) |
374 | 458 attrs = [] |
375 txt_strs = [] | 459 text.style_map = map |
460 attr = [0,0, text] | |
461 attrs.append(attr) | |
462 | |
463 txt_strs = '' | |
376 for node in text.childNodes: | 464 for node in text.childNodes: |
377 if node.localName == None: | 465 if node.localName == None: |
378 txt_strs.append(node.data) | 466 txt_strs = txt_strs + node.data |
379 elif node.localName == 'tspan': | 467 elif node.localName == 'tspan': |
380 node.setAttribute('style', text.getAttribute('style')) | 468 txt_strs = translate_tspan(node,coord_id, codefo, doc,txt_strs,attrs) |
381 translate_text(node, coord_id, codefo, doc) | 469 pass |
382 pass | 470 pass |
383 pass | 471 attr[1] = len(txt_strs)-1 |
384 if txt_strs: | 472 if txt_strs: |
385 text_id = _get_id(text) | 473 text_id = _get_id(text) |
386 x = float(text.getAttribute('x')) | 474 x = float(text.getAttribute('x')) |
387 y = float(text.getAttribute('y')) | 475 y = float(text.getAttribute('y')) |
388 print >> codefo, 'dnl' | 476 print >> codefo, 'dnl' |
389 print >> codefo, \ | 477 print >> codefo, \ |
390 'ADD_TEXT([%s], [%s], %f, %f, MB_FONT_SZ, [%s])dnl' % ( | 478 'PANGO_BEGIN_TEXT([%s], [%s], %f, %f, 16, [%s])dnl' % ( |
391 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'), | 479 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'), |
392 x, y, coord_id.encode('utf8')) | 480 x, y, coord_id.encode('utf8')) |
393 translate_style(text, coord_id, codefo, doc, 'TEXT_') | 481 generate_font_attributes(attrs, coord_id, codefo, doc) |
482 print >> codefo, \ | |
483 'PANGO_END_TEXT([%s], [%s], %f, %f, 16, [%s])dnl' % ( | |
484 text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'), | |
485 x, y, coord_id.encode('utf8')) | |
486 translate_style(text, coord_id, codefo, doc, 'TEXT_') | |
394 pass | 487 pass |
395 pass | 488 pass |
396 | 489 |
397 reo_func = re.compile('([a-zA-Z]+)\\([^\\)]*\\)') | 490 reo_func = re.compile('([a-zA-Z]+)\\([^\\)]*\\)') |
398 reo_translate = re.compile('translate\\(([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)\\)') | 491 reo_translate = re.compile('translate\\(([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)\\)') |
447 elif node.localName == 'rect': | 540 elif node.localName == 'rect': |
448 translate_rect(node, group_id, codefo, doc) | 541 translate_rect(node, group_id, codefo, doc) |
449 elif node.localName == 'text': | 542 elif node.localName == 'text': |
450 translate_text(node, group_id, codefo, doc) | 543 translate_text(node, group_id, codefo, doc) |
451 pass | 544 pass |
545 elif node.localName == 'textarea': | |
546 translate_textarea(node, group_id, codefo, doc) | |
547 pass | |
452 pass | 548 pass |
453 pass | 549 pass |
454 | 550 |
455 ## \brief Translate "scenes" tag in "metadata" tag. | 551 ## \brief Translate "scenes" tag in "metadata" tag. |
456 # | 552 # |