# HG changeset patch
# User wycc
# Date 1233373879 -28800
# Node ID 86a5ae82ccf2ba864f6ce7427d38aaa8254c084a
# Parent a90fd749af82d6238d977a09a0e9d022724bef8c
* Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
* Generate seperate text segments if the x is defined in the tspan. With this settings, we may be able to implement the text alignment already.
* Be warnned that the text element may not be generated at all. We may need to convert the text element into a group for this situation. Currently, we leave it as it is.
diff -r a90fd749af82 -r 86a5ae82ccf2 examples/calculator/calculator_scr.svg
--- a/examples/calculator/calculator_scr.svg Sat Jan 31 09:41:04 2009 +0800
+++ b/examples/calculator/calculator_scr.svg Sat Jan 31 11:51:19 2009 +0800
@@ -509,9 +509,9 @@
style="font-size:28px;font-style:normal;font-weight:normal;fill:#008000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
x="36.939316"
y="61.546173"
- id="screen_text_u">0
None
diff -r a90fd749af82 -r 86a5ae82ccf2 examples/svg2code_ex/svg2code_ex.svg
--- a/examples/svg2code_ex/svg2code_ex.svg Sat Jan 31 09:41:04 2009 +0800
+++ b/examples/svg2code_ex/svg2code_ex.svg Sat Jan 31 11:51:19 2009 +0800
@@ -329,7 +329,7 @@
height="600px"
showgrid="true"
inkscape:window-width="822"
- inkscape:window-height="695"
+ inkscape:window-height="721"
inkscape:window-x="130"
inkscape:window-y="120" />
test
+ sodipodi:role="line"
+ style="font-size:28px">test
face);
}
+static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr);
shape_t *rdman_shape_text_new(redraw_man_t *rdman,
const char *txt, co_aix x, co_aix y,
co_aix font_size, cairo_font_face_t *face,PangoAttrList *attrs) {
@@ -62,6 +63,7 @@
text->shape.free = sh_text_free;
text->layout = NULL;
text->attrs = attrs;
+ sh_text_P_generate_layout(text, rdman->cr);
rdman_shape_man(rdman, (shape_t *)text);
@@ -77,39 +79,14 @@
text->data = buf;
}
-static int get_extents(sh_text_t *text, cairo_text_extents_t *extents) {
+static int get_extents(sh_text_t *text, PangoRectangle *extents) {
cairo_matrix_t fmatrix;
cairo_matrix_t ctm;
cairo_scaled_font_t *new_scaled;
cairo_font_options_t *fopt;
- if((text->flags & TXF_SCALE_DIRTY) ||
- text->scaled_font == NULL) {
- fopt = cairo_font_options_create();
- if(fopt == NULL)
- return ERR;
- memset(&fmatrix, 0, sizeof(cairo_matrix_t));
- fmatrix.xx = text->d_font_size;
- fmatrix.yy = text->d_font_size;
- memset(&ctm, 0, sizeof(cairo_matrix_t));
- ctm.xx = 1;
- ctm.yy = 1;
- new_scaled = cairo_scaled_font_create(text->face,
- &fmatrix,
- &ctm,
- fopt);
- cairo_font_options_destroy(fopt);
- if(new_scaled == NULL)
- return ERR;
-
- if(text->scaled_font)
- cairo_scaled_font_destroy(text->scaled_font);
- text->scaled_font = new_scaled;
- text->flags &= ~TXF_SCALE_DIRTY;
- }
-
- cairo_scaled_font_text_extents(text->scaled_font,
- text->data, extents);
+ pango_layout_get_extents(text->layout, NULL, extents);
+ pango_extents_to_pixels(extents,NULL);
return OK;
}
@@ -117,7 +94,7 @@
sh_text_t *text;
co_aix x, y;
co_aix shw;
- cairo_text_extents_t extents;
+ PangoRectangle extents;
co_aix poses[2][2];
int r;
@@ -129,6 +106,8 @@
y = text->y;
coord_trans_pos(shape->coord, &x, &y);
r = get_extents(text, &extents);
+
+ printf("x=%f y=%f text=%s ascent=%d,descent=%d,width=%d height=%d\n", x,y,text->data,PANGO_ASCENT(extents), PANGO_DESCENT(extents), extents.width, extents.height);
ASSERT(r == OK);
text->d_x = x;
@@ -137,8 +116,9 @@
/* FIXME: It is unreasonable that a font exceed it's bbox.
* We add 5 pixels in get right bbox. But, it is unreasonable.
*/
- poses[0][0] = x + extents.x_bearing - 5 - shw;
- poses[0][1] = y + extents.y_bearing - 5 - shw;
+
+ poses[0][0] = x + extents.x - 5 - shw;
+ poses[0][1] = y + extents.y - 5 - shw;
poses[1][0] = poses[0][0] + extents.width + 10 + shape->stroke_width;
poses[1][1] = poses[0][1] + extents.height + 10 + shape->stroke_width;
geo_from_positions(shape->geo, 2, poses);
@@ -158,14 +138,11 @@
desc = pango_font_description_from_string("Sans Bold");
pango_layout_set_font_description (text->layout, desc);
pango_cairo_update_layout(cr,text->layout);
- pango_layout_set_text(text->layout,text->data,-1);
+ pango_layout_set_text(text->layout,text->data,strlen(text->data));
pango_layout_set_attributes(text->layout, text->attrs);
-
}
static void draw_text(sh_text_t *text, cairo_t *cr) {
- if (text->layout==NULL) {
- sh_text_P_generate_layout(text,cr);
- }
+ sh_text_P_generate_layout(text, cr);
cairo_move_to(cr, text->d_x, text->d_y);
pango_cairo_show_layout(cr,text->layout);
}
diff -r a90fd749af82 -r 86a5ae82ccf2 tools/svg2code.py
--- a/tools/svg2code.py Sat Jan 31 09:41:04 2009 +0800
+++ b/tools/svg2code.py Sat Jan 31 11:51:19 2009 +0800
@@ -125,10 +125,12 @@
def translate_style(node, coord_id, codefo, doc, prefix):
node_id = node.getAttribute('id')
- style_str = node.getAttribute('style')
- prop_map = get_style_map(style_str)
+ try:
+ prop_map = node.style_map
+ except:
+ style_str = node.getAttribute('style')
+ prop_map = get_style_map(style_str)
- print node_id,style_str
try:
opacity = float(node.getAttribute('opacity'))
except:
@@ -353,11 +355,26 @@
return style_map
-
-def translate_tspan(tspan, coord_id, codefo, doc,txt_strs,attrs):
- map = translate_font_style(tspan, codefo)
- tspan.style_map = map
- attr = [len(txt_strs),0, tspan]
+def merge_style(tspan,text):
+ newmap = tspan.style_map
+ map = text.style_map
+ for k,v in text.style_map.items():
+ if not newmap.has_key(k):
+ newmap[k] = v
+def translate_tspan(tspan, text,coord_id, codefo, doc,txt_strs,attrs):
+ try:
+ map = tspan.style_map
+ except:
+ map = translate_font_style(tspan, codefo)
+ tspan.style_map = map
+ if tspan.hasAttribute('x'):
+ # Render the tspan as an independent text if the x attribute is defined. All elements inside
+ # the tspan will be ignore by the outter text or tspan elements.
+ # FIXME: We need to apply the style map recursively.
+ merge_style(tspan,text)
+ translate_text(tspan,coord_id,codefo,doc)
+ return ''
+ attr = [len(txt_strs.encode('utf8'))-1,0, tspan]
attrs.append(attr)
for node in tspan.childNodes:
if node.localName == None:
@@ -366,7 +383,7 @@
txt_strs = translate_tspan(node,coord_id, codefo, doc,txt_strs,attrs)
pass
pass
- attr[1] = len(txt_strs)-1
+ attr[1] = len(txt_strs.encode('utf8'))
return txt_strs
@@ -383,10 +400,10 @@
# FIXME: Implement all units here
if style_map['font-size'].endswith('px'):
font_sz = float(style_map['font-size'][:-2])
- print >> codefo, 'PANGO_SIZE(%d,%d,%d)' % (font_sz,start,end)
+ print >> codefo, 'PANGO_SIZE(%d,%d,%d)' % (font_sz*1024,start,end)
else:
font_sz = float(style_map['font-size'])
- print >> codefo, 'PANGO_SIZE(%d,%d,%d)' % (font_sz,start,end)
+ print >> codefo, 'PANGO_SIZE(%d,%d,%d)' % (font_sz*1024,start,end)
pass
if style_map.has_key('font-style'):
@@ -454,9 +471,12 @@
pass
def translate_text(text, coord_id, codefo, doc):
- map = translate_font_style(text, codefo)
+ try:
+ map = text.style_map
+ except:
+ map = translate_font_style(text, codefo)
+ text.style_map = map
attrs = []
- text.style_map = map
attr = [0,0, text]
attrs.append(attr)
@@ -465,10 +485,10 @@
if node.localName == None:
txt_strs = txt_strs + node.data
elif node.localName == 'tspan':
- txt_strs = translate_tspan(node,coord_id, codefo, doc,txt_strs,attrs)
+ txt_strs = translate_tspan(node,text,coord_id, codefo, doc,txt_strs,attrs)
pass
pass
- attr[1] = len(txt_strs)-1
+ attr[1] = len(txt_strs.encode('utf8'))
if txt_strs:
text_id = _get_id(text)
x = float(text.getAttribute('x'))