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 #if defined(WIN32) || defined(__APPLE__)
22 # include <glibmm/i18n.h>
23 #endif
25 #include <display/curve.h>
26 #include <libnr/n-art-bpath.h>
27 #include <libnr/nr-path.h>
28 #include <libnr/nr-matrix-fns.h>
30 #include "svg/svg.h"
31 #include "xml/repr.h"
32 #include "attributes.h"
34 #include "sp-path.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);
52 static SPShapeClass *parent_class;
54 /**
55 * Gets the GType object for SPPathClass
56 */
57 GType
58 sp_path_get_type(void)
59 {
60 static GType type = 0;
62 if (!type) {
63 GTypeInfo info = {
64 sizeof(SPPathClass),
65 NULL, NULL,
66 (GClassInitFunc) sp_path_class_init,
67 NULL, NULL,
68 sizeof(SPPath),
69 16,
70 (GInstanceInitFunc) sp_path_init,
71 NULL, /* value_table */
72 };
73 type = g_type_register_static(SP_TYPE_SHAPE, "SPPath", &info, (GTypeFlags)0);
74 }
75 return type;
76 }
78 /**
79 * Does the object-oriented work of initializing the class structure
80 * including parent class, and registers function pointers for
81 * the functions build, set, write, and set_transform.
82 */
83 static void
84 sp_path_class_init(SPPathClass * klass)
85 {
86 GObjectClass *gobject_class = (GObjectClass *) klass;
87 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
88 SPItemClass *item_class = (SPItemClass *) klass;
90 parent_class = (SPShapeClass *)g_type_class_peek_parent(klass);
92 gobject_class->finalize = sp_path_finalize;
94 sp_object_class->build = sp_path_build;
95 sp_object_class->release = sp_path_release;
96 sp_object_class->set = sp_path_set;
97 sp_object_class->write = sp_path_write;
98 sp_object_class->update = sp_path_update;
100 item_class->description = sp_path_description;
101 item_class->set_transform = sp_path_set_transform;
102 }
105 gint
106 sp_nodes_in_path(SPPath *path)
107 {
108 SPCurve *curve = SP_SHAPE(path)->curve;
109 if (!curve) return 0;
110 gint r = curve->end;
111 gint i = curve->length - 1;
112 if (i > r) i = r; // sometimes after switching from node editor length is wrong, e.g. f6 - draw - f2 - tab - f1, this fixes it
113 for (; i >= 0; i --)
114 if ((curve->bpath + i) -> code == NR_MOVETO)
115 r --;
116 return r;
117 }
119 static gchar *
120 sp_path_description(SPItem * item)
121 {
122 int count = sp_nodes_in_path(SP_PATH(item));
123 return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
124 "<b>Path</b> (%i nodes)",count), count);
125 }
127 /**
128 * Initializes an SPPath. Currently does nothing.
129 */
130 static void
131 sp_path_init(SPPath *path)
132 {
133 new (&path->connEndPair) SPConnEndPair(path);
134 }
136 static void
137 sp_path_finalize(GObject *obj)
138 {
139 SPPath *path = (SPPath *) obj;
141 path->connEndPair.~SPConnEndPair();
142 }
144 /**
145 * Given a repr, this sets the data items in the path object such as
146 * fill & style attributes, markers, and CSS properties.
147 */
148 static void
149 sp_path_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
150 {
151 sp_object_read_attr(object, "d");
153 /* d is a required attribute */
154 gchar const *d = sp_object_getAttribute(object, "d", NULL);
155 if (d == NULL) {
156 sp_object_set(object, sp_attribute_lookup("d"), "");
157 }
159 /* Are these calls actually necessary? */
160 sp_object_read_attr(object, "marker");
161 sp_object_read_attr(object, "marker-start");
162 sp_object_read_attr(object, "marker-mid");
163 sp_object_read_attr(object, "marker-end");
165 sp_conn_end_pair_build(object);
167 if (((SPObjectClass *) parent_class)->build) {
168 ((SPObjectClass *) parent_class)->build(object, document, repr);
169 }
170 }
172 static void
173 sp_path_release(SPObject *object)
174 {
175 SPPath *path = SP_PATH(object);
177 path->connEndPair.release();
179 if (((SPObjectClass *) parent_class)->release) {
180 ((SPObjectClass *) parent_class)->release(object);
181 }
182 }
184 /**
185 * Sets a value in the path object given by 'key', to 'value'. This is used
186 * for setting attributes and markers on a path object.
187 */
188 static void
189 sp_path_set(SPObject *object, unsigned int key, gchar const *value)
190 {
191 SPPath *path = (SPPath *) object;
193 switch (key) {
194 case SP_ATTR_D:
195 if (value) {
196 NArtBpath *bpath = sp_svg_read_path(value);
197 SPCurve *curve = sp_curve_new_from_bpath(bpath);
198 if (curve) {
199 sp_shape_set_curve((SPShape *) path, curve, TRUE);
200 sp_curve_unref(curve);
201 }
202 } else {
203 sp_shape_set_curve((SPShape *) path, NULL, TRUE);
204 }
205 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206 break;
207 case SP_PROP_MARKER:
208 case SP_PROP_MARKER_START:
209 case SP_PROP_MARKER_MID:
210 case SP_PROP_MARKER_END:
211 sp_shape_set_marker(object, key, value);
212 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
213 break;
214 case SP_ATTR_CONNECTOR_TYPE:
215 case SP_ATTR_CONNECTION_START:
216 case SP_ATTR_CONNECTION_END:
217 path->connEndPair.setAttr(key, value);
218 break;
219 default:
220 if (((SPObjectClass *) parent_class)->set) {
221 ((SPObjectClass *) parent_class)->set(object, key, value);
222 }
223 break;
224 }
225 }
227 /**
228 *
229 * Writes the path object into a Inkscape::XML::Node
230 */
231 static Inkscape::XML::Node *
232 sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
233 {
234 SPShape *shape = (SPShape *) object;
236 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
237 repr = sp_repr_new("svg:path");
238 }
240 if ( shape->curve != NULL ) {
241 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
242 if (abp) {
243 gchar *str = sp_svg_write_path(abp);
244 repr->setAttribute("d", str);
245 g_free(str);
246 } else {
247 repr->setAttribute("d", "");
248 }
249 } else {
250 repr->setAttribute("d", NULL);
251 }
253 SP_PATH(shape)->connEndPair.writeRepr(repr);
255 if (((SPObjectClass *)(parent_class))->write) {
256 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
257 }
259 return repr;
260 }
262 static void
263 sp_path_update(SPObject *object, SPCtx *ctx, guint flags)
264 {
265 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
266 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
267 }
269 if (((SPObjectClass *) parent_class)->update) {
270 ((SPObjectClass *) parent_class)->update(object, ctx, flags);
271 }
273 SPPath *path = SP_PATH(object);
274 path->connEndPair.update();
275 }
278 /**
279 * Writes the given transform into the repr for the given item.
280 */
281 static NR::Matrix
282 sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
283 {
284 SPShape *shape = (SPShape *) item;
286 if (!shape->curve) { // 0 nodes, nothing to transform
287 return NR::identity();
288 }
290 /* Transform the path */
291 NRBPath dpath, spath;
292 spath.path = shape->curve->bpath;
293 nr_path_duplicate_transform(&dpath, &spath, xform);
294 SPCurve *curve = sp_curve_new_from_bpath(dpath.path);
295 if (curve) {
296 sp_shape_set_curve(shape, curve, TRUE);
297 sp_curve_unref(curve);
298 }
300 // Adjust stroke
301 sp_item_adjust_stroke(item, NR::expansion(xform));
303 // Adjust pattern fill
304 sp_item_adjust_pattern(item, xform);
306 // Adjust gradient fill
307 sp_item_adjust_gradient(item, xform);
309 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
311 // nothing remains - we've written all of the transform, so return identity
312 return NR::identity();
313 }
316 /*
317 Local Variables:
318 mode:c++
319 c-file-style:"stroustrup"
320 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
321 indent-tabs-mode:nil
322 fill-column:99
323 End:
324 */
325 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :