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::Document *xml_doc = sp_document_repr_doc(desktop->doc());
510 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
511 repr->setAttribute("style", style);
512 if (mask)
513 repr->setAttribute("mask", mask);
514 if (clip_path)
515 repr->setAttribute("clip-path", clip_path);
517 repr->setAttribute("d", d);
518 g_free(d);
520 // for slice, remove fill
521 if (bop == bool_op_slice) {
522 SPCSSAttr *css;
524 css = sp_repr_css_attr_new();
525 sp_repr_css_set_property(css, "fill", "none");
527 sp_repr_css_change(repr, css, "style");
529 sp_repr_css_attr_unref(css);
530 }
532 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
533 // this means it's basically random which of the pieces inherits the original's id and clones
534 // a better algorithm might figure out e.g. the biggest piece
535 repr->setAttribute("id", id);
537 repr->setAttribute("transform", transform);
539 // add the new repr to the parent
540 parent->appendChild(repr);
542 // move to the saved position
543 repr->setPosition(pos > 0 ? pos : 0);
545 selection->add(repr);
546 Inkscape::GC::release(repr);
548 delete resPath[i];
549 }
550 if ( resPath ) free(resPath);
552 } else {
553 gchar *d = res->svg_dump_path();
555 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
556 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
557 repr->setAttribute("style", style);
559 if ( mask )
560 repr->setAttribute("mask", mask);
562 if ( clip_path )
563 repr->setAttribute("clip-path", clip_path);
565 repr->setAttribute("d", d);
566 g_free(d);
568 repr->setAttribute("transform", transform);
570 repr->setAttribute("id", id);
571 parent->appendChild(repr);
572 repr->setPosition(pos > 0 ? pos : 0);
574 selection->add(repr);
575 Inkscape::GC::release(repr);
576 }
578 sp_document_done(sp_desktop_document(desktop), verb, description);
580 delete res;
581 }
584 void
585 sp_selected_path_outline()
586 {
587 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
589 Inkscape::Selection *selection = sp_desktop_selection(desktop);
591 if (selection->isEmpty()) {
592 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
593 return;
594 }
596 bool did = false;
598 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
599 items != NULL;
600 items = items->next) {
602 SPItem *item = (SPItem *) items->data;
604 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
605 continue;
607 SPCurve *curve = NULL;
608 if (SP_IS_SHAPE(item)) {
609 curve = sp_shape_get_curve(SP_SHAPE(item));
610 if (curve == NULL)
611 continue;
612 }
613 if (SP_IS_TEXT(item)) {
614 curve = SP_TEXT(item)->getNormalizedBpath();
615 if (curve == NULL)
616 continue;
617 }
619 { // pas de stroke pas de chocolat
620 SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
621 gchar const *val = sp_repr_css_property(css, "stroke", NULL);
623 if (val == NULL || strcmp(val, "none") == 0) {
624 sp_curve_unref(curve);
625 continue;
626 }
627 }
629 // remember old stroke style, to be set on fill
630 SPCSSAttr *ncss;
631 {
632 SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
633 gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
634 gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
636 ncss = sp_repr_css_attr_new();
638 sp_repr_css_set_property(ncss, "stroke", "none");
639 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
640 sp_repr_css_set_property(ncss, "fill", val);
641 if ( opac ) {
642 sp_repr_css_set_property(ncss, "fill-opacity", opac);
643 } else {
644 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
645 }
646 sp_repr_css_unset_property(ncss, "marker-start");
647 sp_repr_css_unset_property(ncss, "marker-mid");
648 sp_repr_css_unset_property(ncss, "marker-end");
649 }
651 NR::Matrix const transform(item->transform);
652 float const scale = transform.expansion();
653 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
654 SPStyle *i_style = SP_OBJECT(item)->style;
655 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
656 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
658 float o_width, o_miter;
659 JoinType o_join;
660 ButtType o_butt;
662 {
663 int jointype, captype;
665 jointype = i_style->stroke_linejoin.computed;
666 captype = i_style->stroke_linecap.computed;
667 o_width = i_style->stroke_width.computed;
669 switch (jointype) {
670 case SP_STROKE_LINEJOIN_MITER:
671 o_join = join_pointy;
672 break;
673 case SP_STROKE_LINEJOIN_ROUND:
674 o_join = join_round;
675 break;
676 default:
677 o_join = join_straight;
678 break;
679 }
681 switch (captype) {
682 case SP_STROKE_LINECAP_SQUARE:
683 o_butt = butt_square;
684 break;
685 case SP_STROKE_LINECAP_ROUND:
686 o_butt = butt_round;
687 break;
688 default:
689 o_butt = butt_straight;
690 break;
691 }
693 if (o_width < 0.1)
694 o_width = 0.1;
695 o_miter = i_style->stroke_miterlimit.value * o_width;
696 }
698 Path *orig = Path_for_item(item, false);
699 if (orig == NULL) {
700 g_free(style);
701 sp_curve_unref(curve);
702 continue;
703 }
705 Path *res = new Path;
706 res->SetBackData(false);
708 if (i_style->stroke_dash.n_dash) {
709 // For dashed strokes, use Stroke method, because Outline can't do dashes
710 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
712 orig->ConvertWithBackData(0.1);
714 orig->DashPolylineFromStyle(i_style, scale, 0);
716 Shape* theShape = new Shape;
717 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
718 0.5 * o_miter);
719 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
721 Shape *theRes = new Shape;
723 theRes->ConvertToShape(theShape, fill_positive);
725 Path *originaux[1];
726 originaux[0] = res;
727 theRes->ConvertToForme(orig, 1, originaux);
729 res->Coalesce(5.0);
731 delete theShape;
732 delete theRes;
734 } else {
736 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
738 orig->Coalesce(0.5 * o_width);
740 Shape *theShape = new Shape;
741 Shape *theRes = new Shape;
743 res->ConvertWithBackData(1.0);
744 res->Fill(theShape, 0);
745 theRes->ConvertToShape(theShape, fill_positive);
747 Path *originaux[1];
748 originaux[0] = res;
749 theRes->ConvertToForme(orig, 1, originaux);
751 delete theShape;
752 delete theRes;
753 }
755 if (orig->descr_cmd.size() <= 1) {
756 // ca a merd\8e, ou bien le resultat est vide
757 delete res;
758 delete orig;
759 g_free(style);
760 continue;
761 }
763 did = true;
765 // remember the position of the item
766 gint pos = SP_OBJECT_REPR(item)->position();
767 // remember parent
768 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
769 // remember id
770 char const *id = SP_OBJECT_REPR(item)->attribute("id");
772 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
774 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
775 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
777 // restore old style
778 repr->setAttribute("style", style);
780 // set old stroke style on fill
781 sp_repr_css_change(repr, ncss, "style");
783 sp_repr_css_attr_unref(ncss);
785 gchar *str = orig->svg_dump_path();
786 repr->setAttribute("d", str);
787 g_free(str);
789 if (mask)
790 repr->setAttribute("mask", mask);
791 if (clip_path)
792 repr->setAttribute("clip-path", clip_path);
794 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
796 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
797 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
799 // add the group to the parent
800 parent->appendChild(g_repr);
801 // move to the saved position
802 g_repr->setPosition(pos > 0 ? pos : 0);
804 g_repr->appendChild(repr);
805 repr->setAttribute("id", id);
806 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
807 sp_item_write_transform(newitem, repr, transform);
809 SPShape *shape = SP_SHAPE(item);
811 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
812 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
813 if (sp_shape_marker_required (shape, m, bp)) {
815 SPMarker* marker = SP_MARKER (shape->marker[m]);
816 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
818 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
820 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
821 tr = NR::scale(i_style->stroke_width.computed) * tr;
822 }
824 // total marker transform
825 tr = marker_item->transform * marker->c2p * tr * transform;
827 if (SP_OBJECT_REPR(marker_item)) {
828 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
829 g_repr->appendChild(m_repr);
830 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
831 sp_item_write_transform(marker_item, m_repr, tr);
832 }
833 }
834 }
835 }
838 selection->add(g_repr);
840 Inkscape::GC::release(g_repr);
843 } else {
845 // add the new repr to the parent
846 parent->appendChild(repr);
848 // move to the saved position
849 repr->setPosition(pos > 0 ? pos : 0);
851 repr->setAttribute("id", id);
853 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
854 sp_item_write_transform(newitem, repr, transform);
856 selection->add(repr);
858 }
860 Inkscape::GC::release(repr);
862 sp_curve_unref(curve);
863 selection->remove(item);
864 SP_OBJECT(item)->deleteObject(false);
866 }
868 delete res;
869 delete orig;
870 g_free(style);
872 }
874 if (did) {
875 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
876 _("Convert stroke to path"));
877 } else {
878 // TRANSLATORS: "to outline" means "to convert stroke to path"
879 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
880 return;
881 }
882 }
885 void
886 sp_selected_path_offset()
887 {
888 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
890 sp_selected_path_do_offset(true, prefOffset);
891 }
892 void
893 sp_selected_path_inset()
894 {
895 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
897 sp_selected_path_do_offset(false, prefOffset);
898 }
900 void
901 sp_selected_path_offset_screen(double pixels)
902 {
903 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
904 }
906 void
907 sp_selected_path_inset_screen(double pixels)
908 {
909 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
910 }
913 void sp_selected_path_create_offset_object_zero()
914 {
915 sp_selected_path_create_offset_object(0, false);
916 }
918 void sp_selected_path_create_offset()
919 {
920 sp_selected_path_create_offset_object(1, false);
921 }
922 void sp_selected_path_create_inset()
923 {
924 sp_selected_path_create_offset_object(-1, false);
925 }
927 void sp_selected_path_create_updating_offset_object_zero()
928 {
929 sp_selected_path_create_offset_object(0, true);
930 }
932 void sp_selected_path_create_updating_offset()
933 {
934 sp_selected_path_create_offset_object(1, true);
935 }
936 void sp_selected_path_create_updating_inset()
937 {
938 sp_selected_path_create_offset_object(-1, true);
939 }
941 void
942 sp_selected_path_create_offset_object(int expand, bool updating)
943 {
944 Inkscape::Selection *selection;
945 Inkscape::XML::Node *repr;
946 SPItem *item;
947 SPCurve *curve;
948 gchar *style, *str;
949 SPDesktop *desktop;
950 float o_width, o_miter;
951 JoinType o_join;
952 ButtType o_butt;
954 curve = NULL;
956 desktop = SP_ACTIVE_DESKTOP;
958 selection = sp_desktop_selection(desktop);
960 item = selection->singleItem();
962 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
963 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
964 return;
965 }
966 if (SP_IS_SHAPE(item))
967 {
968 curve = sp_shape_get_curve(SP_SHAPE(item));
969 if (curve == NULL)
970 return;
971 }
972 if (SP_IS_TEXT(item))
973 {
974 curve = SP_TEXT(item)->getNormalizedBpath();
975 if (curve == NULL)
976 return;
977 }
979 NR::Matrix const transform(item->transform);
981 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
983 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
985 // remember the position of the item
986 gint pos = SP_OBJECT_REPR(item)->position();
987 // remember parent
988 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
990 {
991 SPStyle *i_style = SP_OBJECT(item)->style;
992 int jointype, captype;
994 jointype = i_style->stroke_linejoin.value;
995 captype = i_style->stroke_linecap.value;
996 o_width = i_style->stroke_width.computed;
997 if (jointype == SP_STROKE_LINEJOIN_MITER)
998 {
999 o_join = join_pointy;
1000 }
1001 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1002 {
1003 o_join = join_round;
1004 }
1005 else
1006 {
1007 o_join = join_straight;
1008 }
1009 if (captype == SP_STROKE_LINECAP_SQUARE)
1010 {
1011 o_butt = butt_square;
1012 }
1013 else if (captype == SP_STROKE_LINECAP_ROUND)
1014 {
1015 o_butt = butt_round;
1016 }
1017 else
1018 {
1019 o_butt = butt_straight;
1020 }
1022 {
1023 double prefOffset = 1.0;
1024 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
1025 o_width = prefOffset;
1026 }
1028 if (o_width < 0.01)
1029 o_width = 0.01;
1030 o_miter = i_style->stroke_miterlimit.value * o_width;
1031 }
1033 Path *orig = Path_for_item(item, true, false);
1034 if (orig == NULL)
1035 {
1036 g_free(style);
1037 sp_curve_unref(curve);
1038 return;
1039 }
1041 Path *res = new Path;
1042 res->SetBackData(false);
1044 {
1045 Shape *theShape = new Shape;
1046 Shape *theRes = new Shape;
1048 orig->ConvertWithBackData(1.0);
1049 orig->Fill(theShape, 0);
1051 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1052 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1053 if (val && strcmp(val, "nonzero") == 0)
1054 {
1055 theRes->ConvertToShape(theShape, fill_nonZero);
1056 }
1057 else if (val && strcmp(val, "evenodd") == 0)
1058 {
1059 theRes->ConvertToShape(theShape, fill_oddEven);
1060 }
1061 else
1062 {
1063 theRes->ConvertToShape(theShape, fill_nonZero);
1064 }
1066 Path *originaux[1];
1067 originaux[0] = orig;
1068 theRes->ConvertToForme(res, 1, originaux);
1070 delete theShape;
1071 delete theRes;
1072 }
1074 sp_curve_unref(curve);
1076 if (res->descr_cmd.size() <= 1)
1077 {
1078 // pas vraiment de points sur le resultat
1079 // donc il ne reste rien
1080 sp_document_done(sp_desktop_document(desktop),
1081 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1082 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1083 (updating ? _("Create linked offset")
1084 : _("Create dynamic offset")));
1085 selection->clear();
1087 delete res;
1088 delete orig;
1089 g_free(style);
1090 return;
1091 }
1093 {
1094 gchar tstr[80];
1096 tstr[79] = '\0';
1098 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1099 repr = xml_doc->createElement("svg:path");
1100 repr->setAttribute("sodipodi:type", "inkscape:offset");
1101 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1102 ? o_width
1103 : expand < 0
1104 ? -o_width
1105 : 0 ));
1107 str = res->svg_dump_path();
1108 repr->setAttribute("inkscape:original", str);
1109 g_free(str);
1111 if ( updating ) {
1112 char const *id = SP_OBJECT(item)->repr->attribute("id");
1113 char const *uri = g_strdup_printf("#%s", id);
1114 repr->setAttribute("xlink:href", uri);
1115 g_free((void *) uri);
1116 } else {
1117 repr->setAttribute("inkscape:href", NULL);
1118 }
1120 repr->setAttribute("style", style);
1122 // add the new repr to the parent
1123 parent->appendChild(repr);
1125 // move to the saved position
1126 repr->setPosition(pos > 0 ? pos : 0);
1128 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1130 if ( updating ) {
1131 // on conserve l'original
1132 // we reapply the transform to the original (offset will feel it)
1133 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1134 } else {
1135 // delete original, apply the transform to the offset
1136 SP_OBJECT(item)->deleteObject(false);
1137 sp_item_write_transform(nitem, repr, transform);
1138 }
1140 // The object just created from a temporary repr is only a seed.
1141 // We need to invoke its write which will update its real repr (in particular adding d=)
1142 SP_OBJECT(nitem)->updateRepr();
1144 Inkscape::GC::release(repr);
1146 selection->set(nitem);
1147 }
1149 sp_document_done(sp_desktop_document(desktop),
1150 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1151 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1152 (updating ? _("Create linked offset")
1153 : _("Create dynamic offset")));
1155 delete res;
1156 delete orig;
1158 g_free(style);
1159 }
1172 void
1173 sp_selected_path_do_offset(bool expand, double prefOffset)
1174 {
1175 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1177 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1179 if (selection->isEmpty()) {
1180 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1181 return;
1182 }
1184 bool did = false;
1186 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1187 items != NULL;
1188 items = items->next) {
1190 SPItem *item = (SPItem *) items->data;
1192 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1193 continue;
1195 SPCurve *curve = NULL;
1196 if (SP_IS_SHAPE(item)) {
1197 curve = sp_shape_get_curve(SP_SHAPE(item));
1198 if (curve == NULL)
1199 continue;
1200 }
1201 if (SP_IS_TEXT(item)) {
1202 curve = SP_TEXT(item)->getNormalizedBpath();
1203 if (curve == NULL)
1204 continue;
1205 }
1207 NR::Matrix const transform(item->transform);
1209 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1211 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1213 float o_width, o_miter;
1214 JoinType o_join;
1215 ButtType o_butt;
1217 {
1218 SPStyle *i_style = SP_OBJECT(item)->style;
1219 int jointype, captype;
1221 jointype = i_style->stroke_linejoin.value;
1222 captype = i_style->stroke_linecap.value;
1223 o_width = i_style->stroke_width.computed;
1225 switch (jointype) {
1226 case SP_STROKE_LINEJOIN_MITER:
1227 o_join = join_pointy;
1228 break;
1229 case SP_STROKE_LINEJOIN_ROUND:
1230 o_join = join_round;
1231 break;
1232 default:
1233 o_join = join_straight;
1234 break;
1235 }
1237 switch (captype) {
1238 case SP_STROKE_LINECAP_SQUARE:
1239 o_butt = butt_square;
1240 break;
1241 case SP_STROKE_LINECAP_ROUND:
1242 o_butt = butt_round;
1243 break;
1244 default:
1245 o_butt = butt_straight;
1246 break;
1247 }
1249 o_width = prefOffset;
1251 if (o_width < 0.1)
1252 o_width = 0.1;
1253 o_miter = i_style->stroke_miterlimit.value * o_width;
1254 }
1256 Path *orig = Path_for_item(item, false);
1257 if (orig == NULL) {
1258 g_free(style);
1259 sp_curve_unref(curve);
1260 continue;
1261 }
1263 Path *res = new Path;
1264 res->SetBackData(false);
1266 {
1267 Shape *theShape = new Shape;
1268 Shape *theRes = new Shape;
1270 orig->ConvertWithBackData(0.03);
1271 orig->Fill(theShape, 0);
1273 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1274 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1275 if (val && strcmp(val, "nonzero") == 0)
1276 {
1277 theRes->ConvertToShape(theShape, fill_nonZero);
1278 }
1279 else if (val && strcmp(val, "evenodd") == 0)
1280 {
1281 theRes->ConvertToShape(theShape, fill_oddEven);
1282 }
1283 else
1284 {
1285 theRes->ConvertToShape(theShape, fill_nonZero);
1286 }
1288 // et maintenant: offset
1289 // methode inexacte
1290 /* Path *originaux[1];
1291 originaux[0] = orig;
1292 theRes->ConvertToForme(res, 1, originaux);
1294 if (expand) {
1295 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1296 } else {
1297 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1298 }
1300 orig->ConvertWithBackData(1.0);
1301 orig->Fill(theShape, 0);
1302 theRes->ConvertToShape(theShape, fill_positive);
1303 originaux[0] = orig;
1304 theRes->ConvertToForme(res, 1, originaux);
1306 if (o_width >= 0.5) {
1307 // res->Coalesce(1.0);
1308 res->ConvertEvenLines(1.0);
1309 res->Simplify(1.0);
1310 } else {
1311 // res->Coalesce(o_width);
1312 res->ConvertEvenLines(1.0*o_width);
1313 res->Simplify(1.0 * o_width);
1314 } */
1315 // methode par makeoffset
1317 if (expand)
1318 {
1319 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1320 }
1321 else
1322 {
1323 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1324 }
1325 theRes->ConvertToShape(theShape, fill_positive);
1327 res->Reset();
1328 theRes->ConvertToForme(res);
1330 if (o_width >= 1.0)
1331 {
1332 res->ConvertEvenLines(1.0);
1333 res->Simplify(1.0);
1334 }
1335 else
1336 {
1337 res->ConvertEvenLines(1.0*o_width);
1338 res->Simplify(1.0 * o_width);
1339 }
1341 delete theShape;
1342 delete theRes;
1343 }
1345 did = true;
1347 sp_curve_unref(curve);
1348 // remember the position of the item
1349 gint pos = SP_OBJECT_REPR(item)->position();
1350 // remember parent
1351 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1352 // remember id
1353 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1355 selection->remove(item);
1356 SP_OBJECT(item)->deleteObject(false);
1358 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1360 gchar tstr[80];
1362 tstr[79] = '\0';
1364 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1365 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1367 repr->setAttribute("style", style);
1369 gchar *str = res->svg_dump_path();
1370 repr->setAttribute("d", str);
1371 g_free(str);
1373 // add the new repr to the parent
1374 parent->appendChild(repr);
1376 // move to the saved position
1377 repr->setPosition(pos > 0 ? pos : 0);
1379 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1381 // reapply the transform
1382 sp_item_write_transform(newitem, repr, transform);
1384 repr->setAttribute("id", id);
1386 selection->add(repr);
1388 Inkscape::GC::release(repr);
1389 }
1391 delete orig;
1392 delete res;
1393 }
1395 if (did) {
1396 sp_document_done(sp_desktop_document(desktop),
1397 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1398 (expand ? _("Outset path") : _("Inset path")));
1399 } else {
1400 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1401 return;
1402 }
1403 }
1406 static bool
1407 sp_selected_path_simplify_items(SPDesktop *desktop,
1408 Inkscape::Selection *selection, GSList *items,
1409 float threshold, bool justCoalesce,
1410 float angleLimit, bool breakableAngles,
1411 bool modifySelection);
1414 //return true if we changed something, else false
1415 bool
1416 sp_selected_path_simplify_item(SPDesktop *desktop,
1417 Inkscape::Selection *selection, SPItem *item,
1418 float threshold, bool justCoalesce,
1419 float angleLimit, bool breakableAngles,
1420 gdouble size, bool modifySelection)
1421 {
1422 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1423 return false;
1425 //If this is a group, do the children instead
1426 if (SP_IS_GROUP(item)) {
1427 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1429 return sp_selected_path_simplify_items(desktop, selection, items,
1430 threshold, justCoalesce,
1431 angleLimit, breakableAngles,
1432 false);
1433 }
1436 SPCurve *curve = NULL;
1438 if (SP_IS_SHAPE(item)) {
1439 curve = sp_shape_get_curve(SP_SHAPE(item));
1440 if (!curve)
1441 return false;
1442 }
1444 if (SP_IS_TEXT(item)) {
1445 curve = SP_TEXT(item)->getNormalizedBpath();
1446 if (!curve)
1447 return false;
1448 }
1450 // save the transform, to re-apply it after simplification
1451 NR::Matrix const transform(item->transform);
1453 /*
1454 reset the transform, effectively transforming the item by transform.inverse();
1455 this is necessary so that the item is transformed twice back and forth,
1456 allowing all compensations to cancel out regardless of the preferences
1457 */
1458 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1460 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1461 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1462 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1464 Path *orig = Path_for_item(item, false);
1465 if (orig == NULL) {
1466 g_free(style);
1467 sp_curve_unref(curve);
1468 return false;
1469 }
1471 sp_curve_unref(curve);
1472 // remember the position of the item
1473 gint pos = SP_OBJECT_REPR(item)->position();
1474 // remember parent
1475 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1476 // remember id
1477 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1479 //If a group was selected, to not change the selection list
1480 if (modifySelection)
1481 selection->remove(item);
1483 SP_OBJECT(item)->deleteObject(false);
1485 if ( justCoalesce ) {
1486 orig->Coalesce(threshold * size);
1487 } else {
1488 orig->ConvertEvenLines(threshold * size);
1489 orig->Simplify(threshold * size);
1490 }
1492 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1493 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1495 // restore style, mask and clip-path
1496 repr->setAttribute("style", style);
1497 g_free(style);
1499 if ( mask ) {
1500 repr->setAttribute("mask", mask);
1501 g_free(mask);
1502 }
1504 if ( clip_path ) {
1505 repr->setAttribute("clip-path", clip_path);
1506 g_free(clip_path);
1507 }
1509 // path
1510 gchar *str = orig->svg_dump_path();
1511 repr->setAttribute("d", str);
1512 g_free(str);
1514 // restore id
1515 repr->setAttribute("id", id);
1517 // add the new repr to the parent
1518 parent->appendChild(repr);
1520 // move to the saved position
1521 repr->setPosition(pos > 0 ? pos : 0);
1523 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1525 // reapply the transform
1526 sp_item_write_transform(newitem, repr, transform);
1528 //If we are not in a selected group
1529 if (modifySelection)
1530 selection->add(repr);
1532 Inkscape::GC::release(repr);
1534 // clean up
1535 if (orig) delete orig;
1537 return true;
1538 }
1541 bool
1542 sp_selected_path_simplify_items(SPDesktop *desktop,
1543 Inkscape::Selection *selection, GSList *items,
1544 float threshold, bool justCoalesce,
1545 float angleLimit, bool breakableAngles,
1546 bool modifySelection)
1547 {
1548 bool simplifyIndividualPaths =
1549 (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0);
1551 gchar *simplificationType;
1552 if (simplifyIndividualPaths) {
1553 simplificationType = "individual paths";
1554 } else {
1555 simplificationType = "as a group";
1556 }
1558 bool didSomething = false;
1560 NR::Rect selectionBbox = selection->bounds();
1561 gdouble selectionSize = L2(selectionBbox.dimensions());
1563 gdouble simplifySize = selectionSize;
1565 int pathsSimplified = 0;
1566 int totalPathCount = g_slist_length(items);
1568 desktop->disableInteraction();
1570 for (; items != NULL; items = items->next) {
1571 SPItem *item = (SPItem *) items->data;
1573 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1574 continue;
1576 if (simplifyIndividualPaths) {
1577 NR::Rect itemBbox = item->invokeBbox(sp_item_i2d_affine(item));
1578 simplifySize = L2(itemBbox.dimensions());
1579 }
1582 pathsSimplified++;
1584 if (pathsSimplified % 20 == 0) {
1585 gchar *message = g_strdup_printf(_("Simplifying %s - <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
1586 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, message);
1587 desktop->updateCanvasNow();
1588 }
1590 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1591 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1592 }
1594 desktop->enableInteraction();
1596 if (pathsSimplified > 20) {
1597 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("Done - <b>%d</b> paths simplified."), pathsSimplified));
1598 }
1600 return didSomething;
1601 }
1603 void
1604 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1605 float angleLimit, bool breakableAngles)
1606 {
1607 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1609 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1611 if (selection->isEmpty()) {
1612 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1613 _("Select <b>path(s)</b> to simplify."));
1614 return;
1615 }
1617 GSList *items = g_slist_copy((GSList *) selection->itemList());
1619 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1620 items, threshold,
1621 justCoalesce,
1622 angleLimit,
1623 breakableAngles, true);
1625 if (didSomething)
1626 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1627 _("Simplify"));
1628 else
1629 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1631 }
1634 // globals for keeping track of accelerated simplify
1635 static double previousTime = 0.0;
1636 static gdouble simplifyMultiply = 1.0;
1638 void
1639 sp_selected_path_simplify(void)
1640 {
1641 gdouble simplifyThreshold =
1642 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1643 bool simplifyJustCoalesce =
1644 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1646 //Get the current time
1647 GTimeVal currentTimeVal;
1648 g_get_current_time(¤tTimeVal);
1649 double currentTime = currentTimeVal.tv_sec * 1000000 +
1650 currentTimeVal.tv_usec;
1652 //Was the previous call to this function recent? (<0.5 sec)
1653 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1655 // add to the threshold 1/2 of its original value
1656 simplifyMultiply += 0.5;
1657 simplifyThreshold *= simplifyMultiply;
1659 } else {
1660 // reset to the default
1661 simplifyMultiply = 1;
1662 }
1664 //remember time for next call
1665 previousTime = currentTime;
1667 //g_print("%g\n", simplify_threshold);
1669 //Make the actual call
1670 sp_selected_path_simplify_selection(simplifyThreshold,
1671 simplifyJustCoalesce, 0.0, false);
1672 }
1676 // fonctions utilitaires
1678 bool
1679 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1680 {
1681 if (who == NULL || a == NULL)
1682 return false;
1683 if (who == a)
1684 return true;
1685 return Ancetre(sp_repr_parent(a), who);
1686 }
1688 Path *
1689 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1690 {
1691 SPCurve *curve;
1693 if (!item)
1694 return NULL;
1696 if (SP_IS_SHAPE(item))
1697 {
1698 curve = sp_shape_get_curve(SP_SHAPE(item));
1699 }
1700 else if (SP_IS_TEXT(item))
1701 {
1702 curve = SP_TEXT(item)->getNormalizedBpath();
1703 }
1704 else
1705 {
1706 curve = NULL;
1707 }
1709 if (!curve)
1710 return NULL;
1711 NArtBpath *bpath = SP_CURVE_BPATH(curve);
1712 if (bpath == NULL)
1713 return NULL;
1715 if ( doTransformation ) {
1716 if (transformFull)
1717 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1718 else
1719 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1720 sp_curve_unref(curve);
1721 curve=NULL;
1722 } else {
1723 bpath=SP_CURVE_BPATH(curve);
1724 }
1726 Path *dest = new Path;
1727 dest->SetBackData(false);
1728 {
1729 int i;
1730 bool closed = false;
1731 float lastX = 0.0;
1732 float lastY = 0.0;
1734 for (i = 0; bpath[i].code != NR_END; i++) {
1735 switch (bpath[i].code) {
1736 case NR_LINETO:
1737 lastX = bpath[i].x3;
1738 lastY = bpath[i].y3;
1739 {
1740 NR::Point tmp(lastX, lastY);
1741 dest->LineTo(tmp);
1742 }
1743 break;
1745 case NR_CURVETO:
1746 {
1747 NR::Point tmp, tms, tme;
1748 tmp[0]=bpath[i].x3;
1749 tmp[1]=bpath[i].y3;
1750 tms[0]=3 * (bpath[i].x1 - lastX);
1751 tms[1]=3 * (bpath[i].y1 - lastY);
1752 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1753 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1754 dest->CubicTo(tmp,tms,tme);
1755 }
1756 lastX = bpath[i].x3;
1757 lastY = bpath[i].y3;
1758 break;
1760 case NR_MOVETO_OPEN:
1761 case NR_MOVETO:
1762 if (closed)
1763 dest->Close();
1764 closed = (bpath[i].code == NR_MOVETO);
1765 lastX = bpath[i].x3;
1766 lastY = bpath[i].y3;
1767 {
1768 NR::Point tmp(lastX, lastY);
1769 dest->MoveTo(tmp);
1770 }
1771 break;
1772 default:
1773 break;
1774 }
1775 }
1776 if (closed)
1777 dest->Close();
1778 }
1780 if ( doTransformation ) {
1781 if ( bpath ) g_free(bpath);
1782 } else {
1783 sp_curve_unref(curve);
1784 }
1785 return dest;
1786 }
1788 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1789 {
1790 //get nearest position on path
1791 Path::cut_position pos = path->PointToCurvilignPosition(p);
1792 return pos;
1793 }
1795 NR::Point get_point_on_Path(Path *path, int piece, double t)
1796 {
1797 NR::Point p;
1798 path->PointAt(piece, t, p);
1799 return p;
1800 }
1803 /*
1804 Local Variables:
1805 mode:c++
1806 c-file-style:"stroustrup"
1807 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1808 indent-tabs-mode:nil
1809 fill-column:99
1810 End:
1811 */
1812 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :