0227e9caf5feaef2c283f48623737e6401257d0e
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 "path-chemistry.h"
41 void
42 sp_selected_path_combine(SPDesktop *desktop)
43 {
44 Inkscape::Selection *selection = sp_desktop_selection(desktop);
45 SPDocument *doc = sp_desktop_document(desktop);
47 if (g_slist_length((GSList *) selection->itemList()) < 2) {
48 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
49 return;
50 }
52 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
53 // set "busy" cursor
54 desktop->setWaitingCursor();
56 GSList *items = g_slist_copy((GSList *) selection->itemList());
57 GSList *to_paths = NULL;
58 for (GSList *i = items; i != NULL; i = i->next) {
59 SPItem *item = (SPItem *) i->data;
60 if (!SP_IS_PATH(item))
61 to_paths = g_slist_prepend(to_paths, item);
62 }
63 GSList *converted = NULL;
64 bool did = sp_item_list_to_curves(to_paths, &items, &converted);
65 g_slist_free(to_paths);
66 for (GSList *i = converted; i != NULL; i = i->next)
67 items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
69 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
70 items = g_slist_reverse(items);
72 // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
73 gint position = 0;
74 char const *id = NULL;
75 char const *transform = NULL;
76 gchar *style = NULL;
77 gchar *path_effect = NULL;
79 SPCurve* curve = 0;
80 SPItem *first = NULL;
81 Inkscape::XML::Node *parent = NULL;
83 for (GSList *i = items; i != NULL; i = i->next) { // going from top to bottom
85 SPItem *item = (SPItem *) i->data;
86 if (!SP_IS_PATH(item))
87 continue;
88 did = true;
90 SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
91 if (first == NULL) { // this is the topmost path
92 first = item;
93 parent = SP_OBJECT_REPR(first)->parent();
94 position = SP_OBJECT_REPR(first)->position();
95 id = SP_OBJECT_REPR(first)->attribute("id");
96 transform = SP_OBJECT_REPR(first)->attribute("transform");
97 // FIXME: merge styles of combined objects instead of using the first one's style
98 style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
99 path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
100 //c->transform(item->transform);
101 curve = c;
102 } else {
103 c->transform(item->getRelativeTransform(SP_OBJECT(first)));
104 curve->append(c, false);
105 c->unref();
106 }
108 // unless this is the topmost object,
109 if (item != first) {
110 // reduce position only if the same parent
111 if (SP_OBJECT_REPR(item)->parent() == parent)
112 position--;
113 // delete the object for real, so that its clones can take appropriate action
114 SP_OBJECT(item)->deleteObject();
115 }
116 }
118 g_slist_free(items);
120 if (did) {
121 selection->clear();
123 // delete the topmost one so that its clones don't get alerted; this object will be
124 // restored shortly, with the same id
125 SP_OBJECT(first)->deleteObject(false);
127 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
128 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
130 // restore id, transform, path effect, and style
131 repr->setAttribute("id", id);
132 if (transform) repr->setAttribute("transform", transform);
133 repr->setAttribute("style", style);
134 g_free(style);
136 // set path data corresponding to new curve
137 gchar *dstring = sp_svg_write_path(curve->get_pathvector());
138 curve->unref();
139 repr->setAttribute("d", dstring);
140 if (path_effect)
141 repr->setAttribute("inkscape:original-d", dstring);
142 g_free(dstring);
144 repr->setAttribute("inkscape:path-effect", path_effect);
145 g_free(path_effect);
147 // add the new group to the parent of the topmost
148 parent->appendChild(repr);
150 // move to the position of the topmost, reduced by the number of deleted items
151 repr->setPosition(position > 0 ? position : 0);
153 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
154 _("Combine"));
156 selection->set(repr);
158 Inkscape::GC::release(repr);
160 } else {
161 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
162 }
164 desktop->clearWaitingCursor();
165 }
167 void
168 sp_selected_path_break_apart(SPDesktop *desktop)
169 {
170 Inkscape::Selection *selection = sp_desktop_selection(desktop);
172 if (selection->isEmpty()) {
173 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
174 return;
175 }
177 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
178 // set "busy" cursor
179 desktop->setWaitingCursor();
181 bool did = false;
183 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
184 items != NULL;
185 items = items->next) {
187 SPItem *item = (SPItem *) items->data;
189 if (!SP_IS_PATH(item))
190 continue;
192 SPPath *path = SP_PATH(item);
194 SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
195 if (curve == NULL)
196 continue;
198 did = true;
200 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
201 gint pos = SP_OBJECT_REPR(item)->position();
202 char const *id = SP_OBJECT_REPR(item)->attribute("id");
204 gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
205 gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
207 Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
209 curve->unref();
211 // it's going to resurrect as one of the pieces, so we delete without advertisement
212 SP_OBJECT(item)->deleteObject(false);
214 curve = new SPCurve(apv);
215 g_assert(curve != NULL);
217 GSList *list = curve->split();
219 curve->unref();
221 GSList *reprs = NULL;
222 for (GSList *l = list; l != NULL; l = l->next) {
223 curve = (SPCurve *) l->data;
225 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
226 repr->setAttribute("style", style);
228 gchar *str = sp_svg_write_path(curve->get_pathvector());
229 repr->setAttribute("d", str);
230 if (path_effect)
231 repr->setAttribute("inkscape:original-d", str);
232 g_free(str);
234 repr->setAttribute("inkscape:path-effect", path_effect);
236 // add the new repr to the parent
237 parent->appendChild(repr);
239 // move to the saved position
240 repr->setPosition(pos > 0 ? pos : 0);
242 // if it's the first one, restore id
243 if (l == list)
244 repr->setAttribute("id", id);
246 reprs = g_slist_prepend (reprs, repr);
248 Inkscape::GC::release(repr);
249 }
251 selection->setReprList(reprs);
253 g_slist_free(reprs);
254 g_slist_free(list);
255 g_free(style);
257 }
259 desktop->clearWaitingCursor();
261 if (did) {
262 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
263 _("Break apart"));
264 } else {
265 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
266 }
267 }
269 /* This function is an entry point from GUI */
270 void
271 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
272 {
273 Inkscape::Selection *selection = sp_desktop_selection(desktop);
275 if (selection->isEmpty()) {
276 if (interactive)
277 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
278 return;
279 }
281 bool did = false;
282 if (interactive) {
283 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
284 // set "busy" cursor
285 desktop->setWaitingCursor();
286 }
288 GSList *selected = g_slist_copy((GSList *) selection->itemList());
289 GSList *to_select = NULL;
290 selection->clear();
291 GSList *items = g_slist_copy(selected);
293 did = sp_item_list_to_curves(items, &selected, &to_select);
295 g_slist_free (items);
296 selection->setReprList(to_select);
297 selection->addList(selected);
298 g_slist_free (to_select);
299 g_slist_free (selected);
301 if (interactive) {
302 desktop->clearWaitingCursor();
303 if (did) {
304 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
305 _("Object to path"));
306 } else {
307 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
308 return;
309 }
310 }
311 }
313 bool
314 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
315 {
316 bool did = false;
318 for (;
319 items != NULL;
320 items = items->next) {
322 SPItem *item = SP_ITEM(items->data);
323 SPDocument *document = item->document;
325 if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
326 continue; // already a path, and no path effect
327 }
329 if (SP_IS_BOX3D(item)) {
330 // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
331 Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
333 if (repr) {
334 *to_select = g_slist_prepend (*to_select, repr);
335 did = true;
336 *selected = g_slist_remove (*selected, item);
337 }
339 continue;
340 }
342 if (SP_IS_GROUP(item)) {
343 sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
344 GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
346 GSList *item_to_select = NULL;
347 GSList *item_selected = NULL;
349 if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
350 did = true;
352 g_slist_free(item_list);
353 g_slist_free(item_to_select);
354 g_slist_free(item_selected);
356 continue;
357 }
359 Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
360 if (!repr)
361 continue;
363 did = true;
364 *selected = g_slist_remove (*selected, item);
366 // remember the position of the item
367 gint pos = SP_OBJECT_REPR(item)->position();
368 // remember parent
369 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
370 // remember id
371 char const *id = SP_OBJECT_REPR(item)->attribute("id");
372 // remember title
373 gchar *title = item->title();
374 // remember description
375 gchar *desc = item->desc();
377 // It's going to resurrect, so we delete without notifying listeners.
378 SP_OBJECT(item)->deleteObject(false);
380 // restore id
381 repr->setAttribute("id", id);
382 // add the new repr to the parent
383 parent->appendChild(repr);
384 SPObject* newObj = document->getObjectByRepr(repr);
385 if (title && newObj) {
386 newObj->setTitle(title);
387 g_free(title);
388 }
389 if (desc && newObj) {
390 newObj->setDesc(desc);
391 g_free(desc);
392 }
394 // move to the saved position
395 repr->setPosition(pos > 0 ? pos : 0);
397 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
398 * desktops where it was previously selected. */
399 *to_select = g_slist_prepend (*to_select, repr);
400 Inkscape::GC::release(repr);
401 }
403 return did;
404 }
406 Inkscape::XML::Node *
407 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
408 {
409 if (!item)
410 return NULL;
412 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
414 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
415 // Special treatment for text: convert each glyph to separate path, then group the paths
416 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
417 g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
418 /* Mask */
419 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
420 if ( mask_str )
421 g_repr->setAttribute("mask", mask_str);
422 /* Clip path */
423 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
424 if ( clip_path_str )
425 g_repr->setAttribute("clip-path", clip_path_str);
426 /* Rotation center */
427 g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
428 g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
429 /* Whole text's style */
430 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
431 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
432 g_repr->setAttribute("style", style_str);
433 g_free(style_str);
434 Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin();
435 do {
436 Inkscape::Text::Layout::iterator iter_next = iter;
437 iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
438 if (iter == iter_next)
439 break;
441 /* This glyph's style */
442 SPObject const *pos_obj = 0;
443 void *rawptr = 0;
444 te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
445 if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
446 break;
447 pos_obj = SP_OBJECT(rawptr);
448 while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
449 pos_obj = SP_OBJECT_PARENT(pos_obj); // SPStrings don't have style
450 }
451 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
452 SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
454 // get path from iter to iter_next:
455 SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
456 iter = iter_next; // shift to next glyph
457 if (!curve) { // error converting this glyph
458 g_free (style_str);
459 continue;
460 }
461 if (curve->is_empty()) { // whitespace glyph?
462 curve->unref();
463 g_free (style_str);
464 continue;
465 }
467 Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
469 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
470 p_repr->setAttribute("d", def_str);
471 g_free(def_str);
472 curve->unref();
474 p_repr->setAttribute("style", style_str);
475 g_free(style_str);
477 g_repr->appendChild(p_repr);
478 Inkscape::GC::release(p_repr);
480 if (iter == te_get_layout(item)->end())
481 break;
483 } while (true);
485 return g_repr;
486 }
488 SPCurve *curve = NULL;
489 if (SP_IS_SHAPE(item)) {
490 curve = sp_shape_get_curve(SP_SHAPE(item));
491 }
493 if (!curve)
494 return NULL;
496 // Prevent empty paths from being added to the document
497 // otherwise we end up with zomby markup in the SVG file
498 if(curve->is_empty())
499 {
500 curve->unref();
501 return NULL;
502 }
504 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
505 /* Transformation */
506 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
507 /* Style */
508 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
509 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
510 repr->setAttribute("style", style_str);
511 g_free(style_str);
513 /* Mask */
514 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
515 if ( mask_str )
516 repr->setAttribute("mask", mask_str);
518 /* Clip path */
519 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
520 if ( clip_path_str )
521 repr->setAttribute("clip-path", clip_path_str);
523 /* Rotation center */
524 repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
525 repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
527 /* Definition */
528 gchar *def_str = sp_svg_write_path(curve->get_pathvector());
529 repr->setAttribute("d", def_str);
530 g_free(def_str);
531 curve->unref();
532 return repr;
533 }
536 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
537 void
538 sp_selected_path_reverse(SPDesktop *desktop)
539 {
540 Inkscape::Selection *selection = sp_desktop_selection(desktop);
541 GSList *items = (GSList *) selection->itemList();
543 if (!items) {
544 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
545 return;
546 }
549 // set "busy" cursor
550 desktop->setWaitingCursor();
552 bool did = false;
553 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
555 for (GSList *i = items; i != NULL; i = i->next) {
557 if (!SP_IS_PATH(i->data))
558 continue;
560 did = true;
561 SPPath *path = SP_PATH(i->data);
563 SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
565 gchar *str = sp_svg_write_path(rcurve->get_pathvector());
566 if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
567 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
568 } else {
569 SP_OBJECT_REPR(path)->setAttribute("d", str);
570 }
571 g_free(str);
573 rcurve->unref();
574 }
576 desktop->clearWaitingCursor();
578 if (did) {
579 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
580 _("Reverse path"));
581 } else {
582 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
583 }
584 }
586 /*
587 Local Variables:
588 mode:c++
589 c-file-style:"stroustrup"
590 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
591 indent-tabs-mode:nil
592 fill-column:99
593 End:
594 */
595 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :