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