Code

Fix new path update preference.
[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         sp_document_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         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
217         gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
219         Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
221         curve->unref();
223         // it's going to resurrect as one of the pieces, so we delete without advertisement
224         SP_OBJECT(item)->deleteObject(false);
226         curve = new SPCurve(apv);
227         g_assert(curve != NULL);
229         GSList *list = curve->split();
231         curve->unref();
233         GSList *reprs = NULL;
234         for (GSList *l = list; l != NULL; l = l->next) {
235             curve = (SPCurve *) l->data;
237             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
238             repr->setAttribute("style", style);
240             repr->setAttribute("inkscape:path-effect", path_effect);
242             gchar *str = sp_svg_write_path(curve->get_pathvector());
243             if (path_effect)
244                 repr->setAttribute("inkscape:original-d", str);
245             else
246                 repr->setAttribute("d", str);
247             g_free(str);
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);
269         g_free(path_effect);
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     Inkscape::Selection *selection = sp_desktop_selection(desktop);
288     if (selection->isEmpty()) {
289         if (interactive)
290             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
291         return;
292     }
294     bool did = false;
295     if (interactive) {
296         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
297         // set "busy" cursor
298         desktop->setWaitingCursor();
299     }
301     GSList *selected = g_slist_copy((GSList *) selection->itemList());
302     GSList *to_select = NULL;
303     selection->clear();
304     GSList *items = g_slist_copy(selected);
306     did = sp_item_list_to_curves(items, &selected, &to_select);
308     g_slist_free (items);
309     selection->setReprList(to_select);
310     selection->addList(selected);
311     g_slist_free (to_select);
312     g_slist_free (selected);
314     if (interactive) {
315         desktop->clearWaitingCursor();
316         if (did) {
317             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
318                              _("Object to path"));
319         } else {
320             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
321             return;
322         }
323     }
326 bool
327 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
329     bool did = false;
330     
331     for (;
332          items != NULL;
333          items = items->next) {
335         SPItem *item = SP_ITEM(items->data);
336         SPDocument *document = item->document;
338         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
339             continue; // already a path, and no path effect
340         }
342         if (SP_IS_BOX3D(item)) {
343             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
344             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
345             
346             if (repr) {
347                 *to_select = g_slist_prepend (*to_select, repr);
348                 did = true;
349                 *selected = g_slist_remove (*selected, item);
350             }
352             continue;
353         }
354         
355         if (SP_IS_GROUP(item)) {
356             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
357             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
358             
359             GSList *item_to_select = NULL;
360             GSList *item_selected = NULL;
361             
362             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
363                 did = true;
365             g_slist_free(item_list);
366             g_slist_free(item_to_select);
367             g_slist_free(item_selected);
369             continue;
370         }
372         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
373         if (!repr)
374             continue;
376         did = true;
377         *selected = g_slist_remove (*selected, item);
379         // remember the position of the item
380         gint pos = SP_OBJECT_REPR(item)->position();
381         // remember parent
382         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
383         // remember id
384         char const *id = SP_OBJECT_REPR(item)->attribute("id");
385         // remember title
386         gchar *title = item->title();
387         // remember description
388         gchar *desc = item->desc();
390         // It's going to resurrect, so we delete without notifying listeners.
391         SP_OBJECT(item)->deleteObject(false);
393         // restore id
394         repr->setAttribute("id", id);
395         // add the new repr to the parent
396         parent->appendChild(repr);
397         SPObject* newObj = document->getObjectByRepr(repr);
398         if (title && newObj) {
399                 newObj->setTitle(title);
400                 g_free(title);
401         }
402         if (desc && newObj) {
403                 newObj->setDesc(desc);
404                 g_free(desc);
405         }
407         // move to the saved position
408         repr->setPosition(pos > 0 ? pos : 0);
410         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
411          * desktops where it was previously selected. */
412         *to_select = g_slist_prepend (*to_select, repr);
413         Inkscape::GC::release(repr);
414     }
415     
416     return did;
419 Inkscape::XML::Node *
420 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
422     if (!item)
423         return NULL;
425     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
427     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
428         // Special treatment for text: convert each glyph to separate path, then group the paths
429         Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
430         g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
431         /* Mask */
432         gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
433         if ( mask_str )
434             g_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             g_repr->setAttribute("clip-path", clip_path_str);
439         /* Rotation center */
440         g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
441         g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
442         /* Whole text's style */
443         gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
444                                              SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
445         g_repr->setAttribute("style", style_str);
446         g_free(style_str);
447         Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); 
448         do {
449             Inkscape::Text::Layout::iterator iter_next = iter;
450             iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
451             if (iter == iter_next)
452                 break;
454             /* This glyph's style */
455             SPObject const *pos_obj = 0;
456             void *rawptr = 0;
457             te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
458             if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
459                 break;
460             pos_obj = SP_OBJECT(rawptr);
461             while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
462                pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
463             }
464             gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
465                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
467             // get path from iter to iter_next:
468             SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
469             iter = iter_next; // shift to next glyph
470             if (!curve) { // error converting this glyph
471                 g_free (style_str);
472                 continue;
473             }
474             if (curve->is_empty()) { // whitespace glyph?
475                 curve->unref();
476                 g_free (style_str);
477                 continue;
478             }
480             Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
482             gchar *def_str = sp_svg_write_path(curve->get_pathvector());
483             p_repr->setAttribute("d", def_str);
484             g_free(def_str);
485             curve->unref();
487             p_repr->setAttribute("style", style_str);
488             g_free(style_str);
490             g_repr->appendChild(p_repr);
491             Inkscape::GC::release(p_repr);
493             if (iter == te_get_layout(item)->end())
494                 break;
496         } while (true);
498         return g_repr;
499     }
501     SPCurve *curve = NULL;
502     if (SP_IS_SHAPE(item)) {
503         curve = sp_shape_get_curve(SP_SHAPE(item));
504     } 
506     if (!curve)
507         return NULL;
509     // Prevent empty paths from being added to the document
510     // otherwise we end up with zomby markup in the SVG file
511     if(curve->is_empty())
512     {
513         curve->unref();
514         return NULL;
515     }
517     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
518     /* Transformation */
519     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
520     /* Style */
521     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
522                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
523     repr->setAttribute("style", style_str);
524     g_free(style_str);
526     /* Mask */
527     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
528     if ( mask_str )
529         repr->setAttribute("mask", mask_str);
531     /* Clip path */
532     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
533     if ( clip_path_str )
534         repr->setAttribute("clip-path", clip_path_str);
536     /* Rotation center */
537     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
538     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
540     /* Definition */
541     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
542     repr->setAttribute("d", def_str);
543     g_free(def_str);
544     curve->unref();
545     return repr;
549 void
550 sp_selected_path_reverse(SPDesktop *desktop)
552     Inkscape::Selection *selection = sp_desktop_selection(desktop);
553     GSList *items = (GSList *) selection->itemList();
555     if (!items) {
556         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
557         return;
558     }
561     // set "busy" cursor
562     desktop->setWaitingCursor();
564     bool did = false;
565     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
567     for (GSList *i = items; i != NULL; i = i->next) {
569         if (!SP_IS_PATH(i->data))
570             continue;
572         did = true;
573         SPPath *path = SP_PATH(i->data);
575         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
577         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
578         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
579             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
580         } else {
581             SP_OBJECT_REPR(path)->setAttribute("d", str);
582         }
583         g_free(str);
585         rcurve->unref();
587         // reverse nodetypes order (Bug #179866)
588         gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
589         if ( nodetypes ) {
590             SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
591             g_free(nodetypes);
592         }
593     }
595     desktop->clearWaitingCursor();
597     if (did) {
598         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
599                          _("Reverse path"));
600     } else {
601         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
602     }
605 /*
606   Local Variables:
607   mode:c++
608   c-file-style:"stroustrup"
609   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
610   indent-tabs-mode:nil
611   fill-column:99
612   End:
613 */
614 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :