Code

A simple layout document as to what, why and how is cppification.
[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(path)->i2d_affine());
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     object->readAttr( "marker");
218     object->readAttr( "marker-start");
219     object->readAttr( "marker-mid");
220     object->readAttr( "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     object->readAttr( "inkscape:original-d");
229     object->readAttr( "d");
231     /* d is a required attribute */
232     gchar const *d = object->getAttribute("d", NULL);
233     if (d == NULL) {
234         object->setKeyValue( 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                         ((SPShape *) path)->setCurve(curve, TRUE);
283                         curve->unref();
284                     }
285                 } else {
286                     ((SPShape *) path)->setCurve(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         case SP_ATTR_CONNECTION_START_POINT:
302         case SP_ATTR_CONNECTION_END_POINT:
303             path->connEndPair.setAttr(key, value);
304             break;
305         default:
306             if (((SPObjectClass *) parent_class)->set) {
307                 ((SPObjectClass *) parent_class)->set(object, key, value);
308             }
309             break;
310     }
313 /**
314  *
315  * Writes the path object into a Inkscape::XML::Node
316  */
317 static Inkscape::XML::Node *
318 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
320     SPShape *shape = (SPShape *) object;
322     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
323         repr = xml_doc->createElement("svg:path");
324     }
326 #ifdef PATH_VERBOSE
327 g_message("sp_path_write writes 'd' attribute");
328 #endif
329     if ( shape->curve != NULL ) {
330         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
331         repr->setAttribute("d", str);
332         g_free(str);
333     } else {
334         repr->setAttribute("d", NULL);
335     }
337     if (flags & SP_OBJECT_WRITE_EXT) {
338         SPPath *path = (SPPath *) object;
339         if ( path->original_curve != NULL ) {
340             gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
341             repr->setAttribute("inkscape:original-d", str);
342             g_free(str);
343         } else {
344             repr->setAttribute("inkscape:original-d", NULL);
345         }
346     }
348     SP_PATH(shape)->connEndPair.writeRepr(repr);
350     if (((SPObjectClass *)(parent_class))->write) {
351         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
352     }
354     return repr;
357 static void
358 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
360     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
361         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
362     }
364     if (((SPObjectClass *) parent_class)->update) {
365         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
366     }
368     SPPath *path = SP_PATH(object);
369     path->connEndPair.update();
373 /**
374  * Writes the given transform into the repr for the given item.
375  */
376 static Geom::Matrix
377 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
379     SPShape *shape = (SPShape *) item;
380     SPPath *path = (SPPath *) item;
382     if (!shape->curve) { // 0 nodes, nothing to transform
383         return Geom::identity();
384     }
386     // Transform the original-d path if this is a valid LPE item, other else the (ordinary) path
387     if (path->original_curve && SP_IS_LPE_ITEM(item) && 
388                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
389         path->original_curve->transform(xform);
390     } else {
391         shape->curve->transform(xform);
392     }
394     // Adjust stroke
395     item->adjust_stroke(xform.descrim());
397     // Adjust pattern fill
398     item->adjust_pattern(xform);
400     // Adjust gradient fill
401     item->adjust_gradient(xform);
403     // Adjust LPE
404     item->adjust_livepatheffect(xform);
406     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
408     // nothing remains - we've written all of the transform, so return identity
409     return Geom::identity();
413 static void
414 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
416     SPShape * const shape = (SPShape *) lpeitem;
417     SPPath * const path = (SPPath *) lpeitem;
418     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
420 #ifdef PATH_VERBOSE
421 g_message("sp_path_update_patheffect");
422 #endif
424     if (path->original_curve && sp_lpe_item_has_path_effect_recursive(lpeitem)) {
425         SPCurve *curve = path->original_curve->copy();
426         /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
427          * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
428         shape->setCurveInsync(curve, TRUE);
430         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
431         if (success && write) {
432             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
433 #ifdef PATH_VERBOSE
434 g_message("sp_path_update_patheffect writes 'd' attribute");
435 #endif
436             if ( shape->curve != NULL ) {
437                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
438                 repr->setAttribute("d", str);
439                 g_free(str);
440             } else {
441                 repr->setAttribute("d", NULL);
442             }
443         } else if (!success) {
444             // LPE was unsuccesfull. Read the old 'd'-attribute.
445             if (gchar const * value = repr->attribute("d")) {
446                 Geom::PathVector pv = sp_svg_read_pathv(value);
447                 SPCurve *oldcurve = new SPCurve(pv);
448                 if (oldcurve) {
449                     shape->setCurve(oldcurve, TRUE);
450                     oldcurve->unref();
451                 }
452             }
453         }
454         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
455         curve->unref();
456     }
460 /**
461  * Adds a original_curve to the path.  If owner is specified, a reference
462  * will be made, otherwise the curve will be copied into the path.
463  * Any existing curve in the path will be unreferenced first.
464  * This routine triggers reapplication of an effect if present
465  * and also triggers a request to update the display. Does not write
466  * result to XML when write=false.
467  */
468 void
469 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
471     if (path->original_curve) {
472         path->original_curve = path->original_curve->unref();
473     }
474     if (curve) {
475         if (owner) {
476             path->original_curve = curve->ref();
477         } else {
478             path->original_curve = curve->copy();
479         }
480     }
481     sp_lpe_item_update_patheffect(path, true, write);
482     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
485 /**
486  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
487  */
488 SPCurve *
489 sp_path_get_original_curve (SPPath *path)
491     if (path->original_curve) {
492         return path->original_curve->copy();
493     }
494     return NULL;
497 /**
498  * Return duplicate of edittable curve which is original_curve if it exists or
499  * shape->curve if not.
500  */
501 SPCurve*
502 sp_path_get_curve_for_edit (SPPath *path)
504     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
505                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
506         return sp_path_get_original_curve(path);
507     } else {
508         return ((SPShape *) path)->getCurve();
509     }
512 /**
513  * Return a reference to original_curve if it exists or
514  * shape->curve if not.
515  */
516 const SPCurve*
517 sp_path_get_curve_reference (SPPath *path)
519     if (path->original_curve && SP_IS_LPE_ITEM(path) && 
520                                 sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
521         return path->original_curve;
522     } else {
523         return path->curve;
524     }
527 /*
528   Local Variables:
529   mode:c++
530   c-file-style:"stroustrup"
531   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
532   indent-tabs-mode:nil
533   fill-column:99
534   End:
535 */
536 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :