Mercurial > MadButterfly
comparison 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 |
comparison
equal
deleted
inserted
replaced
278:a90fd749af82 | 279:86a5ae82ccf2 |
---|---|
33 if(text->scaled_font) | 33 if(text->scaled_font) |
34 cairo_scaled_font_destroy(text->scaled_font); | 34 cairo_scaled_font_destroy(text->scaled_font); |
35 cairo_font_face_destroy(text->face); | 35 cairo_font_face_destroy(text->face); |
36 } | 36 } |
37 | 37 |
38 static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr); | |
38 shape_t *rdman_shape_text_new(redraw_man_t *rdman, | 39 shape_t *rdman_shape_text_new(redraw_man_t *rdman, |
39 const char *txt, co_aix x, co_aix y, | 40 const char *txt, co_aix x, co_aix y, |
40 co_aix font_size, cairo_font_face_t *face,PangoAttrList *attrs) { | 41 co_aix font_size, cairo_font_face_t *face,PangoAttrList *attrs) { |
41 sh_text_t *text; | 42 sh_text_t *text; |
42 | 43 |
60 text->flags |= TXF_SCALE_DIRTY; | 61 text->flags |= TXF_SCALE_DIRTY; |
61 | 62 |
62 text->shape.free = sh_text_free; | 63 text->shape.free = sh_text_free; |
63 text->layout = NULL; | 64 text->layout = NULL; |
64 text->attrs = attrs; | 65 text->attrs = attrs; |
66 sh_text_P_generate_layout(text, rdman->cr); | |
65 | 67 |
66 rdman_shape_man(rdman, (shape_t *)text); | 68 rdman_shape_man(rdman, (shape_t *)text); |
67 | 69 |
68 return (shape_t *)text; | 70 return (shape_t *)text; |
69 } | 71 } |
75 buf = strdup(txt); | 77 buf = strdup(txt); |
76 if(text->data) free(text->data); | 78 if(text->data) free(text->data); |
77 text->data = buf; | 79 text->data = buf; |
78 } | 80 } |
79 | 81 |
80 static int get_extents(sh_text_t *text, cairo_text_extents_t *extents) { | 82 static int get_extents(sh_text_t *text, PangoRectangle *extents) { |
81 cairo_matrix_t fmatrix; | 83 cairo_matrix_t fmatrix; |
82 cairo_matrix_t ctm; | 84 cairo_matrix_t ctm; |
83 cairo_scaled_font_t *new_scaled; | 85 cairo_scaled_font_t *new_scaled; |
84 cairo_font_options_t *fopt; | 86 cairo_font_options_t *fopt; |
85 | 87 |
86 if((text->flags & TXF_SCALE_DIRTY) || | 88 pango_layout_get_extents(text->layout, NULL, extents); |
87 text->scaled_font == NULL) { | 89 pango_extents_to_pixels(extents,NULL); |
88 fopt = cairo_font_options_create(); | |
89 if(fopt == NULL) | |
90 return ERR; | |
91 memset(&fmatrix, 0, sizeof(cairo_matrix_t)); | |
92 fmatrix.xx = text->d_font_size; | |
93 fmatrix.yy = text->d_font_size; | |
94 memset(&ctm, 0, sizeof(cairo_matrix_t)); | |
95 ctm.xx = 1; | |
96 ctm.yy = 1; | |
97 new_scaled = cairo_scaled_font_create(text->face, | |
98 &fmatrix, | |
99 &ctm, | |
100 fopt); | |
101 cairo_font_options_destroy(fopt); | |
102 if(new_scaled == NULL) | |
103 return ERR; | |
104 | |
105 if(text->scaled_font) | |
106 cairo_scaled_font_destroy(text->scaled_font); | |
107 text->scaled_font = new_scaled; | |
108 text->flags &= ~TXF_SCALE_DIRTY; | |
109 } | |
110 | |
111 cairo_scaled_font_text_extents(text->scaled_font, | |
112 text->data, extents); | |
113 return OK; | 90 return OK; |
114 } | 91 } |
115 | 92 |
116 void sh_text_transform(shape_t *shape) { | 93 void sh_text_transform(shape_t *shape) { |
117 sh_text_t *text; | 94 sh_text_t *text; |
118 co_aix x, y; | 95 co_aix x, y; |
119 co_aix shw; | 96 co_aix shw; |
120 cairo_text_extents_t extents; | 97 PangoRectangle extents; |
121 co_aix poses[2][2]; | 98 co_aix poses[2][2]; |
122 int r; | 99 int r; |
123 | 100 |
124 text = (sh_text_t *)shape; | 101 text = (sh_text_t *)shape; |
125 | 102 |
127 | 104 |
128 x = text->x; | 105 x = text->x; |
129 y = text->y; | 106 y = text->y; |
130 coord_trans_pos(shape->coord, &x, &y); | 107 coord_trans_pos(shape->coord, &x, &y); |
131 r = get_extents(text, &extents); | 108 r = get_extents(text, &extents); |
109 | |
110 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); | |
132 ASSERT(r == OK); | 111 ASSERT(r == OK); |
133 | 112 |
134 text->d_x = x; | 113 text->d_x = x; |
135 text->d_y = y-text->font_size; | 114 text->d_y = y-text->font_size; |
136 shw = shape->stroke_width / 2; | 115 shw = shape->stroke_width / 2; |
137 /* FIXME: It is unreasonable that a font exceed it's bbox. | 116 /* FIXME: It is unreasonable that a font exceed it's bbox. |
138 * We add 5 pixels in get right bbox. But, it is unreasonable. | 117 * We add 5 pixels in get right bbox. But, it is unreasonable. |
139 */ | 118 */ |
140 poses[0][0] = x + extents.x_bearing - 5 - shw; | 119 |
141 poses[0][1] = y + extents.y_bearing - 5 - shw; | 120 poses[0][0] = x + extents.x - 5 - shw; |
121 poses[0][1] = y + extents.y - 5 - shw; | |
142 poses[1][0] = poses[0][0] + extents.width + 10 + shape->stroke_width; | 122 poses[1][0] = poses[0][0] + extents.width + 10 + shape->stroke_width; |
143 poses[1][1] = poses[0][1] + extents.height + 10 + shape->stroke_width; | 123 poses[1][1] = poses[0][1] + extents.height + 10 + shape->stroke_width; |
144 geo_from_positions(shape->geo, 2, poses); | 124 geo_from_positions(shape->geo, 2, poses); |
145 /*! \todo Support ratation for shape_text. */ | 125 /*! \todo Support ratation for shape_text. */ |
146 } | 126 } |
156 } | 136 } |
157 text->layout = pango_cairo_create_layout(cr); | 137 text->layout = pango_cairo_create_layout(cr); |
158 desc = pango_font_description_from_string("Sans Bold"); | 138 desc = pango_font_description_from_string("Sans Bold"); |
159 pango_layout_set_font_description (text->layout, desc); | 139 pango_layout_set_font_description (text->layout, desc); |
160 pango_cairo_update_layout(cr,text->layout); | 140 pango_cairo_update_layout(cr,text->layout); |
161 pango_layout_set_text(text->layout,text->data,-1); | 141 pango_layout_set_text(text->layout,text->data,strlen(text->data)); |
162 pango_layout_set_attributes(text->layout, text->attrs); | 142 pango_layout_set_attributes(text->layout, text->attrs); |
163 | |
164 } | 143 } |
165 static void draw_text(sh_text_t *text, cairo_t *cr) { | 144 static void draw_text(sh_text_t *text, cairo_t *cr) { |
166 if (text->layout==NULL) { | 145 sh_text_P_generate_layout(text, cr); |
167 sh_text_P_generate_layout(text,cr); | |
168 } | |
169 cairo_move_to(cr, text->d_x, text->d_y); | 146 cairo_move_to(cr, text->d_x, text->d_y); |
170 pango_cairo_show_layout(cr,text->layout); | 147 pango_cairo_show_layout(cr,text->layout); |
171 } | 148 } |
172 | 149 |
173 | 150 |