comparison src/shape_text.c @ 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 13ce87b6dbf5
children 7270e368ee98
comparison
equal deleted inserted replaced
290:f65c4589549f 291:137a73822d48
20 co_aix d_font_size; 20 co_aix d_font_size;
21 cairo_font_face_t *face; 21 cairo_font_face_t *face;
22 cairo_scaled_font_t *scaled_font; 22 cairo_scaled_font_t *scaled_font;
23 int flags; 23 int flags;
24 PangoLayout *layout; 24 PangoLayout *layout;
25 int align;
25 PangoAttrList *attrs; 26 PangoAttrList *attrs;
26 } sh_text_t; 27 } sh_text_t;
27 28
28 #define TXF_SCALE_DIRTY 0x1 29 #define TXF_SCALE_DIRTY 0x1
29 30
61 text->flags |= TXF_SCALE_DIRTY; 62 text->flags |= TXF_SCALE_DIRTY;
62 63
63 text->shape.free = sh_text_free; 64 text->shape.free = sh_text_free;
64 text->layout = NULL; 65 text->layout = NULL;
65 text->attrs = attrs; 66 text->attrs = attrs;
67 text->align = TEXTALIGN_START;
66 sh_text_P_generate_layout(text, rdman->cr); 68 sh_text_P_generate_layout(text, rdman->cr);
67 69
68 rdman_shape_man(rdman, (shape_t *)text); 70 rdman_shape_man(rdman, (shape_t *)text);
69 71
70 return (shape_t *)text; 72 return (shape_t *)text;
75 char *buf; 77 char *buf;
76 78
77 buf = strdup(txt); 79 buf = strdup(txt);
78 if(text->data) free(text->data); 80 if(text->data) free(text->data);
79 text->data = buf; 81 text->data = buf;
82 }
83
84 void sh_text_set_style(shape_t *shape,int begin,int end,mb_textstyle_t *format)
85 {
86 PangoAttribute *attr;
87 sh_text_t *text = (sh_text_t *)shape;
88
89 if (end == -1) {
90 end = strlen(text->data);
91 } else
92 end++;
93 if (format->property & TEXTSTYLE_BOLD) {
94 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
95 attr->start_index = begin;
96 attr->end_index = end;
97 pango_attr_list_change(text->attrs,attr);
98 }
99 if (format->property & TEXTSTYLE_ITALIC) {
100 attr = pango_attr_style_new(PANGO_STYLE_ITALIC);
101 attr->start_index = begin;
102 attr->end_index = end;
103 pango_attr_list_change(text->attrs,attr);
104 }
105 if (format->property & TEXTSTYLE_UNDERLINE) {
106 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
107 attr->start_index = begin;
108 attr->end_index = end;
109 pango_attr_list_change(text->attrs,attr);
110 }
111 if (format->property & TEXTSTYLE_COLOR) {
112 printf("color=%x\n", format->color);
113 printf("red = %x\n",TEXTCOLOR_RED(format->color));
114 printf("green = %x\n",TEXTCOLOR_GREEN(format->color));
115 printf("blue = %x\n",TEXTCOLOR_BLUE(format->color));
116 attr = pango_attr_foreground_new(TEXTCOLOR_RED(format->color)<<8,TEXTCOLOR_GREEN(format->color)<<8,TEXTCOLOR_BLUE(format->color)<<8);
117 attr->start_index = begin;
118 attr->end_index = end;
119 pango_attr_list_change(text->attrs,attr);
120 }
121 if (format->property & TEXTSTYLE_FONT) {
122 attr = pango_attr_family_new(format->font);
123 attr->start_index = begin;
124 attr->end_index = end;
125 pango_attr_list_change(text->attrs,attr);
126 }
127 if (format->property & TEXTSTYLE_ALIGN) {
128 // We can have one align style for the whole text only
129 if (begin != 0 || (end != strlen(text->data)-1))
130 return;
131 text->align = format->align;
132 }
80 } 133 }
81 134
82 static int get_extents(sh_text_t *text, PangoRectangle *extents) { 135 static int get_extents(sh_text_t *text, PangoRectangle *extents) {
83 cairo_matrix_t fmatrix; 136 cairo_matrix_t fmatrix;
84 cairo_matrix_t ctm; 137 cairo_matrix_t ctm;
139 cairo_set_source_rgb (cr, 0, 0, 0); 192 cairo_set_source_rgb (cr, 0, 0, 0);
140 pango_layout_set_font_description (text->layout, desc); 193 pango_layout_set_font_description (text->layout, desc);
141 pango_layout_set_text(text->layout,text->data,strlen(text->data)); 194 pango_layout_set_text(text->layout,text->data,strlen(text->data));
142 pango_layout_set_attributes(text->layout, text->attrs); 195 pango_layout_set_attributes(text->layout, text->attrs);
143 pango_cairo_update_layout(cr,text->layout); 196 pango_cairo_update_layout(cr,text->layout);
197 printf("text=%s\n",text->data);
144 } 198 }
145 static void draw_text(sh_text_t *text, cairo_t *cr) { 199 static void draw_text(sh_text_t *text, cairo_t *cr) {
146 sh_text_P_generate_layout(text, cr); 200 sh_text_P_generate_layout(text, cr);
147 cairo_move_to(cr, text->d_x, text->d_y); 201 cairo_move_to(cr, text->d_x, text->d_y);
148 pango_cairo_show_layout(cr,text->layout); 202 pango_cairo_show_layout(cr,text->layout);