Code

Fixed directory for inkscape executable.
[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 "live_effects/effect.h"
24 #include "live_effects/lpeobject.h"
25 #include "live_effects/lpeobject-reference.h"
26 #include "sp-lpe-item.h"
28 #include <display/curve.h>
29 #include <libnr/nr-matrix-fns.h>
30 #include <2geom/pathvector.h>
31 #include <2geom/bezier-curve.h>
32 #include <2geom/hvlinesegment.h>
33 #include "helper/geom-curves.h"
35 #include "svg/svg.h"
36 #include "xml/repr.h"
37 #include "attributes.h"
39 #include "sp-path.h"
40 #include "sp-guide.h"
42 #include "document.h"
43 #include "desktop.h"
44 #include "desktop-handles.h"
45 #include "desktop-style.h"
46 #include "event-context.h"
47 #include "inkscape.h"
48 #include "style.h"
49 #include "message-stack.h"
50 #include "selection.h"
52 #define noPATH_VERBOSE
54 static void sp_path_class_init(SPPathClass *klass);
55 static void sp_path_init(SPPath *path);
56 static void sp_path_finalize(GObject *obj);
57 static void sp_path_release(SPObject *object);
59 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
60 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
62 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
63 static Geom::Matrix sp_path_set_transform(SPItem *item, Geom::Matrix const &xform);
64 static gchar * sp_path_description(SPItem *item);
65 static void sp_path_convert_to_guides(SPItem *item);
67 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
68 static void sp_path_update_patheffect(SPLPEItem *lpeitem, bool write);
70 static SPShapeClass *parent_class;
72 /**
73  * Gets the GType object for SPPathClass
74  */
75 GType
76 sp_path_get_type(void)
77 {
78     static GType type = 0;
80     if (!type) {
81         GTypeInfo info = {
82             sizeof(SPPathClass),
83             NULL, NULL,
84             (GClassInitFunc) sp_path_class_init,
85             NULL, NULL,
86             sizeof(SPPath),
87             16,
88             (GInstanceInitFunc) sp_path_init,
89             NULL,   /* value_table */
90         };
91         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
92     }
93     return type;
94 }
96 /**
97  *  Does the object-oriented work of initializing the class structure
98  *  including parent class, and registers function pointers for
99  *  the functions build, set, write, and set_transform.
100  */
101 static void
102 sp_path_class_init(SPPathClass * klass)
104     GObjectClass *gobject_class = (GObjectClass *) klass;
105     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
106     SPItemClass *item_class = (SPItemClass *) klass;
107     SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
109     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
111     gobject_class->finalize = sp_path_finalize;
113     sp_object_class->build = sp_path_build;
114     sp_object_class->release = sp_path_release;
115     sp_object_class->set = sp_path_set;
116     sp_object_class->write = sp_path_write;
117     sp_object_class->update = sp_path_update;
119     item_class->description = sp_path_description;
120     item_class->set_transform = sp_path_set_transform;
121     item_class->convert_to_guides = sp_path_convert_to_guides;
123     lpe_item_class->update_patheffect = sp_path_update_patheffect;
127 gint
128 sp_nodes_in_path(SPPath *path)
130     SPCurve *curve = SP_SHAPE(path)->curve;
131     if (!curve)
132         return 0;
133     return curve->nodes_in_path();
136 static gchar *
137 sp_path_description(SPItem * item)
139     int count = sp_nodes_in_path(SP_PATH(item));
140     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
142         Glib::ustring s;
144         PathEffectList effect_list =  sp_lpe_item_get_effect_list(SP_LPE_ITEM(item));
145         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
146         {
147             LivePathEffectObject *lpeobj = (*it)->lpeobject;
148             if (!lpeobj || !lpeobj->get_lpe())
149                 break;
150             if (s.empty())
151                 s = lpeobj->get_lpe()->getName();
152             else
153                 s = s + ", " + lpeobj->get_lpe()->getName();
154         }
156         return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect: %s)",
157                                         "<b>Path</b> (%i nodes, path effect: %s)",count), count, s.c_str());
158     } else {
159         return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
160                                         "<b>Path</b> (%i nodes)",count), count);
161     }
164 static void
165 sp_path_convert_to_guides(SPItem *item)
167     SPPath *path = SP_PATH(item);
169     SPCurve *curve = SP_SHAPE(path)->curve;
170     if (!curve) return;
172     std::list<std::pair<Geom::Point, Geom::Point> > pts;
174     Geom::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
176     Geom::PathVector const & pv = curve->get_pathvector();
177     for(Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit) {
178         for(Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
179             // only add curves for straight line segments
180             if( is_straight_curve(*cit) )
181             {
182                 pts.push_back(std::make_pair(cit->initialPoint() * i2d, cit->finalPoint() * i2d));
183             }
184         }
185     }
187     sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
190 /**
191  * Initializes an SPPath.
192  */
193 static void
194 sp_path_init(SPPath *path)
196     new (&path->connEndPair) SPConnEndPair(path);
198     path->original_curve = NULL;
201 static void
202 sp_path_finalize(GObject *obj)
204     SPPath *path = (SPPath *) obj;
206     path->connEndPair.~SPConnEndPair();
209 /**
210  *  Given a repr, this sets the data items in the path object such as
211  *  fill & style attributes, markers, and CSS properties.
212  */
213 static void
214 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
216     /* Are these calls actually necessary? */
217     sp_object_read_attr(object, "marker");
218     sp_object_read_attr(object, "marker-start");
219     sp_object_read_attr(object, "marker-mid");
220     sp_object_read_attr(object, "marker-end");
222     sp_conn_end_pair_build(object);
224     if (((SPObjectClass *) parent_class)->build) {
225         ((SPObjectClass *) parent_class)->build(object, document, repr);
226     }
228     sp_object_read_attr(object, "inkscape:original-d");
229     sp_object_read_attr(object, "d");
231     /* d is a required attribute */
232     gchar const *d = sp_object_getAttribute(object, "d", NULL);
233     if (d == NULL) {
234         sp_object_set(object, sp_attribute_lookup("d"), "");
235     }
238 static void
239 sp_path_release(SPObject *object)
241     SPPath *path = SP_PATH(object);
243     path->connEndPair.release();
245     if (path->original_curve) {
246         path->original_curve = path->original_curve->unref();
247     }
249     if (((SPObjectClass *) parent_class)->release) {
250         ((SPObjectClass *) parent_class)->release(object);
251     }
254 /**
255  *  Sets a value in the path object given by 'key', to 'value'.  This is used
256  *  for setting attributes and markers on a path object.
257  */
258 static void
259 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
261     SPPath *path = (SPPath *) object;
263     switch (key) {
264         case SP_ATTR_INKSCAPE_ORIGINAL_D:
265                 if (value) {
266                     Geom::PathVector pv = sp_svg_read_pathv(value);
267                     SPCurve *curve = new SPCurve(pv);
268                     if (curve) {
269                         sp_path_set_original_curve(path, curve, TRUE, true);
270                         curve->unref();
271                     }
272                 } else {
273                     sp_path_set_original_curve(path, NULL, TRUE, true);
274                 }
275                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
276             break;
277        case SP_ATTR_D:
278                 if (value) {
279                     Geom::PathVector pv = sp_svg_read_pathv(value);
280                     SPCurve *curve = new SPCurve(pv);
281                     if (curve) {
282                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
283                         curve->unref();
284                     }
285                 } else {
286                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
287                 }
288                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
289             break;
290         case SP_PROP_MARKER:
291         case SP_PROP_MARKER_START:
292         case SP_PROP_MARKER_MID:
293         case SP_PROP_MARKER_END:
294             sp_shape_set_marker(object, key, value);
295             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
296             break;
297         case SP_ATTR_CONNECTOR_TYPE:
298         case SP_ATTR_CONNECTION_START:
299         case SP_ATTR_CONNECTION_END:
300             path->connEndPair.setAttr(key, value);
301             break;
302         default:
303             if (((SPObjectClass *) parent_class)->set) {
304                 ((SPObjectClass *) parent_class)->set(object, key, value);
305             }
306             break;
307     }
310 /**
311  *
312  * Writes the path object into a Inkscape::XML::Node
313  */
314 static Inkscape::XML::Node *
315 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
317     SPShape *shape = (SPShape *) object;
319     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
320         repr = xml_doc->createElement("svg:path");
321     }
323     if ( shape->curve != NULL ) {
324         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
325         repr->setAttribute("d", str);
326         g_free(str);
327     } else {
328         repr->setAttribute("d", NULL);
329     }
331     SPPath *path = (SPPath *) object;
332     if ( path->original_curve != NULL ) {
333         gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
334         repr->setAttribute("inkscape:original-d", str);
335         g_free(str);
336     } else {
337         repr->setAttribute("inkscape:original-d", NULL);
338     }
340     SP_PATH(shape)->connEndPair.writeRepr(repr);
342     if (((SPObjectClass *)(parent_class))->write) {
343         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
344     }
346     return repr;
349 static void
350 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
352     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
353         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
354     }
356     if (((SPObjectClass *) parent_class)->update) {
357         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
358     }
360     SPPath *path = SP_PATH(object);
361     path->connEndPair.update();
365 /**
366  * Writes the given transform into the repr for the given item.
367  */
368 static Geom::Matrix
369 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
371     SPShape *shape = (SPShape *) item;
372     SPPath *path = (SPPath *) item;
374     if (!shape->curve) { // 0 nodes, nothing to transform
375         return Geom::identity();
376     }
378     // Transform the original-d path if this is a valid LPE item, other else the (ordinary) path
379     if (path->original_curve && SP_IS_LPE_ITEM(item) && 
380                                 sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
381         path->original_curve->transform(xform);
382     } else {
383         shape->curve->transform(xform);
384     }
386     // Adjust stroke
387     sp_item_adjust_stroke(item, xform.descrim());
389     // Adjust pattern fill
390     sp_item_adjust_pattern(item, xform);
392     // Adjust gradient fill
393     sp_item_adjust_gradient(item, xform);
395     // Adjust LPE
396     sp_item_adjust_livepatheffect(item, xform);
398     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
400     // nothing remains - we've written all of the transform, so return identity
401     return Geom::identity();
405 static void
406 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
408     SPShape * const shape = (SPShape *) lpeitem;
409     SPPath * const path = (SPPath *) lpeitem;
410     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
412     if (path->original_curve && sp_lpe_item_has_path_effect_recursive(lpeitem)) {
413         SPCurve *curve = path->original_curve->copy();
414         /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
415          * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
416         sp_shape_set_curve_insync(shape, curve, TRUE);
418         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
419         if (success && write) {
420             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
421             if ( shape->curve != NULL ) {
422                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
423                 repr->setAttribute("d", str);
424                 g_free(str);
425             } else {
426                 repr->setAttribute("d", NULL);
427             }
428         } else {
429             // LPE was unsuccesfull. Read the old 'd'-attribute.
430             if (gchar const * value = repr->attribute("d")) {
431                 Geom::PathVector pv = sp_svg_read_pathv(value);
432                 SPCurve *oldcurve = new SPCurve(pv);
433                 if (oldcurve) {
434                     sp_shape_set_curve(shape, oldcurve, TRUE);
435                     oldcurve->unref();
436                 }
437             }
438         }
439         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
440         curve->unref();
441     }
445 /**
446  * Adds a original_curve to the path.  If owner is specified, a reference
447  * will be made, otherwise the curve will be copied into the path.
448  * Any existing curve in the path will be unreferenced first.
449  * This routine triggers reapplication of an effect if present
450  * and also triggers a request to update the display. Does not write
451  * result to XML when write=false.
452  */
453 void
454 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
456     if (path->original_curve) {
457         path->original_curve = path->original_curve->unref();
458     }
459     if (curve) {
460         if (owner) {
461             path->original_curve = curve->ref();
462         } else {
463             path->original_curve = curve->copy();
464         }
465     }
466     sp_lpe_item_update_patheffect(path, true, write);
467     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
470 /**
471  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
472  */
473 SPCurve *
474 sp_path_get_original_curve (SPPath *path)
476     if (path->original_curve) {
477         return path->original_curve->copy();
478     }
479     return NULL;
482 /**
483  * Return duplicate of edittable curve which is original_curve if it exists or
484  * shape->curve if not.
485  */
486 SPCurve*
487 sp_path_get_curve_for_edit (SPPath *path)
489     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
490                                 sp_lpe_item_has_path_effect(SP_LPE_ITEM(path))) {
491         return sp_path_get_original_curve(path);
492     } else {
493         return sp_shape_get_curve( (SPShape *) path );
494     }
497 /**
498  * Return a reference to original_curve if it exists or
499  * shape->curve if not.
500  */
501 const SPCurve*
502 sp_path_get_curve_reference (SPPath *path)
504     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
505                                 sp_lpe_item_has_path_effect(SP_LPE_ITEM(path))) {
506         return path->original_curve;
507     } else {
508         return path->curve;
509     }
512 /*
513   Local Variables:
514   mode:c++
515   c-file-style:"stroustrup"
516   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
517   indent-tabs-mode:nil
518   fill-column:99
519   End:
520 */
521 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :