Code

replace single toggle button with less confusing pick-alpha and set-alpha options
[inkscape.git] / src / splivarot.cpp
1 #define __SP_LIVAROT_C__
2 /*
3  *  splivarot.cpp
4  *  Inkscape
5  *
6  *  Created by fred on Fri Dec 05 2003.
7  *  tweaked endlessly by bulia byak <buliabyak@users.sf.net>
8  *  public domain
9  *
10  */
12 /*
13  * contains lots of stitched pieces of path-chemistry.c
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #include <vector>
21 #include <glib/gmem.h>
22 #include "xml/repr.h"
23 #include "svg/svg.h"
24 #include "sp-path.h"
25 #include "sp-shape.h"
26 #include "sp-marker.h"
27 #include "enums.h"
28 #include "sp-text.h"
29 #include "sp-item-group.h"
30 #include "style.h"
31 #include "inkscape.h"
32 #include "document.h"
33 #include "message-stack.h"
34 #include "selection.h"
35 #include "desktop-handles.h"
36 #include "desktop.h"
37 #include "display/canvas-bpath.h"
38 #include "display/curve.h"
39 #include <glibmm/i18n.h>
40 #include "prefs-utils.h"
42 #include "libnr/n-art-bpath.h"
43 #include "libnr/nr-path.h"
44 #include "xml/repr.h"
45 #include "xml/repr-sorting.h"
47 #include <libnr/nr-matrix-fns.h>
48 #include <libnr/nr-matrix-ops.h>
49 #include <libnr/nr-matrix-translate-ops.h>
50 #include <libnr/nr-scale-matrix-ops.h>
52 #include "livarot/Path.h"
53 #include "livarot/Shape.h"
55 #include "splivarot.h"
57 bool   Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
59 void sp_selected_path_boolop(bool_op bop);
60 void sp_selected_path_do_offset(bool expand, double prefOffset);
61 void sp_selected_path_create_offset_object(int expand, bool updating);
63 void
64 sp_selected_path_union()
65 {
66     sp_selected_path_boolop(bool_op_union);
67 }
69 void
70 sp_selected_path_intersect()
71 {
72     sp_selected_path_boolop(bool_op_inters);
73 }
75 void
76 sp_selected_path_diff()
77 {
78     sp_selected_path_boolop(bool_op_diff);
79 }
81 void
82 sp_selected_path_symdiff()
83 {
84     sp_selected_path_boolop(bool_op_symdiff);
85 }
86 void
87 sp_selected_path_cut()
88 {
89     sp_selected_path_boolop(bool_op_cut);
90 }
91 void
92 sp_selected_path_slice()
93 {
94     sp_selected_path_boolop(bool_op_slice);
95 }
98 // boolean operations
99 // take the source paths from the file, do the operation, delete the originals and add the results
100 void
101 sp_selected_path_boolop(bool_op bop)
103     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
105     Inkscape::Selection *selection = sp_desktop_selection(desktop);
107     GSList *il = (GSList *) selection->itemList();
109     if (g_slist_length(il) < 2) {
110         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
111         return;
112     }
114     if (g_slist_length(il) > 2) {
115         if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
116             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
117             return;
118         }
119     }
121     // reverseOrderForOp marks whether the order of the list is the top->down order
122     // it's only used when there are 2 objects, and for operations who need to know the
123     // topmost object (differences, cuts)
124     bool reverseOrderForOp = false;
126     // mettre les elements de la liste dans l'ordre pour ces operations
127     if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
128         // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
129         Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
130         Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
132         if (a == NULL || b == NULL) {
133             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."));
134             return;
135         }
137         if (Ancetre(a, b)) {
138             // a is the parent of b, already in the proper order
139         } else if (Ancetre(b, a)) {
140             // reverse order
141             reverseOrderForOp = true;
142         } else {
144             // objects are not in parent/child relationship;
145             // find their lowest common ancestor
146             Inkscape::XML::Node *dad = LCA(a, b);
147             if (dad == NULL) {
148                 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."));
149                 return;
150             }
152             // find the children of the LCA that lead from it to the a and b
153             Inkscape::XML::Node *as = AncetreFils(a, dad);
154             Inkscape::XML::Node *bs = AncetreFils(b, dad);
156             // find out which comes first
157             for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
158                 if (child == as) {
159                     /* a first, so reverse. */
160                     reverseOrderForOp = true;
161                     break;
162                 }
163                 if (child == bs)
164                     break;
165             }
166         }
167     }
169     il = g_slist_copy(il);
171     // first check if all the input objects have shapes
172     // otherwise bail out
173     for (GSList *l = il; l != NULL; l = l->next)
174     {
175         SPItem *item = SP_ITEM(l->data);
176         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
177         {
178             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
179             g_slist_free(il);
180             return;
181         }
182     }
184     // extract the livarot Paths from the source objects
185     // also get the winding rule specified in the style
186     int nbOriginaux = g_slist_length(il);
187     std::vector<Path *> originaux(nbOriginaux);
188     std::vector<FillRule> origWind(nbOriginaux);
189     int curOrig;
190     {
191         curOrig = 0;
192         for (GSList *l = il; l != NULL; l = l->next)
193         {
194             SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
195             gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
196             if (val && strcmp(val, "nonzero") == 0) {
197                 origWind[curOrig]= fill_nonZero;
198             } else if (val && strcmp(val, "evenodd") == 0) {
199                 origWind[curOrig]= fill_oddEven;
200             } else {
201                 origWind[curOrig]= fill_nonZero;
202             }
204             originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
205             if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
206             {
207                 for (int i = curOrig; i >= 0; i--) delete originaux[i];
208                 g_slist_free(il);
209                 return;
210             }
211             curOrig++;
212         }
213     }
214     // reverse if needed
215     // note that the selection list keeps its order
216     if ( reverseOrderForOp ) {
217         Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
218         FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
219     }
221     // and work
222     // some temporary instances, first
223     Shape *theShapeA = new Shape;
224     Shape *theShapeB = new Shape;
225     Shape *theShape = new Shape;
226     Path *res = new Path;
227     res->SetBackData(false);
228     Path::cut_position  *toCut=NULL;
229     int                  nbToCut=0;
231     if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
232         // true boolean op
233         // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
234         originaux[0]->ConvertWithBackData(0.1);
236         originaux[0]->Fill(theShape, 0);
238         theShapeA->ConvertToShape(theShape, origWind[0]);
240         curOrig = 1;
241         for (GSList *l = il->next; l != NULL; l = l->next) {
242             originaux[curOrig]->ConvertWithBackData(0.1);
244             originaux[curOrig]->Fill(theShape, curOrig);
246             theShapeB->ConvertToShape(theShape, origWind[curOrig]);
248             // les elements arrivent en ordre inverse dans la liste
249             theShape->Booleen(theShapeB, theShapeA, bop);
251             {
252                 Shape *swap = theShape;
253                 theShape = theShapeA;
254                 theShapeA = swap;
255             }
256             curOrig++;
257         }
259         {
260             Shape *swap = theShape;
261             theShape = theShapeA;
262             theShapeA = swap;
263         }
265     } else if ( bop == bool_op_cut ) {
266         // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
267         // technically, the cut path is not necessarily a polygon (thus has no winding rule)
268         // it is just uncrossed, and cleaned from duplicate edges and points
269         // then it's fed to Booleen() which will uncross it against the other path
270         // then comes the trick: each edge of the cut path is duplicated (one in each direction),
271         // thus making a polygon. the weight of the edges of the cut are all 0, but
272         // the Booleen need to invert the ones inside the source polygon (for the subsequent
273         // ConvertToForme)
275         // the cut path needs to have the highest pathID in the back data
276         // that's how the Booleen() function knows it's an edge of the cut
278         // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
279         // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
280         // left it at 1.0. Investigate replacing this by a combination of difference and
281         // intersection of the same two paths. -- bb
282         {
283             Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
284             int   swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
285         }
286         originaux[0]->ConvertWithBackData(1.0);
288         originaux[0]->Fill(theShape, 0);
290         theShapeA->ConvertToShape(theShape, origWind[0]);
292         originaux[1]->ConvertWithBackData(1.0);
294         originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
296         theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
298         // les elements arrivent en ordre inverse dans la liste
299         theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
301     } else if ( bop == bool_op_slice ) {
302         // slice is not really a boolean operation
303         // you just put the 2 shapes in a single polygon, uncross it
304         // the points where the degree is > 2 are intersections
305         // just check it's an intersection on the path you want to cut, and keep it
306         // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
307         // make new subpath at each one of these positions
308         // inversion pour l'op\8eration
309         {
310             Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
311             int   swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
312         }
313         originaux[0]->ConvertWithBackData(1.0);
315         originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
317         originaux[1]->ConvertWithBackData(1.0);
319         originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
321         theShape->ConvertToShape(theShapeA, fill_justDont);
323         if ( theShape->hasBackData() ) {
324             // should always be the case, but ya never know
325             {
326                 for (int i = 0; i < theShape->numberOfPoints(); i++) {
327                     if ( theShape->getPoint(i).totalDegree() > 2 ) {
328                         // possibly an intersection
329                         // we need to check that at least one edge from the source path is incident to it
330                         // before we declare it's an intersection
331                         int cb = theShape->getPoint(i).incidentEdge[FIRST];
332                         int   nbOrig=0;
333                         int   nbOther=0;
334                         int   piece=-1;
335                         float t=0.0;
336                         while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
337                             if ( theShape->ebData[cb].pathID == 0 ) {
338                                 // the source has an edge incident to the point, get its position on the path
339                                 piece=theShape->ebData[cb].pieceID;
340                                 if ( theShape->getEdge(cb).st == i ) {
341                                     t=theShape->ebData[cb].tSt;
342                                 } else {
343                                     t=theShape->ebData[cb].tEn;
344                                 }
345                                 nbOrig++;
346                             }
347                             if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
348                             cb=theShape->NextAt(i, cb);
349                         }
350                         if ( nbOrig > 0 && nbOther > 0 ) {
351                             // point incident to both path and cut: an intersection
352                             // note that you only keep one position on the source; you could have degenerate
353                             // cases where the source crosses itself at this point, and you wouyld miss an intersection
354                             toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
355                             toCut[nbToCut].piece=piece;
356                             toCut[nbToCut].t=t;
357                             nbToCut++;
358                         }
359                     }
360                 }
361             }
362             {
363                 // i think it's useless now
364                 int i = theShape->numberOfEdges() - 1;
365                 for (;i>=0;i--) {
366                     if ( theShape->ebData[i].pathID == 1 ) {
367                         theShape->SubEdge(i);
368                     }
369                 }
370             }
372         }
373     }
375     int*    nesting=NULL;
376     int*    conts=NULL;
377     int     nbNest=0;
378     // pour compenser le swap juste avant
379     if ( bop == bool_op_slice ) {
380 //    theShape->ConvertToForme(res, nbOriginaux, originaux, true);
381 //    res->ConvertForcedToMoveTo();
382         res->Copy(originaux[0]);
383         res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
384         free(toCut);
385     } else if ( bop == bool_op_cut ) {
386         // il faut appeler pour desallouer PointData (pas vital, mais bon)
387         // the Booleen() function did not deallocated the point_data array in theShape, because this
388         // function needs it.
389         // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
390         // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
391         theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
392     } else {
393         theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
394     }
396     delete theShape;
397     delete theShapeA;
398     delete theShapeB;
399     for (int i = 0; i < nbOriginaux; i++)  delete originaux[i];
401     if (res->descr_cmd.size() <= 1)
402     {
403         // only one command, presumably a moveto: it isn't a path
404         for (GSList *l = il; l != NULL; l = l->next)
405         {
406             SP_OBJECT(l->data)->deleteObject();
407         }
408         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
409                          /* TODO: annotate */ "splivarot.cpp:409");
410         selection->clear();
412         delete res;
413         g_slist_free(il);
414         return;
415     }
417     // remember important aspects of the source path, to be restored
418     Inkscape::XML::Node *repr_source;
419     if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
420         if (reverseOrderForOp) {
421              repr_source = SP_OBJECT_REPR(il->data);
422         } else {
423              repr_source = SP_OBJECT_REPR(il->next->data);
424         }
425     } else {
426         // find out the bottom object
427         GSList *sorted = g_slist_copy((GSList *) selection->reprList());
429         sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
430         repr_source = ((Inkscape::XML::Node *) sorted->data);
431         g_slist_free(sorted);
432     }
433     gint pos = repr_source->position();
434     Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
435     char const *id = repr_source->attribute("id");
436     char const *style = repr_source->attribute("style");
439     // remove source paths
440     selection->clear();
441     for (GSList *l = il; l != NULL; l = l->next) {
442         // if this is the bottommost object,
443         if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
444             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
445             SP_OBJECT(l->data)->deleteObject(false);
446         } else {
447             // delete the object for real, so that its clones can take appropriate action
448             SP_OBJECT(l->data)->deleteObject();
449         }
450     }
451     g_slist_free(il);
453     // premultiply by the inverse of parent's repr
454     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
455     NR::Matrix local = sp_item_i2doc_affine(parent_item);
456     gchar affinestr[80];
457     gchar *transform = NULL;
458     if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
459         transform = affinestr;
460     }
462     // now that we have the result, add it on the canvas
463     if ( bop == bool_op_cut || bop == bool_op_slice ) {
464         int    nbRP=0;
465         Path** resPath;
466         if ( bop == bool_op_slice ) {
467             // there are moveto's at each intersection, but it's still one unique path
468             // so break it down and add each subpath independently
469             // we could call break_apart to do this, but while we have the description...
470             resPath=res->SubPaths(nbRP, false);
471         } else {
472             // cut operation is a bit wicked: you need to keep holes
473             // that's why you needed the nesting
474             // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
475             // to get the path for each part of the polygon. that's why you need the nesting info:
476             // to know in wich subpath to add a subpath
477             resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
479             // cleaning
480             if ( conts ) free(conts);
481             if ( nesting ) free(nesting);
482         }
484         // add all the pieces resulting from cut or slice
485         for (int i=0;i<nbRP;i++) {
486             gchar *d = resPath[i]->svg_dump_path();
488             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
489             repr->setAttribute("style", style);
490             repr->setAttribute("d", d);
491             g_free(d);
493             // for slice, remove fill
494             if (bop == bool_op_slice) {
495                 SPCSSAttr *css;
497                 css = sp_repr_css_attr_new();
498                 sp_repr_css_set_property(css, "fill", "none");
500                 sp_repr_css_change(repr, css, "style");
502                 sp_repr_css_attr_unref(css);
503             }
505             // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
506             // this means it's basically random which of the pieces inherits the original's id and clones
507             // a better algorithm might figure out e.g. the biggest piece
508             repr->setAttribute("id", id);
510             repr->setAttribute("transform", transform);
512             // add the new repr to the parent
513             parent->appendChild(repr);
515             // move to the saved position
516             repr->setPosition(pos > 0 ? pos : 0);
518             selection->add(repr);
519             Inkscape::GC::release(repr);
521             delete resPath[i];
522         }
523         if ( resPath ) free(resPath);
525     } else {
526         gchar *d = res->svg_dump_path();
528         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
529         repr->setAttribute("style", style);
531         repr->setAttribute("d", d);
532         g_free(d);
534         repr->setAttribute("transform", transform);
536         repr->setAttribute("id", id);
537         parent->appendChild(repr);
538         repr->setPosition(pos > 0 ? pos : 0);
540         selection->add(repr);
541         Inkscape::GC::release(repr);
542     }
544     unsigned int operation_verb = SP_VERB_NONE;
545     switch(bop) {
546         case bool_op_union   : operation_verb = SP_VERB_SELECTION_UNION;     break;
547         case bool_op_inters  : operation_verb = SP_VERB_SELECTION_INTERSECT; break;
548         case bool_op_diff    : operation_verb = SP_VERB_SELECTION_DIFF;      break;
549         case bool_op_symdiff : operation_verb = SP_VERB_SELECTION_SYMDIFF;   break;
550         case bool_op_cut     : operation_verb = SP_VERB_SELECTION_CUT;       break;
551         case bool_op_slice   : operation_verb = SP_VERB_SELECTION_SLICE;     break;
552     }
554     sp_document_done(sp_desktop_document(desktop), operation_verb, 
555                      /* TODO: annotate */ "splivarot.cpp:555");
557     delete res;
561 void
562 sp_selected_path_outline()
564     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
566     Inkscape::Selection *selection = sp_desktop_selection(desktop);
568     if (selection->isEmpty()) {
569         // TRANSLATORS: "to outline" means "to convert stroke to path"
570         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to outline."));
571         return;
572     }
574     bool did = false;
576     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
577          items != NULL;
578          items = items->next) {
580         SPItem *item = (SPItem *) items->data;
582         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
583             continue;
585         SPCurve *curve = NULL;
586         if (SP_IS_SHAPE(item)) {
587             curve = sp_shape_get_curve(SP_SHAPE(item));
588             if (curve == NULL)
589                 continue;
590         }
591         if (SP_IS_TEXT(item)) {
592             curve = SP_TEXT(item)->getNormalizedBpath();
593             if (curve == NULL)
594                 continue;
595         }
597         {   // pas de stroke pas de chocolat
598             SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
599             gchar const *val = sp_repr_css_property(css, "stroke", NULL);
601             if (val == NULL || strcmp(val, "none") == 0) {
602                 sp_curve_unref(curve);
603                 continue;
604             }
605         }
607         // remember old stroke style, to be set on fill
608         SPCSSAttr *ncss;
609         {
610             SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
611             gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
612             gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
614             ncss = sp_repr_css_attr_new();
616             sp_repr_css_set_property(ncss, "stroke", "none");
617             sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
618             sp_repr_css_set_property(ncss, "fill", val);
619             if ( opac ) {
620                 sp_repr_css_set_property(ncss, "fill-opacity", opac);
621             } else {
622                 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
623             }
624             sp_repr_css_unset_property(ncss, "marker-start");
625             sp_repr_css_unset_property(ncss, "marker-mid");
626             sp_repr_css_unset_property(ncss, "marker-end");
627         }
629         NR::Matrix const transform(item->transform);
630         float const scale = transform.expansion();
631         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
632         SPStyle *i_style = SP_OBJECT(item)->style;
634         float o_width, o_miter;
635         JoinType o_join;
636         ButtType o_butt;
638         {
639             int jointype, captype;
641             jointype = i_style->stroke_linejoin.computed;
642             captype = i_style->stroke_linecap.computed;
643             o_width = i_style->stroke_width.computed;
645             switch (jointype) {
646                 case SP_STROKE_LINEJOIN_MITER:
647                     o_join = join_pointy;
648                     break;
649                 case SP_STROKE_LINEJOIN_ROUND:
650                     o_join = join_round;
651                     break;
652                 default:
653                     o_join = join_straight;
654                     break;
655             }
657             switch (captype) {
658                 case SP_STROKE_LINECAP_SQUARE:
659                     o_butt = butt_square;
660                     break;
661                 case SP_STROKE_LINECAP_ROUND:
662                     o_butt = butt_round;
663                     break;
664                 default:
665                     o_butt = butt_straight;
666                     break;
667             }
669             if (o_width < 0.1)
670                 o_width = 0.1;
671             o_miter = i_style->stroke_miterlimit.value * o_width;
672         }
674         Path *orig = Path_for_item(item, false);
675         if (orig == NULL) {
676             g_free(style);
677             sp_curve_unref(curve);
678             continue;
679         }
681         Path *res = new Path;
682         res->SetBackData(false);
684         if (i_style->stroke_dash.n_dash) {
685             // For dashed strokes, use Stroke method, because Outline can't do dashes
686             // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
688             orig->ConvertWithBackData(0.1);
690             orig->DashPolylineFromStyle(i_style, scale, 0);
692             Shape* theShape = new Shape;
693             orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
694                          0.5 * o_miter);
695             orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
697             Shape *theRes = new Shape;
699             theRes->ConvertToShape(theShape, fill_positive);
701             Path *originaux[1];
702             originaux[0] = res;
703             theRes->ConvertToForme(orig, 1, originaux);
705             res->Coalesce(5.0);
707             delete theShape;
708             delete theRes;
710         } else {
712             orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
714             orig->Coalesce(0.5 * o_width);
716             Shape *theShape = new Shape;
717             Shape *theRes = new Shape;
719             res->ConvertWithBackData(1.0);
720             res->Fill(theShape, 0);
721             theRes->ConvertToShape(theShape, fill_positive);
723             Path *originaux[1];
724             originaux[0] = res;
725             theRes->ConvertToForme(orig, 1, originaux);
727             delete theShape;
728             delete theRes;
729         }
731         if (orig->descr_cmd.size() <= 1) {
732             // ca a merd\8e, ou bien le resultat est vide
733             delete res;
734             delete orig;
735             g_free(style);
736             continue;
737         }
739         did = true;
741         // remember the position of the item
742         gint pos = SP_OBJECT_REPR(item)->position();
743         // remember parent
744         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
745         // remember id
746         char const *id = SP_OBJECT_REPR(item)->attribute("id");
748         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
750             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
752             // restore old style
753             repr->setAttribute("style", style);
755             // set old stroke style on fill
756             sp_repr_css_change(repr, ncss, "style");
758             sp_repr_css_attr_unref(ncss);
760             gchar *str = orig->svg_dump_path();
761             repr->setAttribute("d", str);
762             g_free(str);
765             if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
767                 Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
769                 // add the group to the parent
770                 parent->appendChild(g_repr);
771                 // move to the saved position
772                 g_repr->setPosition(pos > 0 ? pos : 0);
774                 g_repr->appendChild(repr);
775                 repr->setAttribute("id", id);
776                 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
777                 sp_item_write_transform(newitem, repr, transform);
779                 SPShape *shape = SP_SHAPE(item);
781                 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
782                     for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
783                         if (sp_shape_marker_required (shape, m, bp)) {
785                             SPMarker* marker = SP_MARKER (shape->marker[m]);
786                             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
788                             NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
790                             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
791                                 tr = NR::scale(i_style->stroke_width.computed) * tr;
792                             }
794                             // total marker transform
795                             tr = marker_item->transform * marker->c2p * tr * transform;
797                             if (SP_OBJECT_REPR(marker_item)) {
798                                 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
799                                 g_repr->appendChild(m_repr);
800                                 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
801                                 sp_item_write_transform(marker_item, m_repr, tr);
802                             }
803                         }
804                     }
805                 }
808                 selection->add(g_repr);
810                 Inkscape::GC::release(g_repr);
813             } else {
815                 // add the new repr to the parent
816                 parent->appendChild(repr);
818                 // move to the saved position
819                 repr->setPosition(pos > 0 ? pos : 0);
821                 repr->setAttribute("id", id);
823                 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
824                 sp_item_write_transform(newitem, repr, transform);
826                 selection->add(repr);
828             }
830             Inkscape::GC::release(repr);
832             sp_curve_unref(curve);
833             selection->remove(item);
834             SP_OBJECT(item)->deleteObject(false);
836         }
838         delete res;
839         delete orig;
840         g_free(style);
842     }
844     if (did) {
845         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE, 
846                          /* TODO: annotate */ "splivarot.cpp:846");
847     } else {
848         // TRANSLATORS: "to outline" means "to convert stroke to path"
849         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> to outline in the selection."));
850         return;
851     }
855 void
856 sp_selected_path_offset()
858     double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
860     sp_selected_path_do_offset(true, prefOffset);
862 void
863 sp_selected_path_inset()
865     double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
867     sp_selected_path_do_offset(false, prefOffset);
870 void
871 sp_selected_path_offset_screen(double pixels)
873     sp_selected_path_do_offset(true,  pixels / SP_ACTIVE_DESKTOP->current_zoom());
876 void
877 sp_selected_path_inset_screen(double pixels)
879     sp_selected_path_do_offset(false,  pixels / SP_ACTIVE_DESKTOP->current_zoom());
883 void sp_selected_path_create_offset_object_zero()
885     sp_selected_path_create_offset_object(0, false);
888 void sp_selected_path_create_offset()
890     sp_selected_path_create_offset_object(1, false);
892 void sp_selected_path_create_inset()
894     sp_selected_path_create_offset_object(-1, false);
897 void sp_selected_path_create_updating_offset_object_zero()
899     sp_selected_path_create_offset_object(0, true);
902 void sp_selected_path_create_updating_offset()
904     sp_selected_path_create_offset_object(1, true);
906 void sp_selected_path_create_updating_inset()
908     sp_selected_path_create_offset_object(-1, true);
911 void
912 sp_selected_path_create_offset_object(int expand, bool updating)
914     Inkscape::Selection *selection;
915     Inkscape::XML::Node *repr;
916     SPItem *item;
917     SPCurve *curve;
918     gchar *style, *str;
919     SPDesktop *desktop;
920     float o_width, o_miter;
921     JoinType o_join;
922     ButtType o_butt;
924     curve = NULL;
926     desktop = SP_ACTIVE_DESKTOP;
928     selection = sp_desktop_selection(desktop);
930     item = selection->singleItem();
932     if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
933         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
934         return;
935     }
936     if (SP_IS_SHAPE(item))
937     {
938         curve = sp_shape_get_curve(SP_SHAPE(item));
939         if (curve == NULL)
940             return;
941     }
942     if (SP_IS_TEXT(item))
943     {
944         curve = SP_TEXT(item)->getNormalizedBpath();
945         if (curve == NULL)
946             return;
947     }
949     NR::Matrix const transform(item->transform);
951     sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
953     style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
955     // remember the position of the item
956     gint pos = SP_OBJECT_REPR(item)->position();
957     // remember parent
958     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
960     {
961         SPStyle *i_style = SP_OBJECT(item)->style;
962         int jointype, captype;
964         jointype = i_style->stroke_linejoin.value;
965         captype = i_style->stroke_linecap.value;
966         o_width = i_style->stroke_width.computed;
967         if (jointype == SP_STROKE_LINEJOIN_MITER)
968         {
969             o_join = join_pointy;
970         }
971         else if (jointype == SP_STROKE_LINEJOIN_ROUND)
972         {
973             o_join = join_round;
974         }
975         else
976         {
977             o_join = join_straight;
978         }
979         if (captype == SP_STROKE_LINECAP_SQUARE)
980         {
981             o_butt = butt_square;
982         }
983         else if (captype == SP_STROKE_LINECAP_ROUND)
984         {
985             o_butt = butt_round;
986         }
987         else
988         {
989             o_butt = butt_straight;
990         }
992         {
993             double prefOffset = 1.0;
994             prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
995             o_width = prefOffset;
996         }
998         if (o_width < 0.01)
999             o_width = 0.01;
1000         o_miter = i_style->stroke_miterlimit.value * o_width;
1001     }
1003     Path *orig = Path_for_item(item, true, false);
1004     if (orig == NULL)
1005     {
1006         g_free(style);
1007         sp_curve_unref(curve);
1008         return;
1009     }
1011     Path *res = new Path;
1012     res->SetBackData(false);
1014     {
1015         Shape *theShape = new Shape;
1016         Shape *theRes = new Shape;
1018         orig->ConvertWithBackData(1.0);
1019         orig->Fill(theShape, 0);
1021         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1022         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1023         if (val && strcmp(val, "nonzero") == 0)
1024         {
1025             theRes->ConvertToShape(theShape, fill_nonZero);
1026         }
1027         else if (val && strcmp(val, "evenodd") == 0)
1028         {
1029             theRes->ConvertToShape(theShape, fill_oddEven);
1030         }
1031         else
1032         {
1033             theRes->ConvertToShape(theShape, fill_nonZero);
1034         }
1036         Path *originaux[1];
1037         originaux[0] = orig;
1038         theRes->ConvertToForme(res, 1, originaux);
1040         delete theShape;
1041         delete theRes;
1042     }
1044     sp_curve_unref(curve);
1046     if (res->descr_cmd.size() <= 1)
1047     {
1048         // pas vraiment de points sur le resultat
1049         // donc il ne reste rien
1050         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
1051                          /* TODO: annotate */ "splivarot.cpp:1051");
1052         selection->clear();
1054         delete res;
1055         delete orig;
1056         g_free(style);
1057         return;
1058     }
1060     {
1061         gchar tstr[80];
1063         tstr[79] = '\0';
1065         repr = sp_repr_new("svg:path");
1066         repr->setAttribute("sodipodi:type", "inkscape:offset");
1067         sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1068                                                           ? o_width
1069                                                           : expand < 0
1070                                                           ? -o_width
1071                                                           : 0 ));
1073         str = res->svg_dump_path();
1074         repr->setAttribute("inkscape:original", str);
1075         g_free(str);
1077         if ( updating ) {
1078             char const *id = SP_OBJECT(item)->repr->attribute("id");
1079             char const *uri = g_strdup_printf("#%s", id);
1080             repr->setAttribute("xlink:href", uri);
1081             g_free((void *) uri);
1082         } else {
1083             repr->setAttribute("inkscape:href", NULL);
1084         }
1086         repr->setAttribute("style", style);
1088         // add the new repr to the parent
1089         parent->appendChild(repr);
1091         // move to the saved position
1092         repr->setPosition(pos > 0 ? pos : 0);
1094         SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1096         if ( updating ) {
1097             // on conserve l'original
1098             // we reapply the transform to the original (offset will feel it)
1099             sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1100         } else {
1101             // delete original, apply the transform to the offset
1102             SP_OBJECT(item)->deleteObject(false);
1103             sp_item_write_transform(nitem, repr, transform);
1104         }
1106         // The object just created from a temporary repr is only a seed.
1107         // We need to invoke its write which will update its real repr (in particular adding d=)
1108         SP_OBJECT(nitem)->updateRepr();
1110         Inkscape::GC::release(repr);
1112         selection->set(nitem);
1113     }
1115     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
1116                      /* TODO: annotate */ "splivarot.cpp:1116");
1118     delete res;
1119     delete orig;
1121     g_free(style);
1135 void
1136 sp_selected_path_do_offset(bool expand, double prefOffset)
1138     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1140     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1142     if (selection->isEmpty()) {
1143         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1144         return;
1145     }
1147     bool did = false;
1149     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1150          items != NULL;
1151          items = items->next) {
1153         SPItem *item = (SPItem *) items->data;
1155         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1156             continue;
1158         SPCurve *curve = NULL;
1159         if (SP_IS_SHAPE(item)) {
1160             curve = sp_shape_get_curve(SP_SHAPE(item));
1161             if (curve == NULL)
1162                 continue;
1163         }
1164         if (SP_IS_TEXT(item)) {
1165             curve = SP_TEXT(item)->getNormalizedBpath();
1166             if (curve == NULL)
1167                 continue;
1168         }
1170         NR::Matrix const transform(item->transform);
1172         sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1174         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1176         float o_width, o_miter;
1177         JoinType o_join;
1178         ButtType o_butt;
1180         {
1181             SPStyle *i_style = SP_OBJECT(item)->style;
1182             int jointype, captype;
1184             jointype = i_style->stroke_linejoin.value;
1185             captype = i_style->stroke_linecap.value;
1186             o_width = i_style->stroke_width.computed;
1188             switch (jointype) {
1189                 case SP_STROKE_LINEJOIN_MITER:
1190                     o_join = join_pointy;
1191                     break;
1192                 case SP_STROKE_LINEJOIN_ROUND:
1193                     o_join = join_round;
1194                     break;
1195                 default:
1196                     o_join = join_straight;
1197                     break;
1198             }
1200             switch (captype) {
1201                 case SP_STROKE_LINECAP_SQUARE:
1202                     o_butt = butt_square;
1203                     break;
1204                 case SP_STROKE_LINECAP_ROUND:
1205                     o_butt = butt_round;
1206                     break;
1207                 default:
1208                     o_butt = butt_straight;
1209                     break;
1210             }
1212             o_width = prefOffset;
1214             if (o_width < 0.1)
1215                 o_width = 0.1;
1216             o_miter = i_style->stroke_miterlimit.value * o_width;
1217         }
1219         Path *orig = Path_for_item(item, false);
1220         if (orig == NULL) {
1221             g_free(style);
1222             sp_curve_unref(curve);
1223             continue;
1224         }
1226         Path *res = new Path;
1227         res->SetBackData(false);
1229         {
1230             Shape *theShape = new Shape;
1231             Shape *theRes = new Shape;
1233             orig->ConvertWithBackData(0.03);
1234             orig->Fill(theShape, 0);
1236             SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1237             gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1238             if (val && strcmp(val, "nonzero") == 0)
1239             {
1240                 theRes->ConvertToShape(theShape, fill_nonZero);
1241             }
1242             else if (val && strcmp(val, "evenodd") == 0)
1243             {
1244                 theRes->ConvertToShape(theShape, fill_oddEven);
1245             }
1246             else
1247             {
1248                 theRes->ConvertToShape(theShape, fill_nonZero);
1249             }
1251             // et maintenant: offset
1252             // methode inexacte
1253 /*                      Path *originaux[1];
1254                         originaux[0] = orig;
1255                         theRes->ConvertToForme(res, 1, originaux);
1257                         if (expand) {
1258                         res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1259                         } else {
1260                         res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1261                         }
1263                         orig->ConvertWithBackData(1.0);
1264                         orig->Fill(theShape, 0);
1265                         theRes->ConvertToShape(theShape, fill_positive);
1266                         originaux[0] = orig;
1267                         theRes->ConvertToForme(res, 1, originaux);
1269                         if (o_width >= 0.5) {
1270                         //     res->Coalesce(1.0);
1271                         res->ConvertEvenLines(1.0);
1272                         res->Simplify(1.0);
1273                         } else {
1274                         //      res->Coalesce(o_width);
1275                         res->ConvertEvenLines(1.0*o_width);
1276                         res->Simplify(1.0 * o_width);
1277                         }    */
1278             // methode par makeoffset
1280             if (expand)
1281             {
1282                 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1283             }
1284             else
1285             {
1286                 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1287             }
1288             theRes->ConvertToShape(theShape, fill_positive);
1290             res->Reset();
1291             theRes->ConvertToForme(res);
1293             if (o_width >= 1.0)
1294             {
1295                 res->ConvertEvenLines(1.0);
1296                 res->Simplify(1.0);
1297             }
1298             else
1299             {
1300                 res->ConvertEvenLines(1.0*o_width);
1301                 res->Simplify(1.0 * o_width);
1302             }
1304             delete theShape;
1305             delete theRes;
1306         }
1308         did = true;
1310         sp_curve_unref(curve);
1311         // remember the position of the item
1312         gint pos = SP_OBJECT_REPR(item)->position();
1313         // remember parent
1314         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1315         // remember id
1316         char const *id = SP_OBJECT_REPR(item)->attribute("id");
1318         selection->remove(item);
1319         SP_OBJECT(item)->deleteObject(false);
1321         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1323             gchar tstr[80];
1325             tstr[79] = '\0';
1327             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1329             repr->setAttribute("style", style);
1331             gchar *str = res->svg_dump_path();
1332             repr->setAttribute("d", str);
1333             g_free(str);
1335             // add the new repr to the parent
1336             parent->appendChild(repr);
1338             // move to the saved position
1339             repr->setPosition(pos > 0 ? pos : 0);
1341             SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1343             // reapply the transform
1344             sp_item_write_transform(newitem, repr, transform);
1346             repr->setAttribute("id", id);
1348             selection->add(repr);
1350             Inkscape::GC::release(repr);
1351         }
1353         delete orig;
1354         delete res;
1355     }
1357     if (did) {
1358         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
1359                          /* TODO: annotate */ "splivarot.cpp:1359");
1360     } else {
1361         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1362         return;
1363     }
1368 //return true if we changed something, else false
1369 bool
1370 sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item,
1371                  float threshold,  bool justCoalesce,
1372                  float angleLimit, bool breakableAngles,
1373                  gdouble size,     bool modifySelection)
1375     if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1376         return false;
1378     //If this is a group, do the children instead
1379     if (SP_IS_GROUP(item)) {
1381         bool didSomething = false;
1383         for ( GSList *children = sp_item_group_item_list(SP_GROUP(item));
1384                  children  ; children = children->next) {
1386             SPItem *child = (SPItem *) children->data;
1387             didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce,
1388                    angleLimit, breakableAngles, size, false);
1389         }
1391         return didSomething;
1392     }
1395     SPCurve *curve = NULL;
1397     if (SP_IS_SHAPE(item)) {
1398         curve = sp_shape_get_curve(SP_SHAPE(item));
1399         if (!curve)
1400             return false;
1401     }
1403     if (SP_IS_TEXT(item)) {
1404         curve = SP_TEXT(item)->getNormalizedBpath();
1405         if (!curve)
1406             return false;
1407     }
1409     // save the transform, to re-apply it after simplification
1410     NR::Matrix const transform(item->transform);
1412     /*
1413        reset the transform, effectively transforming the item by transform.inverse();
1414        this is necessary so that the item is transformed twice back and forth,
1415        allowing all compensations to cancel out regardless of the preferences
1416     */
1417     sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1419     gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1421     Path *orig = Path_for_item(item, false);
1422     if (orig == NULL) {
1423         g_free(style);
1424         sp_curve_unref(curve);
1425         return false;
1426     }
1428     sp_curve_unref(curve);
1429     // remember the position of the item
1430     gint pos = SP_OBJECT_REPR(item)->position();
1431     // remember parent
1432     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1433     // remember id
1434     char const *id = SP_OBJECT_REPR(item)->attribute("id");
1436     //If a group was selected, to not change the selection list
1437     if (modifySelection)
1438         selection->remove(item);
1440     SP_OBJECT(item)->deleteObject(false);
1442     if ( justCoalesce ) {
1443         orig->Coalesce(threshold * size);
1444     } else {
1445         orig->ConvertEvenLines(threshold * size);
1446         orig->Simplify(threshold * size);
1447     }
1449     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1451     repr->setAttribute("style", style);
1453     gchar *str = orig->svg_dump_path();
1454     repr->setAttribute("d", str);
1455     g_free(str);
1457     // restore id
1458     repr->setAttribute("id", id);
1460     // add the new repr to the parent
1461     parent->appendChild(repr);
1463     // move to the saved position
1464     repr->setPosition(pos > 0 ? pos : 0);
1466     SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1468     // reapply the transform
1469     sp_item_write_transform(newitem, repr, transform);
1471     //If we are not in a selected group
1472     if (modifySelection)
1473         selection->add(repr);
1475     Inkscape::GC::release(repr);
1477     // clean up
1478     if (orig) delete orig;
1480     return true;
1484 void
1485 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1486                        float angleLimit, bool breakableAngles)
1488     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1490     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1492     if (selection->isEmpty()) {
1493         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1494                          _("Select <b>path(s)</b> to simplify."));
1495         return;
1496     }
1498     // remember selection size
1499     NR::Rect bbox = selection->bounds();
1500     gdouble size  = L2(bbox.dimensions());
1502     bool didSomething = false;
1504     //Loop through all of the items in the selection
1505     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1506                         items != NULL; items = items->next) {
1508         SPItem *item = (SPItem *) items->data;
1510         if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1511             continue;
1513         didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1514                            threshold, justCoalesce, angleLimit, breakableAngles, size, true);
1515     }
1518     if (didSomething)
1519         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY, 
1520                          /* TODO: annotate */ "splivarot.cpp:1520");
1521     else
1522         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1527 // globals for keeping track of accelerated simplify
1528 static double previousTime      = 0.0;
1529 static gdouble simplifyMultiply = 1.0;
1531 void
1532 sp_selected_path_simplify(void)
1534     gdouble simplifyThreshold =
1535         prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1536     bool simplifyJustCoalesce =
1537         (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1539     //Get the current time
1540     GTimeVal currentTimeVal;
1541     g_get_current_time(&currentTimeVal);
1542     double currentTime = currentTimeVal.tv_sec * 1000000 +
1543                 currentTimeVal.tv_usec;
1545     //Was the previous call to this function recent? (<0.5 sec)
1546     if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1548         // add to the threshold 1/2 of its original value
1549         simplifyMultiply  += 0.5;
1550         simplifyThreshold *= simplifyMultiply;
1552     } else {
1553         // reset to the default
1554         simplifyMultiply = 1;
1555     }
1557     //remember time for next call
1558     previousTime = currentTime;
1560     //g_print("%g\n", simplify_threshold);
1562     //Make the actual call
1563     sp_selected_path_simplify_selection(simplifyThreshold,
1564                       simplifyJustCoalesce, 0.0, false);
1569 // fonctions utilitaires
1571 bool
1572 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1574     if (who == NULL || a == NULL)
1575         return false;
1576     if (who == a)
1577         return true;
1578     return Ancetre(sp_repr_parent(a), who);
1581 Path *
1582 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1584     SPCurve *curve;
1586     if (!item)
1587         return NULL;
1589     if (SP_IS_SHAPE(item))
1590     {
1591         curve = sp_shape_get_curve(SP_SHAPE(item));
1592     }
1593     else if (SP_IS_TEXT(item))
1594     {
1595         curve = SP_TEXT(item)->getNormalizedBpath();
1596     }
1597     else
1598     {
1599         curve = NULL;
1600     }
1602     if (!curve)
1603         return NULL;
1604     NArtBpath *bpath = SP_CURVE_BPATH(curve);
1605     if (bpath == NULL)
1606         return NULL;
1608     if ( doTransformation ) {
1609         if (transformFull)
1610             bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1611         else
1612             bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1613         sp_curve_unref(curve);
1614         curve=NULL;
1615     } else {
1616         bpath=SP_CURVE_BPATH(curve);
1617     }
1619     Path *dest = new Path;
1620     dest->SetBackData(false);
1621     {
1622         int   i;
1623         bool  closed = false;
1624         float lastX  = 0.0;
1625         float lastY  = 0.0;
1627         for (i = 0; bpath[i].code != NR_END; i++) {
1628             switch (bpath[i].code) {
1629                 case NR_LINETO:
1630                     lastX = bpath[i].x3;
1631                     lastY = bpath[i].y3;
1632                     {
1633                         NR::Point tmp(lastX, lastY);
1634                         dest->LineTo(tmp);
1635                     }
1636                     break;
1638                 case NR_CURVETO:
1639                 {
1640                     NR::Point tmp, tms, tme;
1641                     tmp[0]=bpath[i].x3;
1642                     tmp[1]=bpath[i].y3;
1643                     tms[0]=3 * (bpath[i].x1 - lastX);
1644                     tms[1]=3 * (bpath[i].y1 - lastY);
1645                     tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1646                     tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1647                     dest->CubicTo(tmp,tms,tme);
1648                 }
1649                 lastX = bpath[i].x3;
1650                 lastY = bpath[i].y3;
1651                 break;
1653                 case NR_MOVETO_OPEN:
1654                 case NR_MOVETO:
1655                     if (closed)
1656                         dest->Close();
1657                     closed = (bpath[i].code == NR_MOVETO);
1658                     lastX = bpath[i].x3;
1659                     lastY = bpath[i].y3;
1660                     {
1661                         NR::Point  tmp(lastX, lastY);
1662                         dest->MoveTo(tmp);
1663                     }
1664                     break;
1665                 default:
1666                     break;
1667             }
1668         }
1669         if (closed)
1670             dest->Close();
1671     }
1673     if ( doTransformation ) {
1674         if ( bpath ) g_free(bpath);
1675     } else {
1676         sp_curve_unref(curve);
1677     }
1678     return dest;
1681 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1683     //get nearest position on path
1684     Path::cut_position pos = path->PointToCurvilignPosition(p);
1685     return pos;
1688 NR::Point get_point_on_Path(Path *path, int piece, double t)
1690     NR::Point p;
1691     path->PointAt(piece, t, p);
1692     return p;
1696 /*
1697   Local Variables:
1698   mode:c++
1699   c-file-style:"stroustrup"
1700   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1701   indent-tabs-mode:nil
1702   fill-column:99
1703   End:
1704 */
1705 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :