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 <cstring>
21 #include <string>
22 #include <vector>
23 #include <glib/gmem.h>
24 #include "xml/repr.h"
25 #include "svg/svg.h"
26 #include "sp-path.h"
27 #include "sp-shape.h"
28 #include "sp-image.h"
29 #include "marker.h"
30 #include "enums.h"
31 #include "sp-text.h"
32 #include "sp-flowtext.h"
33 #include "text-editing.h"
34 #include "sp-item-group.h"
35 #include "style.h"
36 #include "document.h"
37 #include "message-stack.h"
38 #include "selection.h"
39 #include "desktop-handles.h"
40 #include "desktop.h"
41 #include "display/canvas-bpath.h"
42 #include "display/curve.h"
43 #include <glibmm/i18n.h>
44 #include "preferences.h"
46 #include "xml/repr.h"
47 #include "xml/repr-sorting.h"
48 #include <2geom/pathvector.h>
49 #include <libnr/nr-scale-matrix-ops.h>
50 #include "helper/geom.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(SPDesktop *desktop, bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
60 void sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset);
61 void sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating);
63 void
64 sp_selected_path_union(SPDesktop *desktop)
65 {
66 sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
67 }
69 void
70 sp_selected_path_union_skip_undo(SPDesktop *desktop)
71 {
72 sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_NONE, _("Union"));
73 }
75 void
76 sp_selected_path_intersect(SPDesktop *desktop)
77 {
78 sp_selected_path_boolop(desktop, bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
79 }
81 void
82 sp_selected_path_diff(SPDesktop *desktop)
83 {
84 sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
85 }
87 void
88 sp_selected_path_diff_skip_undo(SPDesktop *desktop)
89 {
90 sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_NONE, _("Difference"));
91 }
93 void
94 sp_selected_path_symdiff(SPDesktop *desktop)
95 {
96 sp_selected_path_boolop(desktop, bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
97 }
98 void
99 sp_selected_path_cut(SPDesktop *desktop)
100 {
101 sp_selected_path_boolop(desktop, bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
102 }
103 void
104 sp_selected_path_slice(SPDesktop *desktop)
105 {
106 sp_selected_path_boolop(desktop, bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path"));
107 }
110 // boolean operations
111 // take the source paths from the file, do the operation, delete the originals and add the results
112 void
113 sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb, const Glib::ustring description)
114 {
115 Inkscape::Selection *selection = sp_desktop_selection(desktop);
117 GSList *il = (GSList *) selection->itemList();
119 // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
120 if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
121 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
122 return;
123 }
124 else if ( g_slist_length(il) < 1 ) {
125 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
126 return;
127 }
129 if (g_slist_length(il) > 2) {
130 if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
131 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, division, or path cut."));
132 return;
133 }
134 }
136 // reverseOrderForOp marks whether the order of the list is the top->down order
137 // it's only used when there are 2 objects, and for operations who need to know the
138 // topmost object (differences, cuts)
139 bool reverseOrderForOp = false;
141 if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice) {
142 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
143 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
144 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
146 if (a == NULL || b == NULL) {
147 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."));
148 return;
149 }
151 if (Ancetre(a, b)) {
152 // a is the parent of b, already in the proper order
153 } else if (Ancetre(b, a)) {
154 // reverse order
155 reverseOrderForOp = true;
156 } else {
158 // objects are not in parent/child relationship;
159 // find their lowest common ancestor
160 Inkscape::XML::Node *dad = LCA(a, b);
161 if (dad == NULL) {
162 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."));
163 return;
164 }
166 // find the children of the LCA that lead from it to the a and b
167 Inkscape::XML::Node *as = AncetreFils(a, dad);
168 Inkscape::XML::Node *bs = AncetreFils(b, dad);
170 // find out which comes first
171 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
172 if (child == as) {
173 /* a first, so reverse. */
174 reverseOrderForOp = true;
175 break;
176 }
177 if (child == bs)
178 break;
179 }
180 }
181 }
183 il = g_slist_copy(il);
185 // first check if all the input objects have shapes
186 // otherwise bail out
187 for (GSList *l = il; l != NULL; l = l->next)
188 {
189 SPItem *item = SP_ITEM(l->data);
190 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item))
191 {
192 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
193 g_slist_free(il);
194 return;
195 }
196 }
198 // extract the livarot Paths from the source objects
199 // also get the winding rule specified in the style
200 int nbOriginaux = g_slist_length(il);
201 std::vector<Path *> originaux(nbOriginaux);
202 std::vector<FillRule> origWind(nbOriginaux);
203 int curOrig;
204 {
205 curOrig = 0;
206 for (GSList *l = il; l != NULL; l = l->next)
207 {
208 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
209 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
210 if (val && strcmp(val, "nonzero") == 0) {
211 origWind[curOrig]= fill_nonZero;
212 } else if (val && strcmp(val, "evenodd") == 0) {
213 origWind[curOrig]= fill_oddEven;
214 } else {
215 origWind[curOrig]= fill_nonZero;
216 }
218 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
219 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
220 {
221 for (int i = curOrig; i >= 0; i--) delete originaux[i];
222 g_slist_free(il);
223 return;
224 }
225 curOrig++;
226 }
227 }
228 // reverse if needed
229 // note that the selection list keeps its order
230 if ( reverseOrderForOp ) {
231 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
232 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
233 }
235 // and work
236 // some temporary instances, first
237 Shape *theShapeA = new Shape;
238 Shape *theShapeB = new Shape;
239 Shape *theShape = new Shape;
240 Path *res = new Path;
241 res->SetBackData(false);
242 Path::cut_position *toCut=NULL;
243 int nbToCut=0;
245 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
246 // true boolean op
247 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
248 originaux[0]->ConvertWithBackData(0.1);
250 originaux[0]->Fill(theShape, 0);
252 theShapeA->ConvertToShape(theShape, origWind[0]);
254 curOrig = 1;
255 for (GSList *l = il->next; l != NULL; l = l->next) {
256 originaux[curOrig]->ConvertWithBackData(0.1);
258 originaux[curOrig]->Fill(theShape, curOrig);
260 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
262 // les elements arrivent en ordre inverse dans la liste
263 theShape->Booleen(theShapeB, theShapeA, bop);
265 {
266 Shape *swap = theShape;
267 theShape = theShapeA;
268 theShapeA = swap;
269 }
270 curOrig++;
271 }
273 {
274 Shape *swap = theShape;
275 theShape = theShapeA;
276 theShapeA = swap;
277 }
279 } else if ( bop == bool_op_cut ) {
280 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
281 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
282 // it is just uncrossed, and cleaned from duplicate edges and points
283 // then it's fed to Booleen() which will uncross it against the other path
284 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
285 // thus making a polygon. the weight of the edges of the cut are all 0, but
286 // the Booleen need to invert the ones inside the source polygon (for the subsequent
287 // ConvertToForme)
289 // the cut path needs to have the highest pathID in the back data
290 // that's how the Booleen() function knows it's an edge of the cut
292 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
293 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
294 // left it at 1.0. Investigate replacing this by a combination of difference and
295 // intersection of the same two paths. -- bb
296 {
297 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
298 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
299 }
300 originaux[0]->ConvertWithBackData(1.0);
302 originaux[0]->Fill(theShape, 0);
304 theShapeA->ConvertToShape(theShape, origWind[0]);
306 originaux[1]->ConvertWithBackData(1.0);
308 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
310 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
312 // les elements arrivent en ordre inverse dans la liste
313 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
315 } else if ( bop == bool_op_slice ) {
316 // slice is not really a boolean operation
317 // you just put the 2 shapes in a single polygon, uncross it
318 // the points where the degree is > 2 are intersections
319 // just check it's an intersection on the path you want to cut, and keep it
320 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
321 // make new subpath at each one of these positions
322 // inversion pour l'op\8eration
323 {
324 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
325 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
326 }
327 originaux[0]->ConvertWithBackData(1.0);
329 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
331 originaux[1]->ConvertWithBackData(1.0);
333 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
335 theShape->ConvertToShape(theShapeA, fill_justDont);
337 if ( theShape->hasBackData() ) {
338 // should always be the case, but ya never know
339 {
340 for (int i = 0; i < theShape->numberOfPoints(); i++) {
341 if ( theShape->getPoint(i).totalDegree() > 2 ) {
342 // possibly an intersection
343 // we need to check that at least one edge from the source path is incident to it
344 // before we declare it's an intersection
345 int cb = theShape->getPoint(i).incidentEdge[FIRST];
346 int nbOrig=0;
347 int nbOther=0;
348 int piece=-1;
349 float t=0.0;
350 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
351 if ( theShape->ebData[cb].pathID == 0 ) {
352 // the source has an edge incident to the point, get its position on the path
353 piece=theShape->ebData[cb].pieceID;
354 if ( theShape->getEdge(cb).st == i ) {
355 t=theShape->ebData[cb].tSt;
356 } else {
357 t=theShape->ebData[cb].tEn;
358 }
359 nbOrig++;
360 }
361 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
362 cb=theShape->NextAt(i, cb);
363 }
364 if ( nbOrig > 0 && nbOther > 0 ) {
365 // point incident to both path and cut: an intersection
366 // note that you only keep one position on the source; you could have degenerate
367 // cases where the source crosses itself at this point, and you wouyld miss an intersection
368 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
369 toCut[nbToCut].piece=piece;
370 toCut[nbToCut].t=t;
371 nbToCut++;
372 }
373 }
374 }
375 }
376 {
377 // i think it's useless now
378 int i = theShape->numberOfEdges() - 1;
379 for (;i>=0;i--) {
380 if ( theShape->ebData[i].pathID == 1 ) {
381 theShape->SubEdge(i);
382 }
383 }
384 }
386 }
387 }
389 int* nesting=NULL;
390 int* conts=NULL;
391 int nbNest=0;
392 // pour compenser le swap juste avant
393 if ( bop == bool_op_slice ) {
394 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
395 // res->ConvertForcedToMoveTo();
396 res->Copy(originaux[0]);
397 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
398 free(toCut);
399 } else if ( bop == bool_op_cut ) {
400 // il faut appeler pour desallouer PointData (pas vital, mais bon)
401 // the Booleen() function did not deallocated the point_data array in theShape, because this
402 // function needs it.
403 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
404 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
405 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
406 } else {
407 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
408 }
410 delete theShape;
411 delete theShapeA;
412 delete theShapeB;
413 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
415 if (res->descr_cmd.size() <= 1)
416 {
417 // only one command, presumably a moveto: it isn't a path
418 for (GSList *l = il; l != NULL; l = l->next)
419 {
420 SP_OBJECT(l->data)->deleteObject();
421 }
422 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
423 description);
424 selection->clear();
426 delete res;
427 g_slist_free(il);
428 return;
429 }
431 // get the source path object
432 SPObject *source;
433 if ( bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
434 if (reverseOrderForOp) {
435 source = SP_OBJECT(il->data);
436 } else {
437 source = SP_OBJECT(il->next->data);
438 }
439 } else {
440 // find out the bottom object
441 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
443 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
445 source = sp_desktop_document(desktop)->
446 getObjectByRepr((Inkscape::XML::Node *)sorted->data);
448 g_slist_free(sorted);
449 }
451 // adjust style properties that depend on a possible transform in the source object in order
452 // to get a correct style attribute for the new path
453 SPItem* item_source = SP_ITEM(source);
454 Geom::Matrix i2doc(sp_item_i2doc_affine(item_source));
455 sp_item_adjust_stroke(item_source, i2doc.descrim());
456 sp_item_adjust_pattern(item_source, i2doc);
457 sp_item_adjust_gradient(item_source, i2doc);
458 sp_item_adjust_livepatheffect(item_source, i2doc);
460 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
462 // remember important aspects of the source path, to be restored
463 gint pos = repr_source->position();
464 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
465 gchar const *id = repr_source->attribute("id");
466 gchar const *style = repr_source->attribute("style");
467 gchar const *mask = repr_source->attribute("mask");
468 gchar const *clip_path = repr_source->attribute("clip-path");
469 gchar *title = source->title();
470 gchar *desc = source->desc();
471 // remove source paths
472 selection->clear();
473 for (GSList *l = il; l != NULL; l = l->next) {
474 // if this is the bottommost object,
475 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
476 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
477 SP_OBJECT(l->data)->deleteObject(false);
478 } else {
479 // delete the object for real, so that its clones can take appropriate action
480 SP_OBJECT(l->data)->deleteObject();
481 }
482 }
483 g_slist_free(il);
485 // premultiply by the inverse of parent's repr
486 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
487 Geom::Matrix local (sp_item_i2doc_affine(parent_item));
488 gchar *transform = sp_svg_transform_write(local.inverse());
490 // now that we have the result, add it on the canvas
491 if ( bop == bool_op_cut || bop == bool_op_slice ) {
492 int nbRP=0;
493 Path** resPath;
494 if ( bop == bool_op_slice ) {
495 // there are moveto's at each intersection, but it's still one unique path
496 // so break it down and add each subpath independently
497 // we could call break_apart to do this, but while we have the description...
498 resPath=res->SubPaths(nbRP, false);
499 } else {
500 // cut operation is a bit wicked: you need to keep holes
501 // that's why you needed the nesting
502 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
503 // to get the path for each part of the polygon. that's why you need the nesting info:
504 // to know in wich subpath to add a subpath
505 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
507 // cleaning
508 if ( conts ) free(conts);
509 if ( nesting ) free(nesting);
510 }
512 // add all the pieces resulting from cut or slice
513 for (int i=0;i<nbRP;i++) {
514 gchar *d = resPath[i]->svg_dump_path();
516 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
517 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
518 repr->setAttribute("style", style);
519 if (mask)
520 repr->setAttribute("mask", mask);
521 if (clip_path)
522 repr->setAttribute("clip-path", clip_path);
524 repr->setAttribute("d", d);
525 g_free(d);
527 // for slice, remove fill
528 if (bop == bool_op_slice) {
529 SPCSSAttr *css;
531 css = sp_repr_css_attr_new();
532 sp_repr_css_set_property(css, "fill", "none");
534 sp_repr_css_change(repr, css, "style");
536 sp_repr_css_attr_unref(css);
537 }
539 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
540 // this means it's basically random which of the pieces inherits the original's id and clones
541 // a better algorithm might figure out e.g. the biggest piece
542 repr->setAttribute("id", id);
544 repr->setAttribute("transform", transform);
546 // add the new repr to the parent
547 parent->appendChild(repr);
549 // move to the saved position
550 repr->setPosition(pos > 0 ? pos : 0);
552 selection->add(repr);
553 Inkscape::GC::release(repr);
555 delete resPath[i];
556 }
557 if ( resPath ) free(resPath);
559 } else {
560 gchar *d = res->svg_dump_path();
562 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
563 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
564 repr->setAttribute("style", style);
566 if ( mask )
567 repr->setAttribute("mask", mask);
569 if ( clip_path )
570 repr->setAttribute("clip-path", clip_path);
572 repr->setAttribute("d", d);
573 g_free(d);
575 repr->setAttribute("transform", transform);
577 repr->setAttribute("id", id);
578 parent->appendChild(repr);
579 if (title) {
580 sp_desktop_document(desktop)->getObjectByRepr(repr)->setTitle(title);
581 }
582 if (desc) {
583 sp_desktop_document(desktop)->getObjectByRepr(repr)->setDesc(desc);
584 }
585 repr->setPosition(pos > 0 ? pos : 0);
587 selection->add(repr);
588 Inkscape::GC::release(repr);
589 }
591 g_free(transform);
592 if (title) g_free(title);
593 if (desc) g_free(desc);
595 if (verb != SP_VERB_NONE) {
596 sp_document_done(sp_desktop_document(desktop), verb, description);
597 }
599 delete res;
600 }
602 static
603 void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix marker_transform,
604 Geom::Scale stroke_scale, Geom::Matrix transform,
605 Inkscape::XML::Node *g_repr, Inkscape::XML::Document *xml_doc, SPDocument * doc )
606 {
607 SPMarker* marker = SP_MARKER (marker_object);
608 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker_object));
610 Geom::Matrix tr(marker_transform);
612 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
613 tr = stroke_scale * tr;
614 }
616 // total marker transform
617 tr = marker_item->transform * marker->c2p * tr * transform;
619 if (SP_OBJECT_REPR(marker_item)) {
620 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
621 g_repr->appendChild(m_repr);
622 SPItem *marker_item = (SPItem *) doc->getObjectByRepr(m_repr);
623 sp_item_write_transform(marker_item, m_repr, tr);
624 }
625 }
627 static
628 void item_outline_add_marker( SPObject const *marker_object, Geom::Matrix marker_transform,
629 Geom::Scale stroke_scale, Geom::Matrix transform,
630 Geom::PathVector* pathv_in )
631 {
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 * transform;
642 Geom::PathVector* marker_pathv = item_outline(marker_item);
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 }
650 }
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)
657 {
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_get_curve(SP_SHAPE(item));
671 } else if (SP_IS_TEXT(item)) {
672 curve = SP_TEXT(item)->getNormalizedBpath();
673 }
674 if (curve == NULL) {
675 return ret_pathv;
676 }
678 // remember old stroke style, to be set on fill
679 SPStyle *i_style = SP_OBJECT_STYLE(item);
681 Geom::Matrix const transform(item->transform);
682 float const scale = transform.descrim();
684 float o_width, o_miter;
685 JoinType o_join;
686 ButtType o_butt;
687 {
688 o_width = i_style->stroke_width.computed;
689 if (o_width < 0.1) {
690 o_width = 0.1;
691 }
692 o_miter = i_style->stroke_miterlimit.value * o_width;
694 switch (i_style->stroke_linejoin.computed) {
695 case SP_STROKE_LINEJOIN_MITER:
696 o_join = join_pointy;
697 break;
698 case SP_STROKE_LINEJOIN_ROUND:
699 o_join = join_round;
700 break;
701 default:
702 o_join = join_straight;
703 break;
704 }
706 switch (i_style->stroke_linecap.computed) {
707 case SP_STROKE_LINECAP_SQUARE:
708 o_butt = butt_square;
709 break;
710 case SP_STROKE_LINECAP_ROUND:
711 o_butt = butt_round;
712 break;
713 default:
714 o_butt = butt_straight;
715 break;
716 }
717 }
719 // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly.
720 Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() );
722 Path *orig = new Path;
723 orig->LoadPathVector(pathv);
725 Path *res = new Path;
726 res->SetBackData(false);
728 if (i_style->stroke_dash.n_dash) {
729 // For dashed strokes, use Stroke method, because Outline can't do dashes
730 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
732 orig->ConvertWithBackData(0.1);
734 orig->DashPolylineFromStyle(i_style, scale, 0);
736 Shape* theShape = new Shape;
737 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
738 0.5 * o_miter);
739 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
741 Shape *theRes = new Shape;
743 theRes->ConvertToShape(theShape, fill_positive);
745 Path *originaux[1];
746 originaux[0] = res;
747 theRes->ConvertToForme(orig, 1, originaux);
749 res->Coalesce(5.0);
751 delete theShape;
752 delete theRes;
753 } else {
754 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
756 orig->Coalesce(0.5 * o_width);
758 Shape *theShape = new Shape;
759 Shape *theRes = new Shape;
761 res->ConvertWithBackData(1.0);
762 res->Fill(theShape, 0);
763 theRes->ConvertToShape(theShape, fill_positive);
765 Path *originaux[1];
766 originaux[0] = res;
767 theRes->ConvertToForme(orig, 1, originaux);
769 delete theShape;
770 delete theRes;
771 }
773 if (orig->descr_cmd.size() <= 1) {
774 // ca a merd\8e, ou bien le resultat est vide
775 delete res;
776 delete orig;
777 curve->unref();
778 return ret_pathv;
779 }
782 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
783 ret_pathv = orig->MakePathVector();
785 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
786 SPShape *shape = SP_SHAPE(item);
788 Geom::PathVector const & pathv = curve->get_pathvector();
790 // START marker
791 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
792 if ( SPObject *marker_obj = shape->marker[i] ) {
793 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(pathv.front().front()));
794 item_outline_add_marker( marker_obj, m,
795 Geom::Scale(i_style->stroke_width.computed), transform,
796 ret_pathv );
797 }
798 }
799 // MID marker
800 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
801 SPObject *midmarker_obj = shape->marker[i];
802 if (!midmarker_obj) continue;
803 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
804 // START position
805 if ( path_it != pathv.begin()
806 && ! ((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
807 {
808 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
809 item_outline_add_marker( midmarker_obj, m,
810 Geom::Scale(i_style->stroke_width.computed), transform,
811 ret_pathv );
812 }
813 // MID position
814 if (path_it->size_default() > 1) {
815 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
816 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
817 while (curve_it2 != path_it->end_default())
818 {
819 /* Put marker between curve_it1 and curve_it2.
820 * Loop to end_default (so including closing segment), because when a path is closed,
821 * there should be a midpoint marker between last segment and closing straight line segment
822 */
823 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
824 item_outline_add_marker( midmarker_obj, m,
825 Geom::Scale(i_style->stroke_width.computed), transform,
826 ret_pathv);
828 ++curve_it1;
829 ++curve_it2;
830 }
831 }
832 // END position
833 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
834 Geom::Curve const &lastcurve = path_it->back_default();
835 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
836 item_outline_add_marker( midmarker_obj, m,
837 Geom::Scale(i_style->stroke_width.computed), transform,
838 ret_pathv );
839 }
840 }
841 }
842 // END marker
843 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
844 if ( SPObject *marker_obj = shape->marker[i] ) {
845 /* Get reference to last curve in the path.
846 * For moveto-only path, this returns the "closing line segment". */
847 Geom::Path const &path_last = pathv.back();
848 unsigned int index = path_last.size_default();
849 if (index > 0) {
850 index--;
851 }
852 Geom::Curve const &lastcurve = path_last[index];
854 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
855 item_outline_add_marker( marker_obj, m,
856 Geom::Scale(i_style->stroke_width.computed), transform,
857 ret_pathv );
858 }
859 }
860 }
862 curve->unref();
863 }
865 delete res;
866 delete orig;
868 return ret_pathv;
869 }
871 void
872 sp_selected_path_outline(SPDesktop *desktop)
873 {
874 Inkscape::Selection *selection = sp_desktop_selection(desktop);
876 if (selection->isEmpty()) {
877 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
878 return;
879 }
881 bool did = false;
883 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
884 items != NULL;
885 items = items->next) {
887 SPItem *item = (SPItem *) items->data;
889 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
890 continue;
892 SPCurve *curve = NULL;
893 if (SP_IS_SHAPE(item)) {
894 curve = sp_shape_get_curve(SP_SHAPE(item));
895 if (curve == NULL)
896 continue;
897 }
898 if (SP_IS_TEXT(item)) {
899 curve = SP_TEXT(item)->getNormalizedBpath();
900 if (curve == NULL)
901 continue;
902 }
904 // pas de stroke pas de chocolat
905 if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
906 curve->unref();
907 continue;
908 }
910 // remember old stroke style, to be set on fill
911 SPStyle *i_style = SP_OBJECT_STYLE(item);
912 SPCSSAttr *ncss;
913 {
914 ncss = sp_css_attr_from_style(i_style, SP_STYLE_FLAG_ALWAYS);
915 gchar const *s_val = sp_repr_css_property(ncss, "stroke", NULL);
916 gchar const *s_opac = sp_repr_css_property(ncss, "stroke-opacity", NULL);
918 sp_repr_css_set_property(ncss, "stroke", "none");
919 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
920 sp_repr_css_set_property(ncss, "fill", s_val);
921 if ( s_opac ) {
922 sp_repr_css_set_property(ncss, "fill-opacity", s_opac);
923 } else {
924 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
925 }
926 sp_repr_css_unset_property(ncss, "marker-start");
927 sp_repr_css_unset_property(ncss, "marker-mid");
928 sp_repr_css_unset_property(ncss, "marker-end");
929 }
931 Geom::Matrix const transform(item->transform);
932 float const scale = transform.descrim();
933 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
934 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
936 float o_width, o_miter;
937 JoinType o_join;
938 ButtType o_butt;
940 {
941 int jointype, captype;
943 jointype = i_style->stroke_linejoin.computed;
944 captype = i_style->stroke_linecap.computed;
945 o_width = i_style->stroke_width.computed;
947 switch (jointype) {
948 case SP_STROKE_LINEJOIN_MITER:
949 o_join = join_pointy;
950 break;
951 case SP_STROKE_LINEJOIN_ROUND:
952 o_join = join_round;
953 break;
954 default:
955 o_join = join_straight;
956 break;
957 }
959 switch (captype) {
960 case SP_STROKE_LINECAP_SQUARE:
961 o_butt = butt_square;
962 break;
963 case SP_STROKE_LINECAP_ROUND:
964 o_butt = butt_round;
965 break;
966 default:
967 o_butt = butt_straight;
968 break;
969 }
971 if (o_width < 0.1)
972 o_width = 0.1;
973 o_miter = i_style->stroke_miterlimit.value * o_width;
974 }
976 SPCurve *curvetemp = curve_for_item(item);
977 if (curvetemp == NULL) {
978 curve->unref();
979 continue;
980 }
981 // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly.
982 Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curvetemp->get_pathvector() );
983 curvetemp->unref();
985 Path *orig = new Path;
986 orig->LoadPathVector(pathv);
988 Path *res = new Path;
989 res->SetBackData(false);
991 if (i_style->stroke_dash.n_dash) {
992 // For dashed strokes, use Stroke method, because Outline can't do dashes
993 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
995 orig->ConvertWithBackData(0.1);
997 orig->DashPolylineFromStyle(i_style, scale, 0);
999 Shape* theShape = new Shape;
1000 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
1001 0.5 * o_miter);
1002 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
1004 Shape *theRes = new Shape;
1006 theRes->ConvertToShape(theShape, fill_positive);
1008 Path *originaux[1];
1009 originaux[0] = res;
1010 theRes->ConvertToForme(orig, 1, originaux);
1012 res->Coalesce(5.0);
1014 delete theShape;
1015 delete theRes;
1017 } else {
1019 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
1021 orig->Coalesce(0.5 * o_width);
1023 Shape *theShape = new Shape;
1024 Shape *theRes = new Shape;
1026 res->ConvertWithBackData(1.0);
1027 res->Fill(theShape, 0);
1028 theRes->ConvertToShape(theShape, fill_positive);
1030 Path *originaux[1];
1031 originaux[0] = res;
1032 theRes->ConvertToForme(orig, 1, originaux);
1034 delete theShape;
1035 delete theRes;
1036 }
1038 if (orig->descr_cmd.size() <= 1) {
1039 // ca a merd\8e, ou bien le resultat est vide
1040 delete res;
1041 delete orig;
1042 continue;
1043 }
1045 did = true;
1047 // remember the position of the item
1048 gint pos = SP_OBJECT_REPR(item)->position();
1049 // remember parent
1050 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1051 // remember id
1052 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1053 // remember title
1054 gchar *title = item->title();
1055 // remember description
1056 gchar *desc = item->desc();
1058 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1060 SPDocument * doc = sp_desktop_document(desktop);
1061 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1062 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1064 // restore old style, but set old stroke style on fill
1065 sp_repr_css_change(repr, ncss, "style");
1067 sp_repr_css_attr_unref(ncss);
1069 gchar *str = orig->svg_dump_path();
1070 repr->setAttribute("d", str);
1071 g_free(str);
1073 if (mask)
1074 repr->setAttribute("mask", mask);
1075 if (clip_path)
1076 repr->setAttribute("clip-path", clip_path);
1078 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
1080 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1081 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
1083 // add the group to the parent
1084 parent->appendChild(g_repr);
1085 // move to the saved position
1086 g_repr->setPosition(pos > 0 ? pos : 0);
1088 g_repr->appendChild(repr);
1089 // restore title, description, id, transform
1090 repr->setAttribute("id", id);
1091 SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr);
1092 sp_item_write_transform(newitem, repr, transform);
1093 if (title) {
1094 newitem->setTitle(title);
1095 }
1096 if (desc) {
1097 newitem->setDesc(desc);
1098 }
1100 SPShape *shape = SP_SHAPE(item);
1102 Geom::PathVector const & pathv = curve->get_pathvector();
1104 // START marker
1105 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
1106 if ( SPObject *marker_obj = shape->marker[i] ) {
1107 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(pathv.front().front()));
1108 sp_selected_path_outline_add_marker( marker_obj, m,
1109 Geom::Scale(i_style->stroke_width.computed), transform,
1110 g_repr, xml_doc, doc );
1111 }
1112 }
1113 // MID marker
1114 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
1115 SPObject *midmarker_obj = shape->marker[i];
1116 if (!midmarker_obj) continue;
1117 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
1118 // START position
1119 if ( path_it != pathv.begin()
1120 && ! ((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
1121 {
1122 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
1123 sp_selected_path_outline_add_marker( midmarker_obj, m,
1124 Geom::Scale(i_style->stroke_width.computed), transform,
1125 g_repr, xml_doc, doc );
1126 }
1127 // MID position
1128 if (path_it->size_default() > 1) {
1129 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
1130 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
1131 while (curve_it2 != path_it->end_default())
1132 {
1133 /* Put marker between curve_it1 and curve_it2.
1134 * Loop to end_default (so including closing segment), because when a path is closed,
1135 * there should be a midpoint marker between last segment and closing straight line segment
1136 */
1137 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
1138 sp_selected_path_outline_add_marker(midmarker_obj, m,
1139 Geom::Scale(i_style->stroke_width.computed), transform,
1140 g_repr, xml_doc, doc);
1142 ++curve_it1;
1143 ++curve_it2;
1144 }
1145 }
1146 // END position
1147 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
1148 Geom::Curve const &lastcurve = path_it->back_default();
1149 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
1150 sp_selected_path_outline_add_marker( midmarker_obj, m,
1151 Geom::Scale(i_style->stroke_width.computed), transform,
1152 g_repr, xml_doc, doc );
1153 }
1154 }
1155 }
1156 // END marker
1157 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
1158 if ( SPObject *marker_obj = shape->marker[i] ) {
1159 /* Get reference to last curve in the path.
1160 * For moveto-only path, this returns the "closing line segment". */
1161 Geom::Path const &path_last = pathv.back();
1162 unsigned int index = path_last.size_default();
1163 if (index > 0) {
1164 index--;
1165 }
1166 Geom::Curve const &lastcurve = path_last[index];
1168 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
1169 sp_selected_path_outline_add_marker( marker_obj, m,
1170 Geom::Scale(i_style->stroke_width.computed), transform,
1171 g_repr, xml_doc, doc );
1172 }
1173 }
1175 selection->add(g_repr);
1177 Inkscape::GC::release(g_repr);
1180 } else {
1182 // add the new repr to the parent
1183 parent->appendChild(repr);
1185 // move to the saved position
1186 repr->setPosition(pos > 0 ? pos : 0);
1188 // restore title, description, id, transform
1189 repr->setAttribute("id", id);
1191 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1192 sp_item_write_transform(newitem, repr, transform);
1193 if (title) {
1194 newitem->setTitle(title);
1195 }
1196 if (desc) {
1197 newitem->setDesc(desc);
1198 }
1200 selection->add(repr);
1202 }
1204 Inkscape::GC::release(repr);
1206 curve->unref();
1207 selection->remove(item);
1208 SP_OBJECT(item)->deleteObject(false);
1210 }
1211 if (title) g_free(title);
1212 if (desc) g_free(desc);
1214 delete res;
1215 delete orig;
1216 }
1218 if (did) {
1219 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
1220 _("Convert stroke to path"));
1221 } else {
1222 // TRANSLATORS: "to outline" means "to convert stroke to path"
1223 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
1224 return;
1225 }
1226 }
1229 void
1230 sp_selected_path_offset(SPDesktop *desktop)
1231 {
1232 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1233 double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1235 sp_selected_path_do_offset(desktop, true, prefOffset);
1236 }
1237 void
1238 sp_selected_path_inset(SPDesktop *desktop)
1239 {
1240 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1241 double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1243 sp_selected_path_do_offset(desktop, false, prefOffset);
1244 }
1246 void
1247 sp_selected_path_offset_screen(SPDesktop *desktop, double pixels)
1248 {
1249 sp_selected_path_do_offset(desktop, true, pixels / desktop->current_zoom());
1250 }
1252 void
1253 sp_selected_path_inset_screen(SPDesktop *desktop, double pixels)
1254 {
1255 sp_selected_path_do_offset(desktop, false, pixels / desktop->current_zoom());
1256 }
1259 void sp_selected_path_create_offset_object_zero(SPDesktop *desktop)
1260 {
1261 sp_selected_path_create_offset_object(desktop, 0, false);
1262 }
1264 void sp_selected_path_create_offset(SPDesktop *desktop)
1265 {
1266 sp_selected_path_create_offset_object(desktop, 1, false);
1267 }
1268 void sp_selected_path_create_inset(SPDesktop *desktop)
1269 {
1270 sp_selected_path_create_offset_object(desktop, -1, false);
1271 }
1273 void sp_selected_path_create_updating_offset_object_zero(SPDesktop *desktop)
1274 {
1275 sp_selected_path_create_offset_object(desktop, 0, true);
1276 }
1278 void sp_selected_path_create_updating_offset(SPDesktop *desktop)
1279 {
1280 sp_selected_path_create_offset_object(desktop, 1, true);
1281 }
1282 void sp_selected_path_create_updating_inset(SPDesktop *desktop)
1283 {
1284 sp_selected_path_create_offset_object(desktop, -1, true);
1285 }
1287 void
1288 sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating)
1289 {
1290 Inkscape::Selection *selection;
1291 Inkscape::XML::Node *repr;
1292 SPItem *item;
1293 SPCurve *curve;
1294 gchar *style, *str;
1295 float o_width, o_miter;
1296 JoinType o_join;
1297 ButtType o_butt;
1299 curve = NULL;
1301 selection = sp_desktop_selection(desktop);
1303 item = selection->singleItem();
1305 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
1306 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
1307 return;
1308 }
1309 if (SP_IS_SHAPE(item))
1310 {
1311 curve = sp_shape_get_curve(SP_SHAPE(item));
1312 if (curve == NULL)
1313 return;
1314 }
1315 if (SP_IS_TEXT(item))
1316 {
1317 curve = SP_TEXT(item)->getNormalizedBpath();
1318 if (curve == NULL)
1319 return;
1320 }
1322 Geom::Matrix const transform(item->transform);
1324 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1326 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
1328 // remember the position of the item
1329 gint pos = SP_OBJECT_REPR(item)->position();
1330 // remember parent
1331 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1333 {
1334 SPStyle *i_style = SP_OBJECT(item)->style;
1335 int jointype, captype;
1337 jointype = i_style->stroke_linejoin.value;
1338 captype = i_style->stroke_linecap.value;
1339 o_width = i_style->stroke_width.computed;
1340 if (jointype == SP_STROKE_LINEJOIN_MITER)
1341 {
1342 o_join = join_pointy;
1343 }
1344 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1345 {
1346 o_join = join_round;
1347 }
1348 else
1349 {
1350 o_join = join_straight;
1351 }
1352 if (captype == SP_STROKE_LINECAP_SQUARE)
1353 {
1354 o_butt = butt_square;
1355 }
1356 else if (captype == SP_STROKE_LINECAP_ROUND)
1357 {
1358 o_butt = butt_round;
1359 }
1360 else
1361 {
1362 o_butt = butt_straight;
1363 }
1365 {
1366 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1367 o_width = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1368 }
1370 if (o_width < 0.01)
1371 o_width = 0.01;
1372 o_miter = i_style->stroke_miterlimit.value * o_width;
1373 }
1375 Path *orig = Path_for_item(item, true, false);
1376 if (orig == NULL)
1377 {
1378 g_free(style);
1379 curve->unref();
1380 return;
1381 }
1383 Path *res = new Path;
1384 res->SetBackData(false);
1386 {
1387 Shape *theShape = new Shape;
1388 Shape *theRes = new Shape;
1390 orig->ConvertWithBackData(1.0);
1391 orig->Fill(theShape, 0);
1393 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1394 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1395 if (val && strcmp(val, "nonzero") == 0)
1396 {
1397 theRes->ConvertToShape(theShape, fill_nonZero);
1398 }
1399 else if (val && strcmp(val, "evenodd") == 0)
1400 {
1401 theRes->ConvertToShape(theShape, fill_oddEven);
1402 }
1403 else
1404 {
1405 theRes->ConvertToShape(theShape, fill_nonZero);
1406 }
1408 Path *originaux[1];
1409 originaux[0] = orig;
1410 theRes->ConvertToForme(res, 1, originaux);
1412 delete theShape;
1413 delete theRes;
1414 }
1416 curve->unref();
1418 if (res->descr_cmd.size() <= 1)
1419 {
1420 // pas vraiment de points sur le resultat
1421 // donc il ne reste rien
1422 sp_document_done(sp_desktop_document(desktop),
1423 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1424 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1425 (updating ? _("Create linked offset")
1426 : _("Create dynamic offset")));
1427 selection->clear();
1429 delete res;
1430 delete orig;
1431 g_free(style);
1432 return;
1433 }
1435 {
1436 gchar tstr[80];
1438 tstr[79] = '\0';
1440 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1441 repr = xml_doc->createElement("svg:path");
1442 repr->setAttribute("sodipodi:type", "inkscape:offset");
1443 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1444 ? o_width
1445 : expand < 0
1446 ? -o_width
1447 : 0 ));
1449 str = res->svg_dump_path();
1450 repr->setAttribute("inkscape:original", str);
1451 g_free(str);
1453 if ( updating ) {
1454 char const *id = SP_OBJECT(item)->repr->attribute("id");
1455 char const *uri = g_strdup_printf("#%s", id);
1456 repr->setAttribute("xlink:href", uri);
1457 g_free((void *) uri);
1458 } else {
1459 repr->setAttribute("inkscape:href", NULL);
1460 }
1462 repr->setAttribute("style", style);
1464 // add the new repr to the parent
1465 parent->appendChild(repr);
1467 // move to the saved position
1468 repr->setPosition(pos > 0 ? pos : 0);
1470 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1472 if ( updating ) {
1473 // on conserve l'original
1474 // we reapply the transform to the original (offset will feel it)
1475 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1476 } else {
1477 // delete original, apply the transform to the offset
1478 SP_OBJECT(item)->deleteObject(false);
1479 sp_item_write_transform(nitem, repr, transform);
1480 }
1482 // The object just created from a temporary repr is only a seed.
1483 // We need to invoke its write which will update its real repr (in particular adding d=)
1484 SP_OBJECT(nitem)->updateRepr();
1486 Inkscape::GC::release(repr);
1488 selection->set(nitem);
1489 }
1491 sp_document_done(sp_desktop_document(desktop),
1492 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1493 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1494 (updating ? _("Create linked offset")
1495 : _("Create dynamic offset")));
1497 delete res;
1498 delete orig;
1500 g_free(style);
1501 }
1514 void
1515 sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset)
1516 {
1517 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1519 if (selection->isEmpty()) {
1520 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1521 return;
1522 }
1524 bool did = false;
1526 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1527 items != NULL;
1528 items = items->next) {
1530 SPItem *item = (SPItem *) items->data;
1532 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1533 continue;
1535 SPCurve *curve = NULL;
1536 if (SP_IS_SHAPE(item)) {
1537 curve = sp_shape_get_curve(SP_SHAPE(item));
1538 if (curve == NULL)
1539 continue;
1540 }
1541 if (SP_IS_TEXT(item)) {
1542 curve = SP_TEXT(item)->getNormalizedBpath();
1543 if (curve == NULL)
1544 continue;
1545 }
1547 Geom::Matrix const transform(item->transform);
1549 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1551 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1553 float o_width, o_miter;
1554 JoinType o_join;
1555 ButtType o_butt;
1557 {
1558 SPStyle *i_style = SP_OBJECT(item)->style;
1559 int jointype, captype;
1561 jointype = i_style->stroke_linejoin.value;
1562 captype = i_style->stroke_linecap.value;
1563 o_width = i_style->stroke_width.computed;
1565 switch (jointype) {
1566 case SP_STROKE_LINEJOIN_MITER:
1567 o_join = join_pointy;
1568 break;
1569 case SP_STROKE_LINEJOIN_ROUND:
1570 o_join = join_round;
1571 break;
1572 default:
1573 o_join = join_straight;
1574 break;
1575 }
1577 switch (captype) {
1578 case SP_STROKE_LINECAP_SQUARE:
1579 o_butt = butt_square;
1580 break;
1581 case SP_STROKE_LINECAP_ROUND:
1582 o_butt = butt_round;
1583 break;
1584 default:
1585 o_butt = butt_straight;
1586 break;
1587 }
1589 o_width = prefOffset;
1591 if (o_width < 0.1)
1592 o_width = 0.1;
1593 o_miter = i_style->stroke_miterlimit.value * o_width;
1594 }
1596 Path *orig = Path_for_item(item, false);
1597 if (orig == NULL) {
1598 g_free(style);
1599 curve->unref();
1600 continue;
1601 }
1603 Path *res = new Path;
1604 res->SetBackData(false);
1606 {
1607 Shape *theShape = new Shape;
1608 Shape *theRes = new Shape;
1610 orig->ConvertWithBackData(0.03);
1611 orig->Fill(theShape, 0);
1613 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1614 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1615 if (val && strcmp(val, "nonzero") == 0)
1616 {
1617 theRes->ConvertToShape(theShape, fill_nonZero);
1618 }
1619 else if (val && strcmp(val, "evenodd") == 0)
1620 {
1621 theRes->ConvertToShape(theShape, fill_oddEven);
1622 }
1623 else
1624 {
1625 theRes->ConvertToShape(theShape, fill_nonZero);
1626 }
1628 // et maintenant: offset
1629 // methode inexacte
1630 /* Path *originaux[1];
1631 originaux[0] = orig;
1632 theRes->ConvertToForme(res, 1, originaux);
1634 if (expand) {
1635 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1636 } else {
1637 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1638 }
1640 orig->ConvertWithBackData(1.0);
1641 orig->Fill(theShape, 0);
1642 theRes->ConvertToShape(theShape, fill_positive);
1643 originaux[0] = orig;
1644 theRes->ConvertToForme(res, 1, originaux);
1646 if (o_width >= 0.5) {
1647 // res->Coalesce(1.0);
1648 res->ConvertEvenLines(1.0);
1649 res->Simplify(1.0);
1650 } else {
1651 // res->Coalesce(o_width);
1652 res->ConvertEvenLines(1.0*o_width);
1653 res->Simplify(1.0 * o_width);
1654 } */
1655 // methode par makeoffset
1657 if (expand)
1658 {
1659 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1660 }
1661 else
1662 {
1663 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1664 }
1665 theRes->ConvertToShape(theShape, fill_positive);
1667 res->Reset();
1668 theRes->ConvertToForme(res);
1670 if (o_width >= 1.0)
1671 {
1672 res->ConvertEvenLines(1.0);
1673 res->Simplify(1.0);
1674 }
1675 else
1676 {
1677 res->ConvertEvenLines(1.0*o_width);
1678 res->Simplify(1.0 * o_width);
1679 }
1681 delete theShape;
1682 delete theRes;
1683 }
1685 did = true;
1687 curve->unref();
1688 // remember the position of the item
1689 gint pos = SP_OBJECT_REPR(item)->position();
1690 // remember parent
1691 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1692 // remember id
1693 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1695 selection->remove(item);
1696 SP_OBJECT(item)->deleteObject(false);
1698 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1700 gchar tstr[80];
1702 tstr[79] = '\0';
1704 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1705 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1707 repr->setAttribute("style", style);
1709 gchar *str = res->svg_dump_path();
1710 repr->setAttribute("d", str);
1711 g_free(str);
1713 // add the new repr to the parent
1714 parent->appendChild(repr);
1716 // move to the saved position
1717 repr->setPosition(pos > 0 ? pos : 0);
1719 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1721 // reapply the transform
1722 sp_item_write_transform(newitem, repr, transform);
1724 repr->setAttribute("id", id);
1726 selection->add(repr);
1728 Inkscape::GC::release(repr);
1729 }
1731 delete orig;
1732 delete res;
1733 }
1735 if (did) {
1736 sp_document_done(sp_desktop_document(desktop),
1737 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1738 (expand ? _("Outset path") : _("Inset path")));
1739 } else {
1740 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1741 return;
1742 }
1743 }
1746 static bool
1747 sp_selected_path_simplify_items(SPDesktop *desktop,
1748 Inkscape::Selection *selection, GSList *items,
1749 float threshold, bool justCoalesce,
1750 float angleLimit, bool breakableAngles,
1751 bool modifySelection);
1754 //return true if we changed something, else false
1755 bool
1756 sp_selected_path_simplify_item(SPDesktop *desktop,
1757 Inkscape::Selection *selection, SPItem *item,
1758 float threshold, bool justCoalesce,
1759 float angleLimit, bool breakableAngles,
1760 gdouble size, bool modifySelection)
1761 {
1762 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1763 return false;
1765 //If this is a group, do the children instead
1766 if (SP_IS_GROUP(item)) {
1767 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1769 return sp_selected_path_simplify_items(desktop, selection, items,
1770 threshold, justCoalesce,
1771 angleLimit, breakableAngles,
1772 false);
1773 }
1776 SPCurve *curve = NULL;
1778 if (SP_IS_SHAPE(item)) {
1779 curve = sp_shape_get_curve(SP_SHAPE(item));
1780 if (!curve)
1781 return false;
1782 }
1784 if (SP_IS_TEXT(item)) {
1785 curve = SP_TEXT(item)->getNormalizedBpath();
1786 if (!curve)
1787 return false;
1788 }
1790 // correct virtual size by full transform (bug #166937)
1791 size /= sp_item_i2doc_affine(item).descrim();
1793 // save the transform, to re-apply it after simplification
1794 Geom::Matrix const transform(item->transform);
1796 /*
1797 reset the transform, effectively transforming the item by transform.inverse();
1798 this is necessary so that the item is transformed twice back and forth,
1799 allowing all compensations to cancel out regardless of the preferences
1800 */
1801 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1803 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1804 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1805 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1807 Path *orig = Path_for_item(item, false);
1808 if (orig == NULL) {
1809 g_free(style);
1810 curve->unref();
1811 return false;
1812 }
1814 curve->unref();
1815 // remember the position of the item
1816 gint pos = SP_OBJECT_REPR(item)->position();
1817 // remember parent
1818 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1819 // remember id
1820 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1821 // remember path effect
1822 char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
1823 // remember title
1824 gchar *title = item->title();
1825 // remember description
1826 gchar *desc = item->desc();
1828 //If a group was selected, to not change the selection list
1829 if (modifySelection)
1830 selection->remove(item);
1832 SP_OBJECT(item)->deleteObject(false);
1834 if ( justCoalesce ) {
1835 orig->Coalesce(threshold * size);
1836 } else {
1837 orig->ConvertEvenLines(threshold * size);
1838 orig->Simplify(threshold * size);
1839 }
1841 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1842 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1844 // restore style, mask and clip-path
1845 repr->setAttribute("style", style);
1846 g_free(style);
1848 if ( mask ) {
1849 repr->setAttribute("mask", mask);
1850 g_free(mask);
1851 }
1853 if ( clip_path ) {
1854 repr->setAttribute("clip-path", clip_path);
1855 g_free(clip_path);
1856 }
1858 // restore path effect
1859 repr->setAttribute("inkscape:path-effect", patheffect);
1861 // path
1862 gchar *str = orig->svg_dump_path();
1863 if (patheffect)
1864 repr->setAttribute("inkscape:original-d", str);
1865 else
1866 repr->setAttribute("d", str);
1867 g_free(str);
1869 // restore id
1870 repr->setAttribute("id", id);
1872 // add the new repr to the parent
1873 parent->appendChild(repr);
1875 // move to the saved position
1876 repr->setPosition(pos > 0 ? pos : 0);
1878 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1880 // reapply the transform
1881 sp_item_write_transform(newitem, repr, transform);
1883 // restore title & description
1884 if (title) {
1885 newitem->setTitle(title);
1886 g_free(title);
1887 }
1888 if (desc) {
1889 newitem->setDesc(desc);
1890 g_free(desc);
1891 }
1893 //If we are not in a selected group
1894 if (modifySelection)
1895 selection->add(repr);
1897 Inkscape::GC::release(repr);
1899 // clean up
1900 if (orig) delete orig;
1902 return true;
1903 }
1906 bool
1907 sp_selected_path_simplify_items(SPDesktop *desktop,
1908 Inkscape::Selection *selection, GSList *items,
1909 float threshold, bool justCoalesce,
1910 float angleLimit, bool breakableAngles,
1911 bool modifySelection)
1912 {
1913 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1914 bool simplifyIndividualPaths = prefs->getBool("/options/simplifyindividualpaths/value");
1916 gchar *simplificationType;
1917 if (simplifyIndividualPaths) {
1918 simplificationType = _("Simplifying paths (separately):");
1919 } else {
1920 simplificationType = _("Simplifying paths:");
1921 }
1923 bool didSomething = false;
1925 Geom::OptRect selectionBbox = selection->bounds();
1926 if (!selectionBbox) {
1927 return false;
1928 }
1929 gdouble selectionSize = L2(selectionBbox->dimensions());
1931 gdouble simplifySize = selectionSize;
1933 int pathsSimplified = 0;
1934 int totalPathCount = g_slist_length(items);
1936 // set "busy" cursor
1937 desktop->setWaitingCursor();
1939 for (; items != NULL; items = items->next) {
1940 SPItem *item = (SPItem *) items->data;
1942 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1943 continue;
1945 if (simplifyIndividualPaths) {
1946 Geom::OptRect itemBbox = item->getBounds(sp_item_i2d_affine(item));
1947 if (itemBbox) {
1948 simplifySize = L2(itemBbox->dimensions());
1949 } else {
1950 simplifySize = 0;
1951 }
1952 }
1954 pathsSimplified++;
1956 if (pathsSimplified % 20 == 0) {
1957 gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."),
1958 simplificationType, pathsSimplified, totalPathCount);
1959 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1960 }
1962 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1963 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1964 }
1966 desktop->clearWaitingCursor();
1968 if (pathsSimplified > 20) {
1969 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1970 }
1972 return didSomething;
1973 }
1975 void
1976 sp_selected_path_simplify_selection(SPDesktop *desktop, float threshold, bool justCoalesce,
1977 float angleLimit, bool breakableAngles)
1978 {
1979 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1981 if (selection->isEmpty()) {
1982 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1983 _("Select <b>path(s)</b> to simplify."));
1984 return;
1985 }
1987 GSList *items = g_slist_copy((GSList *) selection->itemList());
1989 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1990 items, threshold,
1991 justCoalesce,
1992 angleLimit,
1993 breakableAngles, true);
1995 if (didSomething)
1996 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1997 _("Simplify"));
1998 else
1999 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
2001 }
2004 // globals for keeping track of accelerated simplify
2005 static double previousTime = 0.0;
2006 static gdouble simplifyMultiply = 1.0;
2008 void
2009 sp_selected_path_simplify(SPDesktop *desktop)
2010 {
2011 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2012 gdouble simplifyThreshold =
2013 prefs->getDouble("/options/simplifythreshold/value", 0.003);
2014 bool simplifyJustCoalesce = prefs->getBool("/options/simplifyjustcoalesce/value", 0);
2016 //Get the current time
2017 GTimeVal currentTimeVal;
2018 g_get_current_time(¤tTimeVal);
2019 double currentTime = currentTimeVal.tv_sec * 1000000 +
2020 currentTimeVal.tv_usec;
2022 //Was the previous call to this function recent? (<0.5 sec)
2023 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
2025 // add to the threshold 1/2 of its original value
2026 simplifyMultiply += 0.5;
2027 simplifyThreshold *= simplifyMultiply;
2029 } else {
2030 // reset to the default
2031 simplifyMultiply = 1;
2032 }
2034 //remember time for next call
2035 previousTime = currentTime;
2037 //g_print("%g\n", simplify_threshold);
2039 //Make the actual call
2040 sp_selected_path_simplify_selection(desktop, simplifyThreshold,
2041 simplifyJustCoalesce, 0.0, false);
2042 }
2046 // fonctions utilitaires
2048 bool
2049 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
2050 {
2051 if (who == NULL || a == NULL)
2052 return false;
2053 if (who == a)
2054 return true;
2055 return Ancetre(sp_repr_parent(a), who);
2056 }
2058 Path *
2059 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
2060 {
2061 SPCurve *curve = curve_for_item(item);
2063 if (curve == NULL)
2064 return NULL;
2066 Geom::PathVector *pathv = pathvector_for_curve(item, curve, doTransformation, transformFull, Geom::identity(), Geom::identity());
2067 curve->unref();
2069 Path *dest = new Path;
2070 dest->LoadPathVector(*pathv);
2071 delete pathv;
2073 return dest;
2074 }
2076 /*
2077 * NOTE: Returns empty pathvector if curve == NULL
2078 * TODO: see if calling this method can be optimized. All the pathvector copying might be slow.
2079 */
2080 Geom::PathVector*
2081 pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, Geom::Matrix extraPreAffine, Geom::Matrix extraPostAffine)
2082 {
2083 if (curve == NULL)
2084 return NULL;
2086 Geom::PathVector *dest = new Geom::PathVector;
2087 *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller!
2089 if (doTransformation) {
2090 if (transformFull) {
2091 *dest *= extraPreAffine * sp_item_i2doc_affine(item) * extraPostAffine;
2092 } else {
2093 *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine;
2094 }
2095 } else {
2096 *dest *= extraPreAffine * extraPostAffine;
2097 }
2099 return dest;
2100 }
2102 SPCurve* curve_for_item(SPItem *item)
2103 {
2104 if (!item)
2105 return NULL;
2107 SPCurve *curve = NULL;
2108 if (SP_IS_SHAPE(item)) {
2109 if (SP_IS_PATH(item)) {
2110 curve = sp_path_get_curve_for_edit(SP_PATH(item));
2111 } else {
2112 curve = sp_shape_get_curve(SP_SHAPE(item));
2113 }
2114 }
2115 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
2116 {
2117 curve = te_get_layout(item)->convertToCurves();
2118 }
2119 else if (SP_IS_IMAGE(item))
2120 {
2121 curve = sp_image_get_curve(SP_IMAGE(item));
2122 }
2124 return curve; // do not forget to unref the curve at some point!
2125 }
2127 boost::optional<Path::cut_position> get_nearest_position_on_Path(Path *path, Geom::Point p, unsigned seg)
2128 {
2129 //get nearest position on path
2130 Path::cut_position pos = path->PointToCurvilignPosition(p, seg);
2131 return pos;
2132 }
2134 Geom::Point get_point_on_Path(Path *path, int piece, double t)
2135 {
2136 Geom::Point p;
2137 path->PointAt(piece, t, p);
2138 return p;
2139 }
2142 /*
2143 Local Variables:
2144 mode:c++
2145 c-file-style:"stroustrup"
2146 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2147 indent-tabs-mode:nil
2148 fill-column:99
2149 End:
2150 */
2151 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :