Mercurial > MadButterfly
annotate src/shape_text.c @ 285:248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
author | wycc |
---|---|
date | Sat, 31 Jan 2009 16:52:28 +0800 |
parents | c8b6ca46950b |
children | 13ce87b6dbf5 |
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; |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
25 PangoAttrList *attrs; |
27 | 26 } sh_text_t; |
27 | |
28 #define TXF_SCALE_DIRTY 0x1 | |
29 | |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
30 static void sh_text_free(shape_t *shape) { |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
31 sh_text_t *text = (sh_text_t *)shape; |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
32 |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
33 if(text->scaled_font) |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
34 cairo_scaled_font_destroy(text->scaled_font); |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
35 cairo_font_face_destroy(text->face); |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
36 } |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
37 |
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
|
38 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
|
39 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
|
40 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
|
41 co_aix font_size, cairo_font_face_t *face,PangoAttrList *attrs) { |
27 | 42 sh_text_t *text; |
43 | |
44 text = (sh_text_t *)malloc(sizeof(sh_text_t)); | |
45 if(text == NULL) | |
46 return NULL; | |
47 | |
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
|
48 |
27 | 49 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
|
50 mb_obj_init(text, MBO_TEXT); |
27 | 51 text->data = strdup(txt); |
52 if(text->data == NULL) { | |
53 free(text); | |
54 return NULL; | |
55 } | |
56 text->x = x; | |
57 text->y = y; | |
58 text->font_size = font_size; | |
59 cairo_font_face_reference(face); | |
60 text->face = face; | |
61 text->flags |= TXF_SCALE_DIRTY; | |
62 | |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
63 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
|
64 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
|
65 text->attrs = attrs; |
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
|
66 sh_text_P_generate_layout(text, rdman->cr); |
27 | 67 |
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
|
68 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
|
69 |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
70 return (shape_t *)text; |
27 | 71 } |
72 | |
88
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
73 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
|
74 sh_text_t *text = (sh_text_t *)shape; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
75 char *buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
76 |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
77 buf = strdup(txt); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
78 if(text->data) free(text->data); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
79 text->data = buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
80 } |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
81 |
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
|
82 static int get_extents(sh_text_t *text, PangoRectangle *extents) { |
27 | 83 cairo_matrix_t fmatrix; |
84 cairo_matrix_t ctm; | |
85 cairo_scaled_font_t *new_scaled; | |
86 cairo_font_options_t *fopt; | |
87 | |
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
|
88 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
|
89 pango_extents_to_pixels(extents,NULL); |
27 | 90 return OK; |
91 } | |
92 | |
93 void sh_text_transform(shape_t *shape) { | |
94 sh_text_t *text; | |
95 co_aix x, y; | |
96 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
|
97 PangoRectangle extents; |
27 | 98 co_aix poses[2][2]; |
99 int r; | |
100 | |
101 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
|
102 |
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
103 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
|
104 |
27 | 105 x = text->x; |
106 y = text->y; | |
107 coord_trans_pos(shape->coord, &x, &y); | |
108 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
|
109 |
280 | 110 //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 | 111 ASSERT(r == OK); |
27 | 112 |
285
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
113 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
|
114 text->d_y = y-PANGO_DESCENT(extents)+5; |
27 | 115 shw = shape->stroke_width / 2; |
116 /* FIXME: It is unreasonable that a font exceed it's bbox. | |
117 * We add 5 pixels in get right bbox. But, it is unreasonable. | |
118 */ | |
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
|
119 |
285
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
120 poses[0][0] = x + extents.x - shw; |
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
121 poses[0][1] = y + extents.y - shw; |
248a40d51473
Check in test program for sh_text_set_text for debugging. It is not working yet.
wycc
parents:
280
diff
changeset
|
122 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
|
123 poses[1][1] = poses[0][1] + extents.height + shape->stroke_width; |
27 | 124 geo_from_positions(shape->geo, 2, poses); |
104 | 125 /*! \todo Support ratation for shape_text. */ |
27 | 126 } |
127 | |
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
|
128 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
|
129 { |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
130 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
|
131 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
|
132 PangoFontDescription *desc; |
27 | 133 |
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
|
134 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
|
135 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
|
136 } |
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
224
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 pango_cairo_update_layout(cr,text->layout); |
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
|
144 } |
27 | 145 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
|
146 sh_text_P_generate_layout(text, cr); |
27 | 147 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
|
148 pango_cairo_show_layout(cr,text->layout); |
27 | 149 } |
150 | |
151 | |
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
|
152 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
|
153 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
|
154 } |
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
|
155 |