Mercurial > MadButterfly
annotate src/shape_text.c @ 365:6adb28388132
Add capability to add new scene and source into the project
Add Save button to save the project.
author | wycc |
---|---|
date | Sat, 14 Mar 2009 16:42:37 +0800 |
parents | b391722bf20e |
children | 27774b93521e |
rev | line source |
---|---|
27 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
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 | 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 | 9 |
104 | 10 #define ASSERT(x) |
27 | 11 #define OK 0 |
12 #define ERR -1 | |
13 | |
14 typedef struct _sh_text { | |
15 shape_t shape; | |
16 char *data; | |
17 co_aix x, y; | |
18 co_aix d_x, d_y; /* device x and y */ | |
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 | 21 cairo_font_face_t *face; |
22 cairo_scaled_font_t *scaled_font; | |
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 | 27 } sh_text_t; |
28 | |
29 #define TXF_SCALE_DIRTY 0x1 | |
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 | 43 sh_text_t *text; |
44 | |
45 text = (sh_text_t *)malloc(sizeof(sh_text_t)); | |
46 if(text == NULL) | |
47 return NULL; | |
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 | 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 | 52 text->data = strdup(txt); |
53 if(text->data == NULL) { | |
54 free(text); | |
55 return NULL; | |
56 } | |
57 text->x = x; | |
58 text->y = y; | |
59 text->font_size = font_size; | |
60 cairo_font_face_reference(face); | |
61 text->face = face; | |
62 text->flags |= TXF_SCALE_DIRTY; | |
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; |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
68 |
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
|
69 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
|
70 |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
71 return (shape_t *)text; |
27 | 72 } |
73 | |
303
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
74 void sh_text_set_pos(shape_t *shape, co_aix x, co_aix y) |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
75 { |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
76 sh_text_t *text = (sh_text_t *)shape; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
77 text->x = x; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
78 text->y = y; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
79 } |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
80 |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
81 |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
82 void sh_text_get_pos(shape_t *shape, co_aix *x, co_aix *y) |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
83 { |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
84 sh_text_t *text = (sh_text_t *)shape; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
85 *x = text->x; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
86 *y = text->y; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
87 } |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
88 |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
89 void sh_text_get_text(shape_t *shape, char *text,int size) |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
90 { |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
91 sh_text_t *t = (sh_text_t *)shape; |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
92 strncpy(text,t->data, size); |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
93 } |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
94 |
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
95 void sh_text_set_text(shape_t *shape, const char *txt) { |
88
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
96 sh_text_t *text = (sh_text_t *)shape; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
97 char *buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
98 |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
99 buf = strdup(txt); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
100 if(text->data) free(text->data); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
101 text->data = buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
102 } |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
103 |
292 | 104 void sh_text_set_color(shape_t *shape, unsigned int color) |
105 { | |
294 | 106 PangoAttribute *attr = pango_attr_foreground_new(TEXTCOLOR_RED(color)<<8,TEXTCOLOR_GREEN(color)<<8,TEXTCOLOR_BLUE(color)<<8); |
292 | 107 sh_text_t *text = (sh_text_t *)shape; |
294 | 108 attr->start_index = 0; |
292 | 109 attr->end_index = -1; |
110 pango_attr_list_change(text->attrs, attr); | |
111 } | |
112 void sh_text_set_bold(shape_t *shape,int bold) | |
113 { | |
114 PangoAttribute *attr = pango_attr_weight_new(bold? PANGO_WEIGHT_BOLD:PANGO_WEIGHT_NORMAL); | |
115 sh_text_t *text = (sh_text_t *)shape; | |
294 | 116 attr->start_index = 0; |
292 | 117 attr->end_index = -1; |
118 pango_attr_list_change(text->attrs, attr); | |
119 } | |
120 void sh_text_set_italic(shape_t *shape,int italic) | |
121 { | |
122 PangoAttribute *attr = pango_attr_style_new(italic? PANGO_STYLE_ITALIC:PANGO_STYLE_NORMAL); | |
123 sh_text_t *text = (sh_text_t *)shape; | |
294 | 124 attr->start_index = 0; |
292 | 125 attr->end_index = -1; |
126 pango_attr_list_change(text->attrs, attr); | |
127 } | |
128 void sh_text_set_underline(shape_t *shape,int underline) | |
129 { | |
130 PangoAttribute *attr = pango_attr_underline_new(underline? PANGO_UNDERLINE_SINGLE:PANGO_UNDERLINE_NONE); | |
131 sh_text_t *text = (sh_text_t *)shape; | |
294 | 132 attr->start_index = 0; |
292 | 133 attr->end_index = -1; |
134 pango_attr_list_change(text->attrs, attr); | |
135 } | |
136 void sh_text_set_font(shape_t *shape,char *family) | |
137 { | |
138 PangoAttribute *attr = pango_attr_family_new(family); | |
139 sh_text_t *text = (sh_text_t *)shape; | |
294 | 140 attr->start_index = 0; |
292 | 141 attr->end_index = -1; |
142 pango_attr_list_change(text->attrs, attr); | |
143 } | |
144 | |
291
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
145 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
|
146 { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
147 PangoAttribute *attr; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
148 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
|
149 |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
150 if (end == -1) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
151 end = strlen(text->data); |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
152 } else |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
153 end++; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
154 if (format->property & TEXTSTYLE_BOLD) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
155 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
|
156 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
157 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
158 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
|
159 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
160 if (format->property & TEXTSTYLE_ITALIC) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
161 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
|
162 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
163 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
164 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
|
165 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
166 if (format->property & TEXTSTYLE_UNDERLINE) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
167 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
|
168 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
169 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
170 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
|
171 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
172 if (format->property & TEXTSTYLE_COLOR) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
179 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
180 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
|
181 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
182 if (format->property & TEXTSTYLE_FONT) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
183 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
|
184 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
185 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
186 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
|
187 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
188 if (format->property & TEXTSTYLE_ALIGN) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
189 // 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
|
190 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
|
191 return; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
192 text->align = format->align; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
193 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
194 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
195 |
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
|
196 static int get_extents(sh_text_t *text, PangoRectangle *extents) { |
27 | 197 cairo_matrix_t fmatrix; |
198 cairo_matrix_t ctm; | |
199 cairo_scaled_font_t *new_scaled; | |
200 cairo_font_options_t *fopt; | |
201 | |
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
|
202 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
|
203 pango_extents_to_pixels(extents,NULL); |
27 | 204 return OK; |
205 } | |
206 | |
207 void sh_text_transform(shape_t *shape) { | |
208 sh_text_t *text; | |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
209 coord_t *coord; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
210 canvas_t *canvas; |
27 | 211 co_aix x, y; |
212 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
|
213 PangoRectangle extents; |
27 | 214 co_aix poses[2][2]; |
215 int r; | |
216 | |
217 text = (sh_text_t *)shape; | |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
218 |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
219 text->d_font_size = coord_trans_size(shape->coord, text->font_size); |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
220 |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
221 coord = sh_get_coord(shape); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
222 canvas = _coord_get_canvas(coord); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
223 sh_text_P_generate_layout(text, (cairo_t *)canvas); |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
224 |
27 | 225 x = text->x; |
226 y = text->y; | |
227 coord_trans_pos(shape->coord, &x, &y); | |
228 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
|
229 |
280 | 230 //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 | 231 ASSERT(r == OK); |
27 | 232 |
285
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
233 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
|
234 text->d_y = y-PANGO_DESCENT(extents)+5; |
27 | 235 shw = shape->stroke_width / 2; |
236 /* FIXME: It is unreasonable that a font exceed it's bbox. | |
237 * We add 5 pixels in get right bbox. But, it is unreasonable. | |
238 */ | |
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
|
239 |
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
|
240 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
|
241 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
|
242 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
|
243 poses[1][1] = poses[0][1] + extents.height + shape->stroke_width; |
27 | 244 geo_from_positions(shape->geo, 2, poses); |
104 | 245 /*! \todo Support ratation for shape_text. */ |
27 | 246 } |
247 | |
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
|
248 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
|
249 { |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
250 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
|
251 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
|
252 PangoFontDescription *desc; |
27 | 253 |
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
|
254 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
|
255 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
|
256 } |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
257 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
|
258 desc = pango_font_description_from_string("Sans Bold"); |
303
f894b30676e9
Add MBAF object suport. This is still work in progress yet. However, it won't affect other features. Therefore, it is checked in before it become mature.
wycc
parents:
294
diff
changeset
|
259 //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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 } |
27 | 266 static void draw_text(sh_text_t *text, cairo_t *cr) { |
267 cairo_move_to(cr, text->d_x, text->d_y); | |
330
b0571f10c1b8
Use pango_cairo_layout_path() instead of pango_cairo_show_layout().
Thinker K.F. Li <thinker@branda.to>
parents:
303
diff
changeset
|
268 pango_cairo_layout_path(cr,text->layout); |
27 | 269 } |
270 | |
271 | |
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
|
272 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
|
273 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
|
274 } |
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
|
275 |