diff src/shape_text.c @ 279:86a5ae82ccf2 mbtext

* 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.
author wycc
date Sat, 31 Jan 2009 11:51:19 +0800
parents a90fd749af82
children c8b6ca46950b
line wrap: on
line diff
--- a/src/shape_text.c	Sat Jan 31 09:41:04 2009 +0800
+++ b/src/shape_text.c	Sat Jan 31 11:51:19 2009 +0800
@@ -35,6 +35,7 @@
     cairo_font_face_destroy(text->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);
 }