Code

Updating to current trunk
[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 #ifdef PATH_VERBOSE
324 g_message("sp_path_write writes 'd' attribute");
325 #endif
326     if ( shape->curve != NULL ) {
327         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
328         repr->setAttribute("d", str);
329         g_free(str);
330     } else {
331         repr->setAttribute("d", NULL);
332     }
334     if (flags & SP_OBJECT_WRITE_EXT) {
335         SPPath *path = (SPPath *) object;
336         if ( path->original_curve != NULL ) {
337             gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
338             repr->setAttribute("inkscape:original-d", str);
339             g_free(str);
340         } else {
341             repr->setAttribute("inkscape:original-d", NULL);
342         }
343     }
345     SP_PATH(shape)->connEndPair.writeRepr(repr);
347     if (((SPObjectClass *)(parent_class))->write) {
348         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
349     }
351     return repr;
354 static void
355 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
357     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
358         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
359     }
361     if (((SPObjectClass *) parent_class)->update) {
362         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
363     }
365     SPPath *path = SP_PATH(object);
366     path->connEndPair.update();
370 /**
371  * Writes the given transform into the repr for the given item.
372  */
373 static Geom::Matrix
374 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
376     SPShape *shape = (SPShape *) item;
377     SPPath *path = (SPPath *) item;
379     if (!shape->curve) { // 0 nodes, nothing to transform
380         return Geom::identity();
381     }
383     // Transform the original-d path if this is a valid LPE item, other else the (ordinary) path
384     if (path->original_curve && SP_IS_LPE_ITEM(item) && 
385                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
386         path->original_curve->transform(xform);
387     } else {
388         shape->curve->transform(xform);
389     }
391     // Adjust stroke
392     sp_item_adjust_stroke(item, xform.descrim());
394     // Adjust pattern fill
395     sp_item_adjust_pattern(item, xform);
397     // Adjust gradient fill
398     sp_item_adjust_gradient(item, xform);
400     // Adjust LPE
401     sp_item_adjust_livepatheffect(item, xform);
403     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
405     // nothing remains - we've written all of the transform, so return identity
406     return Geom::identity();
410 static void
411 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
413     SPShape * const shape = (SPShape *) lpeitem;
414     SPPath * const path = (SPPath *) lpeitem;
415     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
417 #ifdef PATH_VERBOSE
418 g_message("sp_path_update_patheffect");
419 #endif
421     if (path->original_curve && sp_lpe_item_has_path_effect_recursive(lpeitem)) {
422         SPCurve *curve = path->original_curve->copy();
423         /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
424          * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
425         sp_shape_set_curve_insync(shape, curve, TRUE);
427         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
428         if (success && write) {
429             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
430 #ifdef PATH_VERBOSE
431 g_message("sp_path_update_patheffect writes 'd' attribute");
432 #endif
433             if ( shape->curve != NULL ) {
434                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
435                 repr->setAttribute("d", str);
436                 g_free(str);
437             } else {
438                 repr->setAttribute("d", NULL);
439             }
440         } else if (!success) {
441             // LPE was unsuccesfull. Read the old 'd'-attribute.
442             if (gchar const * value = repr->attribute("d")) {
443                 Geom::PathVector pv = sp_svg_read_pathv(value);
444                 SPCurve *oldcurve = new SPCurve(pv);
445                 if (oldcurve) {
446                     sp_shape_set_curve(shape, oldcurve, TRUE);
447                     oldcurve->unref();
448                 }
449             }
450         }
451         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
452         curve->unref();
453     }
457 /**
458  * Adds a original_curve to the path.  If owner is specified, a reference
459  * will be made, otherwise the curve will be copied into the path.
460  * Any existing curve in the path will be unreferenced first.
461  * This routine triggers reapplication of an effect if present
462  * and also triggers a request to update the display. Does not write
463  * result to XML when write=false.
464  */
465 void
466 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
468     if (path->original_curve) {
469         path->original_curve = path->original_curve->unref();
470     }
471     if (curve) {
472         if (owner) {
473             path->original_curve = curve->ref();
474         } else {
475             path->original_curve = curve->copy();
476         }
477     }
478     sp_lpe_item_update_patheffect(path, true, write);
479     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
482 /**
483  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
484  */
485 SPCurve *
486 sp_path_get_original_curve (SPPath *path)
488     if (path->original_curve) {
489         return path->original_curve->copy();
490     }
491     return NULL;
494 /**
495  * Return duplicate of edittable curve which is original_curve if it exists or
496  * shape->curve if not.
497  */
498 SPCurve*
499 sp_path_get_curve_for_edit (SPPath *path)
501     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
502                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
503         return sp_path_get_original_curve(path);
504     } else {
505         return sp_shape_get_curve( (SPShape *) path );
506     }
509 /**
510  * Return a reference to original_curve if it exists or
511  * shape->curve if not.
512  */
513 const SPCurve*
514 sp_path_get_curve_reference (SPPath *path)
516     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
517                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
518         return path->original_curve;
519     } else {
520         return path->curve;
521     }
524 /*
525   Local Variables:
526   mode:c++
527   c-file-style:"stroustrup"
528   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
529   indent-tabs-mode:nil
530   fill-column:99
531   End:
532 */
533 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :