Code

Pot and Dutch translation update
[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 /** Converts the selected items to LPEItems if they are not already so; e.g. SPRects) */
327 void sp_selected_to_lpeitems(SPDesktop *desktop)
329     Inkscape::Selection *selection = sp_desktop_selection(desktop);
331     if (selection->isEmpty()) {
332         return;
333     }
335     bool did = false;
337     GSList *selected = g_slist_copy((GSList *) selection->itemList());
338     GSList *to_select = NULL;
339     selection->clear();
340     GSList *items = g_slist_copy(selected);
342     did = sp_item_list_to_curves(items, &selected, &to_select, true);
344     g_slist_free (items);
345     selection->setReprList(to_select);
346     selection->addList(selected);
347     g_slist_free (to_select);
348     g_slist_free (selected);
351 bool
352 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select, bool skip_all_lpeitems)
354     bool did = false;
355     
356     for (;
357          items != NULL;
358          items = items->next) {
360         SPItem *item = SP_ITEM(items->data);
361         SPDocument *document = item->document;
363         if ( skip_all_lpeitems &&
364              SP_IS_LPE_ITEM(item) && 
365              !SP_IS_GROUP(item) ) // also convert objects in an SPGroup when skip_all_lpeitems is set.
366         { 
367             continue;
368         }
370         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
371             continue; // already a path, and no path effect
372         }
374         if (SP_IS_BOX3D(item)) {
375             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
376             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
377             
378             if (repr) {
379                 *to_select = g_slist_prepend (*to_select, repr);
380                 did = true;
381                 *selected = g_slist_remove (*selected, item);
382             }
384             continue;
385         }
386         
387         if (SP_IS_GROUP(item)) {
388             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
389             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
390             
391             GSList *item_to_select = NULL;
392             GSList *item_selected = NULL;
393             
394             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
395                 did = true;
397             g_slist_free(item_list);
398             g_slist_free(item_to_select);
399             g_slist_free(item_selected);
401             continue;
402         }
404         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
405         if (!repr)
406             continue;
408         did = true;
409         *selected = g_slist_remove (*selected, item);
411         // remember the position of the item
412         gint pos = SP_OBJECT_REPR(item)->position();
413         // remember parent
414         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
415         // remember id
416         char const *id = SP_OBJECT_REPR(item)->attribute("id");
417         // remember title
418         gchar *title = item->title();
419         // remember description
420         gchar *desc = item->desc();
422         // It's going to resurrect, so we delete without notifying listeners.
423         SP_OBJECT(item)->deleteObject(false);
425         // restore id
426         repr->setAttribute("id", id);
427         // add the new repr to the parent
428         parent->appendChild(repr);
429         SPObject* newObj = document->getObjectByRepr(repr);
430         if (title && newObj) {
431                 newObj->setTitle(title);
432                 g_free(title);
433         }
434         if (desc && newObj) {
435                 newObj->setDesc(desc);
436                 g_free(desc);
437         }
439         // move to the saved position
440         repr->setPosition(pos > 0 ? pos : 0);
442         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
443          * desktops where it was previously selected. */
444         *to_select = g_slist_prepend (*to_select, repr);
445         Inkscape::GC::release(repr);
446     }
447     
448     return did;
451 Inkscape::XML::Node *
452 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
454     if (!item)
455         return NULL;
457     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
459     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
460         // Special treatment for text: convert each glyph to separate path, then group the paths
461         Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
462         g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
463         /* Mask */
464         gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
465         if ( mask_str )
466             g_repr->setAttribute("mask", mask_str);
467         /* Clip path */
468         gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
469         if ( clip_path_str )
470             g_repr->setAttribute("clip-path", clip_path_str);
471         /* Rotation center */
472         g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
473         g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
474         /* Whole text's style */
475         gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
476                                              SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
477         g_repr->setAttribute("style", style_str);
478         g_free(style_str);
479         Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); 
480         do {
481             Inkscape::Text::Layout::iterator iter_next = iter;
482             iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
483             if (iter == iter_next)
484                 break;
486             /* This glyph's style */
487             SPObject const *pos_obj = 0;
488             void *rawptr = 0;
489             te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
490             if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
491                 break;
492             pos_obj = SP_OBJECT(rawptr);
493             while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
494                pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
495             }
496             gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
497                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
499             // get path from iter to iter_next:
500             SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
501             iter = iter_next; // shift to next glyph
502             if (!curve) { // error converting this glyph
503                 g_free (style_str);
504                 continue;
505             }
506             if (curve->is_empty()) { // whitespace glyph?
507                 curve->unref();
508                 g_free (style_str);
509                 continue;
510             }
512             Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
514             gchar *def_str = sp_svg_write_path(curve->get_pathvector());
515             p_repr->setAttribute("d", def_str);
516             g_free(def_str);
517             curve->unref();
519             p_repr->setAttribute("style", style_str);
520             g_free(style_str);
522             g_repr->appendChild(p_repr);
523             Inkscape::GC::release(p_repr);
525             if (iter == te_get_layout(item)->end())
526                 break;
528         } while (true);
530         return g_repr;
531     }
533     SPCurve *curve = NULL;
534     if (SP_IS_SHAPE(item)) {
535         curve = sp_shape_get_curve(SP_SHAPE(item));
536     } 
538     if (!curve)
539         return NULL;
541     // Prevent empty paths from being added to the document
542     // otherwise we end up with zomby markup in the SVG file
543     if(curve->is_empty())
544     {
545         curve->unref();
546         return NULL;
547     }
549     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
550     /* Transformation */
551     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
552     /* Style */
553     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
554                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
555     repr->setAttribute("style", style_str);
556     g_free(style_str);
558     /* Mask */
559     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
560     if ( mask_str )
561         repr->setAttribute("mask", mask_str);
563     /* Clip path */
564     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
565     if ( clip_path_str )
566         repr->setAttribute("clip-path", clip_path_str);
568     /* Rotation center */
569     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
570     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
572     /* Definition */
573     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
574     repr->setAttribute("d", def_str);
575     g_free(def_str);
576     curve->unref();
577     return repr;
581 void
582 sp_selected_path_reverse(SPDesktop *desktop)
584     Inkscape::Selection *selection = sp_desktop_selection(desktop);
585     GSList *items = (GSList *) selection->itemList();
587     if (!items) {
588         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
589         return;
590     }
593     // set "busy" cursor
594     desktop->setWaitingCursor();
596     bool did = false;
597     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
599     for (GSList *i = items; i != NULL; i = i->next) {
601         if (!SP_IS_PATH(i->data))
602             continue;
604         did = true;
605         SPPath *path = SP_PATH(i->data);
607         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
609         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
610         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
611             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
612         } else {
613             SP_OBJECT_REPR(path)->setAttribute("d", str);
614         }
615         g_free(str);
617         rcurve->unref();
619         // reverse nodetypes order (Bug #179866)
620         gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
621         if ( nodetypes ) {
622             SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
623             g_free(nodetypes);
624         }
625     }
627     desktop->clearWaitingCursor();
629     if (did) {
630         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
631                          _("Reverse path"));
632     } else {
633         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
634     }
637 /*
638   Local Variables:
639   mode:c++
640   c-file-style:"stroustrup"
641   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
642   indent-tabs-mode:nil
643   fill-column:99
644   End:
645 */
646 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :