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_union_skip_undo()
71 {
72 sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union"));
73 }
75 void
76 sp_selected_path_intersect()
77 {
78 sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
79 }
81 void
82 sp_selected_path_diff()
83 {
84 sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
85 }
87 void
88 sp_selected_path_symdiff()
89 {
90 sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
91 }
92 void
93 sp_selected_path_cut()
94 {
95 sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
96 }
97 void
98 sp_selected_path_slice()
99 {
100 sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path"));
101 }
104 // boolean operations
105 // take the source paths from the file, do the operation, delete the originals and add the results
106 void
107 sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description)
108 {
109 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
111 Inkscape::Selection *selection = sp_desktop_selection(desktop);
113 GSList *il = (GSList *) selection->itemList();
115 // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
116 if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
117 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
118 return;
119 }
120 else if ( g_slist_length(il) < 1 ) {
121 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
122 return;
123 }
125 if (g_slist_length(il) > 2) {
126 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
127 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
128 return;
129 }
130 }
132 // reverseOrderForOp marks whether the order of the list is the top->down order
133 // it's only used when there are 2 objects, and for operations who need to know the
134 // topmost object (differences, cuts)
135 bool reverseOrderForOp = false;
137 // mettre les elements de la liste dans l'ordre pour ces operations
138 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
139 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
140 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
141 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
143 if (a == NULL || b == NULL) {
144 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."));
145 return;
146 }
148 if (Ancetre(a, b)) {
149 // a is the parent of b, already in the proper order
150 } else if (Ancetre(b, a)) {
151 // reverse order
152 reverseOrderForOp = true;
153 } else {
155 // objects are not in parent/child relationship;
156 // find their lowest common ancestor
157 Inkscape::XML::Node *dad = LCA(a, b);
158 if (dad == NULL) {
159 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."));
160 return;
161 }
163 // find the children of the LCA that lead from it to the a and b
164 Inkscape::XML::Node *as = AncetreFils(a, dad);
165 Inkscape::XML::Node *bs = AncetreFils(b, dad);
167 // find out which comes first
168 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
169 if (child == as) {
170 /* a first, so reverse. */
171 reverseOrderForOp = true;
172 break;
173 }
174 if (child == bs)
175 break;
176 }
177 }
178 }
180 il = g_slist_copy(il);
182 // first check if all the input objects have shapes
183 // otherwise bail out
184 for (GSList *l = il; l != NULL; l = l->next)
185 {
186 SPItem *item = SP_ITEM(l->data);
187 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
188 {
189 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
190 g_slist_free(il);
191 return;
192 }
193 }
195 // extract the livarot Paths from the source objects
196 // also get the winding rule specified in the style
197 int nbOriginaux = g_slist_length(il);
198 std::vector<Path *> originaux(nbOriginaux);
199 std::vector<FillRule> origWind(nbOriginaux);
200 int curOrig;
201 {
202 curOrig = 0;
203 for (GSList *l = il; l != NULL; l = l->next)
204 {
205 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
206 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
207 if (val && strcmp(val, "nonzero") == 0) {
208 origWind[curOrig]= fill_nonZero;
209 } else if (val && strcmp(val, "evenodd") == 0) {
210 origWind[curOrig]= fill_oddEven;
211 } else {
212 origWind[curOrig]= fill_nonZero;
213 }
215 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
216 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
217 {
218 for (int i = curOrig; i >= 0; i--) delete originaux[i];
219 g_slist_free(il);
220 return;
221 }
222 curOrig++;
223 }
224 }
225 // reverse if needed
226 // note that the selection list keeps its order
227 if ( reverseOrderForOp ) {
228 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
229 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
230 }
232 // and work
233 // some temporary instances, first
234 Shape *theShapeA = new Shape;
235 Shape *theShapeB = new Shape;
236 Shape *theShape = new Shape;
237 Path *res = new Path;
238 res->SetBackData(false);
239 Path::cut_position *toCut=NULL;
240 int nbToCut=0;
242 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
243 // true boolean op
244 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
245 originaux[0]->ConvertWithBackData(0.1);
247 originaux[0]->Fill(theShape, 0);
249 theShapeA->ConvertToShape(theShape, origWind[0]);
251 curOrig = 1;
252 for (GSList *l = il->next; l != NULL; l = l->next) {
253 originaux[curOrig]->ConvertWithBackData(0.1);
255 originaux[curOrig]->Fill(theShape, curOrig);
257 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
259 // les elements arrivent en ordre inverse dans la liste
260 theShape->Booleen(theShapeB, theShapeA, bop);
262 {
263 Shape *swap = theShape;
264 theShape = theShapeA;
265 theShapeA = swap;
266 }
267 curOrig++;
268 }
270 {
271 Shape *swap = theShape;
272 theShape = theShapeA;
273 theShapeA = swap;
274 }
276 } else if ( bop == bool_op_cut ) {
277 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
278 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
279 // it is just uncrossed, and cleaned from duplicate edges and points
280 // then it's fed to Booleen() which will uncross it against the other path
281 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
282 // thus making a polygon. the weight of the edges of the cut are all 0, but
283 // the Booleen need to invert the ones inside the source polygon (for the subsequent
284 // ConvertToForme)
286 // the cut path needs to have the highest pathID in the back data
287 // that's how the Booleen() function knows it's an edge of the cut
289 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
290 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
291 // left it at 1.0. Investigate replacing this by a combination of difference and
292 // intersection of the same two paths. -- bb
293 {
294 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
295 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
296 }
297 originaux[0]->ConvertWithBackData(1.0);
299 originaux[0]->Fill(theShape, 0);
301 theShapeA->ConvertToShape(theShape, origWind[0]);
303 originaux[1]->ConvertWithBackData(1.0);
305 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
307 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
309 // les elements arrivent en ordre inverse dans la liste
310 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
312 } else if ( bop == bool_op_slice ) {
313 // slice is not really a boolean operation
314 // you just put the 2 shapes in a single polygon, uncross it
315 // the points where the degree is > 2 are intersections
316 // just check it's an intersection on the path you want to cut, and keep it
317 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
318 // make new subpath at each one of these positions
319 // inversion pour l'op\8eration
320 {
321 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
322 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
323 }
324 originaux[0]->ConvertWithBackData(1.0);
326 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
328 originaux[1]->ConvertWithBackData(1.0);
330 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
332 theShape->ConvertToShape(theShapeA, fill_justDont);
334 if ( theShape->hasBackData() ) {
335 // should always be the case, but ya never know
336 {
337 for (int i = 0; i < theShape->numberOfPoints(); i++) {
338 if ( theShape->getPoint(i).totalDegree() > 2 ) {
339 // possibly an intersection
340 // we need to check that at least one edge from the source path is incident to it
341 // before we declare it's an intersection
342 int cb = theShape->getPoint(i).incidentEdge[FIRST];
343 int nbOrig=0;
344 int nbOther=0;
345 int piece=-1;
346 float t=0.0;
347 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
348 if ( theShape->ebData[cb].pathID == 0 ) {
349 // the source has an edge incident to the point, get its position on the path
350 piece=theShape->ebData[cb].pieceID;
351 if ( theShape->getEdge(cb).st == i ) {
352 t=theShape->ebData[cb].tSt;
353 } else {
354 t=theShape->ebData[cb].tEn;
355 }
356 nbOrig++;
357 }
358 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
359 cb=theShape->NextAt(i, cb);
360 }
361 if ( nbOrig > 0 && nbOther > 0 ) {
362 // point incident to both path and cut: an intersection
363 // note that you only keep one position on the source; you could have degenerate
364 // cases where the source crosses itself at this point, and you wouyld miss an intersection
365 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
366 toCut[nbToCut].piece=piece;
367 toCut[nbToCut].t=t;
368 nbToCut++;
369 }
370 }
371 }
372 }
373 {
374 // i think it's useless now
375 int i = theShape->numberOfEdges() - 1;
376 for (;i>=0;i--) {
377 if ( theShape->ebData[i].pathID == 1 ) {
378 theShape->SubEdge(i);
379 }
380 }
381 }
383 }
384 }
386 int* nesting=NULL;
387 int* conts=NULL;
388 int nbNest=0;
389 // pour compenser le swap juste avant
390 if ( bop == bool_op_slice ) {
391 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
392 // res->ConvertForcedToMoveTo();
393 res->Copy(originaux[0]);
394 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
395 free(toCut);
396 } else if ( bop == bool_op_cut ) {
397 // il faut appeler pour desallouer PointData (pas vital, mais bon)
398 // the Booleen() function did not deallocated the point_data array in theShape, because this
399 // function needs it.
400 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
401 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
402 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
403 } else {
404 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
405 }
407 delete theShape;
408 delete theShapeA;
409 delete theShapeB;
410 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
412 if (res->descr_cmd.size() <= 1)
413 {
414 // only one command, presumably a moveto: it isn't a path
415 for (GSList *l = il; l != NULL; l = l->next)
416 {
417 SP_OBJECT(l->data)->deleteObject();
418 }
419 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
420 description);
421 selection->clear();
423 delete res;
424 g_slist_free(il);
425 return;
426 }
428 // get the source path object
429 SPObject *source;
430 if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
431 if (reverseOrderForOp) {
432 source = SP_OBJECT(il->data);
433 } else {
434 source = SP_OBJECT(il->next->data);
435 }
436 } else {
437 // find out the bottom object
438 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
440 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
442 source = sp_desktop_document(desktop)->
443 getObjectByRepr((Inkscape::XML::Node *)sorted->data);
445 g_slist_free(sorted);
446 }
448 // adjust style properties that depend on a possible transform in the source object in order
449 // to get a correct style attribute for the new path
450 SPItem* item_source = SP_ITEM(source);
451 NR::Matrix i2root = sp_item_i2root_affine(item_source);
452 sp_item_adjust_stroke(item_source, i2root.expansion());
453 sp_item_adjust_pattern(item_source, i2root);
454 sp_item_adjust_gradient(item_source, i2root);
456 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
458 // remember important aspects of the source path, to be restored
459 gint pos = repr_source->position();
460 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
461 gchar const *id = repr_source->attribute("id");
462 gchar const *style = repr_source->attribute("style");
463 gchar const *mask = repr_source->attribute("mask");
464 gchar const *clip_path = repr_source->attribute("clip-path");
466 // remove source paths
467 selection->clear();
468 for (GSList *l = il; l != NULL; l = l->next) {
469 // if this is the bottommost object,
470 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
471 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
472 SP_OBJECT(l->data)->deleteObject(false);
473 } else {
474 // delete the object for real, so that its clones can take appropriate action
475 SP_OBJECT(l->data)->deleteObject();
476 }
477 }
478 g_slist_free(il);
480 // premultiply by the inverse of parent's repr
481 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
482 NR::Matrix local = sp_item_i2doc_affine(parent_item);
483 gchar *transform = sp_svg_transform_write(local);
485 // now that we have the result, add it on the canvas
486 if ( bop == bool_op_cut || bop == bool_op_slice ) {
487 int nbRP=0;
488 Path** resPath;
489 if ( bop == bool_op_slice ) {
490 // there are moveto's at each intersection, but it's still one unique path
491 // so break it down and add each subpath independently
492 // we could call break_apart to do this, but while we have the description...
493 resPath=res->SubPaths(nbRP, false);
494 } else {
495 // cut operation is a bit wicked: you need to keep holes
496 // that's why you needed the nesting
497 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
498 // to get the path for each part of the polygon. that's why you need the nesting info:
499 // to know in wich subpath to add a subpath
500 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
502 // cleaning
503 if ( conts ) free(conts);
504 if ( nesting ) free(nesting);
505 }
507 // add all the pieces resulting from cut or slice
508 for (int i=0;i<nbRP;i++) {
509 gchar *d = resPath[i]->svg_dump_path();
511 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
512 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
513 repr->setAttribute("style", style);
514 if (mask)
515 repr->setAttribute("mask", mask);
516 if (clip_path)
517 repr->setAttribute("clip-path", clip_path);
519 repr->setAttribute("d", d);
520 g_free(d);
522 // for slice, remove fill
523 if (bop == bool_op_slice) {
524 SPCSSAttr *css;
526 css = sp_repr_css_attr_new();
527 sp_repr_css_set_property(css, "fill", "none");
529 sp_repr_css_change(repr, css, "style");
531 sp_repr_css_attr_unref(css);
532 }
534 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
535 // this means it's basically random which of the pieces inherits the original's id and clones
536 // a better algorithm might figure out e.g. the biggest piece
537 repr->setAttribute("id", id);
539 repr->setAttribute("transform", transform);
541 // add the new repr to the parent
542 parent->appendChild(repr);
544 // move to the saved position
545 repr->setPosition(pos > 0 ? pos : 0);
547 selection->add(repr);
548 Inkscape::GC::release(repr);
550 delete resPath[i];
551 }
552 if ( resPath ) free(resPath);
554 } else {
555 gchar *d = res->svg_dump_path();
557 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
558 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
559 repr->setAttribute("style", style);
561 if ( mask )
562 repr->setAttribute("mask", mask);
564 if ( clip_path )
565 repr->setAttribute("clip-path", clip_path);
567 repr->setAttribute("d", d);
568 g_free(d);
570 repr->setAttribute("transform", transform);
572 repr->setAttribute("id", id);
573 parent->appendChild(repr);
574 repr->setPosition(pos > 0 ? pos : 0);
576 selection->add(repr);
577 Inkscape::GC::release(repr);
578 }
580 g_free(transform);
582 if (verb != SP_VERB_NONE) {
583 sp_document_done(sp_desktop_document(desktop), verb, description);
584 }
586 delete res;
587 }
590 void
591 sp_selected_path_outline()
592 {
593 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
595 Inkscape::Selection *selection = sp_desktop_selection(desktop);
597 if (selection->isEmpty()) {
598 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
599 return;
600 }
602 bool did = false;
604 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
605 items != NULL;
606 items = items->next) {
608 SPItem *item = (SPItem *) items->data;
610 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
611 continue;
613 SPCurve *curve = NULL;
614 if (SP_IS_SHAPE(item)) {
615 curve = sp_shape_get_curve(SP_SHAPE(item));
616 if (curve == NULL)
617 continue;
618 }
619 if (SP_IS_TEXT(item)) {
620 curve = SP_TEXT(item)->getNormalizedBpath();
621 if (curve == NULL)
622 continue;
623 }
625 { // pas de stroke pas de chocolat
626 SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
627 gchar const *val = sp_repr_css_property(css, "stroke", NULL);
629 if (val == NULL || strcmp(val, "none") == 0) {
630 sp_curve_unref(curve);
631 continue;
632 }
633 }
635 // remember old stroke style, to be set on fill
636 SPCSSAttr *ncss;
637 {
638 SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
639 gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
640 gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
642 ncss = sp_repr_css_attr_new();
644 sp_repr_css_set_property(ncss, "stroke", "none");
645 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
646 sp_repr_css_set_property(ncss, "fill", val);
647 if ( opac ) {
648 sp_repr_css_set_property(ncss, "fill-opacity", opac);
649 } else {
650 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
651 }
652 sp_repr_css_unset_property(ncss, "marker-start");
653 sp_repr_css_unset_property(ncss, "marker-mid");
654 sp_repr_css_unset_property(ncss, "marker-end");
655 }
657 NR::Matrix const transform(item->transform);
658 float const scale = transform.expansion();
659 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
660 SPStyle *i_style = SP_OBJECT(item)->style;
661 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
662 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
664 float o_width, o_miter;
665 JoinType o_join;
666 ButtType o_butt;
668 {
669 int jointype, captype;
671 jointype = i_style->stroke_linejoin.computed;
672 captype = i_style->stroke_linecap.computed;
673 o_width = i_style->stroke_width.computed;
675 switch (jointype) {
676 case SP_STROKE_LINEJOIN_MITER:
677 o_join = join_pointy;
678 break;
679 case SP_STROKE_LINEJOIN_ROUND:
680 o_join = join_round;
681 break;
682 default:
683 o_join = join_straight;
684 break;
685 }
687 switch (captype) {
688 case SP_STROKE_LINECAP_SQUARE:
689 o_butt = butt_square;
690 break;
691 case SP_STROKE_LINECAP_ROUND:
692 o_butt = butt_round;
693 break;
694 default:
695 o_butt = butt_straight;
696 break;
697 }
699 if (o_width < 0.1)
700 o_width = 0.1;
701 o_miter = i_style->stroke_miterlimit.value * o_width;
702 }
704 Path *orig = Path_for_item(item, false);
705 if (orig == NULL) {
706 g_free(style);
707 sp_curve_unref(curve);
708 continue;
709 }
711 Path *res = new Path;
712 res->SetBackData(false);
714 if (i_style->stroke_dash.n_dash) {
715 // For dashed strokes, use Stroke method, because Outline can't do dashes
716 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
718 orig->ConvertWithBackData(0.1);
720 orig->DashPolylineFromStyle(i_style, scale, 0);
722 Shape* theShape = new Shape;
723 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
724 0.5 * o_miter);
725 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
727 Shape *theRes = new Shape;
729 theRes->ConvertToShape(theShape, fill_positive);
731 Path *originaux[1];
732 originaux[0] = res;
733 theRes->ConvertToForme(orig, 1, originaux);
735 res->Coalesce(5.0);
737 delete theShape;
738 delete theRes;
740 } else {
742 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
744 orig->Coalesce(0.5 * o_width);
746 Shape *theShape = new Shape;
747 Shape *theRes = new Shape;
749 res->ConvertWithBackData(1.0);
750 res->Fill(theShape, 0);
751 theRes->ConvertToShape(theShape, fill_positive);
753 Path *originaux[1];
754 originaux[0] = res;
755 theRes->ConvertToForme(orig, 1, originaux);
757 delete theShape;
758 delete theRes;
759 }
761 if (orig->descr_cmd.size() <= 1) {
762 // ca a merd\8e, ou bien le resultat est vide
763 delete res;
764 delete orig;
765 g_free(style);
766 continue;
767 }
769 did = true;
771 // remember the position of the item
772 gint pos = SP_OBJECT_REPR(item)->position();
773 // remember parent
774 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
775 // remember id
776 char const *id = SP_OBJECT_REPR(item)->attribute("id");
778 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
780 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
781 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
783 // restore old style
784 repr->setAttribute("style", style);
786 // set old stroke style on fill
787 sp_repr_css_change(repr, ncss, "style");
789 sp_repr_css_attr_unref(ncss);
791 gchar *str = orig->svg_dump_path();
792 repr->setAttribute("d", str);
793 g_free(str);
795 if (mask)
796 repr->setAttribute("mask", mask);
797 if (clip_path)
798 repr->setAttribute("clip-path", clip_path);
800 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
802 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
803 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
805 // add the group to the parent
806 parent->appendChild(g_repr);
807 // move to the saved position
808 g_repr->setPosition(pos > 0 ? pos : 0);
810 g_repr->appendChild(repr);
811 repr->setAttribute("id", id);
812 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
813 sp_item_write_transform(newitem, repr, transform);
815 SPShape *shape = SP_SHAPE(item);
817 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
818 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
819 if (sp_shape_marker_required (shape, m, bp)) {
821 SPMarker* marker = SP_MARKER (shape->marker[m]);
822 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
824 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
826 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
827 tr = NR::scale(i_style->stroke_width.computed) * tr;
828 }
830 // total marker transform
831 tr = marker_item->transform * marker->c2p * tr * transform;
833 if (SP_OBJECT_REPR(marker_item)) {
834 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
835 g_repr->appendChild(m_repr);
836 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
837 sp_item_write_transform(marker_item, m_repr, tr);
838 }
839 }
840 }
841 }
844 selection->add(g_repr);
846 Inkscape::GC::release(g_repr);
849 } else {
851 // add the new repr to the parent
852 parent->appendChild(repr);
854 // move to the saved position
855 repr->setPosition(pos > 0 ? pos : 0);
857 repr->setAttribute("id", id);
859 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
860 sp_item_write_transform(newitem, repr, transform);
862 selection->add(repr);
864 }
866 Inkscape::GC::release(repr);
868 sp_curve_unref(curve);
869 selection->remove(item);
870 SP_OBJECT(item)->deleteObject(false);
872 }
874 delete res;
875 delete orig;
876 g_free(style);
878 }
880 if (did) {
881 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
882 _("Convert stroke to path"));
883 } else {
884 // TRANSLATORS: "to outline" means "to convert stroke to path"
885 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
886 return;
887 }
888 }
891 void
892 sp_selected_path_offset()
893 {
894 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
896 sp_selected_path_do_offset(true, prefOffset);
897 }
898 void
899 sp_selected_path_inset()
900 {
901 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
903 sp_selected_path_do_offset(false, prefOffset);
904 }
906 void
907 sp_selected_path_offset_screen(double pixels)
908 {
909 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
910 }
912 void
913 sp_selected_path_inset_screen(double pixels)
914 {
915 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
916 }
919 void sp_selected_path_create_offset_object_zero()
920 {
921 sp_selected_path_create_offset_object(0, false);
922 }
924 void sp_selected_path_create_offset()
925 {
926 sp_selected_path_create_offset_object(1, false);
927 }
928 void sp_selected_path_create_inset()
929 {
930 sp_selected_path_create_offset_object(-1, false);
931 }
933 void sp_selected_path_create_updating_offset_object_zero()
934 {
935 sp_selected_path_create_offset_object(0, true);
936 }
938 void sp_selected_path_create_updating_offset()
939 {
940 sp_selected_path_create_offset_object(1, true);
941 }
942 void sp_selected_path_create_updating_inset()
943 {
944 sp_selected_path_create_offset_object(-1, true);
945 }
947 void
948 sp_selected_path_create_offset_object(int expand, bool updating)
949 {
950 Inkscape::Selection *selection;
951 Inkscape::XML::Node *repr;
952 SPItem *item;
953 SPCurve *curve;
954 gchar *style, *str;
955 SPDesktop *desktop;
956 float o_width, o_miter;
957 JoinType o_join;
958 ButtType o_butt;
960 curve = NULL;
962 desktop = SP_ACTIVE_DESKTOP;
964 selection = sp_desktop_selection(desktop);
966 item = selection->singleItem();
968 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
969 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
970 return;
971 }
972 if (SP_IS_SHAPE(item))
973 {
974 curve = sp_shape_get_curve(SP_SHAPE(item));
975 if (curve == NULL)
976 return;
977 }
978 if (SP_IS_TEXT(item))
979 {
980 curve = SP_TEXT(item)->getNormalizedBpath();
981 if (curve == NULL)
982 return;
983 }
985 NR::Matrix const transform(item->transform);
987 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
989 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
991 // remember the position of the item
992 gint pos = SP_OBJECT_REPR(item)->position();
993 // remember parent
994 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
996 {
997 SPStyle *i_style = SP_OBJECT(item)->style;
998 int jointype, captype;
1000 jointype = i_style->stroke_linejoin.value;
1001 captype = i_style->stroke_linecap.value;
1002 o_width = i_style->stroke_width.computed;
1003 if (jointype == SP_STROKE_LINEJOIN_MITER)
1004 {
1005 o_join = join_pointy;
1006 }
1007 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1008 {
1009 o_join = join_round;
1010 }
1011 else
1012 {
1013 o_join = join_straight;
1014 }
1015 if (captype == SP_STROKE_LINECAP_SQUARE)
1016 {
1017 o_butt = butt_square;
1018 }
1019 else if (captype == SP_STROKE_LINECAP_ROUND)
1020 {
1021 o_butt = butt_round;
1022 }
1023 else
1024 {
1025 o_butt = butt_straight;
1026 }
1028 {
1029 double prefOffset = 1.0;
1030 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
1031 o_width = prefOffset;
1032 }
1034 if (o_width < 0.01)
1035 o_width = 0.01;
1036 o_miter = i_style->stroke_miterlimit.value * o_width;
1037 }
1039 Path *orig = Path_for_item(item, true, false);
1040 if (orig == NULL)
1041 {
1042 g_free(style);
1043 sp_curve_unref(curve);
1044 return;
1045 }
1047 Path *res = new Path;
1048 res->SetBackData(false);
1050 {
1051 Shape *theShape = new Shape;
1052 Shape *theRes = new Shape;
1054 orig->ConvertWithBackData(1.0);
1055 orig->Fill(theShape, 0);
1057 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1058 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1059 if (val && strcmp(val, "nonzero") == 0)
1060 {
1061 theRes->ConvertToShape(theShape, fill_nonZero);
1062 }
1063 else if (val && strcmp(val, "evenodd") == 0)
1064 {
1065 theRes->ConvertToShape(theShape, fill_oddEven);
1066 }
1067 else
1068 {
1069 theRes->ConvertToShape(theShape, fill_nonZero);
1070 }
1072 Path *originaux[1];
1073 originaux[0] = orig;
1074 theRes->ConvertToForme(res, 1, originaux);
1076 delete theShape;
1077 delete theRes;
1078 }
1080 sp_curve_unref(curve);
1082 if (res->descr_cmd.size() <= 1)
1083 {
1084 // pas vraiment de points sur le resultat
1085 // donc il ne reste rien
1086 sp_document_done(sp_desktop_document(desktop),
1087 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1088 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1089 (updating ? _("Create linked offset")
1090 : _("Create dynamic offset")));
1091 selection->clear();
1093 delete res;
1094 delete orig;
1095 g_free(style);
1096 return;
1097 }
1099 {
1100 gchar tstr[80];
1102 tstr[79] = '\0';
1104 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1105 repr = xml_doc->createElement("svg:path");
1106 repr->setAttribute("sodipodi:type", "inkscape:offset");
1107 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1108 ? o_width
1109 : expand < 0
1110 ? -o_width
1111 : 0 ));
1113 str = res->svg_dump_path();
1114 repr->setAttribute("inkscape:original", str);
1115 g_free(str);
1117 if ( updating ) {
1118 char const *id = SP_OBJECT(item)->repr->attribute("id");
1119 char const *uri = g_strdup_printf("#%s", id);
1120 repr->setAttribute("xlink:href", uri);
1121 g_free((void *) uri);
1122 } else {
1123 repr->setAttribute("inkscape:href", NULL);
1124 }
1126 repr->setAttribute("style", style);
1128 // add the new repr to the parent
1129 parent->appendChild(repr);
1131 // move to the saved position
1132 repr->setPosition(pos > 0 ? pos : 0);
1134 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1136 if ( updating ) {
1137 // on conserve l'original
1138 // we reapply the transform to the original (offset will feel it)
1139 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1140 } else {
1141 // delete original, apply the transform to the offset
1142 SP_OBJECT(item)->deleteObject(false);
1143 sp_item_write_transform(nitem, repr, transform);
1144 }
1146 // The object just created from a temporary repr is only a seed.
1147 // We need to invoke its write which will update its real repr (in particular adding d=)
1148 SP_OBJECT(nitem)->updateRepr();
1150 Inkscape::GC::release(repr);
1152 selection->set(nitem);
1153 }
1155 sp_document_done(sp_desktop_document(desktop),
1156 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1157 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1158 (updating ? _("Create linked offset")
1159 : _("Create dynamic offset")));
1161 delete res;
1162 delete orig;
1164 g_free(style);
1165 }
1178 void
1179 sp_selected_path_do_offset(bool expand, double prefOffset)
1180 {
1181 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1183 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1185 if (selection->isEmpty()) {
1186 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1187 return;
1188 }
1190 bool did = false;
1192 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1193 items != NULL;
1194 items = items->next) {
1196 SPItem *item = (SPItem *) items->data;
1198 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1199 continue;
1201 SPCurve *curve = NULL;
1202 if (SP_IS_SHAPE(item)) {
1203 curve = sp_shape_get_curve(SP_SHAPE(item));
1204 if (curve == NULL)
1205 continue;
1206 }
1207 if (SP_IS_TEXT(item)) {
1208 curve = SP_TEXT(item)->getNormalizedBpath();
1209 if (curve == NULL)
1210 continue;
1211 }
1213 NR::Matrix const transform(item->transform);
1215 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1217 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1219 float o_width, o_miter;
1220 JoinType o_join;
1221 ButtType o_butt;
1223 {
1224 SPStyle *i_style = SP_OBJECT(item)->style;
1225 int jointype, captype;
1227 jointype = i_style->stroke_linejoin.value;
1228 captype = i_style->stroke_linecap.value;
1229 o_width = i_style->stroke_width.computed;
1231 switch (jointype) {
1232 case SP_STROKE_LINEJOIN_MITER:
1233 o_join = join_pointy;
1234 break;
1235 case SP_STROKE_LINEJOIN_ROUND:
1236 o_join = join_round;
1237 break;
1238 default:
1239 o_join = join_straight;
1240 break;
1241 }
1243 switch (captype) {
1244 case SP_STROKE_LINECAP_SQUARE:
1245 o_butt = butt_square;
1246 break;
1247 case SP_STROKE_LINECAP_ROUND:
1248 o_butt = butt_round;
1249 break;
1250 default:
1251 o_butt = butt_straight;
1252 break;
1253 }
1255 o_width = prefOffset;
1257 if (o_width < 0.1)
1258 o_width = 0.1;
1259 o_miter = i_style->stroke_miterlimit.value * o_width;
1260 }
1262 Path *orig = Path_for_item(item, false);
1263 if (orig == NULL) {
1264 g_free(style);
1265 sp_curve_unref(curve);
1266 continue;
1267 }
1269 Path *res = new Path;
1270 res->SetBackData(false);
1272 {
1273 Shape *theShape = new Shape;
1274 Shape *theRes = new Shape;
1276 orig->ConvertWithBackData(0.03);
1277 orig->Fill(theShape, 0);
1279 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1280 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1281 if (val && strcmp(val, "nonzero") == 0)
1282 {
1283 theRes->ConvertToShape(theShape, fill_nonZero);
1284 }
1285 else if (val && strcmp(val, "evenodd") == 0)
1286 {
1287 theRes->ConvertToShape(theShape, fill_oddEven);
1288 }
1289 else
1290 {
1291 theRes->ConvertToShape(theShape, fill_nonZero);
1292 }
1294 // et maintenant: offset
1295 // methode inexacte
1296 /* Path *originaux[1];
1297 originaux[0] = orig;
1298 theRes->ConvertToForme(res, 1, originaux);
1300 if (expand) {
1301 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1302 } else {
1303 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1304 }
1306 orig->ConvertWithBackData(1.0);
1307 orig->Fill(theShape, 0);
1308 theRes->ConvertToShape(theShape, fill_positive);
1309 originaux[0] = orig;
1310 theRes->ConvertToForme(res, 1, originaux);
1312 if (o_width >= 0.5) {
1313 // res->Coalesce(1.0);
1314 res->ConvertEvenLines(1.0);
1315 res->Simplify(1.0);
1316 } else {
1317 // res->Coalesce(o_width);
1318 res->ConvertEvenLines(1.0*o_width);
1319 res->Simplify(1.0 * o_width);
1320 } */
1321 // methode par makeoffset
1323 if (expand)
1324 {
1325 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1326 }
1327 else
1328 {
1329 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1330 }
1331 theRes->ConvertToShape(theShape, fill_positive);
1333 res->Reset();
1334 theRes->ConvertToForme(res);
1336 if (o_width >= 1.0)
1337 {
1338 res->ConvertEvenLines(1.0);
1339 res->Simplify(1.0);
1340 }
1341 else
1342 {
1343 res->ConvertEvenLines(1.0*o_width);
1344 res->Simplify(1.0 * o_width);
1345 }
1347 delete theShape;
1348 delete theRes;
1349 }
1351 did = true;
1353 sp_curve_unref(curve);
1354 // remember the position of the item
1355 gint pos = SP_OBJECT_REPR(item)->position();
1356 // remember parent
1357 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1358 // remember id
1359 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1361 selection->remove(item);
1362 SP_OBJECT(item)->deleteObject(false);
1364 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1366 gchar tstr[80];
1368 tstr[79] = '\0';
1370 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1371 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1373 repr->setAttribute("style", style);
1375 gchar *str = res->svg_dump_path();
1376 repr->setAttribute("d", str);
1377 g_free(str);
1379 // add the new repr to the parent
1380 parent->appendChild(repr);
1382 // move to the saved position
1383 repr->setPosition(pos > 0 ? pos : 0);
1385 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1387 // reapply the transform
1388 sp_item_write_transform(newitem, repr, transform);
1390 repr->setAttribute("id", id);
1392 selection->add(repr);
1394 Inkscape::GC::release(repr);
1395 }
1397 delete orig;
1398 delete res;
1399 }
1401 if (did) {
1402 sp_document_done(sp_desktop_document(desktop),
1403 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1404 (expand ? _("Outset path") : _("Inset path")));
1405 } else {
1406 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1407 return;
1408 }
1409 }
1412 static bool
1413 sp_selected_path_simplify_items(SPDesktop *desktop,
1414 Inkscape::Selection *selection, GSList *items,
1415 float threshold, bool justCoalesce,
1416 float angleLimit, bool breakableAngles,
1417 bool modifySelection);
1420 //return true if we changed something, else false
1421 bool
1422 sp_selected_path_simplify_item(SPDesktop *desktop,
1423 Inkscape::Selection *selection, SPItem *item,
1424 float threshold, bool justCoalesce,
1425 float angleLimit, bool breakableAngles,
1426 gdouble size, bool modifySelection)
1427 {
1428 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1429 return false;
1431 //If this is a group, do the children instead
1432 if (SP_IS_GROUP(item)) {
1433 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1435 return sp_selected_path_simplify_items(desktop, selection, items,
1436 threshold, justCoalesce,
1437 angleLimit, breakableAngles,
1438 false);
1439 }
1442 SPCurve *curve = NULL;
1444 if (SP_IS_SHAPE(item)) {
1445 curve = sp_shape_get_curve(SP_SHAPE(item));
1446 if (!curve)
1447 return false;
1448 }
1450 if (SP_IS_TEXT(item)) {
1451 curve = SP_TEXT(item)->getNormalizedBpath();
1452 if (!curve)
1453 return false;
1454 }
1456 // save the transform, to re-apply it after simplification
1457 NR::Matrix const transform(item->transform);
1459 /*
1460 reset the transform, effectively transforming the item by transform.inverse();
1461 this is necessary so that the item is transformed twice back and forth,
1462 allowing all compensations to cancel out regardless of the preferences
1463 */
1464 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1466 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1467 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1468 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1470 Path *orig = Path_for_item(item, false);
1471 if (orig == NULL) {
1472 g_free(style);
1473 sp_curve_unref(curve);
1474 return false;
1475 }
1477 sp_curve_unref(curve);
1478 // remember the position of the item
1479 gint pos = SP_OBJECT_REPR(item)->position();
1480 // remember parent
1481 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1482 // remember id
1483 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1485 //If a group was selected, to not change the selection list
1486 if (modifySelection)
1487 selection->remove(item);
1489 SP_OBJECT(item)->deleteObject(false);
1491 if ( justCoalesce ) {
1492 orig->Coalesce(threshold * size);
1493 } else {
1494 orig->ConvertEvenLines(threshold * size);
1495 orig->Simplify(threshold * size);
1496 }
1498 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1499 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1501 // restore style, mask and clip-path
1502 repr->setAttribute("style", style);
1503 g_free(style);
1505 if ( mask ) {
1506 repr->setAttribute("mask", mask);
1507 g_free(mask);
1508 }
1510 if ( clip_path ) {
1511 repr->setAttribute("clip-path", clip_path);
1512 g_free(clip_path);
1513 }
1515 // path
1516 gchar *str = orig->svg_dump_path();
1517 repr->setAttribute("d", str);
1518 g_free(str);
1520 // restore id
1521 repr->setAttribute("id", id);
1523 // add the new repr to the parent
1524 parent->appendChild(repr);
1526 // move to the saved position
1527 repr->setPosition(pos > 0 ? pos : 0);
1529 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1531 // reapply the transform
1532 sp_item_write_transform(newitem, repr, transform);
1534 //If we are not in a selected group
1535 if (modifySelection)
1536 selection->add(repr);
1538 Inkscape::GC::release(repr);
1540 // clean up
1541 if (orig) delete orig;
1543 return true;
1544 }
1547 bool
1548 sp_selected_path_simplify_items(SPDesktop *desktop,
1549 Inkscape::Selection *selection, GSList *items,
1550 float threshold, bool justCoalesce,
1551 float angleLimit, bool breakableAngles,
1552 bool modifySelection)
1553 {
1554 bool simplifyIndividualPaths =
1555 (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0);
1557 gchar *simplificationType;
1558 if (simplifyIndividualPaths) {
1559 simplificationType = _("Simplifying paths (separately):");
1560 } else {
1561 simplificationType = _("Simplifying paths:");
1562 }
1564 bool didSomething = false;
1566 NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
1567 if (!selectionBbox) {
1568 return false;
1569 }
1570 gdouble selectionSize = L2(selectionBbox->dimensions());
1572 gdouble simplifySize = selectionSize;
1574 int pathsSimplified = 0;
1575 int totalPathCount = g_slist_length(items);
1577 // set "busy" cursor
1578 desktop->setWaitingCursor();
1580 for (; items != NULL; items = items->next) {
1581 SPItem *item = (SPItem *) items->data;
1583 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1584 continue;
1586 if (simplifyIndividualPaths) {
1587 NR::Maybe<NR::Rect> itemBbox = item->getBounds(sp_item_i2d_affine(item));
1588 if (itemBbox) {
1589 simplifySize = L2(itemBbox->dimensions());
1590 } else {
1591 simplifySize = 0;
1592 }
1593 }
1595 pathsSimplified++;
1597 if (pathsSimplified % 20 == 0) {
1598 gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
1599 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1600 }
1602 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1603 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1604 }
1606 desktop->clearWaitingCursor();
1608 if (pathsSimplified > 20) {
1609 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1610 }
1612 return didSomething;
1613 }
1615 void
1616 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1617 float angleLimit, bool breakableAngles)
1618 {
1619 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1621 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1623 if (selection->isEmpty()) {
1624 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1625 _("Select <b>path(s)</b> to simplify."));
1626 return;
1627 }
1629 GSList *items = g_slist_copy((GSList *) selection->itemList());
1631 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1632 items, threshold,
1633 justCoalesce,
1634 angleLimit,
1635 breakableAngles, true);
1637 if (didSomething)
1638 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1639 _("Simplify"));
1640 else
1641 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1643 }
1646 // globals for keeping track of accelerated simplify
1647 static double previousTime = 0.0;
1648 static gdouble simplifyMultiply = 1.0;
1650 void
1651 sp_selected_path_simplify(void)
1652 {
1653 gdouble simplifyThreshold =
1654 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1655 bool simplifyJustCoalesce =
1656 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1658 //Get the current time
1659 GTimeVal currentTimeVal;
1660 g_get_current_time(¤tTimeVal);
1661 double currentTime = currentTimeVal.tv_sec * 1000000 +
1662 currentTimeVal.tv_usec;
1664 //Was the previous call to this function recent? (<0.5 sec)
1665 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1667 // add to the threshold 1/2 of its original value
1668 simplifyMultiply += 0.5;
1669 simplifyThreshold *= simplifyMultiply;
1671 } else {
1672 // reset to the default
1673 simplifyMultiply = 1;
1674 }
1676 //remember time for next call
1677 previousTime = currentTime;
1679 //g_print("%g\n", simplify_threshold);
1681 //Make the actual call
1682 sp_selected_path_simplify_selection(simplifyThreshold,
1683 simplifyJustCoalesce, 0.0, false);
1684 }
1688 // fonctions utilitaires
1690 bool
1691 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1692 {
1693 if (who == NULL || a == NULL)
1694 return false;
1695 if (who == a)
1696 return true;
1697 return Ancetre(sp_repr_parent(a), who);
1698 }
1700 Path *
1701 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1702 {
1703 SPCurve *curve;
1705 if (!item)
1706 return NULL;
1708 if (SP_IS_SHAPE(item))
1709 {
1710 curve = sp_shape_get_curve(SP_SHAPE(item));
1711 }
1712 else if (SP_IS_TEXT(item))
1713 {
1714 curve = SP_TEXT(item)->getNormalizedBpath();
1715 }
1716 else
1717 {
1718 curve = NULL;
1719 }
1721 if (!curve)
1722 return NULL;
1723 NArtBpath *bpath = SP_CURVE_BPATH(curve);
1724 if (bpath == NULL)
1725 return NULL;
1727 if ( doTransformation ) {
1728 if (transformFull)
1729 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1730 else
1731 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1732 sp_curve_unref(curve);
1733 curve=NULL;
1734 } else {
1735 bpath=SP_CURVE_BPATH(curve);
1736 }
1738 Path *dest = bpath_to_Path(bpath);
1740 if ( doTransformation ) {
1741 if ( bpath ) g_free(bpath);
1742 } else {
1743 sp_curve_unref(curve);
1744 }
1745 return dest;
1746 }
1748 Path *bpath_to_Path(NArtBpath const *bpath) {
1749 Path *dest = new Path;
1750 dest->SetBackData(false);
1751 {
1752 int i;
1753 bool closed = false;
1754 float lastX = 0.0;
1755 float lastY = 0.0;
1757 for (i = 0; bpath[i].code != NR_END; i++) {
1758 switch (bpath[i].code) {
1759 case NR_LINETO:
1760 lastX = bpath[i].x3;
1761 lastY = bpath[i].y3;
1762 {
1763 NR::Point tmp(lastX, lastY);
1764 dest->LineTo(tmp);
1765 }
1766 break;
1768 case NR_CURVETO:
1769 {
1770 NR::Point tmp, tms, tme;
1771 tmp[0]=bpath[i].x3;
1772 tmp[1]=bpath[i].y3;
1773 tms[0]=3 * (bpath[i].x1 - lastX);
1774 tms[1]=3 * (bpath[i].y1 - lastY);
1775 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1776 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1777 dest->CubicTo(tmp,tms,tme);
1778 }
1779 lastX = bpath[i].x3;
1780 lastY = bpath[i].y3;
1781 break;
1783 case NR_MOVETO_OPEN:
1784 case NR_MOVETO:
1785 if (closed)
1786 dest->Close();
1787 closed = (bpath[i].code == NR_MOVETO);
1788 lastX = bpath[i].x3;
1789 lastY = bpath[i].y3;
1790 {
1791 NR::Point tmp(lastX, lastY);
1792 dest->MoveTo(tmp);
1793 }
1794 break;
1795 default:
1796 break;
1797 }
1798 }
1799 if (closed)
1800 dest->Close();
1801 }
1802 return dest;
1803 }
1805 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1806 {
1807 //get nearest position on path
1808 Path::cut_position pos = path->PointToCurvilignPosition(p);
1809 return pos;
1810 }
1812 NR::Point get_point_on_Path(Path *path, int piece, double t)
1813 {
1814 NR::Point p;
1815 path->PointAt(piece, t, p);
1816 return p;
1817 }
1820 /*
1821 Local Variables:
1822 mode:c++
1823 c-file-style:"stroustrup"
1824 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1825 indent-tabs-mode:nil
1826 fill-column:99
1827 End:
1828 */
1829 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :