Code

Next roud of NR ==> Geom conversion
[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::Rect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
148     item->x1 = (int)bbox.min()[Geom::X] - 1;
149     item->y1 = (int)bbox.min()[Geom::Y] - 1;
150     item->x2 = (int)bbox.max()[Geom::X] + 1;
151     item->y2 = (int)bbox.max()[Geom::Y] + 1;
153     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
156 static void
157 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
159     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
161     sp_canvas_prepare_buffer(buf);
163     NR::Rect area (NR::Point(buf->rect.x0, buf->rect.y0), NR::Point(buf->rect.x1, buf->rect.y1));
165     if ( !cbp->curve  || 
166          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
167          cbp->curve->get_segment_count() < 1)
168         return;
170     if (!buf->ct)
171         return;
173     bool dofill = ((cbp->fill_rgba & 0xff) != 0);
174     bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
176     cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
177     cairo_new_path(buf->ct);
179     if (!dofill)
180         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
181     else
182         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
184     if (dofill) {
185         // RGB / BGR
186         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));
187         cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
188                             : CAIRO_FILL_RULE_WINDING);
189         if (dostroke)
190             cairo_fill_preserve(buf->ct);
191         else 
192             cairo_fill(buf->ct);
193     }
195     if (dostroke) {
196         // RGB / BGR
197         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));
198         cairo_set_line_width(buf->ct, 1);
199         if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
200             cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
201         }
202         cairo_stroke(buf->ct);
203     }
206 static double
207 sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
209     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
211     if ( !cbp->curve  || 
212          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
213          cbp->curve->get_segment_count() < 1)
214         return NR_HUGE;
216     double width = 0.5;
217     Geom::Rect viewbox = item->canvas->getViewbox();
218     viewbox.expandBy (width);
219     double dist = NR_HUGE;
220     pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
222     if (dist <= 1.0) {
223         *actual_item = item;
224     }
226     return dist;
229 SPCanvasItem *
230 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
232     g_return_val_if_fail (parent != NULL, NULL);
233     g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
235     SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
237     sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
239     return item;
242 void
243 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
245     g_return_if_fail (cbp != NULL);
246     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
248     if (cbp->curve) {
249         cbp->curve = cbp->curve->unref();
250     }
252     if (curve) {
253         cbp->curve = curve->ref();
254     }
256     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
259 void
260 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
262     g_return_if_fail (cbp != NULL);
263     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
265     cbp->fill_rgba = rgba;
266     cbp->fill_rule = rule;
268     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
271 void
272 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
274     g_return_if_fail (cbp != NULL);
275     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
277     cbp->stroke_rgba = rgba;
278     cbp->stroke_width = MAX (width, 0.1);
279     cbp->stroke_linejoin = join;
280     cbp->stroke_linecap = cap;
281     cbp->dashes[0] = dash;
282     cbp->dashes[1] = gap;
284     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
289 /*
290  * FIXME: The following code should actually be in a separate file called
291  * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
292  */
294 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
295 static void sp_canvastext_init (SPCanvasText *canvastext);
296 static void sp_canvastext_destroy (GtkObject *object);
298 static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
299 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
301 static SPCanvasItemClass *parent_class_ct;
303 GtkType
304 sp_canvastext_get_type (void)
306     static GtkType type = 0;
308     if (!type) {
309         GtkTypeInfo info = {
310             (gchar *)"SPCanvasText",
311             sizeof (SPCanvasText),
312             sizeof (SPCanvasTextClass),
313             (GtkClassInitFunc) sp_canvastext_class_init,
314             (GtkObjectInitFunc) sp_canvastext_init,
315             NULL, NULL, NULL
316         };
317         type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
318     }
319     return type;
322 static void
323 sp_canvastext_class_init (SPCanvasTextClass *klass)
325     GtkObjectClass *object_class = (GtkObjectClass *) klass;
326     SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
328     parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
330     object_class->destroy = sp_canvastext_destroy;
332     item_class->update = sp_canvastext_update;
333     item_class->render = sp_canvastext_render;
336 static void
337 sp_canvastext_init (SPCanvasText *canvastext)
339     canvastext->rgba = 0x0000ff7f;
340     canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
341     canvastext->affine = Geom::identity();
342     canvastext->fontsize = 10.0;
343     canvastext->item = NULL;
344     canvastext->desktop = NULL;
345     canvastext->text = NULL;
348 static void
349 sp_canvastext_destroy (GtkObject *object)
351     g_return_if_fail (object != NULL);
352     g_return_if_fail (SP_IS_CANVASTEXT (object));
354     SPCanvasText *canvastext = SP_CANVASTEXT (object);
356     canvastext->item=NULL;
358     if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
359         (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
362 // FIXME: remove this as soon as we know how to correctly determine the text extent
363 static const double arbitrary_factor = 0.7;
365 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
366 static double anchor_offset_x = 0;
367 static double anchor_offset_y = 0;
369 static void
370 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
372     SPCanvasText *cl = SP_CANVASTEXT (item);
374     if (!buf->ct)
375         return;
377     guint32 rgba = cl->rgba;
378     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));
380     Geom::Point s = cl->s * cl->affine;
381     double offsetx = s[Geom::X] - buf->rect.x0;
382     double offsety = s[Geom::Y] - buf->rect.y0;
383     offsetx -= anchor_offset_x;
384     offsety += anchor_offset_y;
386     cairo_move_to(buf->ct, offsetx, offsety);
387     cairo_set_font_size(buf->ct, cl->fontsize);
388     cairo_show_text(buf->ct, cl->text);
389     cairo_stroke(buf->ct);
391     cairo_new_path(buf->ct);
394 static void
395 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
397     SPCanvasText *cl = SP_CANVASTEXT (item);
399     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
401     if (parent_class_ct->update)
402         (* parent_class_ct->update) (item, affine, flags);
404     sp_canvas_item_reset_bounds (item);
406     cl->affine = affine;
408     Geom::Point s = cl->s * affine;
410     // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
411     // method but update() seems to be called before so we don't have the information available when we need it
412     /**
413     cairo_t tmp_buf;
414     cairo_text_extents_t bbox;
415     cairo_text_extents(&tmp_buf, cl->text, &bbox);
416     **/
417     item->x1 = s[Geom::X] + 0;
418     item->y1 = s[Geom::Y] - cl->fontsize;
419     item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
420     item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
422     // adjust update region according to anchor shift
423     // FIXME: use the correct text extent
424     anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
425     anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
426     item->x1 -= anchor_offset_x;
427     item->x2 -= anchor_offset_x;
428     item->y1 += anchor_offset_y;
429     item->y2 += anchor_offset_y;
431     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
434 SPCanvasItem *
435 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
437     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
439     SPCanvasText *ct = SP_CANVASTEXT(item);
441     ct->desktop = desktop;
443     ct->s = pos;
444     g_free(ct->text);
445     ct->text = g_strdup(new_text);
447     // TODO: anything else to do?
449     return item;
453 void
454 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
456     g_return_if_fail (ct != NULL);
457     g_return_if_fail (SP_IS_CANVASTEXT (ct));
459     if (rgba != ct->rgba) {
460         SPCanvasItem *item;
461         ct->rgba = rgba;
462         item = SP_CANVAS_ITEM (ct);
463         sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
464     }
465     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
468 #define EPSILON 1e-6
469 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
471 void
472 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
474     sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
477 void
478 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
480     Geom::Point pos = ct->desktop->doc2dt(start);
482     g_return_if_fail (ct != NULL);
483     g_return_if_fail (SP_IS_CANVASTEXT (ct));
485     if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
486         ct->s[Geom::X] = pos[0];
487         ct->s[Geom::Y] = pos[1];
488         sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
489     }
490     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
493 void
494 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
496     g_free (ct->text);
497     ct->text = g_strdup(new_text);
498     sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
501 void
502 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
504     std::ostringstream number;
505     number << num;
506     sp_canvastext_set_text(ct, number.str().c_str());
509 void
510 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
512     ct->fontsize = size;
515 void
516 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
518     ct->anchor_x = anchor_x;
519     ct->anchor_y = anchor_y;
522 /*
523   Local Variables:
524   mode:c++
525   c-file-style:"stroustrup"
526   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
527   indent-tabs-mode:nil
528   fill-column:99
529   End:
530 */
531 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :