Code

make win32 compile using libxslt
[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);
51 static void sp_path_update_patheffect(SPShape *shape, bool write);
53 static SPShapeClass *parent_class;
55 /**
56  * Gets the GType object for SPPathClass
57  */
58 GType
59 sp_path_get_type(void)
60 {
61     static GType type = 0;
63     if (!type) {
64         GTypeInfo info = {
65             sizeof(SPPathClass),
66             NULL, NULL,
67             (GClassInitFunc) sp_path_class_init,
68             NULL, NULL,
69             sizeof(SPPath),
70             16,
71             (GInstanceInitFunc) sp_path_init,
72             NULL,   /* value_table */
73         };
74         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
75     }
76     return type;
77 }
79 /**
80  *  Does the object-oriented work of initializing the class structure
81  *  including parent class, and registers function pointers for
82  *  the functions build, set, write, and set_transform.
83  */
84 static void
85 sp_path_class_init(SPPathClass * klass)
86 {
87     GObjectClass *gobject_class = (GObjectClass *) klass;
88     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
89     SPItemClass *item_class = (SPItemClass *) klass;
90     SPShapeClass *shape_class = (SPShapeClass *) klass;
92     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
94     gobject_class->finalize = sp_path_finalize;
96     sp_object_class->build = sp_path_build;
97     sp_object_class->release = sp_path_release;
98     sp_object_class->set = sp_path_set;
99     sp_object_class->write = sp_path_write;
100     sp_object_class->update = sp_path_update;
102     item_class->description = sp_path_description;
103     item_class->set_transform = sp_path_set_transform;
105     shape_class->update_patheffect = sp_path_update_patheffect;
109 gint
110 sp_nodes_in_path(SPPath *path)
112     SPCurve *curve = SP_SHAPE(path)->curve;
113     if (!curve) return 0;
114     gint r = curve->end;
115     gint i = curve->length - 1;
116     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
117     for (; i >= 0; i --)
118         if (SP_CURVE_BPATH(curve)[i].code == NR_MOVETO)
119             r --;
120     return r;
123 static gchar *
124 sp_path_description(SPItem * item)
126     int count = sp_nodes_in_path(SP_PATH(item));
127     if (SP_SHAPE(item)->path_effect_href) {
128         return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect)",
129                                         "<b>Path</b> (%i nodes, path effect)",count), count);
130     } else {
131         return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
132                                         "<b>Path</b> (%i nodes)",count), count);
133     }
136 /**
137  * Initializes an SPPath.
138  */
139 static void
140 sp_path_init(SPPath *path)
142     new (&path->connEndPair) SPConnEndPair(path);
144     path->original_curve = NULL;
147 static void
148 sp_path_finalize(GObject *obj)
150     SPPath *path = (SPPath *) obj;
152     path->connEndPair.~SPConnEndPair();
155 /**
156  *  Given a repr, this sets the data items in the path object such as
157  *  fill & style attributes, markers, and CSS properties.
158  */
159 static void
160 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
162     /* Are these calls actually necessary? */
163     sp_object_read_attr(object, "marker");
164     sp_object_read_attr(object, "marker-start");
165     sp_object_read_attr(object, "marker-mid");
166     sp_object_read_attr(object, "marker-end");
168     sp_conn_end_pair_build(object);
170     if (((SPObjectClass *) parent_class)->build) {
171         ((SPObjectClass *) parent_class)->build(object, document, repr);
172     }
174     sp_object_read_attr(object, "inkscape:original-d");
175     sp_object_read_attr(object, "d");
177     /* d is a required attribute */
178     gchar const *d = sp_object_getAttribute(object, "d", NULL);
179     if (d == NULL) {
180         sp_object_set(object, sp_attribute_lookup("d"), "");
181     }
184 static void
185 sp_path_release(SPObject *object)
187     SPPath *path = SP_PATH(object);
189     path->connEndPair.release();
191     if (path->original_curve) {
192         path->original_curve = sp_curve_unref (path->original_curve);
193     }
195     if (((SPObjectClass *) parent_class)->release) {
196         ((SPObjectClass *) parent_class)->release(object);
197     }
200 /**
201  *  Sets a value in the path object given by 'key', to 'value'.  This is used
202  *  for setting attributes and markers on a path object.
203  */
204 static void
205 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
207     SPPath *path = (SPPath *) object;
209     switch (key) {
210         case SP_ATTR_INKSCAPE_ORIGINAL_D:
211                 if (value) {
212                     NArtBpath *bpath = sp_svg_read_path(value);
213                     SPCurve *curve = sp_curve_new_from_bpath(bpath);
214                     if (curve) {
215                         sp_path_set_original_curve(path, curve, TRUE, true);
216                         sp_curve_unref(curve);
217                     }
218                 } else {
219                     sp_path_set_original_curve(path, NULL, TRUE, true);
220                 }
221                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
222             break;
223        case SP_ATTR_D:
224             if (!((SPShape *) path)->path_effect_href) {
225                 if (value) {
226                     NArtBpath *bpath = sp_svg_read_path(value);
227                     SPCurve *curve = sp_curve_new_from_bpath(bpath);
228                     if (curve) {
229                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
230                         sp_curve_unref(curve);
231                     }
232                 } else {
233                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
234                 }
235                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
236             }
237             break;
238         case SP_PROP_MARKER:
239         case SP_PROP_MARKER_START:
240         case SP_PROP_MARKER_MID:
241         case SP_PROP_MARKER_END:
242             sp_shape_set_marker(object, key, value);
243             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
244             break;
245         case SP_ATTR_CONNECTOR_TYPE:
246         case SP_ATTR_CONNECTION_START:
247         case SP_ATTR_CONNECTION_END:
248             path->connEndPair.setAttr(key, value);
249             break;
250         default:
251             if (((SPObjectClass *) parent_class)->set) {
252                 ((SPObjectClass *) parent_class)->set(object, key, value);
253             }
254             break;
255     }
258 /**
259  *
260  * Writes the path object into a Inkscape::XML::Node
261  */
262 static Inkscape::XML::Node *
263 sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
265     SPShape *shape = (SPShape *) object;
267     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
268         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
269         repr = xml_doc->createElement("svg:path");
270     }
272     if ( shape->curve != NULL ) {
273         NArtBpath *abp = sp_curve_first_bpath(shape->curve);
274         if (abp) {
275             gchar *str = sp_svg_write_path(abp);
276             repr->setAttribute("d", str);
277             g_free(str);
278         } else {
279             repr->setAttribute("d", "");
280         }
281     } else {
282         repr->setAttribute("d", NULL);
283     }
285     SPPath *path = (SPPath *) object;
286     if ( path->original_curve != NULL ) {
287         NArtBpath *abp = sp_curve_first_bpath(path->original_curve);
288         if (abp) {
289             gchar *str = sp_svg_write_path(abp);
290             repr->setAttribute("inkscape:original-d", str);
291             g_free(str);
292         } else {
293             repr->setAttribute("inkscape:original-d", "");
294         }
295     } else {
296         repr->setAttribute("inkscape:original-d", NULL);
297     }
299     SP_PATH(shape)->connEndPair.writeRepr(repr);
301     if (((SPObjectClass *)(parent_class))->write) {
302         ((SPObjectClass *)(parent_class))->write(object, repr, flags);
303     }
305     return repr;
308 static void
309 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
311     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
312         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
313     }
315     if (((SPObjectClass *) parent_class)->update) {
316         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
317     }
319     SPPath *path = SP_PATH(object);
320     path->connEndPair.update();
324 /**
325  * Writes the given transform into the repr for the given item.
326  */
327 static NR::Matrix
328 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
330     SPShape *shape = (SPShape *) item;
331     SPPath *path = (SPPath *) item;
333     if (!shape->curve) { // 0 nodes, nothing to transform
334         return NR::identity();
335     }
337     if (path->original_curve) { /* Transform the original-d path */
338         NRBPath dorigpath, sorigpath;
339         sorigpath.path = SP_CURVE_BPATH(path->original_curve);
340         nr_path_duplicate_transform(&dorigpath, &sorigpath, xform);
341         SPCurve *origcurve = sp_curve_new_from_bpath(dorigpath.path);
342         if (origcurve) {
343             sp_path_set_original_curve(path, origcurve, TRUE, true);
344             sp_curve_unref(origcurve);
345         }
346     } else {    /* Transform the path */
347         NRBPath dpath, spath;
348         spath.path = SP_CURVE_BPATH(shape->curve);
349         nr_path_duplicate_transform(&dpath, &spath, xform);
350         SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
351         if (curve) {
352             sp_shape_set_curve(shape, curve, TRUE);
353             sp_curve_unref(curve);
354         }
355     }
357     // Adjust stroke
358     sp_item_adjust_stroke(item, NR::expansion(xform));
360     // Adjust pattern fill
361     sp_item_adjust_pattern(item, xform);
363     // Adjust gradient fill
364     sp_item_adjust_gradient(item, xform);
366     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
368     // nothing remains - we've written all of the transform, so return identity
369     return NR::identity();
372 static void
373 sp_path_update_patheffect(SPShape *shape, bool write)
375     SPPath *path = (SPPath *) shape;
376     if (path->original_curve) {
377         SPCurve *curve = sp_curve_copy (path->original_curve);
378         sp_shape_perform_path_effect(curve, shape);
379         sp_shape_set_curve(shape, curve, TRUE);
380         sp_curve_unref(curve);
382         if (write) {
383             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
384             Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
385             if ( shape->curve != NULL ) {
386                 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
387                 if (abp) {
388                     gchar *str = sp_svg_write_path(abp);
389                     repr->setAttribute("d", str);
390                     g_free(str);
391                 } else {
392                     repr->setAttribute("d", "");
393                 }
394             } else {
395                 repr->setAttribute("d", NULL);
396             }
397         }
398     } else {
400     }
404 /**
405  * Adds a original_curve to the path.  If owner is specified, a reference
406  * will be made, otherwise the curve will be copied into the path.
407  * Any existing curve in the path will be unreferenced first.
408  * This routine triggers reapplication of the an effect is present
409  * an also triggers a request to update the display. Does not write
410 * result to XML when write=false.
411  */
412 void
413 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
415     if (path->original_curve) {
416         path->original_curve = sp_curve_unref (path->original_curve);
417     }
418     if (curve) {
419         if (owner) {
420             path->original_curve = sp_curve_ref (curve);
421         } else {
422             path->original_curve = sp_curve_copy (curve);
423         }
424     }
425     sp_path_update_patheffect(path, write);
426     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
429 /**
430  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
431  */
432 SPCurve *
433 sp_path_get_original_curve (SPPath *path)
435     if (path->original_curve) {
436         return sp_curve_copy (path->original_curve);
437     }
438     return NULL;
441 /**
442  * Return duplicate of edittable curve which is original_curve if it exists or
443  * shape->curve if not.
444  */
445 SPCurve*
446 sp_path_get_curve_for_edit (SPPath *path)
448     if (path->original_curve) {
449         return sp_path_get_original_curve(path);
450     } else {
451         return sp_shape_get_curve( (SPShape *) path );
452     }
455 /*
456   Local Variables:
457   mode:c++
458   c-file-style:"stroustrup"
459   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
460   indent-tabs-mode:nil
461   fill-column:99
462   End:
463 */
464 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :