Code

fix compositing for premultiplication and non-alpha cases
[inkscape.git] / src / sp-path.cpp
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"
34 #include "document.h"
36 #define noPATH_VERBOSE
38 static void sp_path_class_init(SPPathClass *klass);
39 static void sp_path_init(SPPath *path);
40 static void sp_path_finalize(GObject *obj);
41 static void sp_path_release(SPObject *object);
43 static void sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
44 static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
46 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
47 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
48 static gchar * sp_path_description(SPItem *item);
50 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
51 static void sp_path_update_patheffect(SPShape *shape, bool write);
53 static SPShapeClass *parent_class;
55 /**
56  * Gets the GType object for SPPathClass
57  */
58 GType
59 sp_path_get_type(void)
60 {
61     static GType type = 0;
63     if (!type) {
64         GTypeInfo info = {
65             sizeof(SPPathClass),
66             NULL, NULL,
67             (GClassInitFunc) sp_path_class_init,
68             NULL, NULL,
69             sizeof(SPPath),
70             16,
71             (GInstanceInitFunc) sp_path_init,
72             NULL,   /* value_table */
73         };
74         type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
75     }
76     return type;
77 }
79 /**
80  *  Does the object-oriented work of initializing the class structure
81  *  including parent class, and registers function pointers for
82  *  the functions build, set, write, and set_transform.
83  */
84 static void
85 sp_path_class_init(SPPathClass * klass)
86 {
87     GObjectClass *gobject_class = (GObjectClass *) klass;
88     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
89     SPItemClass *item_class = (SPItemClass *) klass;
90     SPShapeClass *shape_class = (SPShapeClass *) klass;
92     parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
94     gobject_class->finalize = sp_path_finalize;
96     sp_object_class->build = sp_path_build;
97     sp_object_class->release = sp_path_release;
98     sp_object_class->set = sp_path_set;
99     sp_object_class->write = sp_path_write;
100     sp_object_class->update = sp_path_update;
102     item_class->description = sp_path_description;
103     item_class->set_transform = sp_path_set_transform;
105     shape_class->update_patheffect = sp_path_update_patheffect;
109 gint
110 sp_nodes_in_path(SPPath *path)
112     SPCurve *curve = SP_SHAPE(path)->curve;
113     if (!curve) return 0;
114     gint r = curve->end;
115     gint i = curve->length - 1;
116     if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
117     for (; i >= 0; i --)
118         if (SP_CURVE_BPATH(curve)[i].code == NR_MOVETO)
119             r --;
120     return r;
123 static gchar *
124 sp_path_description(SPItem * item)
126     int count = sp_nodes_in_path(SP_PATH(item));
127     return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
128                                     "<b>Path</b> (%i nodes)",count), count);
131 /**
132  * Initializes an SPPath.
133  */
134 static void
135 sp_path_init(SPPath *path)
137     new (&path->connEndPair) SPConnEndPair(path);
139     path->original_curve = NULL;
142 static void
143 sp_path_finalize(GObject *obj)
145     SPPath *path = (SPPath *) obj;
147     path->connEndPair.~SPConnEndPair();
150 /**
151  *  Given a repr, this sets the data items in the path object such as
152  *  fill & style attributes, markers, and CSS properties.
153  */
154 static void
155 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
157     /* Are these calls actually necessary? */
158     sp_object_read_attr(object, "marker");
159     sp_object_read_attr(object, "marker-start");
160     sp_object_read_attr(object, "marker-mid");
161     sp_object_read_attr(object, "marker-end");
163     sp_conn_end_pair_build(object);
165     if (((SPObjectClass *) parent_class)->build) {
166         ((SPObjectClass *) parent_class)->build(object, document, repr);
167     }
169     sp_object_read_attr(object, "inkscape:original-d");
170     sp_object_read_attr(object, "d");
172     /* d is a required attribute */
173     gchar const *d = sp_object_getAttribute(object, "d", NULL);
174     if (d == NULL) {
175         sp_object_set(object, sp_attribute_lookup("d"), "");
176     }
179 static void
180 sp_path_release(SPObject *object)
182     SPPath *path = SP_PATH(object);
184     path->connEndPair.release();
186     if (path->original_curve) {
187         path->original_curve = sp_curve_unref (path->original_curve);
188     }
190     if (((SPObjectClass *) parent_class)->release) {
191         ((SPObjectClass *) parent_class)->release(object);
192     }
195 /**
196  *  Sets a value in the path object given by 'key', to 'value'.  This is used
197  *  for setting attributes and markers on a path object.
198  */
199 static void
200 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
202     SPPath *path = (SPPath *) object;
204     switch (key) {
205         case SP_ATTR_INKSCAPE_ORIGINAL_D:
206                 if (value) {
207                     NArtBpath *bpath = sp_svg_read_path(value);
208                     SPCurve *curve = sp_curve_new_from_bpath(bpath);
209                     if (curve) {
210                         sp_path_set_original_curve(path, curve, TRUE, true);
211                         sp_curve_unref(curve);
212                     }
213                 } else {
214                     sp_path_set_original_curve(path, NULL, TRUE, true);
215                 }
216                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
217             break;
218        case SP_ATTR_D:
219             if (!((SPShape *) path)->path_effect_href) {
220                 if (value) {
221                     NArtBpath *bpath = sp_svg_read_path(value);
222                     SPCurve *curve = sp_curve_new_from_bpath(bpath);
223                     if (curve) {
224                         sp_shape_set_curve((SPShape *) path, curve, TRUE);
225                         sp_curve_unref(curve);
226                     }
227                 } else {
228                     sp_shape_set_curve((SPShape *) path, NULL, TRUE);
229                 }
230                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
231             }
232             break;
233         case SP_PROP_MARKER:
234         case SP_PROP_MARKER_START:
235         case SP_PROP_MARKER_MID:
236         case SP_PROP_MARKER_END:
237             sp_shape_set_marker(object, key, value);
238             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
239             break;
240         case SP_ATTR_CONNECTOR_TYPE:
241         case SP_ATTR_CONNECTION_START:
242         case SP_ATTR_CONNECTION_END:
243             path->connEndPair.setAttr(key, value);
244             break;
245         default:
246             if (((SPObjectClass *) parent_class)->set) {
247                 ((SPObjectClass *) parent_class)->set(object, key, value);
248             }
249             break;
250     }
253 /**
254  *
255  * Writes the path object into a Inkscape::XML::Node
256  */
257 static Inkscape::XML::Node *
258 sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
260     SPShape *shape = (SPShape *) object;
262     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
263         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
264         repr = xml_doc->createElement("svg:path");
265     }
267     if ( shape->curve != NULL ) {
268         NArtBpath *abp = sp_curve_first_bpath(shape->curve);
269         if (abp) {
270             gchar *str = sp_svg_write_path(abp);
271             repr->setAttribute("d", str);
272             g_free(str);
273         } else {
274             repr->setAttribute("d", "");
275         }
276     } else {
277         repr->setAttribute("d", NULL);
278     }
280     SPPath *path = (SPPath *) object;
281     if ( path->original_curve != NULL ) {
282         NArtBpath *abp = sp_curve_first_bpath(path->original_curve);
283         if (abp) {
284             gchar *str = sp_svg_write_path(abp);
285             repr->setAttribute("inkscape:original-d", str);
286             g_free(str);
287         } else {
288             repr->setAttribute("inkscape:original-d", "");
289         }
290     } else {
291         repr->setAttribute("inkscape:original-d", NULL);
292     }
294     SP_PATH(shape)->connEndPair.writeRepr(repr);
296     if (((SPObjectClass *)(parent_class))->write) {
297         ((SPObjectClass *)(parent_class))->write(object, repr, flags);
298     }
300     return repr;
303 static void
304 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
306     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
307         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
308     }
310     if (((SPObjectClass *) parent_class)->update) {
311         ((SPObjectClass *) parent_class)->update(object, ctx, flags);
312     }
314     SPPath *path = SP_PATH(object);
315     path->connEndPair.update();
319 /**
320  * Writes the given transform into the repr for the given item.
321  */
322 static NR::Matrix
323 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
325     SPShape *shape = (SPShape *) item;
326     SPPath *path = (SPPath *) item;
328     if (!shape->curve) { // 0 nodes, nothing to transform
329         return NR::identity();
330     }
332     if (path->original_curve) { /* Transform the original-d path */
333         NRBPath dorigpath, sorigpath;
334         sorigpath.path = SP_CURVE_BPATH(path->original_curve);
335         nr_path_duplicate_transform(&dorigpath, &sorigpath, xform);
336         SPCurve *origcurve = sp_curve_new_from_bpath(dorigpath.path);
337         if (origcurve) {
338             sp_path_set_original_curve(path, origcurve, TRUE, true);
339             sp_curve_unref(origcurve);
340         }
341     } else {    /* Transform the path */
342         NRBPath dpath, spath;
343         spath.path = SP_CURVE_BPATH(shape->curve);
344         nr_path_duplicate_transform(&dpath, &spath, xform);
345         SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
346         if (curve) {
347             sp_shape_set_curve(shape, curve, TRUE);
348             sp_curve_unref(curve);
349         }
350     }
352     // Adjust stroke
353     sp_item_adjust_stroke(item, NR::expansion(xform));
355     // Adjust pattern fill
356     sp_item_adjust_pattern(item, xform);
358     // Adjust gradient fill
359     sp_item_adjust_gradient(item, xform);
361     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
363     // nothing remains - we've written all of the transform, so return identity
364     return NR::identity();
367 static void
368 sp_path_update_patheffect(SPShape *shape, bool write)
370     SPPath *path = (SPPath *) shape;
371     if (path->original_curve) {
372         SPCurve *curve = sp_curve_copy (path->original_curve);
373         sp_shape_perform_path_effect(curve, shape);
374         sp_shape_set_curve(shape, curve, TRUE);
375         sp_curve_unref(curve);
377         if (write) {
378             // could also do SP_OBJECT(shape)->updateRepr();  but only the d attribute needs updating.
379             Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
380             if ( shape->curve != NULL ) {
381                 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
382                 if (abp) {
383                     gchar *str = sp_svg_write_path(abp);
384                     repr->setAttribute("d", str);
385                     g_free(str);
386                 } else {
387                     repr->setAttribute("d", "");
388                 }
389             } else {
390                 repr->setAttribute("d", NULL);
391             }
392         }
393     } else {
395     }
399 /**
400  * Adds a original_curve to the path.  If owner is specified, a reference
401  * will be made, otherwise the curve will be copied into the path.
402  * Any existing curve in the path will be unreferenced first.
403  * This routine triggers reapplication of the an effect is present
404  * an also triggers a request to update the display. Does not write
405 * result to XML when write=false.
406  */
407 void
408 sp_path_set_original_curve (SPPath *path, SPCurve *curve, unsigned int owner, bool write)
410     if (path->original_curve) {
411         path->original_curve = sp_curve_unref (path->original_curve);
412     }
413     if (curve) {
414         if (owner) {
415             path->original_curve = sp_curve_ref (curve);
416         } else {
417             path->original_curve = sp_curve_copy (curve);
418         }
419     }
420     sp_path_update_patheffect(path, write);
421     SP_OBJECT(path)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
424 /**
425  * Return duplicate of original_curve (if any exists) or NULL if there is no curve
426  */
427 SPCurve *
428 sp_path_get_original_curve (SPPath *path)
430     if (path->original_curve) {
431         return sp_curve_copy (path->original_curve);
432     }
433     return NULL;
436 /**
437  * Return duplicate of edittable curve which is original_curve if it exists or
438  * shape->curve if not.
439  */
440 SPCurve*
441 sp_path_get_curve_for_edit (SPPath *path)
443     if (path->original_curve) {
444         return sp_path_get_original_curve(path);
445     } else {
446         return sp_shape_get_curve( (SPShape *) path );
447     }
450 /*
451   Local Variables:
452   mode:c++
453   c-file-style:"stroustrup"
454   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
455   indent-tabs-mode:nil
456   fill-column:99
457   End:
458 */
459 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :