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-pixops.h>
30 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
31 static void sp_canvastext_init (SPCanvasText *canvastext);
32 static void sp_canvastext_destroy (GtkObject *object);
34 static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
35 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
37 static SPCanvasItemClass *parent_class_ct;
39 GtkType
40 sp_canvastext_get_type (void)
41 {
42 static GtkType type = 0;
44 if (!type) {
45 GtkTypeInfo info = {
46 (gchar *)"SPCanvasText",
47 sizeof (SPCanvasText),
48 sizeof (SPCanvasTextClass),
49 (GtkClassInitFunc) sp_canvastext_class_init,
50 (GtkObjectInitFunc) sp_canvastext_init,
51 NULL, NULL, NULL
52 };
53 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
54 }
55 return type;
56 }
58 static void
59 sp_canvastext_class_init (SPCanvasTextClass *klass)
60 {
61 GtkObjectClass *object_class = (GtkObjectClass *) klass;
62 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
64 parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
66 object_class->destroy = sp_canvastext_destroy;
68 item_class->update = sp_canvastext_update;
69 item_class->render = sp_canvastext_render;
70 }
72 static void
73 sp_canvastext_init (SPCanvasText *canvastext)
74 {
75 canvastext->rgba = 0x0000ff7f;
76 canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
77 canvastext->affine = Geom::identity();
78 canvastext->fontsize = 10.0;
79 canvastext->item = NULL;
80 canvastext->text = NULL;
81 }
83 static void
84 sp_canvastext_destroy (GtkObject *object)
85 {
86 g_return_if_fail (object != NULL);
87 g_return_if_fail (SP_IS_CANVASTEXT (object));
89 SPCanvasText *canvastext = SP_CANVASTEXT (object);
91 canvastext->item=NULL;
93 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
94 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
95 }
97 // FIXME: remove this as soon as we know how to correctly determine the text extent
98 static const double arbitrary_factor = 0.7;
100 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
101 static double anchor_offset_x = 0;
102 static double anchor_offset_y = 0;
104 static void
105 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
106 {
107 SPCanvasText *cl = SP_CANVASTEXT (item);
109 if (!buf->ct)
110 return;
112 guint32 rgba = cl->rgba;
113 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));
115 Geom::Point s = cl->s * cl->affine;
116 double offsetx = s[Geom::X] - buf->rect.x0;
117 double offsety = s[Geom::Y] - buf->rect.y0;
118 offsetx -= anchor_offset_x;
119 offsety += anchor_offset_y;
121 cairo_move_to(buf->ct, offsetx, offsety);
122 cairo_set_font_size(buf->ct, cl->fontsize);
123 cairo_show_text(buf->ct, cl->text);
124 cairo_stroke(buf->ct);
126 cairo_new_path(buf->ct);
127 }
129 static void
130 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
131 {
132 SPCanvasText *cl = SP_CANVASTEXT (item);
134 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
136 if (parent_class_ct->update)
137 (* parent_class_ct->update) (item, affine, flags);
139 sp_canvas_item_reset_bounds (item);
141 cl->affine = affine;
143 Geom::Point s = cl->s * affine;
145 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
146 // method but update() seems to be called before so we don't have the information available when we need it
147 /**
148 cairo_t tmp_buf;
149 cairo_text_extents_t bbox;
150 cairo_text_extents(&tmp_buf, cl->text, &bbox);
151 **/
152 item->x1 = s[Geom::X] + 0;
153 item->y1 = s[Geom::Y] - cl->fontsize;
154 item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
155 item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
157 // adjust update region according to anchor shift
158 // FIXME: use the correct text extent
159 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
160 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
161 item->x1 -= anchor_offset_x;
162 item->x2 -= anchor_offset_x;
163 item->y1 += anchor_offset_y;
164 item->y2 += anchor_offset_y;
166 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
167 }
169 SPCanvasItem *
170 sp_canvastext_new(SPCanvasGroup *parent, Geom::Point pos, gchar const *new_text)
171 {
172 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
174 SPCanvasText *ct = SP_CANVASTEXT(item);
176 ct->s = pos;
177 g_free(ct->text);
178 ct->text = g_strdup(new_text);
180 // TODO: anything else to do?
182 return item;
183 }
186 void
187 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
188 {
189 g_return_if_fail (ct != NULL);
190 g_return_if_fail (SP_IS_CANVASTEXT (ct));
192 if (rgba != ct->rgba) {
193 SPCanvasItem *item;
194 ct->rgba = rgba;
195 item = SP_CANVAS_ITEM (ct);
196 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
197 }
198 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
199 }
201 #define EPSILON 1e-6
202 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
204 void
205 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
206 {
207 g_return_if_fail (ct != NULL);
208 g_return_if_fail (SP_IS_CANVASTEXT (ct));
210 if (DIFFER (x0, ct->s[Geom::X]) || DIFFER (y0, ct->s[Geom::Y])) {
211 ct->s[Geom::X] = x0;
212 ct->s[Geom::Y] = y0;
213 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
214 }
215 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
216 }
218 void
219 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
220 {
221 sp_canvastext_set_coords(ct, start[0], start[1]);
222 }
224 void
225 sp_canvastext_set_text (SPCanvasText *ct, gchar const* new_text)
226 {
227 g_free (ct->text);
228 ct->text = g_strdup(new_text);
229 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
230 }
232 void
233 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
234 {
235 std::ostringstream number;
236 number << num;
237 sp_canvastext_set_text(ct, number.str().c_str());
238 }
240 void
241 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
242 {
243 ct->fontsize = size;
244 }
246 void
247 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
248 {
249 ct->anchor_x = anchor_x;
250 ct->anchor_y = anchor_y;
251 }
253 /*
254 Local Variables:
255 mode:c++
256 c-file-style:"stroustrup"
257 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
258 indent-tabs-mode:nil
259 fill-column:99
260 End:
261 */
262 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :