Code

e0e10b6deba7c740fab38769eb3d859766576bb6
[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 <display/curve.h>
24 #include <libnr/nr-matrix-fns.h>
25 #include <2geom/pathvector.h>
26 #include <2geom/bezier-curve.h>
27 #include <2geom/hvlinesegment.h>
28 #include "helper/geom-curves.h"
30 #include "svg/svg.h"
31 #include "xml/repr.h"
32 #include "attributes.h"
34 #include "sp-path.h"
35 #include "sp-guide.h"
37 #include "document.h"
38 #include "desktop.h"
39 #include "desktop-handles.h"
40 #include "desktop-style.h"
41 #include "event-context.h"
42 #include "inkscape.h"
43 #include "style.h"
44 #include "message-stack.h"
45 #include "prefs-utils.h"
46 #include "selection.h"
48 #define noPATH_VERBOSE
50 static void sp_path_class_init(SPPathClass *klass);
51 static void sp_path_init(SPPath *path);
52 static void sp_path_finalize(GObject *obj);
53 static void sp_path_release(SPObject *object);
55 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
56 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
58 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
59 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
60 static gchar * sp_path_description(SPItem *item);
61 static void sp_path_convert_to_guides(SPItem *item);
63 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
64 static void sp_path_update_patheffect(SPLPEItem *lpeitem, bool write);
66 static SPShapeClass *parent_class;
68 /**
69  * Gets the GType object for SPPathClass
70  */
71 GType
72 sp_path_get_type(void)
73 {
74     static GType type = 0;
76     if (!type) {
77         GTypeInfo info = {
78             sizeof(SPPathClass),
79             NULL, NULL,
80             (GClassInitFunc) sp_path_class_init,
81             NULL, NULL,
82             sizeof(SPPath),
83             16,
84             (GInstanceInitFunc) sp_path_init,
85             NULL,   /* value_table */
86         };
87         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
88     }
89     return type;
90 }
92 /**
93  *  Does the object-oriented work of initializing the class structure
94  *  including parent class, and registers function pointers for
95  *  the functions build, set, write, and set_transform.
96  */
97 static void
98 sp_path_class_init(SPPathClass * klass)
99 {
100     GObjectClass *gobject_class = (GObjectClass *) klass;
101     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
102     SPItemClass *item_class = (SPItemClass *) klass;
103     SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
105     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
107     gobject_class->finalize = sp_path_finalize;
109     sp_object_class->build = sp_path_build;
110     sp_object_class->release = sp_path_release;
111     sp_object_class->set = sp_path_set;
112     sp_object_class->write = sp_path_write;
113     sp_object_class->update = sp_path_update;
115     item_class->description = sp_path_description;
116     item_class->set_transform = sp_path_set_transform;
117     item_class->convert_to_guides = sp_path_convert_to_guides;
119     lpe_item_class->update_patheffect = sp_path_update_patheffect;
123 gint
124 sp_nodes_in_path(SPPath *path)
126     SPCurve *curve = SP_SHAPE(path)->curve;
127     if (!curve)
128         return 0;
129     return curve->nodes_in_path();
132 static gchar *
133 sp_path_description(SPItem * item)
135     int count = sp_nodes_in_path(SP_PATH(item));
136     if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
137         return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect)",
138                                         "<b>Path</b> (%i nodes, path effect)",count), count);
139     } else {
140         return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
141                                         "<b>Path</b> (%i nodes)",count), count);
142     }
145 static void
146 sp_path_convert_to_guides(SPItem *item)
148     SPPath *path = SP_PATH(item);
150     SPCurve *curve = SP_SHAPE(path)->curve;
151     if (!curve) return;
153     std::list<std::pair<Geom::Point, Geom::Point> > pts;
155     Geom::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
157     Geom::PathVector const & pv = curve->get_pathvector();
158     for(Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit) {
159         for(Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
160             // only add curves for straight line segments
161             if( is_straight_curve(*cit) )
162             {
163                 pts.push_back(std::make_pair(cit->initialPoint() * i2d, cit->finalPoint() * i2d));
164             }
165         }
166     }
168     sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
171 /**
172  * Initializes an SPPath.
173  */
174 static void
175 sp_path_init(SPPath *path)
177     new (&path->connEndPair) SPConnEndPair(path);
179     path->original_curve = NULL;
182 static void
183 sp_path_finalize(GObject *obj)
185     SPPath *path = (SPPath *) obj;
187     path->connEndPair.~SPConnEndPair();
190 /**
191  *  Given a repr, this sets the data items in the path object such as
192  *  fill & style attributes, markers, and CSS properties.
193  */
194 static void
195 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
197     /* Are these calls actually necessary? */
198     sp_object_read_attr(object, "marker");
199     sp_object_read_attr(object, "marker-start");
200     sp_object_read_attr(object, "marker-mid");
201     sp_object_read_attr(object, "marker-end");
203     sp_conn_end_pair_build(object);
205     if (((SPObjectClass *) parent_class)->build) {
206         ((SPObjectClass *) parent_class)->build(object, document, repr);
207     }
209     sp_object_read_attr(object, "inkscape:original-d");
210     sp_object_read_attr(object, "d");
212     /* d is a required attribute */
213     gchar const *d = sp_object_getAttribute(object, "d", NULL);
214     if (d == NULL) {
215         sp_object_set(object, sp_attribute_lookup("d"), "");
216     }
219 static void
220 sp_path_release(SPObject *object)
222     SPPath *path = SP_PATH(object);
224     path->connEndPair.release();
226     if (path->original_curve) {
227         path->original_curve = path->original_curve->unref();
228     }
230     if (((SPObjectClass *) parent_class)->release) {
231         ((SPObjectClass *) parent_class)->release(object);
232     }
235 /**
236  *  Sets a value in the path object given by 'key', to 'value'.  This is used
237  *  for setting attributes and markers on a path object.
238  */
239 static void
240 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
242     SPPath *path = (SPPath *) object;
244     switch (key) {
245         case SP_ATTR_INKSCAPE_ORIGINAL_D:
246                 if (value) {
247                     Geom::PathVector pv = sp_svg_read_pathv(value);
248                     SPCurve *curve = new SPCurve(pv);
249                     if (curve) {
250                         sp_path_set_original_curve(path, curve, TRUE, true);
251                         curve->unref();
252                     }
253                 } else {
254                     sp_path_set_original_curve(path, NULL, TRUE, true);
255                 }
256                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
257             break;
258        case SP_ATTR_D:
259             if (!sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
260                 if (value) {
261                     Geom::PathVector pv = sp_svg_read_pathv(value);
262                     SPCurve *curve = new SPCurve(pv);
263                     if (curve) {
264                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
265                         curve->unref();
266                     }
267                 } else {
268                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
269                 }
270                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
271             }
272             break;
273         case SP_PROP_MARKER:
274         case SP_PROP_MARKER_START:
275         case SP_PROP_MARKER_MID:
276         case SP_PROP_MARKER_END:
277             sp_shape_set_marker(object, key, value);
278             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
279             break;
280         case SP_ATTR_CONNECTOR_TYPE:
281         case SP_ATTR_CONNECTION_START:
282         case SP_ATTR_CONNECTION_END:
283             path->connEndPair.setAttr(key, value);
284             break;
285         default:
286             if (((SPObjectClass *) parent_class)->set) {
287                 ((SPObjectClass *) parent_class)->set(object, key, value);
288             }
289             break;
290     }
293 /**
294  *
295  * Writes the path object into a Inkscape::XML::Node
296  */
297 static Inkscape::XML::Node *
298 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
300     SPShape *shape = (SPShape *) object;
302     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
303         repr = xml_doc->createElement("svg:path");
304     }
306     if ( shape->curve != NULL ) {
307         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
308         repr->setAttribute("d", str);
309         g_free(str);
310     } else {
311         repr->setAttribute("d", NULL);
312     }
314     SPPath *path = (SPPath *) object;
315     if ( path->original_curve != NULL ) {
316         gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
317         repr->setAttribute("inkscape:original-d", str);
318         g_free(str);
319     } else {
320         repr->setAttribute("inkscape:original-d", NULL);
321     }
323     SP_PATH(shape)->connEndPair.writeRepr(repr);
325     if (((SPObjectClass *)(parent_class))->write) {
326         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
327     }
329     return repr;
332 static void
333 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
335     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
336         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
337     }
339     if (((SPObjectClass *) parent_class)->update) {
340         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
341     }
343     SPPath *path = SP_PATH(object);
344     path->connEndPair.update();
348 /**
349  * Writes the given transform into the repr for the given item.
350  */
351 static NR::Matrix
352 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
354     SPShape *shape = (SPShape *) item;
355     SPPath *path = (SPPath *) item;
357     if (!shape->curve) { // 0 nodes, nothing to transform
358         return NR::identity();
359     }
361     // Transform the original-d path or the (ordinary) path
362     if (path->original_curve) {
363         path->original_curve->transform(to_2geom(xform));
364         sp_lpe_item_update_patheffect(path, true, true);
365     } else {
366         shape->curve->transform(to_2geom(xform));
367     }
369     // Adjust stroke
370     sp_item_adjust_stroke(item, NR::expansion(xform));
372     // Adjust pattern fill
373     sp_item_adjust_pattern(item, xform);
375     // Adjust gradient fill
376     sp_item_adjust_gradient(item, xform);
378     // Adjust LPE
379     sp_item_adjust_livepatheffect(item, xform);
381     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
383     // nothing remains - we've written all of the transform, so return identity
384     return NR::identity();
387 static void
388 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
390     SPShape *shape = (SPShape *) lpeitem;
391     SPPath *path = (SPPath *) lpeitem;
392     if (path->original_curve) {
393         SPCurve *curve = path->original_curve->copy();
394         sp_shape_set_curve_insync(shape, curve, TRUE);
395         sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
396         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
397         curve->unref();
399         if (write) {
400             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
401             Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
402             if ( shape->curve != NULL ) {
403                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
404                 repr->setAttribute("d", str);
405                 g_free(str);
406             } else {
407                 repr->setAttribute("d", NULL);
408             }
409         }
410     } else {
412     }
416 /**
417  * Adds a original_curve to the path.  If owner is specified, a reference
418  * will be made, otherwise the curve will be copied into the path.
419  * Any existing curve in the path will be unreferenced first.
420  * This routine triggers reapplication of an effect if present
421  * and also triggers a request to update the display. Does not write
422  * result to XML when write=false.
423  */
424 void
425 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
427     if (path->original_curve) {
428         path->original_curve = path->original_curve->unref();
429     }
430     if (curve) {
431         if (owner) {
432             path->original_curve = curve->ref();
433         } else {
434             path->original_curve = curve->copy();
435         }
436     }
437     sp_lpe_item_update_patheffect(path, true, write);
438     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
441 /**
442  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
443  */
444 SPCurve *
445 sp_path_get_original_curve (SPPath *path)
447     if (path->original_curve) {
448         return path->original_curve->copy();
449     }
450     return NULL;
453 /**
454  * Return duplicate of edittable curve which is original_curve if it exists or
455  * shape->curve if not.
456  */
457 SPCurve*
458 sp_path_get_curve_for_edit (SPPath *path)
460     if (path->original_curve) {
461         return sp_path_get_original_curve(path);
462     } else {
463         return sp_shape_get_curve( (SPShape *) path );
464     }
467 /**
468  * Return a reference to original_curve if it exists or
469  * shape->curve if not.
470  */
471 const SPCurve*
472 sp_path_get_curve_reference (SPPath *path)
474     if (path->original_curve) {
475         return path->original_curve;
476     } else {
477         return path->curve;
478     }
481 /* Create a single dot represented by a circle */
482 void freehand_create_single_dot(SPEventContext *ec, NR::Point const &pt, char const *tool, guint event_state) {
483     g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
485     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
486     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
487     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
488     repr->setAttribute("sodipodi:type", "arc");
489     SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
490     Inkscape::GC::release(repr);
492     /* apply the tool's current style */
493     sp_desktop_apply_style_tool(desktop, repr, tool, false);
495     /* find out stroke width (TODO: is there an easier way??) */
496     double stroke_width = 3.0;
497     gchar const *style_str = NULL;
498     style_str = repr->attribute("style");
499     if (style_str) {
500         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
501         sp_style_merge_from_style_string(style, style_str);
502         stroke_width = style->stroke_width.computed;
503         style->stroke_width.computed = 0;
504         sp_style_unref(style);
505     }
506     /* unset stroke and set fill color to former stroke color */
507     gchar * str;
508     str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
509     repr->setAttribute("style", str);
510     g_free(str);
512     /* put the circle where the mouse click occurred and set the diameter to the
513        current stroke width, multiplied by the amount specified in the preferences */
514     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
515     NR::Point pp = pt * i2d;
516     double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
517     if (event_state & GDK_MOD1_MASK) {
518         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
519            as specified in prefs. Very simple, but it might be sufficient in practice. If not,
520            we need to devise something more sophisticated. */
521         double s = g_random_double_range(-0.5, 0.5);
522         rad *= (1 + s);
523     }
524     if (event_state & GDK_SHIFT_MASK) {
525         // double the point size
526         rad *= 2;
527     }
529     sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
530     sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
531     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
532     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
533     item->updateRepr();
535     sp_desktop_selection(desktop)->set(item);
537     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
538     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
541 /*
542   Local Variables:
543   mode:c++
544   c-file-style:"stroustrup"
545   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
546   indent-tabs-mode:nil
547   fill-column:99
548   End:
549 */
550 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :