X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsplivarot.cpp;h=085dfeda0a52595bd027f5c06b3b8a94c423ed10;hb=8fd48ac0af8adc1c726581baee9b8de8b493f603;hp=c5969576e6d348d23658d35a1130b1d4e690a889;hpb=555c285af940ce0786916ed8954c7901126c5557;p=inkscape.git diff --git a/src/splivarot.cpp b/src/splivarot.cpp index c5969576e..085dfeda0 100644 --- a/src/splivarot.cpp +++ b/src/splivarot.cpp @@ -17,14 +17,22 @@ # include #endif +#include +#include #include +#include #include "xml/repr.h" #include "svg/svg.h" #include "sp-path.h" +#include "sp-shape.h" +#include "sp-image.h" +#include "marker.h" +#include "enums.h" #include "sp-text.h" +#include "sp-flowtext.h" +#include "text-editing.h" #include "sp-item-group.h" #include "style.h" -#include "inkscape.h" #include "document.h" #include "message-stack.h" #include "selection.h" @@ -33,12 +41,13 @@ #include "display/canvas-bpath.h" #include "display/curve.h" #include -#include "prefs-utils.h" +#include "preferences.h" -#include "libnr/n-art-bpath.h" -#include "libnr/nr-path.h" #include "xml/repr.h" #include "xml/repr-sorting.h" +#include <2geom/pathvector.h> +#include +#include "helper/geom.h" #include "livarot/Path.h" #include "livarot/Shape.h" @@ -47,64 +56,79 @@ bool Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who); -void sp_selected_path_boolop(bool_op bop); -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_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description=""); +void sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset); +void sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating); void -sp_selected_path_union() +sp_selected_path_union(SPDesktop *desktop) { - sp_selected_path_boolop(bool_op_union); + sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_SELECTION_UNION, _("Union")); } void -sp_selected_path_intersect() +sp_selected_path_union_skip_undo(SPDesktop *desktop) { - sp_selected_path_boolop(bool_op_inters); + sp_selected_path_boolop(desktop, bool_op_union, SP_VERB_NONE, _("Union")); } void -sp_selected_path_diff() +sp_selected_path_intersect(SPDesktop *desktop) { - sp_selected_path_boolop(bool_op_diff); + sp_selected_path_boolop(desktop, bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection")); } void -sp_selected_path_symdiff() +sp_selected_path_diff(SPDesktop *desktop) { - sp_selected_path_boolop(bool_op_symdiff); + sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference")); +} + +void +sp_selected_path_diff_skip_undo(SPDesktop *desktop) +{ + sp_selected_path_boolop(desktop, bool_op_diff, SP_VERB_NONE, _("Difference")); +} + +void +sp_selected_path_symdiff(SPDesktop *desktop) +{ + sp_selected_path_boolop(desktop, bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion")); } void -sp_selected_path_cut() +sp_selected_path_cut(SPDesktop *desktop) { - sp_selected_path_boolop(bool_op_cut); + sp_selected_path_boolop(desktop, bool_op_cut, SP_VERB_SELECTION_CUT, _("Division")); } void -sp_selected_path_slice() +sp_selected_path_slice(SPDesktop *desktop) { - sp_selected_path_boolop(bool_op_slice); + sp_selected_path_boolop(desktop, 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(SPDesktop *desktop, bool_op bop, const unsigned int verb, const Glib::ustring description) { - SPDesktop *desktop = SP_ACTIVE_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 ) { - desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select exactly 2 paths to perform difference, XOR, division, or path cut.")); + if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) { + desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select exactly 2 paths to perform difference, division, or path cut.")); return; } } @@ -114,8 +138,7 @@ sp_selected_path_boolop(bool_op bop) // topmost object (differences, cuts) bool reverseOrderForOp = false; - // mettre les elements de la liste dans l'ordre pour ces operations - if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice) { + if (bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice) { // check in the tree to find which element of the selection list is topmost (for 2-operand commands only) Inkscape::XML::Node *a = SP_OBJECT_REPR(il->data); Inkscape::XML::Node *b = SP_OBJECT_REPR(il->next->data); @@ -164,7 +187,7 @@ sp_selected_path_boolop(bool_op bop) for (GSList *l = il; l != NULL; l = l->next) { SPItem *item = SP_ITEM(l->data); - if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) + if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item)) { desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is not a path, cannot perform boolean operation.")); g_slist_free(il); @@ -396,7 +419,8 @@ sp_selected_path_boolop(bool_op bop) { SP_OBJECT(l->data)->deleteObject(); } - sp_document_done(sp_desktop_document(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, + description); selection->clear(); delete res; @@ -404,28 +428,46 @@ sp_selected_path_boolop(bool_op bop) return; } - // remember important aspects of the source path, to be restored - Inkscape::XML::Node *repr_source; - if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) { + // get the source path object + SPObject *source; + if ( bop == bool_op_diff || 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); } - 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"); + // 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); + Geom::Matrix i2doc(sp_item_i2doc_affine(item_source)); + sp_item_adjust_stroke(item_source, i2doc.descrim()); + sp_item_adjust_pattern(item_source, i2doc); + sp_item_adjust_gradient(item_source, i2doc); + sp_item_adjust_livepatheffect(item_source, i2doc); + + 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); + 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"); + gchar *title = source->title(); + gchar *desc = source->desc(); // remove source paths selection->clear(); for (GSList *l = il; l != NULL; l = l->next) { @@ -442,12 +484,8 @@ sp_selected_path_boolop(bool_op bop) // premultiply by the inverse of parent's repr 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; - } + Geom::Matrix local (sp_item_i2doc_affine(parent_item)); + gchar *transform = sp_svg_transform_write(local.inverse()); // now that we have the result, add it on the canvas if ( bop == bool_op_cut || bop == bool_op_slice ) { @@ -475,8 +513,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); @@ -515,9 +559,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); @@ -525,28 +576,61 @@ sp_selected_path_boolop(bool_op bop) repr->setAttribute("id", id); parent->appendChild(repr); - repr->setPosition(pos > 0 ? pos : 0); + if (title) { + sp_desktop_document(desktop)->getObjectByRepr(repr)->setTitle(title); + } + if (desc) { + sp_desktop_document(desktop)->getObjectByRepr(repr)->setDesc(desc); + } + repr->setPosition(pos > 0 ? pos : 0); selection->add(repr); Inkscape::GC::release(repr); } - sp_document_done(sp_desktop_document(desktop)); + g_free(transform); + if (title) g_free(title); + if (desc) g_free(desc); + + if (verb != SP_VERB_NONE) { + sp_document_done(sp_desktop_document(desktop), verb, description); + } delete res; } +static +void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix marker_transform, + Geom::Scale stroke_scale, Geom::Matrix transform, + Inkscape::XML::Node *g_repr, Inkscape::XML::Document *xml_doc, SPDocument * doc ) +{ + SPMarker* marker = SP_MARKER (marker_object); + SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker_object)); + + Geom::Matrix tr(marker_transform); + + if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) { + tr = stroke_scale * 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 *) doc->getObjectByRepr(m_repr); + sp_item_write_transform(marker_item, m_repr, tr); + } +} void -sp_selected_path_outline() +sp_selected_path_outline(SPDesktop *desktop) { - SPDesktop *desktop = SP_ACTIVE_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,44 +657,43 @@ sp_selected_path_outline() continue; } - { // pas de stroke pas de chocolat - SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style"); - gchar const *val = sp_repr_css_property(css, "stroke", NULL); - - if (val == NULL || strcmp(val, "none") == 0) { - sp_curve_unref(curve); - continue; - } + // pas de stroke pas de chocolat + if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) { + curve->unref(); + continue; } // remember old stroke style, to be set on fill + SPStyle *i_style = SP_OBJECT_STYLE(item); SPCSSAttr *ncss; { - SPCSSAttr *ocss = sp_repr_css_attr(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); - - ncss = sp_repr_css_attr_new(); + ncss = sp_css_attr_from_style(i_style, SP_STYLE_FLAG_ALWAYS); + gchar const *s_val = sp_repr_css_property(ncss, "stroke", NULL); + gchar const *s_opac = sp_repr_css_property(ncss, "stroke-opacity", NULL); sp_repr_css_set_property(ncss, "stroke", "none"); sp_repr_css_set_property(ncss, "stroke-opacity", "1.0"); - sp_repr_css_set_property(ncss, "fill", val); - if ( opac ) { - sp_repr_css_set_property(ncss, "fill-opacity", opac); + sp_repr_css_set_property(ncss, "fill", s_val); + if ( s_opac ) { + sp_repr_css_set_property(ncss, "fill-opacity", s_opac); } 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); - gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style")); + Geom::Matrix const transform(item->transform); + float const scale = transform.descrim(); + 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; @@ -646,18 +729,49 @@ sp_selected_path_outline() o_miter = i_style->stroke_miterlimit.value * o_width; } - Path *orig = Path_for_item(item, false); - if (orig == NULL) { - g_free(style); - sp_curve_unref(curve); + SPCurve *curvetemp = curve_for_item(item); + if (curvetemp == NULL) { + curve->unref(); continue; } + // Livarots outline of arcs is broken. So convert the path to linear and cubics only, for which the outline is created correctly. + Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curvetemp->get_pathvector() ); + curvetemp->unref(); + + Path *orig = new Path; + orig->LoadPathVector(pathv); 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); @@ -681,31 +795,29 @@ sp_selected_path_outline() // ca a merdŽ, ou bien le resultat est vide delete res; delete orig; - g_free(style); continue; } did = true; - sp_curve_unref(curve); // remember the position of the item gint pos = SP_OBJECT_REPR(item)->position(); // remember parent Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent(); // remember id char const *id = SP_OBJECT_REPR(item)->attribute("id"); - - selection->remove(item); - SP_OBJECT(item)->deleteObject(false); - + // remember title + gchar *title = item->title(); + // remember description + gchar *desc = item->desc(); + 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"); + SPDocument * doc = sp_desktop_document(desktop); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc); + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); - // restore old style - repr->setAttribute("style", style); - - // set old stroke style on fill + // restore old style, but set old stroke style on fill sp_repr_css_change(repr, ncss, "style"); sp_repr_css_attr_unref(ncss); @@ -714,111 +826,234 @@ 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(doc); + Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g"); - SPItem *newitem = (SPItem *) sp_desktop_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); + // restore title, description, id, transform + repr->setAttribute("id", id); + SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr); + sp_item_write_transform(newitem, repr, transform); + if (title) { + newitem->setTitle(title); + } + if (desc) { + newitem->setDesc(desc); + } + + SPShape *shape = SP_SHAPE(item); + + Geom::PathVector const & pathv = curve->get_pathvector(); + + // START marker + for (int i = 0; i < 2; i++) { // SP_MARKER_LOC and SP_MARKER_LOC_START + if ( SPObject *marker_obj = shape->marker[i] ) { + Geom::Matrix const m (sp_shape_marker_get_transform_at_start(pathv.front().front())); + sp_selected_path_outline_add_marker( marker_obj, m, + Geom::Scale(i_style->stroke_width.computed), transform, + g_repr, xml_doc, doc ); + } + } + // MID marker + for (int i = 0; i < 3; i += 2) { // SP_MARKER_LOC and SP_MARKER_LOC_MID + SPObject *midmarker_obj = shape->marker[i]; + if (!midmarker_obj) continue; + for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) { + // START position + if ( path_it != pathv.begin() + && ! ((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 + { + Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front())); + sp_selected_path_outline_add_marker( midmarker_obj, m, + Geom::Scale(i_style->stroke_width.computed), transform, + g_repr, xml_doc, doc ); + } + // MID position + if (path_it->size_default() > 1) { + Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve + Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve + while (curve_it2 != path_it->end_default()) + { + /* Put marker between curve_it1 and curve_it2. + * Loop to end_default (so including closing segment), because when a path is closed, + * there should be a midpoint marker between last segment and closing straight line segment + */ + Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2)); + sp_selected_path_outline_add_marker(midmarker_obj, m, + Geom::Scale(i_style->stroke_width.computed), transform, + g_repr, xml_doc, doc); + + ++curve_it1; + ++curve_it2; + } + } + // END position + if ( path_it != (pathv.end()-1) && !path_it->empty()) { + Geom::Curve const &lastcurve = path_it->back_default(); + Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve); + sp_selected_path_outline_add_marker( midmarker_obj, m, + Geom::Scale(i_style->stroke_width.computed), transform, + g_repr, xml_doc, doc ); + } + } + } + // END marker + for (int i = 0; i < 4; i += 3) { // SP_MARKER_LOC and SP_MARKER_LOC_END + if ( SPObject *marker_obj = shape->marker[i] ) { + /* Get reference to last curve in the path. + * For moveto-only path, this returns the "closing line segment". */ + Geom::Path const &path_last = pathv.back(); + unsigned int index = path_last.size_default(); + if (index > 0) { + index--; + } + Geom::Curve const &lastcurve = path_last[index]; + + Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve); + sp_selected_path_outline_add_marker( marker_obj, m, + Geom::Scale(i_style->stroke_width.computed), transform, + g_repr, xml_doc, doc ); + } + } + + 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); + + // restore title, description, id, transform + repr->setAttribute("id", id); + + SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); + sp_item_write_transform(newitem, repr, transform); + if (title) { + newitem->setTitle(title); + } + if (desc) { + newitem->setDesc(desc); + } + + selection->add(repr); + + } Inkscape::GC::release(repr); + + curve->unref(); + selection->remove(item); + SP_OBJECT(item)->deleteObject(false); + } + if (title) g_free(title); + if (desc) g_free(desc); delete res; delete orig; - g_free(style); - } if (did) { - sp_document_done(sp_desktop_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; } } void -sp_selected_path_offset() +sp_selected_path_offset(SPDesktop *desktop) { - double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0); - sp_selected_path_do_offset(true, prefOffset); + sp_selected_path_do_offset(desktop, true, prefOffset); } void -sp_selected_path_inset() +sp_selected_path_inset(SPDesktop *desktop) { - double prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", 1.0); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + double prefOffset = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0); - sp_selected_path_do_offset(false, prefOffset); + sp_selected_path_do_offset(desktop, false, prefOffset); } void -sp_selected_path_offset_screen(double pixels) +sp_selected_path_offset_screen(SPDesktop *desktop, double pixels) { - sp_selected_path_do_offset(true, pixels / SP_ACTIVE_DESKTOP->current_zoom()); + sp_selected_path_do_offset(desktop, true, pixels / desktop->current_zoom()); } void -sp_selected_path_inset_screen(double pixels) +sp_selected_path_inset_screen(SPDesktop *desktop, double pixels) { - sp_selected_path_do_offset(false, pixels / SP_ACTIVE_DESKTOP->current_zoom()); + sp_selected_path_do_offset(desktop, false, pixels / desktop->current_zoom()); } -void sp_selected_path_create_offset_object_zero() +void sp_selected_path_create_offset_object_zero(SPDesktop *desktop) { - sp_selected_path_create_offset_object(0, false); + sp_selected_path_create_offset_object(desktop, 0, false); } -void sp_selected_path_create_offset() +void sp_selected_path_create_offset(SPDesktop *desktop) { - sp_selected_path_create_offset_object(1, false); + sp_selected_path_create_offset_object(desktop, 1, false); } -void sp_selected_path_create_inset() +void sp_selected_path_create_inset(SPDesktop *desktop) { - sp_selected_path_create_offset_object(-1, false); + sp_selected_path_create_offset_object(desktop, -1, false); } -void sp_selected_path_create_updating_offset_object_zero() +void sp_selected_path_create_updating_offset_object_zero(SPDesktop *desktop) { - sp_selected_path_create_offset_object(0, true); + sp_selected_path_create_offset_object(desktop, 0, true); } -void sp_selected_path_create_updating_offset() +void sp_selected_path_create_updating_offset(SPDesktop *desktop) { - sp_selected_path_create_offset_object(1, true); + sp_selected_path_create_offset_object(desktop, 1, true); } -void sp_selected_path_create_updating_inset() +void sp_selected_path_create_updating_inset(SPDesktop *desktop) { - sp_selected_path_create_offset_object(-1, true); + sp_selected_path_create_offset_object(desktop, -1, true); } void -sp_selected_path_create_offset_object(int expand, bool updating) +sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updating) { Inkscape::Selection *selection; Inkscape::XML::Node *repr; SPItem *item; SPCurve *curve; gchar *style, *str; - SPDesktop *desktop; float o_width, o_miter; JoinType o_join; ButtType o_butt; curve = NULL; - desktop = SP_ACTIVE_DESKTOP; - selection = sp_desktop_selection(desktop); item = selection->singleItem(); @@ -840,9 +1075,9 @@ sp_selected_path_create_offset_object(int expand, bool updating) return; } - NR::Matrix const transform(item->transform); + Geom::Matrix const transform(item->transform); - sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity()); + sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity()); style = g_strdup(SP_OBJECT(item)->repr->attribute("style")); @@ -884,9 +1119,8 @@ sp_selected_path_create_offset_object(int expand, bool updating) } { - double prefOffset = 1.0; - prefOffset = prefs_get_double_attribute("options.defaultoffsetwidth", "value", prefOffset); - o_width = prefOffset; + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + o_width = prefs->getDouble("/options/defaultoffsetwidth/value", 1.0); } if (o_width < 0.01) @@ -898,7 +1132,7 @@ sp_selected_path_create_offset_object(int expand, bool updating) if (orig == NULL) { g_free(style); - sp_curve_unref(curve); + curve->unref(); return; } @@ -935,13 +1169,17 @@ sp_selected_path_create_offset_object(int expand, bool updating) delete theRes; } - sp_curve_unref(curve); + curve->unref(); if (res->descr_cmd.size() <= 1) { // pas vraiment de points sur le resultat // donc il ne reste rien - sp_document_done(sp_desktop_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; @@ -955,7 +1193,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 @@ -1005,7 +1244,11 @@ sp_selected_path_create_offset_object(int expand, bool updating) selection->set(nitem); } - sp_document_done(sp_desktop_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; @@ -1025,10 +1268,8 @@ sp_selected_path_create_offset_object(int expand, bool updating) void -sp_selected_path_do_offset(bool expand, double prefOffset) +sp_selected_path_do_offset(SPDesktop *desktop, bool expand, double prefOffset) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { @@ -1059,9 +1300,9 @@ sp_selected_path_do_offset(bool expand, double prefOffset) continue; } - NR::Matrix const transform(item->transform); + Geom::Matrix const transform(item->transform); - sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity()); + sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity()); gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style")); @@ -1111,7 +1352,7 @@ sp_selected_path_do_offset(bool expand, double prefOffset) Path *orig = Path_for_item(item, false); if (orig == NULL) { g_free(style); - sp_curve_unref(curve); + curve->unref(); continue; } @@ -1199,7 +1440,7 @@ sp_selected_path_do_offset(bool expand, double prefOffset) did = true; - sp_curve_unref(curve); + curve->unref(); // remember the position of the item gint pos = SP_OBJECT_REPR(item)->position(); // remember parent @@ -1216,7 +1457,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); @@ -1247,7 +1489,9 @@ sp_selected_path_do_offset(bool expand, double prefOffset) } if (did) { - sp_document_done(sp_desktop_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; @@ -1255,10 +1499,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) @@ -1268,18 +1520,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); } @@ -1297,33 +1543,44 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio return false; } + // correct virtual size by full transform (bug #166937) + size /= sp_item_i2doc_affine(item).descrim(); + // save the transform, to re-apply it after simplification - NR::Matrix const transform(item->transform); + Geom::Matrix const transform(item->transform); /* reset the transform, effectively transforming the item by transform.inverse(); this is necessary so that the item is transformed twice back and forth, allowing all compensations to cancel out regardless of the preferences */ - sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity()); + sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::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) { g_free(style); - sp_curve_unref(curve); + curve->unref(); return false; } - sp_curve_unref(curve); + curve->unref(); // remember the position of the item gint pos = SP_OBJECT_REPR(item)->position(); // remember parent Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent(); // remember id char const *id = SP_OBJECT_REPR(item)->attribute("id"); - + // remember path effect + char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect"); + // remember title + gchar *title = item->title(); + // remember description + gchar *desc = item->desc(); + //If a group was selected, to not change the selection list if (modifySelection) selection->remove(item); @@ -1337,12 +1594,32 @@ 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); + } + + // restore path effect + repr->setAttribute("inkscape:path-effect", patheffect); + // path gchar *str = orig->svg_dump_path(); - repr->setAttribute("d", str); + if (patheffect) + repr->setAttribute("inkscape:original-d", str); + else + repr->setAttribute("d", str); g_free(str); // restore id @@ -1359,6 +1636,16 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio // reapply the transform sp_item_write_transform(newitem, repr, transform); + // restore title & description + if (title) { + newitem->setTitle(title); + g_free(title); + } + if (desc) { + newitem->setDesc(desc); + g_free(desc); + } + //If we are not in a selected group if (modifySelection) selection->add(repr); @@ -1372,42 +1659,98 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio } -void -sp_selected_path_simplify_selection(float threshold, bool justCoalesce, - float angleLimit, bool breakableAngles) +bool +sp_selected_path_simplify_items(SPDesktop *desktop, + Inkscape::Selection *selection, GSList *items, + float threshold, bool justCoalesce, + float angleLimit, bool breakableAngles, + bool modifySelection) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + bool simplifyIndividualPaths = prefs->getBool("/options/simplifyindividualpaths/value"); - Inkscape::Selection *selection = sp_desktop_selection(desktop); + gchar *simplificationType; + if (simplifyIndividualPaths) { + simplificationType = _("Simplifying paths (separately):"); + } else { + simplificationType = _("Simplifying paths:"); + } - if (selection->isEmpty()) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, - _("Select path(s) to simplify.")); - return; + bool didSomething = false; + + Geom::OptRect selectionBbox = selection->bounds(); + if (!selectionBbox) { + return false; } + gdouble selectionSize = L2(selectionBbox->dimensions()); - // remember selection size - NR::Rect bbox = selection->bounds(); - gdouble size = L2(bbox.dimensions()); + gdouble simplifySize = selectionSize; - bool didSomething = false; + int pathsSimplified = 0; + int totalPathCount = g_slist_length(items); - //Loop through all of the items in the selection - for (GSList *items = g_slist_copy((GSList *) selection->itemList()); - items != NULL; items = items->next) { + // 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; + continue; + + if (simplifyIndividualPaths) { + Geom::OptRect 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, size, true); + 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(SPDesktop *desktop, float threshold, bool justCoalesce, + float angleLimit, bool breakableAngles) +{ + Inkscape::Selection *selection = sp_desktop_selection(desktop); + + if (selection->isEmpty()) { + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, + _("Select path(s) to simplify.")); + return; + } + + 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_desktop_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.")); @@ -1419,12 +1762,12 @@ static double previousTime = 0.0; static gdouble simplifyMultiply = 1.0; void -sp_selected_path_simplify(void) +sp_selected_path_simplify(SPDesktop *desktop) { + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); gdouble simplifyThreshold = - prefs_get_double_attribute("options.simplifythreshold", "value", 0.003); - bool simplifyJustCoalesce = - (bool) prefs_get_int_attribute("options.simplifyjustcoalesce", "value", 0); + prefs->getDouble("/options/simplifythreshold/value", 0.003); + bool simplifyJustCoalesce = prefs->getBool("/options/simplifyjustcoalesce/value", 0); //Get the current time GTimeVal currentTimeVal; @@ -1450,8 +1793,8 @@ sp_selected_path_simplify(void) //g_print("%g\n", simplify_threshold); //Make the actual call - sp_selected_path_simplify_selection(simplifyThreshold, - simplifyJustCoalesce, 0.0, false); + sp_selected_path_simplify_selection(desktop, simplifyThreshold, + simplifyJustCoalesce, 0.0, false); } @@ -1471,113 +1814,82 @@ Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who) Path * Path_for_item(SPItem *item, bool doTransformation, bool transformFull) { - SPCurve *curve; + SPCurve *curve = curve_for_item(item); - if (!item) + if (curve == NULL) return NULL; + + Geom::PathVector *pathv = pathvector_for_curve(item, curve, doTransformation, transformFull, Geom::identity(), Geom::identity()); + curve->unref(); + + Path *dest = new Path; + dest->LoadPathVector(*pathv); + delete pathv; + + return dest; +} - if (SP_IS_SHAPE(item)) - { - curve = sp_shape_get_curve(SP_SHAPE(item)); - } - else if (SP_IS_TEXT(item)) - { - curve = SP_TEXT(item)->getNormalizedBpath(); - } - else - { - curve = NULL; - } - - if (!curve) - return NULL; - NArtBpath *bpath = curve->bpath; - if (bpath == NULL) +/* + * NOTE: Returns empty pathvector if curve == NULL + * TODO: see if calling this method can be optimized. All the pathvector copying might be slow. + */ +Geom::PathVector* +pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, Geom::Matrix extraPreAffine, Geom::Matrix extraPostAffine) +{ + if (curve == NULL) return NULL; - if ( doTransformation ) { - if (transformFull) - bpath = nr_artpath_affine(curve->bpath, sp_item_i2doc_affine(item)); - else - bpath = nr_artpath_affine(curve->bpath, item->transform); - sp_curve_unref(curve); - curve=NULL; + Geom::PathVector *dest = new Geom::PathVector; + *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller! + + if (doTransformation) { + if (transformFull) { + *dest *= extraPreAffine * sp_item_i2doc_affine(item) * extraPostAffine; + } else { + *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine; + } } else { - bpath=curve->bpath; + *dest *= extraPreAffine * extraPostAffine; } + + return dest; +} - Path *dest = new Path; - dest->SetBackData(false); - { - int i; - bool closed = false; - float lastX = 0.0; - float lastY = 0.0; - - for (i = 0; bpath[i].code != NR_END; i++) { - switch (bpath[i].code) { - case NR_LINETO: - lastX = bpath[i].x3; - lastY = bpath[i].y3; - { - NR::Point tmp(lastX, lastY); - dest->LineTo(tmp); - } - break; - - case NR_CURVETO: - { - NR::Point tmp, tms, tme; - tmp[0]=bpath[i].x3; - tmp[1]=bpath[i].y3; - tms[0]=3 * (bpath[i].x1 - lastX); - tms[1]=3 * (bpath[i].y1 - lastY); - tme[0]=3 * (bpath[i].x3 - bpath[i].x2); - tme[1]=3 * (bpath[i].y3 - bpath[i].y2); - dest->CubicTo(tmp,tms,tme); - } - lastX = bpath[i].x3; - lastY = bpath[i].y3; - break; - - case NR_MOVETO_OPEN: - case NR_MOVETO: - if (closed) - dest->Close(); - closed = (bpath[i].code == NR_MOVETO); - lastX = bpath[i].x3; - lastY = bpath[i].y3; - { - NR::Point tmp(lastX, lastY); - dest->MoveTo(tmp); - } - break; - default: - break; - } +SPCurve* curve_for_item(SPItem *item) +{ + if (!item) + return NULL; + + SPCurve *curve = NULL; + if (SP_IS_SHAPE(item)) { + if (SP_IS_PATH(item)) { + curve = sp_path_get_curve_for_edit(SP_PATH(item)); + } else { + curve = sp_shape_get_curve(SP_SHAPE(item)); } - if (closed) - dest->Close(); } - - if ( doTransformation ) { - if ( bpath ) nr_free(bpath); - } else { - sp_curve_unref(curve); + else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) + { + curve = te_get_layout(item)->convertToCurves(); } - return dest; + else if (SP_IS_IMAGE(item)) + { + curve = sp_image_get_curve(SP_IMAGE(item)); + } + + return curve; // do not forget to unref the curve at some point! } -NR::Maybe get_nearest_position_on_Path(Path *path, NR::Point p) +boost::optional get_nearest_position_on_Path(Path *path, Geom::Point p, unsigned seg) { //get nearest position on path - Path::cut_position pos = path->PointToCurvilignPosition(p); + Path::cut_position pos = path->PointToCurvilignPosition(p, seg); return pos; } -NR::Point get_point_on_Path(Path *path, int piece, double t) +Geom::Point get_point_on_Path(Path *path, int piece, double t) { - NR::Point p; + Geom::Point p; path->PointAt(piece, t, p); return p; }