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 "inkscape.h"
37 #include "document.h"
38 #include "message-stack.h"
39 #include "selection.h"
40 #include "desktop-handles.h"
41 #include "desktop.h"
42 #include "display/canvas-bpath.h"
43 #include "display/curve.h"
44 #include <glibmm/i18n.h>
45 #include "prefs-utils.h"
47 #include "libnr/n-art-bpath.h"
48 #include "libnr/nr-path.h"
49 #include "xml/repr.h"
50 #include "xml/repr-sorting.h"
51 #include <2geom/pathvector.h>
52 #include <libnr/nr-matrix-fns.h>
53 #include <libnr/nr-matrix-ops.h>
54 #include <libnr/nr-matrix-translate-ops.h>
55 #include <libnr/nr-scale-matrix-ops.h>
56 #include <libnr/n-art-bpath-2geom.h>
58 #include "livarot/Path.h"
59 #include "livarot/Shape.h"
61 #include "splivarot.h"
63 bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
65 void sp_selected_path_boolop(bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
66 void sp_selected_path_do_offset(bool expand, double prefOffset);
67 void sp_selected_path_create_offset_object(int expand, bool updating);
69 void
70 sp_selected_path_union()
71 {
72 sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
73 }
75 void
76 sp_selected_path_union_skip_undo()
77 {
78 sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union"));
79 }
81 void
82 sp_selected_path_intersect()
83 {
84 sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
85 }
87 void
88 sp_selected_path_diff()
89 {
90 sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
91 }
93 void
94 sp_selected_path_diff_skip_undo()
95 {
96 sp_selected_path_boolop(bool_op_diff, SP_VERB_NONE, _("Difference"));
97 }
99 void
100 sp_selected_path_symdiff()
101 {
102 sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
103 }
104 void
105 sp_selected_path_cut()
106 {
107 sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
108 }
109 void
110 sp_selected_path_slice()
111 {
112 sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path"));
113 }
116 // boolean operations
117 // take the source paths from the file, do the operation, delete the originals and add the results
118 void
119 sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description)
120 {
121 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
123 Inkscape::Selection *selection = sp_desktop_selection(desktop);
125 GSList *il = (GSList *) selection->itemList();
127 // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
128 if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) {
129 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
130 return;
131 }
132 else if ( g_slist_length(il) < 1 ) {
133 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
134 return;
135 }
137 if (g_slist_length(il) > 2) {
138 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
139 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>exactly 2 paths</b> to perform difference, XOR, division, or path cut."));
140 return;
141 }
142 }
144 // reverseOrderForOp marks whether the order of the list is the top->down order
145 // it's only used when there are 2 objects, and for operations who need to know the
146 // topmost object (differences, cuts)
147 bool reverseOrderForOp = false;
149 // mettre les elements de la liste dans l'ordre pour ces operations
150 if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) {
151 // check in the tree to find which element of the selection list is topmost (for 2-operand commands only)
152 Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data);
153 Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data);
155 if (a == NULL || b == NULL) {
156 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."));
157 return;
158 }
160 if (Ancetre(a, b)) {
161 // a is the parent of b, already in the proper order
162 } else if (Ancetre(b, a)) {
163 // reverse order
164 reverseOrderForOp = true;
165 } else {
167 // objects are not in parent/child relationship;
168 // find their lowest common ancestor
169 Inkscape::XML::Node *dad = LCA(a, b);
170 if (dad == NULL) {
171 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."));
172 return;
173 }
175 // find the children of the LCA that lead from it to the a and b
176 Inkscape::XML::Node *as = AncetreFils(a, dad);
177 Inkscape::XML::Node *bs = AncetreFils(b, dad);
179 // find out which comes first
180 for (Inkscape::XML::Node *child = dad->firstChild(); child; child = child->next()) {
181 if (child == as) {
182 /* a first, so reverse. */
183 reverseOrderForOp = true;
184 break;
185 }
186 if (child == bs)
187 break;
188 }
189 }
190 }
192 il = g_slist_copy(il);
194 // first check if all the input objects have shapes
195 // otherwise bail out
196 for (GSList *l = il; l != NULL; l = l->next)
197 {
198 SPItem *item = SP_ITEM(l->data);
199 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item))
200 {
201 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
202 g_slist_free(il);
203 return;
204 }
205 }
207 // extract the livarot Paths from the source objects
208 // also get the winding rule specified in the style
209 int nbOriginaux = g_slist_length(il);
210 std::vector<Path *> originaux(nbOriginaux);
211 std::vector<FillRule> origWind(nbOriginaux);
212 int curOrig;
213 {
214 curOrig = 0;
215 for (GSList *l = il; l != NULL; l = l->next)
216 {
217 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(il->data), "style");
218 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
219 if (val && strcmp(val, "nonzero") == 0) {
220 origWind[curOrig]= fill_nonZero;
221 } else if (val && strcmp(val, "evenodd") == 0) {
222 origWind[curOrig]= fill_oddEven;
223 } else {
224 origWind[curOrig]= fill_nonZero;
225 }
227 originaux[curOrig] = Path_for_item((SPItem *) l->data, true, true);
228 if (originaux[curOrig] == NULL || originaux[curOrig]->descr_cmd.size() <= 1)
229 {
230 for (int i = curOrig; i >= 0; i--) delete originaux[i];
231 g_slist_free(il);
232 return;
233 }
234 curOrig++;
235 }
236 }
237 // reverse if needed
238 // note that the selection list keeps its order
239 if ( reverseOrderForOp ) {
240 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
241 FillRule swai=origWind[0]; origWind[0]=origWind[1]; origWind[1]=swai;
242 }
244 // and work
245 // some temporary instances, first
246 Shape *theShapeA = new Shape;
247 Shape *theShapeB = new Shape;
248 Shape *theShape = new Shape;
249 Path *res = new Path;
250 res->SetBackData(false);
251 Path::cut_position *toCut=NULL;
252 int nbToCut=0;
254 if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) {
255 // true boolean op
256 // get the polygons of each path, with the winding rule specified, and apply the operation iteratively
257 originaux[0]->ConvertWithBackData(0.1);
259 originaux[0]->Fill(theShape, 0);
261 theShapeA->ConvertToShape(theShape, origWind[0]);
263 curOrig = 1;
264 for (GSList *l = il->next; l != NULL; l = l->next) {
265 originaux[curOrig]->ConvertWithBackData(0.1);
267 originaux[curOrig]->Fill(theShape, curOrig);
269 theShapeB->ConvertToShape(theShape, origWind[curOrig]);
271 // les elements arrivent en ordre inverse dans la liste
272 theShape->Booleen(theShapeB, theShapeA, bop);
274 {
275 Shape *swap = theShape;
276 theShape = theShapeA;
277 theShapeA = swap;
278 }
279 curOrig++;
280 }
282 {
283 Shape *swap = theShape;
284 theShape = theShapeA;
285 theShapeA = swap;
286 }
288 } else if ( bop == bool_op_cut ) {
289 // cuts= sort of a bastard boolean operation, thus not the axact same modus operandi
290 // technically, the cut path is not necessarily a polygon (thus has no winding rule)
291 // it is just uncrossed, and cleaned from duplicate edges and points
292 // then it's fed to Booleen() which will uncross it against the other path
293 // then comes the trick: each edge of the cut path is duplicated (one in each direction),
294 // thus making a polygon. the weight of the edges of the cut are all 0, but
295 // the Booleen need to invert the ones inside the source polygon (for the subsequent
296 // ConvertToForme)
298 // the cut path needs to have the highest pathID in the back data
299 // that's how the Booleen() function knows it's an edge of the cut
301 // FIXME: this gives poor results, the final paths are full of extraneous nodes. Decreasing
302 // ConvertWithBackData parameter below simply increases the number of nodes, so for now I
303 // left it at 1.0. Investigate replacing this by a combination of difference and
304 // intersection of the same two paths. -- bb
305 {
306 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
307 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
308 }
309 originaux[0]->ConvertWithBackData(1.0);
311 originaux[0]->Fill(theShape, 0);
313 theShapeA->ConvertToShape(theShape, origWind[0]);
315 originaux[1]->ConvertWithBackData(1.0);
317 originaux[1]->Fill(theShape, 1,false,false,false); //do not closeIfNeeded
319 theShapeB->ConvertToShape(theShape, fill_justDont); // fill_justDont doesn't computes winding numbers
321 // les elements arrivent en ordre inverse dans la liste
322 theShape->Booleen(theShapeB, theShapeA, bool_op_cut, 1);
324 } else if ( bop == bool_op_slice ) {
325 // slice is not really a boolean operation
326 // you just put the 2 shapes in a single polygon, uncross it
327 // the points where the degree is > 2 are intersections
328 // just check it's an intersection on the path you want to cut, and keep it
329 // the intersections you have found are then fed to ConvertPositionsToMoveTo() which will
330 // make new subpath at each one of these positions
331 // inversion pour l'op\8eration
332 {
333 Path* swap=originaux[0];originaux[0]=originaux[1];originaux[1]=swap;
334 int swai=origWind[0];origWind[0]=origWind[1];origWind[1]=(fill_typ)swai;
335 }
336 originaux[0]->ConvertWithBackData(1.0);
338 originaux[0]->Fill(theShapeA, 0,false,false,false); // don't closeIfNeeded
340 originaux[1]->ConvertWithBackData(1.0);
342 originaux[1]->Fill(theShapeA, 1,true,false,false);// don't closeIfNeeded and just dump in the shape, don't reset it
344 theShape->ConvertToShape(theShapeA, fill_justDont);
346 if ( theShape->hasBackData() ) {
347 // should always be the case, but ya never know
348 {
349 for (int i = 0; i < theShape->numberOfPoints(); i++) {
350 if ( theShape->getPoint(i).totalDegree() > 2 ) {
351 // possibly an intersection
352 // we need to check that at least one edge from the source path is incident to it
353 // before we declare it's an intersection
354 int cb = theShape->getPoint(i).incidentEdge[FIRST];
355 int nbOrig=0;
356 int nbOther=0;
357 int piece=-1;
358 float t=0.0;
359 while ( cb >= 0 && cb < theShape->numberOfEdges() ) {
360 if ( theShape->ebData[cb].pathID == 0 ) {
361 // the source has an edge incident to the point, get its position on the path
362 piece=theShape->ebData[cb].pieceID;
363 if ( theShape->getEdge(cb).st == i ) {
364 t=theShape->ebData[cb].tSt;
365 } else {
366 t=theShape->ebData[cb].tEn;
367 }
368 nbOrig++;
369 }
370 if ( theShape->ebData[cb].pathID == 1 ) nbOther++; // the cut is incident to this point
371 cb=theShape->NextAt(i, cb);
372 }
373 if ( nbOrig > 0 && nbOther > 0 ) {
374 // point incident to both path and cut: an intersection
375 // note that you only keep one position on the source; you could have degenerate
376 // cases where the source crosses itself at this point, and you wouyld miss an intersection
377 toCut=(Path::cut_position*)realloc(toCut, (nbToCut+1)*sizeof(Path::cut_position));
378 toCut[nbToCut].piece=piece;
379 toCut[nbToCut].t=t;
380 nbToCut++;
381 }
382 }
383 }
384 }
385 {
386 // i think it's useless now
387 int i = theShape->numberOfEdges() - 1;
388 for (;i>=0;i--) {
389 if ( theShape->ebData[i].pathID == 1 ) {
390 theShape->SubEdge(i);
391 }
392 }
393 }
395 }
396 }
398 int* nesting=NULL;
399 int* conts=NULL;
400 int nbNest=0;
401 // pour compenser le swap juste avant
402 if ( bop == bool_op_slice ) {
403 // theShape->ConvertToForme(res, nbOriginaux, originaux, true);
404 // res->ConvertForcedToMoveTo();
405 res->Copy(originaux[0]);
406 res->ConvertPositionsToMoveTo(nbToCut, toCut); // cut where you found intersections
407 free(toCut);
408 } else if ( bop == bool_op_cut ) {
409 // il faut appeler pour desallouer PointData (pas vital, mais bon)
410 // the Booleen() function did not deallocated the point_data array in theShape, because this
411 // function needs it.
412 // this function uses the point_data to get the winding number of each path (ie: is a hole or not)
413 // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info)
414 theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts);
415 } else {
416 theShape->ConvertToForme(res, nbOriginaux, &originaux[0]);
417 }
419 delete theShape;
420 delete theShapeA;
421 delete theShapeB;
422 for (int i = 0; i < nbOriginaux; i++) delete originaux[i];
424 if (res->descr_cmd.size() <= 1)
425 {
426 // only one command, presumably a moveto: it isn't a path
427 for (GSList *l = il; l != NULL; l = l->next)
428 {
429 SP_OBJECT(l->data)->deleteObject();
430 }
431 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
432 description);
433 selection->clear();
435 delete res;
436 g_slist_free(il);
437 return;
438 }
440 // get the source path object
441 SPObject *source;
442 if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
443 if (reverseOrderForOp) {
444 source = SP_OBJECT(il->data);
445 } else {
446 source = SP_OBJECT(il->next->data);
447 }
448 } else {
449 // find out the bottom object
450 GSList *sorted = g_slist_copy((GSList *) selection->reprList());
452 sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
454 source = sp_desktop_document(desktop)->
455 getObjectByRepr((Inkscape::XML::Node *)sorted->data);
457 g_slist_free(sorted);
458 }
460 // adjust style properties that depend on a possible transform in the source object in order
461 // to get a correct style attribute for the new path
462 SPItem* item_source = SP_ITEM(source);
463 NR::Matrix i2root = from_2geom(sp_item_i2root_affine(item_source));
464 sp_item_adjust_stroke(item_source, NR::expansion(i2root));
465 sp_item_adjust_pattern(item_source, i2root);
466 sp_item_adjust_gradient(item_source, i2root);
467 sp_item_adjust_livepatheffect(item_source, i2root);
469 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
471 // remember important aspects of the source path, to be restored
472 gint pos = repr_source->position();
473 Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
474 gchar const *id = repr_source->attribute("id");
475 gchar const *style = repr_source->attribute("style");
476 gchar const *mask = repr_source->attribute("mask");
477 gchar const *clip_path = repr_source->attribute("clip-path");
479 // remove source paths
480 selection->clear();
481 for (GSList *l = il; l != NULL; l = l->next) {
482 // if this is the bottommost object,
483 if (!strcmp(SP_OBJECT_REPR(l->data)->attribute("id"), id)) {
484 // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
485 SP_OBJECT(l->data)->deleteObject(false);
486 } else {
487 // delete the object for real, so that its clones can take appropriate action
488 SP_OBJECT(l->data)->deleteObject();
489 }
490 }
491 g_slist_free(il);
493 // premultiply by the inverse of parent's repr
494 SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
495 NR::Matrix local = from_2geom(sp_item_i2doc_affine(parent_item));
496 gchar *transform = sp_svg_transform_write(local.inverse());
498 // now that we have the result, add it on the canvas
499 if ( bop == bool_op_cut || bop == bool_op_slice ) {
500 int nbRP=0;
501 Path** resPath;
502 if ( bop == bool_op_slice ) {
503 // there are moveto's at each intersection, but it's still one unique path
504 // so break it down and add each subpath independently
505 // we could call break_apart to do this, but while we have the description...
506 resPath=res->SubPaths(nbRP, false);
507 } else {
508 // cut operation is a bit wicked: you need to keep holes
509 // that's why you needed the nesting
510 // ConvertToFormeNested() dumped all the subpath in a single Path "res", so we need
511 // to get the path for each part of the polygon. that's why you need the nesting info:
512 // to know in wich subpath to add a subpath
513 resPath=res->SubPathsWithNesting(nbRP, true, nbNest, nesting, conts);
515 // cleaning
516 if ( conts ) free(conts);
517 if ( nesting ) free(nesting);
518 }
520 // add all the pieces resulting from cut or slice
521 for (int i=0;i<nbRP;i++) {
522 gchar *d = resPath[i]->svg_dump_path();
524 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
525 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
526 repr->setAttribute("style", style);
527 if (mask)
528 repr->setAttribute("mask", mask);
529 if (clip_path)
530 repr->setAttribute("clip-path", clip_path);
532 repr->setAttribute("d", d);
533 g_free(d);
535 // for slice, remove fill
536 if (bop == bool_op_slice) {
537 SPCSSAttr *css;
539 css = sp_repr_css_attr_new();
540 sp_repr_css_set_property(css, "fill", "none");
542 sp_repr_css_change(repr, css, "style");
544 sp_repr_css_attr_unref(css);
545 }
547 // we assign the same id on all pieces, but it on adding to document, it will be changed on all except one
548 // this means it's basically random which of the pieces inherits the original's id and clones
549 // a better algorithm might figure out e.g. the biggest piece
550 repr->setAttribute("id", id);
552 repr->setAttribute("transform", transform);
554 // add the new repr to the parent
555 parent->appendChild(repr);
557 // move to the saved position
558 repr->setPosition(pos > 0 ? pos : 0);
560 selection->add(repr);
561 Inkscape::GC::release(repr);
563 delete resPath[i];
564 }
565 if ( resPath ) free(resPath);
567 } else {
568 gchar *d = res->svg_dump_path();
570 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
571 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
572 repr->setAttribute("style", style);
574 if ( mask )
575 repr->setAttribute("mask", mask);
577 if ( clip_path )
578 repr->setAttribute("clip-path", clip_path);
580 repr->setAttribute("d", d);
581 g_free(d);
583 repr->setAttribute("transform", transform);
585 repr->setAttribute("id", id);
586 parent->appendChild(repr);
587 repr->setPosition(pos > 0 ? pos : 0);
589 selection->add(repr);
590 Inkscape::GC::release(repr);
591 }
593 g_free(transform);
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, NR::scale stroke_scale, NR::Matrix transform,
604 Inkscape::XML::Node *g_repr, Inkscape::XML::Document *xml_doc, SPDocument * doc )
605 {
606 SPMarker* marker = SP_MARKER (marker_object);
607 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker_object));
609 NR::Matrix tr(from_2geom(marker_transform));
611 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
612 tr = stroke_scale * tr;
613 }
615 // total marker transform
616 tr = marker_item->transform * marker->c2p * tr * transform;
618 if (SP_OBJECT_REPR(marker_item)) {
619 Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
620 g_repr->appendChild(m_repr);
621 SPItem *marker_item = (SPItem *) doc->getObjectByRepr(m_repr);
622 sp_item_write_transform(marker_item, m_repr, tr);
623 }
624 }
626 void
627 sp_selected_path_outline()
628 {
629 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
631 Inkscape::Selection *selection = sp_desktop_selection(desktop);
633 if (selection->isEmpty()) {
634 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
635 return;
636 }
638 bool did = false;
640 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
641 items != NULL;
642 items = items->next) {
644 SPItem *item = (SPItem *) items->data;
646 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
647 continue;
649 SPCurve *curve = NULL;
650 if (SP_IS_SHAPE(item)) {
651 curve = sp_shape_get_curve(SP_SHAPE(item));
652 if (curve == NULL)
653 continue;
654 }
655 if (SP_IS_TEXT(item)) {
656 curve = SP_TEXT(item)->getNormalizedBpath();
657 if (curve == NULL)
658 continue;
659 }
661 // pas de stroke pas de chocolat
662 if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
663 curve->unref();
664 continue;
665 }
667 // remember old stroke style, to be set on fill
668 SPStyle *i_style = SP_OBJECT_STYLE(item);
669 SPCSSAttr *ncss;
670 {
671 ncss = sp_css_attr_from_style(i_style, SP_STYLE_FLAG_ALWAYS);
672 gchar const *s_val = sp_repr_css_property(ncss, "stroke", NULL);
673 gchar const *s_opac = sp_repr_css_property(ncss, "stroke-opacity", NULL);
675 sp_repr_css_set_property(ncss, "stroke", "none");
676 sp_repr_css_set_property(ncss, "stroke-opacity", "1.0");
677 sp_repr_css_set_property(ncss, "fill", s_val);
678 if ( s_opac ) {
679 sp_repr_css_set_property(ncss, "fill-opacity", s_opac);
680 } else {
681 sp_repr_css_set_property(ncss, "fill-opacity", "1.0");
682 }
683 sp_repr_css_unset_property(ncss, "marker-start");
684 sp_repr_css_unset_property(ncss, "marker-mid");
685 sp_repr_css_unset_property(ncss, "marker-end");
686 }
688 NR::Matrix const transform(item->transform);
689 float const scale = NR::expansion(transform);
690 gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
691 gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
693 float o_width, o_miter;
694 JoinType o_join;
695 ButtType o_butt;
697 {
698 int jointype, captype;
700 jointype = i_style->stroke_linejoin.computed;
701 captype = i_style->stroke_linecap.computed;
702 o_width = i_style->stroke_width.computed;
704 switch (jointype) {
705 case SP_STROKE_LINEJOIN_MITER:
706 o_join = join_pointy;
707 break;
708 case SP_STROKE_LINEJOIN_ROUND:
709 o_join = join_round;
710 break;
711 default:
712 o_join = join_straight;
713 break;
714 }
716 switch (captype) {
717 case SP_STROKE_LINECAP_SQUARE:
718 o_butt = butt_square;
719 break;
720 case SP_STROKE_LINECAP_ROUND:
721 o_butt = butt_round;
722 break;
723 default:
724 o_butt = butt_straight;
725 break;
726 }
728 if (o_width < 0.1)
729 o_width = 0.1;
730 o_miter = i_style->stroke_miterlimit.value * o_width;
731 }
733 Path *orig = Path_for_item(item, false);
734 if (orig == NULL) {
735 curve->unref();
736 continue;
737 }
739 Path *res = new Path;
740 res->SetBackData(false);
742 if (i_style->stroke_dash.n_dash) {
743 // For dashed strokes, use Stroke method, because Outline can't do dashes
744 // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround
746 orig->ConvertWithBackData(0.1);
748 orig->DashPolylineFromStyle(i_style, scale, 0);
750 Shape* theShape = new Shape;
751 orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt,
752 0.5 * o_miter);
753 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
755 Shape *theRes = new Shape;
757 theRes->ConvertToShape(theShape, fill_positive);
759 Path *originaux[1];
760 originaux[0] = res;
761 theRes->ConvertToForme(orig, 1, originaux);
763 res->Coalesce(5.0);
765 delete theShape;
766 delete theRes;
768 } else {
770 orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter);
772 orig->Coalesce(0.5 * o_width);
774 Shape *theShape = new Shape;
775 Shape *theRes = new Shape;
777 res->ConvertWithBackData(1.0);
778 res->Fill(theShape, 0);
779 theRes->ConvertToShape(theShape, fill_positive);
781 Path *originaux[1];
782 originaux[0] = res;
783 theRes->ConvertToForme(orig, 1, originaux);
785 delete theShape;
786 delete theRes;
787 }
789 if (orig->descr_cmd.size() <= 1) {
790 // ca a merd\8e, ou bien le resultat est vide
791 delete res;
792 delete orig;
793 continue;
794 }
796 did = true;
798 // remember the position of the item
799 gint pos = SP_OBJECT_REPR(item)->position();
800 // remember parent
801 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
802 // remember id
803 char const *id = SP_OBJECT_REPR(item)->attribute("id");
805 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
807 SPDocument * doc = sp_desktop_document(desktop);
808 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
809 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
811 // restore old style, but set old stroke style on fill
812 sp_repr_css_change(repr, ncss, "style");
814 sp_repr_css_attr_unref(ncss);
816 gchar *str = orig->svg_dump_path();
817 repr->setAttribute("d", str);
818 g_free(str);
820 if (mask)
821 repr->setAttribute("mask", mask);
822 if (clip_path)
823 repr->setAttribute("clip-path", clip_path);
825 if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
827 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
828 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
830 // add the group to the parent
831 parent->appendChild(g_repr);
832 // move to the saved position
833 g_repr->setPosition(pos > 0 ? pos : 0);
835 g_repr->appendChild(repr);
836 repr->setAttribute("id", id);
837 SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr);
838 sp_item_write_transform(newitem, repr, transform);
840 SPShape *shape = SP_SHAPE(item);
842 Geom::PathVector const & pathv = curve->get_pathvector();
843 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
844 if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_START] ) {
845 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
846 sp_selected_path_outline_add_marker( marker_obj, m,
847 NR::scale(i_style->stroke_width.computed), transform,
848 g_repr, xml_doc, doc );
849 }
851 if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_MID] ) {
852 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
853 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
854 while (curve_it2 != path_it->end_default())
855 {
856 /* Put marker between curve_it1 and curve_it2.
857 * Loop to end_default (so including closing segment), because when a path is closed,
858 * there should be a midpoint marker between last segment and closing straight line segment
859 */
860 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
861 sp_selected_path_outline_add_marker( marker_obj, m,
862 NR::scale(i_style->stroke_width.computed), transform,
863 g_repr, xml_doc, doc );
865 ++curve_it1;
866 ++curve_it2;
867 }
868 }
870 if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_END] ) {
871 Geom::Matrix const m (sp_shape_marker_get_transform_at_end(path_it->back_default()));
872 sp_selected_path_outline_add_marker( marker_obj, m,
873 NR::scale(i_style->stroke_width.computed), transform,
874 g_repr, xml_doc, doc );
875 }
876 }
878 selection->add(g_repr);
880 Inkscape::GC::release(g_repr);
883 } else {
885 // add the new repr to the parent
886 parent->appendChild(repr);
888 // move to the saved position
889 repr->setPosition(pos > 0 ? pos : 0);
891 repr->setAttribute("id", id);
893 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
894 sp_item_write_transform(newitem, repr, transform);
896 selection->add(repr);
898 }
900 Inkscape::GC::release(repr);
902 curve->unref();
903 selection->remove(item);
904 SP_OBJECT(item)->deleteObject(false);
906 }
908 delete res;
909 delete orig;
910 }
912 if (did) {
913 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE,
914 _("Convert stroke to path"));
915 } else {
916 // TRANSLATORS: "to outline" means "to convert stroke to path"
917 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
918 return;
919 }
920 }
923 void
924 sp_selected_path_offset()
925 {
926 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
928 sp_selected_path_do_offset(true, prefOffset);
929 }
930 void
931 sp_selected_path_inset()
932 {
933 double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0);
935 sp_selected_path_do_offset(false, prefOffset);
936 }
938 void
939 sp_selected_path_offset_screen(double pixels)
940 {
941 sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom());
942 }
944 void
945 sp_selected_path_inset_screen(double pixels)
946 {
947 sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom());
948 }
951 void sp_selected_path_create_offset_object_zero()
952 {
953 sp_selected_path_create_offset_object(0, false);
954 }
956 void sp_selected_path_create_offset()
957 {
958 sp_selected_path_create_offset_object(1, false);
959 }
960 void sp_selected_path_create_inset()
961 {
962 sp_selected_path_create_offset_object(-1, false);
963 }
965 void sp_selected_path_create_updating_offset_object_zero()
966 {
967 sp_selected_path_create_offset_object(0, true);
968 }
970 void sp_selected_path_create_updating_offset()
971 {
972 sp_selected_path_create_offset_object(1, true);
973 }
974 void sp_selected_path_create_updating_inset()
975 {
976 sp_selected_path_create_offset_object(-1, true);
977 }
979 void
980 sp_selected_path_create_offset_object(int expand, bool updating)
981 {
982 Inkscape::Selection *selection;
983 Inkscape::XML::Node *repr;
984 SPItem *item;
985 SPCurve *curve;
986 gchar *style, *str;
987 SPDesktop *desktop;
988 float o_width, o_miter;
989 JoinType o_join;
990 ButtType o_butt;
992 curve = NULL;
994 desktop = SP_ACTIVE_DESKTOP;
996 selection = sp_desktop_selection(desktop);
998 item = selection->singleItem();
1000 if (item == NULL || ( !SP_IS_SHAPE(item) && !SP_IS_TEXT(item) ) ) {
1001 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Selected object is <b>not a path</b>, cannot inset/outset."));
1002 return;
1003 }
1004 if (SP_IS_SHAPE(item))
1005 {
1006 curve = sp_shape_get_curve(SP_SHAPE(item));
1007 if (curve == NULL)
1008 return;
1009 }
1010 if (SP_IS_TEXT(item))
1011 {
1012 curve = SP_TEXT(item)->getNormalizedBpath();
1013 if (curve == NULL)
1014 return;
1015 }
1017 NR::Matrix const transform(item->transform);
1019 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1021 style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
1023 // remember the position of the item
1024 gint pos = SP_OBJECT_REPR(item)->position();
1025 // remember parent
1026 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1028 {
1029 SPStyle *i_style = SP_OBJECT(item)->style;
1030 int jointype, captype;
1032 jointype = i_style->stroke_linejoin.value;
1033 captype = i_style->stroke_linecap.value;
1034 o_width = i_style->stroke_width.computed;
1035 if (jointype == SP_STROKE_LINEJOIN_MITER)
1036 {
1037 o_join = join_pointy;
1038 }
1039 else if (jointype == SP_STROKE_LINEJOIN_ROUND)
1040 {
1041 o_join = join_round;
1042 }
1043 else
1044 {
1045 o_join = join_straight;
1046 }
1047 if (captype == SP_STROKE_LINECAP_SQUARE)
1048 {
1049 o_butt = butt_square;
1050 }
1051 else if (captype == SP_STROKE_LINECAP_ROUND)
1052 {
1053 o_butt = butt_round;
1054 }
1055 else
1056 {
1057 o_butt = butt_straight;
1058 }
1060 {
1061 double prefOffset = 1.0;
1062 prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset);
1063 o_width = prefOffset;
1064 }
1066 if (o_width < 0.01)
1067 o_width = 0.01;
1068 o_miter = i_style->stroke_miterlimit.value * o_width;
1069 }
1071 Path *orig = Path_for_item(item, true, false);
1072 if (orig == NULL)
1073 {
1074 g_free(style);
1075 curve->unref();
1076 return;
1077 }
1079 Path *res = new Path;
1080 res->SetBackData(false);
1082 {
1083 Shape *theShape = new Shape;
1084 Shape *theRes = new Shape;
1086 orig->ConvertWithBackData(1.0);
1087 orig->Fill(theShape, 0);
1089 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1090 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1091 if (val && strcmp(val, "nonzero") == 0)
1092 {
1093 theRes->ConvertToShape(theShape, fill_nonZero);
1094 }
1095 else if (val && strcmp(val, "evenodd") == 0)
1096 {
1097 theRes->ConvertToShape(theShape, fill_oddEven);
1098 }
1099 else
1100 {
1101 theRes->ConvertToShape(theShape, fill_nonZero);
1102 }
1104 Path *originaux[1];
1105 originaux[0] = orig;
1106 theRes->ConvertToForme(res, 1, originaux);
1108 delete theShape;
1109 delete theRes;
1110 }
1112 curve->unref();
1114 if (res->descr_cmd.size() <= 1)
1115 {
1116 // pas vraiment de points sur le resultat
1117 // donc il ne reste rien
1118 sp_document_done(sp_desktop_document(desktop),
1119 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1120 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1121 (updating ? _("Create linked offset")
1122 : _("Create dynamic offset")));
1123 selection->clear();
1125 delete res;
1126 delete orig;
1127 g_free(style);
1128 return;
1129 }
1131 {
1132 gchar tstr[80];
1134 tstr[79] = '\0';
1136 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1137 repr = xml_doc->createElement("svg:path");
1138 repr->setAttribute("sodipodi:type", "inkscape:offset");
1139 sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
1140 ? o_width
1141 : expand < 0
1142 ? -o_width
1143 : 0 ));
1145 str = res->svg_dump_path();
1146 repr->setAttribute("inkscape:original", str);
1147 g_free(str);
1149 if ( updating ) {
1150 char const *id = SP_OBJECT(item)->repr->attribute("id");
1151 char const *uri = g_strdup_printf("#%s", id);
1152 repr->setAttribute("xlink:href", uri);
1153 g_free((void *) uri);
1154 } else {
1155 repr->setAttribute("inkscape:href", NULL);
1156 }
1158 repr->setAttribute("style", style);
1160 // add the new repr to the parent
1161 parent->appendChild(repr);
1163 // move to the saved position
1164 repr->setPosition(pos > 0 ? pos : 0);
1166 SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1168 if ( updating ) {
1169 // on conserve l'original
1170 // we reapply the transform to the original (offset will feel it)
1171 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
1172 } else {
1173 // delete original, apply the transform to the offset
1174 SP_OBJECT(item)->deleteObject(false);
1175 sp_item_write_transform(nitem, repr, transform);
1176 }
1178 // The object just created from a temporary repr is only a seed.
1179 // We need to invoke its write which will update its real repr (in particular adding d=)
1180 SP_OBJECT(nitem)->updateRepr();
1182 Inkscape::GC::release(repr);
1184 selection->set(nitem);
1185 }
1187 sp_document_done(sp_desktop_document(desktop),
1188 (updating ? SP_VERB_SELECTION_LINKED_OFFSET
1189 : SP_VERB_SELECTION_DYNAMIC_OFFSET),
1190 (updating ? _("Create linked offset")
1191 : _("Create dynamic offset")));
1193 delete res;
1194 delete orig;
1196 g_free(style);
1197 }
1210 void
1211 sp_selected_path_do_offset(bool expand, double prefOffset)
1212 {
1213 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1215 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1217 if (selection->isEmpty()) {
1218 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to inset/outset."));
1219 return;
1220 }
1222 bool did = false;
1224 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1225 items != NULL;
1226 items = items->next) {
1228 SPItem *item = (SPItem *) items->data;
1230 if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
1231 continue;
1233 SPCurve *curve = NULL;
1234 if (SP_IS_SHAPE(item)) {
1235 curve = sp_shape_get_curve(SP_SHAPE(item));
1236 if (curve == NULL)
1237 continue;
1238 }
1239 if (SP_IS_TEXT(item)) {
1240 curve = SP_TEXT(item)->getNormalizedBpath();
1241 if (curve == NULL)
1242 continue;
1243 }
1245 NR::Matrix const transform(item->transform);
1247 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1249 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1251 float o_width, o_miter;
1252 JoinType o_join;
1253 ButtType o_butt;
1255 {
1256 SPStyle *i_style = SP_OBJECT(item)->style;
1257 int jointype, captype;
1259 jointype = i_style->stroke_linejoin.value;
1260 captype = i_style->stroke_linecap.value;
1261 o_width = i_style->stroke_width.computed;
1263 switch (jointype) {
1264 case SP_STROKE_LINEJOIN_MITER:
1265 o_join = join_pointy;
1266 break;
1267 case SP_STROKE_LINEJOIN_ROUND:
1268 o_join = join_round;
1269 break;
1270 default:
1271 o_join = join_straight;
1272 break;
1273 }
1275 switch (captype) {
1276 case SP_STROKE_LINECAP_SQUARE:
1277 o_butt = butt_square;
1278 break;
1279 case SP_STROKE_LINECAP_ROUND:
1280 o_butt = butt_round;
1281 break;
1282 default:
1283 o_butt = butt_straight;
1284 break;
1285 }
1287 o_width = prefOffset;
1289 if (o_width < 0.1)
1290 o_width = 0.1;
1291 o_miter = i_style->stroke_miterlimit.value * o_width;
1292 }
1294 Path *orig = Path_for_item(item, false);
1295 if (orig == NULL) {
1296 g_free(style);
1297 curve->unref();
1298 continue;
1299 }
1301 Path *res = new Path;
1302 res->SetBackData(false);
1304 {
1305 Shape *theShape = new Shape;
1306 Shape *theRes = new Shape;
1308 orig->ConvertWithBackData(0.03);
1309 orig->Fill(theShape, 0);
1311 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
1312 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
1313 if (val && strcmp(val, "nonzero") == 0)
1314 {
1315 theRes->ConvertToShape(theShape, fill_nonZero);
1316 }
1317 else if (val && strcmp(val, "evenodd") == 0)
1318 {
1319 theRes->ConvertToShape(theShape, fill_oddEven);
1320 }
1321 else
1322 {
1323 theRes->ConvertToShape(theShape, fill_nonZero);
1324 }
1326 // et maintenant: offset
1327 // methode inexacte
1328 /* Path *originaux[1];
1329 originaux[0] = orig;
1330 theRes->ConvertToForme(res, 1, originaux);
1332 if (expand) {
1333 res->OutsideOutline(orig, 0.5 * o_width, o_join, o_butt, o_miter);
1334 } else {
1335 res->OutsideOutline(orig, -0.5 * o_width, o_join, o_butt, o_miter);
1336 }
1338 orig->ConvertWithBackData(1.0);
1339 orig->Fill(theShape, 0);
1340 theRes->ConvertToShape(theShape, fill_positive);
1341 originaux[0] = orig;
1342 theRes->ConvertToForme(res, 1, originaux);
1344 if (o_width >= 0.5) {
1345 // res->Coalesce(1.0);
1346 res->ConvertEvenLines(1.0);
1347 res->Simplify(1.0);
1348 } else {
1349 // res->Coalesce(o_width);
1350 res->ConvertEvenLines(1.0*o_width);
1351 res->Simplify(1.0 * o_width);
1352 } */
1353 // methode par makeoffset
1355 if (expand)
1356 {
1357 theShape->MakeOffset(theRes, o_width, o_join, o_miter);
1358 }
1359 else
1360 {
1361 theShape->MakeOffset(theRes, -o_width, o_join, o_miter);
1362 }
1363 theRes->ConvertToShape(theShape, fill_positive);
1365 res->Reset();
1366 theRes->ConvertToForme(res);
1368 if (o_width >= 1.0)
1369 {
1370 res->ConvertEvenLines(1.0);
1371 res->Simplify(1.0);
1372 }
1373 else
1374 {
1375 res->ConvertEvenLines(1.0*o_width);
1376 res->Simplify(1.0 * o_width);
1377 }
1379 delete theShape;
1380 delete theRes;
1381 }
1383 did = true;
1385 curve->unref();
1386 // remember the position of the item
1387 gint pos = SP_OBJECT_REPR(item)->position();
1388 // remember parent
1389 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1390 // remember id
1391 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1393 selection->remove(item);
1394 SP_OBJECT(item)->deleteObject(false);
1396 if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
1398 gchar tstr[80];
1400 tstr[79] = '\0';
1402 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1403 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1405 repr->setAttribute("style", style);
1407 gchar *str = res->svg_dump_path();
1408 repr->setAttribute("d", str);
1409 g_free(str);
1411 // add the new repr to the parent
1412 parent->appendChild(repr);
1414 // move to the saved position
1415 repr->setPosition(pos > 0 ? pos : 0);
1417 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1419 // reapply the transform
1420 sp_item_write_transform(newitem, repr, transform);
1422 repr->setAttribute("id", id);
1424 selection->add(repr);
1426 Inkscape::GC::release(repr);
1427 }
1429 delete orig;
1430 delete res;
1431 }
1433 if (did) {
1434 sp_document_done(sp_desktop_document(desktop),
1435 (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
1436 (expand ? _("Outset path") : _("Inset path")));
1437 } else {
1438 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
1439 return;
1440 }
1441 }
1444 static bool
1445 sp_selected_path_simplify_items(SPDesktop *desktop,
1446 Inkscape::Selection *selection, GSList *items,
1447 float threshold, bool justCoalesce,
1448 float angleLimit, bool breakableAngles,
1449 bool modifySelection);
1452 //return true if we changed something, else false
1453 bool
1454 sp_selected_path_simplify_item(SPDesktop *desktop,
1455 Inkscape::Selection *selection, SPItem *item,
1456 float threshold, bool justCoalesce,
1457 float angleLimit, bool breakableAngles,
1458 gdouble size, bool modifySelection)
1459 {
1460 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1461 return false;
1463 //If this is a group, do the children instead
1464 if (SP_IS_GROUP(item)) {
1465 GSList *items = sp_item_group_item_list(SP_GROUP(item));
1467 return sp_selected_path_simplify_items(desktop, selection, items,
1468 threshold, justCoalesce,
1469 angleLimit, breakableAngles,
1470 false);
1471 }
1474 SPCurve *curve = NULL;
1476 if (SP_IS_SHAPE(item)) {
1477 curve = sp_shape_get_curve(SP_SHAPE(item));
1478 if (!curve)
1479 return false;
1480 }
1482 if (SP_IS_TEXT(item)) {
1483 curve = SP_TEXT(item)->getNormalizedBpath();
1484 if (!curve)
1485 return false;
1486 }
1488 // save the transform, to re-apply it after simplification
1489 NR::Matrix const transform(item->transform);
1491 /*
1492 reset the transform, effectively transforming the item by transform.inverse();
1493 this is necessary so that the item is transformed twice back and forth,
1494 allowing all compensations to cancel out regardless of the preferences
1495 */
1496 sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
1498 gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
1499 gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
1500 gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
1502 Path *orig = Path_for_item(item, false);
1503 if (orig == NULL) {
1504 g_free(style);
1505 curve->unref();
1506 return false;
1507 }
1509 curve->unref();
1510 // remember the position of the item
1511 gint pos = SP_OBJECT_REPR(item)->position();
1512 // remember parent
1513 Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
1514 // remember id
1515 char const *id = SP_OBJECT_REPR(item)->attribute("id");
1516 // remember path effect
1517 char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
1519 //If a group was selected, to not change the selection list
1520 if (modifySelection)
1521 selection->remove(item);
1523 SP_OBJECT(item)->deleteObject(false);
1525 if ( justCoalesce ) {
1526 orig->Coalesce(threshold * size);
1527 } else {
1528 orig->ConvertEvenLines(threshold * size);
1529 orig->Simplify(threshold * size);
1530 }
1532 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
1533 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1535 // restore style, mask and clip-path
1536 repr->setAttribute("style", style);
1537 g_free(style);
1539 if ( mask ) {
1540 repr->setAttribute("mask", mask);
1541 g_free(mask);
1542 }
1544 if ( clip_path ) {
1545 repr->setAttribute("clip-path", clip_path);
1546 g_free(clip_path);
1547 }
1549 // path
1550 gchar *str = orig->svg_dump_path();
1551 if (patheffect)
1552 repr->setAttribute("inkscape:original-d", str);
1553 else
1554 repr->setAttribute("d", str);
1555 g_free(str);
1557 // restore id
1558 repr->setAttribute("id", id);
1560 // add the new repr to the parent
1561 parent->appendChild(repr);
1563 // move to the saved position
1564 repr->setPosition(pos > 0 ? pos : 0);
1566 SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
1568 // reapply the transform
1569 sp_item_write_transform(newitem, repr, transform);
1571 // restore path effect
1572 repr->setAttribute("inkscape:path-effect", patheffect);
1574 //If we are not in a selected group
1575 if (modifySelection)
1576 selection->add(repr);
1578 Inkscape::GC::release(repr);
1580 // clean up
1581 if (orig) delete orig;
1583 return true;
1584 }
1587 bool
1588 sp_selected_path_simplify_items(SPDesktop *desktop,
1589 Inkscape::Selection *selection, GSList *items,
1590 float threshold, bool justCoalesce,
1591 float angleLimit, bool breakableAngles,
1592 bool modifySelection)
1593 {
1594 bool simplifyIndividualPaths =
1595 (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0);
1597 gchar *simplificationType;
1598 if (simplifyIndividualPaths) {
1599 simplificationType = _("Simplifying paths (separately):");
1600 } else {
1601 simplificationType = _("Simplifying paths:");
1602 }
1604 bool didSomething = false;
1606 NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
1607 if (!selectionBbox) {
1608 return false;
1609 }
1610 gdouble selectionSize = L2(selectionBbox->dimensions());
1612 gdouble simplifySize = selectionSize;
1614 int pathsSimplified = 0;
1615 int totalPathCount = g_slist_length(items);
1617 // set "busy" cursor
1618 desktop->setWaitingCursor();
1620 for (; items != NULL; items = items->next) {
1621 SPItem *item = (SPItem *) items->data;
1623 if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
1624 continue;
1626 if (simplifyIndividualPaths) {
1627 NR::Maybe<NR::Rect> itemBbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
1628 if (itemBbox) {
1629 simplifySize = L2(itemBbox->dimensions());
1630 } else {
1631 simplifySize = 0;
1632 }
1633 }
1635 pathsSimplified++;
1637 if (pathsSimplified % 20 == 0) {
1638 gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
1639 desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
1640 }
1642 didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
1643 threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
1644 }
1646 desktop->clearWaitingCursor();
1648 if (pathsSimplified > 20) {
1649 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
1650 }
1652 return didSomething;
1653 }
1655 void
1656 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
1657 float angleLimit, bool breakableAngles)
1658 {
1659 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1661 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1663 if (selection->isEmpty()) {
1664 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1665 _("Select <b>path(s)</b> to simplify."));
1666 return;
1667 }
1669 GSList *items = g_slist_copy((GSList *) selection->itemList());
1671 bool didSomething = sp_selected_path_simplify_items(desktop, selection,
1672 items, threshold,
1673 justCoalesce,
1674 angleLimit,
1675 breakableAngles, true);
1677 if (didSomething)
1678 sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY,
1679 _("Simplify"));
1680 else
1681 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
1683 }
1686 // globals for keeping track of accelerated simplify
1687 static double previousTime = 0.0;
1688 static gdouble simplifyMultiply = 1.0;
1690 void
1691 sp_selected_path_simplify(void)
1692 {
1693 gdouble simplifyThreshold =
1694 prefs_get_double_attribute("options.simplifythreshold", "value", 0.003);
1695 bool simplifyJustCoalesce =
1696 (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0);
1698 //Get the current time
1699 GTimeVal currentTimeVal;
1700 g_get_current_time(¤tTimeVal);
1701 double currentTime = currentTimeVal.tv_sec * 1000000 +
1702 currentTimeVal.tv_usec;
1704 //Was the previous call to this function recent? (<0.5 sec)
1705 if (previousTime > 0.0 && currentTime - previousTime < 500000.0) {
1707 // add to the threshold 1/2 of its original value
1708 simplifyMultiply += 0.5;
1709 simplifyThreshold *= simplifyMultiply;
1711 } else {
1712 // reset to the default
1713 simplifyMultiply = 1;
1714 }
1716 //remember time for next call
1717 previousTime = currentTime;
1719 //g_print("%g\n", simplify_threshold);
1721 //Make the actual call
1722 sp_selected_path_simplify_selection(simplifyThreshold,
1723 simplifyJustCoalesce, 0.0, false);
1724 }
1728 // fonctions utilitaires
1730 bool
1731 Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
1732 {
1733 if (who == NULL || a == NULL)
1734 return false;
1735 if (who == a)
1736 return true;
1737 return Ancetre(sp_repr_parent(a), who);
1738 }
1740 Path *
1741 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
1742 {
1743 SPCurve *curve = curve_for_item(item);
1744 NArtBpath *bpath = bpath_for_curve(item, curve, doTransformation, transformFull, NR::identity(), NR::identity());
1746 if (bpath == NULL) {
1747 return NULL;
1748 }
1750 Geom::PathVector pathv = pathvector_for_curve(item, curve, doTransformation, transformFull);
1752 Path *dest = new Path;
1753 dest->LoadPathVector(pathv);
1755 curve->unref();
1757 return dest;
1758 }
1760 /*
1761 * This function always returns a new NArtBpath, the caller must g_free the returned path!
1762 */
1763 NArtBpath *
1764 bpath_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, NR::Matrix extraPreAffine, NR::Matrix extraPostAffine)
1765 {
1766 if (curve == NULL)
1767 return NULL;
1769 NArtBpath *bpath = BPath_from_2GeomPath(curve->get_pathvector());
1770 if (bpath == NULL) {
1771 return NULL;
1772 }
1774 NArtBpath *new_bpath; // we will get a duplicate which has to be freed at some point!
1775 if (doTransformation) {
1776 if (transformFull) {
1777 new_bpath = nr_artpath_affine(bpath, extraPreAffine * from_2geom(sp_item_i2doc_affine(item)) * extraPostAffine);
1778 } else {
1779 new_bpath = nr_artpath_affine(bpath, extraPreAffine * item->transform * extraPostAffine);
1780 }
1781 } else {
1782 new_bpath = nr_artpath_affine(bpath, extraPreAffine * NR::identity() * extraPostAffine);
1783 }
1785 g_free(bpath);
1786 return new_bpath;
1787 }
1789 /*
1790 * NOTE: Returns empty pathvector if curve == NULL
1791 * TODO: see if calling this method can be optimized. All the pathvector copying might be slow.
1792 */
1793 Geom::PathVector
1794 pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull)
1795 {
1796 if (curve == NULL)
1797 return Geom::PathVector();
1799 if (doTransformation) {
1800 if (transformFull) {
1801 return (curve->get_pathvector()) * sp_item_i2doc_affine(item);
1802 } else {
1803 return (curve->get_pathvector()) * to_2geom(item->transform);
1804 }
1805 } else {
1806 return curve->get_pathvector();
1807 }
1808 }
1810 SPCurve* curve_for_item(SPItem *item)
1811 {
1812 if (!item) {
1813 return NULL;
1814 }
1816 SPCurve *curve = NULL;
1817 if (SP_IS_SHAPE(item)) {
1818 if (SP_IS_PATH(item)) {
1819 curve = sp_path_get_curve_for_edit(SP_PATH(item));
1820 } else {
1821 curve = sp_shape_get_curve(SP_SHAPE(item));
1822 }
1823 }
1824 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
1825 {
1826 curve = te_get_layout(item)->convertToCurves();
1827 }
1828 else if (SP_IS_IMAGE(item))
1829 {
1830 curve = sp_image_get_curve(SP_IMAGE(item));
1831 }
1833 return curve; // do not forget to unref the curve at some point!
1834 }
1836 Path *bpath_to_Path(NArtBpath const *bpath) {
1837 Path *dest = new Path;
1838 dest->SetBackData(false);
1839 {
1840 int i;
1841 bool closed = false;
1842 float lastX = 0.0;
1843 float lastY = 0.0;
1845 for (i = 0; bpath[i].code != NR_END; i++) {
1846 switch (bpath[i].code) {
1847 case NR_LINETO:
1848 lastX = bpath[i].x3;
1849 lastY = bpath[i].y3;
1850 {
1851 NR::Point tmp(lastX, lastY);
1852 dest->LineTo(tmp);
1853 }
1854 break;
1856 case NR_CURVETO:
1857 {
1858 NR::Point tmp, tms, tme;
1859 tmp[0]=bpath[i].x3;
1860 tmp[1]=bpath[i].y3;
1861 tms[0]=3 * (bpath[i].x1 - lastX);
1862 tms[1]=3 * (bpath[i].y1 - lastY);
1863 tme[0]=3 * (bpath[i].x3 - bpath[i].x2);
1864 tme[1]=3 * (bpath[i].y3 - bpath[i].y2);
1865 dest->CubicTo(tmp,tms,tme);
1866 }
1867 lastX = bpath[i].x3;
1868 lastY = bpath[i].y3;
1869 break;
1871 case NR_MOVETO_OPEN:
1872 case NR_MOVETO:
1873 if (closed)
1874 dest->Close();
1875 closed = (bpath[i].code == NR_MOVETO);
1876 lastX = bpath[i].x3;
1877 lastY = bpath[i].y3;
1878 {
1879 NR::Point tmp(lastX, lastY);
1880 dest->MoveTo(tmp);
1881 }
1882 break;
1883 default:
1884 break;
1885 }
1886 }
1887 if (closed)
1888 dest->Close();
1889 }
1890 return dest;
1891 }
1893 NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p, unsigned seg)
1894 {
1895 //get nearest position on path
1896 Path::cut_position pos = path->PointToCurvilignPosition(p, seg);
1897 return pos;
1898 }
1900 NR::Point get_point_on_Path(Path *path, int piece, double t)
1901 {
1902 NR::Point p;
1903 path->PointAt(piece, t, p);
1904 return p;
1905 }
1908 /*
1909 Local Variables:
1910 mode:c++
1911 c-file-style:"stroustrup"
1912 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1913 indent-tabs-mode:nil
1914 fill-column:99
1915 End:
1916 */
1917 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :