Code

when applying LPE to rect, convert it to path first
[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 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
47 enum {
48     /* Not used yet. This is the placeholder of Lauris's idea. */
49     SP_TOCURVE_INTERACTIVE       = 1 << 0,
50     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
51     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
52     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
53 };
55 void
56 sp_selected_path_combine(void)
57 {
58     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
59     Inkscape::Selection *selection = sp_desktop_selection(desktop);
60     
61     if (g_slist_length((GSList *) selection->itemList()) < 2) {
62         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
63         return;
64     }
66     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
67     // set "busy" cursor
68     desktop->setWaitingCursor();
70     sp_selected_path_to_curves0(FALSE, 0);
72     GSList *items = g_slist_copy((GSList *) selection->itemList());
73     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
74     items = g_slist_reverse(items);
76     // remember the position, id and style of the topmost path, they will be assigned to the combined one
77     gint position = 0;
78     char const *id = NULL;
79     gchar *style = NULL;
81     SPCurve* curve = 0;
82     bool did = false;
83     SPItem *first = NULL;
84     Inkscape::XML::Node *parent = NULL; 
86     for (GSList *i = items; i != NULL; i = i->next) {  // going from top to bottom
88         SPItem *item = (SPItem *) i->data;
89         if (!SP_IS_PATH(item))
90             continue;
91         did = true;
93         SPCurve *c = sp_shape_get_curve(SP_SHAPE(item));
94         if (first == NULL) {  // this is the topmost path
95             first = item;
96             parent = SP_OBJECT_REPR(first)->parent();
97             position = SP_OBJECT_REPR(first)->position();
98             id = SP_OBJECT_REPR(first)->attribute("id");
99             // FIXME: merge styles of combined objects instead of using the first one's style
100             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
101             sp_curve_transform(c, item->transform);
102             curve = c;
103         } else {
104             sp_curve_transform(c, item->getRelativeTransform(SP_OBJECT(first)));
105             sp_curve_append(curve, c, false);
106             sp_curve_unref(c);
107         }
109         // unless this is the topmost object,
110         if (item != first) {
111             // reduce position only if the same parent
112             if (SP_OBJECT_REPR(item)->parent() == parent)
113                 position--;
114             // delete the object for real, so that its clones can take appropriate action
115             SP_OBJECT(item)->deleteObject();
116         }
117     }
119     g_slist_free(items);
121     if (did) {
122         selection->clear();
124         // delete the topmost one so that its clones don't get alerted; this object will be
125         // restored shortly, with the same id
126         SP_OBJECT(first)->deleteObject(false);
128         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
129         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
131         // restore id and style
132         repr->setAttribute("id", id);
134         repr->setAttribute("style", style);
135         g_free(style);
137         // set path data corresponding to new curve
138         gchar *dstring = sp_svg_write_path(SP_CURVE_BPATH(curve));
139         sp_curve_unref(curve);
140         repr->setAttribute("d", dstring);
141         g_free(dstring);
143         // add the new group to the parent of the topmost
144         parent->appendChild(repr);
146         // move to the position of the topmost, reduced by the number of deleted items
147         repr->setPosition(position > 0 ? position : 0);
149         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
150                          _("Combine"));
152         selection->set(repr);
154         Inkscape::GC::release(repr);
156     } else {
157         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
158     }
160     desktop->clearWaitingCursor();
163 void
164 sp_selected_path_break_apart(void)
166     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
168     Inkscape::Selection *selection = sp_desktop_selection(desktop);
170     if (selection->isEmpty()) {
171         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
172         return;
173     }
175     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
176     // set "busy" cursor
177     desktop->setWaitingCursor();
179     bool did = false;
181     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
182          items != NULL;
183          items = items->next) {
185         SPItem *item = (SPItem *) items->data;
187         if (!SP_IS_PATH(item))
188             continue;
190         SPPath *path = SP_PATH(item);
192         SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
193         if (curve == NULL)
194             continue;
196         did = true;
198         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
199         gint pos = SP_OBJECT_REPR(item)->position();
200         char const *id = SP_OBJECT_REPR(item)->attribute("id");
202         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
204         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
206         sp_curve_unref(curve);
208         // it's going to resurrect as one of the pieces, so we delete without advertisement
209         SP_OBJECT(item)->deleteObject(false);
211         curve = sp_curve_new_from_bpath(abp);
212         g_assert(curve != NULL);
214         GSList *list = sp_curve_split(curve);
216         sp_curve_unref(curve);
218         GSList *reprs = NULL;
219         for (GSList *l = list; l != NULL; l = l->next) {
220             curve = (SPCurve *) l->data;
222             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
223             repr->setAttribute("style", style);
225             gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
226             repr->setAttribute("d", str);
227             g_free(str);
229             // add the new repr to the parent
230             parent->appendChild(repr);
232             // move to the saved position
233             repr->setPosition(pos > 0 ? pos : 0);
235             // if it's the first one, restore id
236             if (l == list)
237                 repr->setAttribute("id", id);
239             reprs = g_slist_prepend (reprs, repr);
241             Inkscape::GC::release(repr);
242         }
244         selection->setReprList(reprs);
246         g_slist_free(reprs);
247         g_slist_free(list);
248         g_free(style);
250     }
252     desktop->clearWaitingCursor();
254     if (did) {
255         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
256                          _("Break apart"));
257     } else {
258         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
259     }
262 /* This function is an entry point from GUI */
263 void
264 sp_selected_path_to_curves(bool interactive)
266     if (interactive) {
267         sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
268     } else {
269         sp_selected_path_to_curves0(false, 0);
270     }
273 static void
274 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
276     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
278     Inkscape::Selection *selection = sp_desktop_selection(desktop);
280     if (selection->isEmpty()) {
281         if (interactive)
282             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
283         return;
284     }
286     bool did = false;
287     if (interactive) {
288         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
289         // set "busy" cursor
290         desktop->setWaitingCursor();
291     }
293     GSList *selected = g_slist_copy((GSList *) selection->itemList());
294     GSList *to_select = NULL;
295     selection->clear();
296     GSList *items = g_slist_copy(selected);
298     did = sp_item_list_to_curves(items, &selected, &to_select);
300     g_slist_free (items);
301     selection->setReprList(to_select);
302     selection->addList(selected);
303     g_slist_free (to_select);
304     g_slist_free (selected);
306     if (interactive) {
307         desktop->clearWaitingCursor();
308         if (did) {
309             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
310                              _("Object to path"));
311         } else {
312             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
313             return;
314         }
315     }
318 static bool
319 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
321     bool did = false;
322     
323     for (;
324          items != NULL;
325          items = items->next) {
327         SPItem *item = SP_ITEM(items->data);
329         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
330             continue; // already a path, and no path effect
331         }
333         if (SP_IS_BOX3D(item)) {
334             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
335             Inkscape::XML::Node *repr = box3d_convert_to_group(SP_BOX3D(item));
336             
337             if (repr) {
338                 *to_select = g_slist_prepend (*to_select, repr);
339                 did = true;
340                 *selected = g_slist_remove (*selected, item);
341             }
343             continue;
344         }
345         
346         if (SP_IS_GROUP(item)) {
347             sp_lpe_item_remove_path_effect(SP_LPE_ITEM(item), true);
348             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
349             
350             GSList *item_to_select = NULL;
351             GSList *item_selected = NULL;
352             
353             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
354                 did = true;
356             g_slist_free(item_list);
357             g_slist_free(item_to_select);
358             g_slist_free(item_selected);
360             continue;
361         }
363         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
364         if (!repr)
365             continue;
367         did = true;
368         *selected = g_slist_remove (*selected, item);
370         // remember the position of the item
371         gint pos = SP_OBJECT_REPR(item)->position();
372         // remember parent
373         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
374         // remember id
375         char const *id = SP_OBJECT_REPR(item)->attribute("id");
377         // It's going to resurrect, so we delete without notifying listeners.
378         SP_OBJECT(item)->deleteObject(false);
380         // restore id
381         repr->setAttribute("id", id);
382         // add the new repr to the parent
383         parent->appendChild(repr);
384         // move to the saved position
385         repr->setPosition(pos > 0 ? pos : 0);
387         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
388          * desktops where it was previously selected. */
389         *to_select = g_slist_prepend (*to_select, repr);
390         Inkscape::GC::release(repr);
391     }
392     
393     return did;
396 Inkscape::XML::Node *
397 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
399     if (!item)
400         return NULL;
402     SPCurve *curve = NULL;
403     if (SP_IS_SHAPE(item)) {
404         curve = sp_shape_get_curve(SP_SHAPE(item));
405     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
406         curve = te_get_layout(item)->convertToCurves();
407     }
409     if (!curve)
410         return NULL;
412     // Prevent empty paths from being added to the document
413     // otherwise we end up with zomby markup in the SVG file
414     if(curve->end <= 0)
415     {
416         sp_curve_unref(curve);
417         return NULL;
418     }
420     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
421     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
422     /* Transformation */
423     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
424     /* Style */
425     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
426                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
427     repr->setAttribute("style", style_str);
428     g_free(style_str);
430     /* Mask */
431     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
432     if ( mask_str )
433         repr->setAttribute("mask", mask_str);
435     /* Clip path */
436     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
437     if ( clip_path_str )
438         repr->setAttribute("clip-path", clip_path_str);
440     /* Rotation center */
441     sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
442     sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
444     /* Definition */
445     gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
446     repr->setAttribute("d", def_str);
447     g_free(def_str);
448     sp_curve_unref(curve);
449     return repr;
453 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
454 void
455 sp_selected_path_reverse()
457     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
459     Inkscape::Selection *selection = sp_desktop_selection(desktop);
460     GSList *items = (GSList *) selection->itemList();
462     if (!items) {
463         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
464         return;
465     }
468     // set "busy" cursor
469     desktop->setWaitingCursor();
471     bool did = false;
472     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
474     for (GSList *i = items; i != NULL; i = i->next) {
476         if (!SP_IS_PATH(i->data))
477             continue;
479         did = true;
480         SPPath *path = SP_PATH(i->data);
482         SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
484         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
485         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
486             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
487         } else {
488             SP_OBJECT_REPR(path)->setAttribute("d", str);
489         }
490         g_free(str);
492         sp_curve_unref(rcurve);
493     }
495     desktop->clearWaitingCursor();
497     if (did) {
498         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
499                          _("Reverse path"));
500     } else {
501         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
502     }
505 /*
506   Local Variables:
507   mode:c++
508   c-file-style:"stroustrup"
509   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
510   indent-tabs-mode:nil
511   fill-column:99
512   End:
513 */
514 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :