Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / display / canvas-text.cpp
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 "sp-canvas-util.h"
17 #include "canvas-text.h"
18 #include "display/inkscape-cairo.h"
19 #include <sstream>
20 #include <string.h>
21 #include "desktop.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 = 0x33337fff;
76     canvastext->rgba_stroke = 0xffffffff;
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->desktop = NULL;
82     canvastext->text = NULL;
83 }
85 static void
86 sp_canvastext_destroy (GtkObject *object)
87 {
88     g_return_if_fail (object != NULL);
89     g_return_if_fail (SP_IS_CANVASTEXT (object));
91     SPCanvasText *canvastext = SP_CANVASTEXT (object);
93     g_free(canvastext->text);
94     canvastext->text = NULL;
95     canvastext->item = NULL;
97     if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
98         (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
99 }
101 // FIXME: remove this as soon as we know how to correctly determine the text extent
102 static const double arbitrary_factor = 0.8;
104 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
105 static double anchor_offset_x = 0;
106 static double anchor_offset_y = 0;
108 static void
109 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
111     SPCanvasText *cl = SP_CANVASTEXT (item);
113     if (!buf->ct)
114         return;
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_text_path(buf->ct, cl->text);
126     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));
127     cairo_set_line_width (buf->ct, 2.0);
128     cairo_stroke_preserve(buf->ct);
129     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));
130     cairo_fill(buf->ct);
132     cairo_new_path(buf->ct);
135 static void
136 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
138     SPCanvasText *cl = SP_CANVASTEXT (item);
140     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
142     if (parent_class_ct->update)
143         (* parent_class_ct->update) (item, affine, flags);
145     sp_canvas_item_reset_bounds (item);
147     cl->affine = affine;
149     Geom::Point s = cl->s * affine;
151     // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
152     // method but update() seems to be called before so we don't have the information available when we need it
153     /**
154     cairo_t tmp_buf;
155     cairo_text_extents_t bbox;
156     cairo_text_extents(&tmp_buf, cl->text, &bbox);
157     **/
158     item->x1 = s[Geom::X] + 0;
159     item->y1 = s[Geom::Y] - cl->fontsize;
160     item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
161     item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
163     // adjust update region according to anchor shift
164     // FIXME: use the correct text extent
165     anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
166     anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
167     item->x1 -= anchor_offset_x;
168     item->x2 -= anchor_offset_x;
169     item->y1 += anchor_offset_y;
170     item->y2 += anchor_offset_y;
172     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
175 SPCanvasItem *
176 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
178     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
180     SPCanvasText *ct = SP_CANVASTEXT(item);
182     ct->desktop = desktop;
184     ct->s = pos;
185     g_free(ct->text);
186     ct->text = g_strdup(new_text);
188     // TODO: anything else to do?
190     return item;
194 void
195 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba, guint32 rgba_stroke)
197     g_return_if_fail (ct != NULL);
198     g_return_if_fail (SP_IS_CANVASTEXT (ct));
200     if (rgba != ct->rgba || rgba_stroke != ct->rgba_stroke) {
201         ct->rgba = rgba;
202         ct->rgba_stroke = rgba_stroke;
203         SPCanvasItem *item = SP_CANVAS_ITEM (ct);
204         sp_canvas_item_request_update( item );
205     }
208 #define EPSILON 1e-6
209 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
211 void
212 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
214     sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
217 void
218 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
220     Geom::Point pos = ct->desktop->doc2dt(start);
222     g_return_if_fail (ct != NULL);
223     g_return_if_fail (SP_IS_CANVASTEXT (ct));
225     if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
226         ct->s[Geom::X] = pos[0];
227         ct->s[Geom::Y] = pos[1];
228         sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
229     }
232 void
233 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
235     g_free (ct->text);
236     ct->text = g_strdup(new_text);
237     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
240 void
241 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
243     std::ostringstream number;
244     number << num;
245     sp_canvastext_set_text(ct, number.str().c_str());
248 void
249 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
251     ct->fontsize = size;
254 void
255 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
257     ct->anchor_x = anchor_x;
258     ct->anchor_y = anchor_y;
262 /*
263   Local Variables:
264   mode:c++
265   c-file-style:"stroustrup"
266   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
267   indent-tabs-mode:nil
268   fill-column:99
269   End:
270 */
271 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :