Code

patch by Jasper: concatenate curves, not path strings (which would break with relativ...
[inkscape.git] / src / path-chemistry.cpp
1 #define __SP_PATH_CHEMISTRY_C__
3 /*
4  * Here are handlers for modifying selections, specific to paths
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
10  *
11  * Copyright (C) 1999-2008 Authors
12  * Copyright (C) 2001-2002 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
20 #include <cstring>
21 #include <string>
22 #include "xml/repr.h"
23 #include "svg/svg.h"
24 #include "display/curve.h"
25 #include <glib/gmem.h>
26 #include <glibmm/i18n.h>
27 #include "sp-path.h"
28 #include "sp-text.h"
29 #include "sp-flowtext.h"
30 #include "libnr/nr-path.h"
31 #include "text-editing.h"
32 #include "style.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "document.h"
36 #include "message-stack.h"
37 #include "selection.h"
38 #include "desktop-handles.h"
39 #include "box3d.h"
41 #include "path-chemistry.h"
43 /* Helper functions for sp_selected_path_to_curves */
44 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
45 enum {
46     /* Not used yet. This is the placeholder of Lauris's idea. */
47     SP_TOCURVE_INTERACTIVE       = 1 << 0,
48     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
49     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
50     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
51 };
53 void
54 sp_selected_path_combine(void)
55 {
56     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
57     Inkscape::Selection *selection = sp_desktop_selection(desktop);
58     
59     if (g_slist_length((GSList *) selection->itemList()) < 2) {
60         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
61         return;
62     }
64     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
65     // set "busy" cursor
66     desktop->setWaitingCursor();
68     sp_selected_path_to_curves0(FALSE, 0);
70     GSList *items = g_slist_copy((GSList *) selection->itemList());
71     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
72     items = g_slist_reverse(items);
74     // remember the position, id and style of the topmost path, they will be assigned to the combined one
75     gint position = 0;
76     char const *id = NULL;
77     gchar *style = NULL;
79     SPCurve* curve = 0;
80     bool did = false;
81     SPItem *first = NULL;
82     Inkscape::XML::Node *parent = NULL; 
84     for (GSList *i = items; i != NULL; i = i->next) {  // going from top to bottom
86         SPItem *item = (SPItem *) i->data;
87         if (!SP_IS_PATH(item))
88             continue;
89         did = true;
91         NArtBpath *abp = NULL;
92         SPCurve *c = sp_shape_get_curve(SP_SHAPE(item));
93         if (first == NULL) {  // this is the topmost path
94             first = item;
95             parent = SP_OBJECT_REPR(first)->parent();
96             position = SP_OBJECT_REPR(first)->position();
97             id = SP_OBJECT_REPR(first)->attribute("id");
98             // FIXME: merge styles of combined objects instead of using the first one's style
99             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
100             sp_curve_transform(c, item->transform);
101             curve = c;
102         } else {
103             sp_curve_transform(c, item->getRelativeTransform(SP_OBJECT(first)));
104             sp_curve_append(curve, c, false);
105             sp_curve_unref(c);
106         }
108         // unless this is the topmost object,
109         if (item != first) {
110             // reduce position only if the same parent
111             if (SP_OBJECT_REPR(item)->parent() == parent)
112                 position--;
113             // delete the object for real, so that its clones can take appropriate action
114             SP_OBJECT(item)->deleteObject();
115         }
116     }
118     g_slist_free(items);
120     if (did) {
121         selection->clear();
123         // delete the topmost one so that its clones don't get alerted; this object will be
124         // restored shortly, with the same id
125         SP_OBJECT(first)->deleteObject(false);
127         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
128         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
130         // restore id and style
131         repr->setAttribute("id", id);
133         repr->setAttribute("style", style);
134         g_free(style);
136         // set path data corresponding to new curve
137         gchar *dstring = sp_svg_write_path(SP_CURVE_BPATH(curve));
138         sp_curve_unref(curve);
139         repr->setAttribute("d", dstring);
140         g_free(dstring);
142         // add the new group to the parent of the topmost
143         parent->appendChild(repr);
145         // move to the position of the topmost, reduced by the number of deleted items
146         repr->setPosition(position > 0 ? position : 0);
148         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
149                          _("Combine"));
151         selection->set(repr);
153         Inkscape::GC::release(repr);
155     } else {
156         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
157     }
159     desktop->clearWaitingCursor();
162 void
163 sp_selected_path_break_apart(void)
165     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
167     Inkscape::Selection *selection = sp_desktop_selection(desktop);
169     if (selection->isEmpty()) {
170         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
171         return;
172     }
174     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
175     // set "busy" cursor
176     desktop->setWaitingCursor();
178     bool did = false;
180     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
181          items != NULL;
182          items = items->next) {
184         SPItem *item = (SPItem *) items->data;
186         if (!SP_IS_PATH(item))
187             continue;
189         SPPath *path = SP_PATH(item);
191         SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
192         if (curve == NULL)
193             continue;
195         did = true;
197         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
198         gint pos = SP_OBJECT_REPR(item)->position();
199         char const *id = SP_OBJECT_REPR(item)->attribute("id");
201         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
203         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
205         sp_curve_unref(curve);
207         // it's going to resurrect as one of the pieces, so we delete without advertisement
208         SP_OBJECT(item)->deleteObject(false);
210         curve = sp_curve_new_from_bpath(abp);
211         g_assert(curve != NULL);
213         GSList *list = sp_curve_split(curve);
215         sp_curve_unref(curve);
217         GSList *reprs = NULL;
218         for (GSList *l = list; l != NULL; l = l->next) {
219             curve = (SPCurve *) l->data;
221             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
222             repr->setAttribute("style", style);
224             gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
225             repr->setAttribute("d", str);
226             g_free(str);
228             // add the new repr to the parent
229             parent->appendChild(repr);
231             // move to the saved position
232             repr->setPosition(pos > 0 ? pos : 0);
234             // if it's the first one, restore id
235             if (l == list)
236                 repr->setAttribute("id", id);
238             reprs = g_slist_prepend (reprs, repr);
240             Inkscape::GC::release(repr);
241         }
243         selection->setReprList(reprs);
245         g_slist_free(reprs);
246         g_slist_free(list);
247         g_free(style);
249     }
251     desktop->clearWaitingCursor();
253     if (did) {
254         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
255                          _("Break apart"));
256     } else {
257         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
258     }
261 /* This function is an entry point from GUI */
262 void
263 sp_selected_path_to_curves(void)
265     sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
268 static void
269 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
271     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
273     Inkscape::Selection *selection = sp_desktop_selection(desktop);
275     if (selection->isEmpty()) {
276         if (interactive)
277             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
278         return;
279     }
281     bool did = false;
282     if (interactive) {
283         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
284         // set "busy" cursor
285         desktop->setWaitingCursor();
286     }
288     GSList *selected = g_slist_copy((GSList *) selection->itemList());
289     GSList *to_select = NULL;
290     selection->clear();
291     GSList *items = g_slist_copy(selected);
293     for (;
294          items != NULL;
295          items = items->next) {
297         SPItem *item = SP_ITEM(items->data);
299         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
300             continue; // already a path, and no path effect
301         }
303         if (SP_IS_BOX3D(item)) {
304             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
305             Inkscape::XML::Node *repr = box3d_convert_to_group(SP_BOX3D(item));
306             
307             if (repr) {
308                 to_select = g_slist_prepend (to_select, repr);
309                 did = true;
310                 selected = g_slist_remove (selected, item);
311             }
313             continue;
314         }
316         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
317         if (!repr)
318             continue;
320         did = true;
321         selected = g_slist_remove (selected, item);
323         // remember the position of the item
324         gint pos = SP_OBJECT_REPR(item)->position();
325         // remember parent
326         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
327         // remember id
328         char const *id = SP_OBJECT_REPR(item)->attribute("id");
330         // It's going to resurrect, so we delete without notifying listeners.
331         SP_OBJECT(item)->deleteObject(false);
333         // restore id
334         repr->setAttribute("id", id);
335         // add the new repr to the parent
336         parent->appendChild(repr);
337         // move to the saved position
338         repr->setPosition(pos > 0 ? pos : 0);
340         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
341          * desktops where it was previously selected. */
342         to_select = g_slist_prepend (to_select, repr);
343         Inkscape::GC::release(repr);
344     }
346     g_slist_free (items);
347     selection->setReprList(to_select);
348     selection->addList(selected);
349     g_slist_free (to_select);
350     g_slist_free (selected);
352     if (interactive) {
353         desktop->clearWaitingCursor();
354         if (did) {
355             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
356                              _("Object to path"));
357         } else {
358             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
359             return;
360         }
361     }
364 Inkscape::XML::Node *
365 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
367     if (!item)
368         return NULL;
370     SPCurve *curve = NULL;
371     if (SP_IS_SHAPE(item)) {
372         curve = sp_shape_get_curve(SP_SHAPE(item));
373     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
374         curve = te_get_layout(item)->convertToCurves();
375     }
377     if (!curve)
378         return NULL;
380     // Prevent empty paths from being added to the document
381     // otherwise we end up with zomby markup in the SVG file
382     if(curve->end <= 0)
383     {
384         sp_curve_unref(curve);
385         return NULL;
386     }
388     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
389     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
390     /* Transformation */
391     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
392     /* Style */
393     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
394                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
395     repr->setAttribute("style", style_str);
396     g_free(style_str);
398     /* Mask */
399     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
400     if ( mask_str )
401         repr->setAttribute("mask", mask_str);
403     /* Clip path */
404     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
405     if ( clip_path_str )
406         repr->setAttribute("clip-path", clip_path_str);
408     /* Rotation center */
409     sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
410     sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
412     /* Definition */
413     gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
414     repr->setAttribute("d", def_str);
415     g_free(def_str);
416     sp_curve_unref(curve);
417     return repr;
421 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
422 void
423 sp_selected_path_reverse()
425     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
427     Inkscape::Selection *selection = sp_desktop_selection(desktop);
428     GSList *items = (GSList *) selection->itemList();
430     if (!items) {
431         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
432         return;
433     }
436     // set "busy" cursor
437     desktop->setWaitingCursor();
439     bool did = false;
440     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
442     for (GSList *i = items; i != NULL; i = i->next) {
444         if (!SP_IS_PATH(i->data))
445             continue;
447         did = true;
448         SPPath *path = SP_PATH(i->data);
450         SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
452         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
453         if ( sp_shape_has_path_effect(SP_SHAPE(path)) ) {
454             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
455         } else {
456             SP_OBJECT_REPR(path)->setAttribute("d", str);
457         }
458         g_free(str);
460         sp_curve_unref(rcurve);
461     }
463     desktop->clearWaitingCursor();
465     if (did) {
466         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
467                          _("Reverse path"));
468     } else {
469         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
470     }
473 /*
474   Local Variables:
475   mode:c++
476   c-file-style:"stroustrup"
477   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478   indent-tabs-mode:nil
479   fill-column:99
480   End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :