annotate src/shape_text.c @ 27:19c603dd6ff9

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