1 /*
2 * Here are handlers for modifying selections, specific to paths
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak <buliabyak@users.sf.net>
7 * Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
8 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
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 using Inkscape::DocumentUndo;
44 void
45 sp_selected_path_combine(SPDesktop *desktop)
46 {
47 Inkscape::Selection *selection = sp_desktop_selection(desktop);
48 SPDocument *doc = sp_desktop_document(desktop);
50 if (g_slist_length((GSList *) selection->itemList()) < 1) {
51 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to combine."));
52 return;
53 }
55 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
56 // set "busy" cursor
57 desktop->setWaitingCursor();
59 GSList *items = g_slist_copy((GSList *) selection->itemList());
61 items = sp_degroup_list (items); // descend into any groups in selection
63 GSList *to_paths = NULL;
64 for (GSList *i = items; i != NULL; i = i->next) {
65 SPItem *item = (SPItem *) i->data;
66 if (!SP_IS_PATH(item) && !SP_IS_GROUP(item))
67 to_paths = g_slist_prepend(to_paths, item);
68 }
69 GSList *converted = NULL;
70 bool did = sp_item_list_to_curves(to_paths, &items, &converted);
71 g_slist_free(to_paths);
72 for (GSList *i = converted; i != NULL; i = i->next)
73 items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
75 items = sp_degroup_list (items); // converting to path may have added more groups, descend again
77 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
78 items = g_slist_reverse(items);
80 // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
81 gint position = 0;
82 char const *id = NULL;
83 char const *transform = NULL;
84 gchar *style = NULL;
85 gchar *path_effect = NULL;
87 SPCurve* curve = 0;
88 SPItem *first = NULL;
89 Inkscape::XML::Node *parent = NULL;
91 if (did) {
92 selection->clear();
93 }
95 for (GSList *i = items; i != NULL; i = i->next) { // going from top to bottom
97 SPItem *item = (SPItem *) i->data;
98 if (!SP_IS_PATH(item))
99 continue;
101 if (!did) {
102 selection->clear();
103 did = true;
104 }
106 SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
107 if (first == NULL) { // this is the topmost path
108 first = item;
109 parent = SP_OBJECT_REPR(first)->parent();
110 position = SP_OBJECT_REPR(first)->position();
111 id = SP_OBJECT_REPR(first)->attribute("id");
112 transform = SP_OBJECT_REPR(first)->attribute("transform");
113 // FIXME: merge styles of combined objects instead of using the first one's style
114 style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
115 path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
116 //c->transform(item->transform);
117 curve = c;
118 } else {
119 c->transform(item->getRelativeTransform(SP_OBJECT(first)));
120 curve->append(c, false);
121 c->unref();
122 }
124 // unless this is the topmost object,
125 if (item != first) {
126 // reduce position only if the same parent
127 if (SP_OBJECT_REPR(item)->parent() == parent)
128 position--;
129 // delete the object for real, so that its clones can take appropriate action
130 SP_OBJECT(item)->deleteObject();
131 }
132 }
134 g_slist_free(items);
136 if (did) {
137 SP_OBJECT(first)->deleteObject(false);
138 // delete the topmost.
140 Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
141 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
143 // restore id, transform, path effect, and style
144 repr->setAttribute("id", id);
145 if (transform) {
146 repr->setAttribute("transform", transform);
147 }
148 repr->setAttribute("style", style);
149 g_free(style);
151 repr->setAttribute("inkscape:path-effect", path_effect);
152 g_free(path_effect);
154 // set path data corresponding to new curve
155 gchar *dstring = sp_svg_write_path(curve->get_pathvector());
156 curve->unref();
157 if (path_effect) {
158 repr->setAttribute("inkscape:original-d", dstring);
159 } else {
160 repr->setAttribute("d", dstring);
161 }
162 g_free(dstring);
164 // add the new group to the parent of the topmost
165 parent->appendChild(repr);
167 // move to the position of the topmost, reduced by the number of deleted items
168 repr->setPosition(position > 0 ? position : 0);
170 DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
171 _("Combine"));
173 selection->set(repr);
175 Inkscape::GC::release(repr);
177 } else {
178 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
179 }
181 desktop->clearWaitingCursor();
182 }
184 void
185 sp_selected_path_break_apart(SPDesktop *desktop)
186 {
187 Inkscape::Selection *selection = sp_desktop_selection(desktop);
189 if (selection->isEmpty()) {
190 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
191 return;
192 }
194 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
195 // set "busy" cursor
196 desktop->setWaitingCursor();
198 bool did = false;
200 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
201 items != NULL;
202 items = items->next) {
204 SPItem *item = (SPItem *) items->data;
206 if (!SP_IS_PATH(item))
207 continue;
209 SPPath *path = SP_PATH(item);
211 SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
212 if (curve == NULL)
213 continue;
215 did = true;
217 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
218 gint pos = SP_OBJECT_REPR(item)->position();
219 char const *id = SP_OBJECT_REPR(item)->attribute("id");
221 // XML Tree being used directly here while it shouldn't be...
222 gchar *style = g_strdup(SP_OBJECT(item)->getRepr()->attribute("style"));
223 // XML Tree being used directly here while it shouldn't be...
224 gchar *path_effect = g_strdup(SP_OBJECT(item)->getRepr()->attribute("inkscape:path-effect"));
226 Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
228 curve->unref();
230 // it's going to resurrect as one of the pieces, so we delete without advertisement
231 SP_OBJECT(item)->deleteObject(false);
233 curve = new SPCurve(apv);
234 g_assert(curve != NULL);
236 GSList *list = curve->split();
238 curve->unref();
240 GSList *reprs = NULL;
241 for (GSList *l = list; l != NULL; l = l->next) {
242 curve = (SPCurve *) l->data;
244 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
245 repr->setAttribute("style", style);
247 repr->setAttribute("inkscape:path-effect", path_effect);
249 gchar *str = sp_svg_write_path(curve->get_pathvector());
250 if (path_effect)
251 repr->setAttribute("inkscape:original-d", str);
252 else
253 repr->setAttribute("d", str);
254 g_free(str);
256 // add the new repr to the parent
257 parent->appendChild(repr);
259 // move to the saved position
260 repr->setPosition(pos > 0 ? pos : 0);
262 // if it's the first one, restore id
263 if (l == list)
264 repr->setAttribute("id", id);
266 reprs = g_slist_prepend (reprs, repr);
268 Inkscape::GC::release(repr);
269 }
271 selection->setReprList(reprs);
273 g_slist_free(reprs);
274 g_slist_free(list);
275 g_free(style);
276 g_free(path_effect);
277 }
279 desktop->clearWaitingCursor();
281 if (did) {
282 DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
283 _("Break apart"));
284 } else {
285 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
286 }
287 }
289 /* This function is an entry point from GUI */
290 void
291 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
292 {
293 Inkscape::Selection *selection = sp_desktop_selection(desktop);
295 if (selection->isEmpty()) {
296 if (interactive)
297 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
298 return;
299 }
301 bool did = false;
302 if (interactive) {
303 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
304 // set "busy" cursor
305 desktop->setWaitingCursor();
306 }
308 GSList *selected = g_slist_copy((GSList *) selection->itemList());
309 GSList *to_select = NULL;
310 selection->clear();
311 GSList *items = g_slist_copy(selected);
313 did = sp_item_list_to_curves(items, &selected, &to_select);
315 g_slist_free (items);
316 selection->setReprList(to_select);
317 selection->addList(selected);
318 g_slist_free (to_select);
319 g_slist_free (selected);
321 if (interactive) {
322 desktop->clearWaitingCursor();
323 if (did) {
324 DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
325 _("Object to path"));
326 } else {
327 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
328 return;
329 }
330 }
331 }
333 /** Converts the selected items to LPEItems if they are not already so; e.g. SPRects) */
334 void sp_selected_to_lpeitems(SPDesktop *desktop)
335 {
336 Inkscape::Selection *selection = sp_desktop_selection(desktop);
338 if (selection->isEmpty()) {
339 return;
340 }
342 bool did = false;
344 GSList *selected = g_slist_copy((GSList *) selection->itemList());
345 GSList *to_select = NULL;
346 selection->clear();
347 GSList *items = g_slist_copy(selected);
349 did = sp_item_list_to_curves(items, &selected, &to_select, true);
351 g_slist_free (items);
352 selection->setReprList(to_select);
353 selection->addList(selected);
354 g_slist_free (to_select);
355 g_slist_free (selected);
356 }
358 bool
359 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select, bool skip_all_lpeitems)
360 {
361 bool did = false;
363 for (;
364 items != NULL;
365 items = items->next) {
367 SPItem *item = SP_ITEM(items->data);
368 SPDocument *document = item->document;
370 if ( skip_all_lpeitems &&
371 SP_IS_LPE_ITEM(item) &&
372 !SP_IS_GROUP(item) ) // also convert objects in an SPGroup when skip_all_lpeitems is set.
373 {
374 continue;
375 }
377 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
378 continue; // already a path, and no path effect
379 }
381 if (SP_IS_BOX3D(item)) {
382 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
383 Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
385 if (repr) {
386 *to_select = g_slist_prepend (*to_select, repr);
387 did = true;
388 *selected = g_slist_remove (*selected, item);
389 }
391 continue;
392 }
394 if (SP_IS_GROUP(item)) {
395 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
396 GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
398 GSList *item_to_select = NULL;
399 GSList *item_selected = NULL;
401 if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
402 did = true;
404 g_slist_free(item_list);
405 g_slist_free(item_to_select);
406 g_slist_free(item_selected);
408 continue;
409 }
411 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
412 if (!repr)
413 continue;
415 did = true;
416 *selected = g_slist_remove (*selected, item);
418 // remember the position of the item
419 gint pos = SP_OBJECT_REPR(item)->position();
420 // remember parent
421 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
422 // remember id
423 char const *id = SP_OBJECT_REPR(item)->attribute("id");
424 // remember title
425 gchar *title = item->title();
426 // remember description
427 gchar *desc = item->desc();
429 // It's going to resurrect, so we delete without notifying listeners.
430 SP_OBJECT(item)->deleteObject(false);
432 // restore id
433 repr->setAttribute("id", id);
434 // add the new repr to the parent
435 parent->appendChild(repr);
436 SPObject* newObj = document->getObjectByRepr(repr);
437 if (title && newObj) {
438 newObj->setTitle(title);
439 g_free(title);
440 }
441 if (desc && newObj) {
442 newObj->setDesc(desc);
443 g_free(desc);
444 }
446 // move to the saved position
447 repr->setPosition(pos > 0 ? pos : 0);
449 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
450 * desktops where it was previously selected. */
451 *to_select = g_slist_prepend (*to_select, repr);
452 Inkscape::GC::release(repr);
453 }
455 return did;
456 }
458 Inkscape::XML::Node *
459 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
460 {
461 if (!item)
462 return NULL;
464 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
466 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
467 // Special treatment for text: convert each glyph to separate path, then group the paths
468 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
469 g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
470 /* Mask */
471 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
472 if ( mask_str )
473 g_repr->setAttribute("mask", mask_str);
474 /* Clip path */
475 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
476 if ( clip_path_str )
477 g_repr->setAttribute("clip-path", clip_path_str);
478 /* Rotation center */
479 g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
480 g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
481 /* Whole text's style */
482 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
483 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
484 g_repr->setAttribute("style", style_str);
485 g_free(style_str);
486 Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin();
487 do {
488 Inkscape::Text::Layout::iterator iter_next = iter;
489 iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
490 if (iter == iter_next)
491 break;
493 /* This glyph's style */
494 SPObject const *pos_obj = 0;
495 void *rawptr = 0;
496 te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
497 if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
498 break;
499 pos_obj = SP_OBJECT(rawptr);
500 while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
501 pos_obj = SP_OBJECT_PARENT(pos_obj); // SPStrings don't have style
502 }
503 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
504 SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
506 // get path from iter to iter_next:
507 SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
508 iter = iter_next; // shift to next glyph
509 if (!curve) { // error converting this glyph
510 g_free (style_str);
511 continue;
512 }
513 if (curve->is_empty()) { // whitespace glyph?
514 curve->unref();
515 g_free (style_str);
516 continue;
517 }
519 Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
521 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
522 p_repr->setAttribute("d", def_str);
523 g_free(def_str);
524 curve->unref();
526 p_repr->setAttribute("style", style_str);
527 g_free(style_str);
529 g_repr->appendChild(p_repr);
530 Inkscape::GC::release(p_repr);
532 if (iter == te_get_layout(item)->end())
533 break;
535 } while (true);
537 return g_repr;
538 }
540 SPCurve *curve = NULL;
541 if (SP_IS_SHAPE(item)) {
542 curve = SP_SHAPE(item)->getCurve();
543 }
545 if (!curve)
546 return NULL;
548 // Prevent empty paths from being added to the document
549 // otherwise we end up with zomby markup in the SVG file
550 if(curve->is_empty())
551 {
552 curve->unref();
553 return NULL;
554 }
556 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
557 /* Transformation */
558 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
559 /* Style */
560 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
561 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
562 repr->setAttribute("style", style_str);
563 g_free(style_str);
565 /* Mask */
566 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
567 if ( mask_str )
568 repr->setAttribute("mask", mask_str);
570 /* Clip path */
571 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
572 if ( clip_path_str )
573 repr->setAttribute("clip-path", clip_path_str);
575 /* Rotation center */
576 repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
577 repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
579 /* Definition */
580 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
581 repr->setAttribute("d", def_str);
582 g_free(def_str);
583 curve->unref();
584 return repr;
585 }
588 void
589 sp_selected_path_reverse(SPDesktop *desktop)
590 {
591 Inkscape::Selection *selection = sp_desktop_selection(desktop);
592 GSList *items = (GSList *) selection->itemList();
594 if (!items) {
595 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
596 return;
597 }
600 // set "busy" cursor
601 desktop->setWaitingCursor();
603 bool did = false;
604 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
606 for (GSList *i = items; i != NULL; i = i->next) {
608 if (!SP_IS_PATH(i->data))
609 continue;
611 did = true;
612 SPPath *path = SP_PATH(i->data);
614 SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
616 gchar *str = sp_svg_write_path(rcurve->get_pathvector());
617 if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
618 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
619 } else {
620 SP_OBJECT_REPR(path)->setAttribute("d", str);
621 }
622 g_free(str);
624 rcurve->unref();
626 // reverse nodetypes order (Bug #179866)
627 gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
628 if ( nodetypes ) {
629 SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
630 g_free(nodetypes);
631 }
632 }
634 desktop->clearWaitingCursor();
636 if (did) {
637 DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
638 _("Reverse path"));
639 } else {
640 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
641 }
642 }
644 /*
645 Local Variables:
646 mode:c++
647 c-file-style:"stroustrup"
648 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
649 indent-tabs-mode:nil
650 fill-column:99
651 End:
652 */
653 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :