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);
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 // set path data corresponding to new curve
143 gchar *dstring = sp_svg_write_path(curve->get_pathvector());
144 curve->unref();
145 repr->setAttribute("d", dstring);
146 if (path_effect)
147 repr->setAttribute("inkscape:original-d", dstring);
148 g_free(dstring);
150 repr->setAttribute("inkscape:path-effect", path_effect);
151 g_free(path_effect);
153 // add the new group to the parent of the topmost
154 parent->appendChild(repr);
156 // move to the position of the topmost, reduced by the number of deleted items
157 repr->setPosition(position > 0 ? position : 0);
159 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
160 _("Combine"));
162 selection->set(repr);
164 Inkscape::GC::release(repr);
166 } else {
167 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
168 }
170 desktop->clearWaitingCursor();
171 }
173 void
174 sp_selected_path_break_apart(SPDesktop *desktop)
175 {
176 Inkscape::Selection *selection = sp_desktop_selection(desktop);
178 if (selection->isEmpty()) {
179 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
180 return;
181 }
183 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
184 // set "busy" cursor
185 desktop->setWaitingCursor();
187 bool did = false;
189 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
190 items != NULL;
191 items = items->next) {
193 SPItem *item = (SPItem *) items->data;
195 if (!SP_IS_PATH(item))
196 continue;
198 SPPath *path = SP_PATH(item);
200 SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
201 if (curve == NULL)
202 continue;
204 did = true;
206 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
207 gint pos = SP_OBJECT_REPR(item)->position();
208 char const *id = SP_OBJECT_REPR(item)->attribute("id");
210 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
211 gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
213 Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
215 curve->unref();
217 // it's going to resurrect as one of the pieces, so we delete without advertisement
218 SP_OBJECT(item)->deleteObject(false);
220 curve = new SPCurve(apv);
221 g_assert(curve != NULL);
223 GSList *list = curve->split();
225 curve->unref();
227 GSList *reprs = NULL;
228 for (GSList *l = list; l != NULL; l = l->next) {
229 curve = (SPCurve *) l->data;
231 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
232 repr->setAttribute("style", style);
234 gchar *str = sp_svg_write_path(curve->get_pathvector());
235 repr->setAttribute("d", str);
236 if (path_effect)
237 repr->setAttribute("inkscape:original-d", str);
238 g_free(str);
240 repr->setAttribute("inkscape:path-effect", path_effect);
242 // add the new repr to the parent
243 parent->appendChild(repr);
245 // move to the saved position
246 repr->setPosition(pos > 0 ? pos : 0);
248 // if it's the first one, restore id
249 if (l == list)
250 repr->setAttribute("id", id);
252 reprs = g_slist_prepend (reprs, repr);
254 Inkscape::GC::release(repr);
255 }
257 selection->setReprList(reprs);
259 g_slist_free(reprs);
260 g_slist_free(list);
261 g_free(style);
263 }
265 desktop->clearWaitingCursor();
267 if (did) {
268 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
269 _("Break apart"));
270 } else {
271 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
272 }
273 }
275 /* This function is an entry point from GUI */
276 void
277 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
278 {
279 Inkscape::Selection *selection = sp_desktop_selection(desktop);
281 if (selection->isEmpty()) {
282 if (interactive)
283 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
284 return;
285 }
287 bool did = false;
288 if (interactive) {
289 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
290 // set "busy" cursor
291 desktop->setWaitingCursor();
292 }
294 GSList *selected = g_slist_copy((GSList *) selection->itemList());
295 GSList *to_select = NULL;
296 selection->clear();
297 GSList *items = g_slist_copy(selected);
299 did = sp_item_list_to_curves(items, &selected, &to_select);
301 g_slist_free (items);
302 selection->setReprList(to_select);
303 selection->addList(selected);
304 g_slist_free (to_select);
305 g_slist_free (selected);
307 if (interactive) {
308 desktop->clearWaitingCursor();
309 if (did) {
310 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
311 _("Object to path"));
312 } else {
313 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
314 return;
315 }
316 }
317 }
319 bool
320 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
321 {
322 bool did = false;
324 for (;
325 items != NULL;
326 items = items->next) {
328 SPItem *item = SP_ITEM(items->data);
329 SPDocument *document = item->document;
331 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
332 continue; // already a path, and no path effect
333 }
335 if (SP_IS_BOX3D(item)) {
336 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
337 Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
339 if (repr) {
340 *to_select = g_slist_prepend (*to_select, repr);
341 did = true;
342 *selected = g_slist_remove (*selected, item);
343 }
345 continue;
346 }
348 if (SP_IS_GROUP(item)) {
349 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
350 GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
352 GSList *item_to_select = NULL;
353 GSList *item_selected = NULL;
355 if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
356 did = true;
358 g_slist_free(item_list);
359 g_slist_free(item_to_select);
360 g_slist_free(item_selected);
362 continue;
363 }
365 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
366 if (!repr)
367 continue;
369 did = true;
370 *selected = g_slist_remove (*selected, item);
372 // remember the position of the item
373 gint pos = SP_OBJECT_REPR(item)->position();
374 // remember parent
375 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
376 // remember id
377 char const *id = SP_OBJECT_REPR(item)->attribute("id");
378 // remember title
379 gchar *title = item->title();
380 // remember description
381 gchar *desc = item->desc();
383 // It's going to resurrect, so we delete without notifying listeners.
384 SP_OBJECT(item)->deleteObject(false);
386 // restore id
387 repr->setAttribute("id", id);
388 // add the new repr to the parent
389 parent->appendChild(repr);
390 SPObject* newObj = document->getObjectByRepr(repr);
391 if (title && newObj) {
392 newObj->setTitle(title);
393 g_free(title);
394 }
395 if (desc && newObj) {
396 newObj->setDesc(desc);
397 g_free(desc);
398 }
400 // move to the saved position
401 repr->setPosition(pos > 0 ? pos : 0);
403 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
404 * desktops where it was previously selected. */
405 *to_select = g_slist_prepend (*to_select, repr);
406 Inkscape::GC::release(repr);
407 }
409 return did;
410 }
412 Inkscape::XML::Node *
413 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
414 {
415 if (!item)
416 return NULL;
418 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
420 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
421 // Special treatment for text: convert each glyph to separate path, then group the paths
422 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
423 g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
424 /* Mask */
425 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
426 if ( mask_str )
427 g_repr->setAttribute("mask", mask_str);
428 /* Clip path */
429 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
430 if ( clip_path_str )
431 g_repr->setAttribute("clip-path", clip_path_str);
432 /* Rotation center */
433 g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
434 g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
435 /* Whole text's style */
436 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
437 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
438 g_repr->setAttribute("style", style_str);
439 g_free(style_str);
440 Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin();
441 do {
442 Inkscape::Text::Layout::iterator iter_next = iter;
443 iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
444 if (iter == iter_next)
445 break;
447 /* This glyph's style */
448 SPObject const *pos_obj = 0;
449 void *rawptr = 0;
450 te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
451 if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
452 break;
453 pos_obj = SP_OBJECT(rawptr);
454 while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
455 pos_obj = SP_OBJECT_PARENT(pos_obj); // SPStrings don't have style
456 }
457 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
458 SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
460 // get path from iter to iter_next:
461 SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
462 iter = iter_next; // shift to next glyph
463 if (!curve) { // error converting this glyph
464 g_free (style_str);
465 continue;
466 }
467 if (curve->is_empty()) { // whitespace glyph?
468 curve->unref();
469 g_free (style_str);
470 continue;
471 }
473 Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
475 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
476 p_repr->setAttribute("d", def_str);
477 g_free(def_str);
478 curve->unref();
480 p_repr->setAttribute("style", style_str);
481 g_free(style_str);
483 g_repr->appendChild(p_repr);
484 Inkscape::GC::release(p_repr);
486 if (iter == te_get_layout(item)->end())
487 break;
489 } while (true);
491 return g_repr;
492 }
494 SPCurve *curve = NULL;
495 if (SP_IS_SHAPE(item)) {
496 curve = sp_shape_get_curve(SP_SHAPE(item));
497 }
499 if (!curve)
500 return NULL;
502 // Prevent empty paths from being added to the document
503 // otherwise we end up with zomby markup in the SVG file
504 if(curve->is_empty())
505 {
506 curve->unref();
507 return NULL;
508 }
510 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
511 /* Transformation */
512 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
513 /* Style */
514 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
515 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
516 repr->setAttribute("style", style_str);
517 g_free(style_str);
519 /* Mask */
520 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
521 if ( mask_str )
522 repr->setAttribute("mask", mask_str);
524 /* Clip path */
525 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
526 if ( clip_path_str )
527 repr->setAttribute("clip-path", clip_path_str);
529 /* Rotation center */
530 repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
531 repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
533 /* Definition */
534 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
535 repr->setAttribute("d", def_str);
536 g_free(def_str);
537 curve->unref();
538 return repr;
539 }
542 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
543 void
544 sp_selected_path_reverse(SPDesktop *desktop)
545 {
546 Inkscape::Selection *selection = sp_desktop_selection(desktop);
547 GSList *items = (GSList *) selection->itemList();
549 if (!items) {
550 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
551 return;
552 }
555 // set "busy" cursor
556 desktop->setWaitingCursor();
558 bool did = false;
559 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
561 for (GSList *i = items; i != NULL; i = i->next) {
563 if (!SP_IS_PATH(i->data))
564 continue;
566 did = true;
567 SPPath *path = SP_PATH(i->data);
569 SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
571 gchar *str = sp_svg_write_path(rcurve->get_pathvector());
572 if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
573 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
574 } else {
575 SP_OBJECT_REPR(path)->setAttribute("d", str);
576 }
577 g_free(str);
579 rcurve->unref();
580 }
582 desktop->clearWaitingCursor();
584 if (did) {
585 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
586 _("Reverse path"));
587 } else {
588 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
589 }
590 }
592 /*
593 Local Variables:
594 mode:c++
595 c-file-style:"stroustrup"
596 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
597 indent-tabs-mode:nil
598 fill-column:99
599 End:
600 */
601 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :