Code

fix Bug #674109 - Pattern spam when adjusting a pattern in text
[inkscape.git] / src / splivarot.cpp
index eb01e625ab8d0b30ff3b8ee60bf5a3a9581b06f4..e7787813d3830383700e0e69f9b70ce5937c7b5d 100644 (file)
@@ -127,8 +127,8 @@ sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb
     }
 
     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 <b>exactly 2 paths</b> 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 <b>exactly 2 paths</b> to perform difference, division, or path cut."));
             return;
         }
     }
@@ -138,8 +138,7 @@ sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb
     // 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);
@@ -431,7 +430,7 @@ sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb
 
     // 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 ( bop == bool_op_diff || bop == bool_op_cut || bop == bool_op_slice ) {
         if (reverseOrderForOp) {
              source = SP_OBJECT(il->data);
         } else {
@@ -452,11 +451,11 @@ sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb
     // 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.descrim());
-    sp_item_adjust_pattern(item_source, i2root);
-    sp_item_adjust_gradient(item_source, i2root);
-    sp_item_adjust_livepatheffect(item_source, i2root);
+    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);
 
@@ -485,7 +484,7 @@ sp_selected_path_boolop(SPDesktop *desktop, bool_op bop, const unsigned int verb
 
     // 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));
+    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
@@ -625,6 +624,253 @@ void sp_selected_path_outline_add_marker( SPObject *marker_object, Geom::Matrix
     }
 }
 
+static
+void item_outline_add_marker( SPObject const *marker_object, Geom::Matrix marker_transform,
+                              Geom::Scale stroke_scale, Geom::PathVector* pathv_in )
+{
+    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;
+
+    Geom::PathVector* marker_pathv = item_outline(marker_item);
+    
+    if (marker_pathv) {
+        for (unsigned int j=0; j < marker_pathv->size(); j++) {
+            pathv_in->push_back((*marker_pathv)[j] * tr);
+        }
+        delete marker_pathv;
+    }
+}
+
+/**
+ *  Returns a pathvector that is the outline of the stroked item, with markers.
+ *  item must be SPShape of SPText.
+ */
+Geom::PathVector* item_outline(SPItem const *item)
+{
+    Geom::PathVector *ret_pathv = NULL;
+
+    if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
+        return ret_pathv;
+
+    // no stroke: no outline
+    if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
+        return ret_pathv;
+    }
+
+    SPCurve *curve = NULL;
+    if (SP_IS_SHAPE(item)) {
+        curve = sp_shape_get_curve(SP_SHAPE(item));
+    } else if (SP_IS_TEXT(item)) {
+        curve = SP_TEXT(item)->getNormalizedBpath();
+    }
+    if (curve == NULL) {
+        return ret_pathv;
+    }
+
+    if (curve->get_pathvector().empty()) {
+        return ret_pathv;
+    }
+
+    // remember old stroke style, to be set on fill
+    SPStyle *i_style = SP_OBJECT_STYLE(item);
+
+    Geom::Matrix const transform(item->transform);
+    float const scale = transform.descrim();
+
+    float o_width, o_miter;
+    JoinType o_join;
+    ButtType o_butt;
+    {
+        o_width = i_style->stroke_width.computed;
+        if (o_width < 0.1) {
+            o_width = 0.1;
+        }
+        o_miter = i_style->stroke_miterlimit.value * o_width;
+
+        switch (i_style->stroke_linejoin.computed) {
+            case SP_STROKE_LINEJOIN_MITER:
+                o_join = join_pointy;
+                break;
+            case SP_STROKE_LINEJOIN_ROUND:
+                o_join = join_round;
+                break;
+            default:
+                o_join = join_straight;
+                break;
+        }
+
+        switch (i_style->stroke_linecap.computed) {
+            case SP_STROKE_LINECAP_SQUARE:
+                o_butt = butt_square;
+                break;
+            case SP_STROKE_LINECAP_ROUND:
+                o_butt = butt_round;
+                break;
+            default:
+                o_butt = butt_straight;
+                break;
+        }
+    }
+
+    // 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( curve->get_pathvector() );
+
+    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);
+
+        Shape *theShape = new Shape;
+        Shape *theRes = new Shape;
+
+        res->ConvertWithBackData(1.0);
+        res->Fill(theShape, 0);
+        theRes->ConvertToShape(theShape, fill_positive);
+
+        Path *originaux[1];
+        originaux[0] = res;
+        theRes->ConvertToForme(orig, 1, originaux);
+
+        delete theShape;
+        delete theRes;
+    }
+
+    if (orig->descr_cmd.size() <= 1) {
+        // ca a merd\8e, ou bien le resultat est vide
+        delete res;
+        delete orig;
+        curve->unref();
+        return ret_pathv;
+    }
+
+
+    if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
+        ret_pathv = orig->MakePathVector();
+
+        if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
+            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()));
+                    item_outline_add_marker( marker_obj, m,
+                                             Geom::Scale(i_style->stroke_width.computed),
+                                             ret_pathv );
+                }
+            }
+            // 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()));
+                        item_outline_add_marker( midmarker_obj, m,
+                                                 Geom::Scale(i_style->stroke_width.computed),
+                                                 ret_pathv );
+                    }
+                    // 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));
+                            item_outline_add_marker( midmarker_obj, m,
+                                                     Geom::Scale(i_style->stroke_width.computed),
+                                                     ret_pathv);
+
+                            ++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);
+                        item_outline_add_marker( midmarker_obj, m,
+                                                 Geom::Scale(i_style->stroke_width.computed),
+                                                 ret_pathv );
+                    }
+                }
+            }
+            // 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);
+                    item_outline_add_marker( marker_obj, m,
+                                             Geom::Scale(i_style->stroke_width.computed),
+                                             ret_pathv );
+                }
+            }
+        }
+
+        curve->unref();
+    }
+
+    delete res;
+    delete orig;
+
+    return ret_pathv;
+}
+
 void
 sp_selected_path_outline(SPDesktop *desktop)
 {
@@ -658,6 +904,10 @@ sp_selected_path_outline(SPDesktop *desktop)
                 continue;
         }
 
+        if (curve->get_pathvector().empty()) {
+            continue;
+        }
+
         // pas de stroke pas de chocolat
         if (!SP_OBJECT_STYLE(item) || SP_OBJECT_STYLE(item)->stroke.noneSet) {
             curve->unref();
@@ -685,7 +935,7 @@ sp_selected_path_outline(SPDesktop *desktop)
             sp_repr_css_unset_property(ncss, "marker-end");
         }
 
-        NR::Matrix const transform(item->transform);
+        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");
@@ -857,42 +1107,70 @@ sp_selected_path_outline(SPDesktop *desktop)
                 SPShape *shape = SP_SHAPE(item);
 
                 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()));
+
+                // 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 );
                     }
-
-                    SPObject *midmarker_obj = shape->marker[SP_MARKER_LOC_MID];
-                    if ( midmarker_obj && (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())
+                }
+                // 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
                         {
-                            /* 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;
+                            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 );
                         }
                     }
-
-                    if ( SPObject *marker_obj = shape->marker[SP_MARKER_LOC_END] ) {
+                }
+                // 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". */
-                        unsigned int index = path_it->size_default();
+                        Geom::Path const &path_last = pathv.back();
+                        unsigned int index = path_last.size_default();
                         if (index > 0) {
                             index--;
                         }
-                        Geom::Curve const &lastcurve = (*path_it)[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,
@@ -1048,7 +1326,7 @@ sp_selected_path_create_offset_object(SPDesktop *desktop, int expand, bool updat
             return;
     }
 
-    NR::Matrix const transform(item->transform);
+    Geom::Matrix const transform(item->transform);
 
     sp_item_write_transform(item, SP_OBJECT_REPR(item), Geom::identity());
 
@@ -1273,7 +1551,7 @@ sp_selected_path_do_offset(SPDesktop *desktop, 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), Geom::identity());
 
@@ -1516,8 +1794,11 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
             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();
@@ -1581,6 +1862,9 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
         g_free(clip_path);
     }
 
+    // restore path effect
+    repr->setAttribute("inkscape:path-effect", patheffect);
+
     // path
     gchar *str = orig->svg_dump_path();
     if (patheffect)
@@ -1603,9 +1887,6 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
     // reapply the transform
     sp_item_write_transform(newitem, repr, transform);
 
-    // restore path effect
-    repr->setAttribute("inkscape:path-effect", patheffect);
-    
     // restore title & description
     if (title) {
        newitem->setTitle(title);
@@ -1850,16 +2131,16 @@ SPCurve* curve_for_item(SPItem *item)
     return curve; // do not forget to unref the curve at some point!
 }
 
-boost::optional<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, Geom::Point p, unsigned seg)
 {
     //get nearest position on path
     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;
 }
@@ -1874,4 +2155,4 @@ NR::Point get_point_on_Path(Path *path, int piece, double t)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :