Code

Add ubuntu palette, closes 1510556.
[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));
409         selection->clear();
411         delete res;
412         g_slist_free(il);
413         return;
414     }
416     // remember important aspects of the source path, to be restored
417     Inkscape::XML::Node *repr_source;
418     if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
419         if (reverseOrderForOp) {
420              repr_source = SP_OBJECT_REPR(il->data);
421         } else {
422              repr_source = SP_OBJECT_REPR(il->next->data);
423         }
424     } else {
425         // find out the bottom object
426         GSList *sorted = g_slist_copy((GSList *) selection->reprList());
428         sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
429         repr_source = ((Inkscape::XML::Node *) sorted->data);
430         g_slist_free(sorted);
431     }
432     gint pos = repr_source->position();
433     Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
434     char const *id = repr_source->attribute("id");
435     char const *style = repr_source->attribute("style");
438     // remove source paths
439     selection->clear();
440     for (GSList *l = il; l != NULL; l = l->next) {
441         // if this is the bottommost object,
442         if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
443             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
444             SP_OBJECT(l->data)->deleteObject(false);
445         } else {
446             // delete the object for real, so that its clones can take appropriate action
447             SP_OBJECT(l->data)->deleteObject();
448         }
449     }
450     g_slist_free(il);
452     // premultiply by the inverse of parent's repr
453     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
454     NR::Matrix local = sp_item_i2doc_affine(parent_item);
455     gchar affinestr[80];
456     gchar *transform = NULL;
457     if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
458         transform = affinestr;
459     }
461     // now that we have the result, add it on the canvas
462     if ( bop == bool_op_cut || bop == bool_op_slice ) {
463         int    nbRP=0;
464         Path** resPath;
465         if ( bop == bool_op_slice ) {
466             // there are moveto's at each intersection, but it's still one unique path
467             // so break it down and add each subpath independently
468             // we could call break_apart to do this, but while we have the description...
469             resPath=res->SubPaths(nbRP, false);
470         } else {
471             // cut operation is a bit wicked: you need to keep holes
472             // that's why you needed the nesting
473             // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
474             // to get the path for each part of the polygon. that's why you need the nesting info:
475             // to know in wich subpath to add a subpath
476             resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
478             // cleaning
479             if ( conts ) free(conts);
480             if ( nesting ) free(nesting);
481         }
483         // add all the pieces resulting from cut or slice
484         for (int i=0;i<nbRP;i++) {
485             gchar *d = resPath[i]->svg_dump_path();
487             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
488             repr->setAttribute("style", style);
489             repr->setAttribute("d", d);
490             g_free(d);
492             // for slice, remove fill
493             if (bop == bool_op_slice) {
494                 SPCSSAttr *css;
496                 css = sp_repr_css_attr_new();
497                 sp_repr_css_set_property(css, "fill", "none");
499                 sp_repr_css_change(repr, css, "style");
501                 sp_repr_css_attr_unref(css);
502             }
504             // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
505             // this means it's basically random which of the pieces inherits the original's id and clones
506             // a better algorithm might figure out e.g. the biggest piece
507             repr->setAttribute("id", id);
509             repr->setAttribute("transform", transform);
511             // add the new repr to the parent
512             parent->appendChild(repr);
514             // move to the saved position
515             repr->setPosition(pos > 0 ? pos : 0);
517             selection->add(repr);
518             Inkscape::GC::release(repr);
520             delete resPath[i];
521         }
522         if ( resPath ) free(resPath);
524     } else {
525         gchar *d = res->svg_dump_path();
527         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
528         repr->setAttribute("style", style);
530         repr->setAttribute("d", d);
531         g_free(d);
533         repr->setAttribute("transform", transform);
535         repr->setAttribute("id", id);
536         parent->appendChild(repr);
537         repr->setPosition(pos > 0 ? pos : 0);
539         selection->add(repr);
540         Inkscape::GC::release(repr);
541     }
543     sp_document_done(sp_desktop_document(desktop));
545     delete res;
549 void
550 sp_selected_path_outline()
552     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
554     Inkscape::Selection *selection = sp_desktop_selection(desktop);
556     if (selection->isEmpty()) {
557         // TRANSLATORS: "to outline" means "to convert stroke to path"
558         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to outline."));
559         return;
560     }
562     bool did = false;
564     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
565          items != NULL;
566          items = items->next) {
568         SPItem *item = (SPItem *) items->data;
570         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
571             continue;
573         SPCurve *curve = NULL;
574         if (SP_IS_SHAPE(item)) {
575             curve = sp_shape_get_curve(SP_SHAPE(item));
576             if (curve == NULL)
577                 continue;
578         }
579         if (SP_IS_TEXT(item)) {
580             curve = SP_TEXT(item)->getNormalizedBpath();
581             if (curve == NULL)
582                 continue;
583         }
585         {   // pas de stroke pas de chocolat
586             SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
587             gchar const *val = sp_repr_css_property(css, "stroke", NULL);
589             if (val == NULL || strcmp(val, "none") == 0) {
590                 sp_curve_unref(curve);
591                 continue;
592             }
593         }
595         // remember old stroke style, to be set on fill
596         SPCSSAttr *ncss;
597         {
598             SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
599             gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
600             gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
602             ncss = sp_repr_css_attr_new();
604             sp_repr_css_set_property(ncss, "stroke", "none");
605             sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
606             sp_repr_css_set_property(ncss, "fill", val);
607             if ( opac ) {
608                 sp_repr_css_set_property(ncss, "fill-opacity", opac);
609             } else {
610                 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
611             }
612             sp_repr_css_unset_property(ncss, "marker-start");
613             sp_repr_css_unset_property(ncss, "marker-mid");
614             sp_repr_css_unset_property(ncss, "marker-end");
615         }
617         NR::Matrix const transform(item->transform);
618         float const scale = transform.expansion();
619         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
620         SPStyle *i_style = SP_OBJECT(item)->style;
622         float o_width, o_miter;
623         JoinType o_join;
624         ButtType o_butt;
626         {
627             int jointype, captype;
629             jointype = i_style->stroke_linejoin.computed;
630             captype = i_style->stroke_linecap.computed;
631             o_width = i_style->stroke_width.computed;
633             switch (jointype) {
634                 case SP_STROKE_LINEJOIN_MITER:
635                     o_join = join_pointy;
636                     break;
637                 case SP_STROKE_LINEJOIN_ROUND:
638                     o_join = join_round;
639                     break;
640                 default:
641                     o_join = join_straight;
642                     break;
643             }
645             switch (captype) {
646                 case SP_STROKE_LINECAP_SQUARE:
647                     o_butt = butt_square;
648                     break;
649                 case SP_STROKE_LINECAP_ROUND:
650                     o_butt = butt_round;
651                     break;
652                 default:
653                     o_butt = butt_straight;
654                     break;
655             }
657             if (o_width < 0.1)
658                 o_width = 0.1;
659             o_miter = i_style->stroke_miterlimit.value * o_width;
660         }
662         Path *orig = Path_for_item(item, false);
663         if (orig == NULL) {
664             g_free(style);
665             sp_curve_unref(curve);
666             continue;
667         }
669         Path *res = new Path;
670         res->SetBackData(false);
672         if (i_style->stroke_dash.n_dash) {
673             // For dashed strokes, use Stroke method, because Outline can't do dashes
674             // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
676             orig->ConvertWithBackData(0.1);
678             orig->DashPolylineFromStyle(i_style, scale, 0);
680             Shape* theShape = new Shape;
681             orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
682                          0.5 * o_miter);
683             orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
685             Shape *theRes = new Shape;
687             theRes->ConvertToShape(theShape, fill_positive);
689             Path *originaux[1];
690             originaux[0] = res;
691             theRes->ConvertToForme(orig, 1, originaux);
693             res->Coalesce(5.0);
695             delete theShape;
696             delete theRes;
698         } else {
700             orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
702             orig->Coalesce(0.5 * o_width);
704             Shape *theShape = new Shape;
705             Shape *theRes = new Shape;
707             res->ConvertWithBackData(1.0);
708             res->Fill(theShape, 0);
709             theRes->ConvertToShape(theShape, fill_positive);
711             Path *originaux[1];
712             originaux[0] = res;
713             theRes->ConvertToForme(orig, 1, originaux);
715             delete theShape;
716             delete theRes;
717         }
719         if (orig->descr_cmd.size() <= 1) {
720             // ca a merd\8e, ou bien le resultat est vide
721             delete res;
722             delete orig;
723             g_free(style);
724             continue;
725         }
727         did = true;
729         // remember the position of the item
730         gint pos = SP_OBJECT_REPR(item)->position();
731         // remember parent
732         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
733         // remember id
734         char const *id = SP_OBJECT_REPR(item)->attribute("id");
736         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
738             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
740             // restore old style
741             repr->setAttribute("style", style);
743             // set old stroke style on fill
744             sp_repr_css_change(repr, ncss, "style");
746             sp_repr_css_attr_unref(ncss);
748             gchar *str = orig->svg_dump_path();
749             repr->setAttribute("d", str);
750             g_free(str);
753             if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
755                 Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
757                 // add the group to the parent
758                 parent->appendChild(g_repr);
759                 // move to the saved position
760                 g_repr->setPosition(pos > 0 ? pos : 0);
762                 g_repr->appendChild(repr);
763                 repr->setAttribute("id", id);
764                 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
765                 sp_item_write_transform(newitem, repr, transform);
767                 SPShape *shape = SP_SHAPE(item);
769                 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
770                     for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
771                         if (sp_shape_marker_required (shape, m, bp)) {
773                             SPMarker* marker = SP_MARKER (shape->marker[m]);
774                             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
776                             NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
778                             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
779                                 tr = NR::scale(i_style->stroke_width.computed) * tr;
780                             }
782                             // total marker transform
783                             tr = marker_item->transform * marker->c2p * tr * transform;
785                             if (SP_OBJECT_REPR(marker_item)) {
786                                 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
787                                 g_repr->appendChild(m_repr);
788                                 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
789                                 sp_item_write_transform(marker_item, m_repr, tr);
790                             }
791                         }
792                     }
793                 }
796                 selection->add(g_repr);
798                 Inkscape::GC::release(g_repr);
801             } else {
803                 // add the new repr to the parent
804                 parent->appendChild(repr);
806                 // move to the saved position
807                 repr->setPosition(pos > 0 ? pos : 0);
809                 repr->setAttribute("id", id);
811                 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
812                 sp_item_write_transform(newitem, repr, transform);
814                 selection->add(repr);
816             }
818             Inkscape::GC::release(repr);
820             sp_curve_unref(curve);
821             selection->remove(item);
822             SP_OBJECT(item)->deleteObject(false);
824         }
826         delete res;
827         delete orig;
828         g_free(style);
830     }
832     if (did) {
833         sp_document_done(sp_desktop_document(desktop));
834     } else {
835         // TRANSLATORS: "to outline" means "to convert stroke to path"
836         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> to outline in the selection."));
837         return;
838     }
842 void
843 sp_selected_path_offset()
845     double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
847     sp_selected_path_do_offset(true, prefOffset);
849 void
850 sp_selected_path_inset()
852     double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
854     sp_selected_path_do_offset(false, prefOffset);
857 void
858 sp_selected_path_offset_screen(double pixels)
860     sp_selected_path_do_offset(true,  pixels / SP_ACTIVE_DESKTOP->current_zoom());
863 void
864 sp_selected_path_inset_screen(double pixels)
866     sp_selected_path_do_offset(false,  pixels / SP_ACTIVE_DESKTOP->current_zoom());
870 void sp_selected_path_create_offset_object_zero()
872     sp_selected_path_create_offset_object(0, false);
875 void sp_selected_path_create_offset()
877     sp_selected_path_create_offset_object(1, false);
879 void sp_selected_path_create_inset()
881     sp_selected_path_create_offset_object(-1, false);
884 void sp_selected_path_create_updating_offset_object_zero()
886     sp_selected_path_create_offset_object(0, true);
889 void sp_selected_path_create_updating_offset()
891     sp_selected_path_create_offset_object(1, true);
893 void sp_selected_path_create_updating_inset()
895     sp_selected_path_create_offset_object(-1, true);
898 void
899 sp_selected_path_create_offset_object(int expand, bool updating)
901     Inkscape::Selection *selection;
902     Inkscape::XML::Node *repr;
903     SPItem *item;
904     SPCurve *curve;
905     gchar *style, *str;
906     SPDesktop *desktop;
907     float o_width, o_miter;
908     JoinType o_join;
909     ButtType o_butt;
911     curve = NULL;
913     desktop = SP_ACTIVE_DESKTOP;
915     selection = sp_desktop_selection(desktop);
917     item = selection->singleItem();
919     if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
920         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
921         return;
922     }
923     if (SP_IS_SHAPE(item))
924     {
925         curve = sp_shape_get_curve(SP_SHAPE(item));
926         if (curve == NULL)
927             return;
928     }
929     if (SP_IS_TEXT(item))
930     {
931         curve = SP_TEXT(item)->getNormalizedBpath();
932         if (curve == NULL)
933             return;
934     }
936     NR::Matrix const transform(item->transform);
938     sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
940     style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
942     // remember the position of the item
943     gint pos = SP_OBJECT_REPR(item)->position();
944     // remember parent
945     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
947     {
948         SPStyle *i_style = SP_OBJECT(item)->style;
949         int jointype, captype;
951         jointype = i_style->stroke_linejoin.value;
952         captype = i_style->stroke_linecap.value;
953         o_width = i_style->stroke_width.computed;
954         if (jointype == SP_STROKE_LINEJOIN_MITER)
955         {
956             o_join = join_pointy;
957         }
958         else if (jointype == SP_STROKE_LINEJOIN_ROUND)
959         {
960             o_join = join_round;
961         }
962         else
963         {
964             o_join = join_straight;
965         }
966         if (captype == SP_STROKE_LINECAP_SQUARE)
967         {
968             o_butt = butt_square;
969         }
970         else if (captype == SP_STROKE_LINECAP_ROUND)
971         {
972             o_butt = butt_round;
973         }
974         else
975         {
976             o_butt = butt_straight;
977         }
979         {
980             double prefOffset = 1.0;
981             prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
982             o_width = prefOffset;
983         }
985         if (o_width < 0.01)
986             o_width = 0.01;
987         o_miter = i_style->stroke_miterlimit.value * o_width;
988     }
990     Path *orig = Path_for_item(item, true, false);
991     if (orig == NULL)
992     {
993         g_free(style);
994         sp_curve_unref(curve);
995         return;
996     }
998     Path *res = new Path;
999     res->SetBackData(false);
1001     {
1002         Shape *theShape = new Shape;
1003         Shape *theRes = new Shape;
1005         orig->ConvertWithBackData(1.0);
1006         orig->Fill(theShape, 0);
1008         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1009         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1010         if (val && strcmp(val, "nonzero") == 0)
1011         {
1012             theRes->ConvertToShape(theShape, fill_nonZero);
1013         }
1014         else if (val && strcmp(val, "evenodd") == 0)
1015         {
1016             theRes->ConvertToShape(theShape, fill_oddEven);
1017         }
1018         else
1019         {
1020             theRes->ConvertToShape(theShape, fill_nonZero);
1021         }
1023         Path *originaux[1];
1024         originaux[0] = orig;
1025         theRes->ConvertToForme(res, 1, originaux);
1027         delete theShape;
1028         delete theRes;
1029     }
1031     sp_curve_unref(curve);
1033     if (res->descr_cmd.size() <= 1)
1034     {
1035         // pas vraiment de points sur le resultat
1036         // donc il ne reste rien
1037         sp_document_done(sp_desktop_document(desktop));
1038         selection->clear();
1040         delete res;
1041         delete orig;
1042         g_free(style);
1043         return;
1044     }
1046     {
1047         gchar tstr[80];
1049         tstr[79] = '\0';
1051         repr = sp_repr_new("svg:path");
1052         repr->setAttribute("sodipodi:type", "inkscape:offset");
1053         sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1054                                                           ? o_width
1055                                                           : expand < 0
1056                                                           ? -o_width
1057                                                           : 0 ));
1059         str = res->svg_dump_path();
1060         repr->setAttribute("inkscape:original", str);
1061         g_free(str);
1063         if ( updating ) {
1064             char const *id = SP_OBJECT(item)->repr->attribute("id");
1065             char const *uri = g_strdup_printf("#%s", id);
1066             repr->setAttribute("xlink:href", uri);
1067             g_free((void *) uri);
1068         } else {
1069             repr->setAttribute("inkscape:href", NULL);
1070         }
1072         repr->setAttribute("style", style);
1074         // add the new repr to the parent
1075         parent->appendChild(repr);
1077         // move to the saved position
1078         repr->setPosition(pos > 0 ? pos : 0);
1080         SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1082         if ( updating ) {
1083             // on conserve l'original
1084             // we reapply the transform to the original (offset will feel it)
1085             sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1086         } else {
1087             // delete original, apply the transform to the offset
1088             SP_OBJECT(item)->deleteObject(false);
1089             sp_item_write_transform(nitem, repr, transform);
1090         }
1092         // The object just created from a temporary repr is only a seed.
1093         // We need to invoke its write which will update its real repr (in particular adding d=)
1094         SP_OBJECT(nitem)->updateRepr();
1096         Inkscape::GC::release(repr);
1098         selection->set(nitem);
1099     }
1101     sp_document_done(sp_desktop_document(desktop));
1103     delete res;
1104     delete orig;
1106     g_free(style);
1120 void
1121 sp_selected_path_do_offset(bool expand, double prefOffset)
1123     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1125     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1127     if (selection->isEmpty()) {
1128         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1129         return;
1130     }
1132     bool did = false;
1134     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1135          items != NULL;
1136          items = items->next) {
1138         SPItem *item = (SPItem *) items->data;
1140         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1141             continue;
1143         SPCurve *curve = NULL;
1144         if (SP_IS_SHAPE(item)) {
1145             curve = sp_shape_get_curve(SP_SHAPE(item));
1146             if (curve == NULL)
1147                 continue;
1148         }
1149         if (SP_IS_TEXT(item)) {
1150             curve = SP_TEXT(item)->getNormalizedBpath();
1151             if (curve == NULL)
1152                 continue;
1153         }
1155         NR::Matrix const transform(item->transform);
1157         sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1159         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1161         float o_width, o_miter;
1162         JoinType o_join;
1163         ButtType o_butt;
1165         {
1166             SPStyle *i_style = SP_OBJECT(item)->style;
1167             int jointype, captype;
1169             jointype = i_style->stroke_linejoin.value;
1170             captype = i_style->stroke_linecap.value;
1171             o_width = i_style->stroke_width.computed;
1173             switch (jointype) {
1174                 case SP_STROKE_LINEJOIN_MITER:
1175                     o_join = join_pointy;
1176                     break;
1177                 case SP_STROKE_LINEJOIN_ROUND:
1178                     o_join = join_round;
1179                     break;
1180                 default:
1181                     o_join = join_straight;
1182                     break;
1183             }
1185             switch (captype) {
1186                 case SP_STROKE_LINECAP_SQUARE:
1187                     o_butt = butt_square;
1188                     break;
1189                 case SP_STROKE_LINECAP_ROUND:
1190                     o_butt = butt_round;
1191                     break;
1192                 default:
1193                     o_butt = butt_straight;
1194                     break;
1195             }
1197             o_width = prefOffset;
1199             if (o_width < 0.1)
1200                 o_width = 0.1;
1201             o_miter = i_style->stroke_miterlimit.value * o_width;
1202         }
1204         Path *orig = Path_for_item(item, false);
1205         if (orig == NULL) {
1206             g_free(style);
1207             sp_curve_unref(curve);
1208             continue;
1209         }
1211         Path *res = new Path;
1212         res->SetBackData(false);
1214         {
1215             Shape *theShape = new Shape;
1216             Shape *theRes = new Shape;
1218             orig->ConvertWithBackData(0.03);
1219             orig->Fill(theShape, 0);
1221             SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1222             gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1223             if (val && strcmp(val, "nonzero") == 0)
1224             {
1225                 theRes->ConvertToShape(theShape, fill_nonZero);
1226             }
1227             else if (val && strcmp(val, "evenodd") == 0)
1228             {
1229                 theRes->ConvertToShape(theShape, fill_oddEven);
1230             }
1231             else
1232             {
1233                 theRes->ConvertToShape(theShape, fill_nonZero);
1234             }
1236             // et maintenant: offset
1237             // methode inexacte
1238 /*                      Path *originaux[1];
1239                         originaux[0] = orig;
1240                         theRes->ConvertToForme(res, 1, originaux);
1242                         if (expand) {
1243                         res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1244                         } else {
1245                         res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1246                         }
1248                         orig->ConvertWithBackData(1.0);
1249                         orig->Fill(theShape, 0);
1250                         theRes->ConvertToShape(theShape, fill_positive);
1251                         originaux[0] = orig;
1252                         theRes->ConvertToForme(res, 1, originaux);
1254                         if (o_width >= 0.5) {
1255                         //     res->Coalesce(1.0);
1256                         res->ConvertEvenLines(1.0);
1257                         res->Simplify(1.0);
1258                         } else {
1259                         //      res->Coalesce(o_width);
1260                         res->ConvertEvenLines(1.0*o_width);
1261                         res->Simplify(1.0 * o_width);
1262                         }    */
1263             // methode par makeoffset
1265             if (expand)
1266             {
1267                 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1268             }
1269             else
1270             {
1271                 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1272             }
1273             theRes->ConvertToShape(theShape, fill_positive);
1275             res->Reset();
1276             theRes->ConvertToForme(res);
1278             if (o_width >= 1.0)
1279             {
1280                 res->ConvertEvenLines(1.0);
1281                 res->Simplify(1.0);
1282             }
1283             else
1284             {
1285                 res->ConvertEvenLines(1.0*o_width);
1286                 res->Simplify(1.0 * o_width);
1287             }
1289             delete theShape;
1290             delete theRes;
1291         }
1293         did = true;
1295         sp_curve_unref(curve);
1296         // remember the position of the item
1297         gint pos = SP_OBJECT_REPR(item)->position();
1298         // remember parent
1299         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1300         // remember id
1301         char const *id = SP_OBJECT_REPR(item)->attribute("id");
1303         selection->remove(item);
1304         SP_OBJECT(item)->deleteObject(false);
1306         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1308             gchar tstr[80];
1310             tstr[79] = '\0';
1312             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1314             repr->setAttribute("style", style);
1316             gchar *str = res->svg_dump_path();
1317             repr->setAttribute("d", str);
1318             g_free(str);
1320             // add the new repr to the parent
1321             parent->appendChild(repr);
1323             // move to the saved position
1324             repr->setPosition(pos > 0 ? pos : 0);
1326             SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1328             // reapply the transform
1329             sp_item_write_transform(newitem, repr, transform);
1331             repr->setAttribute("id", id);
1333             selection->add(repr);
1335             Inkscape::GC::release(repr);
1336         }
1338         delete orig;
1339         delete res;
1340     }
1342     if (did) {
1343         sp_document_done(sp_desktop_document(desktop));
1344     } else {
1345         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1346         return;
1347     }
1352 //return true if we changed something, else false
1353 bool
1354 sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item,
1355                  float threshold,  bool justCoalesce,
1356                  float angleLimit, bool breakableAngles,
1357                  gdouble size,     bool modifySelection)
1359     if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1360         return false;
1362     //If this is a group, do the children instead
1363     if (SP_IS_GROUP(item)) {
1365         bool didSomething = false;
1367         for ( GSList *children = sp_item_group_item_list(SP_GROUP(item));
1368                  children  ; children = children->next) {
1370             SPItem *child = (SPItem *) children->data;
1371             didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce,
1372                    angleLimit, breakableAngles, size, false);
1373         }
1375         return didSomething;
1376     }
1379     SPCurve *curve = NULL;
1381     if (SP_IS_SHAPE(item)) {
1382         curve = sp_shape_get_curve(SP_SHAPE(item));
1383         if (!curve)
1384             return false;
1385     }
1387     if (SP_IS_TEXT(item)) {
1388         curve = SP_TEXT(item)->getNormalizedBpath();
1389         if (!curve)
1390             return false;
1391     }
1393     // save the transform, to re-apply it after simplification
1394     NR::Matrix const transform(item->transform);
1396     /*
1397        reset the transform, effectively transforming the item by transform.inverse();
1398        this is necessary so that the item is transformed twice back and forth,
1399        allowing all compensations to cancel out regardless of the preferences
1400     */
1401     sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1403     gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1405     Path *orig = Path_for_item(item, false);
1406     if (orig == NULL) {
1407         g_free(style);
1408         sp_curve_unref(curve);
1409         return false;
1410     }
1412     sp_curve_unref(curve);
1413     // remember the position of the item
1414     gint pos = SP_OBJECT_REPR(item)->position();
1415     // remember parent
1416     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1417     // remember id
1418     char const *id = SP_OBJECT_REPR(item)->attribute("id");
1420     //If a group was selected, to not change the selection list
1421     if (modifySelection)
1422         selection->remove(item);
1424     SP_OBJECT(item)->deleteObject(false);
1426     if ( justCoalesce ) {
1427         orig->Coalesce(threshold * size);
1428     } else {
1429         orig->ConvertEvenLines(threshold * size);
1430         orig->Simplify(threshold * size);
1431     }
1433     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1435     repr->setAttribute("style", style);
1437     gchar *str = orig->svg_dump_path();
1438     repr->setAttribute("d", str);
1439     g_free(str);
1441     // restore id
1442     repr->setAttribute("id", id);
1444     // add the new repr to the parent
1445     parent->appendChild(repr);
1447     // move to the saved position
1448     repr->setPosition(pos > 0 ? pos : 0);
1450     SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1452     // reapply the transform
1453     sp_item_write_transform(newitem, repr, transform);
1455     //If we are not in a selected group
1456     if (modifySelection)
1457         selection->add(repr);
1459     Inkscape::GC::release(repr);
1461     // clean up
1462     if (orig) delete orig;
1464     return true;
1468 void
1469 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1470                        float angleLimit, bool breakableAngles)
1472     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1474     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1476     if (selection->isEmpty()) {
1477         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1478                          _("Select <b>path(s)</b> to simplify."));
1479         return;
1480     }
1482     // remember selection size
1483     NR::Rect bbox = selection->bounds();
1484     gdouble size  = L2(bbox.dimensions());
1486     bool didSomething = false;
1488     //Loop through all of the items in the selection
1489     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1490                         items != NULL; items = items->next) {
1492         SPItem *item = (SPItem *) items->data;
1494         if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1495             continue;
1497         didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1498                            threshold, justCoalesce, angleLimit, breakableAngles, size, true);
1499     }
1502     if (didSomething)
1503         sp_document_done(sp_desktop_document(desktop));
1504     else
1505         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1510 // globals for keeping track of accelerated simplify
1511 static double previousTime      = 0.0;
1512 static gdouble simplifyMultiply = 1.0;
1514 void
1515 sp_selected_path_simplify(void)
1517     gdouble simplifyThreshold =
1518         prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1519     bool simplifyJustCoalesce =
1520         (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1522     //Get the current time
1523     GTimeVal currentTimeVal;
1524     g_get_current_time(&currentTimeVal);
1525     double currentTime = currentTimeVal.tv_sec * 1000000 +
1526                 currentTimeVal.tv_usec;
1528     //Was the previous call to this function recent? (<0.5 sec)
1529     if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1531         // add to the threshold 1/2 of its original value
1532         simplifyMultiply  += 0.5;
1533         simplifyThreshold *= simplifyMultiply;
1535     } else {
1536         // reset to the default
1537         simplifyMultiply = 1;
1538     }
1540     //remember time for next call
1541     previousTime = currentTime;
1543     //g_print("%g\n", simplify_threshold);
1545     //Make the actual call
1546     sp_selected_path_simplify_selection(simplifyThreshold,
1547                       simplifyJustCoalesce, 0.0, false);
1552 // fonctions utilitaires
1554 bool
1555 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1557     if (who == NULL || a == NULL)
1558         return false;
1559     if (who == a)
1560         return true;
1561     return Ancetre(sp_repr_parent(a), who);
1564 Path *
1565 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1567     SPCurve *curve;
1569     if (!item)
1570         return NULL;
1572     if (SP_IS_SHAPE(item))
1573     {
1574         curve = sp_shape_get_curve(SP_SHAPE(item));
1575     }
1576     else if (SP_IS_TEXT(item))
1577     {
1578         curve = SP_TEXT(item)->getNormalizedBpath();
1579     }
1580     else
1581     {
1582         curve = NULL;
1583     }
1585     if (!curve)
1586         return NULL;
1587     NArtBpath *bpath = SP_CURVE_BPATH(curve);
1588     if (bpath == NULL)
1589         return NULL;
1591     if ( doTransformation ) {
1592         if (transformFull)
1593             bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1594         else
1595             bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1596         sp_curve_unref(curve);
1597         curve=NULL;
1598     } else {
1599         bpath=SP_CURVE_BPATH(curve);
1600     }
1602     Path *dest = new Path;
1603     dest->SetBackData(false);
1604     {
1605         int   i;
1606         bool  closed = false;
1607         float lastX  = 0.0;
1608         float lastY  = 0.0;
1610         for (i = 0; bpath[i].code != NR_END; i++) {
1611             switch (bpath[i].code) {
1612                 case NR_LINETO:
1613                     lastX = bpath[i].x3;
1614                     lastY = bpath[i].y3;
1615                     {
1616                         NR::Point tmp(lastX, lastY);
1617                         dest->LineTo(tmp);
1618                     }
1619                     break;
1621                 case NR_CURVETO:
1622                 {
1623                     NR::Point tmp, tms, tme;
1624                     tmp[0]=bpath[i].x3;
1625                     tmp[1]=bpath[i].y3;
1626                     tms[0]=3 * (bpath[i].x1 - lastX);
1627                     tms[1]=3 * (bpath[i].y1 - lastY);
1628                     tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1629                     tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1630                     dest->CubicTo(tmp,tms,tme);
1631                 }
1632                 lastX = bpath[i].x3;
1633                 lastY = bpath[i].y3;
1634                 break;
1636                 case NR_MOVETO_OPEN:
1637                 case NR_MOVETO:
1638                     if (closed)
1639                         dest->Close();
1640                     closed = (bpath[i].code == NR_MOVETO);
1641                     lastX = bpath[i].x3;
1642                     lastY = bpath[i].y3;
1643                     {
1644                         NR::Point  tmp(lastX, lastY);
1645                         dest->MoveTo(tmp);
1646                     }
1647                     break;
1648                 default:
1649                     break;
1650             }
1651         }
1652         if (closed)
1653             dest->Close();
1654     }
1656     if ( doTransformation ) {
1657         if ( bpath ) g_free(bpath);
1658     } else {
1659         sp_curve_unref(curve);
1660     }
1661     return dest;
1664 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1666     //get nearest position on path
1667     Path::cut_position pos = path->PointToCurvilignPosition(p);
1668     return pos;
1671 NR::Point get_point_on_Path(Path *path, int piece, double t)
1673     NR::Point p;
1674     path->PointAt(piece, t, p);
1675     return p;
1679 /*
1680   Local Variables:
1681   mode:c++
1682   c-file-style:"stroustrup"
1683   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1684   indent-tabs-mode:nil
1685   fill-column:99
1686   End:
1687 */
1688 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :