Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / splivarot.cpp
1 /*
2  *  splivarot.cpp
3  *  Inkscape
4  *
5  *  Created by fred on Fri Dec 05 2003.
6  *  tweaked endlessly by bulia byak <buliabyak@users.sf.net>
7  *  public domain
8  *
9  */
11 /*
12  * contains lots of stitched pieces of path-chemistry.c
13  */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <cstring>
20 #include <string>
21 #include <vector>
22 #include <glib/gmem.h>
23 #include "xml/repr.h"
24 #include "svg/svg.h"
25 #include "sp-path.h"
26 #include "sp-shape.h"
27 #include "sp-image.h"
28 #include "marker.h"
29 #include "enums.h"
30 #include "sp-text.h"
31 #include "sp-flowtext.h"
32 #include "text-editing.h"
33 #include "sp-item-group.h"
34 #include "style.h"
35 #include "document.h"
36 #include "message-stack.h"
37 #include "selection.h"
38 #include "desktop-handles.h"
39 #include "desktop.h"
40 #include "display/canvas-bpath.h"
41 #include "display/curve.h"
42 #include <glibmm/i18n.h>
43 #include "preferences.h"
45 #include "xml/repr.h"
46 #include "xml/repr-sorting.h"
47 #include <2geom/pathvector.h>
48 #include <libnr/nr-scale-matrix-ops.h>
49 #include "helper/geom.h"
51 #include "livarot/Path.h"
52 #include "livarot/Shape.h"
54 #include "splivarot.h"
56 using Inkscape::DocumentUndo;
58 bool   Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
60 void sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
61 void sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset);
62 void sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating);
64 void
65 sp_selected_path_union(SPDesktop *desktop)
66 {
67     sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
68 }
70 void
71 sp_selected_path_union_skip_undo(SPDesktop *desktop)
72 {
73     sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_NONE, _("Union"));
74 }
76 void
77 sp_selected_path_intersect(SPDesktop *desktop)
78 {
79     sp_selected_path_boolop(desktop, bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
80 }
82 void
83 sp_selected_path_diff(SPDesktop *desktop)
84 {
85     sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
86 }
88 void
89 sp_selected_path_diff_skip_undo(SPDesktop *desktop)
90 {
91     sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_NONE, _("Difference"));
92 }
94 void
95 sp_selected_path_symdiff(SPDesktop *desktop)
96 {
97     sp_selected_path_boolop(desktop, bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
98 }
99 void
100 sp_selected_path_cut(SPDesktop *desktop)
102     sp_selected_path_boolop(desktop, bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
104 void
105 sp_selected_path_slice(SPDesktop *desktop)
107     sp_selected_path_boolop(desktop, bool_op_slice, SP_VERB_SELECTION_SLICE,  _("Cut path"));
111 // boolean operations
112 // take the source paths from the file, do the operation, delete the originals and add the results
113 void
114 sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb, const Glib::ustring description)
116     Inkscape::Selection *selection = sp_desktop_selection(desktop);
117     
118     GSList *il = (GSList *) selection->itemList();
119     
120     // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
121     if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
122         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
123         return;
124     }
125     else if ( g_slist_length(il) < 1 ) {
126         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
127         return;
128     }
130     if (g_slist_length(il) > 2) {
131         if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
132             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, division, or path cut."));
133             return;
134         }
135     }
137     // reverseOrderForOp marks whether the order of the list is the top->down order
138     // it's only used when there are 2 objects, and for operations who need to know the
139     // topmost object (differences, cuts)
140     bool reverseOrderForOp = false;
142     if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice) {
143         // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
144         Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
145         Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
147         if (a == NULL || b == 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         if (Ancetre(a, b)) {
153             // a is the parent of b, already in the proper order
154         } else if (Ancetre(b, a)) {
155             // reverse order
156             reverseOrderForOp = true;
157         } else {
159             // objects are not in parent/child relationship;
160             // find their lowest common ancestor
161             Inkscape::XML::Node *dad = LCA(a, b);
162             if (dad == NULL) {
163                 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."));
164                 return;
165             }
167             // find the children of the LCA that lead from it to the a and b
168             Inkscape::XML::Node *as = AncetreFils(a, dad);
169             Inkscape::XML::Node *bs = AncetreFils(b, dad);
171             // find out which comes first
172             for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
173                 if (child == as) {
174                     /* a first, so reverse. */
175                     reverseOrderForOp = true;
176                     break;
177                 }
178                 if (child == bs)
179                     break;
180             }
181         }
182     }
184     il = g_slist_copy(il);
186     // first check if all the input objects have shapes
187     // otherwise bail out
188     for (GSList *l = il; l != NULL; l = l->next)
189     {
190         SPItem *item = SP_ITEM(l->data);
191         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item))
192         {
193             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
194             g_slist_free(il);
195             return;
196         }
197     }
199     // extract the livarot Paths from the source objects
200     // also get the winding rule specified in the style
201     int nbOriginaux = g_slist_length(il);
202     std::vector<Path *> originaux(nbOriginaux);
203     std::vector<FillRule> origWind(nbOriginaux);
204     int curOrig;
205     {
206         curOrig = 0;
207         for (GSList *l = il; l != NULL; l = l->next)
208         {
209             SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
210             gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
211             if (val && strcmp(val, "nonzero") == 0) {
212                 origWind[curOrig]= fill_nonZero;
213             } else if (val && strcmp(val, "evenodd") == 0) {
214                 origWind[curOrig]= fill_oddEven;
215             } else {
216                 origWind[curOrig]= fill_nonZero;
217             }
219             originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
220             if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
221             {
222                 for (int i = curOrig; i >= 0; i--) delete originaux[i];
223                 g_slist_free(il);
224                 return;
225             }
226             curOrig++;
227         }
228     }
229     // reverse if needed
230     // note that the selection list keeps its order
231     if ( reverseOrderForOp ) {
232         Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
233         FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
234     }
236     // and work
237     // some temporary instances, first
238     Shape *theShapeA = new Shape;
239     Shape *theShapeB = new Shape;
240     Shape *theShape = new Shape;
241     Path *res = new Path;
242     res->SetBackData(false);
243     Path::cut_position  *toCut=NULL;
244     int                  nbToCut=0;
246     if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
247         // true boolean op
248         // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
249         originaux[0]->ConvertWithBackData(0.1);
251         originaux[0]->Fill(theShape, 0);
253         theShapeA->ConvertToShape(theShape, origWind[0]);
255         curOrig = 1;
256         for (GSList *l = il->next; l != NULL; l = l->next) {
257             originaux[curOrig]->ConvertWithBackData(0.1);
259             originaux[curOrig]->Fill(theShape, curOrig);
261             theShapeB->ConvertToShape(theShape, origWind[curOrig]);
263             // les elements arrivent en ordre inverse dans la liste
264             theShape->Booleen(theShapeB, theShapeA, bop);
266             {
267                 Shape *swap = theShape;
268                 theShape = theShapeA;
269                 theShapeA = swap;
270             }
271             curOrig++;
272         }
274         {
275             Shape *swap = theShape;
276             theShape = theShapeA;
277             theShapeA = swap;
278         }
280     } else if ( bop == bool_op_cut ) {
281         // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
282         // technically, the cut path is not necessarily a polygon (thus has no winding rule)
283         // it is just uncrossed, and cleaned from duplicate edges and points
284         // then it's fed to Booleen() which will uncross it against the other path
285         // then comes the trick: each edge of the cut path is duplicated (one in each direction),
286         // thus making a polygon. the weight of the edges of the cut are all 0, but
287         // the Booleen need to invert the ones inside the source polygon (for the subsequent
288         // ConvertToForme)
290         // the cut path needs to have the highest pathID in the back data
291         // that's how the Booleen() function knows it's an edge of the cut
293         // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
294         // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
295         // left it at 1.0. Investigate replacing this by a combination of difference and
296         // intersection of the same two paths. -- bb
297         {
298             Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
299             int   swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
300         }
301         originaux[0]->ConvertWithBackData(1.0);
303         originaux[0]->Fill(theShape, 0);
305         theShapeA->ConvertToShape(theShape, origWind[0]);
307         originaux[1]->ConvertWithBackData(1.0);
309         originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
311         theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
313         // les elements arrivent en ordre inverse dans la liste
314         theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
316     } else if ( bop == bool_op_slice ) {
317         // slice is not really a boolean operation
318         // you just put the 2 shapes in a single polygon, uncross it
319         // the points where the degree is > 2 are intersections
320         // just check it's an intersection on the path you want to cut, and keep it
321         // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
322         // make new subpath at each one of these positions
323         // inversion pour l'op\8eration
324         {
325             Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
326             int   swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
327         }
328         originaux[0]->ConvertWithBackData(1.0);
330         originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
332         originaux[1]->ConvertWithBackData(1.0);
334         originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
336         theShape->ConvertToShape(theShapeA, fill_justDont);
338         if ( theShape->hasBackData() ) {
339             // should always be the case, but ya never know
340             {
341                 for (int i = 0; i < theShape->numberOfPoints(); i++) {
342                     if ( theShape->getPoint(i).totalDegree() > 2 ) {
343                         // possibly an intersection
344                         // we need to check that at least one edge from the source path is incident to it
345                         // before we declare it's an intersection
346                         int cb = theShape->getPoint(i).incidentEdge[FIRST];
347                         int   nbOrig=0;
348                         int   nbOther=0;
349                         int   piece=-1;
350                         float t=0.0;
351                         while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
352                             if ( theShape->ebData[cb].pathID == 0 ) {
353                                 // the source has an edge incident to the point, get its position on the path
354                                 piece=theShape->ebData[cb].pieceID;
355                                 if ( theShape->getEdge(cb).st == i ) {
356                                     t=theShape->ebData[cb].tSt;
357                                 } else {
358                                     t=theShape->ebData[cb].tEn;
359                                 }
360                                 nbOrig++;
361                             }
362                             if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
363                             cb=theShape->NextAt(i, cb);
364                         }
365                         if ( nbOrig > 0 && nbOther > 0 ) {
366                             // point incident to both path and cut: an intersection
367                             // note that you only keep one position on the source; you could have degenerate
368                             // cases where the source crosses itself at this point, and you wouyld miss an intersection
369                             toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
370                             toCut[nbToCut].piece=piece;
371                             toCut[nbToCut].t=t;
372                             nbToCut++;
373                         }
374                     }
375                 }
376             }
377             {
378                 // i think it's useless now
379                 int i = theShape->numberOfEdges() - 1;
380                 for (;i>=0;i--) {
381                     if ( theShape->ebData[i].pathID == 1 ) {
382                         theShape->SubEdge(i);
383                     }
384                 }
385             }
387         }
388     }
390     int*    nesting=NULL;
391     int*    conts=NULL;
392     int     nbNest=0;
393     // pour compenser le swap juste avant
394     if ( bop == bool_op_slice ) {
395 //    theShape->ConvertToForme(res, nbOriginaux, originaux, true);
396 //    res->ConvertForcedToMoveTo();
397         res->Copy(originaux[0]);
398         res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
399         free(toCut);
400     } else if ( bop == bool_op_cut ) {
401         // il faut appeler pour desallouer PointData (pas vital, mais bon)
402         // the Booleen() function did not deallocated the point_data array in theShape, because this
403         // function needs it.
404         // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
405         // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
406         theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
407     } else {
408         theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
409     }
411     delete theShape;
412     delete theShapeA;
413     delete theShapeB;
414     for (int i = 0; i < nbOriginaux; i++)  delete originaux[i];
416     if (res->descr_cmd.size() <= 1)
417     {
418         // only one command, presumably a moveto: it isn't a path
419         for (GSList *l = il; l != NULL; l = l->next)
420         {
421             SP_OBJECT(l->data)->deleteObject();
422         }
423         DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_NONE, 
424                            description);
425         selection->clear();
427         delete res;
428         g_slist_free(il);
429         return;
430     }
432     // get the source path object
433     SPObject *source;
434     if ( bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
435         if (reverseOrderForOp) {
436              source = SP_OBJECT(il->data);
437         } else {
438              source = SP_OBJECT(il->next->data);
439         }
440     } else {
441         // find out the bottom object
442         GSList *sorted = g_slist_copy((GSList *) selection->reprList());
444         sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
446         source = sp_desktop_document(desktop)->
447             getObjectByRepr((Inkscape::XML::Node *)sorted->data);
449         g_slist_free(sorted);
450     }
452     // adjust style properties that depend on a possible transform in the source object in order
453     // to get a correct style attribute for the new path
454     SPItem* item_source = SP_ITEM(source);
455     Geom::Matrix i2doc(item_source->i2doc_affine());
456     item_source->adjust_stroke(i2doc.descrim());
457     item_source->adjust_pattern(i2doc);
458     item_source->adjust_gradient(i2doc);
459     item_source->adjust_livepatheffect(i2doc);
461     Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
463     // remember important aspects of the source path, to be restored
464     gint pos = repr_source->position();
465     Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
466     gchar const *id = repr_source->attribute("id");
467     gchar const *style = repr_source->attribute("style");
468     gchar const *mask = repr_source->attribute("mask");
469     gchar const *clip_path = repr_source->attribute("clip-path");
470     gchar *title = source->title();
471     gchar *desc = source->desc();
472     // remove source paths
473     selection->clear();
474     for (GSList *l = il; l != NULL; l = l->next) {
475         // if this is the bottommost object,
476         if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
477             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
478             SP_OBJECT(l->data)->deleteObject(false);
479         } else {
480             // delete the object for real, so that its clones can take appropriate action
481             SP_OBJECT(l->data)->deleteObject();
482         }
483     }
484     g_slist_free(il);
486     // premultiply by the inverse of parent's repr
487     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
488     Geom::Matrix local (parent_item->i2doc_affine());
489     gchar *transform = sp_svg_transform_write(local.inverse());
491     // now that we have the result, add it on the canvas
492     if ( bop == bool_op_cut || bop == bool_op_slice ) {
493         int    nbRP=0;
494         Path** resPath;
495         if ( bop == bool_op_slice ) {
496             // there are moveto's at each intersection, but it's still one unique path
497             // so break it down and add each subpath independently
498             // we could call break_apart to do this, but while we have the description...
499             resPath=res->SubPaths(nbRP, false);
500         } else {
501             // cut operation is a bit wicked: you need to keep holes
502             // that's why you needed the nesting
503             // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
504             // to get the path for each part of the polygon. that's why you need the nesting info:
505             // to know in wich subpath to add a subpath
506             resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
508             // cleaning
509             if ( conts ) free(conts);
510             if ( nesting ) free(nesting);
511         }
513         // add all the pieces resulting from cut or slice
514         for (int i=0;i<nbRP;i++) {
515             gchar *d = resPath[i]->svg_dump_path();
517             Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
518             Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
519             repr->setAttribute("style", style);
520             if (mask)
521                 repr->setAttribute("mask", mask);
522             if (clip_path)
523                 repr->setAttribute("clip-path", clip_path);
525             repr->setAttribute("d", d);
526             g_free(d);
528             // for slice, remove fill
529             if (bop == bool_op_slice) {
530                 SPCSSAttr *css;
532                 css = sp_repr_css_attr_new();
533                 sp_repr_css_set_property(css, "fill", "none");
535                 sp_repr_css_change(repr, css, "style");
537                 sp_repr_css_attr_unref(css);
538             }
540             // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
541             // this means it's basically random which of the pieces inherits the original's id and clones
542             // a better algorithm might figure out e.g. the biggest piece
543             repr->setAttribute("id", id);
545             repr->setAttribute("transform", transform);
547             // add the new repr to the parent
548             parent->appendChild(repr);
550             // move to the saved position
551             repr->setPosition(pos > 0 ? pos : 0);
553             selection->add(repr);
554             Inkscape::GC::release(repr);
556             delete resPath[i];
557         }
558         if ( resPath ) free(resPath);
560     } else {
561         gchar *d = res->svg_dump_path();
563         Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
564         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
565         repr->setAttribute("style", style);
567         if ( mask )
568             repr->setAttribute("mask", mask);
570         if ( clip_path )
571             repr->setAttribute("clip-path", clip_path);
573         repr->setAttribute("d", d);
574         g_free(d);
576         repr->setAttribute("transform", transform);
578         repr->setAttribute("id", id);
579         parent->appendChild(repr);
580         if (title) {
581                 sp_desktop_document(desktop)->getObjectByRepr(repr)->setTitle(title);
582         }            
583         if (desc) {
584                 sp_desktop_document(desktop)->getObjectByRepr(repr)->setDesc(desc);
585         }
586                 repr->setPosition(pos > 0 ? pos : 0);
588         selection->add(repr);
589         Inkscape::GC::release(repr);
590     }
592     g_free(transform);
593     if (title) g_free(title);
594     if (desc) g_free(desc);
596     if (verb != SP_VERB_NONE) {
597         DocumentUndo::done(sp_desktop_document(desktop), verb, description);
598     }
600     delete res;
603 static
604 void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix marker_transform,
605                                           Geom::Scale stroke_scale, Geom::Matrix transform,
606                                           Inkscape::XML::Node *g_repr, Inkscape::XML::Document *xml_doc, SPDocument * doc )
608     SPMarker* marker = SP_MARKER (marker_object);
609     SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker_object));
611     Geom::Matrix tr(marker_transform);
613     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
614         tr = stroke_scale * tr;
615     }
617     // total marker transform
618     tr = marker_item->transform * marker->c2p * tr * transform;
620     if (SP_OBJECT_REPR(marker_item)) {
621         Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
622         g_repr->appendChild(m_repr);
623         SPItem *marker_item = (SPItem *) doc->getObjectByRepr(m_repr);
624         marker_item->doWriteTransform(m_repr, tr);
625     }
628 static
629 void item_outline_add_marker( SPObject const *marker_object, Geom::Matrix marker_transform,
630                               Geom::Scale stroke_scale, Geom::PathVector* pathv_in )
632     SPMarker* marker = SP_MARKER (marker_object);
633     SPItem* marker_item = sp_item_first_item_child(SP_OBJECT(marker_object));
635     Geom::Matrix tr(marker_transform);
636     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
637         tr = stroke_scale * tr;
638     }
639     // total marker transform
640     tr = marker_item->transform * marker->c2p * tr;
642     Geom::PathVector* marker_pathv = item_outline(marker_item);
643     
644     if (marker_pathv) {
645         for (unsigned int j=0; j < marker_pathv->size(); j++) {
646             pathv_in->push_back((*marker_pathv)[j] * tr);
647         }
648         delete marker_pathv;
649     }
652 /**
653  *  Returns a pathvector that is the outline of the stroked item, with markers.
654  *  item must be SPShape of SPText.
655  */
656 Geom::PathVector* item_outline(SPItem const *item)
658     Geom::PathVector *ret_pathv = NULL;
660     if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
661         return ret_pathv;
663     // no stroke: no outline
664     if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
665         return ret_pathv;
666     }
668     SPCurve *curve = NULL;
669     if (SP_IS_SHAPE(item)) {
670         curve = SP_SHAPE(item)->getCurve();
671     } else if (SP_IS_TEXT(item)) {
672         curve = SP_TEXT(item)->getNormalizedBpath();
673     }
674     if (curve == NULL) {
675         return ret_pathv;
676     }
678     if (curve->get_pathvector().empty()) {
679         return ret_pathv;
680     }
682     // remember old stroke style, to be set on fill
683     SPStyle *i_style = SP_OBJECT_STYLE(item);
685     Geom::Matrix const transform(item->transform);
686     float const scale = transform.descrim();
688     float o_width, o_miter;
689     JoinType o_join;
690     ButtType o_butt;
691     {
692         o_width = i_style->stroke_width.computed;
693         if (o_width < 0.1) {
694             o_width = 0.1;
695         }
696         o_miter = i_style->stroke_miterlimit.value * o_width;
698         switch (i_style->stroke_linejoin.computed) {
699             case SP_STROKE_LINEJOIN_MITER:
700                 o_join = join_pointy;
701                 break;
702             case SP_STROKE_LINEJOIN_ROUND:
703                 o_join = join_round;
704                 break;
705             default:
706                 o_join = join_straight;
707                 break;
708         }
710         switch (i_style->stroke_linecap.computed) {
711             case SP_STROKE_LINECAP_SQUARE:
712                 o_butt = butt_square;
713                 break;
714             case SP_STROKE_LINECAP_ROUND:
715                 o_butt = butt_round;
716                 break;
717             default:
718                 o_butt = butt_straight;
719                 break;
720         }
721     }
723     // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly.
724     Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() );
726     Path *orig = new Path;
727     orig->LoadPathVector(pathv);
729     Path *res = new Path;
730     res->SetBackData(false);
732     if (i_style->stroke_dash.n_dash) {
733         // For dashed strokes, use Stroke method, because Outline can't do dashes
734         // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
736         orig->ConvertWithBackData(0.1);
738         orig->DashPolylineFromStyle(i_style, scale, 0);
740         Shape* theShape = new Shape;
741         orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
742                      0.5 * o_miter);
743         orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
745         Shape *theRes = new Shape;
747         theRes->ConvertToShape(theShape, fill_positive);
749         Path *originaux[1];
750         originaux[0] = res;
751         theRes->ConvertToForme(orig, 1, originaux);
753         res->Coalesce(5.0);
755         delete theShape;
756         delete theRes;
757     } else {
758         orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
760         orig->Coalesce(0.5 * o_width);
762         Shape *theShape = new Shape;
763         Shape *theRes = new Shape;
765         res->ConvertWithBackData(1.0);
766         res->Fill(theShape, 0);
767         theRes->ConvertToShape(theShape, fill_positive);
769         Path *originaux[1];
770         originaux[0] = res;
771         theRes->ConvertToForme(orig, 1, originaux);
773         delete theShape;
774         delete theRes;
775     }
777     if (orig->descr_cmd.size() <= 1) {
778         // ca a merd\8e, ou bien le resultat est vide
779         delete res;
780         delete orig;
781         curve->unref();
782         return ret_pathv;
783     }
786     if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
787         ret_pathv = orig->MakePathVector();
789         if (SP_IS_SHAPE(item) && SP_SHAPE(item)->hasMarkers ()) {
790             SPShape *shape = SP_SHAPE(item);
792             Geom::PathVector const & pathv = curve->get_pathvector();
794             // START marker
795             for (int i = 0; i < 2; i++) {  // SP_MARKER_LOC and SP_MARKER_LOC_START
796                 if ( SPObject *marker_obj = shape->marker[i] ) {
797                     Geom::Matrix const m (sp_shape_marker_get_transform_at_start(pathv.front().front()));
798                     item_outline_add_marker( marker_obj, m,
799                                              Geom::Scale(i_style->stroke_width.computed),
800                                              ret_pathv );
801                 }
802             }
803             // MID marker
804             for (int i = 0; i < 3; i += 2) {  // SP_MARKER_LOC and SP_MARKER_LOC_MID
805                 SPObject *midmarker_obj = shape->marker[i];
806                 if (!midmarker_obj) continue;
807                 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
808                     // START position
809                     if ( path_it != pathv.begin() 
810                          && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
811                     {
812                         Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
813                         item_outline_add_marker( midmarker_obj, m,
814                                                  Geom::Scale(i_style->stroke_width.computed),
815                                                  ret_pathv );
816                     }
817                     // MID position
818                    if (path_it->size_default() > 1) {
819                         Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
820                         Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
821                         while (curve_it2 != path_it->end_default())
822                         {
823                             /* Put marker between curve_it1 and curve_it2.
824                              * Loop to end_default (so including closing segment), because when a path is closed,
825                              * there should be a midpoint marker between last segment and closing straight line segment
826                              */
827                             Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
828                             item_outline_add_marker( midmarker_obj, m,
829                                                      Geom::Scale(i_style->stroke_width.computed),
830                                                      ret_pathv);
832                             ++curve_it1;
833                             ++curve_it2;
834                         }
835                     }
836                     // END position
837                     if ( path_it != (pathv.end()-1) && !path_it->empty()) {
838                         Geom::Curve const &lastcurve = path_it->back_default();
839                         Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
840                         item_outline_add_marker( midmarker_obj, m,
841                                                  Geom::Scale(i_style->stroke_width.computed),
842                                                  ret_pathv );
843                     }
844                 }
845             }
846             // END marker
847             for (int i = 0; i < 4; i += 3) {  // SP_MARKER_LOC and SP_MARKER_LOC_END
848                 if ( SPObject *marker_obj = shape->marker[i] ) {
849                     /* Get reference to last curve in the path.
850                      * For moveto-only path, this returns the "closing line segment". */
851                     Geom::Path const &path_last = pathv.back();
852                     unsigned int index = path_last.size_default();
853                     if (index > 0) {
854                         index--;
855                     }
856                     Geom::Curve const &lastcurve = path_last[index];
858                     Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
859                     item_outline_add_marker( marker_obj, m,
860                                              Geom::Scale(i_style->stroke_width.computed),
861                                              ret_pathv );
862                 }
863             }
864         }
866         curve->unref();
867     }
869     delete res;
870     delete orig;
872     return ret_pathv;
875 void
876 sp_selected_path_outline(SPDesktop *desktop)
878     Inkscape::Selection *selection = sp_desktop_selection(desktop);
880     if (selection->isEmpty()) {
881         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
882         return;
883     }
885     bool did = false;
887     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
888          items != NULL;
889          items = items->next) {
891         SPItem *item = (SPItem *) items->data;
893         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
894             continue;
896         SPCurve *curve = NULL;
897         if (SP_IS_SHAPE(item)) {
898             curve = SP_SHAPE(item)->getCurve();
899             if (curve == NULL)
900                 continue;
901         }
902         if (SP_IS_TEXT(item)) {
903             curve = SP_TEXT(item)->getNormalizedBpath();
904             if (curve == NULL)
905                 continue;
906         }
908         if (curve->get_pathvector().empty()) {
909             continue;
910         }
912         // pas de stroke pas de chocolat
913         if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
914             curve->unref();
915             continue;
916         }
918         // remember old stroke style, to be set on fill
919         SPStyle *i_style = SP_OBJECT_STYLE(item);
920         SPCSSAttr *ncss;
921         {
922             ncss = sp_css_attr_from_style(i_style, SP_STYLE_FLAG_ALWAYS);
923             gchar const *s_val = sp_repr_css_property(ncss, "stroke", NULL);
924             gchar const *s_opac = sp_repr_css_property(ncss, "stroke-opacity", NULL);
926             sp_repr_css_set_property(ncss, "stroke", "none");
927             sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
928             sp_repr_css_set_property(ncss, "fill", s_val);
929             if ( s_opac ) {
930                 sp_repr_css_set_property(ncss, "fill-opacity", s_opac);
931             } else {
932                 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
933             }
934             sp_repr_css_unset_property(ncss, "marker-start");
935             sp_repr_css_unset_property(ncss, "marker-mid");
936             sp_repr_css_unset_property(ncss, "marker-end");
937         }
939         Geom::Matrix const transform(item->transform);
940         float const scale = transform.descrim();
941         gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
942         gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
944         float o_width, o_miter;
945         JoinType o_join;
946         ButtType o_butt;
948         {
949             int jointype, captype;
951             jointype = i_style->stroke_linejoin.computed;
952             captype = i_style->stroke_linecap.computed;
953             o_width = i_style->stroke_width.computed;
955             switch (jointype) {
956                 case SP_STROKE_LINEJOIN_MITER:
957                     o_join = join_pointy;
958                     break;
959                 case SP_STROKE_LINEJOIN_ROUND:
960                     o_join = join_round;
961                     break;
962                 default:
963                     o_join = join_straight;
964                     break;
965             }
967             switch (captype) {
968                 case SP_STROKE_LINECAP_SQUARE:
969                     o_butt = butt_square;
970                     break;
971                 case SP_STROKE_LINECAP_ROUND:
972                     o_butt = butt_round;
973                     break;
974                 default:
975                     o_butt = butt_straight;
976                     break;
977             }
979             if (o_width < 0.1)
980                 o_width = 0.1;
981             o_miter = i_style->stroke_miterlimit.value * o_width;
982         }
984         SPCurve *curvetemp = curve_for_item(item);
985         if (curvetemp == NULL) {
986             curve->unref();
987             continue;
988         }
989         // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly.
990         Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curvetemp->get_pathvector() );
991         curvetemp->unref();
993         Path *orig = new Path;
994         orig->LoadPathVector(pathv);
996         Path *res = new Path;
997         res->SetBackData(false);
999         if (i_style->stroke_dash.n_dash) {
1000             // For dashed strokes, use Stroke method, because Outline can't do dashes
1001             // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
1003             orig->ConvertWithBackData(0.1);
1005             orig->DashPolylineFromStyle(i_style, scale, 0);
1007             Shape* theShape = new Shape;
1008             orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
1009                          0.5 * o_miter);
1010             orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
1012             Shape *theRes = new Shape;
1014             theRes->ConvertToShape(theShape, fill_positive);
1016             Path *originaux[1];
1017             originaux[0] = res;
1018             theRes->ConvertToForme(orig, 1, originaux);
1020             res->Coalesce(5.0);
1022             delete theShape;
1023             delete theRes;
1025         } else {
1027             orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
1029             orig->Coalesce(0.5 * o_width);
1031             Shape *theShape = new Shape;
1032             Shape *theRes = new Shape;
1034             res->ConvertWithBackData(1.0);
1035             res->Fill(theShape, 0);
1036             theRes->ConvertToShape(theShape, fill_positive);
1038             Path *originaux[1];
1039             originaux[0] = res;
1040             theRes->ConvertToForme(orig, 1, originaux);
1042             delete theShape;
1043             delete theRes;
1044         }
1046         if (orig->descr_cmd.size() <= 1) {
1047             // ca a merd\8e, ou bien le resultat est vide
1048             delete res;
1049             delete orig;
1050             continue;
1051         }
1053         did = true;
1055         // remember the position of the item
1056         gint pos = SP_OBJECT_REPR(item)->position();
1057         // remember parent
1058         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1059         // remember id
1060         char const *id = SP_OBJECT_REPR(item)->attribute("id");
1061         // remember title
1062         gchar *title = item->title();
1063         // remember description
1064         gchar *desc = item->desc();
1065         
1066         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1068             SPDocument * doc = sp_desktop_document(desktop);
1069             Inkscape::XML::Document *xml_doc = doc->getReprDoc();
1070             Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1072             // restore old style, but set old stroke style on fill
1073             sp_repr_css_change(repr, ncss, "style");
1075             sp_repr_css_attr_unref(ncss);
1077             gchar *str = orig->svg_dump_path();
1078             repr->setAttribute("d", str);
1079             g_free(str);
1081             if (mask)
1082                 repr->setAttribute("mask", mask);
1083             if (clip_path)
1084                 repr->setAttribute("clip-path", clip_path);
1086             if (SP_IS_SHAPE(item) && SP_SHAPE(item)->hasMarkers ()) {
1088                 Inkscape::XML::Document *xml_doc = doc->getReprDoc();
1089                 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
1091                 // add the group to the parent
1092                 parent->appendChild(g_repr);
1093                 // move to the saved position
1094                 g_repr->setPosition(pos > 0 ? pos : 0);
1096                 g_repr->appendChild(repr);
1097                 // restore title, description, id, transform
1098                 repr->setAttribute("id", id);
1099                 SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr);
1100                 newitem->doWriteTransform(repr, transform);
1101                 if (title) {
1102                         newitem->setTitle(title);
1103                 }
1104                 if (desc) {
1105                         newitem->setDesc(desc);
1106                 }
1107                 
1108                 SPShape *shape = SP_SHAPE(item);
1110                 Geom::PathVector const & pathv = curve->get_pathvector();
1112                 // START marker
1113                 for (int i = 0; i < 2; i++) {  // SP_MARKER_LOC and SP_MARKER_LOC_START
1114                     if ( SPObject *marker_obj = shape->marker[i] ) {
1115                         Geom::Matrix const m (sp_shape_marker_get_transform_at_start(pathv.front().front()));
1116                         sp_selected_path_outline_add_marker( marker_obj, m,
1117                                                              Geom::Scale(i_style->stroke_width.computed), transform,
1118                                                              g_repr, xml_doc, doc );
1119                     }
1120                 }
1121                 // MID marker
1122                 for (int i = 0; i < 3; i += 2) {  // SP_MARKER_LOC and SP_MARKER_LOC_MID
1123                     SPObject *midmarker_obj = shape->marker[i];
1124                     if (!midmarker_obj) continue;
1125                     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
1126                         // START position
1127                         if ( path_it != pathv.begin() 
1128                              && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
1129                         {
1130                             Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
1131                             sp_selected_path_outline_add_marker( midmarker_obj, m,
1132                                                                  Geom::Scale(i_style->stroke_width.computed), transform,
1133                                                                  g_repr, xml_doc, doc );
1134                         }
1135                         // MID position
1136                        if (path_it->size_default() > 1) {
1137                             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
1138                             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
1139                             while (curve_it2 != path_it->end_default())
1140                             {
1141                                 /* Put marker between curve_it1 and curve_it2.
1142                                  * Loop to end_default (so including closing segment), because when a path is closed,
1143                                  * there should be a midpoint marker between last segment and closing straight line segment
1144                                  */
1145                                 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
1146                                 sp_selected_path_outline_add_marker(midmarker_obj, m,
1147                                                                     Geom::Scale(i_style->stroke_width.computed), transform,
1148                                                                     g_repr, xml_doc, doc);
1150                                 ++curve_it1;
1151                                 ++curve_it2;
1152                             }
1153                         }
1154                         // END position
1155                         if ( path_it != (pathv.end()-1) && !path_it->empty()) {
1156                             Geom::Curve const &lastcurve = path_it->back_default();
1157                             Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
1158                             sp_selected_path_outline_add_marker( midmarker_obj, m,
1159                                                                  Geom::Scale(i_style->stroke_width.computed), transform,
1160                                                                  g_repr, xml_doc, doc );
1161                         }
1162                     }
1163                 }
1164                 // END marker
1165                 for (int i = 0; i < 4; i += 3) {  // SP_MARKER_LOC and SP_MARKER_LOC_END
1166                     if ( SPObject *marker_obj = shape->marker[i] ) {
1167                         /* Get reference to last curve in the path.
1168                          * For moveto-only path, this returns the "closing line segment". */
1169                         Geom::Path const &path_last = pathv.back();
1170                         unsigned int index = path_last.size_default();
1171                         if (index > 0) {
1172                             index--;
1173                         }
1174                         Geom::Curve const &lastcurve = path_last[index];
1176                         Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
1177                         sp_selected_path_outline_add_marker( marker_obj, m,
1178                                                              Geom::Scale(i_style->stroke_width.computed), transform,
1179                                                              g_repr, xml_doc, doc );
1180                     }
1181                 }
1183                 selection->add(g_repr);
1185                 Inkscape::GC::release(g_repr);
1188             } else {
1190                 // add the new repr to the parent
1191                 parent->appendChild(repr);
1193                 // move to the saved position
1194                 repr->setPosition(pos > 0 ? pos : 0);
1196                 // restore title, description, id, transform
1197                 repr->setAttribute("id", id);
1199                 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1200                 newitem->doWriteTransform(repr, transform);
1201                 if (title) {
1202                         newitem->setTitle(title);
1203                 }
1204                 if (desc) {
1205                         newitem->setDesc(desc);
1206                 }
1207                 
1208                 selection->add(repr);
1210             }
1212             Inkscape::GC::release(repr);
1214             curve->unref();
1215             selection->remove(item);
1216             SP_OBJECT(item)->deleteObject(false);
1218         }
1219         if (title) g_free(title);
1220         if (desc) g_free(desc);
1222         delete res;
1223         delete orig;
1224     }
1226     if (did) {
1227         DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE, 
1228                            _("Convert stroke to path"));
1229     } else {
1230         // TRANSLATORS: "to outline" means "to convert stroke to path"
1231         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
1232         return;
1233     }
1237 void
1238 sp_selected_path_offset(SPDesktop *desktop)
1240     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1241     double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1243     sp_selected_path_do_offset(desktop, true, prefOffset);
1245 void
1246 sp_selected_path_inset(SPDesktop *desktop)
1248     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1249     double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1251     sp_selected_path_do_offset(desktop, false, prefOffset);
1254 void
1255 sp_selected_path_offset_screen(SPDesktop *desktop, double pixels)
1257     sp_selected_path_do_offset(desktop, true,  pixels / desktop->current_zoom());
1260 void
1261 sp_selected_path_inset_screen(SPDesktop *desktop, double pixels)
1263     sp_selected_path_do_offset(desktop, false,  pixels / desktop->current_zoom());
1267 void sp_selected_path_create_offset_object_zero(SPDesktop *desktop)
1269     sp_selected_path_create_offset_object(desktop, 0, false);
1272 void sp_selected_path_create_offset(SPDesktop *desktop)
1274     sp_selected_path_create_offset_object(desktop, 1, false);
1276 void sp_selected_path_create_inset(SPDesktop *desktop)
1278     sp_selected_path_create_offset_object(desktop, -1, false);
1281 void sp_selected_path_create_updating_offset_object_zero(SPDesktop *desktop)
1283     sp_selected_path_create_offset_object(desktop, 0, true);
1286 void sp_selected_path_create_updating_offset(SPDesktop *desktop)
1288     sp_selected_path_create_offset_object(desktop, 1, true);
1290 void sp_selected_path_create_updating_inset(SPDesktop *desktop)
1292     sp_selected_path_create_offset_object(desktop, -1, true);
1295 void
1296 sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating)
1298     Inkscape::Selection *selection;
1299     Inkscape::XML::Node *repr;
1300     SPItem *item;
1301     SPCurve *curve;
1302     gchar *style, *str;
1303     float o_width, o_miter;
1304     JoinType o_join;
1305     ButtType o_butt;
1307     curve = NULL;
1309     selection = sp_desktop_selection(desktop);
1311     item = selection->singleItem();
1313     if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
1314         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
1315         return;
1316     }
1317     if (SP_IS_SHAPE(item))
1318     {
1319         curve = SP_SHAPE(item)->getCurve();
1320         if (curve == NULL)
1321             return;
1322     }
1323     if (SP_IS_TEXT(item))
1324     {
1325         curve = SP_TEXT(item)->getNormalizedBpath();
1326         if (curve == NULL)
1327             return;
1328     }
1330     Geom::Matrix const transform(item->transform);
1332     item->doWriteTransform(SP_OBJECT_REPR(item), Geom::identity());
1334         //XML Tree being used directly here while it shouldn't be...
1335     style = g_strdup(SP_OBJECT(item)->getRepr()->attribute("style"));
1337     // remember the position of the item
1338     gint pos = SP_OBJECT_REPR(item)->position();
1339     // remember parent
1340     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1342     {
1343         SPStyle *i_style = SP_OBJECT(item)->style;
1344         int jointype, captype;
1346         jointype = i_style->stroke_linejoin.value;
1347         captype = i_style->stroke_linecap.value;
1348         o_width = i_style->stroke_width.computed;
1349         if (jointype == SP_STROKE_LINEJOIN_MITER)
1350         {
1351             o_join = join_pointy;
1352         }
1353         else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1354         {
1355             o_join = join_round;
1356         }
1357         else
1358         {
1359             o_join = join_straight;
1360         }
1361         if (captype == SP_STROKE_LINECAP_SQUARE)
1362         {
1363             o_butt = butt_square;
1364         }
1365         else if (captype == SP_STROKE_LINECAP_ROUND)
1366         {
1367             o_butt = butt_round;
1368         }
1369         else
1370         {
1371             o_butt = butt_straight;
1372         }
1374         {
1375             Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1376             o_width = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1377         }
1379         if (o_width < 0.01)
1380             o_width = 0.01;
1381         o_miter = i_style->stroke_miterlimit.value * o_width;
1382     }
1384     Path *orig = Path_for_item(item, true, false);
1385     if (orig == NULL)
1386     {
1387         g_free(style);
1388         curve->unref();
1389         return;
1390     }
1392     Path *res = new Path;
1393     res->SetBackData(false);
1395     {
1396         Shape *theShape = new Shape;
1397         Shape *theRes = new Shape;
1399         orig->ConvertWithBackData(1.0);
1400         orig->Fill(theShape, 0);
1402         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1403         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1404         if (val && strcmp(val, "nonzero") == 0)
1405         {
1406             theRes->ConvertToShape(theShape, fill_nonZero);
1407         }
1408         else if (val && strcmp(val, "evenodd") == 0)
1409         {
1410             theRes->ConvertToShape(theShape, fill_oddEven);
1411         }
1412         else
1413         {
1414             theRes->ConvertToShape(theShape, fill_nonZero);
1415         }
1417         Path *originaux[1];
1418         originaux[0] = orig;
1419         theRes->ConvertToForme(res, 1, originaux);
1421         delete theShape;
1422         delete theRes;
1423     }
1425     curve->unref();
1427     if (res->descr_cmd.size() <= 1)
1428     {
1429         // pas vraiment de points sur le resultat
1430         // donc il ne reste rien
1431         DocumentUndo::done(sp_desktop_document(desktop), 
1432                            (updating ? SP_VERB_SELECTION_LINKED_OFFSET 
1433                             : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1434                            (updating ? _("Create linked offset")
1435                             : _("Create dynamic offset")));
1436         selection->clear();
1438         delete res;
1439         delete orig;
1440         g_free(style);
1441         return;
1442     }
1444     {
1445         gchar tstr[80];
1447         tstr[79] = '\0';
1449         Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
1450         repr = xml_doc->createElement("svg:path");
1451         repr->setAttribute("sodipodi:type", "inkscape:offset");
1452         sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1453                                                           ? o_width
1454                                                           : expand < 0
1455                                                           ? -o_width
1456                                                           : 0 ));
1458         str = res->svg_dump_path();
1459         repr->setAttribute("inkscape:original", str);
1460         g_free(str);
1462         if ( updating ) {
1464                         //XML Tree being used directly here while it shouldn't be
1465             char const *id = SP_OBJECT(item)->getRepr()->attribute("id");
1466             char const *uri = g_strdup_printf("#%s", id);
1467             repr->setAttribute("xlink:href", uri);
1468             g_free((void *) uri);
1469         } else {
1470             repr->setAttribute("inkscape:href", NULL);
1471         }
1473         repr->setAttribute("style", style);
1475         // add the new repr to the parent
1476         parent->appendChild(repr);
1478         // move to the saved position
1479         repr->setPosition(pos > 0 ? pos : 0);
1481         SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1483         if ( updating ) {
1484             // on conserve l'original
1485             // we reapply the transform to the original (offset will feel it)
1486             item->doWriteTransform(SP_OBJECT_REPR(item), transform);
1487         } else {
1488             // delete original, apply the transform to the offset
1489             SP_OBJECT(item)->deleteObject(false);
1490             nitem->doWriteTransform(repr, transform);
1491         }
1493         // The object just created from a temporary repr is only a seed.
1494         // We need to invoke its write which will update its real repr (in particular adding d=)
1495         SP_OBJECT(nitem)->updateRepr();
1497         Inkscape::GC::release(repr);
1499         selection->set(nitem);
1500     }
1502     DocumentUndo::done(sp_desktop_document(desktop), 
1503                        (updating ? SP_VERB_SELECTION_LINKED_OFFSET 
1504                         : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1505                        (updating ? _("Create linked offset")
1506                         : _("Create dynamic offset")));
1508     delete res;
1509     delete orig;
1511     g_free(style);
1525 void
1526 sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset)
1528     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1530     if (selection->isEmpty()) {
1531         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1532         return;
1533     }
1535     bool did = false;
1537     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1538          items != NULL;
1539          items = items->next) {
1541         SPItem *item = (SPItem *) items->data;
1543         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1544             continue;
1546         SPCurve *curve = NULL;
1547         if (SP_IS_SHAPE(item)) {
1548             curve = SP_SHAPE(item)->getCurve();
1549             if (curve == NULL)
1550                 continue;
1551         }
1552         if (SP_IS_TEXT(item)) {
1553             curve = SP_TEXT(item)->getNormalizedBpath();
1554             if (curve == NULL)
1555                 continue;
1556         }
1558         Geom::Matrix const transform(item->transform);
1560         item->doWriteTransform(SP_OBJECT_REPR(item), Geom::identity());
1562         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1564         float o_width, o_miter;
1565         JoinType o_join;
1566         ButtType o_butt;
1568         {
1569             SPStyle *i_style = SP_OBJECT(item)->style;
1570             int jointype, captype;
1572             jointype = i_style->stroke_linejoin.value;
1573             captype = i_style->stroke_linecap.value;
1574             o_width = i_style->stroke_width.computed;
1576             switch (jointype) {
1577                 case SP_STROKE_LINEJOIN_MITER:
1578                     o_join = join_pointy;
1579                     break;
1580                 case SP_STROKE_LINEJOIN_ROUND:
1581                     o_join = join_round;
1582                     break;
1583                 default:
1584                     o_join = join_straight;
1585                     break;
1586             }
1588             switch (captype) {
1589                 case SP_STROKE_LINECAP_SQUARE:
1590                     o_butt = butt_square;
1591                     break;
1592                 case SP_STROKE_LINECAP_ROUND:
1593                     o_butt = butt_round;
1594                     break;
1595                 default:
1596                     o_butt = butt_straight;
1597                     break;
1598             }
1600             o_width = prefOffset;
1602             if (o_width < 0.1)
1603                 o_width = 0.1;
1604             o_miter = i_style->stroke_miterlimit.value * o_width;
1605         }
1607         Path *orig = Path_for_item(item, false);
1608         if (orig == NULL) {
1609             g_free(style);
1610             curve->unref();
1611             continue;
1612         }
1614         Path *res = new Path;
1615         res->SetBackData(false);
1617         {
1618             Shape *theShape = new Shape;
1619             Shape *theRes = new Shape;
1621             orig->ConvertWithBackData(0.03);
1622             orig->Fill(theShape, 0);
1624             SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1625             gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1626             if (val && strcmp(val, "nonzero") == 0)
1627             {
1628                 theRes->ConvertToShape(theShape, fill_nonZero);
1629             }
1630             else if (val && strcmp(val, "evenodd") == 0)
1631             {
1632                 theRes->ConvertToShape(theShape, fill_oddEven);
1633             }
1634             else
1635             {
1636                 theRes->ConvertToShape(theShape, fill_nonZero);
1637             }
1639             // et maintenant: offset
1640             // methode inexacte
1641 /*                      Path *originaux[1];
1642                         originaux[0] = orig;
1643                         theRes->ConvertToForme(res, 1, originaux);
1645                         if (expand) {
1646                         res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1647                         } else {
1648                         res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1649                         }
1651                         orig->ConvertWithBackData(1.0);
1652                         orig->Fill(theShape, 0);
1653                         theRes->ConvertToShape(theShape, fill_positive);
1654                         originaux[0] = orig;
1655                         theRes->ConvertToForme(res, 1, originaux);
1657                         if (o_width >= 0.5) {
1658                         //     res->Coalesce(1.0);
1659                         res->ConvertEvenLines(1.0);
1660                         res->Simplify(1.0);
1661                         } else {
1662                         //      res->Coalesce(o_width);
1663                         res->ConvertEvenLines(1.0*o_width);
1664                         res->Simplify(1.0 * o_width);
1665                         }    */
1666             // methode par makeoffset
1668             if (expand)
1669             {
1670                 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1671             }
1672             else
1673             {
1674                 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1675             }
1676             theRes->ConvertToShape(theShape, fill_positive);
1678             res->Reset();
1679             theRes->ConvertToForme(res);
1681             if (o_width >= 1.0)
1682             {
1683                 res->ConvertEvenLines(1.0);
1684                 res->Simplify(1.0);
1685             }
1686             else
1687             {
1688                 res->ConvertEvenLines(1.0*o_width);
1689                 res->Simplify(1.0 * o_width);
1690             }
1692             delete theShape;
1693             delete theRes;
1694         }
1696         did = true;
1698         curve->unref();
1699         // remember the position of the item
1700         gint pos = SP_OBJECT_REPR(item)->position();
1701         // remember parent
1702         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1703         // remember id
1704         char const *id = SP_OBJECT_REPR(item)->attribute("id");
1706         selection->remove(item);
1707         SP_OBJECT(item)->deleteObject(false);
1709         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1711             gchar tstr[80];
1713             tstr[79] = '\0';
1715             Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
1716             Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1718             repr->setAttribute("style", style);
1720             gchar *str = res->svg_dump_path();
1721             repr->setAttribute("d", str);
1722             g_free(str);
1724             // add the new repr to the parent
1725             parent->appendChild(repr);
1727             // move to the saved position
1728             repr->setPosition(pos > 0 ? pos : 0);
1730             SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1732             // reapply the transform
1733             newitem->doWriteTransform(repr, transform);
1735             repr->setAttribute("id", id);
1737             selection->add(repr);
1739             Inkscape::GC::release(repr);
1740         }
1742         delete orig;
1743         delete res;
1744     }
1746     if (did) {
1747         DocumentUndo::done(sp_desktop_document(desktop), 
1748                            (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1749                            (expand ? _("Outset path") : _("Inset path")));
1750     } else {
1751         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1752         return;
1753     }
1757 static bool
1758 sp_selected_path_simplify_items(SPDesktop *desktop,
1759                                 Inkscape::Selection *selection, GSList *items,
1760                                 float threshold,  bool justCoalesce,
1761                                 float angleLimit, bool breakableAngles,
1762                                 bool modifySelection);
1765 //return true if we changed something, else false
1766 bool
1767 sp_selected_path_simplify_item(SPDesktop *desktop,
1768                  Inkscape::Selection *selection, SPItem *item,
1769                  float threshold,  bool justCoalesce,
1770                  float angleLimit, bool breakableAngles,
1771                  gdouble size,     bool modifySelection)
1773     if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1774         return false;
1776     //If this is a group, do the children instead
1777     if (SP_IS_GROUP(item)) {
1778         GSList *items = sp_item_group_item_list(SP_GROUP(item));
1779         
1780         return sp_selected_path_simplify_items(desktop, selection, items,
1781                                                threshold, justCoalesce,
1782                                                angleLimit, breakableAngles,
1783                                                false);
1784     }
1787     SPCurve *curve = NULL;
1789     if (SP_IS_SHAPE(item)) {
1790         curve = SP_SHAPE(item)->getCurve();
1791         if (!curve)
1792             return false;
1793     }
1795     if (SP_IS_TEXT(item)) {
1796         curve = SP_TEXT(item)->getNormalizedBpath();
1797         if (!curve)
1798             return false;
1799     }
1801     // correct virtual size by full transform (bug #166937)
1802     size /= item->i2doc_affine().descrim();
1804     // save the transform, to re-apply it after simplification
1805     Geom::Matrix const transform(item->transform);
1807     /*
1808        reset the transform, effectively transforming the item by transform.inverse();
1809        this is necessary so that the item is transformed twice back and forth,
1810        allowing all compensations to cancel out regardless of the preferences
1811     */
1812     item->doWriteTransform(SP_OBJECT_REPR(item), Geom::identity());
1814     gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1815     gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1816     gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1818     Path *orig = Path_for_item(item, false);
1819     if (orig == NULL) {
1820         g_free(style);
1821         curve->unref();
1822         return false;
1823     }
1825     curve->unref();
1826     // remember the position of the item
1827     gint pos = SP_OBJECT_REPR(item)->position();
1828     // remember parent
1829     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1830     // remember id
1831     char const *id = SP_OBJECT_REPR(item)->attribute("id");
1832     // remember path effect
1833     char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
1834     // remember title
1835     gchar *title = item->title();
1836     // remember description
1837     gchar *desc = item->desc();
1838     
1839     //If a group was selected, to not change the selection list
1840     if (modifySelection)
1841         selection->remove(item);
1843     SP_OBJECT(item)->deleteObject(false);
1845     if ( justCoalesce ) {
1846         orig->Coalesce(threshold * size);
1847     } else {
1848         orig->ConvertEvenLines(threshold * size);
1849         orig->Simplify(threshold * size);
1850     }
1852     Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
1853     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1855     // restore style, mask and clip-path
1856     repr->setAttribute("style", style);
1857     g_free(style);
1859     if ( mask ) {
1860         repr->setAttribute("mask", mask);
1861         g_free(mask);
1862     }
1864     if ( clip_path ) {
1865         repr->setAttribute("clip-path", clip_path);
1866         g_free(clip_path);
1867     }
1869     // restore path effect
1870     repr->setAttribute("inkscape:path-effect", patheffect);
1872     // path
1873     gchar *str = orig->svg_dump_path();
1874     if (patheffect)
1875         repr->setAttribute("inkscape:original-d", str);
1876     else 
1877         repr->setAttribute("d", str);
1878     g_free(str);
1880     // restore id
1881     repr->setAttribute("id", id);
1883     // add the new repr to the parent
1884     parent->appendChild(repr);
1886     // move to the saved position
1887     repr->setPosition(pos > 0 ? pos : 0);
1889     SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1891     // reapply the transform
1892     newitem->doWriteTransform(repr, transform);
1894     // restore title & description
1895     if (title) {
1896         newitem->setTitle(title);
1897         g_free(title);
1898     }
1899     if (desc) {
1900         newitem->setDesc(desc);
1901         g_free(desc);
1902     }
1903     
1904     //If we are not in a selected group
1905     if (modifySelection)
1906         selection->add(repr);
1908     Inkscape::GC::release(repr);
1910     // clean up
1911     if (orig) delete orig;
1913     return true;
1917 bool
1918 sp_selected_path_simplify_items(SPDesktop *desktop,
1919                                 Inkscape::Selection *selection, GSList *items,
1920                                 float threshold,  bool justCoalesce,
1921                                 float angleLimit, bool breakableAngles,
1922                                 bool modifySelection)
1924     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1925     bool simplifyIndividualPaths = prefs->getBool("/options/simplifyindividualpaths/value");
1927     gchar *simplificationType;
1928     if (simplifyIndividualPaths) {
1929         simplificationType = _("Simplifying paths (separately):");
1930     } else {
1931         simplificationType = _("Simplifying paths:");
1932     }
1934     bool didSomething = false;
1936     Geom::OptRect selectionBbox = selection->bounds();
1937     if (!selectionBbox) {
1938         return false;
1939     }
1940     gdouble selectionSize  = L2(selectionBbox->dimensions());
1942     gdouble simplifySize  = selectionSize;
1944     int pathsSimplified = 0;
1945     int totalPathCount  = g_slist_length(items);
1947     // set "busy" cursor
1948     desktop->setWaitingCursor();
1950     for (; items != NULL; items = items->next) {
1951         SPItem *item = (SPItem *) items->data;
1953         if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1954           continue;
1956         if (simplifyIndividualPaths) {
1957             Geom::OptRect itemBbox = item->getBounds(item->i2d_affine());
1958             if (itemBbox) {
1959                 simplifySize      = L2(itemBbox->dimensions());
1960             } else {
1961                 simplifySize      = 0;
1962             }
1963         }
1965         pathsSimplified++;
1967         if (pathsSimplified % 20 == 0) {
1968             gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."),
1969                 simplificationType, pathsSimplified, totalPathCount);
1970             desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1971         }
1973         didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1974             threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1975     }
1977     desktop->clearWaitingCursor();
1979     if (pathsSimplified > 20) {
1980         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1981     }
1983     return didSomething;
1986 void
1987 sp_selected_path_simplify_selection(SPDesktop *desktop, float threshold, bool justCoalesce,
1988                                     float angleLimit, bool breakableAngles)
1990     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1992     if (selection->isEmpty()) {
1993         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1994                          _("Select <b>path(s)</b> to simplify."));
1995         return;
1996     }
1998     GSList *items = g_slist_copy((GSList *) selection->itemList());
2000     bool didSomething = sp_selected_path_simplify_items(desktop, selection,
2001                                                         items, threshold,
2002                                                         justCoalesce,
2003                                                         angleLimit,
2004                                                         breakableAngles, true);
2006     if (didSomething)
2007         DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY, 
2008                            _("Simplify"));
2009     else
2010         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
2015 // globals for keeping track of accelerated simplify
2016 static double previousTime      = 0.0;
2017 static gdouble simplifyMultiply = 1.0;
2019 void
2020 sp_selected_path_simplify(SPDesktop *desktop)
2022     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2023     gdouble simplifyThreshold =
2024         prefs->getDouble("/options/simplifythreshold/value", 0.003);
2025     bool simplifyJustCoalesce = prefs->getBool("/options/simplifyjustcoalesce/value", 0);
2027     //Get the current time
2028     GTimeVal currentTimeVal;
2029     g_get_current_time(&currentTimeVal);
2030     double currentTime = currentTimeVal.tv_sec * 1000000 +
2031                 currentTimeVal.tv_usec;
2033     //Was the previous call to this function recent? (<0.5 sec)
2034     if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
2036         // add to the threshold 1/2 of its original value
2037         simplifyMultiply  += 0.5;
2038         simplifyThreshold *= simplifyMultiply;
2040     } else {
2041         // reset to the default
2042         simplifyMultiply = 1;
2043     }
2045     //remember time for next call
2046     previousTime = currentTime;
2048     //g_print("%g\n", simplify_threshold);
2050     //Make the actual call
2051     sp_selected_path_simplify_selection(desktop, simplifyThreshold,
2052                                         simplifyJustCoalesce, 0.0, false);
2057 // fonctions utilitaires
2059 bool
2060 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
2062     if (who == NULL || a == NULL)
2063         return false;
2064     if (who == a)
2065         return true;
2066     return Ancetre(sp_repr_parent(a), who);
2069 Path *
2070 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
2072     SPCurve *curve = curve_for_item(item);
2074     if (curve == NULL)
2075         return NULL;
2076     
2077     Geom::PathVector *pathv = pathvector_for_curve(item, curve, doTransformation, transformFull, Geom::identity(), Geom::identity());
2078     curve->unref();
2079     
2080     Path *dest = new Path;
2081     dest->LoadPathVector(*pathv);    
2082     delete pathv;
2083     
2084     return dest;
2087 /* 
2088  * NOTE: Returns empty pathvector if curve == NULL
2089  * TODO: see if calling this method can be optimized. All the pathvector copying might be slow.
2090  */
2091 Geom::PathVector*
2092 pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, Geom::Matrix extraPreAffine, Geom::Matrix extraPostAffine)
2094     if (curve == NULL)
2095         return NULL;
2097     Geom::PathVector *dest = new Geom::PathVector;    
2098     *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller!
2099     
2100     if (doTransformation) {
2101         if (transformFull) {
2102             *dest *= extraPreAffine * item->i2doc_affine() * extraPostAffine;
2103         } else {
2104             *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine;
2105         }
2106     } else {
2107         *dest *= extraPreAffine * extraPostAffine;
2108     }
2109     
2110     return dest;
2113 SPCurve* curve_for_item(SPItem *item)
2115     if (!item) 
2116         return NULL;
2117     
2118     SPCurve *curve = NULL;
2119     if (SP_IS_SHAPE(item)) {
2120         if (SP_IS_PATH(item)) {
2121             curve = sp_path_get_curve_for_edit(SP_PATH(item));
2122         } else {
2123             curve = SP_SHAPE(item)->getCurve();
2124         }
2125     }
2126     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
2127     {
2128         curve = te_get_layout(item)->convertToCurves();
2129     }
2130     else if (SP_IS_IMAGE(item))
2131     {
2132     curve = sp_image_get_curve(SP_IMAGE(item));
2133     }
2134     
2135     return curve; // do not forget to unref the curve at some point!
2138 boost::optional<Path::cut_position> get_nearest_position_on_Path(Path *path, Geom::Point p, unsigned seg)
2140     //get nearest position on path
2141     Path::cut_position pos = path->PointToCurvilignPosition(p, seg);
2142     return pos;
2145 Geom::Point get_point_on_Path(Path *path, int piece, double t)
2147     Geom::Point p;
2148     path->PointAt(piece, t, p);
2149     return p;
2153 /*
2154   Local Variables:
2155   mode:c++
2156   c-file-style:"stroustrup"
2157   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2158   indent-tabs-mode:nil
2159   fill-column:99
2160   End:
2161 */
2162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :