Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / splivarot.cpp
index 47a365c681e10c9aa1703383dc1963d4a70017a2..47051be2be132a954340c3d8ee223dccc8e1b068 100644 (file)
@@ -54,6 +54,7 @@
 #include <libnr/nr-matrix-translate-ops.h>
 #include <libnr/nr-scale-matrix-ops.h>
 #include <libnr/n-art-bpath-2geom.h>
+#include "helper/geom.h"
 
 #include "livarot/Path.h"
 #include "livarot/Shape.h"
@@ -460,7 +461,7 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     // 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 = from_2geom(sp_item_i2root_affine(item_source));
+    NR::Matrix i2root(sp_item_i2root_affine(item_source));
     sp_item_adjust_stroke(item_source, NR::expansion(i2root));
     sp_item_adjust_pattern(item_source, i2root);
     sp_item_adjust_gradient(item_source, i2root);
@@ -492,7 +493,7 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
 
     // premultiply by the inverse of parent's repr
     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
-    NR::Matrix local = from_2geom(sp_item_i2doc_affine(parent_item));
+    NR::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
@@ -606,7 +607,7 @@ void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix
     SPMarker* marker = SP_MARKER (marker_object);
     SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker_object));
 
-    NR::Matrix tr(from_2geom(marker_transform));
+    NR::Matrix tr(marker_transform);
 
     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
         tr = stroke_scale * tr;
@@ -658,30 +659,25 @@ sp_selected_path_outline()
                 continue;
         }
 
-        {   // pas de stroke pas de chocolat
-            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) {
-                curve->unref();
-                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_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);
-
-            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");
             }
@@ -692,8 +688,6 @@ sp_selected_path_outline()
 
         NR::Matrix const transform(item->transform);
         float const scale = NR::expansion(transform);
-        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");
 
@@ -737,12 +731,17 @@ 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);
+        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);
@@ -798,7 +797,6 @@ sp_selected_path_outline()
             // ca a merd\8e, ou bien le resultat est vide
             delete res;
             delete orig;
-            g_free(style);
             continue;
         }
 
@@ -817,10 +815,7 @@ sp_selected_path_outline()
             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);
@@ -919,8 +914,6 @@ sp_selected_path_outline()
 
         delete res;
         delete orig;
-        g_free(style);
-
     }
 
     if (did) {
@@ -1617,7 +1610,7 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
 
   bool didSomething = false;
 
-  NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
+  boost::optional<NR::Rect> selectionBbox = selection->bounds();
   if (!selectionBbox) {
     return false;
   }
@@ -1638,7 +1631,7 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
           continue;
 
       if (simplifyIndividualPaths) {
-          NR::Maybe<NR::Rect> itemBbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
+          boost::optional<NR::Rect> itemBbox = item->getBounds(sp_item_i2d_affine(item));
           if (itemBbox) {
               simplifySize      = L2(itemBbox->dimensions());
           } else {
@@ -1755,82 +1748,55 @@ Path *
 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
 {
     SPCurve *curve = curve_for_item(item);
-    NArtBpath *bpath = bpath_for_curve(item, curve, doTransformation, transformFull, NR::identity(), NR::identity());
-    
-    if (bpath == NULL) {
-        return NULL;
-    }
-
-    Geom::PathVector pathv = pathvector_for_curve(item, curve, doTransformation, transformFull);
-
-    Path *dest = new Path;
-    dest->LoadPathVector(pathv);
 
+    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;
 }
 
-/* 
- * This function always returns a new NArtBpath, the caller must g_free the returned path!
-*/
-NArtBpath *
-bpath_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, NR::Matrix extraPreAffine, NR::Matrix extraPostAffine)
-{
-    if (curve == NULL)
-        return NULL;
-
-    NArtBpath *bpath = BPath_from_2GeomPath(curve->get_pathvector());
-    if (bpath == NULL) {
-        return NULL;
-    }
-
-    NArtBpath *new_bpath; // we will get a duplicate which has to be freed at some point!
-    if (doTransformation) {
-        if (transformFull) {
-            new_bpath = nr_artpath_affine(bpath, extraPreAffine * from_2geom(sp_item_i2doc_affine(item)) * extraPostAffine);
-        } else {
-            new_bpath = nr_artpath_affine(bpath, extraPreAffine * item->transform * extraPostAffine);
-        }
-    } else {
-        new_bpath = nr_artpath_affine(bpath, extraPreAffine * NR::identity() * extraPostAffine);
-    }
-
-    g_free(bpath);
-    return new_bpath;
-}
-
 /* 
  * 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::PathVector*
+pathvector_for_curve(SPItem *item, SPCurve *curve, bool doTransformation, bool transformFull, Geom::Matrix extraPreAffine, Geom::Matrix extraPostAffine)
 {
     if (curve == NULL)
-        return Geom::PathVector();
+        return NULL;
 
+    Geom::PathVector *dest = new Geom::PathVector;    
+    *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller!
+    
     if (doTransformation) {
         if (transformFull) {
-            return (curve->get_pathvector()) * sp_item_i2doc_affine(item);
+            *dest *= extraPreAffine * sp_item_i2doc_affine(item) * extraPostAffine;
         } else {
-            return (curve->get_pathvector()) * to_2geom(item->transform);
+            *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine;
         }
     } else {
-        return curve->get_pathvector();
+        *dest *= extraPreAffine * extraPostAffine;
     }
+    
+    return dest;
 }
 
 SPCurve* curve_for_item(SPItem *item)
 {
-    if (!item) {
-       return NULL;
-    }
-
+    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));
+            curve = sp_path_get_curve_for_edit(SP_PATH(item));
         } else {
             curve = sp_shape_get_curve(SP_SHAPE(item));
         }
@@ -1841,13 +1807,14 @@ SPCurve* curve_for_item(SPItem *item)
     }
     else if (SP_IS_IMAGE(item))
     {
-       curve = sp_image_get_curve(SP_IMAGE(item));
+    curve = sp_image_get_curve(SP_IMAGE(item));
     }
     
     return curve; // do not forget to unref the curve at some point!
 }
 
-Path *bpath_to_Path(NArtBpath const *bpath) {
+Path *bpath_to_Path(NArtBpath const *bpath) 
+{
     Path *dest = new Path;
     dest->SetBackData(false);
     {
@@ -1904,7 +1871,7 @@ Path *bpath_to_Path(NArtBpath const *bpath) {
     return dest;
 }
 
-NR::Maybe<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p, unsigned seg)
+boost::optional<Path::cut_position> get_nearest_position_on_Path(Path *path, NR::Point p, unsigned seg)
 {
     //get nearest position on path
     Path::cut_position pos = path->PointToCurvilignPosition(p, seg);