X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsplivarot.cpp;h=f1ce029f4f36b95c17bcb3c718e2bf9234dbd678;hb=b87ac1b021756da7aba18d8c6bed38cc031ee0d4;hp=065533f71395a5be9635ad8c43bce94f66b638d6;hpb=eab70b2c7444eed4cd5c671378fc26fe11e89674;p=inkscape.git diff --git a/src/splivarot.cpp b/src/splivarot.cpp index 065533f71..f1ce029f4 100644 --- a/src/splivarot.cpp +++ b/src/splivarot.cpp @@ -17,9 +17,14 @@ # include #endif +#include +#include #include "xml/repr.h" #include "svg/svg.h" #include "sp-path.h" +#include "sp-shape.h" +#include "marker.h" +#include "enums.h" #include "sp-text.h" #include "sp-item-group.h" #include "style.h" @@ -39,6 +44,11 @@ #include "xml/repr.h" #include "xml/repr-sorting.h" +#include +#include +#include +#include + #include "livarot/Path.h" #include "livarot/Shape.h" @@ -46,60 +56,71 @@ bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who); -void sp_selected_path_boolop(bool_op bop); +void sp_selected_path_boolop(bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description=""); void sp_selected_path_do_offset(bool expand, double prefOffset); void sp_selected_path_create_offset_object(int expand, bool updating); void sp_selected_path_union() { - sp_selected_path_boolop(bool_op_union); + sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union")); +} + +void +sp_selected_path_union_skip_undo() +{ + sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union")); } void sp_selected_path_intersect() { - sp_selected_path_boolop(bool_op_inters); + sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection")); } void sp_selected_path_diff() { - sp_selected_path_boolop(bool_op_diff); + sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference")); } void sp_selected_path_symdiff() { - sp_selected_path_boolop(bool_op_symdiff); + sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion")); } void sp_selected_path_cut() { - sp_selected_path_boolop(bool_op_cut); + sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division")); } void sp_selected_path_slice() { - sp_selected_path_boolop(bool_op_slice); + sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE, _("Cut path")); } // boolean operations // take the source paths from the file, do the operation, delete the originals and add the results void -sp_selected_path_boolop(bool_op bop) +sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description) { SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); - + Inkscape::Selection *selection = sp_desktop_selection(desktop); + GSList *il = (GSList *) selection->itemList(); - - if (g_slist_length(il) < 2) { + + // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334) + if ( (g_slist_length(il) < 2) && (bop != bool_op_union)) { desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select at least 2 paths to perform a boolean operation.")); return; } + else if ( g_slist_length(il) < 1 ) { + desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select at least 1 path to perform a boolean union.")); + return; + } if (g_slist_length(il) > 2) { if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) { @@ -174,8 +195,8 @@ sp_selected_path_boolop(bool_op bop) // extract the livarot Paths from the source objects // also get the winding rule specified in the style int nbOriginaux = g_slist_length(il); - Path *originaux[nbOriginaux]; - FillRule origWind[nbOriginaux]; + std::vector originaux(nbOriginaux); + std::vector origWind(nbOriginaux); int curOrig; { curOrig = 0; @@ -221,7 +242,7 @@ sp_selected_path_boolop(bool_op bop) if ( bop == bool_op_inters || bop == bool_op_union || bop == bool_op_diff || bop == bool_op_symdiff ) { // true boolean op // get the polygons of each path, with the winding rule specified, and apply the operation iteratively - originaux[0]->ConvertWithBackData(0.03); + originaux[0]->ConvertWithBackData(0.1); originaux[0]->Fill(theShape, 0); @@ -229,7 +250,7 @@ sp_selected_path_boolop(bool_op bop) curOrig = 1; for (GSList *l = il->next; l != NULL; l = l->next) { - originaux[curOrig]->ConvertWithBackData(0.03); + originaux[curOrig]->ConvertWithBackData(0.1); originaux[curOrig]->Fill(theShape, curOrig); @@ -378,9 +399,9 @@ sp_selected_path_boolop(bool_op bop) // function needs it. // this function uses the point_data to get the winding number of each path (ie: is a hole or not) // for later reconstruction in objects, you also need to extract which path is parent of holes (nesting info) - theShape->ConvertToFormeNested(res, nbOriginaux, originaux, 1, nbNest, nesting, conts); + theShape->ConvertToFormeNested(res, nbOriginaux, &originaux[0], 1, nbNest, nesting, conts); } else { - theShape->ConvertToForme(res, nbOriginaux, originaux); + theShape->ConvertToForme(res, nbOriginaux, &originaux[0]); } delete theShape; @@ -395,7 +416,8 @@ sp_selected_path_boolop(bool_op bop) { SP_OBJECT(l->data)->deleteObject(); } - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, + description); selection->clear(); delete res; @@ -403,27 +425,43 @@ sp_selected_path_boolop(bool_op bop) return; } - // remember important aspects of the source path, to be restored - Inkscape::XML::Node *repr_source; + // get the source path object + SPObject *source; if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) { if (reverseOrderForOp) { - repr_source = SP_OBJECT_REPR(il->data); + source = SP_OBJECT(il->data); } else { - repr_source = SP_OBJECT_REPR(il->next->data); + source = SP_OBJECT(il->next->data); } } else { // find out the bottom object GSList *sorted = g_slist_copy((GSList *) selection->reprList()); sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position); - repr_source = ((Inkscape::XML::Node *) sorted->data); + + source = sp_desktop_document(desktop)-> + getObjectByRepr((Inkscape::XML::Node *)sorted->data); + g_slist_free(sorted); } + + // adjust style properties that depend on a possible transform in the source object in order + // to get a correct style attribute for the new path + SPItem* item_source = SP_ITEM(source); + NR::Matrix i2root = sp_item_i2root_affine(item_source); + sp_item_adjust_stroke(item_source, i2root.expansion()); + sp_item_adjust_pattern(item_source, i2root); + sp_item_adjust_gradient(item_source, i2root); + + Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source); + + // remember important aspects of the source path, to be restored gint pos = repr_source->position(); Inkscape::XML::Node *parent = sp_repr_parent(repr_source); - char const *id = repr_source->attribute("id"); - char const *style = repr_source->attribute("style"); - + gchar const *id = repr_source->attribute("id"); + gchar const *style = repr_source->attribute("style"); + gchar const *mask = repr_source->attribute("mask"); + gchar const *clip_path = repr_source->attribute("clip-path"); // remove source paths selection->clear(); @@ -440,13 +478,9 @@ sp_selected_path_boolop(bool_op bop) g_slist_free(il); // premultiply by the inverse of parent's repr - SPItem *parent_item = SP_ITEM(SP_DT_DOCUMENT(desktop)->getObjectByRepr(parent)); + SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent)); NR::Matrix local = sp_item_i2doc_affine(parent_item); - gchar affinestr[80]; - gchar *transform = NULL; - if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) { - transform = affinestr; - } + gchar *transform = sp_svg_transform_write(local); // now that we have the result, add it on the canvas if ( bop == bool_op_cut || bop == bool_op_slice ) { @@ -474,8 +508,14 @@ sp_selected_path_boolop(bool_op bop) for (int i=0;isvg_dump_path(); - Inkscape::XML::Node *repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); repr->setAttribute("style", style); + if (mask) + repr->setAttribute("mask", mask); + if (clip_path) + repr->setAttribute("clip-path", clip_path); + repr->setAttribute("d", d); g_free(d); @@ -514,9 +554,16 @@ sp_selected_path_boolop(bool_op bop) } else { gchar *d = res->svg_dump_path(); - Inkscape::XML::Node *repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); repr->setAttribute("style", style); + if ( mask ) + repr->setAttribute("mask", mask); + + if ( clip_path ) + repr->setAttribute("clip-path", clip_path); + repr->setAttribute("d", d); g_free(d); @@ -530,7 +577,11 @@ sp_selected_path_boolop(bool_op bop) Inkscape::GC::release(repr); } - sp_document_done(SP_DT_DOCUMENT(desktop)); + g_free(transform); + + if (verb != SP_VERB_NONE) { + sp_document_done(sp_desktop_document(desktop), verb, description); + } delete res; } @@ -541,11 +592,10 @@ sp_selected_path_outline() { SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { - // TRANSLATORS: "to outline" means "to convert stroke to path" - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select path(s) to outline.")); + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select stroked path(s) to convert stroke to path.")); return; } @@ -573,7 +623,7 @@ sp_selected_path_outline() } { // pas de stroke pas de chocolat - SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style"); + SPCSSAttr *css = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style"); gchar const *val = sp_repr_css_property(css, "stroke", NULL); if (val == NULL || strcmp(val, "none") == 0) { @@ -585,7 +635,7 @@ sp_selected_path_outline() // remember old stroke style, to be set on fill SPCSSAttr *ncss; { - SPCSSAttr *ocss = sp_repr_css_attr(SP_OBJECT_REPR(item), "style"); + SPCSSAttr *ocss = sp_repr_css_attr_inherited(SP_OBJECT_REPR(item), "style"); gchar const *val = sp_repr_css_property(ocss, "stroke", NULL); gchar const *opac = sp_repr_css_property(ocss, "stroke-opacity", NULL); @@ -599,17 +649,23 @@ sp_selected_path_outline() } else { sp_repr_css_set_property(ncss, "fill-opacity", "1.0"); } + sp_repr_css_unset_property(ncss, "marker-start"); + sp_repr_css_unset_property(ncss, "marker-mid"); + sp_repr_css_unset_property(ncss, "marker-end"); } NR::Matrix const transform(item->transform); + float const scale = transform.expansion(); gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style")); + SPStyle *i_style = SP_OBJECT(item)->style; + gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask"); + gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path"); float o_width, o_miter; JoinType o_join; ButtType o_butt; { - SPStyle *i_style = SP_OBJECT(item)->style; int jointype, captype; jointype = i_style->stroke_linejoin.computed; @@ -655,8 +711,34 @@ sp_selected_path_outline() Path *res = new Path; res->SetBackData(false); + if (i_style->stroke_dash.n_dash) { + // For dashed strokes, use Stroke method, because Outline can't do dashes + // However Stroke adds lots of extra nodes _or_ makes the path crooked, so consider this a temporary workaround + + orig->ConvertWithBackData(0.1); + + orig->DashPolylineFromStyle(i_style, scale, 0); + + Shape* theShape = new Shape; + orig->Stroke(theShape, false, 0.5*o_width, o_join, o_butt, + 0.5 * o_miter); + orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter); + + Shape *theRes = new Shape; + + theRes->ConvertToShape(theShape, fill_positive); + + Path *originaux[1]; + originaux[0] = res; + theRes->ConvertToForme(orig, 1, originaux); + + res->Coalesce(5.0); + + delete theShape; + delete theRes; + + } else { - { orig->Outline(res, 0.5 * o_width, o_join, o_butt, 0.5 * o_miter); orig->Coalesce(0.5 * o_width); @@ -686,7 +768,6 @@ sp_selected_path_outline() did = true; - sp_curve_unref(curve); // remember the position of the item gint pos = SP_OBJECT_REPR(item)->position(); // remember parent @@ -694,12 +775,10 @@ sp_selected_path_outline() // remember id char const *id = SP_OBJECT_REPR(item)->attribute("id"); - selection->remove(item); - SP_OBJECT(item)->deleteObject(false); - if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether - Inkscape::XML::Node *repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); // restore old style repr->setAttribute("style", style); @@ -713,20 +792,83 @@ sp_selected_path_outline() repr->setAttribute("d", str); g_free(str); - // add the new repr to the parent - parent->appendChild(repr); + if (mask) + repr->setAttribute("mask", mask); + if (clip_path) + repr->setAttribute("clip-path", clip_path); - // move to the saved position - repr->setPosition(pos > 0 ? pos : 0); + if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) { - repr->setAttribute("id", id); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g"); - SPItem *newitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr); - sp_item_write_transform(newitem, repr, transform); + // add the group to the parent + parent->appendChild(g_repr); + // move to the saved position + g_repr->setPosition(pos > 0 ? pos : 0); - selection->add(repr); + g_repr->appendChild(repr); + repr->setAttribute("id", id); + SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); + sp_item_write_transform(newitem, repr, transform); + + SPShape *shape = SP_SHAPE(item); + + for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) { + for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) { + if (sp_shape_marker_required (shape, m, bp)) { + + SPMarker* marker = SP_MARKER (shape->marker[m]); + SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m])); + + NR::Matrix tr(sp_shape_marker_get_transform(shape, bp)); + + if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) { + tr = NR::scale(i_style->stroke_width.computed) * tr; + } + + // total marker transform + tr = marker_item->transform * marker->c2p * tr * transform; + + if (SP_OBJECT_REPR(marker_item)) { + Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc); + g_repr->appendChild(m_repr); + SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr); + sp_item_write_transform(marker_item, m_repr, tr); + } + } + } + } + + + selection->add(g_repr); + + Inkscape::GC::release(g_repr); + + + } else { + + // add the new repr to the parent + parent->appendChild(repr); + + // move to the saved position + repr->setPosition(pos > 0 ? pos : 0); + + repr->setAttribute("id", id); + + SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); + sp_item_write_transform(newitem, repr, transform); + + selection->add(repr); + + } Inkscape::GC::release(repr); + + sp_curve_unref(curve); + selection->remove(item); + SP_OBJECT(item)->deleteObject(false); + } delete res; @@ -736,10 +878,11 @@ sp_selected_path_outline() } if (did) { - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE, + _("Convert stroke to path")); } else { // TRANSLATORS: "to outline" means "to convert stroke to path" - desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("No stroked paths to outline in the selection.")); + desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("No stroked paths in the selection.")); return; } } @@ -818,7 +961,7 @@ sp_selected_path_create_offset_object(int expand, bool updating) desktop = SP_ACTIVE_DESKTOP; - selection = SP_DT_SELECTION(desktop); + selection = sp_desktop_selection(desktop); item = selection->singleItem(); @@ -940,7 +1083,11 @@ sp_selected_path_create_offset_object(int expand, bool updating) { // pas vraiment de points sur le resultat // donc il ne reste rien - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), + (updating ? SP_VERB_SELECTION_LINKED_OFFSET + : SP_VERB_SELECTION_DYNAMIC_OFFSET), + (updating ? _("Create linked offset") + : _("Create dynamic offset"))); selection->clear(); delete res; @@ -954,7 +1101,8 @@ sp_selected_path_create_offset_object(int expand, bool updating) tstr[79] = '\0'; - repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + repr = xml_doc->createElement("svg:path"); repr->setAttribute("sodipodi:type", "inkscape:offset"); sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0 ? o_width @@ -983,7 +1131,7 @@ sp_selected_path_create_offset_object(int expand, bool updating) // move to the saved position repr->setPosition(pos > 0 ? pos : 0); - SPItem *nitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr); + SPItem *nitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); if ( updating ) { // on conserve l'original @@ -1004,7 +1152,11 @@ sp_selected_path_create_offset_object(int expand, bool updating) selection->set(nitem); } - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), + (updating ? SP_VERB_SELECTION_LINKED_OFFSET + : SP_VERB_SELECTION_DYNAMIC_OFFSET), + (updating ? _("Create linked offset") + : _("Create dynamic offset"))); delete res; delete orig; @@ -1028,7 +1180,7 @@ sp_selected_path_do_offset(bool expand, double prefOffset) { SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select path(s) to inset/outset.")); @@ -1215,7 +1367,8 @@ sp_selected_path_do_offset(bool expand, double prefOffset) tstr[79] = '\0'; - Inkscape::XML::Node *repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); repr->setAttribute("style", style); @@ -1229,7 +1382,7 @@ sp_selected_path_do_offset(bool expand, double prefOffset) // move to the saved position repr->setPosition(pos > 0 ? pos : 0); - SPItem *newitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr); + SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); // reapply the transform sp_item_write_transform(newitem, repr, transform); @@ -1246,7 +1399,9 @@ sp_selected_path_do_offset(bool expand, double prefOffset) } if (did) { - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), + (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET), + (expand ? _("Outset path") : _("Inset path"))); } else { desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("No paths to inset/outset in the selection.")); return; @@ -1254,10 +1409,18 @@ sp_selected_path_do_offset(bool expand, double prefOffset) } +static bool +sp_selected_path_simplify_items(SPDesktop *desktop, + Inkscape::Selection *selection, GSList *items, + float threshold, bool justCoalesce, + float angleLimit, bool breakableAngles, + bool modifySelection); + //return true if we changed something, else false bool -sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item, +sp_selected_path_simplify_item(SPDesktop *desktop, + Inkscape::Selection *selection, SPItem *item, float threshold, bool justCoalesce, float angleLimit, bool breakableAngles, gdouble size, bool modifySelection) @@ -1267,18 +1430,12 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio //If this is a group, do the children instead if (SP_IS_GROUP(item)) { - - bool didSomething = false; - - for ( GSList *children = sp_item_group_item_list(SP_GROUP(item)); - children ; children = children->next) { - - SPItem *child = (SPItem *) children->data; - didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce, - angleLimit, breakableAngles, size, false); - } - - return didSomething; + GSList *items = sp_item_group_item_list(SP_GROUP(item)); + + return sp_selected_path_simplify_items(desktop, selection, items, + threshold, justCoalesce, + angleLimit, breakableAngles, + false); } @@ -1307,6 +1464,8 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity()); gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style")); + gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask")); + gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path")); Path *orig = Path_for_item(item, false); if (orig == NULL) { @@ -1336,10 +1495,24 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio orig->Simplify(threshold * size); } - Inkscape::XML::Node *repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); + // restore style, mask and clip-path repr->setAttribute("style", style); + g_free(style); + if ( mask ) { + repr->setAttribute("mask", mask); + g_free(mask); + } + + if ( clip_path ) { + repr->setAttribute("clip-path", clip_path); + g_free(clip_path); + } + + // path gchar *str = orig->svg_dump_path(); repr->setAttribute("d", str); g_free(str); @@ -1353,7 +1526,7 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio // move to the saved position repr->setPosition(pos > 0 ? pos : 0); - SPItem *newitem = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr); + SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); // reapply the transform sp_item_write_transform(newitem, repr, transform); @@ -1371,13 +1544,81 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio } +bool +sp_selected_path_simplify_items(SPDesktop *desktop, + Inkscape::Selection *selection, GSList *items, + float threshold, bool justCoalesce, + float angleLimit, bool breakableAngles, + bool modifySelection) +{ + bool simplifyIndividualPaths = + (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0); + + gchar *simplificationType; + if (simplifyIndividualPaths) { + simplificationType = _("Simplifying paths (separately):"); + } else { + simplificationType = _("Simplifying paths:"); + } + + bool didSomething = false; + + NR::Maybe selectionBbox = selection->bounds(); + if (!selectionBbox) { + return false; + } + gdouble selectionSize = L2(selectionBbox->dimensions()); + + gdouble simplifySize = selectionSize; + + int pathsSimplified = 0; + int totalPathCount = g_slist_length(items); + + // set "busy" cursor + desktop->setWaitingCursor(); + + for (; items != NULL; items = items->next) { + SPItem *item = (SPItem *) items->data; + + if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item))) + continue; + + if (simplifyIndividualPaths) { + NR::Maybe itemBbox = item->getBounds(sp_item_i2d_affine(item)); + if (itemBbox) { + simplifySize = L2(itemBbox->dimensions()); + } else { + simplifySize = 0; + } + } + + pathsSimplified++; + + if (pathsSimplified % 20 == 0) { + gchar *message = g_strdup_printf(_("%s %d of %d paths simplified..."), simplificationType, pathsSimplified, totalPathCount); + desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message); + } + + didSomething |= sp_selected_path_simplify_item(desktop, selection, item, + threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection); + } + + desktop->clearWaitingCursor(); + + if (pathsSimplified > 20) { + desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("%d paths simplified."), pathsSimplified)); + } + + return didSomething; +} + void sp_selected_path_simplify_selection(float threshold, bool justCoalesce, float angleLimit, bool breakableAngles) { SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, @@ -1385,28 +1626,17 @@ sp_selected_path_simplify_selection(float threshold, bool justCoalesce, return; } - // remember selection size - NR::Rect bbox = selection->bounds(); - gdouble size = L2(bbox.dimensions()); - - bool didSomething = false; - - //Loop through all of the items in the selection - for (GSList *items = g_slist_copy((GSList *) selection->itemList()); - items != NULL; items = items->next) { - - SPItem *item = (SPItem *) items->data; - - if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item))) - continue; - - didSomething |= sp_selected_path_simplify_item(desktop, selection, item, - threshold, justCoalesce, angleLimit, breakableAngles, size, true); - } + GSList *items = g_slist_copy((GSList *) selection->itemList()); + bool didSomething = sp_selected_path_simplify_items(desktop, selection, + items, threshold, + justCoalesce, + angleLimit, + breakableAngles, true); if (didSomething) - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY, + _("Simplify")); else desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("No paths to simplify in the selection.")); @@ -1490,21 +1720,32 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull) if (!curve) return NULL; - NArtBpath *bpath = curve->bpath; + NArtBpath *bpath = SP_CURVE_BPATH(curve); if (bpath == NULL) return NULL; if ( doTransformation ) { if (transformFull) - bpath = nr_artpath_affine(curve->bpath, sp_item_i2doc_affine(item)); + bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), sp_item_i2doc_affine(item)); else - bpath = nr_artpath_affine(curve->bpath, item->transform); + bpath = nr_artpath_affine(SP_CURVE_BPATH(curve), item->transform); sp_curve_unref(curve); curve=NULL; } else { - bpath=curve->bpath; + bpath=SP_CURVE_BPATH(curve); } + Path *dest = bpath_to_Path(bpath); + + if ( doTransformation ) { + if ( bpath ) g_free(bpath); + } else { + sp_curve_unref(curve); + } + return dest; +} + +Path *bpath_to_Path(NArtBpath const *bpath) { Path *dest = new Path; dest->SetBackData(false); { @@ -1558,12 +1799,6 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull) if (closed) dest->Close(); } - - if ( doTransformation ) { - if ( bpath ) nr_free(bpath); - } else { - sp_curve_unref(curve); - } return dest; }