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 "libnr/nr-path.h"
31 #include "text-editing.h"
32 #include "style.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "document.h"
36 #include "message-stack.h"
37 #include "selection.h"
38 #include "desktop-handles.h"
39 #include "box3d.h"
41 #include "path-chemistry.h"
43 /* Helper functions for sp_selected_path_to_curves */
44 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
45 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
47 enum {
48 /* Not used yet. This is the placeholder of Lauris's idea. */
49 SP_TOCURVE_INTERACTIVE = 1 << 0,
50 SP_TOCURVE_GROUPING_BY_WORD = 1 << 1,
51 SP_TOCURVE_GROUPING_BY_LINE = 1 << 2,
52 SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
53 };
55 void
56 sp_selected_path_combine(void)
57 {
58 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
59 Inkscape::Selection *selection = sp_desktop_selection(desktop);
61 if (g_slist_length((GSList *) selection->itemList()) < 2) {
62 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
63 return;
64 }
66 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
67 // set "busy" cursor
68 desktop->setWaitingCursor();
70 sp_selected_path_to_curves0(FALSE, 0);
72 GSList *items = g_slist_copy((GSList *) selection->itemList());
73 items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
74 items = g_slist_reverse(items);
76 // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
77 gint position = 0;
78 char const *id = NULL;
79 char const *transform = NULL;
80 gchar *style = NULL;
82 SPCurve* curve = 0;
83 bool did = false;
84 SPItem *first = NULL;
85 Inkscape::XML::Node *parent = NULL;
87 for (GSList *i = items; i != NULL; i = i->next) { // going from top to bottom
89 SPItem *item = (SPItem *) i->data;
90 if (!SP_IS_PATH(item))
91 continue;
92 did = true;
94 SPCurve *c = sp_shape_get_curve(SP_SHAPE(item));
95 if (first == NULL) { // this is the topmost path
96 first = item;
97 parent = SP_OBJECT_REPR(first)->parent();
98 position = SP_OBJECT_REPR(first)->position();
99 id = SP_OBJECT_REPR(first)->attribute("id");
100 transform = SP_OBJECT_REPR(first)->attribute("transform");
101 // FIXME: merge styles of combined objects instead of using the first one's style
102 style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
103 //sp_curve_transform(c, item->transform);
104 curve = c;
105 } else {
106 sp_curve_transform(c, item->getRelativeTransform(SP_OBJECT(first)));
107 sp_curve_append(curve, c, false);
108 sp_curve_unref(c);
109 }
111 // unless this is the topmost object,
112 if (item != first) {
113 // reduce position only if the same parent
114 if (SP_OBJECT_REPR(item)->parent() == parent)
115 position--;
116 // delete the object for real, so that its clones can take appropriate action
117 SP_OBJECT(item)->deleteObject();
118 }
119 }
121 g_slist_free(items);
123 if (did) {
124 selection->clear();
126 // delete the topmost one so that its clones don't get alerted; this object will be
127 // restored shortly, with the same id
128 SP_OBJECT(first)->deleteObject(false);
130 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
131 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
133 // restore id, transform and style
134 repr->setAttribute("id", id);
135 if (transform) repr->setAttribute("transform", transform);
136 repr->setAttribute("style", style);
137 g_free(style);
139 // set path data corresponding to new curve
140 gchar *dstring = sp_svg_write_path(SP_CURVE_BPATH(curve));
141 sp_curve_unref(curve);
142 repr->setAttribute("d", dstring);
143 g_free(dstring);
145 // add the new group to the parent of the topmost
146 parent->appendChild(repr);
148 // move to the position of the topmost, reduced by the number of deleted items
149 repr->setPosition(position > 0 ? position : 0);
151 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
152 _("Combine"));
154 selection->set(repr);
156 Inkscape::GC::release(repr);
158 } else {
159 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
160 }
162 desktop->clearWaitingCursor();
163 }
165 void
166 sp_selected_path_break_apart(void)
167 {
168 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
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_shape_get_curve(SP_SHAPE(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"));
206 NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
208 sp_curve_unref(curve);
210 // it's going to resurrect as one of the pieces, so we delete without advertisement
211 SP_OBJECT(item)->deleteObject(false);
213 curve = sp_curve_new_from_bpath(abp);
214 g_assert(curve != NULL);
216 GSList *list = sp_curve_split(curve);
218 sp_curve_unref(curve);
220 GSList *reprs = NULL;
221 for (GSList *l = list; l != NULL; l = l->next) {
222 curve = (SPCurve *) l->data;
224 Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
225 repr->setAttribute("style", style);
227 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
228 repr->setAttribute("d", str);
229 g_free(str);
231 // add the new repr to the parent
232 parent->appendChild(repr);
234 // move to the saved position
235 repr->setPosition(pos > 0 ? pos : 0);
237 // if it's the first one, restore id
238 if (l == list)
239 repr->setAttribute("id", id);
241 reprs = g_slist_prepend (reprs, repr);
243 Inkscape::GC::release(repr);
244 }
246 selection->setReprList(reprs);
248 g_slist_free(reprs);
249 g_slist_free(list);
250 g_free(style);
252 }
254 desktop->clearWaitingCursor();
256 if (did) {
257 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
258 _("Break apart"));
259 } else {
260 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
261 }
262 }
264 /* This function is an entry point from GUI */
265 void
266 sp_selected_path_to_curves(bool interactive)
267 {
268 if (interactive) {
269 sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
270 } else {
271 sp_selected_path_to_curves0(false, 0);
272 }
273 }
275 static void
276 sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
277 {
278 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
280 Inkscape::Selection *selection = sp_desktop_selection(desktop);
282 if (selection->isEmpty()) {
283 if (interactive)
284 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
285 return;
286 }
288 bool did = false;
289 if (interactive) {
290 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
291 // set "busy" cursor
292 desktop->setWaitingCursor();
293 }
295 GSList *selected = g_slist_copy((GSList *) selection->itemList());
296 GSList *to_select = NULL;
297 selection->clear();
298 GSList *items = g_slist_copy(selected);
300 did = sp_item_list_to_curves(items, &selected, &to_select);
302 g_slist_free (items);
303 selection->setReprList(to_select);
304 selection->addList(selected);
305 g_slist_free (to_select);
306 g_slist_free (selected);
308 if (interactive) {
309 desktop->clearWaitingCursor();
310 if (did) {
311 sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
312 _("Object to path"));
313 } else {
314 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
315 return;
316 }
317 }
318 }
320 static bool
321 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
322 {
323 bool did = false;
325 for (;
326 items != NULL;
327 items = items->next) {
329 SPItem *item = SP_ITEM(items->data);
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_path_effect(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");
379 // It's going to resurrect, so we delete without notifying listeners.
380 SP_OBJECT(item)->deleteObject(false);
382 // restore id
383 repr->setAttribute("id", id);
384 // add the new repr to the parent
385 parent->appendChild(repr);
386 // move to the saved position
387 repr->setPosition(pos > 0 ? pos : 0);
389 /* Buglet: We don't re-add the (new version of the) object to the selection of any other
390 * desktops where it was previously selected. */
391 *to_select = g_slist_prepend (*to_select, repr);
392 Inkscape::GC::release(repr);
393 }
395 return did;
396 }
398 Inkscape::XML::Node *
399 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
400 {
401 if (!item)
402 return NULL;
404 SPCurve *curve = NULL;
405 if (SP_IS_SHAPE(item)) {
406 curve = sp_shape_get_curve(SP_SHAPE(item));
407 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
408 curve = te_get_layout(item)->convertToCurves();
409 }
411 if (!curve)
412 return NULL;
414 // Prevent empty paths from being added to the document
415 // otherwise we end up with zomby markup in the SVG file
416 if(curve->end <= 0)
417 {
418 sp_curve_unref(curve);
419 return NULL;
420 }
422 Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
423 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
424 /* Transformation */
425 repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
426 /* Style */
427 gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
428 SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
429 repr->setAttribute("style", style_str);
430 g_free(style_str);
432 /* Mask */
433 gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
434 if ( mask_str )
435 repr->setAttribute("mask", mask_str);
437 /* Clip path */
438 gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
439 if ( clip_path_str )
440 repr->setAttribute("clip-path", clip_path_str);
442 /* Rotation center */
443 sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
444 sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
446 /* Definition */
447 gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
448 repr->setAttribute("d", def_str);
449 g_free(def_str);
450 sp_curve_unref(curve);
451 return repr;
452 }
455 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
456 void
457 sp_selected_path_reverse()
458 {
459 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
461 Inkscape::Selection *selection = sp_desktop_selection(desktop);
462 GSList *items = (GSList *) selection->itemList();
464 if (!items) {
465 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
466 return;
467 }
470 // set "busy" cursor
471 desktop->setWaitingCursor();
473 bool did = false;
474 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
476 for (GSList *i = items; i != NULL; i = i->next) {
478 if (!SP_IS_PATH(i->data))
479 continue;
481 did = true;
482 SPPath *path = SP_PATH(i->data);
484 SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
486 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
487 if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
488 SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
489 } else {
490 SP_OBJECT_REPR(path)->setAttribute("d", str);
491 }
492 g_free(str);
494 sp_curve_unref(rcurve);
495 }
497 desktop->clearWaitingCursor();
499 if (did) {
500 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
501 _("Reverse path"));
502 } else {
503 sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
504 }
505 }
507 /*
508 Local Variables:
509 mode:c++
510 c-file-style:"stroustrup"
511 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
512 indent-tabs-mode:nil
513 fill-column:99
514 End:
515 */
516 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :