Code

714538d1b455cc9fdfd1b48fefc3b991e9d54c98
[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 <libnr/nr-path.h>
30 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
32 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
33 static void sp_canvas_bpath_init (SPCanvasBPath *path);
34 static void sp_canvas_bpath_destroy (GtkObject *object);
36 static void sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
37 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
38 static double sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
40 static SPCanvasItemClass *parent_class;
42 GtkType
43 sp_canvas_bpath_get_type (void)
44 {
45     static GtkType type = 0;
46     if (!type) {
47         GtkTypeInfo info = {
48             (gchar *)"SPCanvasBPath",
49             sizeof (SPCanvasBPath),
50             sizeof (SPCanvasBPathClass),
51             (GtkClassInitFunc) sp_canvas_bpath_class_init,
52             (GtkObjectInitFunc) sp_canvas_bpath_init,
53             NULL, NULL, NULL
54         };
55         type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
56     }
57     return type;
58 }
60 static void
61 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
62 {
63     GtkObjectClass *object_class;
64     SPCanvasItemClass *item_class;
66     object_class = GTK_OBJECT_CLASS (klass);
67     item_class = (SPCanvasItemClass *) klass;
69     parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
71     object_class->destroy = sp_canvas_bpath_destroy;
73     item_class->update = sp_canvas_bpath_update;
74     item_class->render = sp_canvas_bpath_render;
75     item_class->point = sp_canvas_bpath_point;
76 }
78 static void
79 sp_canvas_bpath_init (SPCanvasBPath * bpath)
80 {
81     bpath->fill_rgba = 0x00000000;
82     bpath->fill_rule = SP_WIND_RULE_EVENODD;
84     bpath->stroke_rgba = 0x00000000;
85     bpath->stroke_width = 1.0;
86     bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
87     bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
88     bpath->stroke_miterlimit = 11.0;
89 }
91 static void
92 sp_canvas_bpath_destroy (GtkObject *object)
93 {
94     SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
96     if (cbp->curve) {
97         cbp->curve = cbp->curve->unref();
98     }
100     if (GTK_OBJECT_CLASS (parent_class)->destroy)
101         (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
104 static void
105 sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
107     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
109     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
111     if (((SPCanvasItemClass *) parent_class)->update)
112         ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
114     sp_canvas_item_reset_bounds (item);
116     if (!cbp->curve) return;
118     cbp->affine = affine;
120     const_NRBPath bp;
121     bp.path = cbp->curve->get_bpath();
122     NRRect bbox;
123     nr_path_matrix_bbox_union(&bp, affine, &bbox);
125     item->x1 = (int)bbox.x0 - 1;
126     item->y1 = (int)bbox.y0 - 1;
127     item->x2 = (int)bbox.x1 + 1;
128     item->y2 = (int)bbox.y1 + 1;
130     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
133 static void
134 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
136     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
138     sp_canvas_prepare_buffer(buf);
140     NR::Rect area (NR::Point(buf->rect.x0, buf->rect.y0), NR::Point(buf->rect.x1, buf->rect.y1));
142     if ( !cbp->curve  || 
143          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
144          cbp->curve->get_length() <= 1)
145         return;
147     if (!buf->ct)
148         return;
150     bool dofill = ((cbp->fill_rgba & 0xff) != 0);
151     bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
153     cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
154     cairo_new_path(buf->ct);
156     if (!dofill)
157         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, true, 1);
158     else
159         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, false, 1);
161     if (dofill) {
162         // RGB / BGR
163         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));
164         cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
165                             : CAIRO_FILL_RULE_WINDING);
166         if (dostroke)
167             cairo_fill_preserve(buf->ct);
168         else 
169             cairo_fill(buf->ct);
170     }
172     if (dostroke) {
173         // RGB / BGR
174         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));
175         cairo_set_line_width(buf->ct, 1);
176         cairo_stroke(buf->ct);
177     }
180 static double
181 sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
183     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
185     const_NRBPath bp;
186     if ( !cbp->curve  || 
187          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
188          cbp->curve->get_length() <= 1)
189         return NR_HUGE;
191     bp.path = cbp->curve->get_bpath();
193     double width = 0.5;
194     NR::Rect viewbox = item->canvas->getViewbox();
195         viewbox.growBy (width);
196     double dist = NR_HUGE;
197     nr_path_matrix_point_bbox_wind_distance(&bp, cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
199     if (dist <= 1.0) {
200         *actual_item = item;
201     }
203     return dist;
206 SPCanvasItem *
207 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
209     g_return_val_if_fail (parent != NULL, NULL);
210     g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
212     SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
214     sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
216     return item;
219 void
220 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
222     g_return_if_fail (cbp != NULL);
223     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
225     if (cbp->curve) {
226         cbp->curve = cbp->curve->unref();
227     }
229     if (curve) {
230         cbp->curve = curve->ref();
231     }
233     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
236 void
237 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
239     g_return_if_fail (cbp != NULL);
240     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
242     cbp->fill_rgba = rgba;
243     cbp->fill_rule = rule;
245     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
248 void
249 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap)
251     g_return_if_fail (cbp != NULL);
252     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
254     cbp->stroke_rgba = rgba;
255     cbp->stroke_width = MAX (width, 0.1);
256     cbp->stroke_linejoin = join;
257     cbp->stroke_linecap = cap;
259     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
265 /*
266   Local Variables:
267   mode:c++
268   c-file-style:"stroustrup"
269   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
270   indent-tabs-mode:nil
271   fill-column:99
272   End:
273 */
274 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :