annotate src/shape_text.c @ 294:2ca0773cd48d

* Add MBAF files * Change begin_index to be start_index * fix the color setting.
author wycc
date Sun, 01 Feb 2009 09:51:12 +0800
parents 7270e368ee98
children f894b30676e9
rev   line source
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <stdlib.h>
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <string.h>
31
da770188a44d resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents: 30
diff changeset
4 #include <math.h>
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <cairo.h>
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
6 #include <pango/pangocairo.h>
186
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
7 #include "mb_types.h"
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
8 #include "mb_shapes.h"
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9
104
Thinker K.F. Li <thinker@branda.to>
parents: 88
diff changeset
10 #define ASSERT(x)
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #define OK 0
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 #define ERR -1
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 typedef struct _sh_text {
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 shape_t shape;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 char *data;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 co_aix x, y;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 co_aix d_x, d_y; /* device x and y */
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 co_aix font_size;
31
da770188a44d resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents: 30
diff changeset
20 co_aix d_font_size;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 cairo_font_face_t *face;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 cairo_scaled_font_t *scaled_font;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 int flags;
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
24 PangoLayout *layout;
291
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
25 int align;
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
26 PangoAttrList *attrs;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 } sh_text_t;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 #define TXF_SCALE_DIRTY 0x1
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
31 static void sh_text_free(shape_t *shape) {
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
32 sh_text_t *text = (sh_text_t *)shape;
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
33
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
34 if(text->scaled_font)
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
35 cairo_scaled_font_destroy(text->scaled_font);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
36 cairo_font_face_destroy(text->face);
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
37 }
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
38
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
39 static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr);
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 104
diff changeset
40 shape_t *rdman_shape_text_new(redraw_man_t *rdman,
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 104
diff changeset
41 const char *txt, co_aix x, co_aix y,
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
42 co_aix font_size, cairo_font_face_t *face,PangoAttrList *attrs) {
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 sh_text_t *text;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 text = (sh_text_t *)malloc(sizeof(sh_text_t));
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 if(text == NULL)
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 return NULL;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
49
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 memset(text, 0, sizeof(sh_text_t));
224
29e1b2bffe4c X backend only sent EVT_MOUSE_MOVE_RAW to MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents: 196
diff changeset
51 mb_obj_init(text, MBO_TEXT);
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 text->data = strdup(txt);
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 if(text->data == NULL) {
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 free(text);
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 return NULL;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 }
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 text->x = x;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 text->y = y;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 text->font_size = font_size;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 cairo_font_face_reference(face);
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 text->face = face;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 text->flags |= TXF_SCALE_DIRTY;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
64 text->shape.free = sh_text_free;
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
65 text->layout = NULL;
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
66 text->attrs = attrs;
291
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
67 text->align = TEXTALIGN_START;
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
68 sh_text_P_generate_layout(text, rdman->cr);
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 104
diff changeset
70 rdman_shape_man(rdman, (shape_t *)text);
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 104
diff changeset
71
73
9ab15ebc9061 Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
72 return (shape_t *)text;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 }
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74
88
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
75 extern void sh_text_set_text(shape_t *shape, const char *txt) {
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
76 sh_text_t *text = (sh_text_t *)shape;
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
77 char *buf;
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
78
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
79 buf = strdup(txt);
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
80 if(text->data) free(text->data);
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
81 text->data = buf;
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
82 }
dd813dcc232c New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents: 73
diff changeset
83
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
84 void sh_text_set_color(shape_t *shape, unsigned int color)
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
85 {
294
2ca0773cd48d * Add MBAF files
wycc
parents: 292
diff changeset
86 PangoAttribute *attr = pango_attr_foreground_new(TEXTCOLOR_RED(color)<<8,TEXTCOLOR_GREEN(color)<<8,TEXTCOLOR_BLUE(color)<<8);
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
87 sh_text_t *text = (sh_text_t *)shape;
294
2ca0773cd48d * Add MBAF files
wycc
parents: 292
diff changeset
88 attr->start_index = 0;
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
89 attr->end_index = -1;
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
90 pango_attr_list_change(text->attrs, attr);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
91 }
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
92 void sh_text_set_bold(shape_t *shape,int bold)
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
93 {
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
94 PangoAttribute *attr = pango_attr_weight_new(bold? PANGO_WEIGHT_BOLD:PANGO_WEIGHT_NORMAL);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
95 sh_text_t *text = (sh_text_t *)shape;
294
2ca0773cd48d * Add MBAF files
wycc
parents: 292
diff changeset
96 attr->start_index = 0;
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
97 attr->end_index = -1;
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
98 pango_attr_list_change(text->attrs, attr);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
99 }
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
100 void sh_text_set_italic(shape_t *shape,int italic)
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
101 {
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
102 PangoAttribute *attr = pango_attr_style_new(italic? PANGO_STYLE_ITALIC:PANGO_STYLE_NORMAL);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
103 sh_text_t *text = (sh_text_t *)shape;
294
2ca0773cd48d * Add MBAF files
wycc
parents: 292
diff changeset
104 attr->start_index = 0;
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
105 attr->end_index = -1;
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
106 pango_attr_list_change(text->attrs, attr);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
107 }
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
108 void sh_text_set_underline(shape_t *shape,int underline)
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
109 {
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
110 PangoAttribute *attr = pango_attr_underline_new(underline? PANGO_UNDERLINE_SINGLE:PANGO_UNDERLINE_NONE);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
111 sh_text_t *text = (sh_text_t *)shape;
294
2ca0773cd48d * Add MBAF files
wycc
parents: 292
diff changeset
112 attr->start_index = 0;
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
113 attr->end_index = -1;
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
114 pango_attr_list_change(text->attrs, attr);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
115 }
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
116 void sh_text_set_font(shape_t *shape,char *family)
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
117 {
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
118 PangoAttribute *attr = pango_attr_family_new(family);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
119 sh_text_t *text = (sh_text_t *)shape;
294
2ca0773cd48d * Add MBAF files
wycc
parents: 292
diff changeset
120 attr->start_index = 0;
292
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
121 attr->end_index = -1;
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
122 pango_attr_list_change(text->attrs, attr);
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
123 }
7270e368ee98 Add more text API
wycc
parents: 291
diff changeset
124
291
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
125 void sh_text_set_style(shape_t *shape,int begin,int end,mb_textstyle_t *format)
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
126 {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
127 PangoAttribute *attr;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
128 sh_text_t *text = (sh_text_t *)shape;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
129
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
130 if (end == -1) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
131 end = strlen(text->data);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
132 } else
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
133 end++;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
134 if (format->property & TEXTSTYLE_BOLD) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
135 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
136 attr->start_index = begin;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
137 attr->end_index = end;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
138 pango_attr_list_change(text->attrs,attr);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
139 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
140 if (format->property & TEXTSTYLE_ITALIC) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
141 attr = pango_attr_style_new(PANGO_STYLE_ITALIC);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
142 attr->start_index = begin;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
143 attr->end_index = end;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
144 pango_attr_list_change(text->attrs,attr);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
145 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
146 if (format->property & TEXTSTYLE_UNDERLINE) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
147 attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
148 attr->start_index = begin;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
149 attr->end_index = end;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
150 pango_attr_list_change(text->attrs,attr);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
151 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
152 if (format->property & TEXTSTYLE_COLOR) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
153 printf("color=%x\n", format->color);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
154 printf("red = %x\n",TEXTCOLOR_RED(format->color));
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
155 printf("green = %x\n",TEXTCOLOR_GREEN(format->color));
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
156 printf("blue = %x\n",TEXTCOLOR_BLUE(format->color));
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
157 attr = pango_attr_foreground_new(TEXTCOLOR_RED(format->color)<<8,TEXTCOLOR_GREEN(format->color)<<8,TEXTCOLOR_BLUE(format->color)<<8);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
158 attr->start_index = begin;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
159 attr->end_index = end;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
160 pango_attr_list_change(text->attrs,attr);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
161 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
162 if (format->property & TEXTSTYLE_FONT) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
163 attr = pango_attr_family_new(format->font);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
164 attr->start_index = begin;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
165 attr->end_index = end;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
166 pango_attr_list_change(text->attrs,attr);
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
167 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
168 if (format->property & TEXTSTYLE_ALIGN) {
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
169 // We can have one align style for the whole text only
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
170 if (begin != 0 || (end != strlen(text->data)-1))
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
171 return;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
172 text->align = format->align;
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
173 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
174 }
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
175
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
176 static int get_extents(sh_text_t *text, PangoRectangle *extents) {
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
177 cairo_matrix_t fmatrix;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
178 cairo_matrix_t ctm;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
179 cairo_scaled_font_t *new_scaled;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
180 cairo_font_options_t *fopt;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
181
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
182 pango_layout_get_extents(text->layout, NULL, extents);
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
183 pango_extents_to_pixels(extents,NULL);
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
184 return OK;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
185 }
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
186
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 void sh_text_transform(shape_t *shape) {
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
188 sh_text_t *text;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
189 co_aix x, y;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
190 co_aix shw;
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
191 PangoRectangle extents;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
192 co_aix poses[2][2];
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
193 int r;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
194
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
195 text = (sh_text_t *)shape;
31
da770188a44d resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents: 30
diff changeset
196
da770188a44d resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents: 30
diff changeset
197 text->d_font_size = coord_trans_size(shape->coord, text->font_size);
da770188a44d resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents: 30
diff changeset
198
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
199 x = text->x;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
200 y = text->y;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
201 coord_trans_pos(shape->coord, &x, &y);
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202 r = get_extents(text, &extents);
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
203
280
c8b6ca46950b Add merged result
wycc
parents: 279
diff changeset
204 //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);
104
Thinker K.F. Li <thinker@branda.to>
parents: 88
diff changeset
205 ASSERT(r == OK);
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206
285
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
207 text->d_x = x-5;
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
208 text->d_y = y-PANGO_DESCENT(extents)+5;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 shw = shape->stroke_width / 2;
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
210 /* FIXME: It is unreasonable that a font exceed it's bbox.
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 * We add 5 pixels in get right bbox. But, it is unreasonable.
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 */
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
213
289
13ce87b6dbf5 Fix bug of sh_text_t that do not update it-self after changes.
Thinker K.F. Li <thinker@branda.to>
parents: 285
diff changeset
214 poses[0][0] = text->d_x + extents.x - shw;
13ce87b6dbf5 Fix bug of sh_text_t that do not update it-self after changes.
Thinker K.F. Li <thinker@branda.to>
parents: 285
diff changeset
215 poses[0][1] = text->d_y + extents.y - shw;
285
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
216 poses[1][0] = poses[0][0] + extents.width + shape->stroke_width;
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
217 poses[1][1] = poses[0][1] + extents.height + shape->stroke_width;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
218 geo_from_positions(shape->geo, 2, poses);
104
Thinker K.F. Li <thinker@branda.to>
parents: 88
diff changeset
219 /*! \todo Support ratation for shape_text. */
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
220 }
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
221
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
222 static void sh_text_P_generate_layout(sh_text_t *text,cairo_t *cr)
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
223 {
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
224 PangoAttribute *attr;
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
225 PangoAttrList *attrlist;
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
226 PangoFontDescription *desc;
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
227
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
228 if (text->layout) {
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
229 g_object_unref(text->layout);
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
230 }
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
231 text->layout = pango_cairo_create_layout(cr);
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
232 desc = pango_font_description_from_string("Sans Bold");
285
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
233 cairo_set_source_rgb (cr, 0, 0, 0);
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
234 pango_layout_set_font_description (text->layout, desc);
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
235 pango_layout_set_text(text->layout,text->data,strlen(text->data));
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
236 pango_layout_set_attributes(text->layout, text->attrs);
285
248a40d51473 Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents: 280
diff changeset
237 pango_cairo_update_layout(cr,text->layout);
291
137a73822d48 Add sh_text_set_style support to change the style of text element.
wycc
parents: 289
diff changeset
238 printf("text=%s\n",text->data);
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
239 }
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
240 static void draw_text(sh_text_t *text, cairo_t *cr) {
279
86a5ae82ccf2 * Modify svg2code_ex to use differnt font size at two test items to illustrate the function of the new text parser
wycc
parents: 278
diff changeset
241 sh_text_P_generate_layout(text, cr);
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
242 cairo_move_to(cr, text->d_x, text->d_y);
278
a90fd749af82 Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents: 224
diff changeset
243 pango_cairo_show_layout(cr,text->layout);
27
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
244 }
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
245
19c603dd6ff9 Add text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
246
30
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents: 27
diff changeset
247 void sh_text_draw(shape_t *shape, cairo_t *cr) {
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents: 27
diff changeset
248 draw_text((sh_text_t *)shape, cr);
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents: 27
diff changeset
249 }
e06a4a667ce2 Accept mouse/pointer event and hint the shape that the pointer is over.
Thinker K.F. Li <thinker@branda.to>
parents: 27
diff changeset
250