1 #define __SP_LIVAROT_C__
2 /*
3 * splivarot.cpp
4 * Inkscape
5 *
6 * Created by fred on Fri Dec 05 2003.
7 * tweaked endlessly by bulia byak <buliabyak@users.sf.net>
8 * public domain
9 *
10 */
12 /*
13 * contains lots of stitched pieces of path-chemistry.c
14 */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
20 #include <vector>
21 #include <glib/gmem.h>
22 #include "xml/repr.h"
23 #include "svg/svg.h"
24 #include "sp-path.h"
25 #include "sp-shape.h"
26 #include "marker.h"
27 #include "enums.h"
28 #include "sp-text.h"
29 #include "sp-item-group.h"
30 #include "style.h"
31 #include "inkscape.h"
32 #include "document.h"
33 #include "message-stack.h"
34 #include "selection.h"
35 #include "desktop-handles.h"
36 #include "desktop.h"
37 #include "display/canvas-bpath.h"
38 #include "display/curve.h"
39 #include <glibmm/i18n.h>
40 #include "prefs-utils.h"
42 #include "libnr/n-art-bpath.h"
43 #include "libnr/nr-path.h"
44 #include "xml/repr.h"
45 #include "xml/repr-sorting.h"
47 #include <libnr/nr-matrix-fns.h>
48 #include <libnr/nr-matrix-ops.h>
49 #include <libnr/nr-matrix-translate-ops.h>
50 #include <libnr/nr-scale-matrix-ops.h>
52 #include "livarot/Path.h"
53 #include "livarot/Shape.h"
55 #include "splivarot.h"
57 bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
59 void sp_selected_path_boolop(bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
60 void sp_selected_path_do_offset(bool expand, double prefOffset);
61 void sp_selected_path_create_offset_object(int expand, bool updating);
63 void
64 sp_selected_path_union()
65 {
66 sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
67 }
69 void
70 sp_selected_path_intersect()
71 {
72 sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
73 }
75 void
76 sp_selected_path_diff()
77 {
78 sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
79 }
81 void
82 sp_selected_path_symdiff()
83 {
84 sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
85 }
86 void
87 sp_selected_path_cut()
88 {
89 sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
90 }
91 void
92 sp_selected_path_slice()
93 {
94 sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path"));
95 }
98 // boolean operations
99 // take the source paths from the file, do the operation, delete the originals and add the results
100 void
101 sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description)
102 {
103 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
105 Inkscape::Selection *selection = sp_desktop_selection(desktop);
107 GSList *il = (GSList *) selection->itemList();
109 // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
110 if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
111 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
112 return;
113 }
114 else if ( g_slist_length(il) < 1 ) {
115 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
116 return;
117 }
119 if (g_slist_length(il) > 2) {
120 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
121 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
122 return;
123 }
124 }
126 // reverseOrderForOp marks whether the order of the list is the top->down order
127 // it's only used when there are 2 objects, and for operations who need to know the
128 // topmost object (differences, cuts)
129 bool reverseOrderForOp = false;
131 // mettre les elements de la liste dans l'ordre pour ces operations
132 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
133 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
134 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
135 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
137 if (a == NULL || b == NULL) {
138 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."));
139 return;
140 }
142 if (Ancetre(a, b)) {
143 // a is the parent of b, already in the proper order
144 } else if (Ancetre(b, a)) {
145 // reverse order
146 reverseOrderForOp = true;
147 } else {
149 // objects are not in parent/child relationship;
150 // find their lowest common ancestor
151 Inkscape::XML::Node *dad = LCA(a, b);
152 if (dad == NULL) {
153 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."));
154 return;
155 }
157 // find the children of the LCA that lead from it to the a and b
158 Inkscape::XML::Node *as = AncetreFils(a, dad);
159 Inkscape::XML::Node *bs = AncetreFils(b, dad);
161 // find out which comes first
162 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
163 if (child == as) {
164 /* a first, so reverse. */
165 reverseOrderForOp = true;
166 break;
167 }
168 if (child == bs)
169 break;
170 }
171 }
172 }
174 il = g_slist_copy(il);
176 // first check if all the input objects have shapes
177 // otherwise bail out
178 for (GSList *l = il; l != NULL; l = l->next)
179 {
180 SPItem *item = SP_ITEM(l->data);
181 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
182 {
183 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
184 g_slist_free(il);
185 return;
186 }
187 }
189 // extract the livarot Paths from the source objects
190 // also get the winding rule specified in the style
191 int nbOriginaux = g_slist_length(il);
192 std::vector<Path *> originaux(nbOriginaux);
193 std::vector<FillRule> origWind(nbOriginaux);
194 int curOrig;
195 {
196 curOrig = 0;
197 for (GSList *l = il; l != NULL; l = l->next)
198 {
199 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
200 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
201 if (val && strcmp(val, "nonzero") == 0) {
202 origWind[curOrig]= fill_nonZero;
203 } else if (val && strcmp(val, "evenodd") == 0) {
204 origWind[curOrig]= fill_oddEven;
205 } else {
206 origWind[curOrig]= fill_nonZero;
207 }
209 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
210 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
211 {
212 for (int i = curOrig; i >= 0; i--) delete originaux[i];
213 g_slist_free(il);
214 return;
215 }
216 curOrig++;
217 }
218 }
219 // reverse if needed
220 // note that the selection list keeps its order
221 if ( reverseOrderForOp ) {
222 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
223 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
224 }
226 // and work
227 // some temporary instances, first
228 Shape *theShapeA = new Shape;
229 Shape *theShapeB = new Shape;
230 Shape *theShape = new Shape;
231 Path *res = new Path;
232 res->SetBackData(false);
233 Path::cut_position *toCut=NULL;
234 int nbToCut=0;
236 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
237 // true boolean op
238 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
239 originaux[0]->ConvertWithBackData(0.1);
241 originaux[0]->Fill(theShape, 0);
243 theShapeA->ConvertToShape(theShape, origWind[0]);
245 curOrig = 1;
246 for (GSList *l = il->next; l != NULL; l = l->next) {
247 originaux[curOrig]->ConvertWithBackData(0.1);
249 originaux[curOrig]->Fill(theShape, curOrig);
251 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
253 // les elements arrivent en ordre inverse dans la liste
254 theShape->Booleen(theShapeB, theShapeA, bop);
256 {
257 Shape *swap = theShape;
258 theShape = theShapeA;
259 theShapeA = swap;
260 }
261 curOrig++;
262 }
264 {
265 Shape *swap = theShape;
266 theShape = theShapeA;
267 theShapeA = swap;
268 }
270 } else if ( bop == bool_op_cut ) {
271 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
272 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
273 // it is just uncrossed, and cleaned from duplicate edges and points
274 // then it's fed to Booleen() which will uncross it against the other path
275 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
276 // thus making a polygon. the weight of the edges of the cut are all 0, but
277 // the Booleen need to invert the ones inside the source polygon (for the subsequent
278 // ConvertToForme)
280 // the cut path needs to have the highest pathID in the back data
281 // that's how the Booleen() function knows it's an edge of the cut
283 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
284 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
285 // left it at 1.0. Investigate replacing this by a combination of difference and
286 // intersection of the same two paths. -- bb
287 {
288 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
289 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
290 }
291 originaux[0]->ConvertWithBackData(1.0);
293 originaux[0]->Fill(theShape, 0);
295 theShapeA->ConvertToShape(theShape, origWind[0]);
297 originaux[1]->ConvertWithBackData(1.0);
299 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
301 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
303 // les elements arrivent en ordre inverse dans la liste
304 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
306 } else if ( bop == bool_op_slice ) {
307 // slice is not really a boolean operation
308 // you just put the 2 shapes in a single polygon, uncross it
309 // the points where the degree is > 2 are intersections
310 // just check it's an intersection on the path you want to cut, and keep it
311 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
312 // make new subpath at each one of these positions
313 // inversion pour l'op\8eration
314 {
315 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
316 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
317 }
318 originaux[0]->ConvertWithBackData(1.0);
320 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
322 originaux[1]->ConvertWithBackData(1.0);
324 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
326 theShape->ConvertToShape(theShapeA, fill_justDont);
328 if ( theShape->hasBackData() ) {
329 // should always be the case, but ya never know
330 {
331 for (int i = 0; i < theShape->numberOfPoints(); i++) {
332 if ( theShape->getPoint(i).totalDegree() > 2 ) {
333 // possibly an intersection
334 // we need to check that at least one edge from the source path is incident to it
335 // before we declare it's an intersection
336 int cb = theShape->getPoint(i).incidentEdge[FIRST];
337 int nbOrig=0;
338 int nbOther=0;
339 int piece=-1;
340 float t=0.0;
341 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
342 if ( theShape->ebData[cb].pathID == 0 ) {
343 // the source has an edge incident to the point, get its position on the path
344 piece=theShape->ebData[cb].pieceID;
345 if ( theShape->getEdge(cb).st == i ) {
346 t=theShape->ebData[cb].tSt;
347 } else {
348 t=theShape->ebData[cb].tEn;
349 }
350 nbOrig++;
351 }
352 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
353 cb=theShape->NextAt(i, cb);
354 }
355 if ( nbOrig > 0 && nbOther > 0 ) {
356 // point incident to both path and cut: an intersection
357 // note that you only keep one position on the source; you could have degenerate
358 // cases where the source crosses itself at this point, and you wouyld miss an intersection
359 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
360 toCut[nbToCut].piece=piece;
361 toCut[nbToCut].t=t;
362 nbToCut++;
363 }
364 }
365 }
366 }
367 {
368 // i think it's useless now
369 int i = theShape->numberOfEdges() - 1;
370 for (;i>=0;i--) {
371 if ( theShape->ebData[i].pathID == 1 ) {
372 theShape->SubEdge(i);
373 }
374 }
375 }
377 }
378 }
380 int* nesting=NULL;
381 int* conts=NULL;
382 int nbNest=0;
383 // pour compenser le swap juste avant
384 if ( bop == bool_op_slice ) {
385 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
386 // res->ConvertForcedToMoveTo();
387 res->Copy(originaux[0]);
388 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
389 free(toCut);
390 } else if ( bop == bool_op_cut ) {
391 // il faut appeler pour desallouer PointData (pas vital, mais bon)
392 // the Booleen() function did not deallocated the point_data array in theShape, because this
393 // function needs it.
394 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
395 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
396 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
397 } else {
398 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
399 }
401 delete theShape;
402 delete theShapeA;
403 delete theShapeB;
404 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
406 if (res->descr_cmd.size() <= 1)
407 {
408 // only one command, presumably a moveto: it isn't a path
409 for (GSList *l = il; l != NULL; l = l->next)
410 {
411 SP_OBJECT(l->data)->deleteObject();
412 }
413 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
414 description);
415 selection->clear();
417 delete res;
418 g_slist_free(il);
419 return;
420 }
422 // get the source path object
423 SPObject *source;
424 if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
425 if (reverseOrderForOp) {
426 source = SP_OBJECT(il->data);
427 } else {
428 source = SP_OBJECT(il->next->data);
429 }
430 } else {
431 // find out the bottom object
432 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
434 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
436 source = sp_desktop_document(desktop)->
437 getObjectByRepr((Inkscape::XML::Node *)sorted->data);
439 g_slist_free(sorted);
440 }
442 // adjust style properties that depend on a possible transform in the source object in order
443 // to get a correct style attribute for the new path
444 SPItem* item_source = SP_ITEM(source);
445 NR::Matrix i2d = sp_item_i2d_affine(item_source);
446 sp_item_adjust_stroke(item_source, i2d.expansion());
447 sp_item_adjust_pattern(item_source, i2d);
448 sp_item_adjust_gradient(item_source, i2d);
450 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
452 // remember important aspects of the source path, to be restored
453 gint pos = repr_source->position();
454 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
455 gchar const *id = repr_source->attribute("id");
456 gchar const *style = repr_source->attribute("style");
457 gchar const *mask = repr_source->attribute("mask");
458 gchar const *clip_path = repr_source->attribute("clip-path");
460 // remove source paths
461 selection->clear();
462 for (GSList *l = il; l != NULL; l = l->next) {
463 // if this is the bottommost object,
464 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
465 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
466 SP_OBJECT(l->data)->deleteObject(false);
467 } else {
468 // delete the object for real, so that its clones can take appropriate action
469 SP_OBJECT(l->data)->deleteObject();
470 }
471 }
472 g_slist_free(il);
474 // premultiply by the inverse of parent's repr
475 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
476 NR::Matrix local = sp_item_i2doc_affine(parent_item);
477 gchar affinestr[80];
478 gchar *transform = NULL;
479 if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
480 transform = affinestr;
481 }
483 // now that we have the result, add it on the canvas
484 if ( bop == bool_op_cut || bop == bool_op_slice ) {
485 int nbRP=0;
486 Path** resPath;
487 if ( bop == bool_op_slice ) {
488 // there are moveto's at each intersection, but it's still one unique path
489 // so break it down and add each subpath independently
490 // we could call break_apart to do this, but while we have the description...
491 resPath=res->SubPaths(nbRP, false);
492 } else {
493 // cut operation is a bit wicked: you need to keep holes
494 // that's why you needed the nesting
495 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
496 // to get the path for each part of the polygon. that's why you need the nesting info:
497 // to know in wich subpath to add a subpath
498 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
500 // cleaning
501 if ( conts ) free(conts);
502 if ( nesting ) free(nesting);
503 }
505 // add all the pieces resulting from cut or slice
506 for (int i=0;i<nbRP;i++) {
507 gchar *d = resPath[i]->svg_dump_path();
509 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
510 repr->setAttribute("style", style);
511 if (mask)
512 repr->setAttribute("mask", mask);
513 if (clip_path)
514 repr->setAttribute("clip-path", clip_path);
516 repr->setAttribute("d", d);
517 g_free(d);
519 // for slice, remove fill
520 if (bop == bool_op_slice) {
521 SPCSSAttr *css;
523 css = sp_repr_css_attr_new();
524 sp_repr_css_set_property(css, "fill", "none");
526 sp_repr_css_change(repr, css, "style");
528 sp_repr_css_attr_unref(css);
529 }
531 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
532 // this means it's basically random which of the pieces inherits the original's id and clones
533 // a better algorithm might figure out e.g. the biggest piece
534 repr->setAttribute("id", id);
536 repr->setAttribute("transform", transform);
538 // add the new repr to the parent
539 parent->appendChild(repr);
541 // move to the saved position
542 repr->setPosition(pos > 0 ? pos : 0);
544 selection->add(repr);
545 Inkscape::GC::release(repr);
547 delete resPath[i];
548 }
549 if ( resPath ) free(resPath);
551 } else {
552 gchar *d = res->svg_dump_path();
554 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
555 repr->setAttribute("style", style);
557 if ( mask )
558 repr->setAttribute("mask", mask);
560 if ( clip_path )
561 repr->setAttribute("clip-path", clip_path);
563 repr->setAttribute("d", d);
564 g_free(d);
566 repr->setAttribute("transform", transform);
568 repr->setAttribute("id", id);
569 parent->appendChild(repr);
570 repr->setPosition(pos > 0 ? pos : 0);
572 selection->add(repr);
573 Inkscape::GC::release(repr);
574 }
576 sp_document_done(sp_desktop_document(desktop), verb, description);
578 delete res;
579 }
582 void
583 sp_selected_path_outline()
584 {
585 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
587 Inkscape::Selection *selection = sp_desktop_selection(desktop);
589 if (selection->isEmpty()) {
590 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
591 return;
592 }
594 bool did = false;
596 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
597 items != NULL;
598 items = items->next) {
600 SPItem *item = (SPItem *) items->data;
602 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
603 continue;
605 SPCurve *curve = NULL;
606 if (SP_IS_SHAPE(item)) {
607 curve = sp_shape_get_curve(SP_SHAPE(item));
608 if (curve == NULL)
609 continue;
610 }
611 if (SP_IS_TEXT(item)) {
612 curve = SP_TEXT(item)->getNormalizedBpath();
613 if (curve == NULL)
614 continue;
615 }
617 { // pas de stroke pas de chocolat
618 SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
619 gchar const *val = sp_repr_css_property(css, "stroke", NULL);
621 if (val == NULL || strcmp(val, "none") == 0) {
622 sp_curve_unref(curve);
623 continue;
624 }
625 }
627 // remember old stroke style, to be set on fill
628 SPCSSAttr *ncss;
629 {
630 SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
631 gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
632 gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
634 ncss = sp_repr_css_attr_new();
636 sp_repr_css_set_property(ncss, "stroke", "none");
637 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
638 sp_repr_css_set_property(ncss, "fill", val);
639 if ( opac ) {
640 sp_repr_css_set_property(ncss, "fill-opacity", opac);
641 } else {
642 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
643 }
644 sp_repr_css_unset_property(ncss, "marker-start");
645 sp_repr_css_unset_property(ncss, "marker-mid");
646 sp_repr_css_unset_property(ncss, "marker-end");
647 }
649 NR::Matrix const transform(item->transform);
650 float const scale = transform.expansion();
651 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
652 SPStyle *i_style = SP_OBJECT(item)->style;
653 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
654 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
656 float o_width, o_miter;
657 JoinType o_join;
658 ButtType o_butt;
660 {
661 int jointype, captype;
663 jointype = i_style->stroke_linejoin.computed;
664 captype = i_style->stroke_linecap.computed;
665 o_width = i_style->stroke_width.computed;
667 switch (jointype) {
668 case SP_STROKE_LINEJOIN_MITER:
669 o_join = join_pointy;
670 break;
671 case SP_STROKE_LINEJOIN_ROUND:
672 o_join = join_round;
673 break;
674 default:
675 o_join = join_straight;
676 break;
677 }
679 switch (captype) {
680 case SP_STROKE_LINECAP_SQUARE:
681 o_butt = butt_square;
682 break;
683 case SP_STROKE_LINECAP_ROUND:
684 o_butt = butt_round;
685 break;
686 default:
687 o_butt = butt_straight;
688 break;
689 }
691 if (o_width < 0.1)
692 o_width = 0.1;
693 o_miter = i_style->stroke_miterlimit.value * o_width;
694 }
696 Path *orig = Path_for_item(item, false);
697 if (orig == NULL) {
698 g_free(style);
699 sp_curve_unref(curve);
700 continue;
701 }
703 Path *res = new Path;
704 res->SetBackData(false);
706 if (i_style->stroke_dash.n_dash) {
707 // For dashed strokes, use Stroke method, because Outline can't do dashes
708 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
710 orig->ConvertWithBackData(0.1);
712 orig->DashPolylineFromStyle(i_style, scale, 0);
714 Shape* theShape = new Shape;
715 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
716 0.5 * o_miter);
717 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
719 Shape *theRes = new Shape;
721 theRes->ConvertToShape(theShape, fill_positive);
723 Path *originaux[1];
724 originaux[0] = res;
725 theRes->ConvertToForme(orig, 1, originaux);
727 res->Coalesce(5.0);
729 delete theShape;
730 delete theRes;
732 } else {
734 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
736 orig->Coalesce(0.5 * o_width);
738 Shape *theShape = new Shape;
739 Shape *theRes = new Shape;
741 res->ConvertWithBackData(1.0);
742 res->Fill(theShape, 0);
743 theRes->ConvertToShape(theShape, fill_positive);
745 Path *originaux[1];
746 originaux[0] = res;
747 theRes->ConvertToForme(orig, 1, originaux);
749 delete theShape;
750 delete theRes;
751 }
753 if (orig->descr_cmd.size() <= 1) {
754 // ca a merd\8e, ou bien le resultat est vide
755 delete res;
756 delete orig;
757 g_free(style);
758 continue;
759 }
761 did = true;
763 // remember the position of the item
764 gint pos = SP_OBJECT_REPR(item)->position();
765 // remember parent
766 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
767 // remember id
768 char const *id = SP_OBJECT_REPR(item)->attribute("id");
770 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
772 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
774 // restore old style
775 repr->setAttribute("style", style);
777 // set old stroke style on fill
778 sp_repr_css_change(repr, ncss, "style");
780 sp_repr_css_attr_unref(ncss);
782 gchar *str = orig->svg_dump_path();
783 repr->setAttribute("d", str);
784 g_free(str);
786 if (mask)
787 repr->setAttribute("mask", mask);
788 if (clip_path)
789 repr->setAttribute("clip-path", clip_path);
791 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
793 Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
795 // add the group to the parent
796 parent->appendChild(g_repr);
797 // move to the saved position
798 g_repr->setPosition(pos > 0 ? pos : 0);
800 g_repr->appendChild(repr);
801 repr->setAttribute("id", id);
802 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
803 sp_item_write_transform(newitem, repr, transform);
805 SPShape *shape = SP_SHAPE(item);
807 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
808 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
809 if (sp_shape_marker_required (shape, m, bp)) {
811 SPMarker* marker = SP_MARKER (shape->marker[m]);
812 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
814 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
816 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
817 tr = NR::scale(i_style->stroke_width.computed) * tr;
818 }
820 // total marker transform
821 tr = marker_item->transform * marker->c2p * tr * transform;
823 if (SP_OBJECT_REPR(marker_item)) {
824 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
825 g_repr->appendChild(m_repr);
826 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
827 sp_item_write_transform(marker_item, m_repr, tr);
828 }
829 }
830 }
831 }
834 selection->add(g_repr);
836 Inkscape::GC::release(g_repr);
839 } else {
841 // add the new repr to the parent
842 parent->appendChild(repr);
844 // move to the saved position
845 repr->setPosition(pos > 0 ? pos : 0);
847 repr->setAttribute("id", id);
849 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
850 sp_item_write_transform(newitem, repr, transform);
852 selection->add(repr);
854 }
856 Inkscape::GC::release(repr);
858 sp_curve_unref(curve);
859 selection->remove(item);
860 SP_OBJECT(item)->deleteObject(false);
862 }
864 delete res;
865 delete orig;
866 g_free(style);
868 }
870 if (did) {
871 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
872 _("Convert stroke to path"));
873 } else {
874 // TRANSLATORS: "to outline" means "to convert stroke to path"
875 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
876 return;
877 }
878 }
881 void
882 sp_selected_path_offset()
883 {
884 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
886 sp_selected_path_do_offset(true, prefOffset);
887 }
888 void
889 sp_selected_path_inset()
890 {
891 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
893 sp_selected_path_do_offset(false, prefOffset);
894 }
896 void
897 sp_selected_path_offset_screen(double pixels)
898 {
899 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
900 }
902 void
903 sp_selected_path_inset_screen(double pixels)
904 {
905 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
906 }
909 void sp_selected_path_create_offset_object_zero()
910 {
911 sp_selected_path_create_offset_object(0, false);
912 }
914 void sp_selected_path_create_offset()
915 {
916 sp_selected_path_create_offset_object(1, false);
917 }
918 void sp_selected_path_create_inset()
919 {
920 sp_selected_path_create_offset_object(-1, false);
921 }
923 void sp_selected_path_create_updating_offset_object_zero()
924 {
925 sp_selected_path_create_offset_object(0, true);
926 }
928 void sp_selected_path_create_updating_offset()
929 {
930 sp_selected_path_create_offset_object(1, true);
931 }
932 void sp_selected_path_create_updating_inset()
933 {
934 sp_selected_path_create_offset_object(-1, true);
935 }
937 void
938 sp_selected_path_create_offset_object(int expand, bool updating)
939 {
940 Inkscape::Selection *selection;
941 Inkscape::XML::Node *repr;
942 SPItem *item;
943 SPCurve *curve;
944 gchar *style, *str;
945 SPDesktop *desktop;
946 float o_width, o_miter;
947 JoinType o_join;
948 ButtType o_butt;
950 curve = NULL;
952 desktop = SP_ACTIVE_DESKTOP;
954 selection = sp_desktop_selection(desktop);
956 item = selection->singleItem();
958 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
959 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
960 return;
961 }
962 if (SP_IS_SHAPE(item))
963 {
964 curve = sp_shape_get_curve(SP_SHAPE(item));
965 if (curve == NULL)
966 return;
967 }
968 if (SP_IS_TEXT(item))
969 {
970 curve = SP_TEXT(item)->getNormalizedBpath();
971 if (curve == NULL)
972 return;
973 }
975 NR::Matrix const transform(item->transform);
977 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
979 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
981 // remember the position of the item
982 gint pos = SP_OBJECT_REPR(item)->position();
983 // remember parent
984 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
986 {
987 SPStyle *i_style = SP_OBJECT(item)->style;
988 int jointype, captype;
990 jointype = i_style->stroke_linejoin.value;
991 captype = i_style->stroke_linecap.value;
992 o_width = i_style->stroke_width.computed;
993 if (jointype == SP_STROKE_LINEJOIN_MITER)
994 {
995 o_join = join_pointy;
996 }
997 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
998 {
999 o_join = join_round;
1000 }
1001 else
1002 {
1003 o_join = join_straight;
1004 }
1005 if (captype == SP_STROKE_LINECAP_SQUARE)
1006 {
1007 o_butt = butt_square;
1008 }
1009 else if (captype == SP_STROKE_LINECAP_ROUND)
1010 {
1011 o_butt = butt_round;
1012 }
1013 else
1014 {
1015 o_butt = butt_straight;
1016 }
1018 {
1019 double prefOffset = 1.0;
1020 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
1021 o_width = prefOffset;
1022 }
1024 if (o_width < 0.01)
1025 o_width = 0.01;
1026 o_miter = i_style->stroke_miterlimit.value * o_width;
1027 }
1029 Path *orig = Path_for_item(item, true, false);
1030 if (orig == NULL)
1031 {
1032 g_free(style);
1033 sp_curve_unref(curve);
1034 return;
1035 }
1037 Path *res = new Path;
1038 res->SetBackData(false);
1040 {
1041 Shape *theShape = new Shape;
1042 Shape *theRes = new Shape;
1044 orig->ConvertWithBackData(1.0);
1045 orig->Fill(theShape, 0);
1047 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1048 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1049 if (val && strcmp(val, "nonzero") == 0)
1050 {
1051 theRes->ConvertToShape(theShape, fill_nonZero);
1052 }
1053 else if (val && strcmp(val, "evenodd") == 0)
1054 {
1055 theRes->ConvertToShape(theShape, fill_oddEven);
1056 }
1057 else
1058 {
1059 theRes->ConvertToShape(theShape, fill_nonZero);
1060 }
1062 Path *originaux[1];
1063 originaux[0] = orig;
1064 theRes->ConvertToForme(res, 1, originaux);
1066 delete theShape;
1067 delete theRes;
1068 }
1070 sp_curve_unref(curve);
1072 if (res->descr_cmd.size() <= 1)
1073 {
1074 // pas vraiment de points sur le resultat
1075 // donc il ne reste rien
1076 sp_document_done(sp_desktop_document(desktop),
1077 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1078 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1079 (updating ? _("Create linked offset")
1080 : _("Create dynamic offset")));
1081 selection->clear();
1083 delete res;
1084 delete orig;
1085 g_free(style);
1086 return;
1087 }
1089 {
1090 gchar tstr[80];
1092 tstr[79] = '\0';
1094 repr = sp_repr_new("svg:path");
1095 repr->setAttribute("sodipodi:type", "inkscape:offset");
1096 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1097 ? o_width
1098 : expand < 0
1099 ? -o_width
1100 : 0 ));
1102 str = res->svg_dump_path();
1103 repr->setAttribute("inkscape:original", str);
1104 g_free(str);
1106 if ( updating ) {
1107 char const *id = SP_OBJECT(item)->repr->attribute("id");
1108 char const *uri = g_strdup_printf("#%s", id);
1109 repr->setAttribute("xlink:href", uri);
1110 g_free((void *) uri);
1111 } else {
1112 repr->setAttribute("inkscape:href", NULL);
1113 }
1115 repr->setAttribute("style", style);
1117 // add the new repr to the parent
1118 parent->appendChild(repr);
1120 // move to the saved position
1121 repr->setPosition(pos > 0 ? pos : 0);
1123 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1125 if ( updating ) {
1126 // on conserve l'original
1127 // we reapply the transform to the original (offset will feel it)
1128 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1129 } else {
1130 // delete original, apply the transform to the offset
1131 SP_OBJECT(item)->deleteObject(false);
1132 sp_item_write_transform(nitem, repr, transform);
1133 }
1135 // The object just created from a temporary repr is only a seed.
1136 // We need to invoke its write which will update its real repr (in particular adding d=)
1137 SP_OBJECT(nitem)->updateRepr();
1139 Inkscape::GC::release(repr);
1141 selection->set(nitem);
1142 }
1144 sp_document_done(sp_desktop_document(desktop),
1145 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1146 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1147 (updating ? _("Create linked offset")
1148 : _("Create dynamic offset")));
1150 delete res;
1151 delete orig;
1153 g_free(style);
1154 }
1167 void
1168 sp_selected_path_do_offset(bool expand, double prefOffset)
1169 {
1170 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1172 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1174 if (selection->isEmpty()) {
1175 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1176 return;
1177 }
1179 bool did = false;
1181 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1182 items != NULL;
1183 items = items->next) {
1185 SPItem *item = (SPItem *) items->data;
1187 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1188 continue;
1190 SPCurve *curve = NULL;
1191 if (SP_IS_SHAPE(item)) {
1192 curve = sp_shape_get_curve(SP_SHAPE(item));
1193 if (curve == NULL)
1194 continue;
1195 }
1196 if (SP_IS_TEXT(item)) {
1197 curve = SP_TEXT(item)->getNormalizedBpath();
1198 if (curve == NULL)
1199 continue;
1200 }
1202 NR::Matrix const transform(item->transform);
1204 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1206 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1208 float o_width, o_miter;
1209 JoinType o_join;
1210 ButtType o_butt;
1212 {
1213 SPStyle *i_style = SP_OBJECT(item)->style;
1214 int jointype, captype;
1216 jointype = i_style->stroke_linejoin.value;
1217 captype = i_style->stroke_linecap.value;
1218 o_width = i_style->stroke_width.computed;
1220 switch (jointype) {
1221 case SP_STROKE_LINEJOIN_MITER:
1222 o_join = join_pointy;
1223 break;
1224 case SP_STROKE_LINEJOIN_ROUND:
1225 o_join = join_round;
1226 break;
1227 default:
1228 o_join = join_straight;
1229 break;
1230 }
1232 switch (captype) {
1233 case SP_STROKE_LINECAP_SQUARE:
1234 o_butt = butt_square;
1235 break;
1236 case SP_STROKE_LINECAP_ROUND:
1237 o_butt = butt_round;
1238 break;
1239 default:
1240 o_butt = butt_straight;
1241 break;
1242 }
1244 o_width = prefOffset;
1246 if (o_width < 0.1)
1247 o_width = 0.1;
1248 o_miter = i_style->stroke_miterlimit.value * o_width;
1249 }
1251 Path *orig = Path_for_item(item, false);
1252 if (orig == NULL) {
1253 g_free(style);
1254 sp_curve_unref(curve);
1255 continue;
1256 }
1258 Path *res = new Path;
1259 res->SetBackData(false);
1261 {
1262 Shape *theShape = new Shape;
1263 Shape *theRes = new Shape;
1265 orig->ConvertWithBackData(0.03);
1266 orig->Fill(theShape, 0);
1268 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1269 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1270 if (val && strcmp(val, "nonzero") == 0)
1271 {
1272 theRes->ConvertToShape(theShape, fill_nonZero);
1273 }
1274 else if (val && strcmp(val, "evenodd") == 0)
1275 {
1276 theRes->ConvertToShape(theShape, fill_oddEven);
1277 }
1278 else
1279 {
1280 theRes->ConvertToShape(theShape, fill_nonZero);
1281 }
1283 // et maintenant: offset
1284 // methode inexacte
1285 /* Path *originaux[1];
1286 originaux[0] = orig;
1287 theRes->ConvertToForme(res, 1, originaux);
1289 if (expand) {
1290 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1291 } else {
1292 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1293 }
1295 orig->ConvertWithBackData(1.0);
1296 orig->Fill(theShape, 0);
1297 theRes->ConvertToShape(theShape, fill_positive);
1298 originaux[0] = orig;
1299 theRes->ConvertToForme(res, 1, originaux);
1301 if (o_width >= 0.5) {
1302 // res->Coalesce(1.0);
1303 res->ConvertEvenLines(1.0);
1304 res->Simplify(1.0);
1305 } else {
1306 // res->Coalesce(o_width);
1307 res->ConvertEvenLines(1.0*o_width);
1308 res->Simplify(1.0 * o_width);
1309 } */
1310 // methode par makeoffset
1312 if (expand)
1313 {
1314 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1315 }
1316 else
1317 {
1318 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1319 }
1320 theRes->ConvertToShape(theShape, fill_positive);
1322 res->Reset();
1323 theRes->ConvertToForme(res);
1325 if (o_width >= 1.0)
1326 {
1327 res->ConvertEvenLines(1.0);
1328 res->Simplify(1.0);
1329 }
1330 else
1331 {
1332 res->ConvertEvenLines(1.0*o_width);
1333 res->Simplify(1.0 * o_width);
1334 }
1336 delete theShape;
1337 delete theRes;
1338 }
1340 did = true;
1342 sp_curve_unref(curve);
1343 // remember the position of the item
1344 gint pos = SP_OBJECT_REPR(item)->position();
1345 // remember parent
1346 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1347 // remember id
1348 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1350 selection->remove(item);
1351 SP_OBJECT(item)->deleteObject(false);
1353 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1355 gchar tstr[80];
1357 tstr[79] = '\0';
1359 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1361 repr->setAttribute("style", style);
1363 gchar *str = res->svg_dump_path();
1364 repr->setAttribute("d", str);
1365 g_free(str);
1367 // add the new repr to the parent
1368 parent->appendChild(repr);
1370 // move to the saved position
1371 repr->setPosition(pos > 0 ? pos : 0);
1373 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1375 // reapply the transform
1376 sp_item_write_transform(newitem, repr, transform);
1378 repr->setAttribute("id", id);
1380 selection->add(repr);
1382 Inkscape::GC::release(repr);
1383 }
1385 delete orig;
1386 delete res;
1387 }
1389 if (did) {
1390 sp_document_done(sp_desktop_document(desktop),
1391 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1392 (expand ? _("Outset path") : _("Inset path")));
1393 } else {
1394 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1395 return;
1396 }
1397 }
1400 static bool
1401 sp_selected_path_simplify_items(SPDesktop *desktop,
1402 Inkscape::Selection *selection, GSList *items,
1403 float threshold, bool justCoalesce,
1404 float angleLimit, bool breakableAngles,
1405 bool modifySelection);
1408 //return true if we changed something, else false
1409 bool
1410 sp_selected_path_simplify_item(SPDesktop *desktop,
1411 Inkscape::Selection *selection, SPItem *item,
1412 float threshold, bool justCoalesce,
1413 float angleLimit, bool breakableAngles,
1414 gdouble size, bool modifySelection)
1415 {
1416 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1417 return false;
1419 //If this is a group, do the children instead
1420 if (SP_IS_GROUP(item)) {
1421 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1423 return sp_selected_path_simplify_items(desktop, selection, items,
1424 threshold, justCoalesce,
1425 angleLimit, breakableAngles,
1426 false);
1427 }
1430 SPCurve *curve = NULL;
1432 if (SP_IS_SHAPE(item)) {
1433 curve = sp_shape_get_curve(SP_SHAPE(item));
1434 if (!curve)
1435 return false;
1436 }
1438 if (SP_IS_TEXT(item)) {
1439 curve = SP_TEXT(item)->getNormalizedBpath();
1440 if (!curve)
1441 return false;
1442 }
1444 // save the transform, to re-apply it after simplification
1445 NR::Matrix const transform(item->transform);
1447 /*
1448 reset the transform, effectively transforming the item by transform.inverse();
1449 this is necessary so that the item is transformed twice back and forth,
1450 allowing all compensations to cancel out regardless of the preferences
1451 */
1452 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1454 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1455 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1456 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1458 Path *orig = Path_for_item(item, false);
1459 if (orig == NULL) {
1460 g_free(style);
1461 sp_curve_unref(curve);
1462 return false;
1463 }
1465 sp_curve_unref(curve);
1466 // remember the position of the item
1467 gint pos = SP_OBJECT_REPR(item)->position();
1468 // remember parent
1469 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1470 // remember id
1471 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1473 //If a group was selected, to not change the selection list
1474 if (modifySelection)
1475 selection->remove(item);
1477 SP_OBJECT(item)->deleteObject(false);
1479 if ( justCoalesce ) {
1480 orig->Coalesce(threshold * size);
1481 } else {
1482 orig->ConvertEvenLines(threshold * size);
1483 orig->Simplify(threshold * size);
1484 }
1486 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1488 // restore style, mask and clip-path
1489 repr->setAttribute("style", style);
1490 g_free(style);
1492 if ( mask ) {
1493 repr->setAttribute("mask", mask);
1494 g_free(mask);
1495 }
1497 if ( clip_path ) {
1498 repr->setAttribute("clip-path", clip_path);
1499 g_free(clip_path);
1500 }
1502 // path
1503 gchar *str = orig->svg_dump_path();
1504 repr->setAttribute("d", str);
1505 g_free(str);
1507 // restore id
1508 repr->setAttribute("id", id);
1510 // add the new repr to the parent
1511 parent->appendChild(repr);
1513 // move to the saved position
1514 repr->setPosition(pos > 0 ? pos : 0);
1516 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1518 // reapply the transform
1519 sp_item_write_transform(newitem, repr, transform);
1521 //If we are not in a selected group
1522 if (modifySelection)
1523 selection->add(repr);
1525 Inkscape::GC::release(repr);
1527 // clean up
1528 if (orig) delete orig;
1530 return true;
1531 }
1534 bool
1535 sp_selected_path_simplify_items(SPDesktop *desktop,
1536 Inkscape::Selection *selection, GSList *items,
1537 float threshold, bool justCoalesce,
1538 float angleLimit, bool breakableAngles,
1539 bool modifySelection)
1540 {
1541 bool simplifyIndividualPaths =
1542 (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0);
1544 gchar *simplificationType;
1545 if (simplifyIndividualPaths) {
1546 simplificationType = "individual paths";
1547 } else {
1548 simplificationType = "as a group";
1549 }
1551 bool didSomething = false;
1553 NR::Rect selectionBbox = selection->bounds();
1554 gdouble selectionSize = L2(selectionBbox.dimensions());
1556 gdouble simplifySize = selectionSize;
1558 int pathsSimplified = 0;
1559 int totalPathCount = g_slist_length(items);
1561 desktop->disableInteraction();
1563 for (; items != NULL; items = items->next) {
1564 SPItem *item = (SPItem *) items->data;
1566 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1567 continue;
1569 if (simplifyIndividualPaths) {
1570 NR::Rect itemBbox = item->invokeBbox(sp_item_i2d_affine(item));
1571 simplifySize = L2(itemBbox.dimensions());
1572 }
1575 pathsSimplified++;
1577 if (pathsSimplified % 20 == 0) {
1578 gchar *message = g_strdup_printf(_("Simplifying %s - <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
1579 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, message);
1580 desktop->updateCanvasNow();
1581 }
1583 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1584 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1585 }
1587 desktop->enableInteraction();
1589 if (pathsSimplified > 20) {
1590 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("Done - <b>%d</b> paths simplified."), pathsSimplified));
1591 }
1593 return didSomething;
1594 }
1596 void
1597 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1598 float angleLimit, bool breakableAngles)
1599 {
1600 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1602 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1604 if (selection->isEmpty()) {
1605 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1606 _("Select <b>path(s)</b> to simplify."));
1607 return;
1608 }
1610 GSList *items = g_slist_copy((GSList *) selection->itemList());
1612 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1613 items, threshold,
1614 justCoalesce,
1615 angleLimit,
1616 breakableAngles, true);
1618 if (didSomething)
1619 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1620 _("Simplify"));
1621 else
1622 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1624 }
1627 // globals for keeping track of accelerated simplify
1628 static double previousTime = 0.0;
1629 static gdouble simplifyMultiply = 1.0;
1631 void
1632 sp_selected_path_simplify(void)
1633 {
1634 gdouble simplifyThreshold =
1635 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1636 bool simplifyJustCoalesce =
1637 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1639 //Get the current time
1640 GTimeVal currentTimeVal;
1641 g_get_current_time(¤tTimeVal);
1642 double currentTime = currentTimeVal.tv_sec * 1000000 +
1643 currentTimeVal.tv_usec;
1645 //Was the previous call to this function recent? (<0.5 sec)
1646 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1648 // add to the threshold 1/2 of its original value
1649 simplifyMultiply += 0.5;
1650 simplifyThreshold *= simplifyMultiply;
1652 } else {
1653 // reset to the default
1654 simplifyMultiply = 1;
1655 }
1657 //remember time for next call
1658 previousTime = currentTime;
1660 //g_print("%g\n", simplify_threshold);
1662 //Make the actual call
1663 sp_selected_path_simplify_selection(simplifyThreshold,
1664 simplifyJustCoalesce, 0.0, false);
1665 }
1669 // fonctions utilitaires
1671 bool
1672 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1673 {
1674 if (who == NULL || a == NULL)
1675 return false;
1676 if (who == a)
1677 return true;
1678 return Ancetre(sp_repr_parent(a), who);
1679 }
1681 Path *
1682 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1683 {
1684 SPCurve *curve;
1686 if (!item)
1687 return NULL;
1689 if (SP_IS_SHAPE(item))
1690 {
1691 curve = sp_shape_get_curve(SP_SHAPE(item));
1692 }
1693 else if (SP_IS_TEXT(item))
1694 {
1695 curve = SP_TEXT(item)->getNormalizedBpath();
1696 }
1697 else
1698 {
1699 curve = NULL;
1700 }
1702 if (!curve)
1703 return NULL;
1704 NArtBpath *bpath = SP_CURVE_BPATH(curve);
1705 if (bpath == NULL)
1706 return NULL;
1708 if ( doTransformation ) {
1709 if (transformFull)
1710 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1711 else
1712 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1713 sp_curve_unref(curve);
1714 curve=NULL;
1715 } else {
1716 bpath=SP_CURVE_BPATH(curve);
1717 }
1719 Path *dest = new Path;
1720 dest->SetBackData(false);
1721 {
1722 int i;
1723 bool closed = false;
1724 float lastX = 0.0;
1725 float lastY = 0.0;
1727 for (i = 0; bpath[i].code != NR_END; i++) {
1728 switch (bpath[i].code) {
1729 case NR_LINETO:
1730 lastX = bpath[i].x3;
1731 lastY = bpath[i].y3;
1732 {
1733 NR::Point tmp(lastX, lastY);
1734 dest->LineTo(tmp);
1735 }
1736 break;
1738 case NR_CURVETO:
1739 {
1740 NR::Point tmp, tms, tme;
1741 tmp[0]=bpath[i].x3;
1742 tmp[1]=bpath[i].y3;
1743 tms[0]=3 * (bpath[i].x1 - lastX);
1744 tms[1]=3 * (bpath[i].y1 - lastY);
1745 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1746 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1747 dest->CubicTo(tmp,tms,tme);
1748 }
1749 lastX = bpath[i].x3;
1750 lastY = bpath[i].y3;
1751 break;
1753 case NR_MOVETO_OPEN:
1754 case NR_MOVETO:
1755 if (closed)
1756 dest->Close();
1757 closed = (bpath[i].code == NR_MOVETO);
1758 lastX = bpath[i].x3;
1759 lastY = bpath[i].y3;
1760 {
1761 NR::Point tmp(lastX, lastY);
1762 dest->MoveTo(tmp);
1763 }
1764 break;
1765 default:
1766 break;
1767 }
1768 }
1769 if (closed)
1770 dest->Close();
1771 }
1773 if ( doTransformation ) {
1774 if ( bpath ) g_free(bpath);
1775 } else {
1776 sp_curve_unref(curve);
1777 }
1778 return dest;
1779 }
1781 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1782 {
1783 //get nearest position on path
1784 Path::cut_position pos = path->PointToCurvilignPosition(p);
1785 return pos;
1786 }
1788 NR::Point get_point_on_Path(Path *path, int piece, double t)
1789 {
1790 NR::Point p;
1791 path->PointAt(piece, t, p);
1792 return p;
1793 }
1796 /*
1797 Local Variables:
1798 mode:c++
1799 c-file-style:"stroustrup"
1800 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1801 indent-tabs-mode:nil
1802 fill-column:99
1803 End:
1804 */
1805 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :