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>
28 #include "svg/svg.h"
29 #include "xml/repr.h"
30 #include "attributes.h"
32 #include "sp-path.h"
33 #include "sp-guide.h"
35 #include "document.h"
36 #include "desktop.h"
37 #include "desktop-handles.h"
38 #include "desktop-style.h"
39 #include "event-context.h"
40 #include "inkscape.h"
41 #include "style.h"
42 #include "message-stack.h"
43 #include "prefs-utils.h"
44 #include "selection.h"
46 #define noPATH_VERBOSE
48 static void sp_path_class_init(SPPathClass *klass);
49 static void sp_path_init(SPPath *path);
50 static void sp_path_finalize(GObject *obj);
51 static void sp_path_release(SPObject *object);
53 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
54 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
56 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
57 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
58 static gchar * sp_path_description(SPItem *item);
59 static void sp_path_convert_to_guides(SPItem *item);
61 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
62 static void sp_path_update_patheffect(SPShape *shape, bool write);
64 static SPShapeClass *parent_class;
66 /**
67 * Gets the GType object for SPPathClass
68 */
69 GType
70 sp_path_get_type(void)
71 {
72 static GType type = 0;
74 if (!type) {
75 GTypeInfo info = {
76 sizeof(SPPathClass),
77 NULL, NULL,
78 (GClassInitFunc) sp_path_class_init,
79 NULL, NULL,
80 sizeof(SPPath),
81 16,
82 (GInstanceInitFunc) sp_path_init,
83 NULL, /* value_table */
84 };
85 type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
86 }
87 return type;
88 }
90 /**
91 * Does the object-oriented work of initializing the class structure
92 * including parent class, and registers function pointers for
93 * the functions build, set, write, and set_transform.
94 */
95 static void
96 sp_path_class_init(SPPathClass * klass)
97 {
98 GObjectClass *gobject_class = (GObjectClass *) klass;
99 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
100 SPItemClass *item_class = (SPItemClass *) klass;
101 SPShapeClass *shape_class = (SPShapeClass *) klass;
103 parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
105 gobject_class->finalize = sp_path_finalize;
107 sp_object_class->build = sp_path_build;
108 sp_object_class->release = sp_path_release;
109 sp_object_class->set = sp_path_set;
110 sp_object_class->write = sp_path_write;
111 sp_object_class->update = sp_path_update;
113 item_class->description = sp_path_description;
114 item_class->set_transform = sp_path_set_transform;
115 item_class->convert_to_guides = sp_path_convert_to_guides;
117 shape_class->update_patheffect = sp_path_update_patheffect;
118 }
121 gint
122 sp_nodes_in_path(SPPath *path)
123 {
124 SPCurve *curve = SP_SHAPE(path)->curve;
125 if (!curve) return 0;
126 gint r = curve->end;
127 gint i = curve->length - 1;
128 if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
129 for (; i >= 0; i --)
130 if (SP_CURVE_BPATH(curve)[i].code == NR_MOVETO)
131 r --;
132 return r;
133 }
135 static gchar *
136 sp_path_description(SPItem * item)
137 {
138 int count = sp_nodes_in_path(SP_PATH(item));
139 if (SP_SHAPE(item)->path_effect_href) {
140 return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect)",
141 "<b>Path</b> (%i nodes, path effect)",count), count);
142 } else {
143 return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
144 "<b>Path</b> (%i nodes)",count), count);
145 }
146 }
148 static void
149 sp_path_convert_to_guides(SPItem *item)
150 {
151 SPPath *path = SP_PATH(item);
153 SPDocument *doc = SP_OBJECT_DOCUMENT(path);
154 std::list<std::pair<Geom::Point, Geom::Point> > pts;
156 NR::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
158 SPCurve *curve = SP_SHAPE(path)->curve;
159 if (!curve) return;
160 NArtBpath *bpath = SP_CURVE_BPATH(curve);
162 NR::Point last_pt;
163 NR::Point pt;
164 for (int i = 0; bpath[i].code != NR_END; i++){
165 if (bpath[i].code == NR_LINETO) {
166 /* we only convert straight line segments (converting curve segments would be unintuitive) */
167 pt = bpath[i].c(3) * i2d;
168 pts.push_back(std::make_pair(last_pt.to_2geom(), pt.to_2geom()));
169 }
171 /* remember current point for potential reuse in the next step
172 (e.g., in case this was an NR_MOVETO or NR_MOVETO_OPEN) */
173 last_pt = bpath[i].c(3) * i2d;
174 }
176 sp_guide_pt_pairs_to_guides(doc, pts);
177 }
179 /**
180 * Initializes an SPPath.
181 */
182 static void
183 sp_path_init(SPPath *path)
184 {
185 new (&path->connEndPair) SPConnEndPair(path);
187 path->original_curve = NULL;
188 }
190 static void
191 sp_path_finalize(GObject *obj)
192 {
193 SPPath *path = (SPPath *) obj;
195 path->connEndPair.~SPConnEndPair();
196 }
198 /**
199 * Given a repr, this sets the data items in the path object such as
200 * fill & style attributes, markers, and CSS properties.
201 */
202 static void
203 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
204 {
205 /* Are these calls actually necessary? */
206 sp_object_read_attr(object, "marker");
207 sp_object_read_attr(object, "marker-start");
208 sp_object_read_attr(object, "marker-mid");
209 sp_object_read_attr(object, "marker-end");
211 sp_conn_end_pair_build(object);
213 if (((SPObjectClass *) parent_class)->build) {
214 ((SPObjectClass *) parent_class)->build(object, document, repr);
215 }
217 sp_object_read_attr(object, "inkscape:original-d");
218 sp_object_read_attr(object, "d");
220 /* d is a required attribute */
221 gchar const *d = sp_object_getAttribute(object, "d", NULL);
222 if (d == NULL) {
223 sp_object_set(object, sp_attribute_lookup("d"), "");
224 }
225 }
227 static void
228 sp_path_release(SPObject *object)
229 {
230 SPPath *path = SP_PATH(object);
232 path->connEndPair.release();
234 if (path->original_curve) {
235 path->original_curve = sp_curve_unref (path->original_curve);
236 }
238 if (((SPObjectClass *) parent_class)->release) {
239 ((SPObjectClass *) parent_class)->release(object);
240 }
241 }
243 /**
244 * Sets a value in the path object given by 'key', to 'value'. This is used
245 * for setting attributes and markers on a path object.
246 */
247 static void
248 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
249 {
250 SPPath *path = (SPPath *) object;
252 switch (key) {
253 case SP_ATTR_INKSCAPE_ORIGINAL_D:
254 if (value) {
255 NArtBpath *bpath = sp_svg_read_path(value);
256 SPCurve *curve = sp_curve_new_from_bpath(bpath);
257 if (curve) {
258 sp_path_set_original_curve(path, curve, TRUE, true);
259 sp_curve_unref(curve);
260 }
261 } else {
262 sp_path_set_original_curve(path, NULL, TRUE, true);
263 }
264 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
265 break;
266 case SP_ATTR_D:
267 if (!((SPShape *) path)->path_effect_href) {
268 if (value) {
269 NArtBpath *bpath = sp_svg_read_path(value);
270 SPCurve *curve = sp_curve_new_from_bpath(bpath);
271 if (curve) {
272 sp_shape_set_curve((SPShape *) path, curve, TRUE);
273 sp_curve_unref(curve);
274 }
275 } else {
276 sp_shape_set_curve((SPShape *) path, NULL, TRUE);
277 }
278 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
279 }
280 break;
281 case SP_PROP_MARKER:
282 case SP_PROP_MARKER_START:
283 case SP_PROP_MARKER_MID:
284 case SP_PROP_MARKER_END:
285 sp_shape_set_marker(object, key, value);
286 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
287 break;
288 case SP_ATTR_CONNECTOR_TYPE:
289 case SP_ATTR_CONNECTION_START:
290 case SP_ATTR_CONNECTION_END:
291 path->connEndPair.setAttr(key, value);
292 break;
293 default:
294 if (((SPObjectClass *) parent_class)->set) {
295 ((SPObjectClass *) parent_class)->set(object, key, value);
296 }
297 break;
298 }
299 }
301 /**
302 *
303 * Writes the path object into a Inkscape::XML::Node
304 */
305 static Inkscape::XML::Node *
306 sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
307 {
308 SPShape *shape = (SPShape *) object;
310 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
311 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
312 repr = xml_doc->createElement("svg:path");
313 }
315 if ( shape->curve != NULL ) {
316 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
317 if (abp) {
318 gchar *str = sp_svg_write_path(abp);
319 repr->setAttribute("d", str);
320 g_free(str);
321 } else {
322 repr->setAttribute("d", "");
323 }
324 } else {
325 repr->setAttribute("d", NULL);
326 }
328 SPPath *path = (SPPath *) object;
329 if ( path->original_curve != NULL ) {
330 NArtBpath *abp = sp_curve_first_bpath(path->original_curve);
331 if (abp) {
332 gchar *str = sp_svg_write_path(abp);
333 repr->setAttribute("inkscape:original-d", str);
334 g_free(str);
335 } else {
336 repr->setAttribute("inkscape:original-d", "");
337 }
338 } else {
339 repr->setAttribute("inkscape:original-d", NULL);
340 }
342 SP_PATH(shape)->connEndPair.writeRepr(repr);
344 if (((SPObjectClass *)(parent_class))->write) {
345 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
346 }
348 return repr;
349 }
351 static void
352 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
353 {
354 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
355 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
356 }
358 if (((SPObjectClass *) parent_class)->update) {
359 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
360 }
362 SPPath *path = SP_PATH(object);
363 path->connEndPair.update();
364 }
367 /**
368 * Writes the given transform into the repr for the given item.
369 */
370 static NR::Matrix
371 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
372 {
373 SPShape *shape = (SPShape *) item;
374 SPPath *path = (SPPath *) item;
376 if (!shape->curve) { // 0 nodes, nothing to transform
377 return NR::identity();
378 }
380 if (path->original_curve) { /* Transform the original-d path */
381 NRBPath dorigpath, sorigpath;
382 sorigpath.path = SP_CURVE_BPATH(path->original_curve);
383 nr_path_duplicate_transform(&dorigpath, &sorigpath, xform);
384 SPCurve *origcurve = sp_curve_new_from_bpath(dorigpath.path);
385 if (origcurve) {
386 sp_path_set_original_curve(path, origcurve, TRUE, true);
387 sp_curve_unref(origcurve);
388 }
389 } else { /* Transform the path */
390 NRBPath dpath, spath;
391 spath.path = SP_CURVE_BPATH(shape->curve);
392 nr_path_duplicate_transform(&dpath, &spath, xform);
393 SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
394 if (curve) {
395 sp_shape_set_curve(shape, curve, TRUE);
396 sp_curve_unref(curve);
397 }
398 }
400 // Adjust stroke
401 sp_item_adjust_stroke(item, NR::expansion(xform));
403 // Adjust pattern fill
404 sp_item_adjust_pattern(item, xform);
406 // Adjust gradient fill
407 sp_item_adjust_gradient(item, xform);
409 // Adjust LPE
410 sp_item_adjust_livepatheffect(item, xform);
412 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
414 // nothing remains - we've written all of the transform, so return identity
415 return NR::identity();
416 }
418 static void
419 sp_path_update_patheffect(SPShape *shape, bool write)
420 {
421 SPPath *path = (SPPath *) shape;
422 if (path->original_curve) {
423 SPCurve *curve = sp_curve_copy (path->original_curve);
424 sp_shape_perform_path_effect(curve, shape);
425 sp_shape_set_curve(shape, curve, TRUE);
426 sp_curve_unref(curve);
428 if (write) {
429 // could also do SP_OBJECT(shape)->updateRepr(); but only the d attribute needs updating.
430 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
431 if ( shape->curve != NULL ) {
432 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
433 if (abp) {
434 gchar *str = sp_svg_write_path(abp);
435 repr->setAttribute("d", str);
436 g_free(str);
437 } else {
438 repr->setAttribute("d", "");
439 }
440 } else {
441 repr->setAttribute("d", NULL);
442 }
443 }
444 } else {
446 }
447 }
450 /**
451 * Adds a original_curve to the path. If owner is specified, a reference
452 * will be made, otherwise the curve will be copied into the path.
453 * Any existing curve in the path will be unreferenced first.
454 * This routine triggers reapplication of the an effect is present
455 * an also triggers a request to update the display. Does not write
456 * result to XML when write=false.
457 */
458 void
459 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
460 {
461 if (path->original_curve) {
462 path->original_curve = sp_curve_unref (path->original_curve);
463 }
464 if (curve) {
465 if (owner) {
466 path->original_curve = sp_curve_ref (curve);
467 } else {
468 path->original_curve = sp_curve_copy (curve);
469 }
470 }
471 sp_path_update_patheffect(path, write);
472 SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
473 }
475 /**
476 * Return duplicate of original_curve (if any exists) or NULL if there is no curve
477 */
478 SPCurve *
479 sp_path_get_original_curve (SPPath *path)
480 {
481 if (path->original_curve) {
482 return sp_curve_copy (path->original_curve);
483 }
484 return NULL;
485 }
487 /**
488 * Return duplicate of edittable curve which is original_curve if it exists or
489 * shape->curve if not.
490 */
491 SPCurve*
492 sp_path_get_curve_for_edit (SPPath *path)
493 {
494 if (path->original_curve) {
495 return sp_path_get_original_curve(path);
496 } else {
497 return sp_shape_get_curve( (SPShape *) path );
498 }
499 }
501 /**
502 * Return a reference to original_curve if it exists or
503 * shape->curve if not.
504 */
505 const SPCurve*
506 sp_path_get_curve_reference (SPPath *path)
507 {
508 if (path->original_curve) {
509 return path->original_curve;
510 } else {
511 return path->curve;
512 }
513 }
515 /* Create a single dot represented by a circle */
516 void freehand_create_single_dot(SPEventContext *ec, NR::Point const &pt, char const *tool, guint event_state) {
517 g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
519 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
520 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
521 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
522 repr->setAttribute("sodipodi:type", "arc");
523 SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
524 Inkscape::GC::release(repr);
526 /* apply the tool's current style */
527 sp_desktop_apply_style_tool(desktop, repr, tool, false);
529 /* find out stroke width (TODO: is there an easier way??) */
530 double stroke_width = 3.0;
531 gchar const *style_str = NULL;
532 style_str = repr->attribute("style");
533 if (style_str) {
534 SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
535 sp_style_merge_from_style_string(style, style_str);
536 stroke_width = style->stroke_width.computed;
537 style->stroke_width.computed = 0;
538 sp_style_unref(style);
539 }
540 /* unset stroke and set fill color to former stroke color */
541 gchar * str;
542 str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
543 repr->setAttribute("style", str);
544 g_free(str);
546 /* put the circle where the mouse click occurred and set the diameter to the
547 current stroke width, multiplied by the amount specified in the preferences */
548 NR::Matrix const i2d (sp_item_i2d_affine (item));
549 NR::Point pp = pt * i2d;
550 double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
551 if (event_state & GDK_MOD1_MASK) {
552 /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
553 as specified in prefs. Very simple, but it might be sufficient in practice. If not,
554 we need to devise something more sophisticated. */
555 double s = g_random_double_range(-0.5, 0.5);
556 rad *= (1 + s);
557 }
558 if (event_state & GDK_SHIFT_MASK) {
559 // double the point size
560 rad *= 2;
561 }
563 sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
564 sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
565 sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
566 sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
567 item->updateRepr();
569 sp_desktop_selection(desktop)->set(item);
571 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
572 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
573 }
575 /*
576 Local Variables:
577 mode:c++
578 c-file-style:"stroustrup"
579 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
580 indent-tabs-mode:nil
581 fill-column:99
582 End:
583 */
584 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :