changeset 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 bec538d361e9
children 331467b8e778
files tools/mb_c_header.m4 tools/mb_c_source.m4 tools/svg2code.py
diffstat 3 files changed, 139 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/tools/mb_c_header.m4	Wed Jul 29 15:00:55 2009 +0800
+++ b/tools/mb_c_header.m4	Wed Jul 29 15:03:26 2009 +0800
@@ -20,6 +20,11 @@
 define([ADD_TEXT],[
 [    shape_t *$1;
 ]])
+define([ADD_STEXT],[
+[    shape_t *$1;
+     int $1_style_blks_num;
+     mb_style_blk_t *$1_style_blks;
+]])
 define([ADD_IMAGE],[[
     paint_t *$1_paint_img;
     shape_t *$1;
@@ -52,6 +57,7 @@
 define([COORD_MATRIX],)
 define([SHAPE_TRANSLATE],)
 define([SHAPE_MATRIX],)
+define([STYLE_BLOCK],[])
 define([ADD_SYMBOL],)
 define([SCENE])
 
--- a/tools/mb_c_source.m4	Wed Jul 29 15:00:55 2009 +0800
+++ b/tools/mb_c_source.m4	Wed Jul 29 15:03:26 2009 +0800
@@ -20,6 +20,7 @@
 define([ADD_RECT])
 define([ADD_COORD])
 define([ADD_TEXT],)
+define([ADD_STEXT])
 define([ADD_IMAGE],)
 define([PANGO_BEGIN_TEXT],)
 define([PANGO_END_TEXT],)
@@ -39,10 +40,24 @@
 define([COORD_MATRIX],)
 define([SHAPE_TRANSLATE],)
 define([SHAPE_MATRIX],)
+define([STYLE_BLOCK])
 define([ADD_SYMBOL],)
 define([SCENE])
 ])
 
+define([C_NL],[
+])
+define([RM_C_NL],
+        [ifelse(index([$1],C_NL), -1,
+                [$1],
+                [substr([$1], 0, index([$1],C_NL))[\n]RM_C_NL(substr([$1],
+             eval(index([$1],C_NL) + 1)))])])
+define([TO_CSTR], ["[]RM_C_NL(patsubst(patsubst(patsubst([$1],
+		  [\\], [[\\\\]]),
+		  [	],[[\\t]]),
+         	  ["], [[\\"]]))[]"])
+
+dnl -------------------- Declare Local Variables --------------------
 define([D_COLOR_STOP],[
 	{$6,$2,$3,$4,$5}])
 
@@ -60,14 +75,26 @@
 ])dnl
 ])
 
+define([D_ADD_STEXT],[dnl
+define([$1_CNT], 0)dnl
+[    mb_style_blk_t *$1_blk;
+]])
+
+define([D_STYLE_BLOCK],[dnl
+define([$1_CNT], eval($1_CNT + 1))dnl
+])
+
 define([DECLARE_VARS], [divert([-1])
 define([DIMPORT],[IMPORT(]QUOTE($[]1)[,[D_])])
 DECLARE_EMPTIES
 DIMPORT([ADD_LINEAR_PAINT])
 DIMPORT([ADD_RADIAL_PAINT])
 DIMPORT([COLOR_STOP])
+DIMPORT([ADD_STEXT])
+DIMPORT([STYLE_BLOCK])
 divert[]])
 
+dnl -------------------- Setup Value for Member Variables --------------------
 define([S_ADD_LINEAR_PAINT],[
     obj->$1 = rdman_paint_linear_new(rdman, $2, $3, $4, $5);
 ifelse(COUNT($6),0,,[dnl
@@ -242,6 +269,28 @@
     rdman_coord_changed(rdman, obj->$1);
 ]])
 
+define([S_ADD_STEXT],[dnl
+[    obj->$1 = rdman_shape_stext_new(rdman, ]TO_CSTR([$2])[, $3, $4);
+    rdman_add_shape(rdman, obj->$1, obj->$5);
+    obj->$1_style_blks_num = ]$1_CNT[;
+    obj->$1_style_blks = $1_blk =
+        (mb_style_blk_t *)malloc(sizeof(mb_style_blk_t) * ]$1_CNT[);
+]dnl
+define($1_IDX,0)dnl
+])
+
+define([S_STYLE_BLOCK],[dnl
+[    $1_blk->n_chars = $2;
+    $1_blk->font_sz = $4;
+    $1_blk->face = mb_font_face_query(rdman, ]TO_CSTR([$3])[, $5, $6);
+    $1_blk++;
+]dnl
+define([$1_IDX], eval($1_IDX + 1))dnl
+ifelse($1_IDX, $1_CNT,
+[    sh_stext_set_style(obj->$1, obj->$1_style_blks, ]$1_CNT[);
+])dnl
+])
+
 define([SETUP_VARS],[divert([-1])
 define([SIMPORT],[IMPORT(]QUOTE($[]1)[,[S_])])
 DECLARE_EMPTIES
@@ -254,6 +303,7 @@
 SIMPORT([ADD_RECT])
 SIMPORT([ADD_COORD])
 SIMPORT([ADD_TEXT])
+SIMPORT([ADD_STEXT])
 SIMPORT([ADD_IMAGE])
 SIMPORT([PANGO_BEGIN_TEXT])
 SIMPORT([PANGO_END_TEXT])
@@ -273,8 +323,10 @@
 SIMPORT([COORD_MATRIX])
 SIMPORT([SHAPE_TRANSLATE])
 SIMPORT([SHAPE_MATRIX])
+SIMPORT([STYLE_BLOCK])
 divert[]])
 
+dnl -------------------- Clear Member Variables --------------------
 define([F_ADD_LINEAR_PAINT],[[
     stops = paint_linear_stops(obj->$1, 0, NULL);
     free(stops);
@@ -321,6 +373,10 @@
     rdman_paint_free(rdman, obj->$1_stroke);
 ]])
 
+define([F_ADD_STEXT],[[
+    rdman_shape_free(rdman, obj->$1);
+]])
+
 define([CLEAR_VARS],[divert([-1])
 define([FIMPORT],[IMPORT(]QUOTE($[]1)[,[F_])])
 DECLARE_EMPTIES
@@ -335,6 +391,7 @@
 FIMPORT([STROKE_SHAPE])
 divert[]])
 
+dnl -------------------- Macro to Reverse Calling --------------------
 define([REVERSE_VARS],[divert([-1])
 define([__REV_VAR],[])
 define([PUSH_REV], [
@@ -374,6 +431,7 @@
 divert[]dnl
 ])
 
+dnl -------------------- Define Symbol Table --------------------
 define([Y_ADD_SYMBOL],[[{"$2", MB_SPRITE_OFFSET($1)},]])
 
 define([DECLARE_SYMS], [divert([-1])
@@ -407,6 +465,7 @@
 divert[]dnl
 ])
 
+dnl -------------------- C Template --------------------
 define([MADBUTTERFLY],[dnl
 [#include <stdio.h>
 #include <stdlib.h>
--- a/tools/svg2code.py	Wed Jul 29 15:00:55 2009 +0800
+++ b/tools/svg2code.py	Wed Jul 29 15:03:26 2009 +0800
@@ -574,17 +574,83 @@
             text_id.encode('utf8'), u''.join(txt_strs).encode('utf8'),
             x, y, coord_id.encode('utf8'))
 	translate_style(text, coord_id, codefo, doc, 'TEXT_')
-        if text.hasAttribute('mbname'):
-	     ## \note mbname declare that this node should be in the
-	     # symbol table.
-	     mbname = text.getAttribute('mbname')
-	     id = text.getAttribute('id')
-	     print >> codefo, 'ADD_SYMBOL([%s],[%s])dnl' % (id,mbname)
-             pass
+        pass
+    pass
+
+def stext_generate_font_attributes(text, attrs, coord_id, codefo, doc):
+    text_id = _get_id(text)
+    
+    for start, end, node in attrs:
+        style_map = node.style_map
+        
+        font_sz = 10
+        if style_map.has_key('font-size'):
+            fsz = style_map['font-size']
+            if fsz.endswith('px'):
+                font_sz = float(fsz[:-2])
+            else:
+                font_sz = float(fsz)
+                pass
+            pass
+        
+        if style_map.has_key('font-family'):
+            font_family = style_map['font-family']
+        else:
+            font_family = 'serif'
+            pass
+        
+        font_slant = 0
+        if style_map.has_key('font-style'):
+            fn_style = style_map['font-style']
+            if fn_style == 'normal':
+                font_slant = 0
+            elif fn_style == 'italic':
+                font_slant = 100
+            elif fn_style == 'oblique':
+                font_slant = 110
+            else:
+                raise ValueError, '%s is not a valid font-style' % (fn_style)
+            pass
+        
+        font_weight = 80
+        if style_map.has_key('font-weight'):
+            fn_weight = style_map['font-weight']
+            if fn_weight == 'normal':
+                font_weight = 80
+            elif fn_weight == 'medium':
+                font_weight = 100
+            elif fn_weight == 'bold':
+                font_weight = 200
+            elif fn_weight == 'bolder':
+                font_weight = 150
+            elif fn_weight == 'light':
+                font_weight = 50
+            elif fn_weight == 'lighter':
+                font_weight = 70
+            else:
+                font_weight = int(fn_weight)
+                pass
+            pass
+        
+        print >> codefo, 'STYLE_BLOCK([%s], %d, [%s], %f, %d, %d)dnl' % (
+            text_id, end - start, font_family, font_sz,
+            font_slant, font_weight)
         pass
     pass
 
 def stext_gen_text(text, coord_id, codefo, doc, txt_strs, attrs):
+    if not txt_strs:
+        return
+    
+    text_id = _get_id(text)
+    x = float(text.getAttribute('x'))
+    y = float(text.getAttribute('y'))
+    print >> codefo, 'dnl'
+    print >> codefo, \
+        'ADD_STEXT([%s], [%s], %f, %f, [%s])dnl' % \
+        (text_id, txt_strs.encode('utf8'), x, y, coord_id)
+    translate_style(text, coord_id, codefo, doc, 'STEXT_')
+    stext_generate_font_attributes(text, attrs, coord_id, codefo, doc)
     pass
 
 def gen_text(text, coord_id, codefo, doc, txt_strs, attrs):
@@ -608,7 +674,7 @@
         if node.localName == None:
             txt_strs = txt_strs + node.data
         elif node.localName == 'tspan':
-	    txt_strs = translate_tspan(node, text,coord_id, codefo,
+	    txt_strs = translate_tspan(node, text, coord_id, codefo,
                                        doc,txt_strs, attrs)
             pass
         pass