057ebac5a03f8de1deb618cd3aa180c720702770
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 "xml/repr.h"
22 #include "svg/svg.h"
23 #include "sp-path.h"
24 #include "sp-shape.h"
25 #include "sp-marker.h"
26 #include "enums.h"
27 #include "sp-text.h"
28 #include "sp-item-group.h"
29 #include "style.h"
30 #include "inkscape.h"
31 #include "document.h"
32 #include "message-stack.h"
33 #include "selection.h"
34 #include "desktop-handles.h"
35 #include "desktop.h"
36 #include "display/canvas-bpath.h"
37 #include "display/curve.h"
38 #include <glibmm/i18n.h>
39 #include "prefs-utils.h"
41 #include "libnr/n-art-bpath.h"
42 #include "libnr/nr-path.h"
43 #include "xml/repr.h"
44 #include "xml/repr-sorting.h"
46 #include <libnr/nr-matrix-fns.h>
47 #include <libnr/nr-matrix-ops.h>
48 #include <libnr/nr-matrix-translate-ops.h>
49 #include <libnr/nr-scale-matrix-ops.h>
51 #include "livarot/Path.h"
52 #include "livarot/Shape.h"
54 #include "splivarot.h"
56 bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
58 void sp_selected_path_boolop(bool_op bop);
59 void sp_selected_path_do_offset(bool expand, double prefOffset);
60 void sp_selected_path_create_offset_object(int expand, bool updating);
62 void
63 sp_selected_path_union()
64 {
65 sp_selected_path_boolop(bool_op_union);
66 }
68 void
69 sp_selected_path_intersect()
70 {
71 sp_selected_path_boolop(bool_op_inters);
72 }
74 void
75 sp_selected_path_diff()
76 {
77 sp_selected_path_boolop(bool_op_diff);
78 }
80 void
81 sp_selected_path_symdiff()
82 {
83 sp_selected_path_boolop(bool_op_symdiff);
84 }
85 void
86 sp_selected_path_cut()
87 {
88 sp_selected_path_boolop(bool_op_cut);
89 }
90 void
91 sp_selected_path_slice()
92 {
93 sp_selected_path_boolop(bool_op_slice);
94 }
97 // boolean operations
98 // take the source paths from the file, do the operation, delete the originals and add the results
99 void
100 sp_selected_path_boolop(bool_op bop)
101 {
102 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
104 Inkscape::Selection *selection = sp_desktop_selection(desktop);
106 GSList *il = (GSList *) selection->itemList();
108 if (g_slist_length(il) < 2) {
109 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
110 return;
111 }
113 if (g_slist_length(il) > 2) {
114 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
115 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
116 return;
117 }
118 }
120 // reverseOrderForOp marks whether the order of the list is the top->down order
121 // it's only used when there are 2 objects, and for operations who need to know the
122 // topmost object (differences, cuts)
123 bool reverseOrderForOp = false;
125 // mettre les elements de la liste dans l'ordre pour ces operations
126 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
127 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
128 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
129 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
131 if (a == NULL || b == NULL) {
132 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."));
133 return;
134 }
136 if (Ancetre(a, b)) {
137 // a is the parent of b, already in the proper order
138 } else if (Ancetre(b, a)) {
139 // reverse order
140 reverseOrderForOp = true;
141 } else {
143 // objects are not in parent/child relationship;
144 // find their lowest common ancestor
145 Inkscape::XML::Node *dad = LCA(a, b);
146 if (dad == NULL) {
147 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."));
148 return;
149 }
151 // find the children of the LCA that lead from it to the a and b
152 Inkscape::XML::Node *as = AncetreFils(a, dad);
153 Inkscape::XML::Node *bs = AncetreFils(b, dad);
155 // find out which comes first
156 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
157 if (child == as) {
158 /* a first, so reverse. */
159 reverseOrderForOp = true;
160 break;
161 }
162 if (child == bs)
163 break;
164 }
165 }
166 }
168 il = g_slist_copy(il);
170 // first check if all the input objects have shapes
171 // otherwise bail out
172 for (GSList *l = il; l != NULL; l = l->next)
173 {
174 SPItem *item = SP_ITEM(l->data);
175 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
176 {
177 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
178 g_slist_free(il);
179 return;
180 }
181 }
183 // extract the livarot Paths from the source objects
184 // also get the winding rule specified in the style
185 int nbOriginaux = g_slist_length(il);
186 std::vector<Path *> originaux(nbOriginaux);
187 std::vector<FillRule> origWind(nbOriginaux);
188 int curOrig;
189 {
190 curOrig = 0;
191 for (GSList *l = il; l != NULL; l = l->next)
192 {
193 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
194 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
195 if (val && strcmp(val, "nonzero") == 0) {
196 origWind[curOrig]= fill_nonZero;
197 } else if (val && strcmp(val, "evenodd") == 0) {
198 origWind[curOrig]= fill_oddEven;
199 } else {
200 origWind[curOrig]= fill_nonZero;
201 }
203 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
204 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
205 {
206 for (int i = curOrig; i >= 0; i--) delete originaux[i];
207 g_slist_free(il);
208 return;
209 }
210 curOrig++;
211 }
212 }
213 // reverse if needed
214 // note that the selection list keeps its order
215 if ( reverseOrderForOp ) {
216 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
217 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
218 }
220 // and work
221 // some temporary instances, first
222 Shape *theShapeA = new Shape;
223 Shape *theShapeB = new Shape;
224 Shape *theShape = new Shape;
225 Path *res = new Path;
226 res->SetBackData(false);
227 Path::cut_position *toCut=NULL;
228 int nbToCut=0;
230 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
231 // true boolean op
232 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
233 originaux[0]->ConvertWithBackData(0.1);
235 originaux[0]->Fill(theShape, 0);
237 theShapeA->ConvertToShape(theShape, origWind[0]);
239 curOrig = 1;
240 for (GSList *l = il->next; l != NULL; l = l->next) {
241 originaux[curOrig]->ConvertWithBackData(0.1);
243 originaux[curOrig]->Fill(theShape, curOrig);
245 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
247 // les elements arrivent en ordre inverse dans la liste
248 theShape->Booleen(theShapeB, theShapeA, bop);
250 {
251 Shape *swap = theShape;
252 theShape = theShapeA;
253 theShapeA = swap;
254 }
255 curOrig++;
256 }
258 {
259 Shape *swap = theShape;
260 theShape = theShapeA;
261 theShapeA = swap;
262 }
264 } else if ( bop == bool_op_cut ) {
265 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
266 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
267 // it is just uncrossed, and cleaned from duplicate edges and points
268 // then it's fed to Booleen() which will uncross it against the other path
269 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
270 // thus making a polygon. the weight of the edges of the cut are all 0, but
271 // the Booleen need to invert the ones inside the source polygon (for the subsequent
272 // ConvertToForme)
274 // the cut path needs to have the highest pathID in the back data
275 // that's how the Booleen() function knows it's an edge of the cut
277 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
278 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
279 // left it at 1.0. Investigate replacing this by a combination of difference and
280 // intersection of the same two paths. -- bb
281 {
282 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
283 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
284 }
285 originaux[0]->ConvertWithBackData(1.0);
287 originaux[0]->Fill(theShape, 0);
289 theShapeA->ConvertToShape(theShape, origWind[0]);
291 originaux[1]->ConvertWithBackData(1.0);
293 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
295 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
297 // les elements arrivent en ordre inverse dans la liste
298 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
300 } else if ( bop == bool_op_slice ) {
301 // slice is not really a boolean operation
302 // you just put the 2 shapes in a single polygon, uncross it
303 // the points where the degree is > 2 are intersections
304 // just check it's an intersection on the path you want to cut, and keep it
305 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
306 // make new subpath at each one of these positions
307 // inversion pour l'op\8eration
308 {
309 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
310 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
311 }
312 originaux[0]->ConvertWithBackData(1.0);
314 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
316 originaux[1]->ConvertWithBackData(1.0);
318 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
320 theShape->ConvertToShape(theShapeA, fill_justDont);
322 if ( theShape->hasBackData() ) {
323 // should always be the case, but ya never know
324 {
325 for (int i = 0; i < theShape->numberOfPoints(); i++) {
326 if ( theShape->getPoint(i).totalDegree() > 2 ) {
327 // possibly an intersection
328 // we need to check that at least one edge from the source path is incident to it
329 // before we declare it's an intersection
330 int cb = theShape->getPoint(i).incidentEdge[FIRST];
331 int nbOrig=0;
332 int nbOther=0;
333 int piece=-1;
334 float t=0.0;
335 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
336 if ( theShape->ebData[cb].pathID == 0 ) {
337 // the source has an edge incident to the point, get its position on the path
338 piece=theShape->ebData[cb].pieceID;
339 if ( theShape->getEdge(cb).st == i ) {
340 t=theShape->ebData[cb].tSt;
341 } else {
342 t=theShape->ebData[cb].tEn;
343 }
344 nbOrig++;
345 }
346 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
347 cb=theShape->NextAt(i, cb);
348 }
349 if ( nbOrig > 0 && nbOther > 0 ) {
350 // point incident to both path and cut: an intersection
351 // note that you only keep one position on the source; you could have degenerate
352 // cases where the source crosses itself at this point, and you wouyld miss an intersection
353 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
354 toCut[nbToCut].piece=piece;
355 toCut[nbToCut].t=t;
356 nbToCut++;
357 }
358 }
359 }
360 }
361 {
362 // i think it's useless now
363 int i = theShape->numberOfEdges() - 1;
364 for (;i>=0;i--) {
365 if ( theShape->ebData[i].pathID == 1 ) {
366 theShape->SubEdge(i);
367 }
368 }
369 }
371 }
372 }
374 int* nesting=NULL;
375 int* conts=NULL;
376 int nbNest=0;
377 // pour compenser le swap juste avant
378 if ( bop == bool_op_slice ) {
379 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
380 // res->ConvertForcedToMoveTo();
381 res->Copy(originaux[0]);
382 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
383 free(toCut);
384 } else if ( bop == bool_op_cut ) {
385 // il faut appeler pour desallouer PointData (pas vital, mais bon)
386 // the Booleen() function did not deallocated the point_data array in theShape, because this
387 // function needs it.
388 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
389 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
390 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
391 } else {
392 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
393 }
395 delete theShape;
396 delete theShapeA;
397 delete theShapeB;
398 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
400 if (res->descr_cmd.size() <= 1)
401 {
402 // only one command, presumably a moveto: it isn't a path
403 for (GSList *l = il; l != NULL; l = l->next)
404 {
405 SP_OBJECT(l->data)->deleteObject();
406 }
407 sp_document_done(sp_desktop_document(desktop));
408 selection->clear();
410 delete res;
411 g_slist_free(il);
412 return;
413 }
415 // remember important aspects of the source path, to be restored
416 Inkscape::XML::Node *repr_source;
417 if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
418 if (reverseOrderForOp) {
419 repr_source = SP_OBJECT_REPR(il->data);
420 } else {
421 repr_source = SP_OBJECT_REPR(il->next->data);
422 }
423 } else {
424 // find out the bottom object
425 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
427 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
428 repr_source = ((Inkscape::XML::Node *) sorted->data);
429 g_slist_free(sorted);
430 }
431 gint pos = repr_source->position();
432 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
433 char const *id = repr_source->attribute("id");
434 char const *style = repr_source->attribute("style");
437 // remove source paths
438 selection->clear();
439 for (GSList *l = il; l != NULL; l = l->next) {
440 // if this is the bottommost object,
441 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
442 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
443 SP_OBJECT(l->data)->deleteObject(false);
444 } else {
445 // delete the object for real, so that its clones can take appropriate action
446 SP_OBJECT(l->data)->deleteObject();
447 }
448 }
449 g_slist_free(il);
451 // premultiply by the inverse of parent's repr
452 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
453 NR::Matrix local = sp_item_i2doc_affine(parent_item);
454 gchar affinestr[80];
455 gchar *transform = NULL;
456 if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
457 transform = affinestr;
458 }
460 // now that we have the result, add it on the canvas
461 if ( bop == bool_op_cut || bop == bool_op_slice ) {
462 int nbRP=0;
463 Path** resPath;
464 if ( bop == bool_op_slice ) {
465 // there are moveto's at each intersection, but it's still one unique path
466 // so break it down and add each subpath independently
467 // we could call break_apart to do this, but while we have the description...
468 resPath=res->SubPaths(nbRP, false);
469 } else {
470 // cut operation is a bit wicked: you need to keep holes
471 // that's why you needed the nesting
472 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
473 // to get the path for each part of the polygon. that's why you need the nesting info:
474 // to know in wich subpath to add a subpath
475 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
477 // cleaning
478 if ( conts ) free(conts);
479 if ( nesting ) free(nesting);
480 }
482 // add all the pieces resulting from cut or slice
483 for (int i=0;i<nbRP;i++) {
484 gchar *d = resPath[i]->svg_dump_path();
486 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
487 repr->setAttribute("style", style);
488 repr->setAttribute("d", d);
489 g_free(d);
491 // for slice, remove fill
492 if (bop == bool_op_slice) {
493 SPCSSAttr *css;
495 css = sp_repr_css_attr_new();
496 sp_repr_css_set_property(css, "fill", "none");
498 sp_repr_css_change(repr, css, "style");
500 sp_repr_css_attr_unref(css);
501 }
503 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
504 // this means it's basically random which of the pieces inherits the original's id and clones
505 // a better algorithm might figure out e.g. the biggest piece
506 repr->setAttribute("id", id);
508 repr->setAttribute("transform", transform);
510 // add the new repr to the parent
511 parent->appendChild(repr);
513 // move to the saved position
514 repr->setPosition(pos > 0 ? pos : 0);
516 selection->add(repr);
517 Inkscape::GC::release(repr);
519 delete resPath[i];
520 }
521 if ( resPath ) free(resPath);
523 } else {
524 gchar *d = res->svg_dump_path();
526 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
527 repr->setAttribute("style", style);
529 repr->setAttribute("d", d);
530 g_free(d);
532 repr->setAttribute("transform", transform);
534 repr->setAttribute("id", id);
535 parent->appendChild(repr);
536 repr->setPosition(pos > 0 ? pos : 0);
538 selection->add(repr);
539 Inkscape::GC::release(repr);
540 }
542 sp_document_done(sp_desktop_document(desktop));
544 delete res;
545 }
548 void
549 sp_selected_path_outline()
550 {
551 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
553 Inkscape::Selection *selection = sp_desktop_selection(desktop);
555 if (selection->isEmpty()) {
556 // TRANSLATORS: "to outline" means "to convert stroke to path"
557 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to outline."));
558 return;
559 }
561 bool did = false;
563 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
564 items != NULL;
565 items = items->next) {
567 SPItem *item = (SPItem *) items->data;
569 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
570 continue;
572 SPCurve *curve = NULL;
573 if (SP_IS_SHAPE(item)) {
574 curve = sp_shape_get_curve(SP_SHAPE(item));
575 if (curve == NULL)
576 continue;
577 }
578 if (SP_IS_TEXT(item)) {
579 curve = SP_TEXT(item)->getNormalizedBpath();
580 if (curve == NULL)
581 continue;
582 }
584 { // pas de stroke pas de chocolat
585 SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
586 gchar const *val = sp_repr_css_property(css, "stroke", NULL);
588 if (val == NULL || strcmp(val, "none") == 0) {
589 sp_curve_unref(curve);
590 continue;
591 }
592 }
594 // remember old stroke style, to be set on fill
595 SPCSSAttr *ncss;
596 {
597 SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style");
598 gchar const *val = sp_repr_css_property(ocss, "stroke", NULL);
599 gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL);
601 ncss = sp_repr_css_attr_new();
603 sp_repr_css_set_property(ncss, "stroke", "none");
604 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
605 sp_repr_css_set_property(ncss, "fill", val);
606 if ( opac ) {
607 sp_repr_css_set_property(ncss, "fill-opacity", opac);
608 } else {
609 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
610 }
611 sp_repr_css_unset_property(ncss, "marker-start");
612 sp_repr_css_unset_property(ncss, "marker-mid");
613 sp_repr_css_unset_property(ncss, "marker-end");
614 }
616 NR::Matrix const transform(item->transform);
617 float const scale = transform.expansion();
618 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
619 SPStyle *i_style = SP_OBJECT(item)->style;
621 float o_width, o_miter;
622 JoinType o_join;
623 ButtType o_butt;
625 {
626 int jointype, captype;
628 jointype = i_style->stroke_linejoin.computed;
629 captype = i_style->stroke_linecap.computed;
630 o_width = i_style->stroke_width.computed;
632 switch (jointype) {
633 case SP_STROKE_LINEJOIN_MITER:
634 o_join = join_pointy;
635 break;
636 case SP_STROKE_LINEJOIN_ROUND:
637 o_join = join_round;
638 break;
639 default:
640 o_join = join_straight;
641 break;
642 }
644 switch (captype) {
645 case SP_STROKE_LINECAP_SQUARE:
646 o_butt = butt_square;
647 break;
648 case SP_STROKE_LINECAP_ROUND:
649 o_butt = butt_round;
650 break;
651 default:
652 o_butt = butt_straight;
653 break;
654 }
656 if (o_width < 0.1)
657 o_width = 0.1;
658 o_miter = i_style->stroke_miterlimit.value * o_width;
659 }
661 Path *orig = Path_for_item(item, false);
662 if (orig == NULL) {
663 g_free(style);
664 sp_curve_unref(curve);
665 continue;
666 }
668 Path *res = new Path;
669 res->SetBackData(false);
671 if (i_style->stroke_dash.n_dash) {
672 // For dashed strokes, use Stroke method, because Outline can't do dashes
673 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
675 orig->ConvertWithBackData(0.1);
677 orig->DashPolylineFromStyle(i_style, scale, 0);
679 Shape* theShape = new Shape;
680 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
681 0.5 * o_miter);
682 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
684 Shape *theRes = new Shape;
686 theRes->ConvertToShape(theShape, fill_positive);
688 Path *originaux[1];
689 originaux[0] = res;
690 theRes->ConvertToForme(orig, 1, originaux);
692 res->Coalesce(5.0);
694 delete theShape;
695 delete theRes;
697 } else {
699 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
701 orig->Coalesce(0.5 * o_width);
703 Shape *theShape = new Shape;
704 Shape *theRes = new Shape;
706 res->ConvertWithBackData(1.0);
707 res->Fill(theShape, 0);
708 theRes->ConvertToShape(theShape, fill_positive);
710 Path *originaux[1];
711 originaux[0] = res;
712 theRes->ConvertToForme(orig, 1, originaux);
714 delete theShape;
715 delete theRes;
716 }
718 if (orig->descr_cmd.size() <= 1) {
719 // ca a merd\8e, ou bien le resultat est vide
720 delete res;
721 delete orig;
722 g_free(style);
723 continue;
724 }
726 did = true;
728 // remember the position of the item
729 gint pos = SP_OBJECT_REPR(item)->position();
730 // remember parent
731 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
732 // remember id
733 char const *id = SP_OBJECT_REPR(item)->attribute("id");
735 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
737 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
739 // restore old style
740 repr->setAttribute("style", style);
742 // set old stroke style on fill
743 sp_repr_css_change(repr, ncss, "style");
745 sp_repr_css_attr_unref(ncss);
747 gchar *str = orig->svg_dump_path();
748 repr->setAttribute("d", str);
749 g_free(str);
752 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
754 Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
756 // add the group to the parent
757 parent->appendChild(g_repr);
758 // move to the saved position
759 g_repr->setPosition(pos > 0 ? pos : 0);
761 g_repr->appendChild(repr);
762 repr->setAttribute("id", id);
763 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
764 sp_item_write_transform(newitem, repr, transform);
766 SPShape *shape = SP_SHAPE(item);
768 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
769 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
770 if (sp_shape_marker_required (shape, m, bp)) {
772 SPMarker* marker = SP_MARKER (shape->marker[m]);
773 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
775 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
777 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
778 tr = NR::scale(i_style->stroke_width.computed) * tr;
779 }
781 // total marker transform
782 tr = marker_item->transform * marker->c2p * tr * transform;
784 if (SP_OBJECT_REPR(marker_item)) {
785 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
786 g_repr->appendChild(m_repr);
787 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
788 sp_item_write_transform(marker_item, m_repr, tr);
789 }
790 }
791 }
792 }
795 selection->add(g_repr);
797 Inkscape::GC::release(g_repr);
800 } else {
802 // add the new repr to the parent
803 parent->appendChild(repr);
805 // move to the saved position
806 repr->setPosition(pos > 0 ? pos : 0);
808 repr->setAttribute("id", id);
810 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
811 sp_item_write_transform(newitem, repr, transform);
813 selection->add(repr);
815 }
817 Inkscape::GC::release(repr);
819 sp_curve_unref(curve);
820 selection->remove(item);
821 SP_OBJECT(item)->deleteObject(false);
823 }
825 delete res;
826 delete orig;
827 g_free(style);
829 }
831 if (did) {
832 sp_document_done(sp_desktop_document(desktop));
833 } else {
834 // TRANSLATORS: "to outline" means "to convert stroke to path"
835 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> to outline in the selection."));
836 return;
837 }
838 }
841 void
842 sp_selected_path_offset()
843 {
844 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
846 sp_selected_path_do_offset(true, prefOffset);
847 }
848 void
849 sp_selected_path_inset()
850 {
851 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
853 sp_selected_path_do_offset(false, prefOffset);
854 }
856 void
857 sp_selected_path_offset_screen(double pixels)
858 {
859 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
860 }
862 void
863 sp_selected_path_inset_screen(double pixels)
864 {
865 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
866 }
869 void sp_selected_path_create_offset_object_zero()
870 {
871 sp_selected_path_create_offset_object(0, false);
872 }
874 void sp_selected_path_create_offset()
875 {
876 sp_selected_path_create_offset_object(1, false);
877 }
878 void sp_selected_path_create_inset()
879 {
880 sp_selected_path_create_offset_object(-1, false);
881 }
883 void sp_selected_path_create_updating_offset_object_zero()
884 {
885 sp_selected_path_create_offset_object(0, true);
886 }
888 void sp_selected_path_create_updating_offset()
889 {
890 sp_selected_path_create_offset_object(1, true);
891 }
892 void sp_selected_path_create_updating_inset()
893 {
894 sp_selected_path_create_offset_object(-1, true);
895 }
897 void
898 sp_selected_path_create_offset_object(int expand, bool updating)
899 {
900 Inkscape::Selection *selection;
901 Inkscape::XML::Node *repr;
902 SPItem *item;
903 SPCurve *curve;
904 gchar *style, *str;
905 SPDesktop *desktop;
906 float o_width, o_miter;
907 JoinType o_join;
908 ButtType o_butt;
910 curve = NULL;
912 desktop = SP_ACTIVE_DESKTOP;
914 selection = sp_desktop_selection(desktop);
916 item = selection->singleItem();
918 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
919 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
920 return;
921 }
922 if (SP_IS_SHAPE(item))
923 {
924 curve = sp_shape_get_curve(SP_SHAPE(item));
925 if (curve == NULL)
926 return;
927 }
928 if (SP_IS_TEXT(item))
929 {
930 curve = SP_TEXT(item)->getNormalizedBpath();
931 if (curve == NULL)
932 return;
933 }
935 NR::Matrix const transform(item->transform);
937 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
939 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
941 // remember the position of the item
942 gint pos = SP_OBJECT_REPR(item)->position();
943 // remember parent
944 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
946 {
947 SPStyle *i_style = SP_OBJECT(item)->style;
948 int jointype, captype;
950 jointype = i_style->stroke_linejoin.value;
951 captype = i_style->stroke_linecap.value;
952 o_width = i_style->stroke_width.computed;
953 if (jointype == SP_STROKE_LINEJOIN_MITER)
954 {
955 o_join = join_pointy;
956 }
957 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
958 {
959 o_join = join_round;
960 }
961 else
962 {
963 o_join = join_straight;
964 }
965 if (captype == SP_STROKE_LINECAP_SQUARE)
966 {
967 o_butt = butt_square;
968 }
969 else if (captype == SP_STROKE_LINECAP_ROUND)
970 {
971 o_butt = butt_round;
972 }
973 else
974 {
975 o_butt = butt_straight;
976 }
978 {
979 double prefOffset = 1.0;
980 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
981 o_width = prefOffset;
982 }
984 if (o_width < 0.01)
985 o_width = 0.01;
986 o_miter = i_style->stroke_miterlimit.value * o_width;
987 }
989 Path *orig = Path_for_item(item, true, false);
990 if (orig == NULL)
991 {
992 g_free(style);
993 sp_curve_unref(curve);
994 return;
995 }
997 Path *res = new Path;
998 res->SetBackData(false);
1000 {
1001 Shape *theShape = new Shape;
1002 Shape *theRes = new Shape;
1004 orig->ConvertWithBackData(1.0);
1005 orig->Fill(theShape, 0);
1007 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1008 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1009 if (val && strcmp(val, "nonzero") == 0)
1010 {
1011 theRes->ConvertToShape(theShape, fill_nonZero);
1012 }
1013 else if (val && strcmp(val, "evenodd") == 0)
1014 {
1015 theRes->ConvertToShape(theShape, fill_oddEven);
1016 }
1017 else
1018 {
1019 theRes->ConvertToShape(theShape, fill_nonZero);
1020 }
1022 Path *originaux[1];
1023 originaux[0] = orig;
1024 theRes->ConvertToForme(res, 1, originaux);
1026 delete theShape;
1027 delete theRes;
1028 }
1030 sp_curve_unref(curve);
1032 if (res->descr_cmd.size() <= 1)
1033 {
1034 // pas vraiment de points sur le resultat
1035 // donc il ne reste rien
1036 sp_document_done(sp_desktop_document(desktop));
1037 selection->clear();
1039 delete res;
1040 delete orig;
1041 g_free(style);
1042 return;
1043 }
1045 {
1046 gchar tstr[80];
1048 tstr[79] = '\0';
1050 repr = sp_repr_new("svg:path");
1051 repr->setAttribute("sodipodi:type", "inkscape:offset");
1052 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1053 ? o_width
1054 : expand < 0
1055 ? -o_width
1056 : 0 ));
1058 str = res->svg_dump_path();
1059 repr->setAttribute("inkscape:original", str);
1060 g_free(str);
1062 if ( updating ) {
1063 char const *id = SP_OBJECT(item)->repr->attribute("id");
1064 char const *uri = g_strdup_printf("#%s", id);
1065 repr->setAttribute("xlink:href", uri);
1066 g_free((void *) uri);
1067 } else {
1068 repr->setAttribute("inkscape:href", NULL);
1069 }
1071 repr->setAttribute("style", style);
1073 // add the new repr to the parent
1074 parent->appendChild(repr);
1076 // move to the saved position
1077 repr->setPosition(pos > 0 ? pos : 0);
1079 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1081 if ( updating ) {
1082 // on conserve l'original
1083 // we reapply the transform to the original (offset will feel it)
1084 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1085 } else {
1086 // delete original, apply the transform to the offset
1087 SP_OBJECT(item)->deleteObject(false);
1088 sp_item_write_transform(nitem, repr, transform);
1089 }
1091 // The object just created from a temporary repr is only a seed.
1092 // We need to invoke its write which will update its real repr (in particular adding d=)
1093 SP_OBJECT(nitem)->updateRepr();
1095 Inkscape::GC::release(repr);
1097 selection->set(nitem);
1098 }
1100 sp_document_done(sp_desktop_document(desktop));
1102 delete res;
1103 delete orig;
1105 g_free(style);
1106 }
1119 void
1120 sp_selected_path_do_offset(bool expand, double prefOffset)
1121 {
1122 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1124 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1126 if (selection->isEmpty()) {
1127 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1128 return;
1129 }
1131 bool did = false;
1133 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1134 items != NULL;
1135 items = items->next) {
1137 SPItem *item = (SPItem *) items->data;
1139 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1140 continue;
1142 SPCurve *curve = NULL;
1143 if (SP_IS_SHAPE(item)) {
1144 curve = sp_shape_get_curve(SP_SHAPE(item));
1145 if (curve == NULL)
1146 continue;
1147 }
1148 if (SP_IS_TEXT(item)) {
1149 curve = SP_TEXT(item)->getNormalizedBpath();
1150 if (curve == NULL)
1151 continue;
1152 }
1154 NR::Matrix const transform(item->transform);
1156 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1158 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1160 float o_width, o_miter;
1161 JoinType o_join;
1162 ButtType o_butt;
1164 {
1165 SPStyle *i_style = SP_OBJECT(item)->style;
1166 int jointype, captype;
1168 jointype = i_style->stroke_linejoin.value;
1169 captype = i_style->stroke_linecap.value;
1170 o_width = i_style->stroke_width.computed;
1172 switch (jointype) {
1173 case SP_STROKE_LINEJOIN_MITER:
1174 o_join = join_pointy;
1175 break;
1176 case SP_STROKE_LINEJOIN_ROUND:
1177 o_join = join_round;
1178 break;
1179 default:
1180 o_join = join_straight;
1181 break;
1182 }
1184 switch (captype) {
1185 case SP_STROKE_LINECAP_SQUARE:
1186 o_butt = butt_square;
1187 break;
1188 case SP_STROKE_LINECAP_ROUND:
1189 o_butt = butt_round;
1190 break;
1191 default:
1192 o_butt = butt_straight;
1193 break;
1194 }
1196 o_width = prefOffset;
1198 if (o_width < 0.1)
1199 o_width = 0.1;
1200 o_miter = i_style->stroke_miterlimit.value * o_width;
1201 }
1203 Path *orig = Path_for_item(item, false);
1204 if (orig == NULL) {
1205 g_free(style);
1206 sp_curve_unref(curve);
1207 continue;
1208 }
1210 Path *res = new Path;
1211 res->SetBackData(false);
1213 {
1214 Shape *theShape = new Shape;
1215 Shape *theRes = new Shape;
1217 orig->ConvertWithBackData(0.03);
1218 orig->Fill(theShape, 0);
1220 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1221 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1222 if (val && strcmp(val, "nonzero") == 0)
1223 {
1224 theRes->ConvertToShape(theShape, fill_nonZero);
1225 }
1226 else if (val && strcmp(val, "evenodd") == 0)
1227 {
1228 theRes->ConvertToShape(theShape, fill_oddEven);
1229 }
1230 else
1231 {
1232 theRes->ConvertToShape(theShape, fill_nonZero);
1233 }
1235 // et maintenant: offset
1236 // methode inexacte
1237 /* Path *originaux[1];
1238 originaux[0] = orig;
1239 theRes->ConvertToForme(res, 1, originaux);
1241 if (expand) {
1242 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1243 } else {
1244 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1245 }
1247 orig->ConvertWithBackData(1.0);
1248 orig->Fill(theShape, 0);
1249 theRes->ConvertToShape(theShape, fill_positive);
1250 originaux[0] = orig;
1251 theRes->ConvertToForme(res, 1, originaux);
1253 if (o_width >= 0.5) {
1254 // res->Coalesce(1.0);
1255 res->ConvertEvenLines(1.0);
1256 res->Simplify(1.0);
1257 } else {
1258 // res->Coalesce(o_width);
1259 res->ConvertEvenLines(1.0*o_width);
1260 res->Simplify(1.0 * o_width);
1261 } */
1262 // methode par makeoffset
1264 if (expand)
1265 {
1266 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1267 }
1268 else
1269 {
1270 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1271 }
1272 theRes->ConvertToShape(theShape, fill_positive);
1274 res->Reset();
1275 theRes->ConvertToForme(res);
1277 if (o_width >= 1.0)
1278 {
1279 res->ConvertEvenLines(1.0);
1280 res->Simplify(1.0);
1281 }
1282 else
1283 {
1284 res->ConvertEvenLines(1.0*o_width);
1285 res->Simplify(1.0 * o_width);
1286 }
1288 delete theShape;
1289 delete theRes;
1290 }
1292 did = true;
1294 sp_curve_unref(curve);
1295 // remember the position of the item
1296 gint pos = SP_OBJECT_REPR(item)->position();
1297 // remember parent
1298 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1299 // remember id
1300 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1302 selection->remove(item);
1303 SP_OBJECT(item)->deleteObject(false);
1305 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1307 gchar tstr[80];
1309 tstr[79] = '\0';
1311 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1313 repr->setAttribute("style", style);
1315 gchar *str = res->svg_dump_path();
1316 repr->setAttribute("d", str);
1317 g_free(str);
1319 // add the new repr to the parent
1320 parent->appendChild(repr);
1322 // move to the saved position
1323 repr->setPosition(pos > 0 ? pos : 0);
1325 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1327 // reapply the transform
1328 sp_item_write_transform(newitem, repr, transform);
1330 repr->setAttribute("id", id);
1332 selection->add(repr);
1334 Inkscape::GC::release(repr);
1335 }
1337 delete orig;
1338 delete res;
1339 }
1341 if (did) {
1342 sp_document_done(sp_desktop_document(desktop));
1343 } else {
1344 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1345 return;
1346 }
1347 }
1351 //return true if we changed something, else false
1352 bool
1353 sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item,
1354 float threshold, bool justCoalesce,
1355 float angleLimit, bool breakableAngles,
1356 gdouble size, bool modifySelection)
1357 {
1358 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1359 return false;
1361 //If this is a group, do the children instead
1362 if (SP_IS_GROUP(item)) {
1364 bool didSomething = false;
1366 for ( GSList *children = sp_item_group_item_list(SP_GROUP(item));
1367 children ; children = children->next) {
1369 SPItem *child = (SPItem *) children->data;
1370 didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce,
1371 angleLimit, breakableAngles, size, false);
1372 }
1374 return didSomething;
1375 }
1378 SPCurve *curve = NULL;
1380 if (SP_IS_SHAPE(item)) {
1381 curve = sp_shape_get_curve(SP_SHAPE(item));
1382 if (!curve)
1383 return false;
1384 }
1386 if (SP_IS_TEXT(item)) {
1387 curve = SP_TEXT(item)->getNormalizedBpath();
1388 if (!curve)
1389 return false;
1390 }
1392 // save the transform, to re-apply it after simplification
1393 NR::Matrix const transform(item->transform);
1395 /*
1396 reset the transform, effectively transforming the item by transform.inverse();
1397 this is necessary so that the item is transformed twice back and forth,
1398 allowing all compensations to cancel out regardless of the preferences
1399 */
1400 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1402 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1404 Path *orig = Path_for_item(item, false);
1405 if (orig == NULL) {
1406 g_free(style);
1407 sp_curve_unref(curve);
1408 return false;
1409 }
1411 sp_curve_unref(curve);
1412 // remember the position of the item
1413 gint pos = SP_OBJECT_REPR(item)->position();
1414 // remember parent
1415 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1416 // remember id
1417 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1419 //If a group was selected, to not change the selection list
1420 if (modifySelection)
1421 selection->remove(item);
1423 SP_OBJECT(item)->deleteObject(false);
1425 if ( justCoalesce ) {
1426 orig->Coalesce(threshold * size);
1427 } else {
1428 orig->ConvertEvenLines(threshold * size);
1429 orig->Simplify(threshold * size);
1430 }
1432 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
1434 repr->setAttribute("style", style);
1436 gchar *str = orig->svg_dump_path();
1437 repr->setAttribute("d", str);
1438 g_free(str);
1440 // restore id
1441 repr->setAttribute("id", id);
1443 // add the new repr to the parent
1444 parent->appendChild(repr);
1446 // move to the saved position
1447 repr->setPosition(pos > 0 ? pos : 0);
1449 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1451 // reapply the transform
1452 sp_item_write_transform(newitem, repr, transform);
1454 //If we are not in a selected group
1455 if (modifySelection)
1456 selection->add(repr);
1458 Inkscape::GC::release(repr);
1460 // clean up
1461 if (orig) delete orig;
1463 return true;
1464 }
1467 void
1468 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1469 float angleLimit, bool breakableAngles)
1470 {
1471 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1473 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1475 if (selection->isEmpty()) {
1476 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1477 _("Select <b>path(s)</b> to simplify."));
1478 return;
1479 }
1481 // remember selection size
1482 NR::Rect bbox = selection->bounds();
1483 gdouble size = L2(bbox.dimensions());
1485 bool didSomething = false;
1487 //Loop through all of the items in the selection
1488 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1489 items != NULL; items = items->next) {
1491 SPItem *item = (SPItem *) items->data;
1493 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1494 continue;
1496 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1497 threshold, justCoalesce, angleLimit, breakableAngles, size, true);
1498 }
1501 if (didSomething)
1502 sp_document_done(sp_desktop_document(desktop));
1503 else
1504 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1506 }
1509 // globals for keeping track of accelerated simplify
1510 static double previousTime = 0.0;
1511 static gdouble simplifyMultiply = 1.0;
1513 void
1514 sp_selected_path_simplify(void)
1515 {
1516 gdouble simplifyThreshold =
1517 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1518 bool simplifyJustCoalesce =
1519 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1521 //Get the current time
1522 GTimeVal currentTimeVal;
1523 g_get_current_time(¤tTimeVal);
1524 double currentTime = currentTimeVal.tv_sec * 1000000 +
1525 currentTimeVal.tv_usec;
1527 //Was the previous call to this function recent? (<0.5 sec)
1528 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1530 // add to the threshold 1/2 of its original value
1531 simplifyMultiply += 0.5;
1532 simplifyThreshold *= simplifyMultiply;
1534 } else {
1535 // reset to the default
1536 simplifyMultiply = 1;
1537 }
1539 //remember time for next call
1540 previousTime = currentTime;
1542 //g_print("%g\n", simplify_threshold);
1544 //Make the actual call
1545 sp_selected_path_simplify_selection(simplifyThreshold,
1546 simplifyJustCoalesce, 0.0, false);
1547 }
1551 // fonctions utilitaires
1553 bool
1554 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1555 {
1556 if (who == NULL || a == NULL)
1557 return false;
1558 if (who == a)
1559 return true;
1560 return Ancetre(sp_repr_parent(a), who);
1561 }
1563 Path *
1564 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1565 {
1566 SPCurve *curve;
1568 if (!item)
1569 return NULL;
1571 if (SP_IS_SHAPE(item))
1572 {
1573 curve = sp_shape_get_curve(SP_SHAPE(item));
1574 }
1575 else if (SP_IS_TEXT(item))
1576 {
1577 curve = SP_TEXT(item)->getNormalizedBpath();
1578 }
1579 else
1580 {
1581 curve = NULL;
1582 }
1584 if (!curve)
1585 return NULL;
1586 NArtBpath *bpath = SP_CURVE_BPATH(curve);
1587 if (bpath == NULL)
1588 return NULL;
1590 if ( doTransformation ) {
1591 if (transformFull)
1592 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item));
1593 else
1594 bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform);
1595 sp_curve_unref(curve);
1596 curve=NULL;
1597 } else {
1598 bpath=SP_CURVE_BPATH(curve);
1599 }
1601 Path *dest = new Path;
1602 dest->SetBackData(false);
1603 {
1604 int i;
1605 bool closed = false;
1606 float lastX = 0.0;
1607 float lastY = 0.0;
1609 for (i = 0; bpath[i].code != NR_END; i++) {
1610 switch (bpath[i].code) {
1611 case NR_LINETO:
1612 lastX = bpath[i].x3;
1613 lastY = bpath[i].y3;
1614 {
1615 NR::Point tmp(lastX, lastY);
1616 dest->LineTo(tmp);
1617 }
1618 break;
1620 case NR_CURVETO:
1621 {
1622 NR::Point tmp, tms, tme;
1623 tmp[0]=bpath[i].x3;
1624 tmp[1]=bpath[i].y3;
1625 tms[0]=3 * (bpath[i].x1 - lastX);
1626 tms[1]=3 * (bpath[i].y1 - lastY);
1627 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1628 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1629 dest->CubicTo(tmp,tms,tme);
1630 }
1631 lastX = bpath[i].x3;
1632 lastY = bpath[i].y3;
1633 break;
1635 case NR_MOVETO_OPEN:
1636 case NR_MOVETO:
1637 if (closed)
1638 dest->Close();
1639 closed = (bpath[i].code == NR_MOVETO);
1640 lastX = bpath[i].x3;
1641 lastY = bpath[i].y3;
1642 {
1643 NR::Point tmp(lastX, lastY);
1644 dest->MoveTo(tmp);
1645 }
1646 break;
1647 default:
1648 break;
1649 }
1650 }
1651 if (closed)
1652 dest->Close();
1653 }
1655 if ( doTransformation ) {
1656 if ( bpath ) nr_free(bpath);
1657 } else {
1658 sp_curve_unref(curve);
1659 }
1660 return dest;
1661 }
1663 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p)
1664 {
1665 //get nearest position on path
1666 Path::cut_position pos = path->PointToCurvilignPosition(p);
1667 return pos;
1668 }
1670 NR::Point get_point_on_Path(Path *path, int piece, double t)
1671 {
1672 NR::Point p;
1673 path->PointAt(piece, t, p);
1674 return p;
1675 }
1678 /*
1679 Local Variables:
1680 mode:c++
1681 c-file-style:"stroustrup"
1682 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1683 indent-tabs-mode:nil
1684 fill-column:99
1685 End:
1686 */
1687 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :