Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / splivarot.cpp
index 5868140a6d25b101fe677b514e1c2cd51b84da8b..47051be2be132a954340c3d8ee223dccc8e1b068 100644 (file)
 #include "libnr/nr-path.h"
 #include "xml/repr.h"
 #include "xml/repr-sorting.h"
-
+#include <2geom/pathvector.h>
 #include <libnr/nr-matrix-fns.h>
 #include <libnr/nr-matrix-ops.h>
 #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"
@@ -459,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);
@@ -491,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
@@ -598,6 +600,29 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     delete res;
 }
 
+static
+void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix marker_transform, NR::scale stroke_scale, NR::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));
+
+    NR::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()
@@ -634,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");
             }
@@ -668,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");
 
@@ -713,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);
@@ -774,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;
         }
 
@@ -789,13 +811,11 @@ sp_selected_path_outline()
 
         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
 
-            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            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);
@@ -811,7 +831,7 @@ sp_selected_path_outline()
 
             if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
 
-                Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+                Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
                 Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
 
                 // add the group to the parent
@@ -821,37 +841,46 @@ sp_selected_path_outline()
 
                 g_repr->appendChild(repr);
                 repr->setAttribute("id", id);
-                SPItem *newitem = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
+                SPItem *newitem = (SPItem *) doc->getObjectByRepr(repr);
                 sp_item_write_transform(newitem, repr, transform);
 
                 SPShape *shape = SP_SHAPE(item);
 
-                for (NArtBpath const* 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;
+                Geom::PathVector const & pathv = curve->get_pathvector();
+                for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
+                    if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_START] ) {
+                        Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
+                        sp_selected_path_outline_add_marker( marker_obj, m,
+                                                             NR::scale(i_style->stroke_width.computed), transform,
+                                                             g_repr, xml_doc, doc );
+                    }
 
-                            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);
-                            }
+                    if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_MID] ) {
+                        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( marker_obj, m,
+                                                                 NR::scale(i_style->stroke_width.computed), transform,
+                                                                 g_repr, xml_doc, doc );
+
+                            ++curve_it1;
+                            ++curve_it2;
                         }
                     }
-                }
 
+                    if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_END] ) {
+                        Geom::Matrix const m (sp_shape_marker_get_transform_at_end(path_it->back_default()));
+                        sp_selected_path_outline_add_marker( marker_obj, m,
+                                                             NR::scale(i_style->stroke_width.computed), transform,
+                                                             g_repr, xml_doc, doc );
+                    }
+                }
 
                 selection->add(g_repr);
 
@@ -885,8 +914,6 @@ sp_selected_path_outline()
 
         delete res;
         delete orig;
-        g_free(style);
-
     }
 
     if (did) {
@@ -1583,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;
   }
@@ -1604,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 {
@@ -1721,56 +1748,52 @@ 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);
-    
-    if (bpath == NULL) {
+
+    if (curve == NULL)
         return NULL;
-    }
     
-    Path *dest = bpath_to_Path(bpath);
-
-    g_free(bpath);
-
+    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)
+ * 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;
 
-    NArtBpath const *bpath = SP_CURVE_BPATH(curve);
-    if (bpath == NULL) {
-        return NULL;
-    }
-
-    NArtBpath *new_bpath; // we will get a duplicate which has to be freed at some point!
+    Geom::PathVector *dest = new Geom::PathVector;    
+    *dest = curve->get_pathvector(); // Make a copy; must be freed by the caller!
+    
     if (doTransformation) {
         if (transformFull) {
-            new_bpath = nr_artpath_affine(bpath, from_2geom(sp_item_i2doc_affine(item)));
+            *dest *= extraPreAffine * sp_item_i2doc_affine(item) * extraPostAffine;
         } else {
-            new_bpath = nr_artpath_affine(bpath, item->transform);
+            *dest *= extraPreAffine * (Geom::Matrix)item->transform * extraPostAffine;
         }
     } else {
-        new_bpath = nr_artpath_affine(bpath, NR::identity());
+        *dest *= extraPreAffine * extraPostAffine;
     }
-
-    return new_bpath;
+    
+    return dest;
 }
 
 SPCurve* curve_for_item(SPItem *item)
 {
-    if (!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));
@@ -1784,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);
     {
@@ -1847,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);