Code

Keeping up with trunk
[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 "text-editing.h"
31 #include "style.h"
32 #include "desktop.h"
33 #include "document.h"
34 #include "message-stack.h"
35 #include "selection.h"
36 #include "desktop-handles.h"
37 #include "box3d.h"
38 #include <2geom/pathvector.h>
39 #include "selection-chemistry.h"
40 #include "path-chemistry.h"
42 void
43 sp_selected_path_combine(SPDesktop *desktop)
44 {
45     Inkscape::Selection *selection = sp_desktop_selection(desktop);
46     SPDocument *doc = sp_desktop_document(desktop);
47     
48     if (g_slist_length((GSList *) selection->itemList()) < 1) {
49         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to combine."));
50         return;
51     }
53     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
54     // set "busy" cursor
55     desktop->setWaitingCursor();
57     GSList *items = g_slist_copy((GSList *) selection->itemList());
59     items = sp_degroup_list (items); // descend into any groups in selection
61     GSList *to_paths = NULL;
62     for (GSList *i = items; i != NULL; i = i->next) {
63         SPItem *item = (SPItem *) i->data;
64         if (!SP_IS_PATH(item) && !SP_IS_GROUP(item))
65             to_paths = g_slist_prepend(to_paths, item);
66     }
67     GSList *converted = NULL;
68     bool did = sp_item_list_to_curves(to_paths, &items, &converted);
69     g_slist_free(to_paths);
70     for (GSList *i = converted; i != NULL; i = i->next)
71         items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
73     items = sp_degroup_list (items); // converting to path may have added more groups, descend again
75     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
76     items = g_slist_reverse(items);
78     // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
79     gint position = 0;
80     char const *id = NULL;
81     char const *transform = NULL;
82     gchar *style = NULL;
83     gchar *path_effect = NULL;
85     SPCurve* curve = 0;
86     SPItem *first = NULL;
87     Inkscape::XML::Node *parent = NULL; 
89     if (did) {
90         selection->clear();
91     }
93     for (GSList *i = items; i != NULL; i = i->next) {  // going from top to bottom
95         SPItem *item = (SPItem *) i->data;
96         if (!SP_IS_PATH(item))
97             continue;
99         if (!did) {
100             selection->clear();
101             did = true;
102         }
104         SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
105         if (first == NULL) {  // this is the topmost path
106             first = item;
107             parent = SP_OBJECT_REPR(first)->parent();
108             position = SP_OBJECT_REPR(first)->position();
109             id = SP_OBJECT_REPR(first)->attribute("id");
110             transform = SP_OBJECT_REPR(first)->attribute("transform");
111             // FIXME: merge styles of combined objects instead of using the first one's style
112             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
113             path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
114             //c->transform(item->transform);
115             curve = c;
116         } else {
117             c->transform(item->getRelativeTransform(SP_OBJECT(first)));
118             curve->append(c, false);
119             c->unref();
120         }
122         // unless this is the topmost object,
123         if (item != first) {
124             // reduce position only if the same parent
125             if (SP_OBJECT_REPR(item)->parent() == parent)
126                 position--;
127             // delete the object for real, so that its clones can take appropriate action
128             SP_OBJECT(item)->deleteObject();
129         }
130     }
132     g_slist_free(items);
134     if (did) {
135         // delete the topmost.
137         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
138         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
140         // restore id, transform, path effect, and style
141         repr->setAttribute("id", id);
142         if (transform) repr->setAttribute("transform", transform);
143         repr->setAttribute("style", style);
144         g_free(style);
146         repr->setAttribute("inkscape:path-effect", path_effect);
147         g_free(path_effect);
149         // set path data corresponding to new curve
150         gchar *dstring = sp_svg_write_path(curve->get_pathvector());
151         curve->unref();
152         if (path_effect)
153             repr->setAttribute("inkscape:original-d", dstring);
154         else
155             repr->setAttribute("d", dstring);
156         g_free(dstring);
158         // add the new group to the parent of the topmost
159         parent->appendChild(repr);
161         // move to the position of the topmost, reduced by the number of deleted items
162         repr->setPosition(position > 0 ? position : 0);
164         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
165                          _("Combine"));
167         selection->set(repr);
169         Inkscape::GC::release(repr);
171     } else {
172         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
173     }
175     desktop->clearWaitingCursor();
178 void
179 sp_selected_path_break_apart(SPDesktop *desktop)
181     Inkscape::Selection *selection = sp_desktop_selection(desktop);
183     if (selection->isEmpty()) {
184         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
185         return;
186     }
188     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
189     // set "busy" cursor
190     desktop->setWaitingCursor();
192     bool did = false;
194     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
195          items != NULL;
196          items = items->next) {
198         SPItem *item = (SPItem *) items->data;
200         if (!SP_IS_PATH(item))
201             continue;
203         SPPath *path = SP_PATH(item);
205         SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
206         if (curve == NULL)
207             continue;
209         did = true;
211         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
212         gint pos = SP_OBJECT_REPR(item)->position();
213         char const *id = SP_OBJECT_REPR(item)->attribute("id");
215         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
216         gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
218         Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
220         curve->unref();
222         // it's going to resurrect as one of the pieces, so we delete without advertisement
223         SP_OBJECT(item)->deleteObject(false);
225         curve = new SPCurve(apv);
226         g_assert(curve != NULL);
228         GSList *list = curve->split();
230         curve->unref();
232         GSList *reprs = NULL;
233         for (GSList *l = list; l != NULL; l = l->next) {
234             curve = (SPCurve *) l->data;
236             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
237             repr->setAttribute("style", style);
239             repr->setAttribute("inkscape:path-effect", path_effect);
241             gchar *str = sp_svg_write_path(curve->get_pathvector());
242             if (path_effect)
243                 repr->setAttribute("inkscape:original-d", str);
244             else
245                 repr->setAttribute("d", str);
246             g_free(str);
248             // add the new repr to the parent
249             parent->appendChild(repr);
251             // move to the saved position
252             repr->setPosition(pos > 0 ? pos : 0);
254             // if it's the first one, restore id
255             if (l == list)
256                 repr->setAttribute("id", id);
258             reprs = g_slist_prepend (reprs, repr);
260             Inkscape::GC::release(repr);
261         }
263         selection->setReprList(reprs);
265         g_slist_free(reprs);
266         g_slist_free(list);
267         g_free(style);
268         g_free(path_effect);
269     }
271     desktop->clearWaitingCursor();
273     if (did) {
274         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
275                          _("Break apart"));
276     } else {
277         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
278     }
281 /* This function is an entry point from GUI */
282 void
283 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
285     Inkscape::Selection *selection = sp_desktop_selection(desktop);
287     if (selection->isEmpty()) {
288         if (interactive)
289             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
290         return;
291     }
293     bool did = false;
294     if (interactive) {
295         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
296         // set "busy" cursor
297         desktop->setWaitingCursor();
298     }
300     GSList *selected = g_slist_copy((GSList *) selection->itemList());
301     GSList *to_select = NULL;
302     selection->clear();
303     GSList *items = g_slist_copy(selected);
305     did = sp_item_list_to_curves(items, &selected, &to_select);
307     g_slist_free (items);
308     selection->setReprList(to_select);
309     selection->addList(selected);
310     g_slist_free (to_select);
311     g_slist_free (selected);
313     if (interactive) {
314         desktop->clearWaitingCursor();
315         if (did) {
316             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
317                              _("Object to path"));
318         } else {
319             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
320             return;
321         }
322     }
325 bool
326 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
328     bool did = false;
329     
330     for (;
331          items != NULL;
332          items = items->next) {
334         SPItem *item = SP_ITEM(items->data);
335         SPDocument *document = item->document;
337         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
338             continue; // already a path, and no path effect
339         }
341         if (SP_IS_BOX3D(item)) {
342             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
343             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
344             
345             if (repr) {
346                 *to_select = g_slist_prepend (*to_select, repr);
347                 did = true;
348                 *selected = g_slist_remove (*selected, item);
349             }
351             continue;
352         }
353         
354         if (SP_IS_GROUP(item)) {
355             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
356             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
357             
358             GSList *item_to_select = NULL;
359             GSList *item_selected = NULL;
360             
361             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
362                 did = true;
364             g_slist_free(item_list);
365             g_slist_free(item_to_select);
366             g_slist_free(item_selected);
368             continue;
369         }
371         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
372         if (!repr)
373             continue;
375         did = true;
376         *selected = g_slist_remove (*selected, item);
378         // remember the position of the item
379         gint pos = SP_OBJECT_REPR(item)->position();
380         // remember parent
381         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
382         // remember id
383         char const *id = SP_OBJECT_REPR(item)->attribute("id");
384         // remember title
385         gchar *title = item->title();
386         // remember description
387         gchar *desc = item->desc();
389         // It's going to resurrect, so we delete without notifying listeners.
390         SP_OBJECT(item)->deleteObject(false);
392         // restore id
393         repr->setAttribute("id", id);
394         // add the new repr to the parent
395         parent->appendChild(repr);
396         SPObject* newObj = document->getObjectByRepr(repr);
397         if (title && newObj) {
398                 newObj->setTitle(title);
399                 g_free(title);
400         }
401         if (desc && newObj) {
402                 newObj->setDesc(desc);
403                 g_free(desc);
404         }
406         // move to the saved position
407         repr->setPosition(pos > 0 ? pos : 0);
409         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
410          * desktops where it was previously selected. */
411         *to_select = g_slist_prepend (*to_select, repr);
412         Inkscape::GC::release(repr);
413     }
414     
415     return did;
418 Inkscape::XML::Node *
419 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
421     if (!item)
422         return NULL;
424     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
426     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
427         // Special treatment for text: convert each glyph to separate path, then group the paths
428         Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
429         g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
430         /* Mask */
431         gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
432         if ( mask_str )
433             g_repr->setAttribute("mask", mask_str);
434         /* Clip path */
435         gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
436         if ( clip_path_str )
437             g_repr->setAttribute("clip-path", clip_path_str);
438         /* Rotation center */
439         g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
440         g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
441         /* Whole text's style */
442         gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
443                                              SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
444         g_repr->setAttribute("style", style_str);
445         g_free(style_str);
446         Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); 
447         do {
448             Inkscape::Text::Layout::iterator iter_next = iter;
449             iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
450             if (iter == iter_next)
451                 break;
453             /* This glyph's style */
454             SPObject const *pos_obj = 0;
455             void *rawptr = 0;
456             te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
457             if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
458                 break;
459             pos_obj = SP_OBJECT(rawptr);
460             while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
461                pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
462             }
463             gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
464                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
466             // get path from iter to iter_next:
467             SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
468             iter = iter_next; // shift to next glyph
469             if (!curve) { // error converting this glyph
470                 g_free (style_str);
471                 continue;
472             }
473             if (curve->is_empty()) { // whitespace glyph?
474                 curve->unref();
475                 g_free (style_str);
476                 continue;
477             }
479             Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
481             gchar *def_str = sp_svg_write_path(curve->get_pathvector());
482             p_repr->setAttribute("d", def_str);
483             g_free(def_str);
484             curve->unref();
486             p_repr->setAttribute("style", style_str);
487             g_free(style_str);
489             g_repr->appendChild(p_repr);
490             Inkscape::GC::release(p_repr);
492             if (iter == te_get_layout(item)->end())
493                 break;
495         } while (true);
497         return g_repr;
498     }
500     SPCurve *curve = NULL;
501     if (SP_IS_SHAPE(item)) {
502         curve = sp_shape_get_curve(SP_SHAPE(item));
503     } 
505     if (!curve)
506         return NULL;
508     // Prevent empty paths from being added to the document
509     // otherwise we end up with zomby markup in the SVG file
510     if(curve->is_empty())
511     {
512         curve->unref();
513         return NULL;
514     }
516     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
517     /* Transformation */
518     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
519     /* Style */
520     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
521                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
522     repr->setAttribute("style", style_str);
523     g_free(style_str);
525     /* Mask */
526     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
527     if ( mask_str )
528         repr->setAttribute("mask", mask_str);
530     /* Clip path */
531     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
532     if ( clip_path_str )
533         repr->setAttribute("clip-path", clip_path_str);
535     /* Rotation center */
536     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
537     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
539     /* Definition */
540     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
541     repr->setAttribute("d", def_str);
542     g_free(def_str);
543     curve->unref();
544     return repr;
548 void
549 sp_selected_path_reverse(SPDesktop *desktop)
551     Inkscape::Selection *selection = sp_desktop_selection(desktop);
552     GSList *items = (GSList *) selection->itemList();
554     if (!items) {
555         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
556         return;
557     }
560     // set "busy" cursor
561     desktop->setWaitingCursor();
563     bool did = false;
564     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
566     for (GSList *i = items; i != NULL; i = i->next) {
568         if (!SP_IS_PATH(i->data))
569             continue;
571         did = true;
572         SPPath *path = SP_PATH(i->data);
574         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
576         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
577         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
578             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
579         } else {
580             SP_OBJECT_REPR(path)->setAttribute("d", str);
581         }
582         g_free(str);
584         rcurve->unref();
586         // reverse nodetypes order (Bug #179866)
587         gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
588         if ( nodetypes ) {
589             SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
590             g_free(nodetypes);
591         }
592     }
594     desktop->clearWaitingCursor();
596     if (did) {
597         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
598                          _("Reverse path"));
599     } else {
600         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
601     }
604 /*
605   Local Variables:
606   mode:c++
607   c-file-style:"stroustrup"
608   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
609   indent-tabs-mode:nil
610   fill-column:99
611   End:
612 */
613 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :