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>
22 #include "desktop.h"
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <color.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 = 0x33337fff;
77 canvastext->rgba_stroke = 0xffffffff;
78 canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
79 canvastext->affine = Geom::identity();
80 canvastext->fontsize = 10.0;
81 canvastext->item = NULL;
82 canvastext->desktop = NULL;
83 canvastext->text = NULL;
84 }
86 static void
87 sp_canvastext_destroy (GtkObject *object)
88 {
89 g_return_if_fail (object != NULL);
90 g_return_if_fail (SP_IS_CANVASTEXT (object));
92 SPCanvasText *canvastext = SP_CANVASTEXT (object);
94 g_free(canvastext->text);
95 canvastext->text = NULL;
96 canvastext->item = NULL;
98 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
99 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
100 }
102 // FIXME: remove this as soon as we know how to correctly determine the text extent
103 static const double arbitrary_factor = 0.8;
105 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
106 static double anchor_offset_x = 0;
107 static double anchor_offset_y = 0;
109 static void
110 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
111 {
112 SPCanvasText *cl = SP_CANVASTEXT (item);
114 if (!buf->ct)
115 return;
117 Geom::Point s = cl->s * cl->affine;
118 double offsetx = s[Geom::X] - buf->rect.x0;
119 double offsety = s[Geom::Y] - buf->rect.y0;
120 offsetx -= anchor_offset_x;
121 offsety += anchor_offset_y;
123 cairo_move_to(buf->ct, offsetx, offsety);
124 cairo_set_font_size(buf->ct, cl->fontsize);
125 cairo_text_path(buf->ct, cl->text);
127 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cl->rgba_stroke), SP_RGBA32_G_F(cl->rgba_stroke), SP_RGBA32_R_F(cl->rgba_stroke), SP_RGBA32_A_F(cl->rgba_stroke));
128 cairo_set_line_width (buf->ct, 2.0);
129 cairo_stroke_preserve(buf->ct);
130 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cl->rgba), SP_RGBA32_G_F(cl->rgba), SP_RGBA32_R_F(cl->rgba), SP_RGBA32_A_F(cl->rgba));
131 cairo_fill(buf->ct);
133 cairo_new_path(buf->ct);
134 }
136 static void
137 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
138 {
139 SPCanvasText *cl = SP_CANVASTEXT (item);
141 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
143 if (parent_class_ct->update)
144 (* parent_class_ct->update) (item, affine, flags);
146 sp_canvas_item_reset_bounds (item);
148 cl->affine = affine;
150 Geom::Point s = cl->s * affine;
152 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
153 // method but update() seems to be called before so we don't have the information available when we need it
154 /**
155 cairo_t tmp_buf;
156 cairo_text_extents_t bbox;
157 cairo_text_extents(&tmp_buf, cl->text, &bbox);
158 **/
159 item->x1 = s[Geom::X] + 0;
160 item->y1 = s[Geom::Y] - cl->fontsize;
161 item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
162 item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
164 // adjust update region according to anchor shift
165 // FIXME: use the correct text extent
166 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
167 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
168 item->x1 -= anchor_offset_x;
169 item->x2 -= anchor_offset_x;
170 item->y1 += anchor_offset_y;
171 item->y2 += anchor_offset_y;
173 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
174 }
176 SPCanvasItem *
177 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
178 {
179 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
181 SPCanvasText *ct = SP_CANVASTEXT(item);
183 ct->desktop = desktop;
185 ct->s = pos;
186 g_free(ct->text);
187 ct->text = g_strdup(new_text);
189 // TODO: anything else to do?
191 return item;
192 }
195 void
196 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba, guint32 rgba_stroke)
197 {
198 g_return_if_fail (ct != NULL);
199 g_return_if_fail (SP_IS_CANVASTEXT (ct));
201 if (rgba != ct->rgba || rgba_stroke != ct->rgba_stroke) {
202 ct->rgba = rgba;
203 ct->rgba_stroke = rgba_stroke;
204 SPCanvasItem *item = SP_CANVAS_ITEM (ct);
205 sp_canvas_item_request_update( item );
206 }
207 }
209 #define EPSILON 1e-6
210 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
212 void
213 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
214 {
215 sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
216 }
218 void
219 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
220 {
221 Geom::Point pos = ct->desktop->doc2dt(start);
223 g_return_if_fail (ct != NULL);
224 g_return_if_fail (SP_IS_CANVASTEXT (ct));
226 if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
227 ct->s[Geom::X] = pos[0];
228 ct->s[Geom::Y] = pos[1];
229 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
230 }
231 }
233 void
234 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
235 {
236 g_free (ct->text);
237 ct->text = g_strdup(new_text);
238 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
239 }
241 void
242 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
243 {
244 std::ostringstream number;
245 number << num;
246 sp_canvastext_set_text(ct, number.str().c_str());
247 }
249 void
250 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
251 {
252 ct->fontsize = size;
253 }
255 void
256 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
257 {
258 ct->anchor_x = anchor_x;
259 ct->anchor_y = anchor_y;
260 }
263 /*
264 Local Variables:
265 mode:c++
266 c-file-style:"stroustrup"
267 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
268 indent-tabs-mode:nil
269 fill-column:99
270 End:
271 */
272 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :