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-pixops.h>
26 #include "helper/geom.h"
28 #include <sstream>
29 #include <string.h>
30 #include <desktop.h>
32 /**
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36 #include <color.h>
38 #include <libnr/nr-pixops.h>
39 **/
41 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
43 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
44 static void sp_canvas_bpath_init (SPCanvasBPath *path);
45 static void sp_canvas_bpath_destroy (GtkObject *object);
47 static void sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
48 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
49 static double sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
51 static SPCanvasItemClass *parent_class;
53 GtkType
54 sp_canvas_bpath_get_type (void)
55 {
56 static GtkType type = 0;
57 if (!type) {
58 GtkTypeInfo info = {
59 (gchar *)"SPCanvasBPath",
60 sizeof (SPCanvasBPath),
61 sizeof (SPCanvasBPathClass),
62 (GtkClassInitFunc) sp_canvas_bpath_class_init,
63 (GtkObjectInitFunc) sp_canvas_bpath_init,
64 NULL, NULL, NULL
65 };
66 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
67 }
68 return type;
69 }
71 static void
72 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
73 {
74 GtkObjectClass *object_class;
75 SPCanvasItemClass *item_class;
77 object_class = GTK_OBJECT_CLASS (klass);
78 item_class = (SPCanvasItemClass *) klass;
80 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
82 object_class->destroy = sp_canvas_bpath_destroy;
84 item_class->update = sp_canvas_bpath_update;
85 item_class->render = sp_canvas_bpath_render;
86 item_class->point = sp_canvas_bpath_point;
87 }
89 static void
90 sp_canvas_bpath_init (SPCanvasBPath * bpath)
91 {
92 bpath->fill_rgba = 0x00000000;
93 bpath->fill_rule = SP_WIND_RULE_EVENODD;
95 bpath->stroke_rgba = 0x00000000;
96 bpath->stroke_width = 1.0;
97 bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
98 bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
99 bpath->stroke_miterlimit = 11.0;
100 }
102 static void
103 sp_canvas_bpath_destroy (GtkObject *object)
104 {
105 SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
107 if (cbp->curve) {
108 cbp->curve = cbp->curve->unref();
109 }
111 if (GTK_OBJECT_CLASS (parent_class)->destroy)
112 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
113 }
115 static void
116 sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
117 {
118 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
120 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
122 if (((SPCanvasItemClass *) parent_class)->update)
123 ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
125 sp_canvas_item_reset_bounds (item);
127 if (!cbp->curve) return;
129 cbp->affine = affine;
131 Geom::OptRect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
133 if (bbox) {
134 item->x1 = (int)bbox->min()[Geom::X] - 1;
135 item->y1 = (int)bbox->min()[Geom::Y] - 1;
136 item->x2 = (int)bbox->max()[Geom::X] + 1;
137 item->y2 = (int)bbox->max()[Geom::Y] + 1;
138 } else {
139 item->x1 = 0;
140 item->y1 = 0;
141 item->x2 = 0;
142 item->y2 = 0;
143 }
144 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
145 }
147 static void
148 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
149 {
150 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
152 sp_canvas_prepare_buffer(buf);
154 Geom::Rect area (Geom::Point(buf->rect.x0, buf->rect.y0), Geom::Point(buf->rect.x1, buf->rect.y1));
156 if ( !cbp->curve ||
157 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
158 cbp->curve->get_segment_count() < 1)
159 return;
161 if (!buf->ct)
162 return;
164 bool dofill = ((cbp->fill_rgba & 0xff) != 0);
165 bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
167 cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
168 cairo_new_path(buf->ct);
170 if (!dofill)
171 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
172 else
173 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
175 if (dofill) {
176 // RGB / BGR
177 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));
178 cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
179 : CAIRO_FILL_RULE_WINDING);
180 if (dostroke)
181 cairo_fill_preserve(buf->ct);
182 else
183 cairo_fill(buf->ct);
184 }
186 if (dostroke) {
187 // RGB / BGR
188 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));
189 cairo_set_line_width(buf->ct, 1);
190 if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
191 cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
192 }
193 cairo_stroke(buf->ct);
194 }
195 }
197 static double
198 sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
199 {
200 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
202 if ( !cbp->curve ||
203 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
204 cbp->curve->get_segment_count() < 1)
205 return NR_HUGE;
207 double width = 0.5;
208 Geom::Rect viewbox = item->canvas->getViewbox();
209 viewbox.expandBy (width);
210 double dist = NR_HUGE;
211 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
213 if (dist <= 1.0) {
214 *actual_item = item;
215 }
217 return dist;
218 }
220 SPCanvasItem *
221 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
222 {
223 g_return_val_if_fail (parent != NULL, NULL);
224 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
226 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
228 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
230 return item;
231 }
233 void
234 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
235 {
236 g_return_if_fail (cbp != NULL);
237 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
239 if (cbp->curve) {
240 cbp->curve = cbp->curve->unref();
241 }
243 if (curve) {
244 cbp->curve = curve->ref();
245 }
247 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
248 }
250 void
251 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
252 {
253 g_return_if_fail (cbp != NULL);
254 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
256 cbp->fill_rgba = rgba;
257 cbp->fill_rule = rule;
259 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
260 }
262 void
263 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
264 {
265 g_return_if_fail (cbp != NULL);
266 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
268 cbp->stroke_rgba = rgba;
269 cbp->stroke_width = MAX (width, 0.1);
270 cbp->stroke_linejoin = join;
271 cbp->stroke_linecap = cap;
272 cbp->dashes[0] = dash;
273 cbp->dashes[1] = gap;
275 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
276 }
278 /*
279 Local Variables:
280 mode:c++
281 c-file-style:"stroustrup"
282 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
283 indent-tabs-mode:nil
284 fill-column:99
285 End:
286 */
287 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :