Mercurial > MadButterfly
view src/shape_text.c @ 285:248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
author | wycc |
---|---|
date | Sat, 31 Jan 2009 16:52:28 +0800 |
parents | c8b6ca46950b |
children | 13ce87b6dbf5 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <cairo.h> #include <pango/pangocairo.h> #include "mb_types.h" #include "mb_shapes.h" #define ASSERT(x) #define OK 0 #define ERR -1 typedef struct _sh_text { shape_t shape; char *data; co_aix x, y; co_aix d_x, d_y; /* device x and y */ co_aix font_size; co_aix d_font_size; cairo_font_face_t *face; cairo_scaled_font_t *scaled_font; int flags; PangoLayout *layout; PangoAttrList *attrs; } sh_text_t; #define TXF_SCALE_DIRTY 0x1 static void sh_text_free(shape_t *shape) { sh_text_t *text = (sh_text_t *)shape; if(text->scaled_font) cairo_scaled_font_destroy(text->scaled_font); 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) { sh_text_t *text; text = (sh_text_t *)malloc(sizeof(sh_text_t)); if(text == NULL) return NULL; memset(text, 0, sizeof(sh_text_t)); mb_obj_init(text, MBO_TEXT); text->data = strdup(txt); if(text->data == NULL) { free(text); return NULL; } text->x = x; text->y = y; text->font_size = font_size; cairo_font_face_reference(face); text->face = face; text->flags |= TXF_SCALE_DIRTY; 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); return (shape_t *)text; } extern void sh_text_set_text(shape_t *shape, const char *txt) { sh_text_t *text = (sh_text_t *)shape; char *buf; buf = strdup(txt); if(text->data) free(text->data); text->data = buf; } 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; pango_layout_get_extents(text->layout, NULL, extents); pango_extents_to_pixels(extents,NULL); return OK; } void sh_text_transform(shape_t *shape) { sh_text_t *text; co_aix x, y; co_aix shw; PangoRectangle extents; co_aix poses[2][2]; int r; text = (sh_text_t *)shape; text->d_font_size = coord_trans_size(shape->coord, text->font_size); x = text->x; 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-5; text->d_y = y-PANGO_DESCENT(extents)+5; shw = shape->stroke_width / 2; /* 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 - shw; poses[0][1] = y + extents.y - shw; poses[1][0] = poses[0][0] + extents.width + shape->stroke_width; poses[1][1] = poses[0][1] + extents.height + shape->stroke_width; geo_from_positions(shape->geo, 2, poses); /*! \todo Support ratation for shape_text. */ } static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr) { PangoAttribute *attr; PangoAttrList *attrlist; PangoFontDescription *desc; if (text->layout) { g_object_unref(text->layout); } text->layout = pango_cairo_create_layout(cr); desc = pango_font_description_from_string("Sans Bold"); cairo_set_source_rgb (cr, 0, 0, 0); pango_layout_set_font_description (text->layout, desc); pango_layout_set_text(text->layout,text->data,strlen(text->data)); pango_layout_set_attributes(text->layout, text->attrs); pango_cairo_update_layout(cr,text->layout); } static void draw_text(sh_text_t *text, cairo_t *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); } void sh_text_draw(shape_t *shape, cairo_t *cr) { draw_text((sh_text_t *)shape, cr); }