Mercurial > MadButterfly
annotate src/shape_text.c @ 148:fce696643b1e
-
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Thu, 25 Sep 2008 10:14:19 +0800 |
parents | 98c83441d7d6 |
children | b90abd31a281 |
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> |
6 #include "mb_types.h" | |
7 #include "shapes.h" | |
8 | |
104 | 9 #define ASSERT(x) |
27 | 10 #define OK 0 |
11 #define ERR -1 | |
12 | |
13 typedef struct _sh_text { | |
14 shape_t shape; | |
15 char *data; | |
16 co_aix x, y; | |
17 co_aix d_x, d_y; /* device x and y */ | |
18 co_aix font_size; | |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
19 co_aix d_font_size; |
27 | 20 cairo_font_face_t *face; |
21 cairo_scaled_font_t *scaled_font; | |
22 int flags; | |
23 } sh_text_t; | |
24 | |
25 #define TXF_SCALE_DIRTY 0x1 | |
26 | |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
27 static void sh_text_free(shape_t *shape) { |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
28 sh_text_t *text = (sh_text_t *)shape; |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
29 |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
30 if(text->scaled_font) |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
31 cairo_scaled_font_destroy(text->scaled_font); |
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
32 cairo_font_face_destroy(text->face); |
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 |
27 | 35 shape_t *sh_text_new(const char *txt, co_aix x, co_aix y, |
36 co_aix font_size, cairo_font_face_t *face) { | |
37 sh_text_t *text; | |
38 | |
39 text = (sh_text_t *)malloc(sizeof(sh_text_t)); | |
40 if(text == NULL) | |
41 return NULL; | |
42 | |
43 memset(text, 0, sizeof(sh_text_t)); | |
44 text->shape.sh_type = SHT_TEXT; | |
45 text->data = strdup(txt); | |
46 if(text->data == NULL) { | |
47 free(text); | |
48 return NULL; | |
49 } | |
50 text->x = x; | |
51 text->y = y; | |
52 text->font_size = font_size; | |
53 cairo_font_face_reference(face); | |
54 text->face = face; | |
55 text->flags |= TXF_SCALE_DIRTY; | |
56 | |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
57 text->shape.free = sh_text_free; |
27 | 58 |
73
9ab15ebc9061
Observer for mouse events
Thinker K.F. Li <thinker@branda.to>
parents:
58
diff
changeset
|
59 return (shape_t *)text; |
27 | 60 } |
61 | |
88
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
62 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
|
63 sh_text_t *text = (sh_text_t *)shape; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
64 char *buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
65 |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
66 buf = strdup(txt); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
67 if(text->data) free(text->data); |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
68 text->data = buf; |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
69 } |
dd813dcc232c
New example, calculator.
Thinker K.F. Li <thinker@branda.to>
parents:
73
diff
changeset
|
70 |
27 | 71 static int get_extents(sh_text_t *text, cairo_text_extents_t *extents) { |
72 cairo_matrix_t fmatrix; | |
73 cairo_matrix_t ctm; | |
74 cairo_scaled_font_t *new_scaled; | |
75 cairo_font_options_t *fopt; | |
76 | |
77 if((text->flags & TXF_SCALE_DIRTY) || | |
78 text->scaled_font == NULL) { | |
79 fopt = cairo_font_options_create(); | |
80 if(fopt == NULL) | |
81 return ERR; | |
82 memset(&fmatrix, 0, sizeof(cairo_matrix_t)); | |
31
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
83 fmatrix.xx = text->d_font_size; |
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
84 fmatrix.yy = text->d_font_size; |
27 | 85 memset(&ctm, 0, sizeof(cairo_matrix_t)); |
86 ctm.xx = 1; | |
87 ctm.yy = 1; | |
88 new_scaled = cairo_scaled_font_create(text->face, | |
89 &fmatrix, | |
90 &ctm, | |
91 fopt); | |
92 cairo_font_options_destroy(fopt); | |
93 if(new_scaled == NULL) | |
94 return ERR; | |
95 | |
96 if(text->scaled_font) | |
97 cairo_scaled_font_destroy(text->scaled_font); | |
98 text->scaled_font = new_scaled; | |
99 text->flags &= ~TXF_SCALE_DIRTY; | |
100 } | |
101 | |
102 cairo_scaled_font_text_extents(text->scaled_font, | |
103 text->data, extents); | |
104 return OK; | |
105 } | |
106 | |
107 void sh_text_transform(shape_t *shape) { | |
108 sh_text_t *text; | |
109 co_aix x, y; | |
110 co_aix shw; | |
111 cairo_text_extents_t extents; | |
112 co_aix poses[2][2]; | |
113 int r; | |
114 | |
115 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
|
116 |
da770188a44d
resize font size for changige of coord.
Thinker K.F. Li <thinker@branda.to>
parents:
30
diff
changeset
|
117 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
|
118 |
27 | 119 x = text->x; |
120 y = text->y; | |
121 coord_trans_pos(shape->coord, &x, &y); | |
122 r = get_extents(text, &extents); | |
104 | 123 ASSERT(r == OK); |
27 | 124 |
125 text->d_x = x; | |
126 text->d_y = y; | |
127 shw = shape->stroke_width / 2; | |
128 /* FIXME: It is unreasonable that a font exceed it's bbox. | |
129 * We add 5 pixels in get right bbox. But, it is unreasonable. | |
130 */ | |
131 poses[0][0] = x + extents.x_bearing - 5 - shw; | |
132 poses[0][1] = y + extents.y_bearing - 5 - shw; | |
133 poses[1][0] = poses[0][0] + extents.width + 10 + shape->stroke_width; | |
134 poses[1][1] = poses[0][1] + extents.height + 10 + shape->stroke_width; | |
135 geo_from_positions(shape->geo, 2, poses); | |
104 | 136 /*! \todo Support ratation for shape_text. */ |
27 | 137 } |
138 | |
139 | |
140 static void draw_text(sh_text_t *text, cairo_t *cr) { | |
141 cairo_set_scaled_font(cr, text->scaled_font); | |
142 cairo_move_to(cr, text->d_x, text->d_y); | |
143 cairo_text_path(cr, text->data); | |
144 } | |
145 | |
146 | |
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
|
147 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
|
148 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
|
149 } |
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
|
150 |