Code

Connector tool: make connectors avoid the convex hull of shapes.
[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     for (GSList *i = items; i != NULL; i = i->next) {  // going from top to bottom
91         SPItem *item = (SPItem *) i->data;
92         if (!SP_IS_PATH(item))
93             continue;
94         did = true;
96         SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
97         if (first == NULL) {  // this is the topmost path
98             first = item;
99             parent = SP_OBJECT_REPR(first)->parent();
100             position = SP_OBJECT_REPR(first)->position();
101             id = SP_OBJECT_REPR(first)->attribute("id");
102             transform = SP_OBJECT_REPR(first)->attribute("transform");
103             // FIXME: merge styles of combined objects instead of using the first one's style
104             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
105             path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
106             //c->transform(item->transform);
107             curve = c;
108         } else {
109             c->transform(item->getRelativeTransform(SP_OBJECT(first)));
110             curve->append(c, false);
111             c->unref();
112         }
114         // unless this is the topmost object,
115         if (item != first) {
116             // reduce position only if the same parent
117             if (SP_OBJECT_REPR(item)->parent() == parent)
118                 position--;
119             // delete the object for real, so that its clones can take appropriate action
120             SP_OBJECT(item)->deleteObject();
121         }
122     }
124     g_slist_free(items);
126     if (did) {
127         selection->clear();
129         // delete the topmost one so that its clones don't get alerted; this object will be
130         // restored shortly, with the same id
131         SP_OBJECT(first)->deleteObject(false);
133         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
134         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
136         // restore id, transform, path effect, and style
137         repr->setAttribute("id", id);
138         if (transform) repr->setAttribute("transform", transform);
139         repr->setAttribute("style", style);
140         g_free(style);
142         repr->setAttribute("inkscape:path-effect", path_effect);
143         g_free(path_effect);
145         // set path data corresponding to new curve
146         gchar *dstring = sp_svg_write_path(curve->get_pathvector());
147         curve->unref();
148         if (path_effect)
149             repr->setAttribute("inkscape:original-d", dstring);
150         else
151             repr->setAttribute("d", dstring);
152         g_free(dstring);
154         // add the new group to the parent of the topmost
155         parent->appendChild(repr);
157         // move to the position of the topmost, reduced by the number of deleted items
158         repr->setPosition(position > 0 ? position : 0);
160         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
161                          _("Combine"));
163         selection->set(repr);
165         Inkscape::GC::release(repr);
167     } else {
168         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
169     }
171     desktop->clearWaitingCursor();
174 void
175 sp_selected_path_break_apart(SPDesktop *desktop)
177     Inkscape::Selection *selection = sp_desktop_selection(desktop);
179     if (selection->isEmpty()) {
180         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
181         return;
182     }
184     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
185     // set "busy" cursor
186     desktop->setWaitingCursor();
188     bool did = false;
190     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
191          items != NULL;
192          items = items->next) {
194         SPItem *item = (SPItem *) items->data;
196         if (!SP_IS_PATH(item))
197             continue;
199         SPPath *path = SP_PATH(item);
201         SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
202         if (curve == NULL)
203             continue;
205         did = true;
207         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
208         gint pos = SP_OBJECT_REPR(item)->position();
209         char const *id = SP_OBJECT_REPR(item)->attribute("id");
211         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
212         gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
214         Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
216         curve->unref();
218         // it's going to resurrect as one of the pieces, so we delete without advertisement
219         SP_OBJECT(item)->deleteObject(false);
221         curve = new SPCurve(apv);
222         g_assert(curve != NULL);
224         GSList *list = curve->split();
226         curve->unref();
228         GSList *reprs = NULL;
229         for (GSList *l = list; l != NULL; l = l->next) {
230             curve = (SPCurve *) l->data;
232             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
233             repr->setAttribute("style", style);
235             repr->setAttribute("inkscape:path-effect", path_effect);
237             gchar *str = sp_svg_write_path(curve->get_pathvector());
238             if (path_effect)
239                 repr->setAttribute("inkscape:original-d", str);
240             else
241                 repr->setAttribute("d", str);
242             g_free(str);
244             // add the new repr to the parent
245             parent->appendChild(repr);
247             // move to the saved position
248             repr->setPosition(pos > 0 ? pos : 0);
250             // if it's the first one, restore id
251             if (l == list)
252                 repr->setAttribute("id", id);
254             reprs = g_slist_prepend (reprs, repr);
256             Inkscape::GC::release(repr);
257         }
259         selection->setReprList(reprs);
261         g_slist_free(reprs);
262         g_slist_free(list);
263         g_free(style);
264         g_free(path_effect);
265     }
267     desktop->clearWaitingCursor();
269     if (did) {
270         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
271                          _("Break apart"));
272     } else {
273         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
274     }
277 /* This function is an entry point from GUI */
278 void
279 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
281     Inkscape::Selection *selection = sp_desktop_selection(desktop);
283     if (selection->isEmpty()) {
284         if (interactive)
285             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
286         return;
287     }
289     bool did = false;
290     if (interactive) {
291         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
292         // set "busy" cursor
293         desktop->setWaitingCursor();
294     }
296     GSList *selected = g_slist_copy((GSList *) selection->itemList());
297     GSList *to_select = NULL;
298     selection->clear();
299     GSList *items = g_slist_copy(selected);
301     did = sp_item_list_to_curves(items, &selected, &to_select);
303     g_slist_free (items);
304     selection->setReprList(to_select);
305     selection->addList(selected);
306     g_slist_free (to_select);
307     g_slist_free (selected);
309     if (interactive) {
310         desktop->clearWaitingCursor();
311         if (did) {
312             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
313                              _("Object to path"));
314         } else {
315             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
316             return;
317         }
318     }
321 bool
322 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
324     bool did = false;
325     
326     for (;
327          items != NULL;
328          items = items->next) {
330         SPItem *item = SP_ITEM(items->data);
331         SPDocument *document = item->document;
333         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
334             continue; // already a path, and no path effect
335         }
337         if (SP_IS_BOX3D(item)) {
338             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
339             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
340             
341             if (repr) {
342                 *to_select = g_slist_prepend (*to_select, repr);
343                 did = true;
344                 *selected = g_slist_remove (*selected, item);
345             }
347             continue;
348         }
349         
350         if (SP_IS_GROUP(item)) {
351             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
352             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
353             
354             GSList *item_to_select = NULL;
355             GSList *item_selected = NULL;
356             
357             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
358                 did = true;
360             g_slist_free(item_list);
361             g_slist_free(item_to_select);
362             g_slist_free(item_selected);
364             continue;
365         }
367         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
368         if (!repr)
369             continue;
371         did = true;
372         *selected = g_slist_remove (*selected, item);
374         // remember the position of the item
375         gint pos = SP_OBJECT_REPR(item)->position();
376         // remember parent
377         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
378         // remember id
379         char const *id = SP_OBJECT_REPR(item)->attribute("id");
380         // remember title
381         gchar *title = item->title();
382         // remember description
383         gchar *desc = item->desc();
385         // It's going to resurrect, so we delete without notifying listeners.
386         SP_OBJECT(item)->deleteObject(false);
388         // restore id
389         repr->setAttribute("id", id);
390         // add the new repr to the parent
391         parent->appendChild(repr);
392         SPObject* newObj = document->getObjectByRepr(repr);
393         if (title && newObj) {
394                 newObj->setTitle(title);
395                 g_free(title);
396         }
397         if (desc && newObj) {
398                 newObj->setDesc(desc);
399                 g_free(desc);
400         }
402         // move to the saved position
403         repr->setPosition(pos > 0 ? pos : 0);
405         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
406          * desktops where it was previously selected. */
407         *to_select = g_slist_prepend (*to_select, repr);
408         Inkscape::GC::release(repr);
409     }
410     
411     return did;
414 Inkscape::XML::Node *
415 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
417     if (!item)
418         return NULL;
420     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
422     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
423         // Special treatment for text: convert each glyph to separate path, then group the paths
424         Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
425         g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
426         /* Mask */
427         gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
428         if ( mask_str )
429             g_repr->setAttribute("mask", mask_str);
430         /* Clip path */
431         gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
432         if ( clip_path_str )
433             g_repr->setAttribute("clip-path", clip_path_str);
434         /* Rotation center */
435         g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
436         g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
437         /* Whole text's style */
438         gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
439                                              SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
440         g_repr->setAttribute("style", style_str);
441         g_free(style_str);
442         Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); 
443         do {
444             Inkscape::Text::Layout::iterator iter_next = iter;
445             iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
446             if (iter == iter_next)
447                 break;
449             /* This glyph's style */
450             SPObject const *pos_obj = 0;
451             void *rawptr = 0;
452             te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
453             if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
454                 break;
455             pos_obj = SP_OBJECT(rawptr);
456             while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
457                pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
458             }
459             gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
460                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
462             // get path from iter to iter_next:
463             SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
464             iter = iter_next; // shift to next glyph
465             if (!curve) { // error converting this glyph
466                 g_free (style_str);
467                 continue;
468             }
469             if (curve->is_empty()) { // whitespace glyph?
470                 curve->unref();
471                 g_free (style_str);
472                 continue;
473             }
475             Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
477             gchar *def_str = sp_svg_write_path(curve->get_pathvector());
478             p_repr->setAttribute("d", def_str);
479             g_free(def_str);
480             curve->unref();
482             p_repr->setAttribute("style", style_str);
483             g_free(style_str);
485             g_repr->appendChild(p_repr);
486             Inkscape::GC::release(p_repr);
488             if (iter == te_get_layout(item)->end())
489                 break;
491         } while (true);
493         return g_repr;
494     }
496     SPCurve *curve = NULL;
497     if (SP_IS_SHAPE(item)) {
498         curve = sp_shape_get_curve(SP_SHAPE(item));
499     } 
501     if (!curve)
502         return NULL;
504     // Prevent empty paths from being added to the document
505     // otherwise we end up with zomby markup in the SVG file
506     if(curve->is_empty())
507     {
508         curve->unref();
509         return NULL;
510     }
512     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
513     /* Transformation */
514     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
515     /* Style */
516     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
517                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
518     repr->setAttribute("style", style_str);
519     g_free(style_str);
521     /* Mask */
522     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
523     if ( mask_str )
524         repr->setAttribute("mask", mask_str);
526     /* Clip path */
527     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
528     if ( clip_path_str )
529         repr->setAttribute("clip-path", clip_path_str);
531     /* Rotation center */
532     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
533     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
535     /* Definition */
536     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
537     repr->setAttribute("d", def_str);
538     g_free(def_str);
539     curve->unref();
540     return repr;
544 void
545 sp_selected_path_reverse(SPDesktop *desktop)
547     Inkscape::Selection *selection = sp_desktop_selection(desktop);
548     GSList *items = (GSList *) selection->itemList();
550     if (!items) {
551         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
552         return;
553     }
556     // set "busy" cursor
557     desktop->setWaitingCursor();
559     bool did = false;
560     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
562     for (GSList *i = items; i != NULL; i = i->next) {
564         if (!SP_IS_PATH(i->data))
565             continue;
567         did = true;
568         SPPath *path = SP_PATH(i->data);
570         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
572         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
573         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
574             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
575         } else {
576             SP_OBJECT_REPR(path)->setAttribute("d", str);
577         }
578         g_free(str);
580         rcurve->unref();
582         // reverse nodetypes order (Bug #179866)
583         gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
584         if ( nodetypes ) {
585             SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
586             g_free(nodetypes);
587         }
588     }
590     desktop->clearWaitingCursor();
592     if (did) {
593         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
594                          _("Reverse path"));
595     } else {
596         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
597     }
600 /*
601   Local Variables:
602   mode:c++
603   c-file-style:"stroustrup"
604   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
605   indent-tabs-mode:nil
606   fill-column:99
607   End:
608 */
609 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :