Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-path.cpp
1 /*
2  * SVG <path> implementation
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   David Turner <novalis@gnu.org>
7  *   Abhishek Sharma
8  *
9  * Copyright (C) 2004 David Turner
10  * Copyright (C) 1999-2002 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #include <glibmm/i18n.h>
22 #include "live_effects/effect.h"
23 #include "live_effects/lpeobject.h"
24 #include "live_effects/lpeobject-reference.h"
25 #include "sp-lpe-item.h"
27 #include <display/curve.h>
28 #include <libnr/nr-matrix-fns.h>
29 #include <2geom/pathvector.h>
30 #include <2geom/bezier-curve.h>
31 #include <2geom/hvlinesegment.h>
32 #include "helper/geom-curves.h"
34 #include "svg/svg.h"
35 #include "xml/repr.h"
36 #include "attributes.h"
38 #include "sp-path.h"
39 #include "sp-guide.h"
41 #include "document.h"
42 #include "desktop.h"
43 #include "desktop-handles.h"
44 #include "desktop-style.h"
45 #include "event-context.h"
46 #include "inkscape.h"
47 #include "style.h"
48 #include "message-stack.h"
49 #include "selection.h"
51 #define noPATH_VERBOSE
53 static void sp_path_class_init(SPPathClass *klass);
54 static void sp_path_init(SPPath *path);
55 static void sp_path_finalize(GObject *obj);
56 static void sp_path_release(SPObject *object);
58 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
59 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
61 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
62 static Geom::Matrix sp_path_set_transform(SPItem *item, Geom::Matrix const &xform);
63 static gchar * sp_path_description(SPItem *item);
64 static void sp_path_convert_to_guides(SPItem *item);
66 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
67 static void sp_path_update_patheffect(SPLPEItem *lpeitem, bool write);
69 static SPShapeClass *parent_class;
71 /**
72  * Gets the GType object for SPPathClass
73  */
74 GType
75 sp_path_get_type(void)
76 {
77     static GType type = 0;
79     if (!type) {
80         GTypeInfo info = {
81             sizeof(SPPathClass),
82             NULL, NULL,
83             (GClassInitFunc) sp_path_class_init,
84             NULL, NULL,
85             sizeof(SPPath),
86             16,
87             (GInstanceInitFunc) sp_path_init,
88             NULL,   /* value_table */
89         };
90         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
91     }
92     return type;
93 }
95 /**
96  *  Does the object-oriented work of initializing the class structure
97  *  including parent class, and registers function pointers for
98  *  the functions build, set, write, and set_transform.
99  */
100 static void
101 sp_path_class_init(SPPathClass * klass)
103     GObjectClass *gobject_class = (GObjectClass *) klass;
104     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
105     SPItemClass *item_class = (SPItemClass *) klass;
106     SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
108     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
110     gobject_class->finalize = sp_path_finalize;
112     sp_object_class->build = sp_path_build;
113     sp_object_class->release = sp_path_release;
114     sp_object_class->set = sp_path_set;
115     sp_object_class->write = sp_path_write;
116     sp_object_class->update = sp_path_update;
118     item_class->description = sp_path_description;
119     item_class->set_transform = sp_path_set_transform;
120     item_class->convert_to_guides = sp_path_convert_to_guides;
122     lpe_item_class->update_patheffect = sp_path_update_patheffect;
126 gint
127 sp_nodes_in_path(SPPath *path)
129     SPCurve *curve = SP_SHAPE(path)->curve;
130     if (!curve)
131         return 0;
132     return curve->nodes_in_path();
135 static gchar *
136 sp_path_description(SPItem * item)
138     int count = sp_nodes_in_path(SP_PATH(item));
139     if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
141         Glib::ustring s;
143         PathEffectList effect_list =  sp_lpe_item_get_effect_list(SP_LPE_ITEM(item));
144         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
145         {
146             LivePathEffectObject *lpeobj = (*it)->lpeobject;
147             if (!lpeobj || !lpeobj->get_lpe())
148                 break;
149             if (s.empty())
150                 s = lpeobj->get_lpe()->getName();
151             else
152                 s = s + ", " + lpeobj->get_lpe()->getName();
153         }
155         return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect: %s)",
156                                         "<b>Path</b> (%i nodes, path effect: %s)",count), count, s.c_str());
157     } else {
158         return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
159                                         "<b>Path</b> (%i nodes)",count), count);
160     }
163 static void
164 sp_path_convert_to_guides(SPItem *item)
166     SPPath *path = SP_PATH(item);
168     SPCurve *curve = SP_SHAPE(path)->curve;
169     if (!curve) return;
171     std::list<std::pair<Geom::Point, Geom::Point> > pts;
173     Geom::Matrix const i2d (SP_ITEM(path)->i2d_affine());
175     Geom::PathVector const & pv = curve->get_pathvector();
176     for(Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit) {
177         for(Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
178             // only add curves for straight line segments
179             if( is_straight_curve(*cit) )
180             {
181                 pts.push_back(std::make_pair(cit->initialPoint() * i2d, cit->finalPoint() * i2d));
182             }
183         }
184     }
186     sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
189 /**
190  * Initializes an SPPath.
191  */
192 static void
193 sp_path_init(SPPath *path)
195     new (&path->connEndPair) SPConnEndPair(path);
197     path->original_curve = NULL;
200 static void
201 sp_path_finalize(GObject *obj)
203     SPPath *path = (SPPath *) obj;
205     path->connEndPair.~SPConnEndPair();
208 /**
209  *  Given a repr, this sets the data items in the path object such as
210  *  fill & style attributes, markers, and CSS properties.
211  */
212 static void
213 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
215     /* Are these calls actually necessary? */
216     object->readAttr( "marker" );
217     object->readAttr( "marker-start" );
218     object->readAttr( "marker-mid" );
219     object->readAttr( "marker-end" );
221     sp_conn_end_pair_build(object);
223     if (((SPObjectClass *) parent_class)->build) {
224         ((SPObjectClass *) parent_class)->build(object, document, repr);
225     }
227     object->readAttr( "inkscape:original-d" );
228     object->readAttr( "d" );
230     /* d is a required attribute */
231     gchar const *d = object->getAttribute("d", NULL);
232     if (d == NULL) {
233         object->setKeyValue( sp_attribute_lookup("d"), "");
234     }
237 static void
238 sp_path_release(SPObject *object)
240     SPPath *path = SP_PATH(object);
242     path->connEndPair.release();
244     if (path->original_curve) {
245         path->original_curve = path->original_curve->unref();
246     }
248     if (((SPObjectClass *) parent_class)->release) {
249         ((SPObjectClass *) parent_class)->release(object);
250     }
253 /**
254  *  Sets a value in the path object given by 'key', to 'value'.  This is used
255  *  for setting attributes and markers on a path object.
256  */
257 static void
258 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
260     SPPath *path = (SPPath *) object;
262     switch (key) {
263         case SP_ATTR_INKSCAPE_ORIGINAL_D:
264                 if (value) {
265                     Geom::PathVector pv = sp_svg_read_pathv(value);
266                     SPCurve *curve = new SPCurve(pv);
267                     if (curve) {
268                         sp_path_set_original_curve(path, curve, TRUE, true);
269                         curve->unref();
270                     }
271                 } else {
272                     sp_path_set_original_curve(path, NULL, TRUE, true);
273                 }
274                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
275             break;
276        case SP_ATTR_D:
277                 if (value) {
278                     Geom::PathVector pv = sp_svg_read_pathv(value);
279                     SPCurve *curve = new SPCurve(pv);
280                     if (curve) {
281                         ((SPShape *) path)->setCurve(curve, TRUE);
282                         curve->unref();
283                     }
284                 } else {
285                     ((SPShape *) path)->setCurve(NULL, TRUE);
286                 }
287                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
288             break;
289         case SP_PROP_MARKER:
290         case SP_PROP_MARKER_START:
291         case SP_PROP_MARKER_MID:
292         case SP_PROP_MARKER_END:
293             sp_shape_set_marker(object, key, value);
294             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
295             break;
296         case SP_ATTR_CONNECTOR_TYPE:
297         case SP_ATTR_CONNECTOR_CURVATURE:
298         case SP_ATTR_CONNECTION_START:
299         case SP_ATTR_CONNECTION_END:
300         case SP_ATTR_CONNECTION_START_POINT:
301         case SP_ATTR_CONNECTION_END_POINT:
302             path->connEndPair.setAttr(key, value);
303             break;
304         default:
305             if (((SPObjectClass *) parent_class)->set) {
306                 ((SPObjectClass *) parent_class)->set(object, key, value);
307             }
308             break;
309     }
312 /**
313  *
314  * Writes the path object into a Inkscape::XML::Node
315  */
316 static Inkscape::XML::Node *
317 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
319     SPShape *shape = (SPShape *) object;
321     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
322         repr = xml_doc->createElement("svg:path");
323     }
325 #ifdef PATH_VERBOSE
326 g_message("sp_path_write writes 'd' attribute");
327 #endif
328     if ( shape->curve != NULL ) {
329         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
330         repr->setAttribute("d", str);
331         g_free(str);
332     } else {
333         repr->setAttribute("d", NULL);
334     }
336     if (flags & SP_OBJECT_WRITE_EXT) {
337         SPPath *path = (SPPath *) object;
338         if ( path->original_curve != NULL ) {
339             gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
340             repr->setAttribute("inkscape:original-d", str);
341             g_free(str);
342         } else {
343             repr->setAttribute("inkscape:original-d", NULL);
344         }
345     }
347     SP_PATH(shape)->connEndPair.writeRepr(repr);
349     if (((SPObjectClass *)(parent_class))->write) {
350         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
351     }
353     return repr;
356 static void
357 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
359     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
360         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
361     }
363     if (((SPObjectClass *) parent_class)->update) {
364         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
365     }
367     SPPath *path = SP_PATH(object);
368     path->connEndPair.update();
372 /**
373  * Writes the given transform into the repr for the given item.
374  */
375 static Geom::Matrix
376 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
378     SPShape *shape = (SPShape *) item;
379     SPPath *path = (SPPath *) item;
381     if (!shape->curve) { // 0 nodes, nothing to transform
382         return Geom::identity();
383     }
385     // Transform the original-d path if this is a valid LPE item, other else the (ordinary) path
386     if (path->original_curve && SP_IS_LPE_ITEM(item) && 
387                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
388         path->original_curve->transform(xform);
389     } else {
390         shape->curve->transform(xform);
391     }
393     // Adjust stroke
394     item->adjust_stroke(xform.descrim());
396     // Adjust pattern fill
397     item->adjust_pattern(xform);
399     // Adjust gradient fill
400     item->adjust_gradient(xform);
402     // Adjust LPE
403     item->adjust_livepatheffect(xform);
405     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
407     // nothing remains - we've written all of the transform, so return identity
408     return Geom::identity();
412 static void
413 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
415     SPShape * const shape = (SPShape *) lpeitem;
416     SPPath * const path = (SPPath *) lpeitem;
417     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
419 #ifdef PATH_VERBOSE
420 g_message("sp_path_update_patheffect");
421 #endif
423     if (path->original_curve && sp_lpe_item_has_path_effect_recursive(lpeitem)) {
424         SPCurve *curve = path->original_curve->copy();
425         /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
426          * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
427         shape->setCurveInsync(curve, TRUE);
429         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
430         if (success && write) {
431             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
432 #ifdef PATH_VERBOSE
433 g_message("sp_path_update_patheffect writes 'd' attribute");
434 #endif
435             if ( shape->curve != NULL ) {
436                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
437                 repr->setAttribute("d", str);
438                 g_free(str);
439             } else {
440                 repr->setAttribute("d", NULL);
441             }
442         } else if (!success) {
443             // LPE was unsuccesfull. Read the old 'd'-attribute.
444             if (gchar const * value = repr->attribute("d")) {
445                 Geom::PathVector pv = sp_svg_read_pathv(value);
446                 SPCurve *oldcurve = new SPCurve(pv);
447                 if (oldcurve) {
448                     shape->setCurve(oldcurve, TRUE);
449                     oldcurve->unref();
450                 }
451             }
452         }
453         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
454         curve->unref();
455     }
459 /**
460  * Adds a original_curve to the path.  If owner is specified, a reference
461  * will be made, otherwise the curve will be copied into the path.
462  * Any existing curve in the path will be unreferenced first.
463  * This routine triggers reapplication of an effect if present
464  * and also triggers a request to update the display. Does not write
465  * result to XML when write=false.
466  */
467 void
468 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
470     if (path->original_curve) {
471         path->original_curve = path->original_curve->unref();
472     }
473     if (curve) {
474         if (owner) {
475             path->original_curve = curve->ref();
476         } else {
477             path->original_curve = curve->copy();
478         }
479     }
480     sp_lpe_item_update_patheffect(path, true, write);
481     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
484 /**
485  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
486  */
487 SPCurve *
488 sp_path_get_original_curve (SPPath *path)
490     if (path->original_curve) {
491         return path->original_curve->copy();
492     }
493     return NULL;
496 /**
497  * Return duplicate of edittable curve which is original_curve if it exists or
498  * shape->curve if not.
499  */
500 SPCurve*
501 sp_path_get_curve_for_edit (SPPath *path)
503     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
504                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
505         return sp_path_get_original_curve(path);
506     } else {
507         return ((SPShape *) path)->getCurve();
508     }
511 /**
512  * Return a reference to original_curve if it exists or
513  * shape->curve if not.
514  */
515 const SPCurve*
516 sp_path_get_curve_reference (SPPath *path)
518     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
519                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
520         return path->original_curve;
521     } else {
522         return path->curve;
523     }
526 /*
527   Local Variables:
528   mode:c++
529   c-file-style:"stroustrup"
530   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
531   indent-tabs-mode:nil
532   fill-column:99
533   End:
534 */
535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :