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-marker.h"
27 #include "enums.h"
28 #include "sp-text.h"
29 #include "sp-item-group.h"
30 #include "style.h"
31 #include "inkscape.h"
32 #include "document.h"
33 #include "message-stack.h"
34 #include "selection.h"
35 #include "desktop-handles.h"
36 #include "desktop.h"
37 #include "display/canvas-bpath.h"
38 #include "display/curve.h"
39 #include <glibmm/i18n.h>
40 #include "prefs-utils.h"
42 #include "libnr/n-art-bpath.h"
43 #include "libnr/nr-path.h"
44 #include "xml/repr.h"
45 #include "xml/repr-sorting.h"
47 #include <libnr/nr-matrix-fns.h>
48 #include <libnr/nr-matrix-ops.h>
49 #include <libnr/nr-matrix-translate-ops.h>
50 #include <libnr/nr-scale-matrix-ops.h>
52 #include "livarot/Path.h"
53 #include "livarot/Shape.h"
55 #include "splivarot.h"
57 bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
59 void sp_selected_path_boolop(bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
60 void sp_selected_path_do_offset(bool expand, double prefOffset);
61 void sp_selected_path_create_offset_object(int expand, bool updating);
63 void
64 sp_selected_path_union()
65 {
66 sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
67 }
69 void
70 sp_selected_path_intersect()
71 {
72 sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
73 }
75 void
76 sp_selected_path_diff()
77 {
78 sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
79 }
81 void
82 sp_selected_path_symdiff()
83 {
84 sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
85 }
86 void
87 sp_selected_path_cut()
88 {
89 sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
90 }
91 void
92 sp_selected_path_slice()
93 {
94 sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut Path"));
95 }
98 // boolean operations
99 // take the source paths from the file, do the operation, delete the originals and add the results
100 void
101 sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description)
102 {
103 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
105 Inkscape::Selection *selection = sp_desktop_selection(desktop);
107 GSList *il = (GSList *) selection->itemList();
109 if (g_slist_length(il) < 2) {
110 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
111 return;
112 }
114 if (g_slist_length(il) > 2) {
115 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
116 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
117 return;
118 }
119 }
121 // reverseOrderForOp marks whether the order of the list is the top->down order
122 // it's only used when there are 2 objects, and for operations who need to know the
123 // topmost object (differences, cuts)
124 bool reverseOrderForOp = false;
126 // mettre les elements de la liste dans l'ordre pour ces operations
127 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
128 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
129 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
130 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
132 if (a == NULL || b == NULL) {
133 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."));
134 return;
135 }
137 if (Ancetre(a, b)) {
138 // a is the parent of b, already in the proper order
139 } else if (Ancetre(b, a)) {
140 // reverse order
141 reverseOrderForOp = true;
142 } else {
144 // objects are not in parent/child relationship;
145 // find their lowest common ancestor
146 Inkscape::XML::Node *dad = LCA(a, b);
147 if (dad == NULL) {
148 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Unable to determine the <b>z-order</b> of the objects selected for difference, XOR, division, or path cut."));
149 return;
150 }
152 // find the children of the LCA that lead from it to the a and b
153 Inkscape::XML::Node *as = AncetreFils(a, dad);
154 Inkscape::XML::Node *bs = AncetreFils(b, dad);
156 // find out which comes first
157 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
158 if (child == as) {
159 /* a first, so reverse. */
160 reverseOrderForOp = true;
161 break;
162 }
163 if (child == bs)
164 break;
165 }
166 }
167 }
169 il = g_slist_copy(il);
171 // first check if all the input objects have shapes
172 // otherwise bail out
173 for (GSList *l = il; l != NULL; l = l->next)
174 {
175 SPItem *item = SP_ITEM(l->data);
176 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
177 {
178 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
179 g_slist_free(il);
180 return;
181 }
182 }
184 // extract the livarot Paths from the source objects
185 // also get the winding rule specified in the style
186 int nbOriginaux = g_slist_length(il);
187 std::vector<Path *> originaux(nbOriginaux);
188 std::vector<FillRule> origWind(nbOriginaux);
189 int curOrig;
190 {
191 curOrig = 0;
192 for (GSList *l = il; l != NULL; l = l->next)
193 {
194 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
195 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
196 if (val && strcmp(val, "nonzero") == 0) {
197 origWind[curOrig]= fill_nonZero;
198 } else if (val && strcmp(val, "evenodd") == 0) {
199 origWind[curOrig]= fill_oddEven;
200 } else {
201 origWind[curOrig]= fill_nonZero;
202 }
204 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
205 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
206 {
207 for (int i = curOrig; i >= 0; i--) delete originaux[i];
208 g_slist_free(il);
209 return;
210 }
211 curOrig++;
212 }
213 }
214 // reverse if needed
215 // note that the selection list keeps its order
216 if ( reverseOrderForOp ) {
217 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
218 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
219 }
221 // and work
222 // some temporary instances, first
223 Shape *theShapeA = new Shape;
224 Shape *theShapeB = new Shape;
225 Shape *theShape = new Shape;
226 Path *res = new Path;
227 res->SetBackData(false);
228 Path::cut_position *toCut=NULL;
229 int nbToCut=0;
231 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
232 // true boolean op
233 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
234 originaux[0]->ConvertWithBackData(0.1);
236 originaux[0]->Fill(theShape, 0);
238 theShapeA->ConvertToShape(theShape, origWind[0]);
240 curOrig = 1;
241 for (GSList *l = il->next; l != NULL; l = l->next) {
242 originaux[curOrig]->ConvertWithBackData(0.1);
244 originaux[curOrig]->Fill(theShape, curOrig);
246 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
248 // les elements arrivent en ordre inverse dans la liste
249 theShape->Booleen(theShapeB, theShapeA, bop);
251 {
252 Shape *swap = theShape;
253 theShape = theShapeA;
254 theShapeA = swap;
255 }
256 curOrig++;
257 }
259 {
260 Shape *swap = theShape;
261 theShape = theShapeA;
262 theShapeA = swap;
263 }
265 } else if ( bop == bool_op_cut ) {
266 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
267 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
268 // it is just uncrossed, and cleaned from duplicate edges and points
269 // then it's fed to Booleen() which will uncross it against the other path
270 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
271 // thus making a polygon. the weight of the edges of the cut are all 0, but
272 // the Booleen need to invert the ones inside the source polygon (for the subsequent
273 // ConvertToForme)
275 // the cut path needs to have the highest pathID in the back data
276 // that's how the Booleen() function knows it's an edge of the cut
278 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
279 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
280 // left it at 1.0. Investigate replacing this by a combination of difference and
281 // intersection of the same two paths. -- bb
282 {
283 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
284 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
285 }
286 originaux[0]->ConvertWithBackData(1.0);
288 originaux[0]->Fill(theShape, 0);
290 theShapeA->ConvertToShape(theShape, origWind[0]);
292 originaux[1]->ConvertWithBackData(1.0);
294 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
296 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
298 // les elements arrivent en ordre inverse dans la liste
299 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
301 } else if ( bop == bool_op_slice ) {
302 // slice is not really a boolean operation
303 // you just put the 2 shapes in a single polygon, uncross it
304 // the points where the degree is > 2 are intersections
305 // just check it's an intersection on the path you want to cut, and keep it
306 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
307 // make new subpath at each one of these positions
308 // inversion pour l'op\8eration
309 {
310 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
311 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
312 }
313 originaux[0]->ConvertWithBackData(1.0);
315 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
317 originaux[1]->ConvertWithBackData(1.0);
319 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
321 theShape->ConvertToShape(theShapeA, fill_justDont);
323 if ( theShape->hasBackData() ) {
324 // should always be the case, but ya never know
325 {
326 for (int i = 0; i < theShape->numberOfPoints(); i++) {
327 if ( theShape->getPoint(i).totalDegree() > 2 ) {
328 // possibly an intersection
329 // we need to check that at least one edge from the source path is incident to it
330 // before we declare it's an intersection
331 int cb = theShape->getPoint(i).incidentEdge[FIRST];
332 int nbOrig=0;
333 int nbOther=0;
334 int piece=-1;
335 float t=0.0;
336 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
337 if ( theShape->ebData[cb].pathID == 0 ) {
338 // the source has an edge incident to the point, get its position on the path
339 piece=theShape->ebData[cb].pieceID;
340 if ( theShape->getEdge(cb).st == i ) {
341 t=theShape->ebData[cb].tSt;
342 } else {
343 t=theShape->ebData[cb].tEn;
344 }
345 nbOrig++;
346 }
347 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
348 cb=theShape->NextAt(i, cb);
349 }
350 if ( nbOrig > 0 && nbOther > 0 ) {
351 // point incident to both path and cut: an intersection
352 // note that you only keep one position on the source; you could have degenerate
353 // cases where the source crosses itself at this point, and you wouyld miss an intersection
354 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
355 toCut[nbToCut].piece=piece;
356 toCut[nbToCut].t=t;
357 nbToCut++;
358 }
359 }
360 }
361 }
362 {
363 // i think it's useless now
364 int i = theShape->numberOfEdges() - 1;
365 for (;i>=0;i--) {
366 if ( theShape->ebData[i].pathID == 1 ) {
367 theShape->SubEdge(i);
368 }
369 }
370 }
372 }
373 }
375 int* nesting=NULL;
376 int* conts=NULL;
377 int nbNest=0;
378 // pour compenser le swap juste avant
379 if ( bop == bool_op_slice ) {
380 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
381 // res->ConvertForcedToMoveTo();
382 res->Copy(originaux[0]);
383 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
384 free(toCut);
385 } else if ( bop == bool_op_cut ) {
386 // il faut appeler pour desallouer PointData (pas vital, mais bon)
387 // the Booleen() function did not deallocated the point_data array in theShape, because this
388 // function needs it.
389 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
390 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
391 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
392 } else {
393 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
394 }
396 delete theShape;
397 delete theShapeA;
398 delete theShapeB;
399 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
401 if (res->descr_cmd.size() <= 1)
402 {
403 // only one command, presumably a moveto: it isn't a path
404 for (GSList *l = il; l != NULL; l = l->next)
405 {
406 SP_OBJECT(l->data)->deleteObject();
407 }
408 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
409 /* TODO: annotate */ "splivarot.cpp:409");
410 selection->clear();
412 delete res;
413 g_slist_free(il);
414 return;
415 }
417 // remember important aspects of the source path, to be restored
418 Inkscape::XML::Node *repr_source;
419 if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
420 if (reverseOrderForOp) {
421 repr_source = SP_OBJECT_REPR(il->data);
422 } else {
423 repr_source = SP_OBJECT_REPR(il->next->data);
424 }
425 } else {
426 // find out the bottom object
427 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
429 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
430 repr_source = ((Inkscape::XML::Node *) sorted->data);
431 g_slist_free(sorted);
432 }
433 gint pos = repr_source->position();
434 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
435 char const *id = repr_source->attribute("id");
436 char const *style = repr_source->attribute("style");
439 // remove source paths
440 selection->clear();
441 for (GSList *l = il; l != NULL; l = l->next) {
442 // if this is the bottommost object,
443 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
444 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
445 SP_OBJECT(l->data)->deleteObject(false);
446 } else {
447 // delete the object for real, so that its clones can take appropriate action
448 SP_OBJECT(l->data)->deleteObject();
449 }
450 }
451 g_slist_free(il);
453 // premultiply by the inverse of parent's repr
454 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
455 NR::Matrix local = sp_item_i2doc_affine(parent_item);
456 gchar affinestr[80];
457 gchar *transform = NULL;
458 if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
459 transform = affinestr;
460 }
462 // now that we have the result, add it on the canvas
463 if ( bop == bool_op_cut || bop == bool_op_slice ) {
464 int nbRP=0;
465 Path** resPath;
466 if ( bop == bool_op_slice ) {
467 // there are moveto's at each intersection, but it's still one unique path
468 // so break it down and add each subpath independently
469 // we could call break_apart to do this, but while we have the description...
470 resPath=res->SubPaths(nbRP, false);
471 } else {
472 // cut operation is a bit wicked: you need to keep holes
473 // that's why you needed the nesting
474 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
475 // to get the path for each part of the polygon. that's why you need the nesting info:
476 // to know in wich subpath to add a subpath
477 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
479 // cleaning
480 if ( conts ) free(conts);
481 if ( nesting ) free(nesting);
482 }
484 // add all the pieces resulting from cut or slice
485 for (int i=0;i<nbRP;i++) {
486 gchar *d = resPath[i]->svg_dump_path();
488 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
489 repr->setAttribute("style", style);
490 repr->setAttribute("d", d);
491 g_free(d);
493 // for slice, remove fill
494 if (bop == bool_op_slice) {
495 SPCSSAttr *css;
497 css = sp_repr_css_attr_new();
498 sp_repr_css_set_property(css, "fill", "none");
500 sp_repr_css_change(repr, css, "style");
502 sp_repr_css_attr_unref(css);
503 }
505 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
506 // this means it's basically random which of the pieces inherits the original's id and clones
507 // a better algorithm might figure out e.g. the biggest piece
508 repr->setAttribute("id", id);
510 repr->setAttribute("transform", transform);
512 // add the new repr to the parent
513 parent->appendChild(repr);
515 // move to the saved position
516 repr->setPosition(pos > 0 ? pos : 0);
518 selection->add(repr);
519 Inkscape::GC::release(repr);
521 delete resPath[i];
522 }
523 if ( resPath ) free(resPath);
525 } else {
526 gchar *d = res->svg_dump_path();
528 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
529 repr->setAttribute("style", style);
531 repr->setAttribute("d", d);
532 g_free(d);
534 repr->setAttribute("transform", transform);
536 repr->setAttribute("id", id);
537 parent->appendChild(repr);
538 repr->setPosition(pos > 0 ? pos : 0);
540 selection->add(repr);
541 Inkscape::GC::release(repr);
542 }
544 sp_document_done(sp_desktop_document(desktop), verb, description);
546 delete res;
547 }
550 void
551 sp_selected_path_outline()
552 {
553 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
555 Inkscape::Selection *selection = sp_desktop_selection(desktop);
557 if (selection->isEmpty()) {
558 // TRANSLATORS: "to outline" means "to convert stroke to path"
559 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to outline."));
560 return;
561 }
563 bool did = false;
565 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
566 items != NULL;
567 items = items->next) {
569 SPItem *item = (SPItem *) items->data;
571 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
572 continue;
574 SPCurve *curve = NULL;
575 if (SP_IS_SHAPE(item)) {
576 curve = sp_shape_get_curve(SP_SHAPE(item));
577 if (curve == NULL)
578 continue;
579 }
580 if (SP_IS_TEXT(item)) {
581 curve = SP_TEXT(item)->getNormalizedBpath();
582 if (curve == NULL)
583 continue;
584 }
586 { // pas de stroke pas de chocolat
587 SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
588 gchar const *val = sp_repr_css_property(css, "stroke", NULL);
590 if (val == NULL || strcmp(val, "none") == 0) {
591 sp_curve_unref(curve);
592 continue;
593 }
594 }
596 // remember old stroke style, to be set on fill
597 SPCSSAttr *ncss;
598 {
599 SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
600 gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
601 gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
603 ncss = sp_repr_css_attr_new();
605 sp_repr_css_set_property(ncss, "stroke", "none");
606 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
607 sp_repr_css_set_property(ncss, "fill", val);
608 if ( opac ) {
609 sp_repr_css_set_property(ncss, "fill-opacity", opac);
610 } else {
611 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
612 }
613 sp_repr_css_unset_property(ncss, "marker-start");
614 sp_repr_css_unset_property(ncss, "marker-mid");
615 sp_repr_css_unset_property(ncss, "marker-end");
616 }
618 NR::Matrix const transform(item->transform);
619 float const scale = transform.expansion();
620 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
621 SPStyle *i_style = SP_OBJECT(item)->style;
623 float o_width, o_miter;
624 JoinType o_join;
625 ButtType o_butt;
627 {
628 int jointype, captype;
630 jointype = i_style->stroke_linejoin.computed;
631 captype = i_style->stroke_linecap.computed;
632 o_width = i_style->stroke_width.computed;
634 switch (jointype) {
635 case SP_STROKE_LINEJOIN_MITER:
636 o_join = join_pointy;
637 break;
638 case SP_STROKE_LINEJOIN_ROUND:
639 o_join = join_round;
640 break;
641 default:
642 o_join = join_straight;
643 break;
644 }
646 switch (captype) {
647 case SP_STROKE_LINECAP_SQUARE:
648 o_butt = butt_square;
649 break;
650 case SP_STROKE_LINECAP_ROUND:
651 o_butt = butt_round;
652 break;
653 default:
654 o_butt = butt_straight;
655 break;
656 }
658 if (o_width < 0.1)
659 o_width = 0.1;
660 o_miter = i_style->stroke_miterlimit.value * o_width;
661 }
663 Path *orig = Path_for_item(item, false);
664 if (orig == NULL) {
665 g_free(style);
666 sp_curve_unref(curve);
667 continue;
668 }
670 Path *res = new Path;
671 res->SetBackData(false);
673 if (i_style->stroke_dash.n_dash) {
674 // For dashed strokes, use Stroke method, because Outline can't do dashes
675 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
677 orig->ConvertWithBackData(0.1);
679 orig->DashPolylineFromStyle(i_style, scale, 0);
681 Shape* theShape = new Shape;
682 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
683 0.5 * o_miter);
684 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
686 Shape *theRes = new Shape;
688 theRes->ConvertToShape(theShape, fill_positive);
690 Path *originaux[1];
691 originaux[0] = res;
692 theRes->ConvertToForme(orig, 1, originaux);
694 res->Coalesce(5.0);
696 delete theShape;
697 delete theRes;
699 } else {
701 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
703 orig->Coalesce(0.5 * o_width);
705 Shape *theShape = new Shape;
706 Shape *theRes = new Shape;
708 res->ConvertWithBackData(1.0);
709 res->Fill(theShape, 0);
710 theRes->ConvertToShape(theShape, fill_positive);
712 Path *originaux[1];
713 originaux[0] = res;
714 theRes->ConvertToForme(orig, 1, originaux);
716 delete theShape;
717 delete theRes;
718 }
720 if (orig->descr_cmd.size() <= 1) {
721 // ca a merd\8e, ou bien le resultat est vide
722 delete res;
723 delete orig;
724 g_free(style);
725 continue;
726 }
728 did = true;
730 // remember the position of the item
731 gint pos = SP_OBJECT_REPR(item)->position();
732 // remember parent
733 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
734 // remember id
735 char const *id = SP_OBJECT_REPR(item)->attribute("id");
737 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
739 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
741 // restore old style
742 repr->setAttribute("style", style);
744 // set old stroke style on fill
745 sp_repr_css_change(repr, ncss, "style");
747 sp_repr_css_attr_unref(ncss);
749 gchar *str = orig->svg_dump_path();
750 repr->setAttribute("d", str);
751 g_free(str);
754 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
756 Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
758 // add the group to the parent
759 parent->appendChild(g_repr);
760 // move to the saved position
761 g_repr->setPosition(pos > 0 ? pos : 0);
763 g_repr->appendChild(repr);
764 repr->setAttribute("id", id);
765 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
766 sp_item_write_transform(newitem, repr, transform);
768 SPShape *shape = SP_SHAPE(item);
770 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
771 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
772 if (sp_shape_marker_required (shape, m, bp)) {
774 SPMarker* marker = SP_MARKER (shape->marker[m]);
775 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
777 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
779 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
780 tr = NR::scale(i_style->stroke_width.computed) * tr;
781 }
783 // total marker transform
784 tr = marker_item->transform * marker->c2p * tr * transform;
786 if (SP_OBJECT_REPR(marker_item)) {
787 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
788 g_repr->appendChild(m_repr);
789 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
790 sp_item_write_transform(marker_item, m_repr, tr);
791 }
792 }
793 }
794 }
797 selection->add(g_repr);
799 Inkscape::GC::release(g_repr);
802 } else {
804 // add the new repr to the parent
805 parent->appendChild(repr);
807 // move to the saved position
808 repr->setPosition(pos > 0 ? pos : 0);
810 repr->setAttribute("id", id);
812 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
813 sp_item_write_transform(newitem, repr, transform);
815 selection->add(repr);
817 }
819 Inkscape::GC::release(repr);
821 sp_curve_unref(curve);
822 selection->remove(item);
823 SP_OBJECT(item)->deleteObject(false);
825 }
827 delete res;
828 delete orig;
829 g_free(style);
831 }
833 if (did) {
834 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
835 /* TODO: annotate */ "splivarot.cpp:846");
836 } else {
837 // TRANSLATORS: "to outline" means "to convert stroke to path"
838 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> to outline in the selection."));
839 return;
840 }
841 }
844 void
845 sp_selected_path_offset()
846 {
847 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
849 sp_selected_path_do_offset(true, prefOffset);
850 }
851 void
852 sp_selected_path_inset()
853 {
854 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
856 sp_selected_path_do_offset(false, prefOffset);
857 }
859 void
860 sp_selected_path_offset_screen(double pixels)
861 {
862 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
863 }
865 void
866 sp_selected_path_inset_screen(double pixels)
867 {
868 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
869 }
872 void sp_selected_path_create_offset_object_zero()
873 {
874 sp_selected_path_create_offset_object(0, false);
875 }
877 void sp_selected_path_create_offset()
878 {
879 sp_selected_path_create_offset_object(1, false);
880 }
881 void sp_selected_path_create_inset()
882 {
883 sp_selected_path_create_offset_object(-1, false);
884 }
886 void sp_selected_path_create_updating_offset_object_zero()
887 {
888 sp_selected_path_create_offset_object(0, true);
889 }
891 void sp_selected_path_create_updating_offset()
892 {
893 sp_selected_path_create_offset_object(1, true);
894 }
895 void sp_selected_path_create_updating_inset()
896 {
897 sp_selected_path_create_offset_object(-1, true);
898 }
900 void
901 sp_selected_path_create_offset_object(int expand, bool updating)
902 {
903 Inkscape::Selection *selection;
904 Inkscape::XML::Node *repr;
905 SPItem *item;
906 SPCurve *curve;
907 gchar *style, *str;
908 SPDesktop *desktop;
909 float o_width, o_miter;
910 JoinType o_join;
911 ButtType o_butt;
913 curve = NULL;
915 desktop = SP_ACTIVE_DESKTOP;
917 selection = sp_desktop_selection(desktop);
919 item = selection->singleItem();
921 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
922 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
923 return;
924 }
925 if (SP_IS_SHAPE(item))
926 {
927 curve = sp_shape_get_curve(SP_SHAPE(item));
928 if (curve == NULL)
929 return;
930 }
931 if (SP_IS_TEXT(item))
932 {
933 curve = SP_TEXT(item)->getNormalizedBpath();
934 if (curve == NULL)
935 return;
936 }
938 NR::Matrix const transform(item->transform);
940 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
942 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
944 // remember the position of the item
945 gint pos = SP_OBJECT_REPR(item)->position();
946 // remember parent
947 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
949 {
950 SPStyle *i_style = SP_OBJECT(item)->style;
951 int jointype, captype;
953 jointype = i_style->stroke_linejoin.value;
954 captype = i_style->stroke_linecap.value;
955 o_width = i_style->stroke_width.computed;
956 if (jointype == SP_STROKE_LINEJOIN_MITER)
957 {
958 o_join = join_pointy;
959 }
960 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
961 {
962 o_join = join_round;
963 }
964 else
965 {
966 o_join = join_straight;
967 }
968 if (captype == SP_STROKE_LINECAP_SQUARE)
969 {
970 o_butt = butt_square;
971 }
972 else if (captype == SP_STROKE_LINECAP_ROUND)
973 {
974 o_butt = butt_round;
975 }
976 else
977 {
978 o_butt = butt_straight;
979 }
981 {
982 double prefOffset = 1.0;
983 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
984 o_width = prefOffset;
985 }
987 if (o_width < 0.01)
988 o_width = 0.01;
989 o_miter = i_style->stroke_miterlimit.value * o_width;
990 }
992 Path *orig = Path_for_item(item, true, false);
993 if (orig == NULL)
994 {
995 g_free(style);
996 sp_curve_unref(curve);
997 return;
998 }
1000 Path *res = new Path;
1001 res->SetBackData(false);
1003 {
1004 Shape *theShape = new Shape;
1005 Shape *theRes = new Shape;
1007 orig->ConvertWithBackData(1.0);
1008 orig->Fill(theShape, 0);
1010 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1011 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1012 if (val && strcmp(val, "nonzero") == 0)
1013 {
1014 theRes->ConvertToShape(theShape, fill_nonZero);
1015 }
1016 else if (val && strcmp(val, "evenodd") == 0)
1017 {
1018 theRes->ConvertToShape(theShape, fill_oddEven);
1019 }
1020 else
1021 {
1022 theRes->ConvertToShape(theShape, fill_nonZero);
1023 }
1025 Path *originaux[1];
1026 originaux[0] = orig;
1027 theRes->ConvertToForme(res, 1, originaux);
1029 delete theShape;
1030 delete theRes;
1031 }
1033 sp_curve_unref(curve);
1035 if (res->descr_cmd.size() <= 1)
1036 {
1037 // pas vraiment de points sur le resultat
1038 // donc il ne reste rien
1039 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
1040 /* TODO: annotate */ "splivarot.cpp:1051");
1041 selection->clear();
1043 delete res;
1044 delete orig;
1045 g_free(style);
1046 return;
1047 }
1049 {
1050 gchar tstr[80];
1052 tstr[79] = '\0';
1054 repr = sp_repr_new("svg:path");
1055 repr->setAttribute("sodipodi:type", "inkscape:offset");
1056 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1057 ? o_width
1058 : expand < 0
1059 ? -o_width
1060 : 0 ));
1062 str = res->svg_dump_path();
1063 repr->setAttribute("inkscape:original", str);
1064 g_free(str);
1066 if ( updating ) {
1067 char const *id = SP_OBJECT(item)->repr->attribute("id");
1068 char const *uri = g_strdup_printf("#%s", id);
1069 repr->setAttribute("xlink:href", uri);
1070 g_free((void *) uri);
1071 } else {
1072 repr->setAttribute("inkscape:href", NULL);
1073 }
1075 repr->setAttribute("style", style);
1077 // add the new repr to the parent
1078 parent->appendChild(repr);
1080 // move to the saved position
1081 repr->setPosition(pos > 0 ? pos : 0);
1083 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1085 if ( updating ) {
1086 // on conserve l'original
1087 // we reapply the transform to the original (offset will feel it)
1088 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1089 } else {
1090 // delete original, apply the transform to the offset
1091 SP_OBJECT(item)->deleteObject(false);
1092 sp_item_write_transform(nitem, repr, transform);
1093 }
1095 // The object just created from a temporary repr is only a seed.
1096 // We need to invoke its write which will update its real repr (in particular adding d=)
1097 SP_OBJECT(nitem)->updateRepr();
1099 Inkscape::GC::release(repr);
1101 selection->set(nitem);
1102 }
1104 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
1105 /* TODO: annotate */ "splivarot.cpp:1116");
1107 delete res;
1108 delete orig;
1110 g_free(style);
1111 }
1124 void
1125 sp_selected_path_do_offset(bool expand, double prefOffset)
1126 {
1127 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1129 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1131 if (selection->isEmpty()) {
1132 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1133 return;
1134 }
1136 bool did = false;
1138 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1139 items != NULL;
1140 items = items->next) {
1142 SPItem *item = (SPItem *) items->data;
1144 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1145 continue;
1147 SPCurve *curve = NULL;
1148 if (SP_IS_SHAPE(item)) {
1149 curve = sp_shape_get_curve(SP_SHAPE(item));
1150 if (curve == NULL)
1151 continue;
1152 }
1153 if (SP_IS_TEXT(item)) {
1154 curve = SP_TEXT(item)->getNormalizedBpath();
1155 if (curve == NULL)
1156 continue;
1157 }
1159 NR::Matrix const transform(item->transform);
1161 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1163 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1165 float o_width, o_miter;
1166 JoinType o_join;
1167 ButtType o_butt;
1169 {
1170 SPStyle *i_style = SP_OBJECT(item)->style;
1171 int jointype, captype;
1173 jointype = i_style->stroke_linejoin.value;
1174 captype = i_style->stroke_linecap.value;
1175 o_width = i_style->stroke_width.computed;
1177 switch (jointype) {
1178 case SP_STROKE_LINEJOIN_MITER:
1179 o_join = join_pointy;
1180 break;
1181 case SP_STROKE_LINEJOIN_ROUND:
1182 o_join = join_round;
1183 break;
1184 default:
1185 o_join = join_straight;
1186 break;
1187 }
1189 switch (captype) {
1190 case SP_STROKE_LINECAP_SQUARE:
1191 o_butt = butt_square;
1192 break;
1193 case SP_STROKE_LINECAP_ROUND:
1194 o_butt = butt_round;
1195 break;
1196 default:
1197 o_butt = butt_straight;
1198 break;
1199 }
1201 o_width = prefOffset;
1203 if (o_width < 0.1)
1204 o_width = 0.1;
1205 o_miter = i_style->stroke_miterlimit.value * o_width;
1206 }
1208 Path *orig = Path_for_item(item, false);
1209 if (orig == NULL) {
1210 g_free(style);
1211 sp_curve_unref(curve);
1212 continue;
1213 }
1215 Path *res = new Path;
1216 res->SetBackData(false);
1218 {
1219 Shape *theShape = new Shape;
1220 Shape *theRes = new Shape;
1222 orig->ConvertWithBackData(0.03);
1223 orig->Fill(theShape, 0);
1225 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1226 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1227 if (val && strcmp(val, "nonzero") == 0)
1228 {
1229 theRes->ConvertToShape(theShape, fill_nonZero);
1230 }
1231 else if (val && strcmp(val, "evenodd") == 0)
1232 {
1233 theRes->ConvertToShape(theShape, fill_oddEven);
1234 }
1235 else
1236 {
1237 theRes->ConvertToShape(theShape, fill_nonZero);
1238 }
1240 // et maintenant: offset
1241 // methode inexacte
1242 /* Path *originaux[1];
1243 originaux[0] = orig;
1244 theRes->ConvertToForme(res, 1, originaux);
1246 if (expand) {
1247 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1248 } else {
1249 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1250 }
1252 orig->ConvertWithBackData(1.0);
1253 orig->Fill(theShape, 0);
1254 theRes->ConvertToShape(theShape, fill_positive);
1255 originaux[0] = orig;
1256 theRes->ConvertToForme(res, 1, originaux);
1258 if (o_width >= 0.5) {
1259 // res->Coalesce(1.0);
1260 res->ConvertEvenLines(1.0);
1261 res->Simplify(1.0);
1262 } else {
1263 // res->Coalesce(o_width);
1264 res->ConvertEvenLines(1.0*o_width);
1265 res->Simplify(1.0 * o_width);
1266 } */
1267 // methode par makeoffset
1269 if (expand)
1270 {
1271 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1272 }
1273 else
1274 {
1275 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1276 }
1277 theRes->ConvertToShape(theShape, fill_positive);
1279 res->Reset();
1280 theRes->ConvertToForme(res);
1282 if (o_width >= 1.0)
1283 {
1284 res->ConvertEvenLines(1.0);
1285 res->Simplify(1.0);
1286 }
1287 else
1288 {
1289 res->ConvertEvenLines(1.0*o_width);
1290 res->Simplify(1.0 * o_width);
1291 }
1293 delete theShape;
1294 delete theRes;
1295 }
1297 did = true;
1299 sp_curve_unref(curve);
1300 // remember the position of the item
1301 gint pos = SP_OBJECT_REPR(item)->position();
1302 // remember parent
1303 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1304 // remember id
1305 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1307 selection->remove(item);
1308 SP_OBJECT(item)->deleteObject(false);
1310 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1312 gchar tstr[80];
1314 tstr[79] = '\0';
1316 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1318 repr->setAttribute("style", style);
1320 gchar *str = res->svg_dump_path();
1321 repr->setAttribute("d", str);
1322 g_free(str);
1324 // add the new repr to the parent
1325 parent->appendChild(repr);
1327 // move to the saved position
1328 repr->setPosition(pos > 0 ? pos : 0);
1330 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1332 // reapply the transform
1333 sp_item_write_transform(newitem, repr, transform);
1335 repr->setAttribute("id", id);
1337 selection->add(repr);
1339 Inkscape::GC::release(repr);
1340 }
1342 delete orig;
1343 delete res;
1344 }
1346 if (did) {
1347 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
1348 /* TODO: annotate */ "splivarot.cpp:1359");
1349 } else {
1350 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1351 return;
1352 }
1353 }
1357 //return true if we changed something, else false
1358 bool
1359 sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item,
1360 float threshold, bool justCoalesce,
1361 float angleLimit, bool breakableAngles,
1362 gdouble size, bool modifySelection)
1363 {
1364 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1365 return false;
1367 //If this is a group, do the children instead
1368 if (SP_IS_GROUP(item)) {
1370 bool didSomething = false;
1372 for ( GSList *children = sp_item_group_item_list(SP_GROUP(item));
1373 children ; children = children->next) {
1375 SPItem *child = (SPItem *) children->data;
1376 didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce,
1377 angleLimit, breakableAngles, size, false);
1378 }
1380 return didSomething;
1381 }
1384 SPCurve *curve = NULL;
1386 if (SP_IS_SHAPE(item)) {
1387 curve = sp_shape_get_curve(SP_SHAPE(item));
1388 if (!curve)
1389 return false;
1390 }
1392 if (SP_IS_TEXT(item)) {
1393 curve = SP_TEXT(item)->getNormalizedBpath();
1394 if (!curve)
1395 return false;
1396 }
1398 // save the transform, to re-apply it after simplification
1399 NR::Matrix const transform(item->transform);
1401 /*
1402 reset the transform, effectively transforming the item by transform.inverse();
1403 this is necessary so that the item is transformed twice back and forth,
1404 allowing all compensations to cancel out regardless of the preferences
1405 */
1406 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1408 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1410 Path *orig = Path_for_item(item, false);
1411 if (orig == NULL) {
1412 g_free(style);
1413 sp_curve_unref(curve);
1414 return false;
1415 }
1417 sp_curve_unref(curve);
1418 // remember the position of the item
1419 gint pos = SP_OBJECT_REPR(item)->position();
1420 // remember parent
1421 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1422 // remember id
1423 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1425 //If a group was selected, to not change the selection list
1426 if (modifySelection)
1427 selection->remove(item);
1429 SP_OBJECT(item)->deleteObject(false);
1431 if ( justCoalesce ) {
1432 orig->Coalesce(threshold * size);
1433 } else {
1434 orig->ConvertEvenLines(threshold * size);
1435 orig->Simplify(threshold * size);
1436 }
1438 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1440 repr->setAttribute("style", style);
1442 gchar *str = orig->svg_dump_path();
1443 repr->setAttribute("d", str);
1444 g_free(str);
1446 // restore id
1447 repr->setAttribute("id", id);
1449 // add the new repr to the parent
1450 parent->appendChild(repr);
1452 // move to the saved position
1453 repr->setPosition(pos > 0 ? pos : 0);
1455 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1457 // reapply the transform
1458 sp_item_write_transform(newitem, repr, transform);
1460 //If we are not in a selected group
1461 if (modifySelection)
1462 selection->add(repr);
1464 Inkscape::GC::release(repr);
1466 // clean up
1467 if (orig) delete orig;
1469 return true;
1470 }
1473 void
1474 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1475 float angleLimit, bool breakableAngles)
1476 {
1477 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1479 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1481 if (selection->isEmpty()) {
1482 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1483 _("Select <b>path(s)</b> to simplify."));
1484 return;
1485 }
1487 // remember selection size
1488 NR::Rect bbox = selection->bounds();
1489 gdouble size = L2(bbox.dimensions());
1491 bool didSomething = false;
1493 //Loop through all of the items in the selection
1494 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1495 items != NULL; items = items->next) {
1497 SPItem *item = (SPItem *) items->data;
1499 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1500 continue;
1502 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1503 threshold, justCoalesce, angleLimit, breakableAngles, size, true);
1504 }
1507 if (didSomething)
1508 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1509 _("Simplify"));
1510 else
1511 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1513 }
1516 // globals for keeping track of accelerated simplify
1517 static double previousTime = 0.0;
1518 static gdouble simplifyMultiply = 1.0;
1520 void
1521 sp_selected_path_simplify(void)
1522 {
1523 gdouble simplifyThreshold =
1524 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1525 bool simplifyJustCoalesce =
1526 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1528 //Get the current time
1529 GTimeVal currentTimeVal;
1530 g_get_current_time(¤tTimeVal);
1531 double currentTime = currentTimeVal.tv_sec * 1000000 +
1532 currentTimeVal.tv_usec;
1534 //Was the previous call to this function recent? (<0.5 sec)
1535 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1537 // add to the threshold 1/2 of its original value
1538 simplifyMultiply += 0.5;
1539 simplifyThreshold *= simplifyMultiply;
1541 } else {
1542 // reset to the default
1543 simplifyMultiply = 1;
1544 }
1546 //remember time for next call
1547 previousTime = currentTime;
1549 //g_print("%g\n", simplify_threshold);
1551 //Make the actual call
1552 sp_selected_path_simplify_selection(simplifyThreshold,
1553 simplifyJustCoalesce, 0.0, false);
1554 }
1558 // fonctions utilitaires
1560 bool
1561 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1562 {
1563 if (who == NULL || a == NULL)
1564 return false;
1565 if (who == a)
1566 return true;
1567 return Ancetre(sp_repr_parent(a), who);
1568 }
1570 Path *
1571 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1572 {
1573 SPCurve *curve;
1575 if (!item)
1576 return NULL;
1578 if (SP_IS_SHAPE(item))
1579 {
1580 curve = sp_shape_get_curve(SP_SHAPE(item));
1581 }
1582 else if (SP_IS_TEXT(item))
1583 {
1584 curve = SP_TEXT(item)->getNormalizedBpath();
1585 }
1586 else
1587 {
1588 curve = NULL;
1589 }
1591 if (!curve)
1592 return NULL;
1593 NArtBpath *bpath = SP_CURVE_BPATH(curve);
1594 if (bpath == NULL)
1595 return NULL;
1597 if ( doTransformation ) {
1598 if (transformFull)
1599 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1600 else
1601 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1602 sp_curve_unref(curve);
1603 curve=NULL;
1604 } else {
1605 bpath=SP_CURVE_BPATH(curve);
1606 }
1608 Path *dest = new Path;
1609 dest->SetBackData(false);
1610 {
1611 int i;
1612 bool closed = false;
1613 float lastX = 0.0;
1614 float lastY = 0.0;
1616 for (i = 0; bpath[i].code != NR_END; i++) {
1617 switch (bpath[i].code) {
1618 case NR_LINETO:
1619 lastX = bpath[i].x3;
1620 lastY = bpath[i].y3;
1621 {
1622 NR::Point tmp(lastX, lastY);
1623 dest->LineTo(tmp);
1624 }
1625 break;
1627 case NR_CURVETO:
1628 {
1629 NR::Point tmp, tms, tme;
1630 tmp[0]=bpath[i].x3;
1631 tmp[1]=bpath[i].y3;
1632 tms[0]=3 * (bpath[i].x1 - lastX);
1633 tms[1]=3 * (bpath[i].y1 - lastY);
1634 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1635 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1636 dest->CubicTo(tmp,tms,tme);
1637 }
1638 lastX = bpath[i].x3;
1639 lastY = bpath[i].y3;
1640 break;
1642 case NR_MOVETO_OPEN:
1643 case NR_MOVETO:
1644 if (closed)
1645 dest->Close();
1646 closed = (bpath[i].code == NR_MOVETO);
1647 lastX = bpath[i].x3;
1648 lastY = bpath[i].y3;
1649 {
1650 NR::Point tmp(lastX, lastY);
1651 dest->MoveTo(tmp);
1652 }
1653 break;
1654 default:
1655 break;
1656 }
1657 }
1658 if (closed)
1659 dest->Close();
1660 }
1662 if ( doTransformation ) {
1663 if ( bpath ) g_free(bpath);
1664 } else {
1665 sp_curve_unref(curve);
1666 }
1667 return dest;
1668 }
1670 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1671 {
1672 //get nearest position on path
1673 Path::cut_position pos = path->PointToCurvilignPosition(p);
1674 return pos;
1675 }
1677 NR::Point get_point_on_Path(Path *path, int piece, double t)
1678 {
1679 NR::Point p;
1680 path->PointAt(piece, t, p);
1681 return p;
1682 }
1685 /*
1686 Local Variables:
1687 mode:c++
1688 c-file-style:"stroustrup"
1689 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1690 indent-tabs-mode:nil
1691 fill-column:99
1692 End:
1693 */
1694 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :