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