1
#define __SP_PATH_CHEMISTRY_C__
4
* Here are handlers for modifying selections, specific to paths
7
* Lauris Kaplinski <lauris@kaplinski.com>
8
* bulia byak <buliabyak@users.sf.net>
9
* Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
11
* Copyright (C) 1999-2008 Authors
12
* Copyright (C) 2001-2002 Ximian, Inc.
14
* Released under GNU GPL, read the file 'COPYING' for more information
24
#include "display/curve.h"
25
#include <glib/gmem.h>
26
#include <glibmm/i18n.h>
29
#include "sp-flowtext.h"
30
#include "text-editing.h"
34
#include "message-stack.h"
35
#include "selection.h"
36
#include "desktop-handles.h"
38
#include <2geom/pathvector.h>
39
#include "selection-chemistry.h"
40
#include "path-chemistry.h"
43
sp_selected_path_combine(SPDesktop *desktop)
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."));
53
desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
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);
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
80
char const *id = NULL;
81
char const *transform = NULL;
83
gchar *path_effect = 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))
96
SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
97
if (first == NULL) { // this is the topmost path
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);
109
c->transform(item->getRelativeTransform(SP_OBJECT(first)));
110
curve->append(c, false);
114
// unless this is the topmost object,
116
// reduce position only if the same parent
117
if (SP_OBJECT_REPR(item)->parent() == parent)
119
// delete the object for real, so that its clones can take appropriate action
120
SP_OBJECT(item)->deleteObject();
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);
142
repr->setAttribute("inkscape:path-effect", path_effect);
145
// set path data corresponding to new curve
146
gchar *dstring = sp_svg_write_path(curve->get_pathvector());
149
repr->setAttribute("inkscape:original-d", dstring);
151
repr->setAttribute("d", dstring);
154
// add the new group to the parent of the topmost
155
parent->appendChild(repr);
157
// move to the position of the topmost, reduced by the number of deleted items
158
repr->setPosition(position > 0 ? position : 0);
160
sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE,
163
selection->set(repr);
165
Inkscape::GC::release(repr);
168
sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
171
desktop->clearWaitingCursor();
175
sp_selected_path_break_apart(SPDesktop *desktop)
177
Inkscape::Selection *selection = sp_desktop_selection(desktop);
179
if (selection->isEmpty()) {
180
sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
184
desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
186
desktop->setWaitingCursor();
190
for (GSList *items = g_slist_copy((GSList *) selection->itemList());
192
items = items->next) {
194
SPItem *item = (SPItem *) items->data;
196
if (!SP_IS_PATH(item))
199
SPPath *path = SP_PATH(item);
201
SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
207
Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
208
gint pos = SP_OBJECT_REPR(item)->position();
209
char const *id = SP_OBJECT_REPR(item)->attribute("id");
211
gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
212
gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
214
Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
218
// it's going to resurrect as one of the pieces, so we delete without advertisement
219
SP_OBJECT(item)->deleteObject(false);
221
curve = new SPCurve(apv);
222
g_assert(curve != NULL);
224
GSList *list = curve->split();
228
GSList *reprs = NULL;
229
for (GSList *l = list; l != NULL; l = l->next) {
230
curve = (SPCurve *) l->data;
232
Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
233
repr->setAttribute("style", style);
235
repr->setAttribute("inkscape:path-effect", path_effect);
237
gchar *str = sp_svg_write_path(curve->get_pathvector());
239
repr->setAttribute("inkscape:original-d", str);
241
repr->setAttribute("d", str);
244
// add the new repr to the parent
245
parent->appendChild(repr);
247
// move to the saved position
248
repr->setPosition(pos > 0 ? pos : 0);
250
// if it's the first one, restore id
252
repr->setAttribute("id", id);
254
reprs = g_slist_prepend (reprs, repr);
256
Inkscape::GC::release(repr);
259
selection->setReprList(reprs);
267
desktop->clearWaitingCursor();
270
sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART,
273
sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
277
/* This function is an entry point from GUI */
279
sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
281
Inkscape::Selection *selection = sp_desktop_selection(desktop);
283
if (selection->isEmpty()) {
285
sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
291
desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
293
desktop->setWaitingCursor();
296
GSList *selected = g_slist_copy((GSList *) selection->itemList());
297
GSList *to_select = NULL;
299
GSList *items = g_slist_copy(selected);
301
did = sp_item_list_to_curves(items, &selected, &to_select);
303
g_slist_free (items);
304
selection->setReprList(to_select);
305
selection->addList(selected);
306
g_slist_free (to_select);
307
g_slist_free (selected);
310
desktop->clearWaitingCursor();
312
sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE,
313
_("Object to path"));
315
sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
322
sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
328
items = items->next) {
330
SPItem *item = SP_ITEM(items->data);
331
SPDocument *document = item->document;
333
if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
334
continue; // already a path, and no path effect
337
if (SP_IS_BOX3D(item)) {
338
// convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
339
Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
342
*to_select = g_slist_prepend (*to_select, repr);
344
*selected = g_slist_remove (*selected, item);
350
if (SP_IS_GROUP(item)) {
351
sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
352
GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
354
GSList *item_to_select = NULL;
355
GSList *item_selected = NULL;
357
if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
360
g_slist_free(item_list);
361
g_slist_free(item_to_select);
362
g_slist_free(item_selected);
367
Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
372
*selected = g_slist_remove (*selected, item);
374
// remember the position of the item
375
gint pos = SP_OBJECT_REPR(item)->position();
377
Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
379
char const *id = SP_OBJECT_REPR(item)->attribute("id");
381
gchar *title = item->title();
382
// remember description
383
gchar *desc = item->desc();
385
// It's going to resurrect, so we delete without notifying listeners.
386
SP_OBJECT(item)->deleteObject(false);
389
repr->setAttribute("id", id);
390
// add the new repr to the parent
391
parent->appendChild(repr);
392
SPObject* newObj = document->getObjectByRepr(repr);
393
if (title && newObj) {
394
newObj->setTitle(title);
397
if (desc && newObj) {
398
newObj->setDesc(desc);
402
// move to the saved position
403
repr->setPosition(pos > 0 ? pos : 0);
405
/* Buglet: We don't re-add the (new version of the) object to the selection of any other
406
* desktops where it was previously selected. */
407
*to_select = g_slist_prepend (*to_select, repr);
408
Inkscape::GC::release(repr);
414
Inkscape::XML::Node *
415
sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
420
Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
422
if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
423
// Special treatment for text: convert each glyph to separate path, then group the paths
424
Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
425
g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
427
gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
429
g_repr->setAttribute("mask", mask_str);
431
gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
433
g_repr->setAttribute("clip-path", clip_path_str);
434
/* Rotation center */
435
g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
436
g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
437
/* Whole text's style */
438
gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
439
SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
440
g_repr->setAttribute("style", style_str);
442
Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin();
444
Inkscape::Text::Layout::iterator iter_next = iter;
445
iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
446
if (iter == iter_next)
449
/* This glyph's style */
450
SPObject const *pos_obj = 0;
452
te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
453
if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
455
pos_obj = SP_OBJECT(rawptr);
456
while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
457
pos_obj = SP_OBJECT_PARENT(pos_obj); // SPStrings don't have style
459
gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
460
SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
462
// get path from iter to iter_next:
463
SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
464
iter = iter_next; // shift to next glyph
465
if (!curve) { // error converting this glyph
469
if (curve->is_empty()) { // whitespace glyph?
475
Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
477
gchar *def_str = sp_svg_write_path(curve->get_pathvector());
478
p_repr->setAttribute("d", def_str);
482
p_repr->setAttribute("style", style_str);
485
g_repr->appendChild(p_repr);
486
Inkscape::GC::release(p_repr);
488
if (iter == te_get_layout(item)->end())
496
SPCurve *curve = NULL;
497
if (SP_IS_SHAPE(item)) {
498
curve = sp_shape_get_curve(SP_SHAPE(item));
504
// Prevent empty paths from being added to the document
505
// otherwise we end up with zomby markup in the SVG file
506
if(curve->is_empty())
512
Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
514
repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
516
gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
517
SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
518
repr->setAttribute("style", style_str);
522
gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
524
repr->setAttribute("mask", mask_str);
527
gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
529
repr->setAttribute("clip-path", clip_path_str);
531
/* Rotation center */
532
repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
533
repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
536
gchar *def_str = sp_svg_write_path(curve->get_pathvector());
537
repr->setAttribute("d", def_str);
545
sp_selected_path_reverse(SPDesktop *desktop)
547
Inkscape::Selection *selection = sp_desktop_selection(desktop);
548
GSList *items = (GSList *) selection->itemList();
551
sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
557
desktop->setWaitingCursor();
560
desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
562
for (GSList *i = items; i != NULL; i = i->next) {
564
if (!SP_IS_PATH(i->data))
568
SPPath *path = SP_PATH(i->data);
570
SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
572
gchar *str = sp_svg_write_path(rcurve->get_pathvector());
573
if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
574
SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
576
SP_OBJECT_REPR(path)->setAttribute("d", str);
582
// reverse nodetypes order (Bug #179866)
583
gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
585
SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
590
desktop->clearWaitingCursor();
593
sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
596
sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
603
c-file-style:"stroustrup"
604
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
609
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :