1
#define __SP_LIVAROT_C__
6
* Created by fred on Fri Dec 05 2003.
7
* tweaked endlessly by bulia byak <buliabyak@users.sf.net>
13
* contains lots of stitched pieces of path-chemistry.c
24
#include "sp-item-group.h"
28
#include "message-stack.h"
29
#include "selection.h"
30
#include "desktop-handles.h"
32
#include "display/canvas-bpath.h"
33
#include "display/curve.h"
34
#include <glibmm/i18n.h>
35
#include "prefs-utils.h"
37
#include "libnr/n-art-bpath.h"
38
#include "libnr/nr-path.h"
40
#include "xml/repr-sorting.h"
42
#include "livarot/Path.h"
43
#include "livarot/Shape.h"
45
Path *Path_for_item(SPItem *item, bool doTransformation, bool transformFull = true);
46
bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
48
void sp_selected_path_boolop(bool_op bop);
49
void sp_selected_path_do_offset(bool expand, double prefOffset);
50
void sp_selected_path_create_offset_object(int expand, bool updating);
53
sp_selected_path_union()
55
sp_selected_path_boolop(bool_op_union);
59
sp_selected_path_intersect()
61
sp_selected_path_boolop(bool_op_inters);
65
sp_selected_path_diff()
67
sp_selected_path_boolop(bool_op_diff);
71
sp_selected_path_symdiff()
73
sp_selected_path_boolop(bool_op_symdiff);
76
sp_selected_path_cut()
78
sp_selected_path_boolop(bool_op_cut);
81
sp_selected_path_slice()
83
sp_selected_path_boolop(bool_op_slice);
88
// take the source paths from the file, do the operation, delete the originals and add the results
90
sp_selected_path_boolop(bool_op bop)
92
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
94
Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
96
GSList *il = (GSList *) selection->itemList();
98
if (g_slist_length(il) < 2) {
99
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
103
if (g_slist_length(il) > 2) {
104
if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
105
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
110
// reverseOrderForOp marks whether the order of the list is the top->down order
111
// it's only used when there are 2 objects, and for operations who need to know the
112
// topmost object (differences, cuts)
113
bool reverseOrderForOp = false;
115
// mettre les elements de la liste dans l'ordre pour ces operations
116
if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
117
// check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
118
Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
119
Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
121
if (a == NULL || b == NULL) {
122
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Unable to determine the <b>z-order</b> of the objects selected for difference, XOR, division, or path cut."));
127
// a is the parent of b, already in the proper order
128
} else if (Ancetre(b, a)) {
130
reverseOrderForOp = true;
133
// objects are not in parent/child relationship;
134
// find their lowest common ancestor
135
Inkscape::XML::Node *dad = LCA(a, b);
137
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Unable to determine the <b>z-order</b> of the objects selected for difference, XOR, division, or path cut."));
141
// find the children of the LCA that lead from it to the a and b
142
Inkscape::XML::Node *as = AncetreFils(a, dad);
143
Inkscape::XML::Node *bs = AncetreFils(b, dad);
145
// find out which comes first
146
for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
148
/* a first, so reverse. */
149
reverseOrderForOp = true;
158
il = g_slist_copy(il);
160
// first check if all the input objects have shapes
161
// otherwise bail out
162
for (GSList *l = il; l != NULL; l = l->next)
164
SPItem *item = SP_ITEM(l->data);
165
if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
167
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
173
// extract the livarot Paths from the source objects
174
// also get the winding rule specified in the style
175
int nbOriginaux = g_slist_length(il);
176
Path *originaux[nbOriginaux];
177
FillRule origWind[nbOriginaux];
181
for (GSList *l = il; l != NULL; l = l->next)
183
SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
184
gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
185
if (val && strcmp(val, "nonzero") == 0) {
186
origWind[curOrig]= fill_nonZero;
187
} else if (val && strcmp(val, "evenodd") == 0) {
188
origWind[curOrig]= fill_oddEven;
190
origWind[curOrig]= fill_nonZero;
193
originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
194
if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
196
for (int i = curOrig; i >= 0; i--) delete originaux[i];
204
// note that the selection list keeps its order
205
if ( reverseOrderForOp ) {
206
Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
207
FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
211
// some temporary instances, first
212
Shape *theShapeA = new Shape;
213
Shape *theShapeB = new Shape;
214
Shape *theShape = new Shape;
215
Path *res = new Path;
216
res->SetBackData(false);
217
Path::cut_position *toCut=NULL;
220
if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
222
// get the polygons of each path, with the winding rule specified, and apply the operation iteratively
223
originaux[0]->ConvertWithBackData(0.001);
225
originaux[0]->Fill(theShape, 0);
227
theShapeA->ConvertToShape(theShape, origWind[0]);
230
for (GSList *l = il->next; l != NULL; l = l->next) {
231
originaux[curOrig]->ConvertWithBackData(0.001);
233
originaux[curOrig]->Fill(theShape, curOrig);
235
theShapeB->ConvertToShape(theShape, origWind[curOrig]);
237
// les elements arrivent en ordre inverse dans la liste
238
theShape->Booleen(theShapeB, theShapeA, bop);
241
Shape *swap = theShape;
242
theShape = theShapeA;
249
Shape *swap = theShape;
250
theShape = theShapeA;
254
} else if ( bop == bool_op_cut ) {
255
// cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
256
// technically, the cut path is not necessarily a polygon (thus has no winding rule)
257
// it is just uncrossed, and cleaned from duplicate edges and points
258
// then it's fed to Booleen() which will uncross it against the other path
259
// then comes the trick: each edge of the cut path is duplicated (one in each direction),
260
// thus making a polygon. the weight of the edges of the cut are all 0, but
261
// the Booleen need to invert the ones inside the source polygon (for the subsequent
264
// the cut path needs to have the highest pathID in the back data
265
// that's how the Booleen() function knows it's an edge of the cut
267
// FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
268
// ConvertWithBackData parameter below simply increases the number of nodes, so for now I
269
// left it at 1.0. Investigate replacing this by a combination of difference and
270
// intersection of the same two paths. -- bb
272
Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
273
int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
275
originaux[0]->ConvertWithBackData(1.0);
277
originaux[0]->Fill(theShape, 0);
279
theShapeA->ConvertToShape(theShape, origWind[0]);
281
originaux[1]->ConvertWithBackData(1.0);
283
originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
285
theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
287
// les elements arrivent en ordre inverse dans la liste
288
theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
290
} else if ( bop == bool_op_slice ) {
291
// slice is not really a boolean operation
292
// you just put the 2 shapes in a single polygon, uncross it
293
// the points where the degree is > 2 are intersections
294
// just check it's an intersection on the path you want to cut, and keep it
295
// the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
296
// make new subpath at each one of these positions
297
// inversion pour l'opration
299
Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
300
int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
302
originaux[0]->ConvertWithBackData(1.0);
304
originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
306
originaux[1]->ConvertWithBackData(1.0);
308
originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
310
theShape->ConvertToShape(theShapeA, fill_justDont);
312
if ( theShape->hasBackData() ) {
313
// should always be the case, but ya never know
315
for (int i = 0; i < theShape->numberOfPoints(); i++) {
316
if ( theShape->getPoint(i).totalDegree() > 2 ) {
317
// possibly an intersection
318
// we need to check that at least one edge from the source path is incident to it
319
// before we declare it's an intersection
320
int cb = theShape->getPoint(i).incidentEdge[FIRST];
325
while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
326
if ( theShape->ebData[cb].pathID == 0 ) {
327
// the source has an edge incident to the point, get its position on the path
328
piece=theShape->ebData[cb].pieceID;
329
if ( theShape->getEdge(cb).st == i ) {
330
t=theShape->ebData[cb].tSt;
332
t=theShape->ebData[cb].tEn;
336
if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
337
cb=theShape->NextAt(i, cb);
339
if ( nbOrig > 0 && nbOther > 0 ) {
340
// point incident to both path and cut: an intersection
341
// note that you only keep one position on the source; you could have degenerate
342
// cases where the source crosses itself at this point, and you wouyld miss an intersection
343
toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
344
toCut[nbToCut].piece=piece;
352
// i think it's useless now
353
int i = theShape->numberOfEdges() - 1;
355
if ( theShape->ebData[i].pathID == 1 ) {
356
theShape->SubEdge(i);
367
// pour compenser le swap juste avant
368
if ( bop == bool_op_slice ) {
369
// theShape->ConvertToForme(res, nbOriginaux, originaux, true);
370
// res->ConvertForcedToMoveTo();
371
res->Copy(originaux[0]);
372
res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
374
} else if ( bop == bool_op_cut ) {
375
// il faut appeler pour desallouer PointData (pas vital, mais bon)
376
// the Booleen() function did not deallocated the point_data array in theShape, because this
377
// function needs it.
378
// this function uses the point_data to get the winding number of each path (ie: is a hole or not)
379
// for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
380
theShape->ConvertToFormeNested(res, nbOriginaux, originaux, 1, nbNest, nesting, conts);
382
theShape->ConvertToForme(res, nbOriginaux, originaux);
388
for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
390
if (res->descr_cmd.size() <= 1)
392
// only one command, presumably a moveto: it isn't a path
393
for (GSList *l = il; l != NULL; l = l->next)
395
SP_OBJECT(l->data)->deleteObject();
397
sp_document_done(SP_DT_DOCUMENT(desktop));
405
// remember important aspects of the source path, to be restored
406
Inkscape::XML::Node *repr_source;
407
if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
408
if (reverseOrderForOp) {
409
repr_source = SP_OBJECT_REPR(il->data);
411
repr_source = SP_OBJECT_REPR(il->next->data);
414
// find out the bottom object
415
GSList *sorted = g_slist_copy((GSList *) selection->reprList());
417
sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
418
repr_source = ((Inkscape::XML::Node *) sorted->data);
419
g_slist_free(sorted);
421
gint pos = repr_source->position();
422
Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
423
char const *id = repr_source->attribute("id");
424
char const *style = repr_source->attribute("style");
427
// remove source paths
429
for (GSList *l = il; l != NULL; l = l->next) {
430
// if this is the bottommost object,
431
if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
432
// delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
433
SP_OBJECT(l->data)->deleteObject(false);
435
// delete the object for real, so that its clones can take appropriate action
436
SP_OBJECT(l->data)->deleteObject();
441
// premultiply by the inverse of parent's repr
442
SPItem *parent_item = SP_ITEM(SP_DT_DOCUMENT(desktop)->getObjectByRepr(parent));
443
NR::Matrix local = sp_item_i2doc_affine(parent_item);
445
gchar *transform = NULL;
446
if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
447
transform = affinestr;
450
// now that we have the result, add it on the canvas
451
if ( bop == bool_op_cut || bop == bool_op_slice ) {
454
if ( bop == bool_op_slice ) {
455
// there are moveto's at each intersection, but it's still one unique path
456
// so break it down and add each subpath independently
457
// we could call break_apart to do this, but while we have the description...
458
resPath=res->SubPaths(nbRP, false);
460
// cut operation is a bit wicked: you need to keep holes
461
// that's why you needed the nesting
462
// ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
463
// to get the path for each part of the polygon. that's why you need the nesting info:
464
// to know in wich subpath to add a subpath
465
resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
468
if ( conts ) free(conts);
469
if ( nesting ) free(nesting);
472
// add all the pieces resulting from cut or slice
473
for (int i=0;i<nbRP;i++) {
474
gchar *d = resPath[i]->svg_dump_path();
476
Inkscape::XML::Node *repr = sp_repr_new("svg:path");
477
repr->setAttribute("style", style);
478
repr->setAttribute("d", d);
481
// for slice, remove fill
482
if (bop == bool_op_slice) {
485
css = sp_repr_css_attr_new();
486
sp_repr_css_set_property(css, "fill", "none");
488
sp_repr_css_change(repr, css, "style");
490
sp_repr_css_attr_unref(css);
493
// we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
494
// this means it's basically random which of the pieces inherits the original's id and clones
495
// a better algorithm might figure out e.g. the biggest piece
496
repr->setAttribute("id", id);
498
repr->setAttribute("transform", transform);
500
// add the new repr to the parent
501
parent->appendChild(repr);
503
// move to the saved position
504
repr->setPosition(pos > 0 ? pos : 0);
506
selection->add(repr);
507
Inkscape::GC::release(repr);
511
if ( resPath ) free(resPath);
514
gchar *d = res->svg_dump_path();
516
Inkscape::XML::Node *repr = sp_repr_new("svg:path");
517
repr->setAttribute("style", style);
519
repr->setAttribute("d", d);
522
repr->setAttribute("transform", transform);
524
repr->setAttribute("id", id);
525
parent->appendChild(repr);
526
repr->setPosition(pos > 0 ? pos : 0);
528
selection->add(repr);
529
Inkscape::GC::release(repr);
532
sp_document_done(SP_DT_DOCUMENT(desktop));
539
sp_selected_path_outline()
541
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
543
Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
545
if (selection->isEmpty()) {
546
// TRANSLATORS: "to outline" means "to convert stroke to path"
547
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to outline."));
553
for (GSList *items = g_slist_copy((GSList *) selection->itemList());
555
items = items->next) {
557
SPItem *item = (SPItem *) items->data;
559
if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
562
SPCurve *curve = NULL;
563
if (SP_IS_SHAPE(item)) {
564
curve = sp_shape_get_curve(SP_SHAPE(item));
568
if (SP_IS_TEXT(item)) {
569
curve = SP_TEXT(item)->getNormalizedBpath();
574
{ // pas de stroke pas de chocolat
575
SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
576
gchar const *val = sp_repr_css_property(css, "stroke", NULL);
578
if (val == NULL || strcmp(val, "none") == 0) {
579
sp_curve_unref(curve);
584
// remember old stroke style, to be set on fill
587
SPCSSAttr *ocss = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
588
gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
589
gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
591
ncss = sp_repr_css_attr_new();
593
sp_repr_css_set_property(ncss, "stroke", "none");
594
sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
595
sp_repr_css_set_property(ncss, "fill", val);
597
sp_repr_css_set_property(ncss, "fill-opacity", opac);
599
sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
603
NR::Matrix const transform(item->transform);
604
gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
606
float o_width, o_miter;
611
SPStyle *i_style = SP_OBJECT(item)->style;
612
int jointype, captype;
614
jointype = i_style->stroke_linejoin.computed;
615
captype = i_style->stroke_linecap.computed;
616
o_width = i_style->stroke_width.computed;
619
case SP_STROKE_LINEJOIN_MITER:
620
o_join = join_pointy;
622
case SP_STROKE_LINEJOIN_ROUND:
626
o_join = join_straight;
631
case SP_STROKE_LINECAP_SQUARE:
632
o_butt = butt_square;
634
case SP_STROKE_LINECAP_ROUND:
638
o_butt = butt_straight;
644
o_miter = i_style->stroke_miterlimit.value * o_width;
647
Path *orig = Path_for_item(item, false);
650
sp_curve_unref(curve);
654
Path *res = new Path;
655
res->SetBackData(false);
659
orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
661
orig->Coalesce(0.5 * o_width);
663
Shape *theShape = new Shape;
664
Shape *theRes = new Shape;
666
res->ConvertWithBackData(1.0);
667
res->Fill(theShape, 0);
668
theRes->ConvertToShape(theShape, fill_positive);
672
theRes->ConvertToForme(orig, 1, originaux);
678
if (orig->descr_cmd.size() <= 1) {
679
// ca a merd, ou bien le resultat est vide
688
sp_curve_unref(curve);
689
// remember the position of the item
690
gint pos = SP_OBJECT_REPR(item)->position();
692
Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
694
char const *id = SP_OBJECT_REPR(item)->attribute("id");
696
selection->remove(item);
697
SP_OBJECT(item)->deleteObject(false);
699
if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
701
Inkscape::XML::Node *repr = sp_repr_new("svg:path");
704
repr->setAttribute("style", style);
706
// set old stroke style on fill
707
sp_repr_css_change(repr, ncss, "style");
709
sp_repr_css_attr_unref(ncss);
711
gchar *str = orig->svg_dump_path();
712
repr->setAttribute("d", str);
715
// add the new repr to the parent
716
parent->appendChild(repr);
718
// move to the saved position
719
repr->setPosition(pos > 0 ? pos : 0);
721
repr->setAttribute("id", id);
723
SPItem *newitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr);
724
sp_item_write_transform(newitem, repr, transform);
726
selection->add(repr);
728
Inkscape::GC::release(repr);
738
sp_document_done(SP_DT_DOCUMENT(desktop));
740
// TRANSLATORS: "to outline" means "to convert stroke to path"
741
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> to outline in the selection."));
748
sp_selected_path_offset()
750
double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
752
sp_selected_path_do_offset(true, prefOffset);
755
sp_selected_path_inset()
757
double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
759
sp_selected_path_do_offset(false, prefOffset);
763
sp_selected_path_offset_screen(double pixels)
765
sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
769
sp_selected_path_inset_screen(double pixels)
771
sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
775
void sp_selected_path_create_offset_object_zero()
777
sp_selected_path_create_offset_object(0, false);
780
void sp_selected_path_create_offset()
782
sp_selected_path_create_offset_object(1, false);
784
void sp_selected_path_create_inset()
786
sp_selected_path_create_offset_object(-1, false);
789
void sp_selected_path_create_updating_offset_object_zero()
791
sp_selected_path_create_offset_object(0, true);
794
void sp_selected_path_create_updating_offset()
796
sp_selected_path_create_offset_object(1, true);
798
void sp_selected_path_create_updating_inset()
800
sp_selected_path_create_offset_object(-1, true);
804
sp_selected_path_create_offset_object(int expand, bool updating)
806
Inkscape::Selection *selection;
807
Inkscape::XML::Node *repr;
812
float o_width, o_miter;
818
desktop = SP_ACTIVE_DESKTOP;
820
selection = SP_DT_SELECTION(desktop);
822
item = selection->singleItem();
824
if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
825
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
828
if (SP_IS_SHAPE(item))
830
curve = sp_shape_get_curve(SP_SHAPE(item));
834
if (SP_IS_TEXT(item))
836
curve = SP_TEXT(item)->getNormalizedBpath();
841
NR::Matrix const transform(item->transform);
843
sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
845
style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
847
// remember the position of the item
848
gint pos = SP_OBJECT_REPR(item)->position();
850
Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
853
SPStyle *i_style = SP_OBJECT(item)->style;
854
int jointype, captype;
856
jointype = i_style->stroke_linejoin.value;
857
captype = i_style->stroke_linecap.value;
858
o_width = i_style->stroke_width.computed;
859
if (jointype == SP_STROKE_LINEJOIN_MITER)
861
o_join = join_pointy;
863
else if (jointype == SP_STROKE_LINEJOIN_ROUND)
869
o_join = join_straight;
871
if (captype == SP_STROKE_LINECAP_SQUARE)
873
o_butt = butt_square;
875
else if (captype == SP_STROKE_LINECAP_ROUND)
881
o_butt = butt_straight;
885
double prefOffset = 1.0;
886
prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
887
o_width = prefOffset;
892
o_miter = i_style->stroke_miterlimit.value * o_width;
895
Path *orig = Path_for_item(item, true, false);
899
sp_curve_unref(curve);
903
Path *res = new Path;
904
res->SetBackData(false);
907
Shape *theShape = new Shape;
908
Shape *theRes = new Shape;
910
orig->ConvertWithBackData(1.0);
911
orig->Fill(theShape, 0);
913
SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
914
gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
915
if (val && strcmp(val, "nonzero") == 0)
917
theRes->ConvertToShape(theShape, fill_nonZero);
919
else if (val && strcmp(val, "evenodd") == 0)
921
theRes->ConvertToShape(theShape, fill_oddEven);
925
theRes->ConvertToShape(theShape, fill_nonZero);
930
theRes->ConvertToForme(res, 1, originaux);
936
sp_curve_unref(curve);
938
if (res->descr_cmd.size() <= 1)
940
// pas vraiment de points sur le resultat
941
// donc il ne reste rien
942
sp_document_done(SP_DT_DOCUMENT(desktop));
956
repr = sp_repr_new("svg:path");
957
repr->setAttribute("sodipodi:type", "inkscape:offset");
958
sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
964
str = res->svg_dump_path();
965
repr->setAttribute("inkscape:original", str);
969
char const *id = SP_OBJECT(item)->repr->attribute("id");
970
char const *uri = g_strdup_printf("#%s", id);
971
repr->setAttribute("xlink:href", uri);
972
g_free((void *) uri);
974
repr->setAttribute("inkscape:href", NULL);
977
repr->setAttribute("style", style);
979
// add the new repr to the parent
980
parent->appendChild(repr);
982
// move to the saved position
983
repr->setPosition(pos > 0 ? pos : 0);
985
SPItem *nitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr);
988
// on conserve l'original
989
// we reapply the transform to the original (offset will feel it)
990
sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
992
// delete original, apply the transform to the offset
993
SP_OBJECT(item)->deleteObject(false);
994
sp_item_write_transform(nitem, repr, transform);
997
// The object just created from a temporary repr is only a seed.
998
// We need to invoke its write which will update its real repr (in particular adding d=)
999
SP_OBJECT(nitem)->updateRepr();
1001
Inkscape::GC::release(repr);
1003
selection->set(nitem);
1006
sp_document_done(SP_DT_DOCUMENT(desktop));
1026
sp_selected_path_do_offset(bool expand, double prefOffset)
1028
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1030
Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1032
if (selection->isEmpty()) {
1033
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1039
for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1041
items = items->next) {
1043
SPItem *item = (SPItem *) items->data;
1045
if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1048
SPCurve *curve = NULL;
1049
if (SP_IS_SHAPE(item)) {
1050
curve = sp_shape_get_curve(SP_SHAPE(item));
1054
if (SP_IS_TEXT(item)) {
1055
curve = SP_TEXT(item)->getNormalizedBpath();
1060
NR::Matrix const transform(item->transform);
1062
sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1064
gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1066
float o_width, o_miter;
1071
SPStyle *i_style = SP_OBJECT(item)->style;
1072
int jointype, captype;
1074
jointype = i_style->stroke_linejoin.value;
1075
captype = i_style->stroke_linecap.value;
1076
o_width = i_style->stroke_width.computed;
1079
case SP_STROKE_LINEJOIN_MITER:
1080
o_join = join_pointy;
1082
case SP_STROKE_LINEJOIN_ROUND:
1083
o_join = join_round;
1086
o_join = join_straight;
1091
case SP_STROKE_LINECAP_SQUARE:
1092
o_butt = butt_square;
1094
case SP_STROKE_LINECAP_ROUND:
1095
o_butt = butt_round;
1098
o_butt = butt_straight;
1102
o_width = prefOffset;
1106
o_miter = i_style->stroke_miterlimit.value * o_width;
1109
Path *orig = Path_for_item(item, false);
1112
sp_curve_unref(curve);
1116
Path *res = new Path;
1117
res->SetBackData(false);
1120
Shape *theShape = new Shape;
1121
Shape *theRes = new Shape;
1123
orig->ConvertWithBackData(0.03);
1124
orig->Fill(theShape, 0);
1126
SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1127
gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1128
if (val && strcmp(val, "nonzero") == 0)
1130
theRes->ConvertToShape(theShape, fill_nonZero);
1132
else if (val && strcmp(val, "evenodd") == 0)
1134
theRes->ConvertToShape(theShape, fill_oddEven);
1138
theRes->ConvertToShape(theShape, fill_nonZero);
1141
// et maintenant: offset
1143
/* Path *originaux[1];
1144
originaux[0] = orig;
1145
theRes->ConvertToForme(res, 1, originaux);
1148
res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1150
res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1153
orig->ConvertWithBackData(1.0);
1154
orig->Fill(theShape, 0);
1155
theRes->ConvertToShape(theShape, fill_positive);
1156
originaux[0] = orig;
1157
theRes->ConvertToForme(res, 1, originaux);
1159
if (o_width >= 0.5) {
1160
// res->Coalesce(1.0);
1161
res->ConvertEvenLines(1.0);
1164
// res->Coalesce(o_width);
1165
res->ConvertEvenLines(1.0*o_width);
1166
res->Simplify(1.0 * o_width);
1168
// methode par makeoffset
1172
theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1176
theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1178
theRes->ConvertToShape(theShape, fill_positive);
1181
theRes->ConvertToForme(res);
1185
res->ConvertEvenLines(1.0);
1190
res->ConvertEvenLines(1.0*o_width);
1191
res->Simplify(1.0 * o_width);
1200
sp_curve_unref(curve);
1201
// remember the position of the item
1202
gint pos = SP_OBJECT_REPR(item)->position();
1204
Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1206
char const *id = SP_OBJECT_REPR(item)->attribute("id");
1208
selection->remove(item);
1209
SP_OBJECT(item)->deleteObject(false);
1211
if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1217
Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1219
repr->setAttribute("style", style);
1221
gchar *str = res->svg_dump_path();
1222
repr->setAttribute("d", str);
1225
// add the new repr to the parent
1226
parent->appendChild(repr);
1228
// move to the saved position
1229
repr->setPosition(pos > 0 ? pos : 0);
1231
SPItem *newitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr);
1233
// reapply the transform
1234
sp_item_write_transform(newitem, repr, transform);
1236
repr->setAttribute("id", id);
1238
selection->add(repr);
1240
Inkscape::GC::release(repr);
1248
sp_document_done(SP_DT_DOCUMENT(desktop));
1250
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1257
//return true if we changed something, else false
1259
sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item,
1260
float threshold, bool justCoalesce,
1261
float angleLimit, bool breakableAngles,
1262
gdouble size, bool modifySelection)
1264
if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1267
//If this is a group, do the children instead
1268
if (SP_IS_GROUP(item)) {
1270
bool didSomething = false;
1272
for ( GSList *children = sp_item_group_item_list(SP_GROUP(item));
1273
children ; children = children->next) {
1275
SPItem *child = (SPItem *) children->data;
1276
didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce,
1277
angleLimit, breakableAngles, size, false);
1280
return didSomething;
1284
SPCurve *curve = NULL;
1286
if (SP_IS_SHAPE(item)) {
1287
curve = sp_shape_get_curve(SP_SHAPE(item));
1292
if (SP_IS_TEXT(item)) {
1293
curve = SP_TEXT(item)->getNormalizedBpath();
1298
// save the transform, to re-apply it after simplification
1299
NR::Matrix const transform(item->transform);
1302
reset the transform, effectively transforming the item by transform.inverse();
1303
this is necessary so that the item is transformed twice back and forth,
1304
allowing all compensations to cancel out regardless of the preferences
1306
sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1308
gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1310
Path *orig = Path_for_item(item, false);
1313
sp_curve_unref(curve);
1317
sp_curve_unref(curve);
1318
// remember the position of the item
1319
gint pos = SP_OBJECT_REPR(item)->position();
1321
Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1323
char const *id = SP_OBJECT_REPR(item)->attribute("id");
1325
//If a group was selected, to not change the selection list
1326
if (modifySelection)
1327
selection->remove(item);
1329
SP_OBJECT(item)->deleteObject(false);
1331
if ( justCoalesce ) {
1332
orig->Coalesce(threshold * size);
1334
orig->ConvertEvenLines(threshold * size);
1335
orig->Simplify(threshold * size);
1338
Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1340
repr->setAttribute("style", style);
1342
gchar *str = orig->svg_dump_path();
1343
repr->setAttribute("d", str);
1347
repr->setAttribute("id", id);
1349
// add the new repr to the parent
1350
parent->appendChild(repr);
1352
// move to the saved position
1353
repr->setPosition(pos > 0 ? pos : 0);
1355
SPItem *newitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr);
1357
// reapply the transform
1358
sp_item_write_transform(newitem, repr, transform);
1360
//If we are not in a selected group
1361
if (modifySelection)
1362
selection->add(repr);
1364
Inkscape::GC::release(repr);
1367
if (orig) delete orig;
1374
sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1375
float angleLimit, bool breakableAngles)
1377
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1379
Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1381
if (selection->isEmpty()) {
1382
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1383
_("Select <b>path(s)</b> to simplify."));
1387
// remember selection size
1388
NR::Rect bbox = selection->bounds();
1389
gdouble size = L2(bbox.dimensions());
1391
bool didSomething = false;
1393
//Loop through all of the items in the selection
1394
for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1395
items != NULL; items = items->next) {
1397
SPItem *item = (SPItem *) items->data;
1399
if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1402
didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1403
threshold, justCoalesce, angleLimit, breakableAngles, size, true);
1408
sp_document_done(SP_DT_DOCUMENT(desktop));
1410
desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1415
// globals for keeping track of accelerated simplify
1416
static double previousTime = 0.0;
1417
static gdouble simplifyMultiply = 1.0;
1420
sp_selected_path_simplify(void)
1422
gdouble simplifyThreshold =
1423
prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1424
bool simplifyJustCoalesce =
1425
(bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1427
//Get the current time
1428
GTimeVal currentTimeVal;
1429
g_get_current_time(¤tTimeVal);
1430
double currentTime = currentTimeVal.tv_sec * 1000000 +
1431
currentTimeVal.tv_usec;
1433
//Was the previous call to this function recent? (<0.5 sec)
1434
if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1436
// add to the threshold 1/2 of its original value
1437
simplifyMultiply += 0.5;
1438
simplifyThreshold *= simplifyMultiply;
1441
// reset to the default
1442
simplifyMultiply = 1;
1445
//remember time for next call
1446
previousTime = currentTime;
1448
//g_print("%g\n", simplify_threshold);
1450
//Make the actual call
1451
sp_selected_path_simplify_selection(simplifyThreshold,
1452
simplifyJustCoalesce, 0.0, false);
1457
// fonctions utilitaires
1460
Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1462
if (who == NULL || a == NULL)
1466
return Ancetre(sp_repr_parent(a), who);
1470
Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1477
if (SP_IS_SHAPE(item))
1479
curve = sp_shape_get_curve(SP_SHAPE(item));
1481
else if (SP_IS_TEXT(item))
1483
curve = SP_TEXT(item)->getNormalizedBpath();
1492
NArtBpath *bpath = curve->bpath;
1496
if ( doTransformation ) {
1498
bpath = nr_artpath_affine(curve->bpath, sp_item_i2doc_affine(item));
1500
bpath = nr_artpath_affine(curve->bpath, item->transform);
1501
sp_curve_unref(curve);
1507
Path *dest = new Path;
1508
dest->SetBackData(false);
1511
bool closed = false;
1515
for (i = 0; bpath[i].code != NR_END; i++) {
1516
switch (bpath[i].code) {
1518
lastX = bpath[i].x3;
1519
lastY = bpath[i].y3;
1521
NR::Point tmp(lastX, lastY);
1528
NR::Point tmp, tms, tme;
1531
tms[0]=3 * (bpath[i].x1 - lastX);
1532
tms[1]=3 * (bpath[i].y1 - lastY);
1533
tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1534
tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1535
dest->CubicTo(tmp,tms,tme);
1537
lastX = bpath[i].x3;
1538
lastY = bpath[i].y3;
1541
case NR_MOVETO_OPEN:
1545
closed = (bpath[i].code == NR_MOVETO);
1546
lastX = bpath[i].x3;
1547
lastY = bpath[i].y3;
1549
NR::Point tmp(lastX, lastY);
1561
if ( doTransformation ) {
1562
if ( bpath ) nr_free(bpath);
1564
sp_curve_unref(curve);
1569
NR::Maybe<Path::cut_position> get_nearest_position_on_Path(SPItem *item, NR::Point p)
1571
//Create and initialize a livarot Path
1572
Path *path = Path_for_item(item, true, true);
1574
return NR::Nothing();
1576
path->ConvertWithBackData(0.01);
1578
//get nearest position on path
1579
Path::cut_position pos = path->PointToCurvilignPosition(p);
1584
NR::Point get_point_on_Path(SPItem *item, int piece, double t)
1586
//Create and initialize a livarot Path
1587
Path *path = Path_for_item(item, true, true);
1588
path->ConvertWithBackData(0.01);
1590
//get nearest position on path
1592
path->PointAt(piece, t, p);
1601
c-file-style:"stroustrup"
1602
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1603
indent-tabs-mode:nil
1607
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :