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 "sp-image.h"
27 #include "marker.h"
28 #include "enums.h"
29 #include "sp-text.h"
30 #include "sp-item-group.h"
31 #include "style.h"
32 #include "inkscape.h"
33 #include "document.h"
34 #include "message-stack.h"
35 #include "selection.h"
36 #include "desktop-handles.h"
37 #include "desktop.h"
38 #include "display/canvas-bpath.h"
39 #include "display/curve.h"
40 #include <glibmm/i18n.h>
41 #include "prefs-utils.h"
43 #include "libnr/n-art-bpath.h"
44 #include "libnr/nr-path.h"
45 #include "xml/repr.h"
46 #include "xml/repr-sorting.h"
48 #include <libnr/nr-matrix-fns.h>
49 #include <libnr/nr-matrix-ops.h>
50 #include <libnr/nr-matrix-translate-ops.h>
51 #include <libnr/nr-scale-matrix-ops.h>
53 #include "livarot/Path.h"
54 #include "livarot/Shape.h"
56 #include "splivarot.h"
58 bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
60 void sp_selected_path_boolop(bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
61 void sp_selected_path_do_offset(bool expand, double prefOffset);
62 void sp_selected_path_create_offset_object(int expand, bool updating);
64 void
65 sp_selected_path_union()
66 {
67 sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
68 }
70 void
71 sp_selected_path_union_skip_undo()
72 {
73 sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union"));
74 }
76 void
77 sp_selected_path_intersect()
78 {
79 sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
80 }
82 void
83 sp_selected_path_diff()
84 {
85 sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
86 }
88 void
89 sp_selected_path_symdiff()
90 {
91 sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
92 }
93 void
94 sp_selected_path_cut()
95 {
96 sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
97 }
98 void
99 sp_selected_path_slice()
100 {
101 sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path"));
102 }
105 // boolean operations
106 // take the source paths from the file, do the operation, delete the originals and add the results
107 void
108 sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description)
109 {
110 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
112 Inkscape::Selection *selection = sp_desktop_selection(desktop);
114 GSList *il = (GSList *) selection->itemList();
116 // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
117 if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
118 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
119 return;
120 }
121 else if ( g_slist_length(il) < 1 ) {
122 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
123 return;
124 }
126 if (g_slist_length(il) > 2) {
127 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
128 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
129 return;
130 }
131 }
133 // reverseOrderForOp marks whether the order of the list is the top->down order
134 // it's only used when there are 2 objects, and for operations who need to know the
135 // topmost object (differences, cuts)
136 bool reverseOrderForOp = false;
138 // mettre les elements de la liste dans l'ordre pour ces operations
139 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
140 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
141 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
142 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
144 if (a == NULL || b == NULL) {
145 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."));
146 return;
147 }
149 if (Ancetre(a, b)) {
150 // a is the parent of b, already in the proper order
151 } else if (Ancetre(b, a)) {
152 // reverse order
153 reverseOrderForOp = true;
154 } else {
156 // objects are not in parent/child relationship;
157 // find their lowest common ancestor
158 Inkscape::XML::Node *dad = LCA(a, b);
159 if (dad == NULL) {
160 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."));
161 return;
162 }
164 // find the children of the LCA that lead from it to the a and b
165 Inkscape::XML::Node *as = AncetreFils(a, dad);
166 Inkscape::XML::Node *bs = AncetreFils(b, dad);
168 // find out which comes first
169 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
170 if (child == as) {
171 /* a first, so reverse. */
172 reverseOrderForOp = true;
173 break;
174 }
175 if (child == bs)
176 break;
177 }
178 }
179 }
181 il = g_slist_copy(il);
183 // first check if all the input objects have shapes
184 // otherwise bail out
185 for (GSList *l = il; l != NULL; l = l->next)
186 {
187 SPItem *item = SP_ITEM(l->data);
188 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
189 {
190 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
191 g_slist_free(il);
192 return;
193 }
194 }
196 // extract the livarot Paths from the source objects
197 // also get the winding rule specified in the style
198 int nbOriginaux = g_slist_length(il);
199 std::vector<Path *> originaux(nbOriginaux);
200 std::vector<FillRule> origWind(nbOriginaux);
201 int curOrig;
202 {
203 curOrig = 0;
204 for (GSList *l = il; l != NULL; l = l->next)
205 {
206 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
207 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
208 if (val && strcmp(val, "nonzero") == 0) {
209 origWind[curOrig]= fill_nonZero;
210 } else if (val && strcmp(val, "evenodd") == 0) {
211 origWind[curOrig]= fill_oddEven;
212 } else {
213 origWind[curOrig]= fill_nonZero;
214 }
216 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
217 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
218 {
219 for (int i = curOrig; i >= 0; i--) delete originaux[i];
220 g_slist_free(il);
221 return;
222 }
223 curOrig++;
224 }
225 }
226 // reverse if needed
227 // note that the selection list keeps its order
228 if ( reverseOrderForOp ) {
229 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
230 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
231 }
233 // and work
234 // some temporary instances, first
235 Shape *theShapeA = new Shape;
236 Shape *theShapeB = new Shape;
237 Shape *theShape = new Shape;
238 Path *res = new Path;
239 res->SetBackData(false);
240 Path::cut_position *toCut=NULL;
241 int nbToCut=0;
243 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
244 // true boolean op
245 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
246 originaux[0]->ConvertWithBackData(0.1);
248 originaux[0]->Fill(theShape, 0);
250 theShapeA->ConvertToShape(theShape, origWind[0]);
252 curOrig = 1;
253 for (GSList *l = il->next; l != NULL; l = l->next) {
254 originaux[curOrig]->ConvertWithBackData(0.1);
256 originaux[curOrig]->Fill(theShape, curOrig);
258 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
260 // les elements arrivent en ordre inverse dans la liste
261 theShape->Booleen(theShapeB, theShapeA, bop);
263 {
264 Shape *swap = theShape;
265 theShape = theShapeA;
266 theShapeA = swap;
267 }
268 curOrig++;
269 }
271 {
272 Shape *swap = theShape;
273 theShape = theShapeA;
274 theShapeA = swap;
275 }
277 } else if ( bop == bool_op_cut ) {
278 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
279 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
280 // it is just uncrossed, and cleaned from duplicate edges and points
281 // then it's fed to Booleen() which will uncross it against the other path
282 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
283 // thus making a polygon. the weight of the edges of the cut are all 0, but
284 // the Booleen need to invert the ones inside the source polygon (for the subsequent
285 // ConvertToForme)
287 // the cut path needs to have the highest pathID in the back data
288 // that's how the Booleen() function knows it's an edge of the cut
290 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
291 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
292 // left it at 1.0. Investigate replacing this by a combination of difference and
293 // intersection of the same two paths. -- bb
294 {
295 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
296 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
297 }
298 originaux[0]->ConvertWithBackData(1.0);
300 originaux[0]->Fill(theShape, 0);
302 theShapeA->ConvertToShape(theShape, origWind[0]);
304 originaux[1]->ConvertWithBackData(1.0);
306 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
308 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
310 // les elements arrivent en ordre inverse dans la liste
311 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
313 } else if ( bop == bool_op_slice ) {
314 // slice is not really a boolean operation
315 // you just put the 2 shapes in a single polygon, uncross it
316 // the points where the degree is > 2 are intersections
317 // just check it's an intersection on the path you want to cut, and keep it
318 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
319 // make new subpath at each one of these positions
320 // inversion pour l'op\8eration
321 {
322 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
323 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
324 }
325 originaux[0]->ConvertWithBackData(1.0);
327 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
329 originaux[1]->ConvertWithBackData(1.0);
331 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
333 theShape->ConvertToShape(theShapeA, fill_justDont);
335 if ( theShape->hasBackData() ) {
336 // should always be the case, but ya never know
337 {
338 for (int i = 0; i < theShape->numberOfPoints(); i++) {
339 if ( theShape->getPoint(i).totalDegree() > 2 ) {
340 // possibly an intersection
341 // we need to check that at least one edge from the source path is incident to it
342 // before we declare it's an intersection
343 int cb = theShape->getPoint(i).incidentEdge[FIRST];
344 int nbOrig=0;
345 int nbOther=0;
346 int piece=-1;
347 float t=0.0;
348 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
349 if ( theShape->ebData[cb].pathID == 0 ) {
350 // the source has an edge incident to the point, get its position on the path
351 piece=theShape->ebData[cb].pieceID;
352 if ( theShape->getEdge(cb).st == i ) {
353 t=theShape->ebData[cb].tSt;
354 } else {
355 t=theShape->ebData[cb].tEn;
356 }
357 nbOrig++;
358 }
359 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
360 cb=theShape->NextAt(i, cb);
361 }
362 if ( nbOrig > 0 && nbOther > 0 ) {
363 // point incident to both path and cut: an intersection
364 // note that you only keep one position on the source; you could have degenerate
365 // cases where the source crosses itself at this point, and you wouyld miss an intersection
366 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
367 toCut[nbToCut].piece=piece;
368 toCut[nbToCut].t=t;
369 nbToCut++;
370 }
371 }
372 }
373 }
374 {
375 // i think it's useless now
376 int i = theShape->numberOfEdges() - 1;
377 for (;i>=0;i--) {
378 if ( theShape->ebData[i].pathID == 1 ) {
379 theShape->SubEdge(i);
380 }
381 }
382 }
384 }
385 }
387 int* nesting=NULL;
388 int* conts=NULL;
389 int nbNest=0;
390 // pour compenser le swap juste avant
391 if ( bop == bool_op_slice ) {
392 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
393 // res->ConvertForcedToMoveTo();
394 res->Copy(originaux[0]);
395 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
396 free(toCut);
397 } else if ( bop == bool_op_cut ) {
398 // il faut appeler pour desallouer PointData (pas vital, mais bon)
399 // the Booleen() function did not deallocated the point_data array in theShape, because this
400 // function needs it.
401 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
402 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
403 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
404 } else {
405 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
406 }
408 delete theShape;
409 delete theShapeA;
410 delete theShapeB;
411 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
413 if (res->descr_cmd.size() <= 1)
414 {
415 // only one command, presumably a moveto: it isn't a path
416 for (GSList *l = il; l != NULL; l = l->next)
417 {
418 SP_OBJECT(l->data)->deleteObject();
419 }
420 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
421 description);
422 selection->clear();
424 delete res;
425 g_slist_free(il);
426 return;
427 }
429 // get the source path object
430 SPObject *source;
431 if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
432 if (reverseOrderForOp) {
433 source = SP_OBJECT(il->data);
434 } else {
435 source = SP_OBJECT(il->next->data);
436 }
437 } else {
438 // find out the bottom object
439 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
441 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
443 source = sp_desktop_document(desktop)->
444 getObjectByRepr((Inkscape::XML::Node *)sorted->data);
446 g_slist_free(sorted);
447 }
449 // adjust style properties that depend on a possible transform in the source object in order
450 // to get a correct style attribute for the new path
451 SPItem* item_source = SP_ITEM(source);
452 NR::Matrix i2root = sp_item_i2root_affine(item_source);
453 sp_item_adjust_stroke(item_source, i2root.expansion());
454 sp_item_adjust_pattern(item_source, i2root);
455 sp_item_adjust_gradient(item_source, i2root);
457 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
459 // remember important aspects of the source path, to be restored
460 gint pos = repr_source->position();
461 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
462 gchar const *id = repr_source->attribute("id");
463 gchar const *style = repr_source->attribute("style");
464 gchar const *mask = repr_source->attribute("mask");
465 gchar const *clip_path = repr_source->attribute("clip-path");
467 // remove source paths
468 selection->clear();
469 for (GSList *l = il; l != NULL; l = l->next) {
470 // if this is the bottommost object,
471 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
472 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
473 SP_OBJECT(l->data)->deleteObject(false);
474 } else {
475 // delete the object for real, so that its clones can take appropriate action
476 SP_OBJECT(l->data)->deleteObject();
477 }
478 }
479 g_slist_free(il);
481 // premultiply by the inverse of parent's repr
482 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
483 NR::Matrix local = sp_item_i2doc_affine(parent_item);
484 gchar *transform = sp_svg_transform_write(local);
486 // now that we have the result, add it on the canvas
487 if ( bop == bool_op_cut || bop == bool_op_slice ) {
488 int nbRP=0;
489 Path** resPath;
490 if ( bop == bool_op_slice ) {
491 // there are moveto's at each intersection, but it's still one unique path
492 // so break it down and add each subpath independently
493 // we could call break_apart to do this, but while we have the description...
494 resPath=res->SubPaths(nbRP, false);
495 } else {
496 // cut operation is a bit wicked: you need to keep holes
497 // that's why you needed the nesting
498 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
499 // to get the path for each part of the polygon. that's why you need the nesting info:
500 // to know in wich subpath to add a subpath
501 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
503 // cleaning
504 if ( conts ) free(conts);
505 if ( nesting ) free(nesting);
506 }
508 // add all the pieces resulting from cut or slice
509 for (int i=0;i<nbRP;i++) {
510 gchar *d = resPath[i]->svg_dump_path();
512 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
513 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
514 repr->setAttribute("style", style);
515 if (mask)
516 repr->setAttribute("mask", mask);
517 if (clip_path)
518 repr->setAttribute("clip-path", clip_path);
520 repr->setAttribute("d", d);
521 g_free(d);
523 // for slice, remove fill
524 if (bop == bool_op_slice) {
525 SPCSSAttr *css;
527 css = sp_repr_css_attr_new();
528 sp_repr_css_set_property(css, "fill", "none");
530 sp_repr_css_change(repr, css, "style");
532 sp_repr_css_attr_unref(css);
533 }
535 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
536 // this means it's basically random which of the pieces inherits the original's id and clones
537 // a better algorithm might figure out e.g. the biggest piece
538 repr->setAttribute("id", id);
540 repr->setAttribute("transform", transform);
542 // add the new repr to the parent
543 parent->appendChild(repr);
545 // move to the saved position
546 repr->setPosition(pos > 0 ? pos : 0);
548 selection->add(repr);
549 Inkscape::GC::release(repr);
551 delete resPath[i];
552 }
553 if ( resPath ) free(resPath);
555 } else {
556 gchar *d = res->svg_dump_path();
558 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
559 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
560 repr->setAttribute("style", style);
562 if ( mask )
563 repr->setAttribute("mask", mask);
565 if ( clip_path )
566 repr->setAttribute("clip-path", clip_path);
568 repr->setAttribute("d", d);
569 g_free(d);
571 repr->setAttribute("transform", transform);
573 repr->setAttribute("id", id);
574 parent->appendChild(repr);
575 repr->setPosition(pos > 0 ? pos : 0);
577 selection->add(repr);
578 Inkscape::GC::release(repr);
579 }
581 g_free(transform);
583 if (verb != SP_VERB_NONE) {
584 sp_document_done(sp_desktop_document(desktop), verb, description);
585 }
587 delete res;
588 }
591 void
592 sp_selected_path_outline()
593 {
594 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
596 Inkscape::Selection *selection = sp_desktop_selection(desktop);
598 if (selection->isEmpty()) {
599 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
600 return;
601 }
603 bool did = false;
605 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
606 items != NULL;
607 items = items->next) {
609 SPItem *item = (SPItem *) items->data;
611 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
612 continue;
614 SPCurve *curve = NULL;
615 if (SP_IS_SHAPE(item)) {
616 curve = sp_shape_get_curve(SP_SHAPE(item));
617 if (curve == NULL)
618 continue;
619 }
620 if (SP_IS_TEXT(item)) {
621 curve = SP_TEXT(item)->getNormalizedBpath();
622 if (curve == NULL)
623 continue;
624 }
626 { // pas de stroke pas de chocolat
627 SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
628 gchar const *val = sp_repr_css_property(css, "stroke", NULL);
630 if (val == NULL || strcmp(val, "none") == 0) {
631 sp_curve_unref(curve);
632 continue;
633 }
634 }
636 // remember old stroke style, to be set on fill
637 SPCSSAttr *ncss;
638 {
639 SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
640 gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
641 gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
643 ncss = sp_repr_css_attr_new();
645 sp_repr_css_set_property(ncss, "stroke", "none");
646 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
647 sp_repr_css_set_property(ncss, "fill", val);
648 if ( opac ) {
649 sp_repr_css_set_property(ncss, "fill-opacity", opac);
650 } else {
651 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
652 }
653 sp_repr_css_unset_property(ncss, "marker-start");
654 sp_repr_css_unset_property(ncss, "marker-mid");
655 sp_repr_css_unset_property(ncss, "marker-end");
656 }
658 NR::Matrix const transform(item->transform);
659 float const scale = transform.expansion();
660 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
661 SPStyle *i_style = SP_OBJECT(item)->style;
662 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
663 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
665 float o_width, o_miter;
666 JoinType o_join;
667 ButtType o_butt;
669 {
670 int jointype, captype;
672 jointype = i_style->stroke_linejoin.computed;
673 captype = i_style->stroke_linecap.computed;
674 o_width = i_style->stroke_width.computed;
676 switch (jointype) {
677 case SP_STROKE_LINEJOIN_MITER:
678 o_join = join_pointy;
679 break;
680 case SP_STROKE_LINEJOIN_ROUND:
681 o_join = join_round;
682 break;
683 default:
684 o_join = join_straight;
685 break;
686 }
688 switch (captype) {
689 case SP_STROKE_LINECAP_SQUARE:
690 o_butt = butt_square;
691 break;
692 case SP_STROKE_LINECAP_ROUND:
693 o_butt = butt_round;
694 break;
695 default:
696 o_butt = butt_straight;
697 break;
698 }
700 if (o_width < 0.1)
701 o_width = 0.1;
702 o_miter = i_style->stroke_miterlimit.value * o_width;
703 }
705 Path *orig = Path_for_item(item, false);
706 if (orig == NULL) {
707 g_free(style);
708 sp_curve_unref(curve);
709 continue;
710 }
712 Path *res = new Path;
713 res->SetBackData(false);
715 if (i_style->stroke_dash.n_dash) {
716 // For dashed strokes, use Stroke method, because Outline can't do dashes
717 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
719 orig->ConvertWithBackData(0.1);
721 orig->DashPolylineFromStyle(i_style, scale, 0);
723 Shape* theShape = new Shape;
724 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
725 0.5 * o_miter);
726 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
728 Shape *theRes = new Shape;
730 theRes->ConvertToShape(theShape, fill_positive);
732 Path *originaux[1];
733 originaux[0] = res;
734 theRes->ConvertToForme(orig, 1, originaux);
736 res->Coalesce(5.0);
738 delete theShape;
739 delete theRes;
741 } else {
743 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
745 orig->Coalesce(0.5 * o_width);
747 Shape *theShape = new Shape;
748 Shape *theRes = new Shape;
750 res->ConvertWithBackData(1.0);
751 res->Fill(theShape, 0);
752 theRes->ConvertToShape(theShape, fill_positive);
754 Path *originaux[1];
755 originaux[0] = res;
756 theRes->ConvertToForme(orig, 1, originaux);
758 delete theShape;
759 delete theRes;
760 }
762 if (orig->descr_cmd.size() <= 1) {
763 // ca a merd\8e, ou bien le resultat est vide
764 delete res;
765 delete orig;
766 g_free(style);
767 continue;
768 }
770 did = true;
772 // remember the position of the item
773 gint pos = SP_OBJECT_REPR(item)->position();
774 // remember parent
775 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
776 // remember id
777 char const *id = SP_OBJECT_REPR(item)->attribute("id");
779 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
781 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
782 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
784 // restore old style
785 repr->setAttribute("style", style);
787 // set old stroke style on fill
788 sp_repr_css_change(repr, ncss, "style");
790 sp_repr_css_attr_unref(ncss);
792 gchar *str = orig->svg_dump_path();
793 repr->setAttribute("d", str);
794 g_free(str);
796 if (mask)
797 repr->setAttribute("mask", mask);
798 if (clip_path)
799 repr->setAttribute("clip-path", clip_path);
801 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
803 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
804 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
806 // add the group to the parent
807 parent->appendChild(g_repr);
808 // move to the saved position
809 g_repr->setPosition(pos > 0 ? pos : 0);
811 g_repr->appendChild(repr);
812 repr->setAttribute("id", id);
813 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
814 sp_item_write_transform(newitem, repr, transform);
816 SPShape *shape = SP_SHAPE(item);
818 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
819 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
820 if (sp_shape_marker_required (shape, m, bp)) {
822 SPMarker* marker = SP_MARKER (shape->marker[m]);
823 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
825 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
827 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
828 tr = NR::scale(i_style->stroke_width.computed) * tr;
829 }
831 // total marker transform
832 tr = marker_item->transform * marker->c2p * tr * transform;
834 if (SP_OBJECT_REPR(marker_item)) {
835 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
836 g_repr->appendChild(m_repr);
837 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
838 sp_item_write_transform(marker_item, m_repr, tr);
839 }
840 }
841 }
842 }
845 selection->add(g_repr);
847 Inkscape::GC::release(g_repr);
850 } else {
852 // add the new repr to the parent
853 parent->appendChild(repr);
855 // move to the saved position
856 repr->setPosition(pos > 0 ? pos : 0);
858 repr->setAttribute("id", id);
860 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
861 sp_item_write_transform(newitem, repr, transform);
863 selection->add(repr);
865 }
867 Inkscape::GC::release(repr);
869 sp_curve_unref(curve);
870 selection->remove(item);
871 SP_OBJECT(item)->deleteObject(false);
873 }
875 delete res;
876 delete orig;
877 g_free(style);
879 }
881 if (did) {
882 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
883 _("Convert stroke to path"));
884 } else {
885 // TRANSLATORS: "to outline" means "to convert stroke to path"
886 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
887 return;
888 }
889 }
892 void
893 sp_selected_path_offset()
894 {
895 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
897 sp_selected_path_do_offset(true, prefOffset);
898 }
899 void
900 sp_selected_path_inset()
901 {
902 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
904 sp_selected_path_do_offset(false, prefOffset);
905 }
907 void
908 sp_selected_path_offset_screen(double pixels)
909 {
910 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
911 }
913 void
914 sp_selected_path_inset_screen(double pixels)
915 {
916 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
917 }
920 void sp_selected_path_create_offset_object_zero()
921 {
922 sp_selected_path_create_offset_object(0, false);
923 }
925 void sp_selected_path_create_offset()
926 {
927 sp_selected_path_create_offset_object(1, false);
928 }
929 void sp_selected_path_create_inset()
930 {
931 sp_selected_path_create_offset_object(-1, false);
932 }
934 void sp_selected_path_create_updating_offset_object_zero()
935 {
936 sp_selected_path_create_offset_object(0, true);
937 }
939 void sp_selected_path_create_updating_offset()
940 {
941 sp_selected_path_create_offset_object(1, true);
942 }
943 void sp_selected_path_create_updating_inset()
944 {
945 sp_selected_path_create_offset_object(-1, true);
946 }
948 void
949 sp_selected_path_create_offset_object(int expand, bool updating)
950 {
951 Inkscape::Selection *selection;
952 Inkscape::XML::Node *repr;
953 SPItem *item;
954 SPCurve *curve;
955 gchar *style, *str;
956 SPDesktop *desktop;
957 float o_width, o_miter;
958 JoinType o_join;
959 ButtType o_butt;
961 curve = NULL;
963 desktop = SP_ACTIVE_DESKTOP;
965 selection = sp_desktop_selection(desktop);
967 item = selection->singleItem();
969 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
970 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
971 return;
972 }
973 if (SP_IS_SHAPE(item))
974 {
975 curve = sp_shape_get_curve(SP_SHAPE(item));
976 if (curve == NULL)
977 return;
978 }
979 if (SP_IS_TEXT(item))
980 {
981 curve = SP_TEXT(item)->getNormalizedBpath();
982 if (curve == NULL)
983 return;
984 }
986 NR::Matrix const transform(item->transform);
988 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
990 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
992 // remember the position of the item
993 gint pos = SP_OBJECT_REPR(item)->position();
994 // remember parent
995 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
997 {
998 SPStyle *i_style = SP_OBJECT(item)->style;
999 int jointype, captype;
1001 jointype = i_style->stroke_linejoin.value;
1002 captype = i_style->stroke_linecap.value;
1003 o_width = i_style->stroke_width.computed;
1004 if (jointype == SP_STROKE_LINEJOIN_MITER)
1005 {
1006 o_join = join_pointy;
1007 }
1008 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1009 {
1010 o_join = join_round;
1011 }
1012 else
1013 {
1014 o_join = join_straight;
1015 }
1016 if (captype == SP_STROKE_LINECAP_SQUARE)
1017 {
1018 o_butt = butt_square;
1019 }
1020 else if (captype == SP_STROKE_LINECAP_ROUND)
1021 {
1022 o_butt = butt_round;
1023 }
1024 else
1025 {
1026 o_butt = butt_straight;
1027 }
1029 {
1030 double prefOffset = 1.0;
1031 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
1032 o_width = prefOffset;
1033 }
1035 if (o_width < 0.01)
1036 o_width = 0.01;
1037 o_miter = i_style->stroke_miterlimit.value * o_width;
1038 }
1040 Path *orig = Path_for_item(item, true, false);
1041 if (orig == NULL)
1042 {
1043 g_free(style);
1044 sp_curve_unref(curve);
1045 return;
1046 }
1048 Path *res = new Path;
1049 res->SetBackData(false);
1051 {
1052 Shape *theShape = new Shape;
1053 Shape *theRes = new Shape;
1055 orig->ConvertWithBackData(1.0);
1056 orig->Fill(theShape, 0);
1058 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1059 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1060 if (val && strcmp(val, "nonzero") == 0)
1061 {
1062 theRes->ConvertToShape(theShape, fill_nonZero);
1063 }
1064 else if (val && strcmp(val, "evenodd") == 0)
1065 {
1066 theRes->ConvertToShape(theShape, fill_oddEven);
1067 }
1068 else
1069 {
1070 theRes->ConvertToShape(theShape, fill_nonZero);
1071 }
1073 Path *originaux[1];
1074 originaux[0] = orig;
1075 theRes->ConvertToForme(res, 1, originaux);
1077 delete theShape;
1078 delete theRes;
1079 }
1081 sp_curve_unref(curve);
1083 if (res->descr_cmd.size() <= 1)
1084 {
1085 // pas vraiment de points sur le resultat
1086 // donc il ne reste rien
1087 sp_document_done(sp_desktop_document(desktop),
1088 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1089 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1090 (updating ? _("Create linked offset")
1091 : _("Create dynamic offset")));
1092 selection->clear();
1094 delete res;
1095 delete orig;
1096 g_free(style);
1097 return;
1098 }
1100 {
1101 gchar tstr[80];
1103 tstr[79] = '\0';
1105 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1106 repr = xml_doc->createElement("svg:path");
1107 repr->setAttribute("sodipodi:type", "inkscape:offset");
1108 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1109 ? o_width
1110 : expand < 0
1111 ? -o_width
1112 : 0 ));
1114 str = res->svg_dump_path();
1115 repr->setAttribute("inkscape:original", str);
1116 g_free(str);
1118 if ( updating ) {
1119 char const *id = SP_OBJECT(item)->repr->attribute("id");
1120 char const *uri = g_strdup_printf("#%s", id);
1121 repr->setAttribute("xlink:href", uri);
1122 g_free((void *) uri);
1123 } else {
1124 repr->setAttribute("inkscape:href", NULL);
1125 }
1127 repr->setAttribute("style", style);
1129 // add the new repr to the parent
1130 parent->appendChild(repr);
1132 // move to the saved position
1133 repr->setPosition(pos > 0 ? pos : 0);
1135 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1137 if ( updating ) {
1138 // on conserve l'original
1139 // we reapply the transform to the original (offset will feel it)
1140 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1141 } else {
1142 // delete original, apply the transform to the offset
1143 SP_OBJECT(item)->deleteObject(false);
1144 sp_item_write_transform(nitem, repr, transform);
1145 }
1147 // The object just created from a temporary repr is only a seed.
1148 // We need to invoke its write which will update its real repr (in particular adding d=)
1149 SP_OBJECT(nitem)->updateRepr();
1151 Inkscape::GC::release(repr);
1153 selection->set(nitem);
1154 }
1156 sp_document_done(sp_desktop_document(desktop),
1157 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1158 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1159 (updating ? _("Create linked offset")
1160 : _("Create dynamic offset")));
1162 delete res;
1163 delete orig;
1165 g_free(style);
1166 }
1179 void
1180 sp_selected_path_do_offset(bool expand, double prefOffset)
1181 {
1182 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1184 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1186 if (selection->isEmpty()) {
1187 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1188 return;
1189 }
1191 bool did = false;
1193 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1194 items != NULL;
1195 items = items->next) {
1197 SPItem *item = (SPItem *) items->data;
1199 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1200 continue;
1202 SPCurve *curve = NULL;
1203 if (SP_IS_SHAPE(item)) {
1204 curve = sp_shape_get_curve(SP_SHAPE(item));
1205 if (curve == NULL)
1206 continue;
1207 }
1208 if (SP_IS_TEXT(item)) {
1209 curve = SP_TEXT(item)->getNormalizedBpath();
1210 if (curve == NULL)
1211 continue;
1212 }
1214 NR::Matrix const transform(item->transform);
1216 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1218 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1220 float o_width, o_miter;
1221 JoinType o_join;
1222 ButtType o_butt;
1224 {
1225 SPStyle *i_style = SP_OBJECT(item)->style;
1226 int jointype, captype;
1228 jointype = i_style->stroke_linejoin.value;
1229 captype = i_style->stroke_linecap.value;
1230 o_width = i_style->stroke_width.computed;
1232 switch (jointype) {
1233 case SP_STROKE_LINEJOIN_MITER:
1234 o_join = join_pointy;
1235 break;
1236 case SP_STROKE_LINEJOIN_ROUND:
1237 o_join = join_round;
1238 break;
1239 default:
1240 o_join = join_straight;
1241 break;
1242 }
1244 switch (captype) {
1245 case SP_STROKE_LINECAP_SQUARE:
1246 o_butt = butt_square;
1247 break;
1248 case SP_STROKE_LINECAP_ROUND:
1249 o_butt = butt_round;
1250 break;
1251 default:
1252 o_butt = butt_straight;
1253 break;
1254 }
1256 o_width = prefOffset;
1258 if (o_width < 0.1)
1259 o_width = 0.1;
1260 o_miter = i_style->stroke_miterlimit.value * o_width;
1261 }
1263 Path *orig = Path_for_item(item, false);
1264 if (orig == NULL) {
1265 g_free(style);
1266 sp_curve_unref(curve);
1267 continue;
1268 }
1270 Path *res = new Path;
1271 res->SetBackData(false);
1273 {
1274 Shape *theShape = new Shape;
1275 Shape *theRes = new Shape;
1277 orig->ConvertWithBackData(0.03);
1278 orig->Fill(theShape, 0);
1280 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1281 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1282 if (val && strcmp(val, "nonzero") == 0)
1283 {
1284 theRes->ConvertToShape(theShape, fill_nonZero);
1285 }
1286 else if (val && strcmp(val, "evenodd") == 0)
1287 {
1288 theRes->ConvertToShape(theShape, fill_oddEven);
1289 }
1290 else
1291 {
1292 theRes->ConvertToShape(theShape, fill_nonZero);
1293 }
1295 // et maintenant: offset
1296 // methode inexacte
1297 /* Path *originaux[1];
1298 originaux[0] = orig;
1299 theRes->ConvertToForme(res, 1, originaux);
1301 if (expand) {
1302 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1303 } else {
1304 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1305 }
1307 orig->ConvertWithBackData(1.0);
1308 orig->Fill(theShape, 0);
1309 theRes->ConvertToShape(theShape, fill_positive);
1310 originaux[0] = orig;
1311 theRes->ConvertToForme(res, 1, originaux);
1313 if (o_width >= 0.5) {
1314 // res->Coalesce(1.0);
1315 res->ConvertEvenLines(1.0);
1316 res->Simplify(1.0);
1317 } else {
1318 // res->Coalesce(o_width);
1319 res->ConvertEvenLines(1.0*o_width);
1320 res->Simplify(1.0 * o_width);
1321 } */
1322 // methode par makeoffset
1324 if (expand)
1325 {
1326 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1327 }
1328 else
1329 {
1330 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1331 }
1332 theRes->ConvertToShape(theShape, fill_positive);
1334 res->Reset();
1335 theRes->ConvertToForme(res);
1337 if (o_width >= 1.0)
1338 {
1339 res->ConvertEvenLines(1.0);
1340 res->Simplify(1.0);
1341 }
1342 else
1343 {
1344 res->ConvertEvenLines(1.0*o_width);
1345 res->Simplify(1.0 * o_width);
1346 }
1348 delete theShape;
1349 delete theRes;
1350 }
1352 did = true;
1354 sp_curve_unref(curve);
1355 // remember the position of the item
1356 gint pos = SP_OBJECT_REPR(item)->position();
1357 // remember parent
1358 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1359 // remember id
1360 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1362 selection->remove(item);
1363 SP_OBJECT(item)->deleteObject(false);
1365 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1367 gchar tstr[80];
1369 tstr[79] = '\0';
1371 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1372 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1374 repr->setAttribute("style", style);
1376 gchar *str = res->svg_dump_path();
1377 repr->setAttribute("d", str);
1378 g_free(str);
1380 // add the new repr to the parent
1381 parent->appendChild(repr);
1383 // move to the saved position
1384 repr->setPosition(pos > 0 ? pos : 0);
1386 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1388 // reapply the transform
1389 sp_item_write_transform(newitem, repr, transform);
1391 repr->setAttribute("id", id);
1393 selection->add(repr);
1395 Inkscape::GC::release(repr);
1396 }
1398 delete orig;
1399 delete res;
1400 }
1402 if (did) {
1403 sp_document_done(sp_desktop_document(desktop),
1404 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1405 (expand ? _("Outset path") : _("Inset path")));
1406 } else {
1407 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1408 return;
1409 }
1410 }
1413 static bool
1414 sp_selected_path_simplify_items(SPDesktop *desktop,
1415 Inkscape::Selection *selection, GSList *items,
1416 float threshold, bool justCoalesce,
1417 float angleLimit, bool breakableAngles,
1418 bool modifySelection);
1421 //return true if we changed something, else false
1422 bool
1423 sp_selected_path_simplify_item(SPDesktop *desktop,
1424 Inkscape::Selection *selection, SPItem *item,
1425 float threshold, bool justCoalesce,
1426 float angleLimit, bool breakableAngles,
1427 gdouble size, bool modifySelection)
1428 {
1429 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1430 return false;
1432 //If this is a group, do the children instead
1433 if (SP_IS_GROUP(item)) {
1434 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1436 return sp_selected_path_simplify_items(desktop, selection, items,
1437 threshold, justCoalesce,
1438 angleLimit, breakableAngles,
1439 false);
1440 }
1443 SPCurve *curve = NULL;
1445 if (SP_IS_SHAPE(item)) {
1446 curve = sp_shape_get_curve(SP_SHAPE(item));
1447 if (!curve)
1448 return false;
1449 }
1451 if (SP_IS_TEXT(item)) {
1452 curve = SP_TEXT(item)->getNormalizedBpath();
1453 if (!curve)
1454 return false;
1455 }
1457 // save the transform, to re-apply it after simplification
1458 NR::Matrix const transform(item->transform);
1460 /*
1461 reset the transform, effectively transforming the item by transform.inverse();
1462 this is necessary so that the item is transformed twice back and forth,
1463 allowing all compensations to cancel out regardless of the preferences
1464 */
1465 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1467 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1468 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1469 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1471 Path *orig = Path_for_item(item, false);
1472 if (orig == NULL) {
1473 g_free(style);
1474 sp_curve_unref(curve);
1475 return false;
1476 }
1478 sp_curve_unref(curve);
1479 // remember the position of the item
1480 gint pos = SP_OBJECT_REPR(item)->position();
1481 // remember parent
1482 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1483 // remember id
1484 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1486 //If a group was selected, to not change the selection list
1487 if (modifySelection)
1488 selection->remove(item);
1490 SP_OBJECT(item)->deleteObject(false);
1492 if ( justCoalesce ) {
1493 orig->Coalesce(threshold * size);
1494 } else {
1495 orig->ConvertEvenLines(threshold * size);
1496 orig->Simplify(threshold * size);
1497 }
1499 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1500 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1502 // restore style, mask and clip-path
1503 repr->setAttribute("style", style);
1504 g_free(style);
1506 if ( mask ) {
1507 repr->setAttribute("mask", mask);
1508 g_free(mask);
1509 }
1511 if ( clip_path ) {
1512 repr->setAttribute("clip-path", clip_path);
1513 g_free(clip_path);
1514 }
1516 // path
1517 gchar *str = orig->svg_dump_path();
1518 repr->setAttribute("d", str);
1519 g_free(str);
1521 // restore id
1522 repr->setAttribute("id", id);
1524 // add the new repr to the parent
1525 parent->appendChild(repr);
1527 // move to the saved position
1528 repr->setPosition(pos > 0 ? pos : 0);
1530 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1532 // reapply the transform
1533 sp_item_write_transform(newitem, repr, transform);
1535 //If we are not in a selected group
1536 if (modifySelection)
1537 selection->add(repr);
1539 Inkscape::GC::release(repr);
1541 // clean up
1542 if (orig) delete orig;
1544 return true;
1545 }
1548 bool
1549 sp_selected_path_simplify_items(SPDesktop *desktop,
1550 Inkscape::Selection *selection, GSList *items,
1551 float threshold, bool justCoalesce,
1552 float angleLimit, bool breakableAngles,
1553 bool modifySelection)
1554 {
1555 bool simplifyIndividualPaths =
1556 (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0);
1558 gchar *simplificationType;
1559 if (simplifyIndividualPaths) {
1560 simplificationType = _("Simplifying paths (separately):");
1561 } else {
1562 simplificationType = _("Simplifying paths:");
1563 }
1565 bool didSomething = false;
1567 NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
1568 if (!selectionBbox) {
1569 return false;
1570 }
1571 gdouble selectionSize = L2(selectionBbox->dimensions());
1573 gdouble simplifySize = selectionSize;
1575 int pathsSimplified = 0;
1576 int totalPathCount = g_slist_length(items);
1578 // set "busy" cursor
1579 desktop->setWaitingCursor();
1581 for (; items != NULL; items = items->next) {
1582 SPItem *item = (SPItem *) items->data;
1584 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1585 continue;
1587 if (simplifyIndividualPaths) {
1588 NR::Maybe<NR::Rect> itemBbox = item->getBounds(sp_item_i2d_affine(item));
1589 if (itemBbox) {
1590 simplifySize = L2(itemBbox->dimensions());
1591 } else {
1592 simplifySize = 0;
1593 }
1594 }
1596 pathsSimplified++;
1598 if (pathsSimplified % 20 == 0) {
1599 gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
1600 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1601 }
1603 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1604 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1605 }
1607 desktop->clearWaitingCursor();
1609 if (pathsSimplified > 20) {
1610 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1611 }
1613 return didSomething;
1614 }
1616 void
1617 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1618 float angleLimit, bool breakableAngles)
1619 {
1620 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1622 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1624 if (selection->isEmpty()) {
1625 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1626 _("Select <b>path(s)</b> to simplify."));
1627 return;
1628 }
1630 GSList *items = g_slist_copy((GSList *) selection->itemList());
1632 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1633 items, threshold,
1634 justCoalesce,
1635 angleLimit,
1636 breakableAngles, true);
1638 if (didSomething)
1639 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1640 _("Simplify"));
1641 else
1642 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1644 }
1647 // globals for keeping track of accelerated simplify
1648 static double previousTime = 0.0;
1649 static gdouble simplifyMultiply = 1.0;
1651 void
1652 sp_selected_path_simplify(void)
1653 {
1654 gdouble simplifyThreshold =
1655 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1656 bool simplifyJustCoalesce =
1657 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1659 //Get the current time
1660 GTimeVal currentTimeVal;
1661 g_get_current_time(¤tTimeVal);
1662 double currentTime = currentTimeVal.tv_sec * 1000000 +
1663 currentTimeVal.tv_usec;
1665 //Was the previous call to this function recent? (<0.5 sec)
1666 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1668 // add to the threshold 1/2 of its original value
1669 simplifyMultiply += 0.5;
1670 simplifyThreshold *= simplifyMultiply;
1672 } else {
1673 // reset to the default
1674 simplifyMultiply = 1;
1675 }
1677 //remember time for next call
1678 previousTime = currentTime;
1680 //g_print("%g\n", simplify_threshold);
1682 //Make the actual call
1683 sp_selected_path_simplify_selection(simplifyThreshold,
1684 simplifyJustCoalesce, 0.0, false);
1685 }
1689 // fonctions utilitaires
1691 bool
1692 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1693 {
1694 if (who == NULL || a == NULL)
1695 return false;
1696 if (who == a)
1697 return true;
1698 return Ancetre(sp_repr_parent(a), who);
1699 }
1701 Path *
1702 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1703 {
1704 SPCurve *curve;
1706 if (!item)
1707 return NULL;
1709 if (SP_IS_SHAPE(item))
1710 {
1711 curve = sp_shape_get_curve(SP_SHAPE(item));
1712 }
1713 else if (SP_IS_TEXT(item))
1714 {
1715 curve = SP_TEXT(item)->getNormalizedBpath();
1716 }
1717 else if (SP_IS_IMAGE(item))
1718 {
1719 curve = sp_image_get_curve(SP_IMAGE(item));
1720 }
1721 else
1722 {
1723 curve = NULL;
1724 }
1726 if (!curve)
1727 return NULL;
1728 NArtBpath *bpath = SP_CURVE_BPATH(curve);
1729 if (bpath == NULL)
1730 return NULL;
1732 if ( doTransformation ) {
1733 if (transformFull)
1734 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1735 else
1736 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1737 sp_curve_unref(curve);
1738 curve=NULL;
1739 } else {
1740 bpath=SP_CURVE_BPATH(curve);
1741 }
1743 Path *dest = bpath_to_Path(bpath);
1745 if ( doTransformation ) {
1746 if ( bpath ) g_free(bpath);
1747 } else {
1748 sp_curve_unref(curve);
1749 }
1750 return dest;
1751 }
1753 Path *bpath_to_Path(NArtBpath const *bpath) {
1754 Path *dest = new Path;
1755 dest->SetBackData(false);
1756 {
1757 int i;
1758 bool closed = false;
1759 float lastX = 0.0;
1760 float lastY = 0.0;
1762 for (i = 0; bpath[i].code != NR_END; i++) {
1763 switch (bpath[i].code) {
1764 case NR_LINETO:
1765 lastX = bpath[i].x3;
1766 lastY = bpath[i].y3;
1767 {
1768 NR::Point tmp(lastX, lastY);
1769 dest->LineTo(tmp);
1770 }
1771 break;
1773 case NR_CURVETO:
1774 {
1775 NR::Point tmp, tms, tme;
1776 tmp[0]=bpath[i].x3;
1777 tmp[1]=bpath[i].y3;
1778 tms[0]=3 * (bpath[i].x1 - lastX);
1779 tms[1]=3 * (bpath[i].y1 - lastY);
1780 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1781 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1782 dest->CubicTo(tmp,tms,tme);
1783 }
1784 lastX = bpath[i].x3;
1785 lastY = bpath[i].y3;
1786 break;
1788 case NR_MOVETO_OPEN:
1789 case NR_MOVETO:
1790 if (closed)
1791 dest->Close();
1792 closed = (bpath[i].code == NR_MOVETO);
1793 lastX = bpath[i].x3;
1794 lastY = bpath[i].y3;
1795 {
1796 NR::Point tmp(lastX, lastY);
1797 dest->MoveTo(tmp);
1798 }
1799 break;
1800 default:
1801 break;
1802 }
1803 }
1804 if (closed)
1805 dest->Close();
1806 }
1807 return dest;
1808 }
1810 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1811 {
1812 //get nearest position on path
1813 Path::cut_position pos = path->PointToCurvilignPosition(p);
1814 return pos;
1815 }
1817 NR::Point get_point_on_Path(Path *path, int piece, double t)
1818 {
1819 NR::Point p;
1820 path->PointAt(piece, t, p);
1821 return p;
1822 }
1825 /*
1826 Local Variables:
1827 mode:c++
1828 c-file-style:"stroustrup"
1829 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1830 indent-tabs-mode:nil
1831 fill-column:99
1832 End:
1833 */
1834 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :