Code

A simple layout document as to what, why and how is cppification.
[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         SP_OBJECT(first)->deleteObject(false);
136         // delete the topmost.
138         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
139         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
141         // restore id, transform, path effect, and style
142         repr->setAttribute("id", id);
143         if (transform) repr->setAttribute("transform", transform);
144         repr->setAttribute("style", style);
145         g_free(style);
147         repr->setAttribute("inkscape:path-effect", path_effect);
148         g_free(path_effect);
150         // set path data corresponding to new curve
151         gchar *dstring = sp_svg_write_path(curve->get_pathvector());
152         curve->unref();
153         if (path_effect)
154             repr->setAttribute("inkscape:original-d", dstring);
155         else
156             repr->setAttribute("d", dstring);
157         g_free(dstring);
159         // add the new group to the parent of the topmost
160         parent->appendChild(repr);
162         // move to the position of the topmost, reduced by the number of deleted items
163         repr->setPosition(position > 0 ? position : 0);
165         SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
166                          _("Combine"));
168         selection->set(repr);
170         Inkscape::GC::release(repr);
172     } else {
173         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
174     }
176     desktop->clearWaitingCursor();
179 void
180 sp_selected_path_break_apart(SPDesktop *desktop)
182     Inkscape::Selection *selection = sp_desktop_selection(desktop);
184     if (selection->isEmpty()) {
185         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
186         return;
187     }
189     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
190     // set "busy" cursor
191     desktop->setWaitingCursor();
193     bool did = false;
195     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
196          items != NULL;
197          items = items->next) {
199         SPItem *item = (SPItem *) items->data;
201         if (!SP_IS_PATH(item))
202             continue;
204         SPPath *path = SP_PATH(item);
206         SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
207         if (curve == NULL)
208             continue;
210         did = true;
212         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
213         gint pos = SP_OBJECT_REPR(item)->position();
214         char const *id = SP_OBJECT_REPR(item)->attribute("id");
216                 // XML Tree being used directly here while it shouldn't be...
217         gchar *style = g_strdup(SP_OBJECT(item)->getRepr()->attribute("style"));
218                 // XML Tree being used directly here while it shouldn't be...
219         gchar *path_effect = g_strdup(SP_OBJECT(item)->getRepr()->attribute("inkscape:path-effect"));
221         Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
223         curve->unref();
225         // it's going to resurrect as one of the pieces, so we delete without advertisement
226         SP_OBJECT(item)->deleteObject(false);
228         curve = new SPCurve(apv);
229         g_assert(curve != NULL);
231         GSList *list = curve->split();
233         curve->unref();
235         GSList *reprs = NULL;
236         for (GSList *l = list; l != NULL; l = l->next) {
237             curve = (SPCurve *) l->data;
239             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
240             repr->setAttribute("style", style);
242             repr->setAttribute("inkscape:path-effect", path_effect);
244             gchar *str = sp_svg_write_path(curve->get_pathvector());
245             if (path_effect)
246                 repr->setAttribute("inkscape:original-d", str);
247             else
248                 repr->setAttribute("d", str);
249             g_free(str);
251             // add the new repr to the parent
252             parent->appendChild(repr);
254             // move to the saved position
255             repr->setPosition(pos > 0 ? pos : 0);
257             // if it's the first one, restore id
258             if (l == list)
259                 repr->setAttribute("id", id);
261             reprs = g_slist_prepend (reprs, repr);
263             Inkscape::GC::release(repr);
264         }
266         selection->setReprList(reprs);
268         g_slist_free(reprs);
269         g_slist_free(list);
270         g_free(style);
271         g_free(path_effect);
272     }
274     desktop->clearWaitingCursor();
276     if (did) {
277         SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
278                          _("Break apart"));
279     } else {
280         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
281     }
284 /* This function is an entry point from GUI */
285 void
286 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
288     Inkscape::Selection *selection = sp_desktop_selection(desktop);
290     if (selection->isEmpty()) {
291         if (interactive)
292             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
293         return;
294     }
296     bool did = false;
297     if (interactive) {
298         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
299         // set "busy" cursor
300         desktop->setWaitingCursor();
301     }
303     GSList *selected = g_slist_copy((GSList *) selection->itemList());
304     GSList *to_select = NULL;
305     selection->clear();
306     GSList *items = g_slist_copy(selected);
308     did = sp_item_list_to_curves(items, &selected, &to_select);
310     g_slist_free (items);
311     selection->setReprList(to_select);
312     selection->addList(selected);
313     g_slist_free (to_select);
314     g_slist_free (selected);
316     if (interactive) {
317         desktop->clearWaitingCursor();
318         if (did) {
319             SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
320                              _("Object to path"));
321         } else {
322             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
323             return;
324         }
325     }
328 /** Converts the selected items to LPEItems if they are not already so; e.g. SPRects) */
329 void sp_selected_to_lpeitems(SPDesktop *desktop)
331     Inkscape::Selection *selection = sp_desktop_selection(desktop);
333     if (selection->isEmpty()) {
334         return;
335     }
337     bool did = false;
339     GSList *selected = g_slist_copy((GSList *) selection->itemList());
340     GSList *to_select = NULL;
341     selection->clear();
342     GSList *items = g_slist_copy(selected);
344     did = sp_item_list_to_curves(items, &selected, &to_select, true);
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);
353 bool
354 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select, bool skip_all_lpeitems)
356     bool did = false;
357     
358     for (;
359          items != NULL;
360          items = items->next) {
362         SPItem *item = SP_ITEM(items->data);
363         SPDocument *document = item->document;
365         if ( skip_all_lpeitems &&
366              SP_IS_LPE_ITEM(item) && 
367              !SP_IS_GROUP(item) ) // also convert objects in an SPGroup when skip_all_lpeitems is set.
368         { 
369             continue;
370         }
372         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
373             continue; // already a path, and no path effect
374         }
376         if (SP_IS_BOX3D(item)) {
377             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
378             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
379             
380             if (repr) {
381                 *to_select = g_slist_prepend (*to_select, repr);
382                 did = true;
383                 *selected = g_slist_remove (*selected, item);
384             }
386             continue;
387         }
388         
389         if (SP_IS_GROUP(item)) {
390             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
391             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
392             
393             GSList *item_to_select = NULL;
394             GSList *item_selected = NULL;
395             
396             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
397                 did = true;
399             g_slist_free(item_list);
400             g_slist_free(item_to_select);
401             g_slist_free(item_selected);
403             continue;
404         }
406         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
407         if (!repr)
408             continue;
410         did = true;
411         *selected = g_slist_remove (*selected, item);
413         // remember the position of the item
414         gint pos = SP_OBJECT_REPR(item)->position();
415         // remember parent
416         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
417         // remember id
418         char const *id = SP_OBJECT_REPR(item)->attribute("id");
419         // remember title
420         gchar *title = item->title();
421         // remember description
422         gchar *desc = item->desc();
424         // It's going to resurrect, so we delete without notifying listeners.
425         SP_OBJECT(item)->deleteObject(false);
427         // restore id
428         repr->setAttribute("id", id);
429         // add the new repr to the parent
430         parent->appendChild(repr);
431         SPObject* newObj = document->getObjectByRepr(repr);
432         if (title && newObj) {
433                 newObj->setTitle(title);
434                 g_free(title);
435         }
436         if (desc && newObj) {
437                 newObj->setDesc(desc);
438                 g_free(desc);
439         }
441         // move to the saved position
442         repr->setPosition(pos > 0 ? pos : 0);
444         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
445          * desktops where it was previously selected. */
446         *to_select = g_slist_prepend (*to_select, repr);
447         Inkscape::GC::release(repr);
448     }
449     
450     return did;
453 Inkscape::XML::Node *
454 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
456     if (!item)
457         return NULL;
459     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
461     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
462         // Special treatment for text: convert each glyph to separate path, then group the paths
463         Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
464         g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
465         /* Mask */
466         gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
467         if ( mask_str )
468             g_repr->setAttribute("mask", mask_str);
469         /* Clip path */
470         gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
471         if ( clip_path_str )
472             g_repr->setAttribute("clip-path", clip_path_str);
473         /* Rotation center */
474         g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
475         g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
476         /* Whole text's style */
477         gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
478                                              SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
479         g_repr->setAttribute("style", style_str);
480         g_free(style_str);
481         Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); 
482         do {
483             Inkscape::Text::Layout::iterator iter_next = iter;
484             iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
485             if (iter == iter_next)
486                 break;
488             /* This glyph's style */
489             SPObject const *pos_obj = 0;
490             void *rawptr = 0;
491             te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
492             if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
493                 break;
494             pos_obj = SP_OBJECT(rawptr);
495             while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
496                pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
497             }
498             gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
499                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
501             // get path from iter to iter_next:
502             SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
503             iter = iter_next; // shift to next glyph
504             if (!curve) { // error converting this glyph
505                 g_free (style_str);
506                 continue;
507             }
508             if (curve->is_empty()) { // whitespace glyph?
509                 curve->unref();
510                 g_free (style_str);
511                 continue;
512             }
514             Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
516             gchar *def_str = sp_svg_write_path(curve->get_pathvector());
517             p_repr->setAttribute("d", def_str);
518             g_free(def_str);
519             curve->unref();
521             p_repr->setAttribute("style", style_str);
522             g_free(style_str);
524             g_repr->appendChild(p_repr);
525             Inkscape::GC::release(p_repr);
527             if (iter == te_get_layout(item)->end())
528                 break;
530         } while (true);
532         return g_repr;
533     }
535     SPCurve *curve = NULL;
536     if (SP_IS_SHAPE(item)) {
537         curve = SP_SHAPE(item)->getCurve();
538     } 
540     if (!curve)
541         return NULL;
543     // Prevent empty paths from being added to the document
544     // otherwise we end up with zomby markup in the SVG file
545     if(curve->is_empty())
546     {
547         curve->unref();
548         return NULL;
549     }
551     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
552     /* Transformation */
553     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
554     /* Style */
555     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
556                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
557     repr->setAttribute("style", style_str);
558     g_free(style_str);
560     /* Mask */
561     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
562     if ( mask_str )
563         repr->setAttribute("mask", mask_str);
565     /* Clip path */
566     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
567     if ( clip_path_str )
568         repr->setAttribute("clip-path", clip_path_str);
570     /* Rotation center */
571     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
572     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
574     /* Definition */
575     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
576     repr->setAttribute("d", def_str);
577     g_free(def_str);
578     curve->unref();
579     return repr;
583 void
584 sp_selected_path_reverse(SPDesktop *desktop)
586     Inkscape::Selection *selection = sp_desktop_selection(desktop);
587     GSList *items = (GSList *) selection->itemList();
589     if (!items) {
590         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
591         return;
592     }
595     // set "busy" cursor
596     desktop->setWaitingCursor();
598     bool did = false;
599     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
601     for (GSList *i = items; i != NULL; i = i->next) {
603         if (!SP_IS_PATH(i->data))
604             continue;
606         did = true;
607         SPPath *path = SP_PATH(i->data);
609         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
611         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
612         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
613             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
614         } else {
615             SP_OBJECT_REPR(path)->setAttribute("d", str);
616         }
617         g_free(str);
619         rcurve->unref();
621         // reverse nodetypes order (Bug #179866)
622         gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
623         if ( nodetypes ) {
624             SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
625             g_free(nodetypes);
626         }
627     }
629     desktop->clearWaitingCursor();
631     if (did) {
632         SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
633                          _("Reverse path"));
634     } else {
635         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
636     }
639 /*
640   Local Variables:
641   mode:c++
642   c-file-style:"stroustrup"
643   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
644   indent-tabs-mode:nil
645   fill-column:99
646   End:
647 */
648 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :