Mercurial > MadButterfly
comparison src/shape_text.c @ 448:16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
mb_graph_engine is a layer to separate MadButterfly from Cairo.
It is only a set of macro mapping to cairo implementation, now.
But, it provides a oppotunities to replace cairo with other engines;
likes skia.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 04 Aug 2009 23:35:41 +0800 |
parents | 27774b93521e |
children | 61a0bceb369d |
comparison
equal
deleted
inserted
replaced
447:38aae921243f | 448:16116d84bc5e |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <string.h> | 3 #include <string.h> |
4 #include <math.h> | 4 #include <math.h> |
5 #include <cairo.h> | 5 #include "mb_graph_engine.h" |
6 #include <pango/pangocairo.h> | 6 #include <pango/pangocairo.h> |
7 #include "mb_types.h" | 7 #include "mb_types.h" |
8 #include "mb_shapes.h" | 8 #include "mb_shapes.h" |
9 | 9 |
10 #define ASSERT(x) | 10 #define ASSERT(x) |
16 char *data; | 16 char *data; |
17 co_aix x, y; | 17 co_aix x, y; |
18 co_aix d_x, d_y; /* device x and y */ | 18 co_aix d_x, d_y; /* device x and y */ |
19 co_aix font_size; | 19 co_aix font_size; |
20 co_aix d_font_size; | 20 co_aix d_font_size; |
21 cairo_font_face_t *face; | 21 mbe_font_face_t *face; |
22 cairo_scaled_font_t *scaled_font; | 22 mbe_scaled_font_t *scaled_font; |
23 int flags; | 23 int flags; |
24 PangoLayout *layout; | 24 PangoLayout *layout; |
25 int align; | 25 int align; |
26 PangoAttrList *attrs; | 26 PangoAttrList *attrs; |
27 } sh_text_t; | 27 } sh_text_t; |
30 | 30 |
31 static void sh_text_free(shape_t *shape) { | 31 static void sh_text_free(shape_t *shape) { |
32 sh_text_t *text = (sh_text_t *)shape; | 32 sh_text_t *text = (sh_text_t *)shape; |
33 | 33 |
34 if(text->scaled_font) | 34 if(text->scaled_font) |
35 cairo_scaled_font_destroy(text->scaled_font); | 35 mbe_scaled_font_destroy(text->scaled_font); |
36 cairo_font_face_destroy(text->face); | 36 mbe_font_face_destroy(text->face); |
37 } | 37 } |
38 | 38 |
39 static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr); | 39 static void sh_text_P_generate_layout(sh_text_t *text,mbe_t *cr); |
40 shape_t *rdman_shape_text_new(redraw_man_t *rdman, | 40 shape_t *rdman_shape_text_new(redraw_man_t *rdman, |
41 const char *txt, co_aix x, co_aix y, | 41 const char *txt, co_aix x, co_aix y, |
42 co_aix font_size, cairo_font_face_t *face,PangoAttrList *attrs) { | 42 co_aix font_size, mbe_font_face_t *face,PangoAttrList *attrs) { |
43 sh_text_t *text; | 43 sh_text_t *text; |
44 | 44 |
45 text = (sh_text_t *)malloc(sizeof(sh_text_t)); | 45 text = (sh_text_t *)malloc(sizeof(sh_text_t)); |
46 if(text == NULL) | 46 if(text == NULL) |
47 return NULL; | 47 return NULL; |
54 return NULL; | 54 return NULL; |
55 } | 55 } |
56 text->x = x; | 56 text->x = x; |
57 text->y = y; | 57 text->y = y; |
58 text->font_size = font_size; | 58 text->font_size = font_size; |
59 cairo_font_face_reference(face); | 59 mbe_font_face_reference(face); |
60 text->face = face; | 60 text->face = face; |
61 text->flags |= TXF_SCALE_DIRTY; | 61 text->flags |= TXF_SCALE_DIRTY; |
62 | 62 |
63 text->shape.free = sh_text_free; | 63 text->shape.free = sh_text_free; |
64 text->layout = NULL; | 64 text->layout = NULL; |
191 text->align = format->align; | 191 text->align = format->align; |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 static int get_extents(sh_text_t *text, PangoRectangle *extents) { | 195 static int get_extents(sh_text_t *text, PangoRectangle *extents) { |
196 cairo_matrix_t fmatrix; | 196 mbe_matrix_t fmatrix; |
197 cairo_matrix_t ctm; | 197 mbe_matrix_t ctm; |
198 cairo_scaled_font_t *new_scaled; | 198 mbe_scaled_font_t *new_scaled; |
199 cairo_font_options_t *fopt; | 199 mbe_font_options_t *fopt; |
200 | 200 |
201 pango_layout_get_extents(text->layout, NULL, extents); | 201 pango_layout_get_extents(text->layout, NULL, extents); |
202 pango_extents_to_pixels(extents,NULL); | 202 pango_extents_to_pixels(extents,NULL); |
203 return OK; | 203 return OK; |
204 } | 204 } |
217 | 217 |
218 text->d_font_size = coord_trans_size(shape->coord, text->font_size); | 218 text->d_font_size = coord_trans_size(shape->coord, text->font_size); |
219 | 219 |
220 coord = sh_get_coord(shape); | 220 coord = sh_get_coord(shape); |
221 canvas = _coord_get_canvas(coord); | 221 canvas = _coord_get_canvas(coord); |
222 sh_text_P_generate_layout(text, (cairo_t *)canvas); | 222 sh_text_P_generate_layout(text, (mbe_t *)canvas); |
223 | 223 |
224 x = text->x; | 224 x = text->x; |
225 y = text->y; | 225 y = text->y; |
226 coord_trans_pos(shape->coord, &x, &y); | 226 coord_trans_pos(shape->coord, &x, &y); |
227 r = get_extents(text, &extents); | 227 r = get_extents(text, &extents); |
242 poses[1][1] = poses[0][1] + extents.height + shape->stroke_width; | 242 poses[1][1] = poses[0][1] + extents.height + shape->stroke_width; |
243 geo_from_positions(shape->geo, 2, poses); | 243 geo_from_positions(shape->geo, 2, poses); |
244 /*! \todo Support ratation for shape_text. */ | 244 /*! \todo Support ratation for shape_text. */ |
245 } | 245 } |
246 | 246 |
247 static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr) | 247 static void sh_text_P_generate_layout(sh_text_t *text,mbe_t *cr) |
248 { | 248 { |
249 PangoAttribute *attr; | 249 PangoAttribute *attr; |
250 PangoAttrList *attrlist; | 250 PangoAttrList *attrlist; |
251 PangoFontDescription *desc; | 251 PangoFontDescription *desc; |
252 | 252 |
253 if (text->layout) { | 253 if (text->layout) { |
254 g_object_unref(text->layout); | 254 g_object_unref(text->layout); |
255 } | 255 } |
256 text->layout = pango_cairo_create_layout(cr); | 256 text->layout = pango_cairo_create_layout(cr); |
257 desc = pango_font_description_from_string("Sans Bold"); | 257 desc = pango_font_description_from_string("Sans Bold"); |
258 //cairo_set_source_rgb (cr, 0, 0, 0); | 258 //mbe_set_source_rgb (cr, 0, 0, 0); |
259 pango_layout_set_font_description (text->layout, desc); | 259 pango_layout_set_font_description (text->layout, desc); |
260 pango_layout_set_text(text->layout,text->data,strlen(text->data)); | 260 pango_layout_set_text(text->layout,text->data,strlen(text->data)); |
261 pango_layout_set_attributes(text->layout, text->attrs); | 261 pango_layout_set_attributes(text->layout, text->attrs); |
262 pango_cairo_update_layout(cr,text->layout); | 262 pango_cairo_update_layout(cr,text->layout); |
263 printf("text=%s\n",text->data); | 263 printf("text=%s\n",text->data); |
264 } | 264 } |
265 static void draw_text(sh_text_t *text, cairo_t *cr) { | 265 static void draw_text(sh_text_t *text, mbe_t *cr) { |
266 cairo_move_to(cr, text->d_x, text->d_y); | 266 mbe_move_to(cr, text->d_x, text->d_y); |
267 pango_cairo_layout_path(cr,text->layout); | 267 pango_cairo_layout_path(cr,text->layout); |
268 } | 268 } |
269 | 269 |
270 | 270 |
271 void sh_text_draw(shape_t *shape, cairo_t *cr) { | 271 void sh_text_draw(shape_t *shape, mbe_t *cr) { |
272 draw_text((sh_text_t *)shape, cr); | 272 draw_text((sh_text_t *)shape, cr); |
273 } | 273 } |
274 | 274 |