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"
37 #define noPATH_VERBOSE
39 static void sp_path_class_init(SPPathClass *klass);
40 static void sp_path_init(SPPath *path);
41 static void sp_path_finalize(GObject *obj);
42 static void sp_path_release(SPObject *object);
44 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
45 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
47 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
48 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
49 static gchar * sp_path_description(SPItem *item);
50 static void sp_path_convert_to_guides(SPItem *item);
52 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
53 static void sp_path_update_patheffect(SPShape *shape, bool write);
55 static SPShapeClass *parent_class;
57 /**
58 * Gets the GType object for SPPathClass
59 */
60 GType
61 sp_path_get_type(void)
62 {
63 static GType type = 0;
65 if (!type) {
66 GTypeInfo info = {
67 sizeof(SPPathClass),
68 NULL, NULL,
69 (GClassInitFunc) sp_path_class_init,
70 NULL, NULL,
71 sizeof(SPPath),
72 16,
73 (GInstanceInitFunc) sp_path_init,
74 NULL, /* value_table */
75 };
76 type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
77 }
78 return type;
79 }
81 /**
82 * Does the object-oriented work of initializing the class structure
83 * including parent class, and registers function pointers for
84 * the functions build, set, write, and set_transform.
85 */
86 static void
87 sp_path_class_init(SPPathClass * klass)
88 {
89 GObjectClass *gobject_class = (GObjectClass *) klass;
90 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
91 SPItemClass *item_class = (SPItemClass *) klass;
92 SPShapeClass *shape_class = (SPShapeClass *) klass;
94 parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
96 gobject_class->finalize = sp_path_finalize;
98 sp_object_class->build = sp_path_build;
99 sp_object_class->release = sp_path_release;
100 sp_object_class->set = sp_path_set;
101 sp_object_class->write = sp_path_write;
102 sp_object_class->update = sp_path_update;
104 item_class->description = sp_path_description;
105 item_class->set_transform = sp_path_set_transform;
106 item_class->convert_to_guides = sp_path_convert_to_guides;
108 shape_class->update_patheffect = sp_path_update_patheffect;
109 }
112 gint
113 sp_nodes_in_path(SPPath *path)
114 {
115 SPCurve *curve = SP_SHAPE(path)->curve;
116 if (!curve) return 0;
117 gint r = curve->end;
118 gint i = curve->length - 1;
119 if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
120 for (; i >= 0; i --)
121 if (SP_CURVE_BPATH(curve)[i].code == NR_MOVETO)
122 r --;
123 return r;
124 }
126 static gchar *
127 sp_path_description(SPItem * item)
128 {
129 int count = sp_nodes_in_path(SP_PATH(item));
130 if (SP_SHAPE(item)->path_effect_href) {
131 return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect)",
132 "<b>Path</b> (%i nodes, path effect)",count), count);
133 } else {
134 return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
135 "<b>Path</b> (%i nodes)",count), count);
136 }
137 }
139 static void
140 sp_path_convert_to_guides(SPItem *item)
141 {
142 SPPath *path = SP_PATH(item);
144 SPDocument *doc = SP_OBJECT_DOCUMENT(path);
145 std::list<std::pair<Geom::Point, Geom::Point> > pts;
147 NR::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
149 SPCurve *curve = SP_SHAPE(path)->curve;
150 if (!curve) return;
151 NArtBpath *bpath = SP_CURVE_BPATH(curve);
153 NR::Point last_pt;
154 NR::Point pt;
155 for (int i = 0; bpath[i].code != NR_END; i++){
156 if (bpath[i].code == NR_LINETO) {
157 /* we only convert straight line segments (converting curve segments would be unintuitive) */
158 pt = bpath[i].c(3) * i2d;
159 pts.push_back(std::make_pair(last_pt.to_2geom(), pt.to_2geom()));
160 }
162 /* remember current point for potential reuse in the next step
163 (e.g., in case this was an NR_MOVETO or NR_MOVETO_OPEN) */
164 last_pt = bpath[i].c(3) * i2d;
165 }
167 sp_guide_pt_pairs_to_guides(doc, 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 = sp_curve_unref (path->original_curve);
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 NArtBpath *bpath = sp_svg_read_path(value);
247 SPCurve *curve = sp_curve_new_from_bpath(bpath);
248 if (curve) {
249 sp_path_set_original_curve(path, curve, TRUE, true);
250 sp_curve_unref(curve);
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 (!((SPShape *) path)->path_effect_href) {
259 if (value) {
260 NArtBpath *bpath = sp_svg_read_path(value);
261 SPCurve *curve = sp_curve_new_from_bpath(bpath);
262 if (curve) {
263 sp_shape_set_curve((SPShape *) path, curve, TRUE);
264 sp_curve_unref(curve);
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::Node *repr, guint flags)
298 {
299 SPShape *shape = (SPShape *) object;
301 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
302 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
303 repr = xml_doc->createElement("svg:path");
304 }
306 if ( shape->curve != NULL ) {
307 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
308 if (abp) {
309 gchar *str = sp_svg_write_path(abp);
310 repr->setAttribute("d", str);
311 g_free(str);
312 } else {
313 repr->setAttribute("d", "");
314 }
315 } else {
316 repr->setAttribute("d", NULL);
317 }
319 SPPath *path = (SPPath *) object;
320 if ( path->original_curve != NULL ) {
321 NArtBpath *abp = sp_curve_first_bpath(path->original_curve);
322 if (abp) {
323 gchar *str = sp_svg_write_path(abp);
324 repr->setAttribute("inkscape:original-d", str);
325 g_free(str);
326 } else {
327 repr->setAttribute("inkscape:original-d", "");
328 }
329 } else {
330 repr->setAttribute("inkscape:original-d", NULL);
331 }
333 SP_PATH(shape)->connEndPair.writeRepr(repr);
335 if (((SPObjectClass *)(parent_class))->write) {
336 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
337 }
339 return repr;
340 }
342 static void
343 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
344 {
345 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
346 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
347 }
349 if (((SPObjectClass *) parent_class)->update) {
350 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
351 }
353 SPPath *path = SP_PATH(object);
354 path->connEndPair.update();
355 }
358 /**
359 * Writes the given transform into the repr for the given item.
360 */
361 static NR::Matrix
362 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
363 {
364 SPShape *shape = (SPShape *) item;
365 SPPath *path = (SPPath *) item;
367 if (!shape->curve) { // 0 nodes, nothing to transform
368 return NR::identity();
369 }
371 if (path->original_curve) { /* Transform the original-d path */
372 NRBPath dorigpath, sorigpath;
373 sorigpath.path = SP_CURVE_BPATH(path->original_curve);
374 nr_path_duplicate_transform(&dorigpath, &sorigpath, xform);
375 SPCurve *origcurve = sp_curve_new_from_bpath(dorigpath.path);
376 if (origcurve) {
377 sp_path_set_original_curve(path, origcurve, TRUE, true);
378 sp_curve_unref(origcurve);
379 }
380 } else { /* Transform the path */
381 NRBPath dpath, spath;
382 spath.path = SP_CURVE_BPATH(shape->curve);
383 nr_path_duplicate_transform(&dpath, &spath, xform);
384 SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
385 if (curve) {
386 sp_shape_set_curve(shape, curve, TRUE);
387 sp_curve_unref(curve);
388 }
389 }
391 // Adjust stroke
392 sp_item_adjust_stroke(item, NR::expansion(xform));
394 // Adjust pattern fill
395 sp_item_adjust_pattern(item, xform);
397 // Adjust gradient fill
398 sp_item_adjust_gradient(item, xform);
400 // Adjust LPE
401 sp_item_adjust_livepatheffect(item, xform);
403 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
405 // nothing remains - we've written all of the transform, so return identity
406 return NR::identity();
407 }
409 static void
410 sp_path_update_patheffect(SPShape *shape, bool write)
411 {
412 SPPath *path = (SPPath *) shape;
413 if (path->original_curve) {
414 SPCurve *curve = sp_curve_copy (path->original_curve);
415 sp_shape_perform_path_effect(curve, shape);
416 sp_shape_set_curve(shape, curve, TRUE);
417 sp_curve_unref(curve);
419 if (write) {
420 // could also do SP_OBJECT(shape)->updateRepr(); but only the d attribute needs updating.
421 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
422 if ( shape->curve != NULL ) {
423 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
424 if (abp) {
425 gchar *str = sp_svg_write_path(abp);
426 repr->setAttribute("d", str);
427 g_free(str);
428 } else {
429 repr->setAttribute("d", "");
430 }
431 } else {
432 repr->setAttribute("d", NULL);
433 }
434 }
435 } else {
437 }
438 }
441 /**
442 * Adds a original_curve to the path. If owner is specified, a reference
443 * will be made, otherwise the curve will be copied into the path.
444 * Any existing curve in the path will be unreferenced first.
445 * This routine triggers reapplication of the an effect is present
446 * an also triggers a request to update the display. Does not write
447 * result to XML when write=false.
448 */
449 void
450 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
451 {
452 if (path->original_curve) {
453 path->original_curve = sp_curve_unref (path->original_curve);
454 }
455 if (curve) {
456 if (owner) {
457 path->original_curve = sp_curve_ref (curve);
458 } else {
459 path->original_curve = sp_curve_copy (curve);
460 }
461 }
462 sp_path_update_patheffect(path, write);
463 SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
464 }
466 /**
467 * Return duplicate of original_curve (if any exists) or NULL if there is no curve
468 */
469 SPCurve *
470 sp_path_get_original_curve (SPPath *path)
471 {
472 if (path->original_curve) {
473 return sp_curve_copy (path->original_curve);
474 }
475 return NULL;
476 }
478 /**
479 * Return duplicate of edittable curve which is original_curve if it exists or
480 * shape->curve if not.
481 */
482 SPCurve*
483 sp_path_get_curve_for_edit (SPPath *path)
484 {
485 if (path->original_curve) {
486 return sp_path_get_original_curve(path);
487 } else {
488 return sp_shape_get_curve( (SPShape *) path );
489 }
490 }
492 /**
493 * Return a reference to original_curve if it exists or
494 * shape->curve if not.
495 */
496 const SPCurve*
497 sp_path_get_curve_reference (SPPath *path)
498 {
499 if (path->original_curve) {
500 return path->original_curve;
501 } else {
502 return path->curve;
503 }
504 }
506 /*
507 Local Variables:
508 mode:c++
509 c-file-style:"stroustrup"
510 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
511 indent-tabs-mode:nil
512 fill-column:99
513 End:
514 */
515 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :