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