1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Here are handlers for modifying selections, specific to paths
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* Copyright (C) 1999-2008 Authors
* Copyright (C) 2001-2002 Ximian, Inc.
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include <cstring>
#include <string>
#include <glibmm/i18n.h>
#include "desktop.h"
#include "document-undo.h"
#include "document.h"
#include "message-stack.h"
#include "path-chemistry.h"
#include "selection-chemistry.h"
#include "selection.h"
#include "text-editing.h"
#include "verbs.h"
#include "display/curve.h"
#include "display/sp-canvas.h"
#include "object/box3d.h"
#include "object/object-set.h"
#include "object/sp-flowtext.h"
#include "object/sp-path.h"
#include "object/sp-text.h"
#include "style.h"
#include "svg/svg.h"
#include "xml/repr.h"
using Inkscape::DocumentUndo;
using Inkscape::ObjectSet;
inline bool less_than_items(SPItem const *first, SPItem const *second)
{
return sp_repr_compare_position(first->getRepr(),
second->getRepr())<0;
}
void
ObjectSet::combine(bool skip_undo)
{
//Inkscape::Selection *selection = desktop->getSelection();
//Inkscape::Preferences *prefs = Inkscape::Preferences::get();
SPDocument *doc = document();
std::vector<SPItem*> items_copy(items().begin(), items().end());
if (items_copy.size() < 1) {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to combine."));
return;
}
if(desktop()){
desktop()->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
// set "busy" cursor
desktop()->setWaitingCursor();
}
items_copy = sp_degroup_list (items_copy); // descend into any groups in selection
std::vector<SPItem*> to_paths;
for (std::vector<SPItem*>::const_reverse_iterator i = items_copy.rbegin(); i != items_copy.rend(); ++i) {
if (!dynamic_cast<SPPath *>(*i) && !dynamic_cast<SPGroup *>(*i)) {
to_paths.push_back(*i);
}
}
std::vector<Inkscape::XML::Node*> converted;
bool did = sp_item_list_to_curves(to_paths, items_copy, converted);
for (auto i : converted)
items_copy.push_back((SPItem*)doc->getObjectByRepr(i));
items_copy = sp_degroup_list (items_copy); // converting to path may have added more groups, descend again
sort(items_copy.begin(),items_copy.end(),less_than_items);
assert(!items_copy.empty()); // cannot be NULL because of list length check at top of function
// remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
gint position = 0;
char const *transform = nullptr;
char const *path_effect = nullptr;
SPCurve* curve = nullptr;
SPItem *first = nullptr;
Inkscape::XML::Node *parent = nullptr;
if (did) {
clear();
}
for (std::vector<SPItem*>::const_reverse_iterator i = items_copy.rbegin(); i != items_copy.rend(); ++i){
SPItem *item = *i;
SPPath *path = dynamic_cast<SPPath *>(item);
if (!path) {
continue;
}
if (!did) {
clear();
did = true;
}
SPCurve *c = path->getCurveForEdit();
if (first == nullptr) { // this is the topmost path
first = item;
parent = first->getRepr()->parent();
position = first->getRepr()->position();
transform = first->getRepr()->attribute("transform");
// FIXME: merge styles of combined objects instead of using the first one's style
path_effect = first->getRepr()->attribute("inkscape:path-effect");
//c->transform(item->transform);
curve = c;
} else {
c->transform(item->getRelativeTransform(first));
curve->append(c, false);
c->unref();
// reduce position only if the same parent
if (item->getRepr()->parent() == parent) {
position--;
}
// delete the object for real, so that its clones can take appropriate action
item->deleteObject();
}
}
if (did) {
Inkscape::XML::Document *xml_doc = doc->getReprDoc();
Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
Inkscape::copy_object_properties(repr, first->getRepr());
// delete the topmost.
first->deleteObject(false);
// restore id, transform, path effect, and style
if (transform) {
repr->setAttribute("transform", transform);
}
repr->setAttribute("inkscape:path-effect", path_effect);
// set path data corresponding to new curve
gchar *dstring = sp_svg_write_path(curve->get_pathvector());
curve->unref();
if (path_effect) {
repr->setAttribute("inkscape:original-d", dstring);
} else {
repr->setAttribute("d", dstring);
}
g_free(dstring);
// add the new group to the parent of the topmost
// move to the position of the topmost, reduced by the number of deleted items
parent->addChildAtPos(repr, position > 0 ? position : 0);
if ( !skip_undo ) {
DocumentUndo::done(doc, SP_VERB_SELECTION_COMBINE,
_("Combine"));
}
set(repr);
Inkscape::GC::release(repr);
} else {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
}
if(desktop())
desktop()->clearWaitingCursor();
}
void
ObjectSet::breakApart(bool skip_undo)
{
if (isEmpty()) {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
return;
}
if(desktop()){
desktop()->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
// set "busy" cursor
desktop()->setWaitingCursor();
// disable redrawing during the break-apart operation for remarkable speedup for large paths
desktop()->getCanvas()->_drawing_disabled = true;
}
bool did = false;
std::vector<SPItem*> itemlist(items().begin(), items().end());
for (auto item : itemlist){
SPPath *path = dynamic_cast<SPPath *>(item);
if (!path) {
continue;
}
SPCurve *curve = path->getCurveForEdit();
if (curve == nullptr) {
continue;
}
did = true;
Inkscape::XML::Node *parent = item->getRepr()->parent();
gint pos = item->getRepr()->position();
char const *id = item->getRepr()->attribute("id");
// XML Tree being used directly here while it shouldn't be...
gchar *style = g_strdup(item->getRepr()->attribute("style"));
// XML Tree being used directly here while it shouldn't be...
gchar *path_effect = g_strdup(item->getRepr()->attribute("inkscape:path-effect"));
Geom::Affine transform = path->transform;
// it's going to resurrect as one of the pieces, so we delete without advertisement
item->deleteObject(false);
std::list<SPCurve *> list = curve->split();
curve->unref();
std::vector<Inkscape::XML::Node*> reprs;
for (auto curve:list) {
Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
repr->setAttribute("style", style);
repr->setAttribute("inkscape:path-effect", path_effect);
gchar *str = sp_svg_write_path(curve->get_pathvector());
if (path_effect)
repr->setAttribute("inkscape:original-d", str);
else
repr->setAttribute("d", str);
str = sp_svg_transform_write(transform);
repr->setAttribute("transform", str);
g_free(str);
// add the new repr to the parent
// move to the saved position
parent->addChildAtPos(repr, pos);
// if it's the first one, restore id
if (curve == *(list.begin()))
repr->setAttribute("id", id);
reprs.push_back(repr);
Inkscape::GC::release(repr);
}
setReprList(reprs);
g_free(style);
g_free(path_effect);
}
if (desktop()) {
desktop()->getCanvas()->_drawing_disabled = false;
desktop()->clearWaitingCursor();
}
if (did) {
if ( !skip_undo ) {
DocumentUndo::done(document(), SP_VERB_SELECTION_BREAK_APART,
_("Break apart"));
}
} else {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
}
}
void ObjectSet::toCurves(bool skip_undo)
{
if (isEmpty()) {
if (desktop())
desktop()->getMessageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
return;
}
bool did = false;
if (desktop()) {
desktop()->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
// set "busy" cursor
desktop()->setWaitingCursor();
}
unlinkRecursive(true);
std::vector<SPItem*> selected(items().begin(), items().end());
std::vector<Inkscape::XML::Node*> to_select;
std::vector<SPItem*> items(selected);
did = sp_item_list_to_curves(items, selected, to_select);
if (did) {
setReprList(to_select);
addList(selected);
}
if (desktop()) {
desktop()->clearWaitingCursor();
}
if (did&& !skip_undo) {
DocumentUndo::done(document(), SP_VERB_OBJECT_TO_CURVE,
_("Object to path"));
} else {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
return;
}
}
/** Converts the selected items to LPEItems if they are not already so; e.g. SPRects) */
void ObjectSet::toLPEItems()
{
if (isEmpty()) {
return;
}
unlinkRecursive(true);
std::vector<SPItem*> selected(items().begin(), items().end());
std::vector<Inkscape::XML::Node*> to_select;
clear();
std::vector<SPItem*> items(selected);
sp_item_list_to_curves(items, selected, to_select, true);
setReprList(to_select);
addList(selected);
}
bool
sp_item_list_to_curves(const std::vector<SPItem*> &items, std::vector<SPItem*>& selected, std::vector<Inkscape::XML::Node*> &to_select, bool skip_all_lpeitems)
{
bool did = false;
for (auto item : items){
g_assert(item != nullptr);
SPDocument *document = item->document;
SPGroup *group = dynamic_cast<SPGroup *>(item);
if ( skip_all_lpeitems &&
dynamic_cast<SPLPEItem *>(item) &&
!group ) // also convert objects in an SPGroup when skip_all_lpeitems is set.
{
continue;
}
SPBox3D *box = dynamic_cast<SPBox3D *>(item);
if (box) {
// convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
Inkscape::XML::Node *repr = box3d_convert_to_group(box)->getRepr();
if (repr) {
to_select.insert(to_select.begin(),repr);
did = true;
selected.erase(remove(selected.begin(), selected.end(), item), selected.end());
}
continue;
}
// remember id
char const *id = item->getRepr()->attribute("id");
SPLPEItem *lpeitem = dynamic_cast<SPLPEItem *>(item);
if (lpeitem) {
lpeitem->removeAllPathEffects(true);
SPObject *elemref = document->getObjectById(id);
if (elemref != item) {
selected.erase(remove(selected.begin(), selected.end(), item), selected.end());
if (elemref) {
//If the LPE item is a shape is converted to a path so we need to reupdate the item
item = dynamic_cast<SPItem *>(elemref);
selected.push_back(item);
did = true;
}
}
}
SPPath *path = dynamic_cast<SPPath *>(item);
if (path) {
// remove connector attributes
if (item->getAttribute("inkscape:connector-type") != nullptr) {
item->removeAttribute("inkscape:connection-start");
item->removeAttribute("inkscape:connection-end");
item->removeAttribute("inkscape:connector-type");
item->removeAttribute("inkscape:connector-curvature");
did = true;
}
continue; // already a path, and no path effect
}
if (group) {
std::vector<SPItem*> item_list = sp_item_group_item_list(group);
std::vector<Inkscape::XML::Node*> item_to_select;
std::vector<SPItem*> item_selected;
if (sp_item_list_to_curves(item_list, item_selected, item_to_select))
did = true;
continue;
}
Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
if (!repr)
continue;
did = true;
selected.erase(remove(selected.begin(), selected.end(), item), selected.end());
// remember the position of the item
gint pos = item->getRepr()->position();
// remember parent
Inkscape::XML::Node *parent = item->getRepr()->parent();
// remember class
char const *class_attr = item->getRepr()->attribute("class");
// It's going to resurrect, so we delete without notifying listeners.
item->deleteObject(false);
// restore id
repr->setAttribute("id", id);
// restore class
repr->setAttribute("class", class_attr);
// add the new repr to the parent
parent->addChildAtPos(repr, pos);
/* Buglet: We don't re-add the (new version of the) object to the selection of any other
* desktops where it was previously selected. */
to_select.insert(to_select.begin(),repr);
Inkscape::GC::release(repr);
}
return did;
}
Inkscape::XML::Node *
sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
{
if (!item)
return nullptr;
Inkscape::XML::Document *xml_doc = item->getRepr()->document();
if (dynamic_cast<SPText *>(item) || dynamic_cast<SPFlowtext *>(item)) {
// Special treatment for text: convert each glyph to separate path, then group the paths
Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
// Save original text for accessibility.
Glib::ustring original_text = sp_te_get_string_multiline( item,
te_get_layout(item)->begin(),
te_get_layout(item)->end() );
if( original_text.size() > 0 ) {
g_repr->setAttributeOrRemoveIfEmpty("aria-label", original_text );
}
g_repr->setAttribute("transform", item->getRepr()->attribute("transform"));
Inkscape::copy_object_properties(g_repr, item->getRepr());
/* Whole text's style */
Glib::ustring style_str =
item->style->write( SP_STYLE_FLAG_IFDIFF, SP_STYLE_SRC_UNSET, item->parent ? item->parent->style : nullptr); // TODO investigate possibility
g_repr->setAttributeOrRemoveIfEmpty("style", style_str);
Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin();
do {
Inkscape::Text::Layout::iterator iter_next = iter;
iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
if (iter == iter_next)
break;
/* This glyph's style */
SPObject *pos_obj = nullptr;
te_get_layout(item)->getSourceOfCharacter(iter, &pos_obj);
if (!pos_obj) // no source for glyph, abort
break;
while (dynamic_cast<SPString const *>(pos_obj) && pos_obj->parent) {
pos_obj = pos_obj->parent; // SPStrings don't have style
}
Glib::ustring style_str =
pos_obj->style->write( SP_STYLE_FLAG_IFDIFF, SP_STYLE_SRC_UNSET, pos_obj->parent ? pos_obj->parent->style : nullptr); // TODO investigate possibility
// get path from iter to iter_next:
SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
iter = iter_next; // shift to next glyph
if (!curve) { // error converting this glyph
continue;
}
if (curve->is_empty()) { // whitespace glyph?
curve->unref();
continue;
}
Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
gchar *def_str = sp_svg_write_path(curve->get_pathvector());
p_repr->setAttribute("d", def_str);
g_free(def_str);
curve->unref();
p_repr->setAttributeOrRemoveIfEmpty("style", style_str);
g_repr->appendChild(p_repr);
Inkscape::GC::release(p_repr);
if (iter == te_get_layout(item)->end())
break;
} while (true);
return g_repr;
}
SPCurve *curve = nullptr;
{
SPShape *shape = dynamic_cast<SPShape *>(item);
if (shape) {
curve = shape->getCurveForEdit();
}
}
if (!curve)
return nullptr;
// Prevent empty paths from being added to the document
// otherwise we end up with zomby markup in the SVG file
if(curve->is_empty())
{
curve->unref();
return nullptr;
}
Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
Inkscape::copy_object_properties(repr, item->getRepr());
/* Transformation */
repr->setAttribute("transform", item->getRepr()->attribute("transform"));
/* Style */
Glib::ustring style_str =
item->style->write( SP_STYLE_FLAG_IFDIFF, SP_STYLE_SRC_UNSET, item->parent ? item->parent->style : nullptr); // TODO investigate possibility
repr->setAttributeOrRemoveIfEmpty("style", style_str);
/* Definition */
gchar *def_str = sp_svg_write_path(curve->get_pathvector());
repr->setAttribute("d", def_str);
g_free(def_str);
curve->unref();
return repr;
}
void
ObjectSet::pathReverse()
{
if (isEmpty()) {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
return;
}
// set "busy" cursor
if(desktop()){
desktop()->setWaitingCursor();
desktop()->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
}
bool did = false;
for (auto i = items().begin(); i != items().end(); ++i){
SPPath *path = dynamic_cast<SPPath *>(*i);
if (!path) {
continue;
}
did = true;
SPCurve *rcurve = path->getCurveForEdit(true)->create_reverse();
gchar *str = sp_svg_write_path(rcurve->get_pathvector());
if ( path->hasPathEffectRecursive() ) {
path->setAttribute("inkscape:original-d", str);
} else {
path->setAttribute("d", str);
}
g_free(str);
rcurve->unref();
// reverse nodetypes order (Bug #179866)
gchar *nodetypes = g_strdup(path->getRepr()->attribute("sodipodi:nodetypes"));
if ( nodetypes ) {
path->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
g_free(nodetypes);
}
}
if(desktop())
desktop()->clearWaitingCursor();
if (did) {
DocumentUndo::done(document(), SP_VERB_SELECTION_REVERSE,
_("Reverse path"));
} else {
if(desktop())
desktop()->getMessageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
}
}
/**
* Copy generic attributes, like those from the "Object Properties" dialog,
* but also style and transformation center.
*
* @param dest XML node to copy attributes to
* @param src XML node to copy attributes from
*/
static void ink_copy_generic_attributes( //
Inkscape::XML::Node *dest, //
Inkscape::XML::Node const *src)
{
static char const *const keys[] = {
// core
"id",
// clip & mask
"clip-path",
"mask",
// style
"style",
// inkscape
"inkscape:highlight-color",
"inkscape:label",
"inkscape:transform-center-x",
"inkscape:transform-center-y",
// interactivity
"onclick",
"onmouseover",
"onmouseout",
"onmousedown",
"onmouseup",
"onmousemove",
"onfocusin",
"onfocusout",
"onload",
};
for (auto *key : keys) {
auto *value = src->attribute(key);
if (value) {
dest->setAttribute(key, value);
}
}
}
/**
* Copy generic child elements, like those from the "Object Properties" dialog
* (title and description) but also XML comments.
*
* Does not check if children of the same type already exist in dest.
*
* @param dest XML node to copy children to
* @param src XML node to copy children from
*/
static void ink_copy_generic_children( //
Inkscape::XML::Node *dest, //
Inkscape::XML::Node const *src)
{
static std::set<std::string> const names{
// descriptive elements
"svg:title",
"svg:desc",
};
for (const auto *child = src->firstChild(); child != nullptr; child = child->next()) {
// check if this child should be copied
if (!(child->type() == Inkscape::XML::COMMENT_NODE || //
(child->name() && names.count(child->name())))) {
continue;
}
auto dchild = child->duplicate(dest->document());
dest->appendChild(dchild);
dchild->release();
}
}
/**
* Copy generic object properties, like:
* - id
* - label
* - title
* - description
* - style
* - clip
* - mask
* - transformation center
* - highlight color
* - interactivity (event attributes)
*
* @param dest XML node to copy to
* @param src XML node to copy from
*/
void Inkscape::copy_object_properties( //
Inkscape::XML::Node *dest, //
Inkscape::XML::Node const *src)
{
ink_copy_generic_attributes(dest, src);
ink_copy_generic_children(dest, src);
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|