eccaf4aeddd1652fce1a26831acc7a11b8a4cced
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 const_NRBPath bp;
122 bp.path = cbp->curve->get_bpath();
123 NRRect bbox;
124 nr_path_matrix_bbox_union(&bp, affine, &bbox);
126 item->x1 = (int)bbox.x0 - 1;
127 item->y1 = (int)bbox.y0 - 1;
128 item->x2 = (int)bbox.x1 + 1;
129 item->y2 = (int)bbox.y1 + 1;
131 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
132 }
134 static void
135 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
136 {
137 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
139 sp_canvas_prepare_buffer(buf);
141 NR::Rect area (NR::Point(buf->rect.x0, buf->rect.y0), NR::Point(buf->rect.x1, buf->rect.y1));
143 if ( !cbp->curve ||
144 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
145 cbp->curve->get_length() <= 1)
146 return;
148 if (!buf->ct)
149 return;
151 bool dofill = ((cbp->fill_rgba & 0xff) != 0);
152 bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
154 cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
155 cairo_new_path(buf->ct);
157 if (!dofill)
158 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, true, 1);
159 else
160 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, false, 1);
162 if (dofill) {
163 // RGB / BGR
164 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));
165 cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
166 : CAIRO_FILL_RULE_WINDING);
167 if (dostroke)
168 cairo_fill_preserve(buf->ct);
169 else
170 cairo_fill(buf->ct);
171 }
173 if (dostroke) {
174 // RGB / BGR
175 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));
176 cairo_set_line_width(buf->ct, 1);
177 cairo_stroke(buf->ct);
178 }
179 }
181 static double
182 sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
183 {
184 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
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 double width = 0.5;
192 NR::Rect viewbox = item->canvas->getViewbox();
193 viewbox.growBy (width);
194 double dist = NR_HUGE;
195 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
197 if (dist <= 1.0) {
198 *actual_item = item;
199 }
201 return dist;
202 }
204 SPCanvasItem *
205 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
206 {
207 g_return_val_if_fail (parent != NULL, NULL);
208 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
210 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
212 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
214 return item;
215 }
217 void
218 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
219 {
220 g_return_if_fail (cbp != NULL);
221 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
223 if (cbp->curve) {
224 cbp->curve = cbp->curve->unref();
225 }
227 if (curve) {
228 cbp->curve = curve->ref();
229 }
231 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
232 }
234 void
235 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
236 {
237 g_return_if_fail (cbp != NULL);
238 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
240 cbp->fill_rgba = rgba;
241 cbp->fill_rule = rule;
243 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
244 }
246 void
247 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap)
248 {
249 g_return_if_fail (cbp != NULL);
250 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
252 cbp->stroke_rgba = rgba;
253 cbp->stroke_width = MAX (width, 0.1);
254 cbp->stroke_linejoin = join;
255 cbp->stroke_linecap = cap;
257 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
258 }
263 /*
264 Local Variables:
265 mode:c++
266 c-file-style:"stroustrup"
267 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
268 indent-tabs-mode:nil
269 fill-column:99
270 End:
271 */
272 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :