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