Code

start switching sp_repr_new* over to XML::Document::create*, and rename create method...
[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 #include "document.h"
36 #define noPATH_VERBOSE
38 static void sp_path_class_init(SPPathClass *klass);
39 static void sp_path_init(SPPath *path);
40 static void sp_path_finalize(GObject *obj);
41 static void sp_path_release(SPObject *object);
43 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
44 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
46 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
47 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
48 static gchar * sp_path_description(SPItem *item);
50 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
52 static SPShapeClass *parent_class;
54 /**
55  * Gets the GType object for SPPathClass
56  */
57 GType
58 sp_path_get_type(void)
59 {
60     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SPPathClass),
65             NULL, NULL,
66             (GClassInitFunc) sp_path_class_init,
67             NULL, NULL,
68             sizeof(SPPath),
69             16,
70             (GInstanceInitFunc) sp_path_init,
71             NULL,   /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
74     }
75     return type;
76 }
78 /**
79  *  Does the object-oriented work of initializing the class structure
80  *  including parent class, and registers function pointers for
81  *  the functions build, set, write, and set_transform.
82  */
83 static void
84 sp_path_class_init(SPPathClass * klass)
85 {
86     GObjectClass *gobject_class = (GObjectClass *) klass;
87     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
88     SPItemClass *item_class = (SPItemClass *) klass;
90     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
92     gobject_class->finalize = sp_path_finalize;
94     sp_object_class->build = sp_path_build;
95     sp_object_class->release = sp_path_release;
96     sp_object_class->set = sp_path_set;
97     sp_object_class->write = sp_path_write;
98     sp_object_class->update = sp_path_update;
100     item_class->description = sp_path_description;
101     item_class->set_transform = sp_path_set_transform;
105 gint
106 sp_nodes_in_path(SPPath *path)
108     SPCurve *curve = SP_SHAPE(path)->curve;
109     if (!curve) return 0;
110     gint r = curve->end;
111     gint i = curve->length - 1;
112     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
113     for (; i >= 0; i --)
114         if (SP_CURVE_BPATH(curve)[i].code == NR_MOVETO)
115             r --;
116     return r;
119 static gchar *
120 sp_path_description(SPItem * item)
122     int count = sp_nodes_in_path(SP_PATH(item));
123     return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
124                                     "<b>Path</b> (%i nodes)",count), count);
127 /**
128  * Initializes an SPPath.  Currently does nothing.
129  */
130 static void
131 sp_path_init(SPPath *path)
133     new (&path->connEndPair) SPConnEndPair(path);
136 static void
137 sp_path_finalize(GObject *obj)
139     SPPath *path = (SPPath *) obj;
141     path->connEndPair.~SPConnEndPair();
144 /**
145  *  Given a repr, this sets the data items in the path object such as
146  *  fill & style attributes, markers, and CSS properties.
147  */
148 static void
149 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
151     sp_object_read_attr(object, "d");
153     /* d is a required attribute */
154     gchar const *d = sp_object_getAttribute(object, "d", NULL);
155     if (d == NULL) {
156         sp_object_set(object, sp_attribute_lookup("d"), "");
157     }
159     /* Are these calls actually necessary? */
160     sp_object_read_attr(object, "marker");
161     sp_object_read_attr(object, "marker-start");
162     sp_object_read_attr(object, "marker-mid");
163     sp_object_read_attr(object, "marker-end");
165     sp_conn_end_pair_build(object);
167     if (((SPObjectClass *) parent_class)->build) {
168         ((SPObjectClass *) parent_class)->build(object, document, repr);
169     }
172 static void
173 sp_path_release(SPObject *object)
175     SPPath *path = SP_PATH(object);
177     path->connEndPair.release();
179     if (((SPObjectClass *) parent_class)->release) {
180         ((SPObjectClass *) parent_class)->release(object);
181     }
184 /**
185  *  Sets a value in the path object given by 'key', to 'value'.  This is used
186  *  for setting attributes and markers on a path object.
187  */
188 static void
189 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
191     SPPath *path = (SPPath *) object;
193     switch (key) {
194         case SP_ATTR_D:
195             if (value) {
196                 NArtBpath *bpath = sp_svg_read_path(value);
197                 SPCurve *curve = sp_curve_new_from_bpath(bpath);
198                 if (curve) {
199                     sp_shape_set_curve((SPShape *) path, curve, TRUE);
200                     sp_curve_unref(curve);
201                 }
202             } else {
203                 sp_shape_set_curve((SPShape *) path, NULL, TRUE);
204             }
205             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206             break;
207         case SP_PROP_MARKER:
208         case SP_PROP_MARKER_START:
209         case SP_PROP_MARKER_MID:
210         case SP_PROP_MARKER_END:
211             sp_shape_set_marker(object, key, value);
212             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
213             break;
214         case SP_ATTR_CONNECTOR_TYPE:
215         case SP_ATTR_CONNECTION_START:
216         case SP_ATTR_CONNECTION_END:
217             path->connEndPair.setAttr(key, value);
218             break;
219         default:
220             if (((SPObjectClass *) parent_class)->set) {
221                 ((SPObjectClass *) parent_class)->set(object, key, value);
222             }
223             break;
224     }
227 /**
228  *
229  * Writes the path object into a Inkscape::XML::Node
230  */
231 static Inkscape::XML::Node *
232 sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
234     SPShape *shape = (SPShape *) object;
236     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
237         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
238         repr = xml_doc->createElement("svg:path");
239     }
241     if ( shape->curve != NULL ) {
242         NArtBpath *abp = sp_curve_first_bpath(shape->curve);
243         if (abp) {
244             gchar *str = sp_svg_write_path(abp);
245             repr->setAttribute("d", str);
246             g_free(str);
247         } else {
248             repr->setAttribute("d", "");
249         }
250     } else {
251         repr->setAttribute("d", NULL);
252     }
254     SP_PATH(shape)->connEndPair.writeRepr(repr);
256     if (((SPObjectClass *)(parent_class))->write) {
257         ((SPObjectClass *)(parent_class))->write(object, repr, flags);
258     }
260     return repr;
263 static void
264 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
266     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
267         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
268     }
270     if (((SPObjectClass *) parent_class)->update) {
271         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
272     }
274     SPPath *path = SP_PATH(object);
275     path->connEndPair.update();
279 /**
280  * Writes the given transform into the repr for the given item.
281  */
282 static NR::Matrix
283 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
285     SPShape *shape = (SPShape *) item;
287     if (!shape->curve) { // 0 nodes, nothing to transform
288         return NR::identity();
289     }
291     /* Transform the path */
292     NRBPath dpath, spath;
293     spath.path = SP_CURVE_BPATH(shape->curve);
294     nr_path_duplicate_transform(&dpath, &spath, xform);
295     SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
296     if (curve) {
297         sp_shape_set_curve(shape, curve, TRUE);
298         sp_curve_unref(curve);
299     }
301     // Adjust stroke
302     sp_item_adjust_stroke(item, NR::expansion(xform));
304     // Adjust pattern fill
305     sp_item_adjust_pattern(item, xform);
307     // Adjust gradient fill
308     sp_item_adjust_gradient(item, xform);
310     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
312     // nothing remains - we've written all of the transform, so return identity
313     return NR::identity();
317 /*
318   Local Variables:
319   mode:c++
320   c-file-style:"stroustrup"
321   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
322   indent-tabs-mode:nil
323   fill-column:99
324   End:
325 */
326 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :