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_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
131 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, 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 // mettre les elements de la liste dans l'ordre pour ces operations
142 if (bop == bool_op_diff || bop == bool_op_symdiff || 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 sp_document_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_symdiff || 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(sp_item_i2doc_affine(item_source));
456 sp_item_adjust_stroke(item_source, i2doc.descrim());
457 sp_item_adjust_pattern(item_source, i2doc);
458 sp_item_adjust_gradient(item_source, i2doc);
459 sp_item_adjust_livepatheffect(item_source, 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 (sp_item_i2doc_affine(parent_item));
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 = sp_document_repr_doc(desktop->doc());
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 = sp_document_repr_doc(desktop->doc());
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 sp_document_done(sp_desktop_document(desktop), verb, description);
598 }
600 delete res;
601 }
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 )
607 {
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 sp_item_write_transform(marker_item, m_repr, tr);
625 }
626 }
628 void
629 sp_selected_path_outline(SPDesktop *desktop)
630 {
631 Inkscape::Selection *selection = sp_desktop_selection(desktop);
633 if (selection->isEmpty()) {
634 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
635 return;
636 }
638 bool did = false;
640 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
641 items != NULL;
642 items = items->next) {
644 SPItem *item = (SPItem *) items->data;
646 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
647 continue;
649 SPCurve *curve = NULL;
650 if (SP_IS_SHAPE(item)) {
651 curve = sp_shape_get_curve(SP_SHAPE(item));
652 if (curve == NULL)
653 continue;
654 }
655 if (SP_IS_TEXT(item)) {
656 curve = SP_TEXT(item)->getNormalizedBpath();
657 if (curve == NULL)
658 continue;
659 }
661 // pas de stroke pas de chocolat
662 if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
663 curve->unref();
664 continue;
665 }
667 // remember old stroke style, to be set on fill
668 SPStyle *i_style = SP_OBJECT_STYLE(item);
669 SPCSSAttr *ncss;
670 {
671 ncss = sp_css_attr_from_style(i_style, SP_STYLE_FLAG_ALWAYS);
672 gchar const *s_val = sp_repr_css_property(ncss, "stroke", NULL);
673 gchar const *s_opac = sp_repr_css_property(ncss, "stroke-opacity", NULL);
675 sp_repr_css_set_property(ncss, "stroke", "none");
676 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
677 sp_repr_css_set_property(ncss, "fill", s_val);
678 if ( s_opac ) {
679 sp_repr_css_set_property(ncss, "fill-opacity", s_opac);
680 } else {
681 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
682 }
683 sp_repr_css_unset_property(ncss, "marker-start");
684 sp_repr_css_unset_property(ncss, "marker-mid");
685 sp_repr_css_unset_property(ncss, "marker-end");
686 }
688 Geom::Matrix const transform(item->transform);
689 float const scale = transform.descrim();
690 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
691 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
693 float o_width, o_miter;
694 JoinType o_join;
695 ButtType o_butt;
697 {
698 int jointype, captype;
700 jointype = i_style->stroke_linejoin.computed;
701 captype = i_style->stroke_linecap.computed;
702 o_width = i_style->stroke_width.computed;
704 switch (jointype) {
705 case SP_STROKE_LINEJOIN_MITER:
706 o_join = join_pointy;
707 break;
708 case SP_STROKE_LINEJOIN_ROUND:
709 o_join = join_round;
710 break;
711 default:
712 o_join = join_straight;
713 break;
714 }
716 switch (captype) {
717 case SP_STROKE_LINECAP_SQUARE:
718 o_butt = butt_square;
719 break;
720 case SP_STROKE_LINECAP_ROUND:
721 o_butt = butt_round;
722 break;
723 default:
724 o_butt = butt_straight;
725 break;
726 }
728 if (o_width < 0.1)
729 o_width = 0.1;
730 o_miter = i_style->stroke_miterlimit.value * o_width;
731 }
733 SPCurve *curvetemp = curve_for_item(item);
734 if (curvetemp == NULL) {
735 curve->unref();
736 continue;
737 }
738 // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly.
739 Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curvetemp->get_pathvector() );
740 curvetemp->unref();
742 Path *orig = new Path;
743 orig->LoadPathVector(pathv);
745 Path *res = new Path;
746 res->SetBackData(false);
748 if (i_style->stroke_dash.n_dash) {
749 // For dashed strokes, use Stroke method, because Outline can't do dashes
750 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
752 orig->ConvertWithBackData(0.1);
754 orig->DashPolylineFromStyle(i_style, scale, 0);
756 Shape* theShape = new Shape;
757 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
758 0.5 * o_miter);
759 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
761 Shape *theRes = new Shape;
763 theRes->ConvertToShape(theShape, fill_positive);
765 Path *originaux[1];
766 originaux[0] = res;
767 theRes->ConvertToForme(orig, 1, originaux);
769 res->Coalesce(5.0);
771 delete theShape;
772 delete theRes;
774 } else {
776 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
778 orig->Coalesce(0.5 * o_width);
780 Shape *theShape = new Shape;
781 Shape *theRes = new Shape;
783 res->ConvertWithBackData(1.0);
784 res->Fill(theShape, 0);
785 theRes->ConvertToShape(theShape, fill_positive);
787 Path *originaux[1];
788 originaux[0] = res;
789 theRes->ConvertToForme(orig, 1, originaux);
791 delete theShape;
792 delete theRes;
793 }
795 if (orig->descr_cmd.size() <= 1) {
796 // ca a merd\8e, ou bien le resultat est vide
797 delete res;
798 delete orig;
799 continue;
800 }
802 did = true;
804 // remember the position of the item
805 gint pos = SP_OBJECT_REPR(item)->position();
806 // remember parent
807 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
808 // remember id
809 char const *id = SP_OBJECT_REPR(item)->attribute("id");
810 // remember title
811 gchar *title = item->title();
812 // remember description
813 gchar *desc = item->desc();
815 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
817 SPDocument * doc = sp_desktop_document(desktop);
818 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
819 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
821 // restore old style, but set old stroke style on fill
822 sp_repr_css_change(repr, ncss, "style");
824 sp_repr_css_attr_unref(ncss);
826 gchar *str = orig->svg_dump_path();
827 repr->setAttribute("d", str);
828 g_free(str);
830 if (mask)
831 repr->setAttribute("mask", mask);
832 if (clip_path)
833 repr->setAttribute("clip-path", clip_path);
835 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
837 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
838 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
840 // add the group to the parent
841 parent->appendChild(g_repr);
842 // move to the saved position
843 g_repr->setPosition(pos > 0 ? pos : 0);
845 g_repr->appendChild(repr);
846 // restore title, description, id, transform
847 repr->setAttribute("id", id);
848 SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr);
849 sp_item_write_transform(newitem, repr, transform);
850 if (title) {
851 newitem->setTitle(title);
852 }
853 if (desc) {
854 newitem->setDesc(desc);
855 }
857 SPShape *shape = SP_SHAPE(item);
859 Geom::PathVector const & pathv = curve->get_pathvector();
860 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
861 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
862 if ( SPObject *marker_obj = shape->marker[i] ) {
863 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
864 sp_selected_path_outline_add_marker( marker_obj, m,
865 Geom::Scale(i_style->stroke_width.computed), transform,
866 g_repr, xml_doc, doc );
867 }
868 }
870 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
871 SPObject *midmarker_obj = shape->marker[i];
872 if ( midmarker_obj && (path_it->size_default() > 1) ) {
873 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
874 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
875 while (curve_it2 != path_it->end_default())
876 {
877 /* Put marker between curve_it1 and curve_it2.
878 * Loop to end_default (so including closing segment), because when a path is closed,
879 * there should be a midpoint marker between last segment and closing straight line segment
880 */
881 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
882 sp_selected_path_outline_add_marker(midmarker_obj, m,
883 Geom::Scale(i_style->stroke_width.computed), transform,
884 g_repr, xml_doc, doc);
886 ++curve_it1;
887 ++curve_it2;
888 }
889 }
890 }
892 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
893 if ( SPObject *marker_obj = shape->marker[i] ) {
894 /* Get reference to last curve in the path.
895 * For moveto-only path, this returns the "closing line segment". */
896 unsigned int index = path_it->size_default();
897 if (index > 0) {
898 index--;
899 }
900 Geom::Curve const &lastcurve = (*path_it)[index];
902 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
903 sp_selected_path_outline_add_marker( marker_obj, m,
904 Geom::Scale(i_style->stroke_width.computed), transform,
905 g_repr, xml_doc, doc );
906 }
907 }
908 }
910 selection->add(g_repr);
912 Inkscape::GC::release(g_repr);
915 } else {
917 // add the new repr to the parent
918 parent->appendChild(repr);
920 // move to the saved position
921 repr->setPosition(pos > 0 ? pos : 0);
923 // restore title, description, id, transform
924 repr->setAttribute("id", id);
926 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
927 sp_item_write_transform(newitem, repr, transform);
928 if (title) {
929 newitem->setTitle(title);
930 }
931 if (desc) {
932 newitem->setDesc(desc);
933 }
935 selection->add(repr);
937 }
939 Inkscape::GC::release(repr);
941 curve->unref();
942 selection->remove(item);
943 SP_OBJECT(item)->deleteObject(false);
945 }
946 if (title) g_free(title);
947 if (desc) g_free(desc);
949 delete res;
950 delete orig;
951 }
953 if (did) {
954 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
955 _("Convert stroke to path"));
956 } else {
957 // TRANSLATORS: "to outline" means "to convert stroke to path"
958 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
959 return;
960 }
961 }
964 void
965 sp_selected_path_offset(SPDesktop *desktop)
966 {
967 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
968 double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
970 sp_selected_path_do_offset(desktop, true, prefOffset);
971 }
972 void
973 sp_selected_path_inset(SPDesktop *desktop)
974 {
975 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
976 double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
978 sp_selected_path_do_offset(desktop, false, prefOffset);
979 }
981 void
982 sp_selected_path_offset_screen(SPDesktop *desktop, double pixels)
983 {
984 sp_selected_path_do_offset(desktop, true, pixels / desktop->current_zoom());
985 }
987 void
988 sp_selected_path_inset_screen(SPDesktop *desktop, double pixels)
989 {
990 sp_selected_path_do_offset(desktop, false, pixels / desktop->current_zoom());
991 }
994 void sp_selected_path_create_offset_object_zero(SPDesktop *desktop)
995 {
996 sp_selected_path_create_offset_object(desktop, 0, false);
997 }
999 void sp_selected_path_create_offset(SPDesktop *desktop)
1000 {
1001 sp_selected_path_create_offset_object(desktop, 1, false);
1002 }
1003 void sp_selected_path_create_inset(SPDesktop *desktop)
1004 {
1005 sp_selected_path_create_offset_object(desktop, -1, false);
1006 }
1008 void sp_selected_path_create_updating_offset_object_zero(SPDesktop *desktop)
1009 {
1010 sp_selected_path_create_offset_object(desktop, 0, true);
1011 }
1013 void sp_selected_path_create_updating_offset(SPDesktop *desktop)
1014 {
1015 sp_selected_path_create_offset_object(desktop, 1, true);
1016 }
1017 void sp_selected_path_create_updating_inset(SPDesktop *desktop)
1018 {
1019 sp_selected_path_create_offset_object(desktop, -1, true);
1020 }
1022 void
1023 sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating)
1024 {
1025 Inkscape::Selection *selection;
1026 Inkscape::XML::Node *repr;
1027 SPItem *item;
1028 SPCurve *curve;
1029 gchar *style, *str;
1030 float o_width, o_miter;
1031 JoinType o_join;
1032 ButtType o_butt;
1034 curve = NULL;
1036 selection = sp_desktop_selection(desktop);
1038 item = selection->singleItem();
1040 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
1041 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
1042 return;
1043 }
1044 if (SP_IS_SHAPE(item))
1045 {
1046 curve = sp_shape_get_curve(SP_SHAPE(item));
1047 if (curve == NULL)
1048 return;
1049 }
1050 if (SP_IS_TEXT(item))
1051 {
1052 curve = SP_TEXT(item)->getNormalizedBpath();
1053 if (curve == NULL)
1054 return;
1055 }
1057 Geom::Matrix const transform(item->transform);
1059 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1061 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
1063 // remember the position of the item
1064 gint pos = SP_OBJECT_REPR(item)->position();
1065 // remember parent
1066 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1068 {
1069 SPStyle *i_style = SP_OBJECT(item)->style;
1070 int jointype, captype;
1072 jointype = i_style->stroke_linejoin.value;
1073 captype = i_style->stroke_linecap.value;
1074 o_width = i_style->stroke_width.computed;
1075 if (jointype == SP_STROKE_LINEJOIN_MITER)
1076 {
1077 o_join = join_pointy;
1078 }
1079 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1080 {
1081 o_join = join_round;
1082 }
1083 else
1084 {
1085 o_join = join_straight;
1086 }
1087 if (captype == SP_STROKE_LINECAP_SQUARE)
1088 {
1089 o_butt = butt_square;
1090 }
1091 else if (captype == SP_STROKE_LINECAP_ROUND)
1092 {
1093 o_butt = butt_round;
1094 }
1095 else
1096 {
1097 o_butt = butt_straight;
1098 }
1100 {
1101 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1102 o_width = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1103 }
1105 if (o_width < 0.01)
1106 o_width = 0.01;
1107 o_miter = i_style->stroke_miterlimit.value * o_width;
1108 }
1110 Path *orig = Path_for_item(item, true, false);
1111 if (orig == NULL)
1112 {
1113 g_free(style);
1114 curve->unref();
1115 return;
1116 }
1118 Path *res = new Path;
1119 res->SetBackData(false);
1121 {
1122 Shape *theShape = new Shape;
1123 Shape *theRes = new Shape;
1125 orig->ConvertWithBackData(1.0);
1126 orig->Fill(theShape, 0);
1128 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1129 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1130 if (val && strcmp(val, "nonzero") == 0)
1131 {
1132 theRes->ConvertToShape(theShape, fill_nonZero);
1133 }
1134 else if (val && strcmp(val, "evenodd") == 0)
1135 {
1136 theRes->ConvertToShape(theShape, fill_oddEven);
1137 }
1138 else
1139 {
1140 theRes->ConvertToShape(theShape, fill_nonZero);
1141 }
1143 Path *originaux[1];
1144 originaux[0] = orig;
1145 theRes->ConvertToForme(res, 1, originaux);
1147 delete theShape;
1148 delete theRes;
1149 }
1151 curve->unref();
1153 if (res->descr_cmd.size() <= 1)
1154 {
1155 // pas vraiment de points sur le resultat
1156 // donc il ne reste rien
1157 sp_document_done(sp_desktop_document(desktop),
1158 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1159 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1160 (updating ? _("Create linked offset")
1161 : _("Create dynamic offset")));
1162 selection->clear();
1164 delete res;
1165 delete orig;
1166 g_free(style);
1167 return;
1168 }
1170 {
1171 gchar tstr[80];
1173 tstr[79] = '\0';
1175 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1176 repr = xml_doc->createElement("svg:path");
1177 repr->setAttribute("sodipodi:type", "inkscape:offset");
1178 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1179 ? o_width
1180 : expand < 0
1181 ? -o_width
1182 : 0 ));
1184 str = res->svg_dump_path();
1185 repr->setAttribute("inkscape:original", str);
1186 g_free(str);
1188 if ( updating ) {
1189 char const *id = SP_OBJECT(item)->repr->attribute("id");
1190 char const *uri = g_strdup_printf("#%s", id);
1191 repr->setAttribute("xlink:href", uri);
1192 g_free((void *) uri);
1193 } else {
1194 repr->setAttribute("inkscape:href", NULL);
1195 }
1197 repr->setAttribute("style", style);
1199 // add the new repr to the parent
1200 parent->appendChild(repr);
1202 // move to the saved position
1203 repr->setPosition(pos > 0 ? pos : 0);
1205 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1207 if ( updating ) {
1208 // on conserve l'original
1209 // we reapply the transform to the original (offset will feel it)
1210 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1211 } else {
1212 // delete original, apply the transform to the offset
1213 SP_OBJECT(item)->deleteObject(false);
1214 sp_item_write_transform(nitem, repr, transform);
1215 }
1217 // The object just created from a temporary repr is only a seed.
1218 // We need to invoke its write which will update its real repr (in particular adding d=)
1219 SP_OBJECT(nitem)->updateRepr();
1221 Inkscape::GC::release(repr);
1223 selection->set(nitem);
1224 }
1226 sp_document_done(sp_desktop_document(desktop),
1227 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1228 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1229 (updating ? _("Create linked offset")
1230 : _("Create dynamic offset")));
1232 delete res;
1233 delete orig;
1235 g_free(style);
1236 }
1249 void
1250 sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset)
1251 {
1252 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1254 if (selection->isEmpty()) {
1255 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1256 return;
1257 }
1259 bool did = false;
1261 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1262 items != NULL;
1263 items = items->next) {
1265 SPItem *item = (SPItem *) items->data;
1267 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1268 continue;
1270 SPCurve *curve = NULL;
1271 if (SP_IS_SHAPE(item)) {
1272 curve = sp_shape_get_curve(SP_SHAPE(item));
1273 if (curve == NULL)
1274 continue;
1275 }
1276 if (SP_IS_TEXT(item)) {
1277 curve = SP_TEXT(item)->getNormalizedBpath();
1278 if (curve == NULL)
1279 continue;
1280 }
1282 Geom::Matrix const transform(item->transform);
1284 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1286 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1288 float o_width, o_miter;
1289 JoinType o_join;
1290 ButtType o_butt;
1292 {
1293 SPStyle *i_style = SP_OBJECT(item)->style;
1294 int jointype, captype;
1296 jointype = i_style->stroke_linejoin.value;
1297 captype = i_style->stroke_linecap.value;
1298 o_width = i_style->stroke_width.computed;
1300 switch (jointype) {
1301 case SP_STROKE_LINEJOIN_MITER:
1302 o_join = join_pointy;
1303 break;
1304 case SP_STROKE_LINEJOIN_ROUND:
1305 o_join = join_round;
1306 break;
1307 default:
1308 o_join = join_straight;
1309 break;
1310 }
1312 switch (captype) {
1313 case SP_STROKE_LINECAP_SQUARE:
1314 o_butt = butt_square;
1315 break;
1316 case SP_STROKE_LINECAP_ROUND:
1317 o_butt = butt_round;
1318 break;
1319 default:
1320 o_butt = butt_straight;
1321 break;
1322 }
1324 o_width = prefOffset;
1326 if (o_width < 0.1)
1327 o_width = 0.1;
1328 o_miter = i_style->stroke_miterlimit.value * o_width;
1329 }
1331 Path *orig = Path_for_item(item, false);
1332 if (orig == NULL) {
1333 g_free(style);
1334 curve->unref();
1335 continue;
1336 }
1338 Path *res = new Path;
1339 res->SetBackData(false);
1341 {
1342 Shape *theShape = new Shape;
1343 Shape *theRes = new Shape;
1345 orig->ConvertWithBackData(0.03);
1346 orig->Fill(theShape, 0);
1348 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1349 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1350 if (val && strcmp(val, "nonzero") == 0)
1351 {
1352 theRes->ConvertToShape(theShape, fill_nonZero);
1353 }
1354 else if (val && strcmp(val, "evenodd") == 0)
1355 {
1356 theRes->ConvertToShape(theShape, fill_oddEven);
1357 }
1358 else
1359 {
1360 theRes->ConvertToShape(theShape, fill_nonZero);
1361 }
1363 // et maintenant: offset
1364 // methode inexacte
1365 /* Path *originaux[1];
1366 originaux[0] = orig;
1367 theRes->ConvertToForme(res, 1, originaux);
1369 if (expand) {
1370 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1371 } else {
1372 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1373 }
1375 orig->ConvertWithBackData(1.0);
1376 orig->Fill(theShape, 0);
1377 theRes->ConvertToShape(theShape, fill_positive);
1378 originaux[0] = orig;
1379 theRes->ConvertToForme(res, 1, originaux);
1381 if (o_width >= 0.5) {
1382 // res->Coalesce(1.0);
1383 res->ConvertEvenLines(1.0);
1384 res->Simplify(1.0);
1385 } else {
1386 // res->Coalesce(o_width);
1387 res->ConvertEvenLines(1.0*o_width);
1388 res->Simplify(1.0 * o_width);
1389 } */
1390 // methode par makeoffset
1392 if (expand)
1393 {
1394 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1395 }
1396 else
1397 {
1398 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1399 }
1400 theRes->ConvertToShape(theShape, fill_positive);
1402 res->Reset();
1403 theRes->ConvertToForme(res);
1405 if (o_width >= 1.0)
1406 {
1407 res->ConvertEvenLines(1.0);
1408 res->Simplify(1.0);
1409 }
1410 else
1411 {
1412 res->ConvertEvenLines(1.0*o_width);
1413 res->Simplify(1.0 * o_width);
1414 }
1416 delete theShape;
1417 delete theRes;
1418 }
1420 did = true;
1422 curve->unref();
1423 // remember the position of the item
1424 gint pos = SP_OBJECT_REPR(item)->position();
1425 // remember parent
1426 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1427 // remember id
1428 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1430 selection->remove(item);
1431 SP_OBJECT(item)->deleteObject(false);
1433 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1435 gchar tstr[80];
1437 tstr[79] = '\0';
1439 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1440 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1442 repr->setAttribute("style", style);
1444 gchar *str = res->svg_dump_path();
1445 repr->setAttribute("d", str);
1446 g_free(str);
1448 // add the new repr to the parent
1449 parent->appendChild(repr);
1451 // move to the saved position
1452 repr->setPosition(pos > 0 ? pos : 0);
1454 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1456 // reapply the transform
1457 sp_item_write_transform(newitem, repr, transform);
1459 repr->setAttribute("id", id);
1461 selection->add(repr);
1463 Inkscape::GC::release(repr);
1464 }
1466 delete orig;
1467 delete res;
1468 }
1470 if (did) {
1471 sp_document_done(sp_desktop_document(desktop),
1472 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1473 (expand ? _("Outset path") : _("Inset path")));
1474 } else {
1475 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1476 return;
1477 }
1478 }
1481 static bool
1482 sp_selected_path_simplify_items(SPDesktop *desktop,
1483 Inkscape::Selection *selection, GSList *items,
1484 float threshold, bool justCoalesce,
1485 float angleLimit, bool breakableAngles,
1486 bool modifySelection);
1489 //return true if we changed something, else false
1490 bool
1491 sp_selected_path_simplify_item(SPDesktop *desktop,
1492 Inkscape::Selection *selection, SPItem *item,
1493 float threshold, bool justCoalesce,
1494 float angleLimit, bool breakableAngles,
1495 gdouble size, bool modifySelection)
1496 {
1497 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1498 return false;
1500 //If this is a group, do the children instead
1501 if (SP_IS_GROUP(item)) {
1502 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1504 return sp_selected_path_simplify_items(desktop, selection, items,
1505 threshold, justCoalesce,
1506 angleLimit, breakableAngles,
1507 false);
1508 }
1511 SPCurve *curve = NULL;
1513 if (SP_IS_SHAPE(item)) {
1514 curve = sp_shape_get_curve(SP_SHAPE(item));
1515 if (!curve)
1516 return false;
1517 }
1519 if (SP_IS_TEXT(item)) {
1520 curve = SP_TEXT(item)->getNormalizedBpath();
1521 if (!curve)
1522 return false;
1523 }
1525 // correct virtual size by full transform (bug #166937)
1526 size /= sp_item_i2doc_affine(item).descrim();
1528 // save the transform, to re-apply it after simplification
1529 Geom::Matrix const transform(item->transform);
1531 /*
1532 reset the transform, effectively transforming the item by transform.inverse();
1533 this is necessary so that the item is transformed twice back and forth,
1534 allowing all compensations to cancel out regardless of the preferences
1535 */
1536 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1538 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1539 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1540 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1542 Path *orig = Path_for_item(item, false);
1543 if (orig == NULL) {
1544 g_free(style);
1545 curve->unref();
1546 return false;
1547 }
1549 curve->unref();
1550 // remember the position of the item
1551 gint pos = SP_OBJECT_REPR(item)->position();
1552 // remember parent
1553 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1554 // remember id
1555 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1556 // remember path effect
1557 char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
1558 // remember title
1559 gchar *title = item->title();
1560 // remember description
1561 gchar *desc = item->desc();
1563 //If a group was selected, to not change the selection list
1564 if (modifySelection)
1565 selection->remove(item);
1567 SP_OBJECT(item)->deleteObject(false);
1569 if ( justCoalesce ) {
1570 orig->Coalesce(threshold * size);
1571 } else {
1572 orig->ConvertEvenLines(threshold * size);
1573 orig->Simplify(threshold * size);
1574 }
1576 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1577 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1579 // restore style, mask and clip-path
1580 repr->setAttribute("style", style);
1581 g_free(style);
1583 if ( mask ) {
1584 repr->setAttribute("mask", mask);
1585 g_free(mask);
1586 }
1588 if ( clip_path ) {
1589 repr->setAttribute("clip-path", clip_path);
1590 g_free(clip_path);
1591 }
1593 // path
1594 gchar *str = orig->svg_dump_path();
1595 if (patheffect)
1596 repr->setAttribute("inkscape:original-d", str);
1597 else
1598 repr->setAttribute("d", str);
1599 g_free(str);
1601 // restore id
1602 repr->setAttribute("id", id);
1604 // add the new repr to the parent
1605 parent->appendChild(repr);
1607 // move to the saved position
1608 repr->setPosition(pos > 0 ? pos : 0);
1610 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1612 // reapply the transform
1613 sp_item_write_transform(newitem, repr, transform);
1615 // restore path effect
1616 repr->setAttribute("inkscape:path-effect", patheffect);
1618 // restore title & description
1619 if (title) {
1620 newitem->setTitle(title);
1621 g_free(title);
1622 }
1623 if (desc) {
1624 newitem->setDesc(desc);
1625 g_free(desc);
1626 }
1628 //If we are not in a selected group
1629 if (modifySelection)
1630 selection->add(repr);
1632 Inkscape::GC::release(repr);
1634 // clean up
1635 if (orig) delete orig;
1637 return true;
1638 }
1641 bool
1642 sp_selected_path_simplify_items(SPDesktop *desktop,
1643 Inkscape::Selection *selection, GSList *items,
1644 float threshold, bool justCoalesce,
1645 float angleLimit, bool breakableAngles,
1646 bool modifySelection)
1647 {
1648 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1649 bool simplifyIndividualPaths = prefs->getBool("/options/simplifyindividualpaths/value");
1651 gchar *simplificationType;
1652 if (simplifyIndividualPaths) {
1653 simplificationType = _("Simplifying paths (separately):");
1654 } else {
1655 simplificationType = _("Simplifying paths:");
1656 }
1658 bool didSomething = false;
1660 Geom::OptRect selectionBbox = selection->bounds();
1661 if (!selectionBbox) {
1662 return false;
1663 }
1664 gdouble selectionSize = L2(selectionBbox->dimensions());
1666 gdouble simplifySize = selectionSize;
1668 int pathsSimplified = 0;
1669 int totalPathCount = g_slist_length(items);
1671 // set "busy" cursor
1672 desktop->setWaitingCursor();
1674 for (; items != NULL; items = items->next) {
1675 SPItem *item = (SPItem *) items->data;
1677 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1678 continue;
1680 if (simplifyIndividualPaths) {
1681 Geom::OptRect itemBbox = item->getBounds(sp_item_i2d_affine(item));
1682 if (itemBbox) {
1683 simplifySize = L2(itemBbox->dimensions());
1684 } else {
1685 simplifySize = 0;
1686 }
1687 }
1689 pathsSimplified++;
1691 if (pathsSimplified % 20 == 0) {
1692 gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."),
1693 simplificationType, pathsSimplified, totalPathCount);
1694 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1695 }
1697 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1698 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1699 }
1701 desktop->clearWaitingCursor();
1703 if (pathsSimplified > 20) {
1704 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1705 }
1707 return didSomething;
1708 }
1710 void
1711 sp_selected_path_simplify_selection(SPDesktop *desktop, float threshold, bool justCoalesce,
1712 float angleLimit, bool breakableAngles)
1713 {
1714 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1716 if (selection->isEmpty()) {
1717 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1718 _("Select <b>path(s)</b> to simplify."));
1719 return;
1720 }
1722 GSList *items = g_slist_copy((GSList *) selection->itemList());
1724 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1725 items, threshold,
1726 justCoalesce,
1727 angleLimit,
1728 breakableAngles, true);
1730 if (didSomething)
1731 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1732 _("Simplify"));
1733 else
1734 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1736 }
1739 // globals for keeping track of accelerated simplify
1740 static double previousTime = 0.0;
1741 static gdouble simplifyMultiply = 1.0;
1743 void
1744 sp_selected_path_simplify(SPDesktop *desktop)
1745 {
1746 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1747 gdouble simplifyThreshold =
1748 prefs->getDouble("/options/simplifythreshold/value", 0.003);
1749 bool simplifyJustCoalesce = prefs->getBool("/options/simplifyjustcoalesce/value", 0);
1751 //Get the current time
1752 GTimeVal currentTimeVal;
1753 g_get_current_time(¤tTimeVal);
1754 double currentTime = currentTimeVal.tv_sec * 1000000 +
1755 currentTimeVal.tv_usec;
1757 //Was the previous call to this function recent? (<0.5 sec)
1758 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1760 // add to the threshold 1/2 of its original value
1761 simplifyMultiply += 0.5;
1762 simplifyThreshold *= simplifyMultiply;
1764 } else {
1765 // reset to the default
1766 simplifyMultiply = 1;
1767 }
1769 //remember time for next call
1770 previousTime = currentTime;
1772 //g_print("%g\n", simplify_threshold);
1774 //Make the actual call
1775 sp_selected_path_simplify_selection(desktop, simplifyThreshold,
1776 simplifyJustCoalesce, 0.0, false);
1777 }
1781 // fonctions utilitaires
1783 bool
1784 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1785 {
1786 if (who == NULL || a == NULL)
1787 return false;
1788 if (who == a)
1789 return true;
1790 return Ancetre(sp_repr_parent(a), who);
1791 }
1793 Path *
1794 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1795 {
1796 SPCurve *curve = curve_for_item(item);
1798 if (curve == NULL)
1799 return NULL;
1801 Geom::PathVector *pathv = pathvector_for_curve(item, curve, doTransformation, transformFull, Geom::identity(), Geom::identity());
1802 curve->unref();
1804 Path *dest = new Path;
1805 dest->LoadPathVector(*pathv);
1806 delete pathv;
1808 return dest;
1809 }
1811 /*
1812 * NOTE: Returns empty pathvector if curve == NULL
1813 * TODO: see if calling this method can be optimized. All the pathvector copying might be slow.
1814 */
1815 Geom::PathVector*
1816 pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, Geom::Matrix extraPreAffine, Geom::Matrix extraPostAffine)
1817 {
1818 if (curve == NULL)
1819 return NULL;
1821 Geom::PathVector *dest = new Geom::PathVector;
1822 *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller!
1824 if (doTransformation) {
1825 if (transformFull) {
1826 *dest *= extraPreAffine * sp_item_i2doc_affine(item) * extraPostAffine;
1827 } else {
1828 *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine;
1829 }
1830 } else {
1831 *dest *= extraPreAffine * extraPostAffine;
1832 }
1834 return dest;
1835 }
1837 SPCurve* curve_for_item(SPItem *item)
1838 {
1839 if (!item)
1840 return NULL;
1842 SPCurve *curve = NULL;
1843 if (SP_IS_SHAPE(item)) {
1844 if (SP_IS_PATH(item)) {
1845 curve = sp_path_get_curve_for_edit(SP_PATH(item));
1846 } else {
1847 curve = sp_shape_get_curve(SP_SHAPE(item));
1848 }
1849 }
1850 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
1851 {
1852 curve = te_get_layout(item)->convertToCurves();
1853 }
1854 else if (SP_IS_IMAGE(item))
1855 {
1856 curve = sp_image_get_curve(SP_IMAGE(item));
1857 }
1859 return curve; // do not forget to unref the curve at some point!
1860 }
1862 boost::optional<Path::cut_position> get_nearest_position_on_Path(Path *path, Geom::Point p, unsigned seg)
1863 {
1864 //get nearest position on path
1865 Path::cut_position pos = path->PointToCurvilignPosition(p, seg);
1866 return pos;
1867 }
1869 Geom::Point get_point_on_Path(Path *path, int piece, double t)
1870 {
1871 Geom::Point p;
1872 path->PointAt(piece, t, p);
1873 return p;
1874 }
1877 /*
1878 Local Variables:
1879 mode:c++
1880 c-file-style:"stroustrup"
1881 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1882 indent-tabs-mode:nil
1883 fill-column:99
1884 End:
1885 */
1886 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :