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 "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 Geom::Matrix sp_path_set_transform(SPItem *item, Geom::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( is_straight_curve(*cit) )
161 {
162 pts.push_back(std::make_pair(cit->initialPoint() * i2d, cit->finalPoint() * i2d));
163 }
164 }
165 }
167 sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
168 }
170 /**
171 * Initializes an SPPath.
172 */
173 static void
174 sp_path_init(SPPath *path)
175 {
176 new (&path->connEndPair) SPConnEndPair(path);
178 path->original_curve = NULL;
179 }
181 static void
182 sp_path_finalize(GObject *obj)
183 {
184 SPPath *path = (SPPath *) obj;
186 path->connEndPair.~SPConnEndPair();
187 }
189 /**
190 * Given a repr, this sets the data items in the path object such as
191 * fill & style attributes, markers, and CSS properties.
192 */
193 static void
194 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
195 {
196 /* Are these calls actually necessary? */
197 sp_object_read_attr(object, "marker");
198 sp_object_read_attr(object, "marker-start");
199 sp_object_read_attr(object, "marker-mid");
200 sp_object_read_attr(object, "marker-end");
202 sp_conn_end_pair_build(object);
204 if (((SPObjectClass *) parent_class)->build) {
205 ((SPObjectClass *) parent_class)->build(object, document, repr);
206 }
208 sp_object_read_attr(object, "inkscape:original-d");
209 sp_object_read_attr(object, "d");
211 /* d is a required attribute */
212 gchar const *d = sp_object_getAttribute(object, "d", NULL);
213 if (d == NULL) {
214 sp_object_set(object, sp_attribute_lookup("d"), "");
215 }
216 }
218 static void
219 sp_path_release(SPObject *object)
220 {
221 SPPath *path = SP_PATH(object);
223 path->connEndPair.release();
225 if (path->original_curve) {
226 path->original_curve = path->original_curve->unref();
227 }
229 if (((SPObjectClass *) parent_class)->release) {
230 ((SPObjectClass *) parent_class)->release(object);
231 }
232 }
234 /**
235 * Sets a value in the path object given by 'key', to 'value'. This is used
236 * for setting attributes and markers on a path object.
237 */
238 static void
239 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
240 {
241 SPPath *path = (SPPath *) object;
243 switch (key) {
244 case SP_ATTR_INKSCAPE_ORIGINAL_D:
245 if (value) {
246 Geom::PathVector pv = sp_svg_read_pathv(value);
247 SPCurve *curve = new SPCurve(pv);
248 if (curve) {
249 sp_path_set_original_curve(path, curve, TRUE, true);
250 curve->unref();
251 }
252 } else {
253 sp_path_set_original_curve(path, NULL, TRUE, true);
254 }
255 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
256 break;
257 case SP_ATTR_D:
258 if (!sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path))) {
259 if (value) {
260 Geom::PathVector pv = sp_svg_read_pathv(value);
261 SPCurve *curve = new SPCurve(pv);
262 if (curve) {
263 sp_shape_set_curve((SPShape *) path, curve, TRUE);
264 curve->unref();
265 }
266 } else {
267 sp_shape_set_curve((SPShape *) path, NULL, TRUE);
268 }
269 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
270 }
271 break;
272 case SP_PROP_MARKER:
273 case SP_PROP_MARKER_START:
274 case SP_PROP_MARKER_MID:
275 case SP_PROP_MARKER_END:
276 sp_shape_set_marker(object, key, value);
277 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
278 break;
279 case SP_ATTR_CONNECTOR_TYPE:
280 case SP_ATTR_CONNECTION_START:
281 case SP_ATTR_CONNECTION_END:
282 path->connEndPair.setAttr(key, value);
283 break;
284 default:
285 if (((SPObjectClass *) parent_class)->set) {
286 ((SPObjectClass *) parent_class)->set(object, key, value);
287 }
288 break;
289 }
290 }
292 /**
293 *
294 * Writes the path object into a Inkscape::XML::Node
295 */
296 static Inkscape::XML::Node *
297 sp_path_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
298 {
299 SPShape *shape = (SPShape *) object;
301 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
302 repr = xml_doc->createElement("svg:path");
303 }
305 if ( shape->curve != NULL ) {
306 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
307 repr->setAttribute("d", str);
308 g_free(str);
309 } else {
310 repr->setAttribute("d", NULL);
311 }
313 SPPath *path = (SPPath *) object;
314 if ( path->original_curve != NULL ) {
315 gchar *str = sp_svg_write_path(path->original_curve->get_pathvector());
316 repr->setAttribute("inkscape:original-d", str);
317 g_free(str);
318 } else {
319 repr->setAttribute("inkscape:original-d", NULL);
320 }
322 SP_PATH(shape)->connEndPair.writeRepr(repr);
324 if (((SPObjectClass *)(parent_class))->write) {
325 ((SPObjectClass *)(parent_class))->write(object, xml_doc, repr, flags);
326 }
328 return repr;
329 }
331 static void
332 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
333 {
334 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
335 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
336 }
338 if (((SPObjectClass *) parent_class)->update) {
339 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
340 }
342 SPPath *path = SP_PATH(object);
343 path->connEndPair.update();
344 }
347 /**
348 * Writes the given transform into the repr for the given item.
349 */
350 static Geom::Matrix
351 sp_path_set_transform(SPItem *item, Geom::Matrix const &xform)
352 {
353 SPShape *shape = (SPShape *) item;
354 SPPath *path = (SPPath *) item;
356 if (!shape->curve) { // 0 nodes, nothing to transform
357 return Geom::identity();
358 }
360 // Transform the original-d path or the (ordinary) path
361 if (path->original_curve) {
362 path->original_curve->transform(xform);
363 } else {
364 shape->curve->transform(xform);
365 }
367 // Adjust stroke
368 sp_item_adjust_stroke(item, xform.descrim());
370 // Adjust pattern fill
371 sp_item_adjust_pattern(item, xform);
373 // Adjust gradient fill
374 sp_item_adjust_gradient(item, xform);
376 // Adjust LPE
377 sp_item_adjust_livepatheffect(item, xform);
379 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
381 // nothing remains - we've written all of the transform, so return identity
382 return Geom::identity();
383 }
386 static void
387 sp_path_update_patheffect(SPLPEItem *lpeitem, bool write)
388 {
389 SPShape * const shape = (SPShape *) lpeitem;
390 SPPath * const path = (SPPath *) lpeitem;
391 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
393 if (path->original_curve) {
394 SPCurve *curve = path->original_curve->copy();
395 /* if a path does not have an lpeitem applied, then reset the curve to the original_curve.
396 * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
397 sp_shape_set_curve_insync(shape, curve, TRUE);
399 bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM(shape), curve);
400 if (success && write) {
401 // could also do SP_OBJECT(shape)->updateRepr(); but only the d attribute needs updating.
402 if ( shape->curve != NULL ) {
403 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
404 repr->setAttribute("d", str);
405 g_free(str);
406 } else {
407 repr->setAttribute("d", NULL);
408 }
409 } else {
410 // LPE was unsuccesfull. Read the old 'd'-attribute.
411 if (gchar const * value = repr->attribute("d")) {
412 Geom::PathVector pv = sp_svg_read_pathv(value);
413 SPCurve *oldcurve = new SPCurve(pv);
414 if (oldcurve) {
415 sp_shape_set_curve(shape, oldcurve, TRUE);
416 oldcurve->unref();
417 }
418 }
419 }
420 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
421 curve->unref();
422 }
423 }
426 /**
427 * Adds a original_curve to the path. If owner is specified, a reference
428 * will be made, otherwise the curve will be copied into the path.
429 * Any existing curve in the path will be unreferenced first.
430 * This routine triggers reapplication of an effect if present
431 * and also triggers a request to update the display. Does not write
432 * result to XML when write=false.
433 */
434 void
435 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
436 {
437 if (path->original_curve) {
438 path->original_curve = path->original_curve->unref();
439 }
440 if (curve) {
441 if (owner) {
442 path->original_curve = curve->ref();
443 } else {
444 path->original_curve = curve->copy();
445 }
446 }
447 sp_lpe_item_update_patheffect(path, true, write);
448 SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
449 }
451 /**
452 * Return duplicate of original_curve (if any exists) or NULL if there is no curve
453 */
454 SPCurve *
455 sp_path_get_original_curve (SPPath *path)
456 {
457 if (path->original_curve) {
458 return path->original_curve->copy();
459 }
460 return NULL;
461 }
463 /**
464 * Return duplicate of edittable curve which is original_curve if it exists or
465 * shape->curve if not.
466 */
467 SPCurve*
468 sp_path_get_curve_for_edit (SPPath *path)
469 {
470 if (path->original_curve) {
471 return sp_path_get_original_curve(path);
472 } else {
473 return sp_shape_get_curve( (SPShape *) path );
474 }
475 }
477 /**
478 * Return a reference to original_curve if it exists or
479 * shape->curve if not.
480 */
481 const SPCurve*
482 sp_path_get_curve_reference (SPPath *path)
483 {
484 if (path->original_curve) {
485 return path->original_curve;
486 } else {
487 return path->curve;
488 }
489 }
491 /*
492 Local Variables:
493 mode:c++
494 c-file-style:"stroustrup"
495 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
496 indent-tabs-mode:nil
497 fill-column:99
498 End:
499 */
500 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :