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 <cstring>
21 #include <string>
22 #include <vector>
23 #include <glib/gmem.h>
24 #include "xml/repr.h"
25 #include "svg/svg.h"
26 #include "sp-path.h"
27 #include "sp-shape.h"
28 #include "sp-image.h"
29 #include "marker.h"
30 #include "enums.h"
31 #include "sp-text.h"
32 #include "sp-flowtext.h"
33 #include "text-editing.h"
34 #include "sp-item-group.h"
35 #include "style.h"
36 #include "document.h"
37 #include "message-stack.h"
38 #include "selection.h"
39 #include "desktop-handles.h"
40 #include "desktop.h"
41 #include "display/canvas-bpath.h"
42 #include "display/curve.h"
43 #include <glibmm/i18n.h>
44 #include "preferences.h"
46 #include "xml/repr.h"
47 #include "xml/repr-sorting.h"
48 #include <2geom/pathvector.h>
49 #include <libnr/nr-scale-matrix-ops.h>
50 #include "helper/geom.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(SPDesktop *desktop, bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
60 void sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset);
61 void sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating);
63 void
64 sp_selected_path_union(SPDesktop *desktop)
65 {
66 sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
67 }
69 void
70 sp_selected_path_union_skip_undo(SPDesktop *desktop)
71 {
72 sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_NONE, _("Union"));
73 }
75 void
76 sp_selected_path_intersect(SPDesktop *desktop)
77 {
78 sp_selected_path_boolop(desktop, bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
79 }
81 void
82 sp_selected_path_diff(SPDesktop *desktop)
83 {
84 sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
85 }
87 void
88 sp_selected_path_diff_skip_undo(SPDesktop *desktop)
89 {
90 sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_NONE, _("Difference"));
91 }
93 void
94 sp_selected_path_symdiff(SPDesktop *desktop)
95 {
96 sp_selected_path_boolop(desktop, bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
97 }
98 void
99 sp_selected_path_cut(SPDesktop *desktop)
100 {
101 sp_selected_path_boolop(desktop, bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
102 }
103 void
104 sp_selected_path_slice(SPDesktop *desktop)
105 {
106 sp_selected_path_boolop(desktop, bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path"));
107 }
110 // boolean operations
111 // take the source paths from the file, do the operation, delete the originals and add the results
112 void
113 sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb, const Glib::ustring description)
114 {
115 Inkscape::Selection *selection = sp_desktop_selection(desktop);
117 GSList *il = (GSList *) selection->itemList();
119 // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
120 if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
121 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
122 return;
123 }
124 else if ( g_slist_length(il) < 1 ) {
125 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
126 return;
127 }
129 if (g_slist_length(il) > 2) {
130 if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
131 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, division, or path cut."));
132 return;
133 }
134 }
136 // reverseOrderForOp marks whether the order of the list is the top->down order
137 // it's only used when there are 2 objects, and for operations who need to know the
138 // topmost object (differences, cuts)
139 bool reverseOrderForOp = false;
141 if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice) {
142 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
143 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
144 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
146 if (a == NULL || b == 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 if (Ancetre(a, b)) {
152 // a is the parent of b, already in the proper order
153 } else if (Ancetre(b, a)) {
154 // reverse order
155 reverseOrderForOp = true;
156 } else {
158 // objects are not in parent/child relationship;
159 // find their lowest common ancestor
160 Inkscape::XML::Node *dad = LCA(a, b);
161 if (dad == NULL) {
162 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."));
163 return;
164 }
166 // find the children of the LCA that lead from it to the a and b
167 Inkscape::XML::Node *as = AncetreFils(a, dad);
168 Inkscape::XML::Node *bs = AncetreFils(b, dad);
170 // find out which comes first
171 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
172 if (child == as) {
173 /* a first, so reverse. */
174 reverseOrderForOp = true;
175 break;
176 }
177 if (child == bs)
178 break;
179 }
180 }
181 }
183 il = g_slist_copy(il);
185 // first check if all the input objects have shapes
186 // otherwise bail out
187 for (GSList *l = il; l != NULL; l = l->next)
188 {
189 SPItem *item = SP_ITEM(l->data);
190 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item))
191 {
192 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
193 g_slist_free(il);
194 return;
195 }
196 }
198 // extract the livarot Paths from the source objects
199 // also get the winding rule specified in the style
200 int nbOriginaux = g_slist_length(il);
201 std::vector<Path *> originaux(nbOriginaux);
202 std::vector<FillRule> origWind(nbOriginaux);
203 int curOrig;
204 {
205 curOrig = 0;
206 for (GSList *l = il; l != NULL; l = l->next)
207 {
208 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
209 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
210 if (val && strcmp(val, "nonzero") == 0) {
211 origWind[curOrig]= fill_nonZero;
212 } else if (val && strcmp(val, "evenodd") == 0) {
213 origWind[curOrig]= fill_oddEven;
214 } else {
215 origWind[curOrig]= fill_nonZero;
216 }
218 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
219 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
220 {
221 for (int i = curOrig; i >= 0; i--) delete originaux[i];
222 g_slist_free(il);
223 return;
224 }
225 curOrig++;
226 }
227 }
228 // reverse if needed
229 // note that the selection list keeps its order
230 if ( reverseOrderForOp ) {
231 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
232 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
233 }
235 // and work
236 // some temporary instances, first
237 Shape *theShapeA = new Shape;
238 Shape *theShapeB = new Shape;
239 Shape *theShape = new Shape;
240 Path *res = new Path;
241 res->SetBackData(false);
242 Path::cut_position *toCut=NULL;
243 int nbToCut=0;
245 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
246 // true boolean op
247 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
248 originaux[0]->ConvertWithBackData(0.1);
250 originaux[0]->Fill(theShape, 0);
252 theShapeA->ConvertToShape(theShape, origWind[0]);
254 curOrig = 1;
255 for (GSList *l = il->next; l != NULL; l = l->next) {
256 originaux[curOrig]->ConvertWithBackData(0.1);
258 originaux[curOrig]->Fill(theShape, curOrig);
260 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
262 // les elements arrivent en ordre inverse dans la liste
263 theShape->Booleen(theShapeB, theShapeA, bop);
265 {
266 Shape *swap = theShape;
267 theShape = theShapeA;
268 theShapeA = swap;
269 }
270 curOrig++;
271 }
273 {
274 Shape *swap = theShape;
275 theShape = theShapeA;
276 theShapeA = swap;
277 }
279 } else if ( bop == bool_op_cut ) {
280 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
281 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
282 // it is just uncrossed, and cleaned from duplicate edges and points
283 // then it's fed to Booleen() which will uncross it against the other path
284 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
285 // thus making a polygon. the weight of the edges of the cut are all 0, but
286 // the Booleen need to invert the ones inside the source polygon (for the subsequent
287 // ConvertToForme)
289 // the cut path needs to have the highest pathID in the back data
290 // that's how the Booleen() function knows it's an edge of the cut
292 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
293 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
294 // left it at 1.0. Investigate replacing this by a combination of difference and
295 // intersection of the same two paths. -- bb
296 {
297 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
298 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
299 }
300 originaux[0]->ConvertWithBackData(1.0);
302 originaux[0]->Fill(theShape, 0);
304 theShapeA->ConvertToShape(theShape, origWind[0]);
306 originaux[1]->ConvertWithBackData(1.0);
308 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
310 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
312 // les elements arrivent en ordre inverse dans la liste
313 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
315 } else if ( bop == bool_op_slice ) {
316 // slice is not really a boolean operation
317 // you just put the 2 shapes in a single polygon, uncross it
318 // the points where the degree is > 2 are intersections
319 // just check it's an intersection on the path you want to cut, and keep it
320 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
321 // make new subpath at each one of these positions
322 // inversion pour l'op\8eration
323 {
324 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
325 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
326 }
327 originaux[0]->ConvertWithBackData(1.0);
329 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
331 originaux[1]->ConvertWithBackData(1.0);
333 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
335 theShape->ConvertToShape(theShapeA, fill_justDont);
337 if ( theShape->hasBackData() ) {
338 // should always be the case, but ya never know
339 {
340 for (int i = 0; i < theShape->numberOfPoints(); i++) {
341 if ( theShape->getPoint(i).totalDegree() > 2 ) {
342 // possibly an intersection
343 // we need to check that at least one edge from the source path is incident to it
344 // before we declare it's an intersection
345 int cb = theShape->getPoint(i).incidentEdge[FIRST];
346 int nbOrig=0;
347 int nbOther=0;
348 int piece=-1;
349 float t=0.0;
350 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
351 if ( theShape->ebData[cb].pathID == 0 ) {
352 // the source has an edge incident to the point, get its position on the path
353 piece=theShape->ebData[cb].pieceID;
354 if ( theShape->getEdge(cb).st == i ) {
355 t=theShape->ebData[cb].tSt;
356 } else {
357 t=theShape->ebData[cb].tEn;
358 }
359 nbOrig++;
360 }
361 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
362 cb=theShape->NextAt(i, cb);
363 }
364 if ( nbOrig > 0 && nbOther > 0 ) {
365 // point incident to both path and cut: an intersection
366 // note that you only keep one position on the source; you could have degenerate
367 // cases where the source crosses itself at this point, and you wouyld miss an intersection
368 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
369 toCut[nbToCut].piece=piece;
370 toCut[nbToCut].t=t;
371 nbToCut++;
372 }
373 }
374 }
375 }
376 {
377 // i think it's useless now
378 int i = theShape->numberOfEdges() - 1;
379 for (;i>=0;i--) {
380 if ( theShape->ebData[i].pathID == 1 ) {
381 theShape->SubEdge(i);
382 }
383 }
384 }
386 }
387 }
389 int* nesting=NULL;
390 int* conts=NULL;
391 int nbNest=0;
392 // pour compenser le swap juste avant
393 if ( bop == bool_op_slice ) {
394 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
395 // res->ConvertForcedToMoveTo();
396 res->Copy(originaux[0]);
397 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
398 free(toCut);
399 } else if ( bop == bool_op_cut ) {
400 // il faut appeler pour desallouer PointData (pas vital, mais bon)
401 // the Booleen() function did not deallocated the point_data array in theShape, because this
402 // function needs it.
403 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
404 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
405 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
406 } else {
407 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
408 }
410 delete theShape;
411 delete theShapeA;
412 delete theShapeB;
413 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
415 if (res->descr_cmd.size() <= 1)
416 {
417 // only one command, presumably a moveto: it isn't a path
418 for (GSList *l = il; l != NULL; l = l->next)
419 {
420 SP_OBJECT(l->data)->deleteObject();
421 }
422 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
423 description);
424 selection->clear();
426 delete res;
427 g_slist_free(il);
428 return;
429 }
431 // get the source path object
432 SPObject *source;
433 if ( bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
434 if (reverseOrderForOp) {
435 source = SP_OBJECT(il->data);
436 } else {
437 source = SP_OBJECT(il->next->data);
438 }
439 } else {
440 // find out the bottom object
441 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
443 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
445 source = sp_desktop_document(desktop)->
446 getObjectByRepr((Inkscape::XML::Node *)sorted->data);
448 g_slist_free(sorted);
449 }
451 // adjust style properties that depend on a possible transform in the source object in order
452 // to get a correct style attribute for the new path
453 SPItem* item_source = SP_ITEM(source);
454 Geom::Matrix i2doc(sp_item_i2doc_affine(item_source));
455 sp_item_adjust_stroke(item_source, i2doc.descrim());
456 sp_item_adjust_pattern(item_source, i2doc);
457 sp_item_adjust_gradient(item_source, i2doc);
458 sp_item_adjust_livepatheffect(item_source, i2doc);
460 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
462 // remember important aspects of the source path, to be restored
463 gint pos = repr_source->position();
464 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
465 gchar const *id = repr_source->attribute("id");
466 gchar const *style = repr_source->attribute("style");
467 gchar const *mask = repr_source->attribute("mask");
468 gchar const *clip_path = repr_source->attribute("clip-path");
469 gchar *title = source->title();
470 gchar *desc = source->desc();
471 // remove source paths
472 selection->clear();
473 for (GSList *l = il; l != NULL; l = l->next) {
474 // if this is the bottommost object,
475 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
476 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
477 SP_OBJECT(l->data)->deleteObject(false);
478 } else {
479 // delete the object for real, so that its clones can take appropriate action
480 SP_OBJECT(l->data)->deleteObject();
481 }
482 }
483 g_slist_free(il);
485 // premultiply by the inverse of parent's repr
486 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
487 Geom::Matrix local (sp_item_i2doc_affine(parent_item));
488 gchar *transform = sp_svg_transform_write(local.inverse());
490 // now that we have the result, add it on the canvas
491 if ( bop == bool_op_cut || bop == bool_op_slice ) {
492 int nbRP=0;
493 Path** resPath;
494 if ( bop == bool_op_slice ) {
495 // there are moveto's at each intersection, but it's still one unique path
496 // so break it down and add each subpath independently
497 // we could call break_apart to do this, but while we have the description...
498 resPath=res->SubPaths(nbRP, false);
499 } else {
500 // cut operation is a bit wicked: you need to keep holes
501 // that's why you needed the nesting
502 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
503 // to get the path for each part of the polygon. that's why you need the nesting info:
504 // to know in wich subpath to add a subpath
505 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
507 // cleaning
508 if ( conts ) free(conts);
509 if ( nesting ) free(nesting);
510 }
512 // add all the pieces resulting from cut or slice
513 for (int i=0;i<nbRP;i++) {
514 gchar *d = resPath[i]->svg_dump_path();
516 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
517 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
518 repr->setAttribute("style", style);
519 if (mask)
520 repr->setAttribute("mask", mask);
521 if (clip_path)
522 repr->setAttribute("clip-path", clip_path);
524 repr->setAttribute("d", d);
525 g_free(d);
527 // for slice, remove fill
528 if (bop == bool_op_slice) {
529 SPCSSAttr *css;
531 css = sp_repr_css_attr_new();
532 sp_repr_css_set_property(css, "fill", "none");
534 sp_repr_css_change(repr, css, "style");
536 sp_repr_css_attr_unref(css);
537 }
539 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
540 // this means it's basically random which of the pieces inherits the original's id and clones
541 // a better algorithm might figure out e.g. the biggest piece
542 repr->setAttribute("id", id);
544 repr->setAttribute("transform", transform);
546 // add the new repr to the parent
547 parent->appendChild(repr);
549 // move to the saved position
550 repr->setPosition(pos > 0 ? pos : 0);
552 selection->add(repr);
553 Inkscape::GC::release(repr);
555 delete resPath[i];
556 }
557 if ( resPath ) free(resPath);
559 } else {
560 gchar *d = res->svg_dump_path();
562 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
563 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
564 repr->setAttribute("style", style);
566 if ( mask )
567 repr->setAttribute("mask", mask);
569 if ( clip_path )
570 repr->setAttribute("clip-path", clip_path);
572 repr->setAttribute("d", d);
573 g_free(d);
575 repr->setAttribute("transform", transform);
577 repr->setAttribute("id", id);
578 parent->appendChild(repr);
579 if (title) {
580 sp_desktop_document(desktop)->getObjectByRepr(repr)->setTitle(title);
581 }
582 if (desc) {
583 sp_desktop_document(desktop)->getObjectByRepr(repr)->setDesc(desc);
584 }
585 repr->setPosition(pos > 0 ? pos : 0);
587 selection->add(repr);
588 Inkscape::GC::release(repr);
589 }
591 g_free(transform);
592 if (title) g_free(title);
593 if (desc) g_free(desc);
595 if (verb != SP_VERB_NONE) {
596 sp_document_done(sp_desktop_document(desktop), verb, description);
597 }
599 delete res;
600 }
602 static
603 void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix marker_transform,
604 Geom::Scale stroke_scale, Geom::Matrix transform,
605 Inkscape::XML::Node *g_repr, Inkscape::XML::Document *xml_doc, SPDocument * doc )
606 {
607 SPMarker* marker = SP_MARKER (marker_object);
608 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker_object));
610 Geom::Matrix tr(marker_transform);
612 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
613 tr = stroke_scale * tr;
614 }
616 // total marker transform
617 tr = marker_item->transform * marker->c2p * tr * transform;
619 if (SP_OBJECT_REPR(marker_item)) {
620 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
621 g_repr->appendChild(m_repr);
622 SPItem *marker_item = (SPItem *) doc->getObjectByRepr(m_repr);
623 sp_item_write_transform(marker_item, m_repr, tr);
624 }
625 }
627 void
628 sp_selected_path_outline(SPDesktop *desktop)
629 {
630 Inkscape::Selection *selection = sp_desktop_selection(desktop);
632 if (selection->isEmpty()) {
633 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
634 return;
635 }
637 bool did = false;
639 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
640 items != NULL;
641 items = items->next) {
643 SPItem *item = (SPItem *) items->data;
645 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
646 continue;
648 SPCurve *curve = NULL;
649 if (SP_IS_SHAPE(item)) {
650 curve = sp_shape_get_curve(SP_SHAPE(item));
651 if (curve == NULL)
652 continue;
653 }
654 if (SP_IS_TEXT(item)) {
655 curve = SP_TEXT(item)->getNormalizedBpath();
656 if (curve == NULL)
657 continue;
658 }
660 // pas de stroke pas de chocolat
661 if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
662 curve->unref();
663 continue;
664 }
666 // remember old stroke style, to be set on fill
667 SPStyle *i_style = SP_OBJECT_STYLE(item);
668 SPCSSAttr *ncss;
669 {
670 ncss = sp_css_attr_from_style(i_style, SP_STYLE_FLAG_ALWAYS);
671 gchar const *s_val = sp_repr_css_property(ncss, "stroke", NULL);
672 gchar const *s_opac = sp_repr_css_property(ncss, "stroke-opacity", NULL);
674 sp_repr_css_set_property(ncss, "stroke", "none");
675 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
676 sp_repr_css_set_property(ncss, "fill", s_val);
677 if ( s_opac ) {
678 sp_repr_css_set_property(ncss, "fill-opacity", s_opac);
679 } else {
680 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
681 }
682 sp_repr_css_unset_property(ncss, "marker-start");
683 sp_repr_css_unset_property(ncss, "marker-mid");
684 sp_repr_css_unset_property(ncss, "marker-end");
685 }
687 Geom::Matrix const transform(item->transform);
688 float const scale = transform.descrim();
689 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
690 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
692 float o_width, o_miter;
693 JoinType o_join;
694 ButtType o_butt;
696 {
697 int jointype, captype;
699 jointype = i_style->stroke_linejoin.computed;
700 captype = i_style->stroke_linecap.computed;
701 o_width = i_style->stroke_width.computed;
703 switch (jointype) {
704 case SP_STROKE_LINEJOIN_MITER:
705 o_join = join_pointy;
706 break;
707 case SP_STROKE_LINEJOIN_ROUND:
708 o_join = join_round;
709 break;
710 default:
711 o_join = join_straight;
712 break;
713 }
715 switch (captype) {
716 case SP_STROKE_LINECAP_SQUARE:
717 o_butt = butt_square;
718 break;
719 case SP_STROKE_LINECAP_ROUND:
720 o_butt = butt_round;
721 break;
722 default:
723 o_butt = butt_straight;
724 break;
725 }
727 if (o_width < 0.1)
728 o_width = 0.1;
729 o_miter = i_style->stroke_miterlimit.value * o_width;
730 }
732 SPCurve *curvetemp = curve_for_item(item);
733 if (curvetemp == NULL) {
734 curve->unref();
735 continue;
736 }
737 // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly.
738 Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curvetemp->get_pathvector() );
739 curvetemp->unref();
741 Path *orig = new Path;
742 orig->LoadPathVector(pathv);
744 Path *res = new Path;
745 res->SetBackData(false);
747 if (i_style->stroke_dash.n_dash) {
748 // For dashed strokes, use Stroke method, because Outline can't do dashes
749 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
751 orig->ConvertWithBackData(0.1);
753 orig->DashPolylineFromStyle(i_style, scale, 0);
755 Shape* theShape = new Shape;
756 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
757 0.5 * o_miter);
758 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
760 Shape *theRes = new Shape;
762 theRes->ConvertToShape(theShape, fill_positive);
764 Path *originaux[1];
765 originaux[0] = res;
766 theRes->ConvertToForme(orig, 1, originaux);
768 res->Coalesce(5.0);
770 delete theShape;
771 delete theRes;
773 } else {
775 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
777 orig->Coalesce(0.5 * o_width);
779 Shape *theShape = new Shape;
780 Shape *theRes = new Shape;
782 res->ConvertWithBackData(1.0);
783 res->Fill(theShape, 0);
784 theRes->ConvertToShape(theShape, fill_positive);
786 Path *originaux[1];
787 originaux[0] = res;
788 theRes->ConvertToForme(orig, 1, originaux);
790 delete theShape;
791 delete theRes;
792 }
794 if (orig->descr_cmd.size() <= 1) {
795 // ca a merd\8e, ou bien le resultat est vide
796 delete res;
797 delete orig;
798 continue;
799 }
801 did = true;
803 // remember the position of the item
804 gint pos = SP_OBJECT_REPR(item)->position();
805 // remember parent
806 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
807 // remember id
808 char const *id = SP_OBJECT_REPR(item)->attribute("id");
809 // remember title
810 gchar *title = item->title();
811 // remember description
812 gchar *desc = item->desc();
814 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
816 SPDocument * doc = sp_desktop_document(desktop);
817 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
818 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
820 // restore old style, but set old stroke style on fill
821 sp_repr_css_change(repr, ncss, "style");
823 sp_repr_css_attr_unref(ncss);
825 gchar *str = orig->svg_dump_path();
826 repr->setAttribute("d", str);
827 g_free(str);
829 if (mask)
830 repr->setAttribute("mask", mask);
831 if (clip_path)
832 repr->setAttribute("clip-path", clip_path);
834 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
836 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
837 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
839 // add the group to the parent
840 parent->appendChild(g_repr);
841 // move to the saved position
842 g_repr->setPosition(pos > 0 ? pos : 0);
844 g_repr->appendChild(repr);
845 // restore title, description, id, transform
846 repr->setAttribute("id", id);
847 SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr);
848 sp_item_write_transform(newitem, repr, transform);
849 if (title) {
850 newitem->setTitle(title);
851 }
852 if (desc) {
853 newitem->setDesc(desc);
854 }
856 SPShape *shape = SP_SHAPE(item);
858 Geom::PathVector const & pathv = curve->get_pathvector();
860 // START marker
861 for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START
862 if ( SPObject *marker_obj = shape->marker[i] ) {
863 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(pathv.front().front()));
864 sp_selected_path_outline_add_marker( marker_obj, m,
865 Geom::Scale(i_style->stroke_width.computed), transform,
866 g_repr, xml_doc, doc );
867 }
868 }
869 // MID marker
870 for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID
871 SPObject *midmarker_obj = shape->marker[i];
872 if (!midmarker_obj) continue;
873 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
874 // START position
875 if ( path_it != pathv.begin()
876 && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
877 {
878 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
879 sp_selected_path_outline_add_marker( midmarker_obj, m,
880 Geom::Scale(i_style->stroke_width.computed), transform,
881 g_repr, xml_doc, doc );
882 }
883 // MID position
884 if (path_it->size_default() > 1) {
885 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
886 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
887 while (curve_it2 != path_it->end_default())
888 {
889 /* Put marker between curve_it1 and curve_it2.
890 * Loop to end_default (so including closing segment), because when a path is closed,
891 * there should be a midpoint marker between last segment and closing straight line segment
892 */
893 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
894 sp_selected_path_outline_add_marker(midmarker_obj, m,
895 Geom::Scale(i_style->stroke_width.computed), transform,
896 g_repr, xml_doc, doc);
898 ++curve_it1;
899 ++curve_it2;
900 }
901 }
902 // END position
903 if ( path_it != (pathv.end()-1) && !path_it->empty()) {
904 Geom::Curve const &lastcurve = path_it->back_default();
905 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
906 sp_selected_path_outline_add_marker( midmarker_obj, m,
907 Geom::Scale(i_style->stroke_width.computed), transform,
908 g_repr, xml_doc, doc );
909 }
910 }
911 }
912 // END marker
913 for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END
914 if ( SPObject *marker_obj = shape->marker[i] ) {
915 /* Get reference to last curve in the path.
916 * For moveto-only path, this returns the "closing line segment". */
917 Geom::Path const &path_last = pathv.back();
918 unsigned int index = path_last.size_default();
919 if (index > 0) {
920 index--;
921 }
922 Geom::Curve const &lastcurve = path_last[index];
924 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
925 sp_selected_path_outline_add_marker( marker_obj, m,
926 Geom::Scale(i_style->stroke_width.computed), transform,
927 g_repr, xml_doc, doc );
928 }
929 }
931 selection->add(g_repr);
933 Inkscape::GC::release(g_repr);
936 } else {
938 // add the new repr to the parent
939 parent->appendChild(repr);
941 // move to the saved position
942 repr->setPosition(pos > 0 ? pos : 0);
944 // restore title, description, id, transform
945 repr->setAttribute("id", id);
947 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
948 sp_item_write_transform(newitem, repr, transform);
949 if (title) {
950 newitem->setTitle(title);
951 }
952 if (desc) {
953 newitem->setDesc(desc);
954 }
956 selection->add(repr);
958 }
960 Inkscape::GC::release(repr);
962 curve->unref();
963 selection->remove(item);
964 SP_OBJECT(item)->deleteObject(false);
966 }
967 if (title) g_free(title);
968 if (desc) g_free(desc);
970 delete res;
971 delete orig;
972 }
974 if (did) {
975 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
976 _("Convert stroke to path"));
977 } else {
978 // TRANSLATORS: "to outline" means "to convert stroke to path"
979 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
980 return;
981 }
982 }
985 void
986 sp_selected_path_offset(SPDesktop *desktop)
987 {
988 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
989 double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
991 sp_selected_path_do_offset(desktop, true, prefOffset);
992 }
993 void
994 sp_selected_path_inset(SPDesktop *desktop)
995 {
996 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
997 double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
999 sp_selected_path_do_offset(desktop, false, prefOffset);
1000 }
1002 void
1003 sp_selected_path_offset_screen(SPDesktop *desktop, double pixels)
1004 {
1005 sp_selected_path_do_offset(desktop, true, pixels / desktop->current_zoom());
1006 }
1008 void
1009 sp_selected_path_inset_screen(SPDesktop *desktop, double pixels)
1010 {
1011 sp_selected_path_do_offset(desktop, false, pixels / desktop->current_zoom());
1012 }
1015 void sp_selected_path_create_offset_object_zero(SPDesktop *desktop)
1016 {
1017 sp_selected_path_create_offset_object(desktop, 0, false);
1018 }
1020 void sp_selected_path_create_offset(SPDesktop *desktop)
1021 {
1022 sp_selected_path_create_offset_object(desktop, 1, false);
1023 }
1024 void sp_selected_path_create_inset(SPDesktop *desktop)
1025 {
1026 sp_selected_path_create_offset_object(desktop, -1, false);
1027 }
1029 void sp_selected_path_create_updating_offset_object_zero(SPDesktop *desktop)
1030 {
1031 sp_selected_path_create_offset_object(desktop, 0, true);
1032 }
1034 void sp_selected_path_create_updating_offset(SPDesktop *desktop)
1035 {
1036 sp_selected_path_create_offset_object(desktop, 1, true);
1037 }
1038 void sp_selected_path_create_updating_inset(SPDesktop *desktop)
1039 {
1040 sp_selected_path_create_offset_object(desktop, -1, true);
1041 }
1043 void
1044 sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating)
1045 {
1046 Inkscape::Selection *selection;
1047 Inkscape::XML::Node *repr;
1048 SPItem *item;
1049 SPCurve *curve;
1050 gchar *style, *str;
1051 float o_width, o_miter;
1052 JoinType o_join;
1053 ButtType o_butt;
1055 curve = NULL;
1057 selection = sp_desktop_selection(desktop);
1059 item = selection->singleItem();
1061 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
1062 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
1063 return;
1064 }
1065 if (SP_IS_SHAPE(item))
1066 {
1067 curve = sp_shape_get_curve(SP_SHAPE(item));
1068 if (curve == NULL)
1069 return;
1070 }
1071 if (SP_IS_TEXT(item))
1072 {
1073 curve = SP_TEXT(item)->getNormalizedBpath();
1074 if (curve == NULL)
1075 return;
1076 }
1078 Geom::Matrix const transform(item->transform);
1080 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1082 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
1084 // remember the position of the item
1085 gint pos = SP_OBJECT_REPR(item)->position();
1086 // remember parent
1087 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1089 {
1090 SPStyle *i_style = SP_OBJECT(item)->style;
1091 int jointype, captype;
1093 jointype = i_style->stroke_linejoin.value;
1094 captype = i_style->stroke_linecap.value;
1095 o_width = i_style->stroke_width.computed;
1096 if (jointype == SP_STROKE_LINEJOIN_MITER)
1097 {
1098 o_join = join_pointy;
1099 }
1100 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1101 {
1102 o_join = join_round;
1103 }
1104 else
1105 {
1106 o_join = join_straight;
1107 }
1108 if (captype == SP_STROKE_LINECAP_SQUARE)
1109 {
1110 o_butt = butt_square;
1111 }
1112 else if (captype == SP_STROKE_LINECAP_ROUND)
1113 {
1114 o_butt = butt_round;
1115 }
1116 else
1117 {
1118 o_butt = butt_straight;
1119 }
1121 {
1122 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1123 o_width = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0);
1124 }
1126 if (o_width < 0.01)
1127 o_width = 0.01;
1128 o_miter = i_style->stroke_miterlimit.value * o_width;
1129 }
1131 Path *orig = Path_for_item(item, true, false);
1132 if (orig == NULL)
1133 {
1134 g_free(style);
1135 curve->unref();
1136 return;
1137 }
1139 Path *res = new Path;
1140 res->SetBackData(false);
1142 {
1143 Shape *theShape = new Shape;
1144 Shape *theRes = new Shape;
1146 orig->ConvertWithBackData(1.0);
1147 orig->Fill(theShape, 0);
1149 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1150 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1151 if (val && strcmp(val, "nonzero") == 0)
1152 {
1153 theRes->ConvertToShape(theShape, fill_nonZero);
1154 }
1155 else if (val && strcmp(val, "evenodd") == 0)
1156 {
1157 theRes->ConvertToShape(theShape, fill_oddEven);
1158 }
1159 else
1160 {
1161 theRes->ConvertToShape(theShape, fill_nonZero);
1162 }
1164 Path *originaux[1];
1165 originaux[0] = orig;
1166 theRes->ConvertToForme(res, 1, originaux);
1168 delete theShape;
1169 delete theRes;
1170 }
1172 curve->unref();
1174 if (res->descr_cmd.size() <= 1)
1175 {
1176 // pas vraiment de points sur le resultat
1177 // donc il ne reste rien
1178 sp_document_done(sp_desktop_document(desktop),
1179 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1180 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1181 (updating ? _("Create linked offset")
1182 : _("Create dynamic offset")));
1183 selection->clear();
1185 delete res;
1186 delete orig;
1187 g_free(style);
1188 return;
1189 }
1191 {
1192 gchar tstr[80];
1194 tstr[79] = '\0';
1196 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1197 repr = xml_doc->createElement("svg:path");
1198 repr->setAttribute("sodipodi:type", "inkscape:offset");
1199 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1200 ? o_width
1201 : expand < 0
1202 ? -o_width
1203 : 0 ));
1205 str = res->svg_dump_path();
1206 repr->setAttribute("inkscape:original", str);
1207 g_free(str);
1209 if ( updating ) {
1210 char const *id = SP_OBJECT(item)->repr->attribute("id");
1211 char const *uri = g_strdup_printf("#%s", id);
1212 repr->setAttribute("xlink:href", uri);
1213 g_free((void *) uri);
1214 } else {
1215 repr->setAttribute("inkscape:href", NULL);
1216 }
1218 repr->setAttribute("style", style);
1220 // add the new repr to the parent
1221 parent->appendChild(repr);
1223 // move to the saved position
1224 repr->setPosition(pos > 0 ? pos : 0);
1226 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1228 if ( updating ) {
1229 // on conserve l'original
1230 // we reapply the transform to the original (offset will feel it)
1231 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1232 } else {
1233 // delete original, apply the transform to the offset
1234 SP_OBJECT(item)->deleteObject(false);
1235 sp_item_write_transform(nitem, repr, transform);
1236 }
1238 // The object just created from a temporary repr is only a seed.
1239 // We need to invoke its write which will update its real repr (in particular adding d=)
1240 SP_OBJECT(nitem)->updateRepr();
1242 Inkscape::GC::release(repr);
1244 selection->set(nitem);
1245 }
1247 sp_document_done(sp_desktop_document(desktop),
1248 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1249 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1250 (updating ? _("Create linked offset")
1251 : _("Create dynamic offset")));
1253 delete res;
1254 delete orig;
1256 g_free(style);
1257 }
1270 void
1271 sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset)
1272 {
1273 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1275 if (selection->isEmpty()) {
1276 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1277 return;
1278 }
1280 bool did = false;
1282 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1283 items != NULL;
1284 items = items->next) {
1286 SPItem *item = (SPItem *) items->data;
1288 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1289 continue;
1291 SPCurve *curve = NULL;
1292 if (SP_IS_SHAPE(item)) {
1293 curve = sp_shape_get_curve(SP_SHAPE(item));
1294 if (curve == NULL)
1295 continue;
1296 }
1297 if (SP_IS_TEXT(item)) {
1298 curve = SP_TEXT(item)->getNormalizedBpath();
1299 if (curve == NULL)
1300 continue;
1301 }
1303 Geom::Matrix const transform(item->transform);
1305 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1307 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1309 float o_width, o_miter;
1310 JoinType o_join;
1311 ButtType o_butt;
1313 {
1314 SPStyle *i_style = SP_OBJECT(item)->style;
1315 int jointype, captype;
1317 jointype = i_style->stroke_linejoin.value;
1318 captype = i_style->stroke_linecap.value;
1319 o_width = i_style->stroke_width.computed;
1321 switch (jointype) {
1322 case SP_STROKE_LINEJOIN_MITER:
1323 o_join = join_pointy;
1324 break;
1325 case SP_STROKE_LINEJOIN_ROUND:
1326 o_join = join_round;
1327 break;
1328 default:
1329 o_join = join_straight;
1330 break;
1331 }
1333 switch (captype) {
1334 case SP_STROKE_LINECAP_SQUARE:
1335 o_butt = butt_square;
1336 break;
1337 case SP_STROKE_LINECAP_ROUND:
1338 o_butt = butt_round;
1339 break;
1340 default:
1341 o_butt = butt_straight;
1342 break;
1343 }
1345 o_width = prefOffset;
1347 if (o_width < 0.1)
1348 o_width = 0.1;
1349 o_miter = i_style->stroke_miterlimit.value * o_width;
1350 }
1352 Path *orig = Path_for_item(item, false);
1353 if (orig == NULL) {
1354 g_free(style);
1355 curve->unref();
1356 continue;
1357 }
1359 Path *res = new Path;
1360 res->SetBackData(false);
1362 {
1363 Shape *theShape = new Shape;
1364 Shape *theRes = new Shape;
1366 orig->ConvertWithBackData(0.03);
1367 orig->Fill(theShape, 0);
1369 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1370 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1371 if (val && strcmp(val, "nonzero") == 0)
1372 {
1373 theRes->ConvertToShape(theShape, fill_nonZero);
1374 }
1375 else if (val && strcmp(val, "evenodd") == 0)
1376 {
1377 theRes->ConvertToShape(theShape, fill_oddEven);
1378 }
1379 else
1380 {
1381 theRes->ConvertToShape(theShape, fill_nonZero);
1382 }
1384 // et maintenant: offset
1385 // methode inexacte
1386 /* Path *originaux[1];
1387 originaux[0] = orig;
1388 theRes->ConvertToForme(res, 1, originaux);
1390 if (expand) {
1391 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1392 } else {
1393 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1394 }
1396 orig->ConvertWithBackData(1.0);
1397 orig->Fill(theShape, 0);
1398 theRes->ConvertToShape(theShape, fill_positive);
1399 originaux[0] = orig;
1400 theRes->ConvertToForme(res, 1, originaux);
1402 if (o_width >= 0.5) {
1403 // res->Coalesce(1.0);
1404 res->ConvertEvenLines(1.0);
1405 res->Simplify(1.0);
1406 } else {
1407 // res->Coalesce(o_width);
1408 res->ConvertEvenLines(1.0*o_width);
1409 res->Simplify(1.0 * o_width);
1410 } */
1411 // methode par makeoffset
1413 if (expand)
1414 {
1415 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1416 }
1417 else
1418 {
1419 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1420 }
1421 theRes->ConvertToShape(theShape, fill_positive);
1423 res->Reset();
1424 theRes->ConvertToForme(res);
1426 if (o_width >= 1.0)
1427 {
1428 res->ConvertEvenLines(1.0);
1429 res->Simplify(1.0);
1430 }
1431 else
1432 {
1433 res->ConvertEvenLines(1.0*o_width);
1434 res->Simplify(1.0 * o_width);
1435 }
1437 delete theShape;
1438 delete theRes;
1439 }
1441 did = true;
1443 curve->unref();
1444 // remember the position of the item
1445 gint pos = SP_OBJECT_REPR(item)->position();
1446 // remember parent
1447 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1448 // remember id
1449 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1451 selection->remove(item);
1452 SP_OBJECT(item)->deleteObject(false);
1454 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1456 gchar tstr[80];
1458 tstr[79] = '\0';
1460 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1461 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1463 repr->setAttribute("style", style);
1465 gchar *str = res->svg_dump_path();
1466 repr->setAttribute("d", str);
1467 g_free(str);
1469 // add the new repr to the parent
1470 parent->appendChild(repr);
1472 // move to the saved position
1473 repr->setPosition(pos > 0 ? pos : 0);
1475 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1477 // reapply the transform
1478 sp_item_write_transform(newitem, repr, transform);
1480 repr->setAttribute("id", id);
1482 selection->add(repr);
1484 Inkscape::GC::release(repr);
1485 }
1487 delete orig;
1488 delete res;
1489 }
1491 if (did) {
1492 sp_document_done(sp_desktop_document(desktop),
1493 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1494 (expand ? _("Outset path") : _("Inset path")));
1495 } else {
1496 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1497 return;
1498 }
1499 }
1502 static bool
1503 sp_selected_path_simplify_items(SPDesktop *desktop,
1504 Inkscape::Selection *selection, GSList *items,
1505 float threshold, bool justCoalesce,
1506 float angleLimit, bool breakableAngles,
1507 bool modifySelection);
1510 //return true if we changed something, else false
1511 bool
1512 sp_selected_path_simplify_item(SPDesktop *desktop,
1513 Inkscape::Selection *selection, SPItem *item,
1514 float threshold, bool justCoalesce,
1515 float angleLimit, bool breakableAngles,
1516 gdouble size, bool modifySelection)
1517 {
1518 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1519 return false;
1521 //If this is a group, do the children instead
1522 if (SP_IS_GROUP(item)) {
1523 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1525 return sp_selected_path_simplify_items(desktop, selection, items,
1526 threshold, justCoalesce,
1527 angleLimit, breakableAngles,
1528 false);
1529 }
1532 SPCurve *curve = NULL;
1534 if (SP_IS_SHAPE(item)) {
1535 curve = sp_shape_get_curve(SP_SHAPE(item));
1536 if (!curve)
1537 return false;
1538 }
1540 if (SP_IS_TEXT(item)) {
1541 curve = SP_TEXT(item)->getNormalizedBpath();
1542 if (!curve)
1543 return false;
1544 }
1546 // correct virtual size by full transform (bug #166937)
1547 size /= sp_item_i2doc_affine(item).descrim();
1549 // save the transform, to re-apply it after simplification
1550 Geom::Matrix const transform(item->transform);
1552 /*
1553 reset the transform, effectively transforming the item by transform.inverse();
1554 this is necessary so that the item is transformed twice back and forth,
1555 allowing all compensations to cancel out regardless of the preferences
1556 */
1557 sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
1559 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1560 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1561 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1563 Path *orig = Path_for_item(item, false);
1564 if (orig == NULL) {
1565 g_free(style);
1566 curve->unref();
1567 return false;
1568 }
1570 curve->unref();
1571 // remember the position of the item
1572 gint pos = SP_OBJECT_REPR(item)->position();
1573 // remember parent
1574 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1575 // remember id
1576 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1577 // remember path effect
1578 char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
1579 // remember title
1580 gchar *title = item->title();
1581 // remember description
1582 gchar *desc = item->desc();
1584 //If a group was selected, to not change the selection list
1585 if (modifySelection)
1586 selection->remove(item);
1588 SP_OBJECT(item)->deleteObject(false);
1590 if ( justCoalesce ) {
1591 orig->Coalesce(threshold * size);
1592 } else {
1593 orig->ConvertEvenLines(threshold * size);
1594 orig->Simplify(threshold * size);
1595 }
1597 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1598 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1600 // restore style, mask and clip-path
1601 repr->setAttribute("style", style);
1602 g_free(style);
1604 if ( mask ) {
1605 repr->setAttribute("mask", mask);
1606 g_free(mask);
1607 }
1609 if ( clip_path ) {
1610 repr->setAttribute("clip-path", clip_path);
1611 g_free(clip_path);
1612 }
1614 // restore path effect
1615 repr->setAttribute("inkscape:path-effect", patheffect);
1617 // path
1618 gchar *str = orig->svg_dump_path();
1619 if (patheffect)
1620 repr->setAttribute("inkscape:original-d", str);
1621 else
1622 repr->setAttribute("d", str);
1623 g_free(str);
1625 // restore id
1626 repr->setAttribute("id", id);
1628 // add the new repr to the parent
1629 parent->appendChild(repr);
1631 // move to the saved position
1632 repr->setPosition(pos > 0 ? pos : 0);
1634 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1636 // reapply the transform
1637 sp_item_write_transform(newitem, repr, transform);
1639 // restore title & description
1640 if (title) {
1641 newitem->setTitle(title);
1642 g_free(title);
1643 }
1644 if (desc) {
1645 newitem->setDesc(desc);
1646 g_free(desc);
1647 }
1649 //If we are not in a selected group
1650 if (modifySelection)
1651 selection->add(repr);
1653 Inkscape::GC::release(repr);
1655 // clean up
1656 if (orig) delete orig;
1658 return true;
1659 }
1662 bool
1663 sp_selected_path_simplify_items(SPDesktop *desktop,
1664 Inkscape::Selection *selection, GSList *items,
1665 float threshold, bool justCoalesce,
1666 float angleLimit, bool breakableAngles,
1667 bool modifySelection)
1668 {
1669 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1670 bool simplifyIndividualPaths = prefs->getBool("/options/simplifyindividualpaths/value");
1672 gchar *simplificationType;
1673 if (simplifyIndividualPaths) {
1674 simplificationType = _("Simplifying paths (separately):");
1675 } else {
1676 simplificationType = _("Simplifying paths:");
1677 }
1679 bool didSomething = false;
1681 Geom::OptRect selectionBbox = selection->bounds();
1682 if (!selectionBbox) {
1683 return false;
1684 }
1685 gdouble selectionSize = L2(selectionBbox->dimensions());
1687 gdouble simplifySize = selectionSize;
1689 int pathsSimplified = 0;
1690 int totalPathCount = g_slist_length(items);
1692 // set "busy" cursor
1693 desktop->setWaitingCursor();
1695 for (; items != NULL; items = items->next) {
1696 SPItem *item = (SPItem *) items->data;
1698 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1699 continue;
1701 if (simplifyIndividualPaths) {
1702 Geom::OptRect itemBbox = item->getBounds(sp_item_i2d_affine(item));
1703 if (itemBbox) {
1704 simplifySize = L2(itemBbox->dimensions());
1705 } else {
1706 simplifySize = 0;
1707 }
1708 }
1710 pathsSimplified++;
1712 if (pathsSimplified % 20 == 0) {
1713 gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."),
1714 simplificationType, pathsSimplified, totalPathCount);
1715 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1716 }
1718 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1719 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1720 }
1722 desktop->clearWaitingCursor();
1724 if (pathsSimplified > 20) {
1725 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1726 }
1728 return didSomething;
1729 }
1731 void
1732 sp_selected_path_simplify_selection(SPDesktop *desktop, float threshold, bool justCoalesce,
1733 float angleLimit, bool breakableAngles)
1734 {
1735 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1737 if (selection->isEmpty()) {
1738 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1739 _("Select <b>path(s)</b> to simplify."));
1740 return;
1741 }
1743 GSList *items = g_slist_copy((GSList *) selection->itemList());
1745 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1746 items, threshold,
1747 justCoalesce,
1748 angleLimit,
1749 breakableAngles, true);
1751 if (didSomething)
1752 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1753 _("Simplify"));
1754 else
1755 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1757 }
1760 // globals for keeping track of accelerated simplify
1761 static double previousTime = 0.0;
1762 static gdouble simplifyMultiply = 1.0;
1764 void
1765 sp_selected_path_simplify(SPDesktop *desktop)
1766 {
1767 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1768 gdouble simplifyThreshold =
1769 prefs->getDouble("/options/simplifythreshold/value", 0.003);
1770 bool simplifyJustCoalesce = prefs->getBool("/options/simplifyjustcoalesce/value", 0);
1772 //Get the current time
1773 GTimeVal currentTimeVal;
1774 g_get_current_time(¤tTimeVal);
1775 double currentTime = currentTimeVal.tv_sec * 1000000 +
1776 currentTimeVal.tv_usec;
1778 //Was the previous call to this function recent? (<0.5 sec)
1779 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1781 // add to the threshold 1/2 of its original value
1782 simplifyMultiply += 0.5;
1783 simplifyThreshold *= simplifyMultiply;
1785 } else {
1786 // reset to the default
1787 simplifyMultiply = 1;
1788 }
1790 //remember time for next call
1791 previousTime = currentTime;
1793 //g_print("%g\n", simplify_threshold);
1795 //Make the actual call
1796 sp_selected_path_simplify_selection(desktop, simplifyThreshold,
1797 simplifyJustCoalesce, 0.0, false);
1798 }
1802 // fonctions utilitaires
1804 bool
1805 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1806 {
1807 if (who == NULL || a == NULL)
1808 return false;
1809 if (who == a)
1810 return true;
1811 return Ancetre(sp_repr_parent(a), who);
1812 }
1814 Path *
1815 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1816 {
1817 SPCurve *curve = curve_for_item(item);
1819 if (curve == NULL)
1820 return NULL;
1822 Geom::PathVector *pathv = pathvector_for_curve(item, curve, doTransformation, transformFull, Geom::identity(), Geom::identity());
1823 curve->unref();
1825 Path *dest = new Path;
1826 dest->LoadPathVector(*pathv);
1827 delete pathv;
1829 return dest;
1830 }
1832 /*
1833 * NOTE: Returns empty pathvector if curve == NULL
1834 * TODO: see if calling this method can be optimized. All the pathvector copying might be slow.
1835 */
1836 Geom::PathVector*
1837 pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, Geom::Matrix extraPreAffine, Geom::Matrix extraPostAffine)
1838 {
1839 if (curve == NULL)
1840 return NULL;
1842 Geom::PathVector *dest = new Geom::PathVector;
1843 *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller!
1845 if (doTransformation) {
1846 if (transformFull) {
1847 *dest *= extraPreAffine * sp_item_i2doc_affine(item) * extraPostAffine;
1848 } else {
1849 *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine;
1850 }
1851 } else {
1852 *dest *= extraPreAffine * extraPostAffine;
1853 }
1855 return dest;
1856 }
1858 SPCurve* curve_for_item(SPItem *item)
1859 {
1860 if (!item)
1861 return NULL;
1863 SPCurve *curve = NULL;
1864 if (SP_IS_SHAPE(item)) {
1865 if (SP_IS_PATH(item)) {
1866 curve = sp_path_get_curve_for_edit(SP_PATH(item));
1867 } else {
1868 curve = sp_shape_get_curve(SP_SHAPE(item));
1869 }
1870 }
1871 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
1872 {
1873 curve = te_get_layout(item)->convertToCurves();
1874 }
1875 else if (SP_IS_IMAGE(item))
1876 {
1877 curve = sp_image_get_curve(SP_IMAGE(item));
1878 }
1880 return curve; // do not forget to unref the curve at some point!
1881 }
1883 boost::optional<Path::cut_position> get_nearest_position_on_Path(Path *path, Geom::Point p, unsigned seg)
1884 {
1885 //get nearest position on path
1886 Path::cut_position pos = path->PointToCurvilignPosition(p, seg);
1887 return pos;
1888 }
1890 Geom::Point get_point_on_Path(Path *path, int piece, double t)
1891 {
1892 Geom::Point p;
1893 path->PointAt(piece, t, p);
1894 return p;
1895 }
1898 /*
1899 Local Variables:
1900 mode:c++
1901 c-file-style:"stroustrup"
1902 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1903 indent-tabs-mode:nil
1904 fill-column:99
1905 End:
1906 */
1907 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :