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 Geom::Matrix sp_path_set_transform(SPItem *item, Geom::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 Geom::Matrix
352 sp_path_set_transform(SPItem *item, Geom::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 Geom::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, xform.descrim());
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 Geom::identity();
384 }
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;
392 if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(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 }
412 }
413 // else: do nothing.
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 /*
483 Local Variables:
484 mode:c++
485 c-file-style:"stroustrup"
486 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
487 indent-tabs-mode:nil
488 fill-column:99
489 End:
490 */
491 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :