Code

4c459ba64dc563b2b76ea151ed3335203cf9efda
[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 "desktop.h"
34 #include "document.h"
35 #include "message-stack.h"
36 #include "selection.h"
37 #include "desktop-handles.h"
38 #include "box3d.h"
39 #include <2geom/pathvector.h>
40 #include "path-chemistry.h"
42 /* Helper functions for sp_selected_path_to_curves */
43 static void sp_selected_path_to_curves0(SPDesktop *desktop, bool do_document_done, guint32 text_grouping_policy);
44 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
46 enum {
47     /* Not used yet. This is the placeholder of Lauris's idea. */
48     SP_TOCURVE_INTERACTIVE       = 1 << 0,
49     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
50     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
51     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
52 };
54 void
55 sp_selected_path_combine(SPDesktop *desktop)
56 {
57     Inkscape::Selection *selection = sp_desktop_selection(desktop);
58     SPDocument *doc = sp_desktop_document(desktop);
59     
60     if (g_slist_length((GSList *) selection->itemList()) < 2) {
61         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
62         return;
63     }
65     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
66     // set "busy" cursor
67     desktop->setWaitingCursor();
69     GSList *items = g_slist_copy((GSList *) selection->itemList());
70     GSList *to_paths = NULL;
71     for (GSList *i = items; i != NULL; i = i->next) {
72         SPItem *item = (SPItem *) i->data;
73         if (!SP_IS_PATH(item))
74             to_paths = g_slist_prepend(to_paths, item);
75     }
76     GSList *converted = NULL;
77     bool did = sp_item_list_to_curves(to_paths, &items, &converted);
78     g_slist_free(to_paths);
79     for (GSList *i = converted; i != NULL; i = i->next)
80         items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
82     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
83     items = g_slist_reverse(items);
85     // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
86     gint position = 0;
87     char const *id = NULL;
88     char const *transform = NULL;
89     gchar *style = NULL;
90     gchar *path_effect = NULL;
92     SPCurve* curve = 0;
93     SPItem *first = NULL;
94     Inkscape::XML::Node *parent = NULL; 
96     for (GSList *i = items; i != NULL; i = i->next) {  // going from top to bottom
98         SPItem *item = (SPItem *) i->data;
99         if (!SP_IS_PATH(item))
100             continue;
101         did = true;
103         SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
104         if (first == NULL) {  // this is the topmost path
105             first = item;
106             parent = SP_OBJECT_REPR(first)->parent();
107             position = SP_OBJECT_REPR(first)->position();
108             id = SP_OBJECT_REPR(first)->attribute("id");
109             transform = SP_OBJECT_REPR(first)->attribute("transform");
110             // FIXME: merge styles of combined objects instead of using the first one's style
111             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
112             path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
113             //c->transform(item->transform);
114             curve = c;
115         } else {
116             c->transform(item->getRelativeTransform(SP_OBJECT(first)));
117             curve->append(c, false);
118             c->unref();
119         }
121         // unless this is the topmost object,
122         if (item != first) {
123             // reduce position only if the same parent
124             if (SP_OBJECT_REPR(item)->parent() == parent)
125                 position--;
126             // delete the object for real, so that its clones can take appropriate action
127             SP_OBJECT(item)->deleteObject();
128         }
129     }
131     g_slist_free(items);
133     if (did) {
134         selection->clear();
136         // delete the topmost one so that its clones don't get alerted; this object will be
137         // restored shortly, with the same id
138         SP_OBJECT(first)->deleteObject(false);
140         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
141         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
143         // restore id, transform, path effect, and style
144         repr->setAttribute("id", id);
145         if (transform) repr->setAttribute("transform", transform);
146         repr->setAttribute("style", style);
147         g_free(style);
149         // set path data corresponding to new curve
150         gchar *dstring = sp_svg_write_path(curve->get_pathvector());
151         curve->unref();
152         repr->setAttribute("d", dstring);
153         if (path_effect)
154             repr->setAttribute("inkscape:original-d", dstring);
155         g_free(dstring);
157         repr->setAttribute("inkscape:path-effect", path_effect);
158         g_free(path_effect);
160         // add the new group to the parent of the topmost
161         parent->appendChild(repr);
163         // move to the position of the topmost, reduced by the number of deleted items
164         repr->setPosition(position > 0 ? position : 0);
166         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
167                          _("Combine"));
169         selection->set(repr);
171         Inkscape::GC::release(repr);
173     } else {
174         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
175     }
177     desktop->clearWaitingCursor();
180 void
181 sp_selected_path_break_apart(SPDesktop *desktop)
183     Inkscape::Selection *selection = sp_desktop_selection(desktop);
185     if (selection->isEmpty()) {
186         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
187         return;
188     }
190     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
191     // set "busy" cursor
192     desktop->setWaitingCursor();
194     bool did = false;
196     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
197          items != NULL;
198          items = items->next) {
200         SPItem *item = (SPItem *) items->data;
202         if (!SP_IS_PATH(item))
203             continue;
205         SPPath *path = SP_PATH(item);
207         SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
208         if (curve == NULL)
209             continue;
211         did = true;
213         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
214         gint pos = SP_OBJECT_REPR(item)->position();
215         char const *id = SP_OBJECT_REPR(item)->attribute("id");
217         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
218         gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
220         Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
222         curve->unref();
224         // it's going to resurrect as one of the pieces, so we delete without advertisement
225         SP_OBJECT(item)->deleteObject(false);
227         curve = new SPCurve(apv);
228         g_assert(curve != NULL);
230         GSList *list = curve->split();
232         curve->unref();
234         GSList *reprs = NULL;
235         for (GSList *l = list; l != NULL; l = l->next) {
236             curve = (SPCurve *) l->data;
238             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
239             repr->setAttribute("style", style);
241             gchar *str = sp_svg_write_path(curve->get_pathvector());
242             repr->setAttribute("d", str);
243             if (path_effect)
244                 repr->setAttribute("inkscape:original-d", str);
245             g_free(str);
247             repr->setAttribute("inkscape:path-effect", path_effect);
249             // add the new repr to the parent
250             parent->appendChild(repr);
252             // move to the saved position
253             repr->setPosition(pos > 0 ? pos : 0);
255             // if it's the first one, restore id
256             if (l == list)
257                 repr->setAttribute("id", id);
259             reprs = g_slist_prepend (reprs, repr);
261             Inkscape::GC::release(repr);
262         }
264         selection->setReprList(reprs);
266         g_slist_free(reprs);
267         g_slist_free(list);
268         g_free(style);
270     }
272     desktop->clearWaitingCursor();
274     if (did) {
275         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
276                          _("Break apart"));
277     } else {
278         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
279     }
282 /* This function is an entry point from GUI */
283 void
284 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
286     sp_selected_path_to_curves0(desktop, interactive, interactive ? SP_TOCURVE_INTERACTIVE : 0);
289 static void
290 sp_selected_path_to_curves0(SPDesktop *desktop, bool interactive, guint32 /*text_grouping_policy*/)
292     Inkscape::Selection *selection = sp_desktop_selection(desktop);
294     if (selection->isEmpty()) {
295         if (interactive)
296             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
297         return;
298     }
300     bool did = false;
301     if (interactive) {
302         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
303         // set "busy" cursor
304         desktop->setWaitingCursor();
305     }
307     GSList *selected = g_slist_copy((GSList *) selection->itemList());
308     GSList *to_select = NULL;
309     selection->clear();
310     GSList *items = g_slist_copy(selected);
312     did = sp_item_list_to_curves(items, &selected, &to_select);
314     g_slist_free (items);
315     selection->setReprList(to_select);
316     selection->addList(selected);
317     g_slist_free (to_select);
318     g_slist_free (selected);
320     if (interactive) {
321         desktop->clearWaitingCursor();
322         if (did) {
323             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
324                              _("Object to path"));
325         } else {
326             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
327             return;
328         }
329     }
332 static bool
333 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
335     bool did = false;
336     
337     for (;
338          items != NULL;
339          items = items->next) {
341         SPItem *item = SP_ITEM(items->data);
343         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
344             continue; // already a path, and no path effect
345         }
347         if (SP_IS_BOX3D(item)) {
348             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
349             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
350             
351             if (repr) {
352                 *to_select = g_slist_prepend (*to_select, repr);
353                 did = true;
354                 *selected = g_slist_remove (*selected, item);
355             }
357             continue;
358         }
359         
360         if (SP_IS_GROUP(item)) {
361             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
362             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
363             
364             GSList *item_to_select = NULL;
365             GSList *item_selected = NULL;
366             
367             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
368                 did = true;
370             g_slist_free(item_list);
371             g_slist_free(item_to_select);
372             g_slist_free(item_selected);
374             continue;
375         }
377         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
378         if (!repr)
379             continue;
381         did = true;
382         *selected = g_slist_remove (*selected, item);
384         // remember the position of the item
385         gint pos = SP_OBJECT_REPR(item)->position();
386         // remember parent
387         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
388         // remember id
389         char const *id = SP_OBJECT_REPR(item)->attribute("id");
391         // It's going to resurrect, so we delete without notifying listeners.
392         SP_OBJECT(item)->deleteObject(false);
394         // restore id
395         repr->setAttribute("id", id);
396         // add the new repr to the parent
397         parent->appendChild(repr);
398         // move to the saved position
399         repr->setPosition(pos > 0 ? pos : 0);
401         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
402          * desktops where it was previously selected. */
403         *to_select = g_slist_prepend (*to_select, repr);
404         Inkscape::GC::release(repr);
405     }
406     
407     return did;
410 Inkscape::XML::Node *
411 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
413     if (!item)
414         return NULL;
416     SPCurve *curve = NULL;
417     if (SP_IS_SHAPE(item)) {
418         curve = sp_shape_get_curve(SP_SHAPE(item));
419     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
420         curve = te_get_layout(item)->convertToCurves();
421     }
423     if (!curve)
424         return NULL;
426     // Prevent empty paths from being added to the document
427     // otherwise we end up with zomby markup in the SVG file
428     if(curve->is_empty())
429     {
430         curve->unref();
431         return NULL;
432     }
434     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
435     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
436     /* Transformation */
437     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
438     /* Style */
439     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
440                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
441     repr->setAttribute("style", style_str);
442     g_free(style_str);
444     /* Mask */
445     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
446     if ( mask_str )
447         repr->setAttribute("mask", mask_str);
449     /* Clip path */
450     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
451     if ( clip_path_str )
452         repr->setAttribute("clip-path", clip_path_str);
454     /* Rotation center */
455     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
456     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
458     /* Definition */
459     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
460     repr->setAttribute("d", def_str);
461     g_free(def_str);
462     curve->unref();
463     return repr;
467 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
468 void
469 sp_selected_path_reverse(SPDesktop *desktop)
471     Inkscape::Selection *selection = sp_desktop_selection(desktop);
472     GSList *items = (GSList *) selection->itemList();
474     if (!items) {
475         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
476         return;
477     }
480     // set "busy" cursor
481     desktop->setWaitingCursor();
483     bool did = false;
484     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
486     for (GSList *i = items; i != NULL; i = i->next) {
488         if (!SP_IS_PATH(i->data))
489             continue;
491         did = true;
492         SPPath *path = SP_PATH(i->data);
494         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
496         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
497         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
498             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
499         } else {
500             SP_OBJECT_REPR(path)->setAttribute("d", str);
501         }
502         g_free(str);
504         rcurve->unref();
505     }
507     desktop->clearWaitingCursor();
509     if (did) {
510         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
511                          _("Reverse path"));
512     } else {
513         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
514     }
517 /*
518   Local Variables:
519   mode:c++
520   c-file-style:"stroustrup"
521   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
522   indent-tabs-mode:nil
523   fill-column:99
524   End:
525 */
526 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :