Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / display / canvas-bpath.cpp
1 /*
2  * Simple bezier bpath CanvasItem for inkscape
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@ximian.com>
6  *
7  * Copyright (C) 2001 Lauris Kaplinski and Ximian, Inc.
8  *
9  * Released under GNU GPL
10  *
11  */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16 #include <sstream>
17 #include <string.h>
18 #include <desktop.h>
20 #include "color.h"
21 #include "sp-canvas-util.h"
22 #include "inkscape-cairo.h"
23 #include "canvas-bpath.h"
24 #include "display/sp-canvas-group.h"
25 #include "display/curve.h"
26 #include "display/inkscape-cairo.h"
27 #include "libnr/nr-pixops.h"
28 #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, Geom::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, Geom::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);
105 static void
106 sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
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::OptRect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
123     if (bbox) {
124         item->x1 = (int)bbox->min()[Geom::X] - 1;
125         item->y1 = (int)bbox->min()[Geom::Y] - 1;
126         item->x2 = (int)bbox->max()[Geom::X] + 1;
127         item->y2 = (int)bbox->max()[Geom::Y] + 1;
128     } else {
129         item->x1 = 0;
130         item->y1 = 0;
131         item->x2 = 0;
132         item->y2 = 0;
133     }
134     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
137 static void
138 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
140     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
142     sp_canvas_prepare_buffer(buf);
144     Geom::Rect area (Geom::Point(buf->rect.x0, buf->rect.y0), Geom::Point(buf->rect.x1, buf->rect.y1));
146     if ( !cbp->curve  || 
147          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
148          cbp->curve->get_segment_count() < 1)
149         return;
151     if (!buf->ct)
152         return;
154     bool dofill = ((cbp->fill_rgba & 0xff) != 0);
155     bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
157     cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
158     cairo_new_path(buf->ct);
160     if (!dofill)
161         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
162     else
163         feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
165     if (dofill) {
166         // RGB / BGR
167         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));
168         cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
169                             : CAIRO_FILL_RULE_WINDING);
170         if (dostroke)
171             cairo_fill_preserve(buf->ct);
172         else 
173             cairo_fill(buf->ct);
174     }
176     if (dostroke) {
177         // RGB / BGR
178         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));
179         cairo_set_line_width(buf->ct, 1);
180         if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
181             cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
182         }
183         cairo_stroke(buf->ct);
184     }
187 static double
188 sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
190     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
192     if ( !cbp->curve  || 
193          ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
194          cbp->curve->get_segment_count() < 1)
195         return NR_HUGE;
197     double width = 0.5;
198     Geom::Rect viewbox = item->canvas->getViewbox();
199     viewbox.expandBy (width);
200     double dist = NR_HUGE;
201     pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
203     if (dist <= 1.0) {
204         *actual_item = item;
205     }
207     return dist;
210 SPCanvasItem *
211 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
213     g_return_val_if_fail (parent != NULL, NULL);
214     g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
216     SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
218     sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
220     return item;
223 void
224 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
226     g_return_if_fail (cbp != NULL);
227     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
229     if (cbp->curve) {
230         cbp->curve = cbp->curve->unref();
231     }
233     if (curve) {
234         cbp->curve = curve->ref();
235     }
237     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
240 void
241 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
243     g_return_if_fail (cbp != NULL);
244     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
246     cbp->fill_rgba = rgba;
247     cbp->fill_rule = rule;
249     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
252 void
253 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
255     g_return_if_fail (cbp != NULL);
256     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
258     cbp->stroke_rgba = rgba;
259     cbp->stroke_width = MAX (width, 0.1);
260     cbp->stroke_linejoin = join;
261     cbp->stroke_linecap = cap;
262     cbp->dashes[0] = dash;
263     cbp->dashes[1] = gap;
265     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
268 /*
269   Local Variables:
270   mode:c++
271   c-file-style:"stroustrup"
272   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
273   indent-tabs-mode:nil
274   fill-column:99
275   End:
276 */
277 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :