Mercurial > MadButterfly
annotate src/shape_text.c @ 1332:a0d3a4f8e99d
Siwtch components and timelines with context menu
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 31 Jan 2011 16:42:15 +0800 |
parents | a8d20bc8ce40 |
children |
rev | line source |
---|---|
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
473
diff
changeset
|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
473
diff
changeset
|
2 // vim: sw=4:ts=8:sts=4 |
27 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
5 #include <string.h> | |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
6 #include <math.h> |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
7 #include "mb_graph_engine.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
|
8 #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
|
9 #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
|
10 #include "mb_shapes.h" |
27 | 11 |
104 | 12 #define ASSERT(x) |
27 | 13 #define OK 0 |
14 #define ERR -1 | |
15 | |
16 typedef struct _sh_text { | |
17 shape_t shape; | |
18 char *data; | |
461
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
19 co_aix x, y,w,h; |
27 | 20 co_aix d_x, d_y; /* device x and y */ |
21 co_aix font_size; | |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
22 co_aix d_font_size; |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
23 mbe_font_face_t *face; |
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
24 mbe_scaled_font_t *scaled_font; |
27 | 25 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
|
26 PangoLayout *layout; |
291
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
27 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
|
28 PangoAttrList *attrs; |
27 | 29 } sh_text_t; |
30 | |
31 #define TXF_SCALE_DIRTY 0x1 | |
32 | |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
33 static void sh_text_free(shape_t *shape) { |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
34 sh_text_t *text = (sh_text_t *)shape; |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
35 |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
36 if(text->scaled_font) |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
37 mbe_scaled_font_destroy(text->scaled_font); |
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
38 mbe_font_face_destroy(text->face); |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
39 } |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
40 |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
41 static void sh_text_P_generate_layout(sh_text_t *text,mbe_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
|
42 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
|
43 const char *txt, co_aix x, co_aix y, |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
44 co_aix font_size, mbe_font_face_t *face,PangoAttrList *attrs) { |
27 | 45 sh_text_t *text; |
46 | |
47 text = (sh_text_t *)malloc(sizeof(sh_text_t)); | |
48 if(text == NULL) | |
49 return NULL; | |
50 | |
51 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
|
52 mb_obj_init(text, MBO_TEXT); |
27 | 53 text->data = strdup(txt); |
54 if(text->data == NULL) { | |
55 free(text); | |
56 return NULL; | |
57 } | |
58 text->x = x; | |
59 text->y = y; | |
60 text->font_size = font_size; | |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
61 mbe_font_face_reference(face); |
27 | 62 text->face = face; |
63 text->flags |= TXF_SCALE_DIRTY; | |
64 | |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
65 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
|
66 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
|
67 text->attrs = attrs; |
291
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
68 text->align = TEXTALIGN_START; |
461
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
69 text->w = text->h = 0; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
473
diff
changeset
|
70 |
1062
a8d20bc8ce40
Rename rdman_shape_man() to rdman_man_shape() to mean managing a shape
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
71 rdman_man_shape(rdman, (shape_t *)text); |
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
|
72 |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
73 return (shape_t *)text; |
27 | 74 } |
75 | |
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
|
76 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
|
77 { |
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 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
|
79 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
|
80 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
|
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 |
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 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
|
85 { |
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 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
|
87 *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
|
88 *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
|
89 } |
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 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
|
92 { |
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 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
|
94 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
|
95 } |
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
|
96 |
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
|
97 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
|
98 sh_text_t *text = (sh_text_t *)shape; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
99 char *buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
100 |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
101 buf = strdup(txt); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
102 if(text->data) free(text->data); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
103 text->data = buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
104 } |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
105 |
292 | 106 void sh_text_set_color(shape_t *shape, unsigned int color) |
107 { | |
294 | 108 PangoAttribute *attr = pango_attr_foreground_new(TEXTCOLOR_RED(color)<<8,TEXTCOLOR_GREEN(color)<<8,TEXTCOLOR_BLUE(color)<<8); |
292 | 109 sh_text_t *text = (sh_text_t *)shape; |
294 | 110 attr->start_index = 0; |
292 | 111 attr->end_index = -1; |
112 pango_attr_list_change(text->attrs, attr); | |
113 } | |
114 void sh_text_set_bold(shape_t *shape,int bold) | |
115 { | |
116 PangoAttribute *attr = pango_attr_weight_new(bold? PANGO_WEIGHT_BOLD:PANGO_WEIGHT_NORMAL); | |
117 sh_text_t *text = (sh_text_t *)shape; | |
294 | 118 attr->start_index = 0; |
292 | 119 attr->end_index = -1; |
120 pango_attr_list_change(text->attrs, attr); | |
121 } | |
122 void sh_text_set_italic(shape_t *shape,int italic) | |
123 { | |
124 PangoAttribute *attr = pango_attr_style_new(italic? PANGO_STYLE_ITALIC:PANGO_STYLE_NORMAL); | |
125 sh_text_t *text = (sh_text_t *)shape; | |
294 | 126 attr->start_index = 0; |
292 | 127 attr->end_index = -1; |
128 pango_attr_list_change(text->attrs, attr); | |
129 } | |
130 void sh_text_set_underline(shape_t *shape,int underline) | |
131 { | |
132 PangoAttribute *attr = pango_attr_underline_new(underline? PANGO_UNDERLINE_SINGLE:PANGO_UNDERLINE_NONE); | |
133 sh_text_t *text = (sh_text_t *)shape; | |
294 | 134 attr->start_index = 0; |
292 | 135 attr->end_index = -1; |
136 pango_attr_list_change(text->attrs, attr); | |
137 } | |
138 void sh_text_set_font(shape_t *shape,char *family) | |
139 { | |
140 PangoAttribute *attr = pango_attr_family_new(family); | |
141 sh_text_t *text = (sh_text_t *)shape; | |
294 | 142 attr->start_index = 0; |
292 | 143 attr->end_index = -1; |
144 pango_attr_list_change(text->attrs, attr); | |
145 } | |
146 | |
461
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
147 void sh_text_set_align(shape_t *shape,int align) |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
148 { |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
149 sh_text_t *text = (sh_text_t *)shape; |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
150 text->align = align; |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
151 } |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
152 void sh_text_set_width(shape_t *shape,int width) |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
153 { |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
154 sh_text_t *text = (sh_text_t *)shape; |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
155 text->w = width; |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
156 } |
291
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
157 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
|
158 { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
159 PangoAttribute *attr; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
160 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
|
161 |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
162 if (end == -1) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
163 end = strlen(text->data); |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
164 } else |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
165 end++; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
166 if (format->property & TEXTSTYLE_BOLD) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
167 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
|
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_ITALIC) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
173 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
|
174 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
175 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
176 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
|
177 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
178 if (format->property & TEXTSTYLE_UNDERLINE) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
179 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
|
180 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
181 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
182 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
|
183 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
184 if (format->property & TEXTSTYLE_COLOR) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
191 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
192 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
|
193 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
194 if (format->property & TEXTSTYLE_FONT) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
195 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
|
196 attr->start_index = begin; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
197 attr->end_index = end; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
198 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
|
199 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
200 if (format->property & TEXTSTYLE_ALIGN) { |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
201 // 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
|
202 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
|
203 return; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
204 text->align = format->align; |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
205 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
206 } |
137a73822d48
Add sh_text_set_style support to change the style of text element.
wycc
parents:
289
diff
changeset
|
207 |
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
|
208 static int get_extents(sh_text_t *text, PangoRectangle *extents) { |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
209 mbe_scaled_font_t *new_scaled; |
27 | 210 |
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
|
211 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
|
212 pango_extents_to_pixels(extents,NULL); |
27 | 213 return OK; |
214 } | |
215 | |
216 void sh_text_transform(shape_t *shape) { | |
217 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
|
218 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
|
219 canvas_t *canvas; |
27 | 220 co_aix x, y; |
221 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
|
222 PangoRectangle extents; |
27 | 223 co_aix poses[2][2]; |
224 int r; | |
225 | |
226 text = (sh_text_t *)shape; | |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
473
diff
changeset
|
227 |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
228 text->d_font_size = coord_trans_size(shape->coord, text->font_size); |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
473
diff
changeset
|
229 |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
330
diff
changeset
|
230 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
|
231 canvas = _coord_get_canvas(coord); |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
232 sh_text_P_generate_layout(text, (mbe_t *)canvas); |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
233 |
27 | 234 x = text->x; |
235 y = text->y; | |
236 coord_trans_pos(shape->coord, &x, &y); | |
237 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
|
238 |
280 | 239 //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 | 240 ASSERT(r == OK); |
27 | 241 |
285
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
242 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
|
243 text->d_y = y-PANGO_DESCENT(extents)+5; |
27 | 244 shw = shape->stroke_width / 2; |
245 /* FIXME: It is unreasonable that a font exceed it's bbox. | |
246 * We add 5 pixels in get right bbox. But, it is unreasonable. | |
247 */ | |
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
|
248 |
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
|
249 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
|
250 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
|
251 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
|
252 poses[1][1] = poses[0][1] + extents.height + shape->stroke_width; |
27 | 253 geo_from_positions(shape->geo, 2, poses); |
104 | 254 /*! \todo Support ratation for shape_text. */ |
27 | 255 } |
256 | |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
257 static void sh_text_P_generate_layout(sh_text_t *text,mbe_t *cr) |
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
|
258 { |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
259 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
|
260 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
|
261 PangoFontDescription *desc; |
27 | 262 |
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
|
263 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
|
264 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
|
265 } |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
266 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
|
267 desc = pango_font_description_from_string("Sans Bold"); |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
268 //mbe_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
|
269 pango_layout_set_font_description (text->layout, desc); |
461
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
270 if (text->w != 0) |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
271 pango_layout_set_width(text->layout, text->w); |
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
|
272 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
|
273 pango_layout_set_attributes(text->layout, text->attrs); |
461
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
274 if (text->align == TEXTALIGN_START) |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
275 pango_layout_set_alignment(text->layout, PANGO_ALIGN_LEFT); |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
276 else if (text->align == TEXTALIGN_MIDDLE) |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
277 pango_layout_set_alignment(text->layout, PANGO_ALIGN_CENTER); |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
278 else if (text->align == TEXTALIGN_END) |
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
279 pango_layout_set_alignment(text->layout, PANGO_ALIGN_RIGHT); |
285
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
280 pango_cairo_update_layout(cr,text->layout); |
461
61a0bceb369d
Add alignment support for the text layout
wycc@122-116-38-188.HINET-IP.hinet.net
parents:
448
diff
changeset
|
281 //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
|
282 } |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
283 static void draw_text(sh_text_t *text, mbe_t *cr) { |
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
284 mbe_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
|
285 pango_cairo_layout_path(cr,text->layout); |
27 | 286 } |
287 | |
288 | |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
393
diff
changeset
|
289 void sh_text_draw(shape_t *shape, mbe_t *cr) { |
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
|
290 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
|
291 } |
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
|
292 |