Code

widgets/select-toolbar.h: Supply missing #includes/declarations so that we don't...
[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/n-art-bpath.h>
25 #include <libnr/nr-path.h>
26 #include <libnr/nr-matrix-fns.h>
27 #include <2geom/pathvector.h>
29 #include "svg/svg.h"
30 #include "xml/repr.h"
31 #include "attributes.h"
33 #include "sp-path.h"
34 #include "sp-guide.h"
36 #include "document.h"
37 #include "desktop.h"
38 #include "desktop-handles.h"
39 #include "desktop-style.h"
40 #include "event-context.h"
41 #include "inkscape.h"
42 #include "style.h"
43 #include "message-stack.h"
44 #include "prefs-utils.h"
45 #include "selection.h"
47 #define noPATH_VERBOSE
49 static void sp_path_class_init(SPPathClass *klass);
50 static void sp_path_init(SPPath *path);
51 static void sp_path_finalize(GObject *obj);
52 static void sp_path_release(SPObject *object);
54 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
55 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
57 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
58 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
59 static gchar * sp_path_description(SPItem *item);
60 static void sp_path_convert_to_guides(SPItem *item);
62 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
63 static void sp_path_update_patheffect(SPLPEItem *lpeitem, bool write);
65 static SPShapeClass *parent_class;
67 /**
68  * Gets the GType object for SPPathClass
69  */
70 GType
71 sp_path_get_type(void)
72 {
73     static GType type = 0;
75     if (!type) {
76         GTypeInfo info = {
77             sizeof(SPPathClass),
78             NULL, NULL,
79             (GClassInitFunc) sp_path_class_init,
80             NULL, NULL,
81             sizeof(SPPath),
82             16,
83             (GInstanceInitFunc) sp_path_init,
84             NULL,   /* value_table */
85         };
86         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
87     }
88     return type;
89 }
91 /**
92  *  Does the object-oriented work of initializing the class structure
93  *  including parent class, and registers function pointers for
94  *  the functions build, set, write, and set_transform.
95  */
96 static void
97 sp_path_class_init(SPPathClass * klass)
98 {
99     GObjectClass *gobject_class = (GObjectClass *) klass;
100     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
101     SPItemClass *item_class = (SPItemClass *) klass;
102     SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
104     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
106     gobject_class->finalize = sp_path_finalize;
108     sp_object_class->build = sp_path_build;
109     sp_object_class->release = sp_path_release;
110     sp_object_class->set = sp_path_set;
111     sp_object_class->write = sp_path_write;
112     sp_object_class->update = sp_path_update;
114     item_class->description = sp_path_description;
115     item_class->set_transform = sp_path_set_transform;
116     item_class->convert_to_guides = sp_path_convert_to_guides;
118     lpe_item_class->update_patheffect = sp_path_update_patheffect;
122 gint
123 sp_nodes_in_path(SPPath *path)
125     SPCurve *curve = SP_SHAPE(path)->curve;
126     if (!curve)
127         return 0;
128     return curve->nodes_in_path();
131 static gchar *
132 sp_path_description(SPItem * item)
134     int count = sp_nodes_in_path(SP_PATH(item));
135     if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(item))) {
136         return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect)",
137                                         "<b>Path</b> (%i nodes, path effect)",count), count);
138     } else {
139         return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
140                                         "<b>Path</b> (%i nodes)",count), count);
141     }
144 static void
145 sp_path_convert_to_guides(SPItem *item)
147     SPPath *path = SP_PATH(item);
149     SPCurve *curve = SP_SHAPE(path)->curve;
150     if (!curve) return;
152     std::list<std::pair<Geom::Point, Geom::Point> > pts;
154     Geom::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
156     Geom::PathVector const & pv = curve->get_pathvector();
157     for(Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit) {
158         for(Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
159             // only add curves for straight line segments
160             if( dynamic_cast<Geom::LineSegment const *>(&*cit) ||
161                 dynamic_cast<Geom::HLineSegment const *>(&*cit) ||
162                 dynamic_cast<Geom::VLineSegment const *>(&*cit) )
163             {
164                 pts.push_back(std::make_pair(cit->initialPoint() * i2d, cit->finalPoint() * i2d));
165             }
166         }
167     }
169     SPDocument *doc = SP_OBJECT_DOCUMENT(path);
170     sp_guide_pt_pairs_to_guides(doc, pts);
173 /**
174  * Initializes an SPPath.
175  */
176 static void
177 sp_path_init(SPPath *path)
179     new (&path->connEndPair) SPConnEndPair(path);
181     path->original_curve = NULL;
184 static void
185 sp_path_finalize(GObject *obj)
187     SPPath *path = (SPPath *) obj;
189     path->connEndPair.~SPConnEndPair();
192 /**
193  *  Given a repr, this sets the data items in the path object such as
194  *  fill & style attributes, markers, and CSS properties.
195  */
196 static void
197 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
199     /* Are these calls actually necessary? */
200     sp_object_read_attr(object, "marker");
201     sp_object_read_attr(object, "marker-start");
202     sp_object_read_attr(object, "marker-mid");
203     sp_object_read_attr(object, "marker-end");
205     sp_conn_end_pair_build(object);
207     if (((SPObjectClass *) parent_class)->build) {
208         ((SPObjectClass *) parent_class)->build(object, document, repr);
209     }
211     sp_object_read_attr(object, "inkscape:original-d");
212     sp_object_read_attr(object, "d");
214     /* d is a required attribute */
215     gchar const *d = sp_object_getAttribute(object, "d", NULL);
216     if (d == NULL) {
217         sp_object_set(object, sp_attribute_lookup("d"), "");
218     }
221 static void
222 sp_path_release(SPObject *object)
224     SPPath *path = SP_PATH(object);
226     path->connEndPair.release();
228     if (path->original_curve) {
229         path->original_curve = path->original_curve->unref();
230     }
232     if (((SPObjectClass *) parent_class)->release) {
233         ((SPObjectClass *) parent_class)->release(object);
234     }
237 /**
238  *  Sets a value in the path object given by 'key', to 'value'.  This is used
239  *  for setting attributes and markers on a path object.
240  */
241 static void
242 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
244     SPPath *path = (SPPath *) object;
246     switch (key) {
247         case SP_ATTR_INKSCAPE_ORIGINAL_D:
248                 if (value) {
249                     Geom::PathVector pv = sp_svg_read_pathv(value);
250                     SPCurve *curve = new SPCurve(pv);
251                     if (curve) {
252                         sp_path_set_original_curve(path, curve, TRUE, true);
253                         curve->unref();
254                     }
255                 } else {
256                     sp_path_set_original_curve(path, NULL, TRUE, true);
257                 }
258                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
259             break;
260        case SP_ATTR_D:
261             if (!sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
262                 if (value) {
263                     Geom::PathVector pv = sp_svg_read_pathv(value);
264                     SPCurve *curve = new SPCurve(pv);
265                     if (curve) {
266                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
267                         curve->unref();
268                     }
269                 } else {
270                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
271                 }
272                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
273             }
274             break;
275         case SP_PROP_MARKER:
276         case SP_PROP_MARKER_START:
277         case SP_PROP_MARKER_MID:
278         case SP_PROP_MARKER_END:
279             sp_shape_set_marker(object, key, value);
280             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
281             break;
282         case SP_ATTR_CONNECTOR_TYPE:
283         case SP_ATTR_CONNECTION_START:
284         case SP_ATTR_CONNECTION_END:
285             path->connEndPair.setAttr(key, value);
286             break;
287         default:
288             if (((SPObjectClass *) parent_class)->set) {
289                 ((SPObjectClass *) parent_class)->set(object, key, value);
290             }
291             break;
292     }
295 /**
296  *
297  * Writes the path object into a Inkscape::XML::Node
298  */
299 static Inkscape::XML::Node *
300 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
302     SPShape *shape = (SPShape *) object;
304     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
305         repr = xml_doc->createElement("svg:path");
306     }
308     if ( shape->curve != NULL ) {
309         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
310         repr->setAttribute("d", str);
311         g_free(str);
312     } else {
313         repr->setAttribute("d", NULL);
314     }
316     SPPath *path = (SPPath *) object;
317     if ( path->original_curve != NULL ) {
318         gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
319         repr->setAttribute("inkscape:original-d", str);
320         g_free(str);
321     } else {
322         repr->setAttribute("inkscape:original-d", NULL);
323     }
325     SP_PATH(shape)->connEndPair.writeRepr(repr);
327     if (((SPObjectClass *)(parent_class))->write) {
328         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
329     }
331     return repr;
334 static void
335 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
337     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
338         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
339     }
341     if (((SPObjectClass *) parent_class)->update) {
342         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
343     }
345     SPPath *path = SP_PATH(object);
346     path->connEndPair.update();
350 /**
351  * Writes the given transform into the repr for the given item.
352  */
353 static NR::Matrix
354 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
356     SPShape *shape = (SPShape *) item;
357     SPPath *path = (SPPath *) item;
359     if (!shape->curve) { // 0 nodes, nothing to transform
360         return NR::identity();
361     }
363     // Transform the original-d path or the (ordinary) path
364     if (path->original_curve) {
365         path->original_curve->transform(xform);
366         sp_lpe_item_update_patheffect(path, true, true);
367     } else {
368         shape->curve->transform(xform);
369     }
371     // Adjust stroke
372     sp_item_adjust_stroke(item, NR::expansion(xform));
374     // Adjust pattern fill
375     sp_item_adjust_pattern(item, xform);
377     // Adjust gradient fill
378     sp_item_adjust_gradient(item, xform);
380     // Adjust LPE
381     sp_item_adjust_livepatheffect(item, xform);
383     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
385     // nothing remains - we've written all of the transform, so return identity
386     return NR::identity();
389 static void
390 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
392     SPShape *shape = (SPShape *) lpeitem;
393     SPPath *path = (SPPath *) lpeitem;
394     if (path->original_curve) {
395         SPCurve *curve = path->original_curve->copy();
396         sp_shape_set_curve_insync(shape, curve, TRUE);
397         sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
398         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
399         curve->unref();
401         if (write) {
402             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
403             Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
404             if ( shape->curve != NULL ) {
405                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
406                 repr->setAttribute("d", str);
407                 g_free(str);
408             } else {
409                 repr->setAttribute("d", NULL);
410             }
411         }
412     } else {
414     }
418 /**
419  * Adds a original_curve to the path.  If owner is specified, a reference
420  * will be made, otherwise the curve will be copied into the path.
421  * Any existing curve in the path will be unreferenced first.
422  * This routine triggers reapplication of an effect if present
423  * and also triggers a request to update the display. Does not write
424  * result to XML when write=false.
425  */
426 void
427 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
429     if (path->original_curve) {
430         path->original_curve = path->original_curve->unref();
431     }
432     if (curve) {
433         if (owner) {
434             path->original_curve = curve->ref();
435         } else {
436             path->original_curve = curve->copy();
437         }
438     }
439     sp_lpe_item_update_patheffect(path, true, write);
440     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
443 /**
444  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
445  */
446 SPCurve *
447 sp_path_get_original_curve (SPPath *path)
449     if (path->original_curve) {
450         return path->original_curve->copy();
451     }
452     return NULL;
455 /**
456  * Return duplicate of edittable curve which is original_curve if it exists or
457  * shape->curve if not.
458  */
459 SPCurve*
460 sp_path_get_curve_for_edit (SPPath *path)
462     if (path->original_curve) {
463         return sp_path_get_original_curve(path);
464     } else {
465         return sp_shape_get_curve( (SPShape *) path );
466     }
469 /**
470  * Return a reference to original_curve if it exists or
471  * shape->curve if not.
472  */
473 const SPCurve*
474 sp_path_get_curve_reference (SPPath *path)
476     if (path->original_curve) {
477         return path->original_curve;
478     } else {
479         return path->curve;
480     }
483 /* Create a single dot represented by a circle */
484 void freehand_create_single_dot(SPEventContext *ec, NR::Point const &pt, char const *tool, guint event_state) {
485     g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
487     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
488     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
489     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
490     repr->setAttribute("sodipodi:type", "arc");
491     SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
492     Inkscape::GC::release(repr);
494     /* apply the tool's current style */
495     sp_desktop_apply_style_tool(desktop, repr, tool, false);
497     /* find out stroke width (TODO: is there an easier way??) */
498     double stroke_width = 3.0;
499     gchar const *style_str = NULL;
500     style_str = repr->attribute("style");
501     if (style_str) {
502         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
503         sp_style_merge_from_style_string(style, style_str);
504         stroke_width = style->stroke_width.computed;
505         style->stroke_width.computed = 0;
506         sp_style_unref(style);
507     }
508     /* unset stroke and set fill color to former stroke color */
509     gchar * str;
510     str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
511     repr->setAttribute("style", str);
512     g_free(str);
514     /* put the circle where the mouse click occurred and set the diameter to the
515        current stroke width, multiplied by the amount specified in the preferences */
516     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
517     NR::Point pp = pt * i2d;
518     double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
519     if (event_state & GDK_MOD1_MASK) {
520         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
521            as specified in prefs. Very simple, but it might be sufficient in practice. If not,
522            we need to devise something more sophisticated. */
523         double s = g_random_double_range(-0.5, 0.5);
524         rad *= (1 + s);
525     }
526     if (event_state & GDK_SHIFT_MASK) {
527         // double the point size
528         rad *= 2;
529     }
531     sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
532     sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
533     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
534     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
535     item->updateRepr();
537     sp_desktop_selection(desktop)->set(item);
539     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
540     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
543 /*
544   Local Variables:
545   mode:c++
546   c-file-style:"stroustrup"
547   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
548   indent-tabs-mode:nil
549   fill-column:99
550   End:
551 */
552 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :