Code

Merge from fe-moved
[inkscape.git] / src / display / canvas-bpath.cpp
1 #define __SP_CANVAS_BPATH_C__
3 /*
4  * Simple bezier bpath CanvasItem for inkscape
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@ximian.com>
8  *
9  * Copyright (C) 2001 Lauris Kaplinski and Ximian, Inc.
10  *
11  * Released under GNU GPL
12  *
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "color.h"
19 #include "sp-canvas-util.h"
20 #include "inkscape-cairo.h"
21 #include "canvas-bpath.h"
22 #include "display/display-forward.h"
23 #include "display/curve.h"
24 #include "display/inkscape-cairo.h"
25 #include "libnr/nr-matrix-fns.h"
26 #include <libnr/nr-pixops.h>
27 #include <libnr/nr-convert2geom.h>
28 #include "helper/geom.h"
30 /*
31  * FIXME: The following #includes and some other content in this file (see below) should actually
32  * be in a separate file called display/canvas-text.cpp. It temporarily had to be moved here
33  * because of linker errors.
34  */
36 /**
37 #include "display-forward.h"
38 #include "sp-canvas-util.h"
39 #include "canvas-text.h"
40 #include "display/inkscape-cairo.h"
41 **/
42 #include <sstream>
43 #include <string.h>
44 #include <desktop.h>
46 /**
47 #ifdef HAVE_CONFIG_H
48 # include "config.h"
49 #endif
50 #include <color.h>
52 #include <libnr/nr-matrix-fns.h>
53 #include <libnr/nr-pixops.h>
54 **/
56 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
58 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
59 static void sp_canvas_bpath_init (SPCanvasBPath *path);
60 static void sp_canvas_bpath_destroy (GtkObject *object);
62 static void sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
63 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
64 static double sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
66 static SPCanvasItemClass *parent_class;
68 GtkType
69 sp_canvas_bpath_get_type (void)
70 {
71     static GtkType type = 0;
72     if (!type) {
73         GtkTypeInfo info = {
74             (gchar *)"SPCanvasBPath",
75             sizeof (SPCanvasBPath),
76             sizeof (SPCanvasBPathClass),
77             (GtkClassInitFunc) sp_canvas_bpath_class_init,
78             (GtkObjectInitFunc) sp_canvas_bpath_init,
79             NULL, NULL, NULL
80         };
81         type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
82     }
83     return type;
84 }
86 static void
87 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
88 {
89     GtkObjectClass *object_class;
90     SPCanvasItemClass *item_class;
92     object_class = GTK_OBJECT_CLASS (klass);
93     item_class = (SPCanvasItemClass *) klass;
95     parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
97     object_class->destroy = sp_canvas_bpath_destroy;
99     item_class->update = sp_canvas_bpath_update;
100     item_class->render = sp_canvas_bpath_render;
101     item_class->point = sp_canvas_bpath_point;
104 static void
105 sp_canvas_bpath_init (SPCanvasBPath * bpath)
107     bpath->fill_rgba = 0x00000000;
108     bpath->fill_rule = SP_WIND_RULE_EVENODD;
110     bpath->stroke_rgba = 0x00000000;
111     bpath->stroke_width = 1.0;
112     bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
113     bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
114     bpath->stroke_miterlimit = 11.0;
117 static void
118 sp_canvas_bpath_destroy (GtkObject *object)
120     SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
122     if (cbp->curve) {
123         cbp->curve = cbp->curve->unref();
124     }
126     if (GTK_OBJECT_CLASS (parent_class)->destroy)
127         (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
130 static void
131 sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
133     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
135     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
137     if (((SPCanvasItemClass *) parent_class)->update)
138         ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
140     sp_canvas_item_reset_bounds (item);
142     if (!cbp->curve) return;
144     cbp->affine = affine;
146     Geom::OptRect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
148     if (bbox) {
149         item->x1 = (int)bbox->min()[Geom::X] - 1;
150         item->y1 = (int)bbox->min()[Geom::Y] - 1;
151         item->x2 = (int)bbox->max()[Geom::X] + 1;
152         item->y2 = (int)bbox->max()[Geom::Y] + 1;
153     } else {
154         item->x1 = 0;
155         item->y1 = 0;
156         item->x2 = 0;
157         item->y2 = 0;
158     }
159     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
162 static void
163 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
165     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
167     sp_canvas_prepare_buffer(buf);
169     Geom::Rect area (Geom::Point(buf->rect.x0, buf->rect.y0), Geom::Point(buf->rect.x1, buf->rect.y1));
171     if ( !cbp->curve  || 
172          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
173          cbp->curve->get_segment_count() < 1)
174         return;
176     if (!buf->ct)
177         return;
179     bool dofill = ((cbp->fill_rgba & 0xff) != 0);
180     bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
182     cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
183     cairo_new_path(buf->ct);
185     if (!dofill)
186         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
187     else
188         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
190     if (dofill) {
191         // RGB / BGR
192         cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cbp->fill_rgba), SP_RGBA32_G_F(cbp->fill_rgba), SP_RGBA32_R_F(cbp->fill_rgba), SP_RGBA32_A_F(cbp->fill_rgba));
193         cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
194                             : CAIRO_FILL_RULE_WINDING);
195         if (dostroke)
196             cairo_fill_preserve(buf->ct);
197         else 
198             cairo_fill(buf->ct);
199     }
201     if (dostroke) {
202         // RGB / BGR
203         cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cbp->stroke_rgba), SP_RGBA32_G_F(cbp->stroke_rgba), SP_RGBA32_R_F(cbp->stroke_rgba), SP_RGBA32_A_F(cbp->stroke_rgba));
204         cairo_set_line_width(buf->ct, 1);
205         if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
206             cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
207         }
208         cairo_stroke(buf->ct);
209     }
212 static double
213 sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
215     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
217     if ( !cbp->curve  || 
218          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
219          cbp->curve->get_segment_count() < 1)
220         return NR_HUGE;
222     double width = 0.5;
223     Geom::Rect viewbox = item->canvas->getViewbox();
224     viewbox.expandBy (width);
225     double dist = NR_HUGE;
226     pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
228     if (dist <= 1.0) {
229         *actual_item = item;
230     }
232     return dist;
235 SPCanvasItem *
236 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
238     g_return_val_if_fail (parent != NULL, NULL);
239     g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
241     SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
243     sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
245     return item;
248 void
249 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
251     g_return_if_fail (cbp != NULL);
252     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
254     if (cbp->curve) {
255         cbp->curve = cbp->curve->unref();
256     }
258     if (curve) {
259         cbp->curve = curve->ref();
260     }
262     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
265 void
266 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
268     g_return_if_fail (cbp != NULL);
269     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
271     cbp->fill_rgba = rgba;
272     cbp->fill_rule = rule;
274     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
277 void
278 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
280     g_return_if_fail (cbp != NULL);
281     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
283     cbp->stroke_rgba = rgba;
284     cbp->stroke_width = MAX (width, 0.1);
285     cbp->stroke_linejoin = join;
286     cbp->stroke_linecap = cap;
287     cbp->dashes[0] = dash;
288     cbp->dashes[1] = gap;
290     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
295 /*
296  * FIXME: The following code should actually be in a separate file called
297  * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
298  */
300 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
301 static void sp_canvastext_init (SPCanvasText *canvastext);
302 static void sp_canvastext_destroy (GtkObject *object);
304 static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
305 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
307 static SPCanvasItemClass *parent_class_ct;
309 GtkType
310 sp_canvastext_get_type (void)
312     static GtkType type = 0;
314     if (!type) {
315         GtkTypeInfo info = {
316             (gchar *)"SPCanvasText",
317             sizeof (SPCanvasText),
318             sizeof (SPCanvasTextClass),
319             (GtkClassInitFunc) sp_canvastext_class_init,
320             (GtkObjectInitFunc) sp_canvastext_init,
321             NULL, NULL, NULL
322         };
323         type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
324     }
325     return type;
328 static void
329 sp_canvastext_class_init (SPCanvasTextClass *klass)
331     GtkObjectClass *object_class = (GtkObjectClass *) klass;
332     SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
334     parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
336     object_class->destroy = sp_canvastext_destroy;
338     item_class->update = sp_canvastext_update;
339     item_class->render = sp_canvastext_render;
342 static void
343 sp_canvastext_init (SPCanvasText *canvastext)
345     canvastext->rgba = 0x0000ff7f;
346     canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
347     canvastext->affine = Geom::identity();
348     canvastext->fontsize = 10.0;
349     canvastext->item = NULL;
350     canvastext->desktop = NULL;
351     canvastext->text = NULL;
354 static void
355 sp_canvastext_destroy (GtkObject *object)
357     g_return_if_fail (object != NULL);
358     g_return_if_fail (SP_IS_CANVASTEXT (object));
360     SPCanvasText *canvastext = SP_CANVASTEXT (object);
362     canvastext->item=NULL;
364     if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
365         (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
368 // FIXME: remove this as soon as we know how to correctly determine the text extent
369 static const double arbitrary_factor = 0.7;
371 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
372 static double anchor_offset_x = 0;
373 static double anchor_offset_y = 0;
375 static void
376 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
378     SPCanvasText *cl = SP_CANVASTEXT (item);
380     if (!buf->ct)
381         return;
383     guint32 rgba = cl->rgba;
384     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));
386     Geom::Point s = cl->s * cl->affine;
387     double offsetx = s[Geom::X] - buf->rect.x0;
388     double offsety = s[Geom::Y] - buf->rect.y0;
389     offsetx -= anchor_offset_x;
390     offsety += anchor_offset_y;
392     cairo_move_to(buf->ct, offsetx, offsety);
393     cairo_set_font_size(buf->ct, cl->fontsize);
394     cairo_show_text(buf->ct, cl->text);
395     cairo_stroke(buf->ct);
397     cairo_new_path(buf->ct);
400 static void
401 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
403     SPCanvasText *cl = SP_CANVASTEXT (item);
405     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
407     if (parent_class_ct->update)
408         (* parent_class_ct->update) (item, affine, flags);
410     sp_canvas_item_reset_bounds (item);
412     cl->affine = affine;
414     Geom::Point s = cl->s * affine;
416     // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
417     // method but update() seems to be called before so we don't have the information available when we need it
418     /**
419     cairo_t tmp_buf;
420     cairo_text_extents_t bbox;
421     cairo_text_extents(&tmp_buf, cl->text, &bbox);
422     **/
423     item->x1 = s[Geom::X] + 0;
424     item->y1 = s[Geom::Y] - cl->fontsize;
425     item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
426     item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
428     // adjust update region according to anchor shift
429     // FIXME: use the correct text extent
430     anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
431     anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
432     item->x1 -= anchor_offset_x;
433     item->x2 -= anchor_offset_x;
434     item->y1 += anchor_offset_y;
435     item->y2 += anchor_offset_y;
437     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
440 SPCanvasItem *
441 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
443     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
445     SPCanvasText *ct = SP_CANVASTEXT(item);
447     ct->desktop = desktop;
449     ct->s = pos;
450     g_free(ct->text);
451     ct->text = g_strdup(new_text);
453     // TODO: anything else to do?
455     return item;
459 void
460 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
462     g_return_if_fail (ct != NULL);
463     g_return_if_fail (SP_IS_CANVASTEXT (ct));
465     if (rgba != ct->rgba) {
466         SPCanvasItem *item;
467         ct->rgba = rgba;
468         item = SP_CANVAS_ITEM (ct);
469         sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
470     }
471     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
474 #define EPSILON 1e-6
475 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
477 void
478 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
480     sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
483 void
484 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
486     Geom::Point pos = ct->desktop->doc2dt(start);
488     g_return_if_fail (ct != NULL);
489     g_return_if_fail (SP_IS_CANVASTEXT (ct));
491     if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
492         ct->s[Geom::X] = pos[0];
493         ct->s[Geom::Y] = pos[1];
494         sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
495     }
496     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
499 void
500 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
502     g_free (ct->text);
503     ct->text = g_strdup(new_text);
504     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
507 void
508 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
510     std::ostringstream number;
511     number << num;
512     sp_canvastext_set_text(ct, number.str().c_str());
515 void
516 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
518     ct->fontsize = size;
521 void
522 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
524     ct->anchor_x = anchor_x;
525     ct->anchor_y = anchor_y;
528 /*
529   Local Variables:
530   mode:c++
531   c-file-style:"stroustrup"
532   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
533   indent-tabs-mode:nil
534   fill-column:99
535   End:
536 */
537 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :