Code

move canvastext to its own file (no linking error for me anymore), add outline to...
[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 "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     canvastext->item=NULL;
96     if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
97         (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
98 }
100 // FIXME: remove this as soon as we know how to correctly determine the text extent
101 static const double arbitrary_factor = 0.8;
103 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
104 static double anchor_offset_x = 0;
105 static double anchor_offset_y = 0;
107 static void
108 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
110     SPCanvasText *cl = SP_CANVASTEXT (item);
112     if (!buf->ct)
113         return;
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_text_path(buf->ct, cl->text);
125     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));
126     cairo_set_line_width (buf->ct, 2.0);
127     cairo_stroke_preserve(buf->ct);
128     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));
129     cairo_fill(buf->ct);
131     cairo_new_path(buf->ct);
134 static void
135 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
137     SPCanvasText *cl = SP_CANVASTEXT (item);
139     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
141     if (parent_class_ct->update)
142         (* parent_class_ct->update) (item, affine, flags);
144     sp_canvas_item_reset_bounds (item);
146     cl->affine = affine;
148     Geom::Point s = cl->s * affine;
150     // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
151     // method but update() seems to be called before so we don't have the information available when we need it
152     /**
153     cairo_t tmp_buf;
154     cairo_text_extents_t bbox;
155     cairo_text_extents(&tmp_buf, cl->text, &bbox);
156     **/
157     item->x1 = s[Geom::X] + 0;
158     item->y1 = s[Geom::Y] - cl->fontsize;
159     item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
160     item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
162     // adjust update region according to anchor shift
163     // FIXME: use the correct text extent
164     anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
165     anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
166     item->x1 -= anchor_offset_x;
167     item->x2 -= anchor_offset_x;
168     item->y1 += anchor_offset_y;
169     item->y2 += anchor_offset_y;
171     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
174 SPCanvasItem *
175 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
177     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
179     SPCanvasText *ct = SP_CANVASTEXT(item);
181     ct->desktop = desktop;
183     ct->s = pos;
184     g_free(ct->text);
185     ct->text = g_strdup(new_text);
187     // TODO: anything else to do?
189     return item;
193 void
194 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba, guint32 rgba_stroke)
196     g_return_if_fail (ct != NULL);
197     g_return_if_fail (SP_IS_CANVASTEXT (ct));
199     if (rgba != ct->rgba || rgba_stroke != ct->rgba_stroke) {
200         ct->rgba = rgba;
201         ct->rgba_stroke = rgba_stroke;
202         SPCanvasItem *item = SP_CANVAS_ITEM (ct);
203         sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
204     }
207 #define EPSILON 1e-6
208 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
210 void
211 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
213     sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
216 void
217 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
219     Geom::Point pos = ct->desktop->doc2dt(start);
221     g_return_if_fail (ct != NULL);
222     g_return_if_fail (SP_IS_CANVASTEXT (ct));
224     if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
225         ct->s[Geom::X] = pos[0];
226         ct->s[Geom::Y] = pos[1];
227         sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
228     }
231 void
232 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
234     g_free (ct->text);
235     ct->text = g_strdup(new_text);
236     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
239 void
240 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
242     std::ostringstream number;
243     number << num;
244     sp_canvastext_set_text(ct, number.str().c_str());
247 void
248 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
250     ct->fontsize = size;
253 void
254 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
256     ct->anchor_x = anchor_x;
257     ct->anchor_y = anchor_y;
261 /*
262   Local Variables:
263   mode:c++
264   c-file-style:"stroustrup"
265   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
266   indent-tabs-mode:nil
267   fill-column:99
268   End:
269 */
270 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :