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>
29 #include "helper/geom.h"
31 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
33 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
34 static void sp_canvas_bpath_init (SPCanvasBPath *path);
35 static void sp_canvas_bpath_destroy (GtkObject *object);
37 static void sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
38 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
39 static double sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
41 static SPCanvasItemClass *parent_class;
43 GtkType
44 sp_canvas_bpath_get_type (void)
45 {
46 static GtkType type = 0;
47 if (!type) {
48 GtkTypeInfo info = {
49 (gchar *)"SPCanvasBPath",
50 sizeof (SPCanvasBPath),
51 sizeof (SPCanvasBPathClass),
52 (GtkClassInitFunc) sp_canvas_bpath_class_init,
53 (GtkObjectInitFunc) sp_canvas_bpath_init,
54 NULL, NULL, NULL
55 };
56 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
57 }
58 return type;
59 }
61 static void
62 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
63 {
64 GtkObjectClass *object_class;
65 SPCanvasItemClass *item_class;
67 object_class = GTK_OBJECT_CLASS (klass);
68 item_class = (SPCanvasItemClass *) klass;
70 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
72 object_class->destroy = sp_canvas_bpath_destroy;
74 item_class->update = sp_canvas_bpath_update;
75 item_class->render = sp_canvas_bpath_render;
76 item_class->point = sp_canvas_bpath_point;
77 }
79 static void
80 sp_canvas_bpath_init (SPCanvasBPath * bpath)
81 {
82 bpath->fill_rgba = 0x00000000;
83 bpath->fill_rule = SP_WIND_RULE_EVENODD;
85 bpath->stroke_rgba = 0x00000000;
86 bpath->stroke_width = 1.0;
87 bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
88 bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
89 bpath->stroke_miterlimit = 11.0;
90 }
92 static void
93 sp_canvas_bpath_destroy (GtkObject *object)
94 {
95 SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
97 if (cbp->curve) {
98 cbp->curve = cbp->curve->unref();
99 }
101 if (GTK_OBJECT_CLASS (parent_class)->destroy)
102 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
103 }
105 static void
106 sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
107 {
108 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
110 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
112 if (((SPCanvasItemClass *) parent_class)->update)
113 ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
115 sp_canvas_item_reset_bounds (item);
117 if (!cbp->curve) return;
119 cbp->affine = affine;
121 Geom::Rect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), to_2geom(affine));
123 item->x1 = (int)bbox.min()[Geom::X] - 1;
124 item->y1 = (int)bbox.min()[Geom::Y] - 1;
125 item->x2 = (int)bbox.max()[Geom::X] + 1;
126 item->y2 = (int)bbox.max()[Geom::Y] + 1;
128 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
129 }
131 static void
132 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
133 {
134 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
136 sp_canvas_prepare_buffer(buf);
138 NR::Rect area (NR::Point(buf->rect.x0, buf->rect.y0), NR::Point(buf->rect.x1, buf->rect.y1));
140 if ( !cbp->curve ||
141 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
142 cbp->curve->get_length() <= 1)
143 return;
145 if (!buf->ct)
146 return;
148 bool dofill = ((cbp->fill_rgba & 0xff) != 0);
149 bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
151 cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
152 cairo_new_path(buf->ct);
154 if (!dofill)
155 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, true, 1);
156 else
157 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, false, 1);
159 if (dofill) {
160 // RGB / BGR
161 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));
162 cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
163 : CAIRO_FILL_RULE_WINDING);
164 if (dostroke)
165 cairo_fill_preserve(buf->ct);
166 else
167 cairo_fill(buf->ct);
168 }
170 if (dostroke) {
171 // RGB / BGR
172 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));
173 cairo_set_line_width(buf->ct, 1);
174 cairo_stroke(buf->ct);
175 }
176 }
178 static double
179 sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
180 {
181 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
183 if ( !cbp->curve ||
184 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
185 cbp->curve->get_length() <= 1)
186 return NR_HUGE;
188 double width = 0.5;
189 Geom::Rect viewbox = to_2geom(item->canvas->getViewbox());
190 viewbox.expandBy (width);
191 double dist = NR_HUGE;
192 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), to_2geom(cbp->affine), to_2geom(p), NULL, NULL, &dist, 0.5, &viewbox);
194 if (dist <= 1.0) {
195 *actual_item = item;
196 }
198 return dist;
199 }
201 SPCanvasItem *
202 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
203 {
204 g_return_val_if_fail (parent != NULL, NULL);
205 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
207 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
209 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
211 return item;
212 }
214 void
215 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
216 {
217 g_return_if_fail (cbp != NULL);
218 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
220 if (cbp->curve) {
221 cbp->curve = cbp->curve->unref();
222 }
224 if (curve) {
225 cbp->curve = curve->ref();
226 }
228 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
229 }
231 void
232 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
233 {
234 g_return_if_fail (cbp != NULL);
235 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
237 cbp->fill_rgba = rgba;
238 cbp->fill_rule = rule;
240 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
241 }
243 void
244 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap)
245 {
246 g_return_if_fail (cbp != NULL);
247 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
249 cbp->stroke_rgba = rgba;
250 cbp->stroke_width = MAX (width, 0.1);
251 cbp->stroke_linejoin = join;
252 cbp->stroke_linecap = cap;
254 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
255 }
260 /*
261 Local Variables:
262 mode:c++
263 c-file-style:"stroustrup"
264 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
265 indent-tabs-mode:nil
266 fill-column:99
267 End:
268 */
269 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :