1 #define __SP_CANVASTEXT_C__
3 /*
4 * Canvas text
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Maximilian Albert <maximilian.albert@gmail.com>
9 *
10 * Copyright (C) 2000-2002 Lauris Kaplinski
11 * Copyright (C) 2008 Maximilian Albert
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "display-forward.h"
17 #include "sp-canvas-util.h"
18 #include "canvas-text.h"
19 #include "display/inkscape-cairo.h"
20 #include <sstream>
21 #include <string.h>
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <color.h>
28 #include <libnr/nr-matrix-fns.h>
29 #include <libnr/nr-pixops.h>
31 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
32 static void sp_canvastext_init (SPCanvasText *canvastext);
33 static void sp_canvastext_destroy (GtkObject *object);
35 static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
36 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
38 static SPCanvasItemClass *parent_class_ct;
40 GtkType
41 sp_canvastext_get_type (void)
42 {
43 static GtkType type = 0;
45 if (!type) {
46 GtkTypeInfo info = {
47 (gchar *)"SPCanvasText",
48 sizeof (SPCanvasText),
49 sizeof (SPCanvasTextClass),
50 (GtkClassInitFunc) sp_canvastext_class_init,
51 (GtkObjectInitFunc) sp_canvastext_init,
52 NULL, NULL, NULL
53 };
54 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
55 }
56 return type;
57 }
59 static void
60 sp_canvastext_class_init (SPCanvasTextClass *klass)
61 {
62 GtkObjectClass *object_class = (GtkObjectClass *) klass;
63 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
65 parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
67 object_class->destroy = sp_canvastext_destroy;
69 item_class->update = sp_canvastext_update;
70 item_class->render = sp_canvastext_render;
71 }
73 static void
74 sp_canvastext_init (SPCanvasText *canvastext)
75 {
76 canvastext->rgba = 0x0000ff7f;
77 canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
78 canvastext->affine = Geom::identity();
79 canvastext->fontsize = 10.0;
80 canvastext->item = NULL;
81 canvastext->text = NULL;
82 }
84 static void
85 sp_canvastext_destroy (GtkObject *object)
86 {
87 g_return_if_fail (object != NULL);
88 g_return_if_fail (SP_IS_CANVASTEXT (object));
90 SPCanvasText *canvastext = SP_CANVASTEXT (object);
92 canvastext->item=NULL;
94 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
95 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
96 }
98 // FIXME: remove this as soon as we know how to correctly determine the text extent
99 static const double arbitrary_factor = 0.7;
101 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
102 static double anchor_offset_x = 0;
103 static double anchor_offset_y = 0;
105 static void
106 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
107 {
108 SPCanvasText *cl = SP_CANVASTEXT (item);
110 if (!buf->ct)
111 return;
113 guint32 rgba = cl->rgba;
114 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
116 Geom::Point s = cl->s * cl->affine;
117 double offsetx = s[Geom::X] - buf->rect.x0;
118 double offsety = s[Geom::Y] - buf->rect.y0;
119 offsetx -= anchor_offset_x;
120 offsety += anchor_offset_y;
122 cairo_move_to(buf->ct, offsetx, offsety);
123 cairo_set_font_size(buf->ct, cl->fontsize);
124 cairo_show_text(buf->ct, cl->text);
125 cairo_stroke(buf->ct);
127 cairo_new_path(buf->ct);
128 }
130 static void
131 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
132 {
133 SPCanvasText *cl = SP_CANVASTEXT (item);
135 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
137 if (parent_class_ct->update)
138 (* parent_class_ct->update) (item, affine, flags);
140 sp_canvas_item_reset_bounds (item);
142 cl->affine = affine;
144 Geom::Point s = cl->s * affine;
146 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
147 // method but update() seems to be called before so we don't have the information available when we need it
148 /**
149 cairo_t tmp_buf;
150 cairo_text_extents_t bbox;
151 cairo_text_extents(&tmp_buf, cl->text, &bbox);
152 **/
153 item->x1 = s[Geom::X] + 0;
154 item->y1 = s[Geom::Y] - cl->fontsize;
155 item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
156 item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
158 // adjust update region according to anchor shift
159 // FIXME: use the correct text extent
160 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
161 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
162 item->x1 -= anchor_offset_x;
163 item->x2 -= anchor_offset_x;
164 item->y1 += anchor_offset_y;
165 item->y2 += anchor_offset_y;
167 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
168 }
170 SPCanvasItem *
171 sp_canvastext_new(SPCanvasGroup *parent, Geom::Point pos, gchar const *new_text)
172 {
173 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
175 SPCanvasText *ct = SP_CANVASTEXT(item);
177 ct->s = pos;
178 g_free(ct->text);
179 ct->text = g_strdup(new_text);
181 // TODO: anything else to do?
183 return item;
184 }
187 void
188 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
189 {
190 g_return_if_fail (ct != NULL);
191 g_return_if_fail (SP_IS_CANVASTEXT (ct));
193 if (rgba != ct->rgba) {
194 SPCanvasItem *item;
195 ct->rgba = rgba;
196 item = SP_CANVAS_ITEM (ct);
197 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
198 }
199 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
200 }
202 #define EPSILON 1e-6
203 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
205 void
206 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
207 {
208 g_return_if_fail (ct != NULL);
209 g_return_if_fail (SP_IS_CANVASTEXT (ct));
211 if (DIFFER (x0, ct->s[Geom::X]) || DIFFER (y0, ct->s[Geom::Y])) {
212 ct->s[Geom::X] = x0;
213 ct->s[Geom::Y] = y0;
214 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
215 }
216 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
217 }
219 void
220 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
221 {
222 sp_canvastext_set_coords(ct, start[0], start[1]);
223 }
225 void
226 sp_canvastext_set_text (SPCanvasText *ct, gchar const* new_text)
227 {
228 g_free (ct->text);
229 ct->text = g_strdup(new_text);
230 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
231 }
233 void
234 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
235 {
236 std::ostringstream number;
237 number << num;
238 sp_canvastext_set_text(ct, number.str().c_str());
239 }
241 void
242 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
243 {
244 ct->fontsize = size;
245 }
247 void
248 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
249 {
250 ct->anchor_x = anchor_x;
251 ct->anchor_y = anchor_y;
252 }
254 /*
255 Local Variables:
256 mode:c++
257 c-file-style:"stroustrup"
258 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
259 indent-tabs-mode:nil
260 fill-column:99
261 End:
262 */
263 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :