Code

Connector tool: make connectors avoid the convex hull of shapes.
[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_CONNECTOR_CURVATURE:
299         case SP_ATTR_CONNECTION_START:
300         case SP_ATTR_CONNECTION_END:
301             path->connEndPair.setAttr(key, value);
302             break;
303         default:
304             if (((SPObjectClass *) parent_class)->set) {
305                 ((SPObjectClass *) parent_class)->set(object, key, value);
306             }
307             break;
308     }
311 /**
312  *
313  * Writes the path object into a Inkscape::XML::Node
314  */
315 static Inkscape::XML::Node *
316 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
318     SPShape *shape = (SPShape *) object;
320     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
321         repr = xml_doc->createElement("svg:path");
322     }
324 #ifdef PATH_VERBOSE
325 g_message("sp_path_write writes 'd' attribute");
326 #endif
327     if ( shape->curve != NULL ) {
328         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
329         repr->setAttribute("d", str);
330         g_free(str);
331     } else {
332         repr->setAttribute("d", NULL);
333     }
335     if (flags & SP_OBJECT_WRITE_EXT) {
336         SPPath *path = (SPPath *) object;
337         if ( path->original_curve != NULL ) {
338             gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
339             repr->setAttribute("inkscape:original-d", str);
340             g_free(str);
341         } else {
342             repr->setAttribute("inkscape:original-d", NULL);
343         }
344     }
346     SP_PATH(shape)->connEndPair.writeRepr(repr);
348     if (((SPObjectClass *)(parent_class))->write) {
349         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
350     }
352     return repr;
355 static void
356 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
358     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
359         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
360     }
362     if (((SPObjectClass *) parent_class)->update) {
363         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
364     }
366     SPPath *path = SP_PATH(object);
367     path->connEndPair.update();
371 /**
372  * Writes the given transform into the repr for the given item.
373  */
374 static Geom::Matrix
375 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
377     SPShape *shape = (SPShape *) item;
378     SPPath *path = (SPPath *) item;
380     if (!shape->curve) { // 0 nodes, nothing to transform
381         return Geom::identity();
382     }
384     // Transform the original-d path if this is a valid LPE item, other else the (ordinary) path
385     if (path->original_curve && SP_IS_LPE_ITEM(item) && 
386                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
387         path->original_curve->transform(xform);
388     } else {
389         shape->curve->transform(xform);
390     }
392     // Adjust stroke
393     sp_item_adjust_stroke(item, xform.descrim());
395     // Adjust pattern fill
396     sp_item_adjust_pattern(item, xform);
398     // Adjust gradient fill
399     sp_item_adjust_gradient(item, xform);
401     // Adjust LPE
402     sp_item_adjust_livepatheffect(item, xform);
404     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
406     // nothing remains - we've written all of the transform, so return identity
407     return Geom::identity();
411 static void
412 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
414     SPShape * const shape = (SPShape *) lpeitem;
415     SPPath * const path = (SPPath *) lpeitem;
416     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
418 #ifdef PATH_VERBOSE
419 g_message("sp_path_update_patheffect");
420 #endif
422     if (path->original_curve && sp_lpe_item_has_path_effect_recursive(lpeitem)) {
423         SPCurve *curve = path->original_curve->copy();
424         /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
425          * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
426         sp_shape_set_curve_insync(shape, curve, TRUE);
428         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
429         if (success && write) {
430             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
431 #ifdef PATH_VERBOSE
432 g_message("sp_path_update_patheffect writes 'd' attribute");
433 #endif
434             if ( shape->curve != NULL ) {
435                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
436                 repr->setAttribute("d", str);
437                 g_free(str);
438             } else {
439                 repr->setAttribute("d", NULL);
440             }
441         } else if (!success) {
442             // LPE was unsuccesfull. Read the old 'd'-attribute.
443             if (gchar const * value = repr->attribute("d")) {
444                 Geom::PathVector pv = sp_svg_read_pathv(value);
445                 SPCurve *oldcurve = new SPCurve(pv);
446                 if (oldcurve) {
447                     sp_shape_set_curve(shape, oldcurve, TRUE);
448                     oldcurve->unref();
449                 }
450             }
451         }
452         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
453         curve->unref();
454     }
458 /**
459  * Adds a original_curve to the path.  If owner is specified, a reference
460  * will be made, otherwise the curve will be copied into the path.
461  * Any existing curve in the path will be unreferenced first.
462  * This routine triggers reapplication of an effect if present
463  * and also triggers a request to update the display. Does not write
464  * result to XML when write=false.
465  */
466 void
467 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
469     if (path->original_curve) {
470         path->original_curve = path->original_curve->unref();
471     }
472     if (curve) {
473         if (owner) {
474             path->original_curve = curve->ref();
475         } else {
476             path->original_curve = curve->copy();
477         }
478     }
479     sp_lpe_item_update_patheffect(path, true, write);
480     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
483 /**
484  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
485  */
486 SPCurve *
487 sp_path_get_original_curve (SPPath *path)
489     if (path->original_curve) {
490         return path->original_curve->copy();
491     }
492     return NULL;
495 /**
496  * Return duplicate of edittable curve which is original_curve if it exists or
497  * shape->curve if not.
498  */
499 SPCurve*
500 sp_path_get_curve_for_edit (SPPath *path)
502     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
503                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
504         return sp_path_get_original_curve(path);
505     } else {
506         return sp_shape_get_curve( (SPShape *) path );
507     }
510 /**
511  * Return a reference to original_curve if it exists or
512  * shape->curve if not.
513  */
514 const SPCurve*
515 sp_path_get_curve_reference (SPPath *path)
517     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
518                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
519         return path->original_curve;
520     } else {
521         return path->curve;
522     }
525 /*
526   Local Variables:
527   mode:c++
528   c-file-style:"stroustrup"
529   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
530   indent-tabs-mode:nil
531   fill-column:99
532   End:
533 */
534 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :