Mercurial > MadButterfly
changeset 291:137a73822d48
Add sh_text_set_style support to change the style of text element.
author | wycc |
---|---|
date | Sun, 01 Feb 2009 01:45:53 +0800 |
parents | f65c4589549f |
children | 7270e368ee98 |
files | examples/dynamic/text.c include/mb_shapes.h src/shape_text.c |
diffstat | 3 files changed, 80 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/dynamic/text.c Sat Jan 31 19:45:50 2009 +0800 +++ b/examples/dynamic/text.c Sun Feb 01 01:45:53 2009 +0800 @@ -27,6 +27,9 @@ MyAppData *en = MBAPP_DATA((MBApp *)arg,MyAppData ); mb_timeval_t timer,interval; shape_t *text = (shape_t *) MB_SPRITE_GET_OBJ(myApp->rootsprite,"mytext"); + mb_textstyle_t style; + + mb_textstyle_init(&style); get_now(&timer); @@ -38,8 +41,12 @@ printf("xxx\n"); if (en->currentscene == 0) { sh_text_set_text(text,"This is 0"); + mb_textstyle_set_color(&style, TEXTCOLOR_RGB(255,0,0)); + sh_text_set_style(text,0,5,&style); } else { sh_text_set_text(text,"This is 1"); + mb_textstyle_set_color(&style, TEXTCOLOR_RGB(0,255,0)); + sh_text_set_style(text,0,5,&style); } rdman_shape_changed(MBAPP_RDMAN(myApp), text); #if 0
--- a/include/mb_shapes.h Sat Jan 31 19:45:50 2009 +0800 +++ b/include/mb_shapes.h Sun Feb 01 01:45:53 2009 +0800 @@ -78,6 +78,11 @@ #define TEXTSTYLE_FONT 0x10 #define TEXTSTYLE_ALIGN 0x20 + +#define TEXTALIGN_START 1 +#define TEXTALIGN_MIDDLE 2 +#define TEXTALIGN_END 3 + typedef struct { int property; unsigned int color; @@ -103,7 +108,11 @@ cairo_surface_t *surface; } mb_text_t; -extern void mb_textstyle_init(mb_textstyle_t *style); +extern void sh_text_set_style(shape_t *shape,int begin,int end,mb_textstyle_t *format); +static inline void mb_textstyle_init(mb_textstyle_t *style) +{ + style->property = 0; +} extern void mb_textstyle_set_font(mb_textstyle_t *style, char *font); static inline char *mb_textstyle_get_font(mb_textstyle_t *style) { @@ -127,7 +136,15 @@ { return style->property & TEXTSTYLE_UNDERLINE; } -extern void mb_textstyle_set_color(mb_textstyle_t *style, unsigned int color); +#define TEXTCOLOR_RED(c) (((c)&0xff0000)>>16) +#define TEXTCOLOR_GREEN(c) (((c)&0xff00)>>8) +#define TEXTCOLOR_BLUE(c) (((c)&0xff)) +#define TEXTCOLOR_RGB(r,g,b) (((r)<<16)|((g)<<8)|(b)) +static inline void mb_textstyle_set_color(mb_textstyle_t *style, unsigned int color) +{ + style->property |= TEXTSTYLE_COLOR; + style->color = color; +} static inline unsigned int mb_textstyle_get_color(mb_textstyle_t *style) { if (style->property & TEXTSTYLE_COLOR)
--- a/src/shape_text.c Sat Jan 31 19:45:50 2009 +0800 +++ b/src/shape_text.c Sun Feb 01 01:45:53 2009 +0800 @@ -22,6 +22,7 @@ cairo_scaled_font_t *scaled_font; int flags; PangoLayout *layout; + int align; PangoAttrList *attrs; } sh_text_t; @@ -63,6 +64,7 @@ text->shape.free = sh_text_free; text->layout = NULL; text->attrs = attrs; + text->align = TEXTALIGN_START; sh_text_P_generate_layout(text, rdman->cr); rdman_shape_man(rdman, (shape_t *)text); @@ -79,6 +81,57 @@ text->data = buf; } +void sh_text_set_style(shape_t *shape,int begin,int end,mb_textstyle_t *format) +{ + PangoAttribute *attr; + sh_text_t *text = (sh_text_t *)shape; + + if (end == -1) { + end = strlen(text->data); + } else + end++; + if (format->property & TEXTSTYLE_BOLD) { + attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD); + attr->start_index = begin; + attr->end_index = end; + pango_attr_list_change(text->attrs,attr); + } + if (format->property & TEXTSTYLE_ITALIC) { + attr = pango_attr_style_new(PANGO_STYLE_ITALIC); + attr->start_index = begin; + attr->end_index = end; + pango_attr_list_change(text->attrs,attr); + } + if (format->property & TEXTSTYLE_UNDERLINE) { + attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); + attr->start_index = begin; + attr->end_index = end; + pango_attr_list_change(text->attrs,attr); + } + if (format->property & TEXTSTYLE_COLOR) { + printf("color=%x\n", format->color); + printf("red = %x\n",TEXTCOLOR_RED(format->color)); + printf("green = %x\n",TEXTCOLOR_GREEN(format->color)); + printf("blue = %x\n",TEXTCOLOR_BLUE(format->color)); + attr = pango_attr_foreground_new(TEXTCOLOR_RED(format->color)<<8,TEXTCOLOR_GREEN(format->color)<<8,TEXTCOLOR_BLUE(format->color)<<8); + attr->start_index = begin; + attr->end_index = end; + pango_attr_list_change(text->attrs,attr); + } + if (format->property & TEXTSTYLE_FONT) { + attr = pango_attr_family_new(format->font); + attr->start_index = begin; + attr->end_index = end; + pango_attr_list_change(text->attrs,attr); + } + if (format->property & TEXTSTYLE_ALIGN) { + // We can have one align style for the whole text only + if (begin != 0 || (end != strlen(text->data)-1)) + return; + text->align = format->align; + } +} + static int get_extents(sh_text_t *text, PangoRectangle *extents) { cairo_matrix_t fmatrix; cairo_matrix_t ctm; @@ -141,6 +194,7 @@ 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); + printf("text=%s\n",text->data); } static void draw_text(sh_text_t *text, cairo_t *cr) { sh_text_P_generate_layout(text, cr);