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;
119 }
122 gint
123 sp_nodes_in_path(SPPath *path)
124 {
125 SPCurve *curve = SP_SHAPE(path)->curve;
126 if (!curve)
127 return 0;
128 return curve->nodes_in_path();
129 }
131 static gchar *
132 sp_path_description(SPItem * item)
133 {
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 }
142 }
144 static void
145 sp_path_convert_to_guides(SPItem *item)
146 {
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 sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
170 }
172 /**
173 * Initializes an SPPath.
174 */
175 static void
176 sp_path_init(SPPath *path)
177 {
178 new (&path->connEndPair) SPConnEndPair(path);
180 path->original_curve = NULL;
181 }
183 static void
184 sp_path_finalize(GObject *obj)
185 {
186 SPPath *path = (SPPath *) obj;
188 path->connEndPair.~SPConnEndPair();
189 }
191 /**
192 * Given a repr, this sets the data items in the path object such as
193 * fill & style attributes, markers, and CSS properties.
194 */
195 static void
196 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
197 {
198 /* Are these calls actually necessary? */
199 sp_object_read_attr(object, "marker");
200 sp_object_read_attr(object, "marker-start");
201 sp_object_read_attr(object, "marker-mid");
202 sp_object_read_attr(object, "marker-end");
204 sp_conn_end_pair_build(object);
206 if (((SPObjectClass *) parent_class)->build) {
207 ((SPObjectClass *) parent_class)->build(object, document, repr);
208 }
210 sp_object_read_attr(object, "inkscape:original-d");
211 sp_object_read_attr(object, "d");
213 /* d is a required attribute */
214 gchar const *d = sp_object_getAttribute(object, "d", NULL);
215 if (d == NULL) {
216 sp_object_set(object, sp_attribute_lookup("d"), "");
217 }
218 }
220 static void
221 sp_path_release(SPObject *object)
222 {
223 SPPath *path = SP_PATH(object);
225 path->connEndPair.release();
227 if (path->original_curve) {
228 path->original_curve = path->original_curve->unref();
229 }
231 if (((SPObjectClass *) parent_class)->release) {
232 ((SPObjectClass *) parent_class)->release(object);
233 }
234 }
236 /**
237 * Sets a value in the path object given by 'key', to 'value'. This is used
238 * for setting attributes and markers on a path object.
239 */
240 static void
241 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
242 {
243 SPPath *path = (SPPath *) object;
245 switch (key) {
246 case SP_ATTR_INKSCAPE_ORIGINAL_D:
247 if (value) {
248 Geom::PathVector pv = sp_svg_read_pathv(value);
249 SPCurve *curve = new SPCurve(pv);
250 if (curve) {
251 sp_path_set_original_curve(path, curve, TRUE, true);
252 curve->unref();
253 }
254 } else {
255 sp_path_set_original_curve(path, NULL, TRUE, true);
256 }
257 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
258 break;
259 case SP_ATTR_D:
260 if (!sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
261 if (value) {
262 Geom::PathVector pv = sp_svg_read_pathv(value);
263 SPCurve *curve = new SPCurve(pv);
264 if (curve) {
265 sp_shape_set_curve((SPShape *) path, curve, TRUE);
266 curve->unref();
267 }
268 } else {
269 sp_shape_set_curve((SPShape *) path, NULL, TRUE);
270 }
271 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
272 }
273 break;
274 case SP_PROP_MARKER:
275 case SP_PROP_MARKER_START:
276 case SP_PROP_MARKER_MID:
277 case SP_PROP_MARKER_END:
278 sp_shape_set_marker(object, key, value);
279 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
280 break;
281 case SP_ATTR_CONNECTOR_TYPE:
282 case SP_ATTR_CONNECTION_START:
283 case SP_ATTR_CONNECTION_END:
284 path->connEndPair.setAttr(key, value);
285 break;
286 default:
287 if (((SPObjectClass *) parent_class)->set) {
288 ((SPObjectClass *) parent_class)->set(object, key, value);
289 }
290 break;
291 }
292 }
294 /**
295 *
296 * Writes the path object into a Inkscape::XML::Node
297 */
298 static Inkscape::XML::Node *
299 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
300 {
301 SPShape *shape = (SPShape *) object;
303 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
304 repr = xml_doc->createElement("svg:path");
305 }
307 if ( shape->curve != NULL ) {
308 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
309 repr->setAttribute("d", str);
310 g_free(str);
311 } else {
312 repr->setAttribute("d", NULL);
313 }
315 SPPath *path = (SPPath *) object;
316 if ( path->original_curve != NULL ) {
317 gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
318 repr->setAttribute("inkscape:original-d", str);
319 g_free(str);
320 } else {
321 repr->setAttribute("inkscape:original-d", NULL);
322 }
324 SP_PATH(shape)->connEndPair.writeRepr(repr);
326 if (((SPObjectClass *)(parent_class))->write) {
327 ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
328 }
330 return repr;
331 }
333 static void
334 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
335 {
336 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
337 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
338 }
340 if (((SPObjectClass *) parent_class)->update) {
341 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
342 }
344 SPPath *path = SP_PATH(object);
345 path->connEndPair.update();
346 }
349 /**
350 * Writes the given transform into the repr for the given item.
351 */
352 static NR::Matrix
353 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
354 {
355 SPShape *shape = (SPShape *) item;
356 SPPath *path = (SPPath *) item;
358 if (!shape->curve) { // 0 nodes, nothing to transform
359 return NR::identity();
360 }
362 // Transform the original-d path or the (ordinary) path
363 if (path->original_curve) {
364 path->original_curve->transform(xform);
365 sp_lpe_item_update_patheffect(path, true, true);
366 } else {
367 shape->curve->transform(xform);
368 }
370 // Adjust stroke
371 sp_item_adjust_stroke(item, NR::expansion(xform));
373 // Adjust pattern fill
374 sp_item_adjust_pattern(item, xform);
376 // Adjust gradient fill
377 sp_item_adjust_gradient(item, xform);
379 // Adjust LPE
380 sp_item_adjust_livepatheffect(item, xform);
382 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
384 // nothing remains - we've written all of the transform, so return identity
385 return NR::identity();
386 }
388 static void
389 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
390 {
391 SPShape *shape = (SPShape *) lpeitem;
392 SPPath *path = (SPPath *) lpeitem;
393 if (path->original_curve) {
394 SPCurve *curve = path->original_curve->copy();
395 sp_shape_set_curve_insync(shape, curve, TRUE);
396 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
397 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
398 curve->unref();
400 if (write) {
401 // could also do SP_OBJECT(shape)->updateRepr(); but only the d attribute needs updating.
402 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
403 if ( shape->curve != NULL ) {
404 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
405 repr->setAttribute("d", str);
406 g_free(str);
407 } else {
408 repr->setAttribute("d", NULL);
409 }
410 }
411 } else {
413 }
414 }
417 /**
418 * Adds a original_curve to the path. If owner is specified, a reference
419 * will be made, otherwise the curve will be copied into the path.
420 * Any existing curve in the path will be unreferenced first.
421 * This routine triggers reapplication of an effect if present
422 * and also triggers a request to update the display. Does not write
423 * result to XML when write=false.
424 */
425 void
426 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
427 {
428 if (path->original_curve) {
429 path->original_curve = path->original_curve->unref();
430 }
431 if (curve) {
432 if (owner) {
433 path->original_curve = curve->ref();
434 } else {
435 path->original_curve = curve->copy();
436 }
437 }
438 sp_lpe_item_update_patheffect(path, true, write);
439 SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
440 }
442 /**
443 * Return duplicate of original_curve (if any exists) or NULL if there is no curve
444 */
445 SPCurve *
446 sp_path_get_original_curve (SPPath *path)
447 {
448 if (path->original_curve) {
449 return path->original_curve->copy();
450 }
451 return NULL;
452 }
454 /**
455 * Return duplicate of edittable curve which is original_curve if it exists or
456 * shape->curve if not.
457 */
458 SPCurve*
459 sp_path_get_curve_for_edit (SPPath *path)
460 {
461 if (path->original_curve) {
462 return sp_path_get_original_curve(path);
463 } else {
464 return sp_shape_get_curve( (SPShape *) path );
465 }
466 }
468 /**
469 * Return a reference to original_curve if it exists or
470 * shape->curve if not.
471 */
472 const SPCurve*
473 sp_path_get_curve_reference (SPPath *path)
474 {
475 if (path->original_curve) {
476 return path->original_curve;
477 } else {
478 return path->curve;
479 }
480 }
482 /* Create a single dot represented by a circle */
483 void freehand_create_single_dot(SPEventContext *ec, NR::Point const &pt, char const *tool, guint event_state) {
484 g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
486 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
487 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
488 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
489 repr->setAttribute("sodipodi:type", "arc");
490 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
491 Inkscape::GC::release(repr);
493 /* apply the tool's current style */
494 sp_desktop_apply_style_tool(desktop, repr, tool, false);
496 /* find out stroke width (TODO: is there an easier way??) */
497 double stroke_width = 3.0;
498 gchar const *style_str = NULL;
499 style_str = repr->attribute("style");
500 if (style_str) {
501 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
502 sp_style_merge_from_style_string(style, style_str);
503 stroke_width = style->stroke_width.computed;
504 style->stroke_width.computed = 0;
505 sp_style_unref(style);
506 }
507 /* unset stroke and set fill color to former stroke color */
508 gchar * str;
509 str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
510 repr->setAttribute("style", str);
511 g_free(str);
513 /* put the circle where the mouse click occurred and set the diameter to the
514 current stroke width, multiplied by the amount specified in the preferences */
515 NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
516 NR::Point pp = pt * i2d;
517 double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
518 if (event_state & GDK_MOD1_MASK) {
519 /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
520 as specified in prefs. Very simple, but it might be sufficient in practice. If not,
521 we need to devise something more sophisticated. */
522 double s = g_random_double_range(-0.5, 0.5);
523 rad *= (1 + s);
524 }
525 if (event_state & GDK_SHIFT_MASK) {
526 // double the point size
527 rad *= 2;
528 }
530 sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
531 sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
532 sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
533 sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
534 item->updateRepr();
536 sp_desktop_selection(desktop)->set(item);
538 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
539 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
540 }
542 /*
543 Local Variables:
544 mode:c++
545 c-file-style:"stroustrup"
546 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
547 indent-tabs-mode:nil
548 fill-column:99
549 End:
550 */
551 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :