Code

Applied patch #1501709
[inkscape.git] / src / sp-path.cpp
1 #define __SP_PATH_C__
3 /*
4  * SVG <path> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   David Turner <novalis@gnu.org>
9  *
10  * Copyright (C) 2004 David Turner
11  * Copyright (C) 1999-2002 Lauris Kaplinski
12  * Copyright (C) 2000-2001 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <glibmm/i18n.h>
23 #include <display/curve.h>
24 #include <libnr/n-art-bpath.h>
25 #include <libnr/nr-path.h>
26 #include <libnr/nr-matrix-fns.h>
28 #include "svg/svg.h"
29 #include "xml/repr.h"
30 #include "attributes.h"
32 #include "sp-path.h"
34 #define noPATH_VERBOSE
36 static void sp_path_class_init(SPPathClass *klass);
37 static void sp_path_init(SPPath *path);
38 static void sp_path_finalize(GObject *obj);
39 static void sp_path_release(SPObject *object);
41 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
42 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
44 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
45 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
46 static gchar * sp_path_description(SPItem *item);
48 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
50 static SPShapeClass *parent_class;
52 /**
53  * Gets the GType object for SPPathClass
54  */
55 GType
56 sp_path_get_type(void)
57 {
58     static GType type = 0;
60     if (!type) {
61         GTypeInfo info = {
62             sizeof(SPPathClass),
63             NULL, NULL,
64             (GClassInitFunc) sp_path_class_init,
65             NULL, NULL,
66             sizeof(SPPath),
67             16,
68             (GInstanceInitFunc) sp_path_init,
69             NULL,   /* value_table */
70         };
71         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
72     }
73     return type;
74 }
76 /**
77  *  Does the object-oriented work of initializing the class structure
78  *  including parent class, and registers function pointers for
79  *  the functions build, set, write, and set_transform.
80  */
81 static void
82 sp_path_class_init(SPPathClass * klass)
83 {
84     GObjectClass *gobject_class = (GObjectClass *) klass;
85     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
86     SPItemClass *item_class = (SPItemClass *) klass;
88     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
90     gobject_class->finalize = sp_path_finalize;
92     sp_object_class->build = sp_path_build;
93     sp_object_class->release = sp_path_release;
94     sp_object_class->set = sp_path_set;
95     sp_object_class->write = sp_path_write;
96     sp_object_class->update = sp_path_update;
98     item_class->description = sp_path_description;
99     item_class->set_transform = sp_path_set_transform;
103 gint
104 sp_nodes_in_path(SPPath *path)
106     SPCurve *curve = SP_SHAPE(path)->curve;
107     if (!curve) return 0;
108     gint r = curve->end;
109     gint i = curve->length - 1;
110     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
111     for (; i >= 0; i --)
112         if (SP_CURVE_BPATH(curve)[i].code == NR_MOVETO)
113             r --;
114     return r;
117 static gchar *
118 sp_path_description(SPItem * item)
120     int count = sp_nodes_in_path(SP_PATH(item));
121     return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
122                                     "<b>Path</b> (%i nodes)",count), count);
125 /**
126  * Initializes an SPPath.  Currently does nothing.
127  */
128 static void
129 sp_path_init(SPPath *path)
131     new (&path->connEndPair) SPConnEndPair(path);
134 static void
135 sp_path_finalize(GObject *obj)
137     SPPath *path = (SPPath *) obj;
139     path->connEndPair.~SPConnEndPair();
142 /**
143  *  Given a repr, this sets the data items in the path object such as
144  *  fill & style attributes, markers, and CSS properties.
145  */
146 static void
147 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
149     sp_object_read_attr(object, "d");
151     /* d is a required attribute */
152     gchar const *d = sp_object_getAttribute(object, "d", NULL);
153     if (d == NULL) {
154         sp_object_set(object, sp_attribute_lookup("d"), "");
155     }
157     /* Are these calls actually necessary? */
158     sp_object_read_attr(object, "marker");
159     sp_object_read_attr(object, "marker-start");
160     sp_object_read_attr(object, "marker-mid");
161     sp_object_read_attr(object, "marker-end");
163     sp_conn_end_pair_build(object);
165     if (((SPObjectClass *) parent_class)->build) {
166         ((SPObjectClass *) parent_class)->build(object, document, repr);
167     }
170 static void
171 sp_path_release(SPObject *object)
173     SPPath *path = SP_PATH(object);
175     path->connEndPair.release();
177     if (((SPObjectClass *) parent_class)->release) {
178         ((SPObjectClass *) parent_class)->release(object);
179     }
182 /**
183  *  Sets a value in the path object given by 'key', to 'value'.  This is used
184  *  for setting attributes and markers on a path object.
185  */
186 static void
187 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
189     SPPath *path = (SPPath *) object;
191     switch (key) {
192         case SP_ATTR_D:
193             if (value) {
194                 NArtBpath *bpath = sp_svg_read_path(value);
195                 SPCurve *curve = sp_curve_new_from_bpath(bpath);
196                 if (curve) {
197                     sp_shape_set_curve((SPShape *) path, curve, TRUE);
198                     sp_curve_unref(curve);
199                 }
200             } else {
201                 sp_shape_set_curve((SPShape *) path, NULL, TRUE);
202             }
203             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
204             break;
205         case SP_PROP_MARKER:
206         case SP_PROP_MARKER_START:
207         case SP_PROP_MARKER_MID:
208         case SP_PROP_MARKER_END:
209             sp_shape_set_marker(object, key, value);
210             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
211             break;
212         case SP_ATTR_CONNECTOR_TYPE:
213         case SP_ATTR_CONNECTION_START:
214         case SP_ATTR_CONNECTION_END:
215             path->connEndPair.setAttr(key, value);
216             break;
217         default:
218             if (((SPObjectClass *) parent_class)->set) {
219                 ((SPObjectClass *) parent_class)->set(object, key, value);
220             }
221             break;
222     }
225 /**
226  *
227  * Writes the path object into a Inkscape::XML::Node
228  */
229 static Inkscape::XML::Node *
230 sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
232     SPShape *shape = (SPShape *) object;
234     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
235         repr = sp_repr_new("svg:path");
236     }
238     if ( shape->curve != NULL ) {
239         NArtBpath *abp = sp_curve_first_bpath(shape->curve);
240         if (abp) {
241             gchar *str = sp_svg_write_path(abp);
242             repr->setAttribute("d", str);
243             g_free(str);
244         } else {
245             repr->setAttribute("d", "");
246         }
247     } else {
248         repr->setAttribute("d", NULL);
249     }
251     SP_PATH(shape)->connEndPair.writeRepr(repr);
253     if (((SPObjectClass *)(parent_class))->write) {
254         ((SPObjectClass *)(parent_class))->write(object, repr, flags);
255     }
257     return repr;
260 static void
261 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
263     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
264         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
265     }
267     if (((SPObjectClass *) parent_class)->update) {
268         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
269     }
271     SPPath *path = SP_PATH(object);
272     path->connEndPair.update();
276 /**
277  * Writes the given transform into the repr for the given item.
278  */
279 static NR::Matrix
280 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
282     SPShape *shape = (SPShape *) item;
284     if (!shape->curve) { // 0 nodes, nothing to transform
285         return NR::identity();
286     }
288     /* Transform the path */
289     NRBPath dpath, spath;
290     spath.path = SP_CURVE_BPATH(shape->curve);
291     nr_path_duplicate_transform(&dpath, &spath, xform);
292     SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
293     if (curve) {
294         sp_shape_set_curve(shape, curve, TRUE);
295         sp_curve_unref(curve);
296     }
298     // Adjust stroke
299     sp_item_adjust_stroke(item, NR::expansion(xform));
301     // Adjust pattern fill
302     sp_item_adjust_pattern(item, xform);
304     // Adjust gradient fill
305     sp_item_adjust_gradient(item, xform);
307     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
309     // nothing remains - we've written all of the transform, so return identity
310     return NR::identity();
314 /*
315   Local Variables:
316   mode:c++
317   c-file-style:"stroustrup"
318   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
319   indent-tabs-mode:nil
320   fill-column:99
321   End:
322 */
323 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :