Code

read PathVector instead of NArtBpath
[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     SPDocument *doc = SP_OBJECT_DOCUMENT(path);
150     std::list<std::pair<Geom::Point, Geom::Point> > pts;
152     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine(SP_ITEM(path))));
154     SPCurve *curve = SP_SHAPE(path)->curve;
155     if (!curve) return;
156     NArtBpath const *bpath = SP_CURVE_BPATH(curve);
158     NR::Point last_pt;
159     NR::Point pt;
160     for (int i = 0; bpath[i].code != NR_END; i++){
161         if (bpath[i].code == NR_LINETO) {
162             /* we only convert straight line segments (converting curve segments would be unintuitive) */
163             pt = bpath[i].c(3) * i2d;
164             pts.push_back(std::make_pair(last_pt.to_2geom(), pt.to_2geom()));
165         }
167         /* remember current point for potential reuse in the next step
168            (e.g., in case this was an NR_MOVETO or NR_MOVETO_OPEN) */
169         last_pt = bpath[i].c(3) * i2d;
170     }
172     sp_guide_pt_pairs_to_guides(doc, pts);
175 /**
176  * Initializes an SPPath.
177  */
178 static void
179 sp_path_init(SPPath *path)
181     new (&path->connEndPair) SPConnEndPair(path);
183     path->original_curve = NULL;
186 static void
187 sp_path_finalize(GObject *obj)
189     SPPath *path = (SPPath *) obj;
191     path->connEndPair.~SPConnEndPair();
194 /**
195  *  Given a repr, this sets the data items in the path object such as
196  *  fill & style attributes, markers, and CSS properties.
197  */
198 static void
199 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
201     /* Are these calls actually necessary? */
202     sp_object_read_attr(object, "marker");
203     sp_object_read_attr(object, "marker-start");
204     sp_object_read_attr(object, "marker-mid");
205     sp_object_read_attr(object, "marker-end");
207     sp_conn_end_pair_build(object);
209     if (((SPObjectClass *) parent_class)->build) {
210         ((SPObjectClass *) parent_class)->build(object, document, repr);
211     }
213     sp_object_read_attr(object, "inkscape:original-d");
214     sp_object_read_attr(object, "d");
216     /* d is a required attribute */
217     gchar const *d = sp_object_getAttribute(object, "d", NULL);
218     if (d == NULL) {
219         sp_object_set(object, sp_attribute_lookup("d"), "");
220     }
223 static void
224 sp_path_release(SPObject *object)
226     SPPath *path = SP_PATH(object);
228     path->connEndPair.release();
230     if (path->original_curve) {
231         path->original_curve = path->original_curve->unref();
232     }
234     if (((SPObjectClass *) parent_class)->release) {
235         ((SPObjectClass *) parent_class)->release(object);
236     }
239 /**
240  *  Sets a value in the path object given by 'key', to 'value'.  This is used
241  *  for setting attributes and markers on a path object.
242  */
243 static void
244 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
246     SPPath *path = (SPPath *) object;
248     switch (key) {
249         case SP_ATTR_INKSCAPE_ORIGINAL_D:
250                 if (value) {
251                     Geom::PathVector pv = sp_svg_read_pathv(value);
252                     SPCurve *curve = new SPCurve(pv);
253                     if (curve) {
254                         sp_path_set_original_curve(path, curve, TRUE, true);
255                         curve->unref();
256                     }
257                 } else {
258                     sp_path_set_original_curve(path, NULL, TRUE, true);
259                 }
260                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
261             break;
262        case SP_ATTR_D:
263             if (!sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
264                 if (value) {
265                     Geom::PathVector pv = sp_svg_read_pathv(value);
266                     SPCurve *curve = new SPCurve(pv);
267                     if (curve) {
268                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
269                         curve->unref();
270                     }
271                 } else {
272                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
273                 }
274                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
275             }
276             break;
277         case SP_PROP_MARKER:
278         case SP_PROP_MARKER_START:
279         case SP_PROP_MARKER_MID:
280         case SP_PROP_MARKER_END:
281             sp_shape_set_marker(object, key, value);
282             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
283             break;
284         case SP_ATTR_CONNECTOR_TYPE:
285         case SP_ATTR_CONNECTION_START:
286         case SP_ATTR_CONNECTION_END:
287             path->connEndPair.setAttr(key, value);
288             break;
289         default:
290             if (((SPObjectClass *) parent_class)->set) {
291                 ((SPObjectClass *) parent_class)->set(object, key, value);
292             }
293             break;
294     }
297 /**
298  *
299  * Writes the path object into a Inkscape::XML::Node
300  */
301 static Inkscape::XML::Node *
302 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
304     SPShape *shape = (SPShape *) object;
306     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
307         repr = xml_doc->createElement("svg:path");
308     }
310     if ( shape->curve != NULL ) {
311         gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
312         repr->setAttribute("d", str);
313         g_free(str);
314     } else {
315         repr->setAttribute("d", NULL);
316     }
318     SPPath *path = (SPPath *) object;
319     if ( path->original_curve != NULL ) {
320         gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
321         repr->setAttribute("inkscape:original-d", str);
322         g_free(str);
323     } else {
324         repr->setAttribute("inkscape:original-d", NULL);
325     }
327     SP_PATH(shape)->connEndPair.writeRepr(repr);
329     if (((SPObjectClass *)(parent_class))->write) {
330         ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
331     }
333     return repr;
336 static void
337 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
339     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
340         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
341     }
343     if (((SPObjectClass *) parent_class)->update) {
344         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
345     }
347     SPPath *path = SP_PATH(object);
348     path->connEndPair.update();
352 /**
353  * Writes the given transform into the repr for the given item.
354  */
355 static NR::Matrix
356 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
358     SPShape *shape = (SPShape *) item;
359     SPPath *path = (SPPath *) item;
361     if (!shape->curve) { // 0 nodes, nothing to transform
362         return NR::identity();
363     }
365     // Transform the original-d path or the (ordinary) path
366     if (path->original_curve) {
367         path->original_curve->transform(xform);
368         sp_lpe_item_update_patheffect(path, true, true);
369     } else {
370         shape->curve->transform(xform);
371     }
373     // Adjust stroke
374     sp_item_adjust_stroke(item, NR::expansion(xform));
376     // Adjust pattern fill
377     sp_item_adjust_pattern(item, xform);
379     // Adjust gradient fill
380     sp_item_adjust_gradient(item, xform);
382     // Adjust LPE
383     sp_item_adjust_livepatheffect(item, xform);
385     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
387     // nothing remains - we've written all of the transform, so return identity
388     return NR::identity();
391 static void
392 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
394     SPShape *shape = (SPShape *) lpeitem;
395     SPPath *path = (SPPath *) lpeitem;
396     if (path->original_curve) {
397         SPCurve *curve = path->original_curve->copy();
398         sp_shape_set_curve_insync(shape, curve, TRUE);
399         sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
400         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
401         curve->unref();
403         if (write) {
404             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
405             Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
406             if ( shape->curve != NULL ) {
407                 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
408                 repr->setAttribute("d", str);
409                 g_free(str);
410             } else {
411                 repr->setAttribute("d", NULL);
412             }
413         }
414     } else {
416     }
420 /**
421  * Adds a original_curve to the path.  If owner is specified, a reference
422  * will be made, otherwise the curve will be copied into the path.
423  * Any existing curve in the path will be unreferenced first.
424  * This routine triggers reapplication of an effect if present
425  * and also triggers a request to update the display. Does not write
426  * result to XML when write=false.
427  */
428 void
429 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
431     if (path->original_curve) {
432         path->original_curve = path->original_curve->unref();
433     }
434     if (curve) {
435         if (owner) {
436             path->original_curve = curve->ref();
437         } else {
438             path->original_curve = curve->copy();
439         }
440     }
441     sp_lpe_item_update_patheffect(path, true, write);
442     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
445 /**
446  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
447  */
448 SPCurve *
449 sp_path_get_original_curve (SPPath *path)
451     if (path->original_curve) {
452         return path->original_curve->copy();
453     }
454     return NULL;
457 /**
458  * Return duplicate of edittable curve which is original_curve if it exists or
459  * shape->curve if not.
460  */
461 SPCurve*
462 sp_path_get_curve_for_edit (SPPath *path)
464     if (path->original_curve) {
465         return sp_path_get_original_curve(path);
466     } else {
467         return sp_shape_get_curve( (SPShape *) path );
468     }
471 /**
472  * Return a reference to original_curve if it exists or
473  * shape->curve if not.
474  */
475 const SPCurve*
476 sp_path_get_curve_reference (SPPath *path)
478     if (path->original_curve) {
479         return path->original_curve;
480     } else {
481         return path->curve;
482     }
485 /* Create a single dot represented by a circle */
486 void freehand_create_single_dot(SPEventContext *ec, NR::Point const &pt, char const *tool, guint event_state) {
487     g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
489     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
490     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
491     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
492     repr->setAttribute("sodipodi:type", "arc");
493     SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
494     Inkscape::GC::release(repr);
496     /* apply the tool's current style */
497     sp_desktop_apply_style_tool(desktop, repr, tool, false);
499     /* find out stroke width (TODO: is there an easier way??) */
500     double stroke_width = 3.0;
501     gchar const *style_str = NULL;
502     style_str = repr->attribute("style");
503     if (style_str) {
504         SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
505         sp_style_merge_from_style_string(style, style_str);
506         stroke_width = style->stroke_width.computed;
507         style->stroke_width.computed = 0;
508         sp_style_unref(style);
509     }
510     /* unset stroke and set fill color to former stroke color */
511     gchar * str;
512     str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
513     repr->setAttribute("style", str);
514     g_free(str);
516     /* put the circle where the mouse click occurred and set the diameter to the
517        current stroke width, multiplied by the amount specified in the preferences */
518     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
519     NR::Point pp = pt * i2d;
520     double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
521     if (event_state & GDK_MOD1_MASK) {
522         /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
523            as specified in prefs. Very simple, but it might be sufficient in practice. If not,
524            we need to devise something more sophisticated. */
525         double s = g_random_double_range(-0.5, 0.5);
526         rad *= (1 + s);
527     }
528     if (event_state & GDK_SHIFT_MASK) {
529         // double the point size
530         rad *= 2;
531     }
533     sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
534     sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
535     sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
536     sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
537     item->updateRepr();
539     sp_desktop_selection(desktop)->set(item);
541     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
542     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
545 /*
546   Local Variables:
547   mode:c++
548   c-file-style:"stroustrup"
549   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
550   indent-tabs-mode:nil
551   fill-column:99
552   End:
553 */
554 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :