Code

improve spcurve::second_point and document its behavior
[inkscape.git] / src / sp-ellipse.cpp
index b2b0f7b0565dd7fbe467fe8e15043272725d3b7d..087c323f12d4af3bedf0eda18890121570420a65 100644 (file)
 #include "libnr/nr-path.h"
 #include "libnr/nr-matrix-fns.h"
 #include "svg/svg.h"
-#include "svg/stringstream.h"
+#include "svg/path-string.h"
 #include "xml/repr.h"
 #include "attributes.h"
 #include "style.h"
 #include "display/curve.h"
 #include <glibmm/i18n.h>
 
+#include "document.h"
 #include "sp-ellipse.h"
 
 #include "prefs-utils.h"
@@ -75,7 +76,9 @@ static void sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_genericellipse_snappoints(SPItem const *item, SnapPointsIter p);
 
 static void sp_genericellipse_set_shape(SPShape *shape);
-static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Node *repr,
+static void sp_genericellipse_update_patheffect (SPLPEItem *lpeitem, bool write);
+
+static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
                                                     guint flags);
 
 static gboolean sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr);
@@ -108,6 +111,7 @@ static void sp_genericellipse_class_init(SPGenericEllipseClass *klass)
 {
     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
     SPItemClass *item_class = (SPItemClass *) klass;
+    SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
     SPShapeClass *shape_class = (SPShapeClass *) klass;
 
     ge_parent_class = (SPShapeClass*) g_type_class_ref(SP_TYPE_SHAPE);
@@ -118,6 +122,7 @@ static void sp_genericellipse_class_init(SPGenericEllipseClass *klass)
     item_class->snappoints = sp_genericellipse_snappoints;
 
     shape_class->set_shape = sp_genericellipse_set_shape;
+    lpe_item_class->update_patheffect = sp_genericellipse_update_patheffect;
 }
 
 static void
@@ -153,13 +158,34 @@ sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags)
         ((SPObjectClass *) ge_parent_class)->update(object, ctx, flags);
 }
 
+static void
+sp_genericellipse_update_patheffect(SPLPEItem *lpeitem, bool write)
+{
+    SPShape *shape = (SPShape *) lpeitem;
+    sp_genericellipse_set_shape(shape);
+
+    if (write) {
+        Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
+        if ( shape->curve != NULL ) {
+            gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
+            repr->setAttribute("d", str);
+            g_free(str);
+        } else {
+            repr->setAttribute("d", NULL);
+        }
+    }
+
+    ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+}
+
+
 #define C1 0.552
 
 /* fixme: Think (Lauris) */
 
 static void sp_genericellipse_set_shape(SPShape *shape)
 {
-    double cx, cy, rx, ry, s, e;
+    double rx, ry, s, e;
     double x0, y0, x1, y1, x2, y2, x3, y3;
     double len;
     gint slice = FALSE;
@@ -172,8 +198,6 @@ static void sp_genericellipse_set_shape(SPShape *shape)
 
     sp_genericellipse_normalize(ellipse);
 
-    cx = 0.0;
-    cy = 0.0;
     rx = ellipse->rx.computed;
     ry = ellipse->ry.computed;
 
@@ -244,23 +268,63 @@ static void sp_genericellipse_set_shape(SPShape *shape)
     }
 
     bpath[i].code = NR_END;
-    SPCurve *c = sp_curve_new_from_bpath(nr_artpath_affine(bpath, aff));
+    SPCurve *c = SPCurve::new_from_bpath(nr_artpath_affine(bpath, aff));
     g_assert(c != NULL);
 
+    sp_lpe_item_perform_path_effect(SP_LPE_ITEM (ellipse), c);
     sp_shape_set_curve_insync((SPShape *) ellipse, c, TRUE);
-    sp_curve_unref(c);
+    c->unref();
 }
 
 static void sp_genericellipse_snappoints(SPItem const *item, SnapPointsIter p)
 {
-    SPGenericEllipse const *ge = SP_GENERICELLIPSE(item);
-
-    NR::Matrix const i2d = sp_item_i2d_affine(item);
+    g_assert(item != NULL);
+    g_assert(SP_IS_GENERICELLIPSE(item));
+    
+    SPGenericEllipse *ellipse = SP_GENERICELLIPSE(item);
+    sp_genericellipse_normalize(ellipse);
+    NR::Matrix const i2d = from_2geom(sp_item_i2d_affine(item));
 
-    /* Add the centre */
-    *p = NR::Point(ge->cx.computed, ge->cy.computed) * i2d;
+    // figure out if we have a slice, whilst guarding against rounding errors
+    bool slice = false;
+    double len = fmod(ellipse->end - ellipse->start, SP_2PI);
+    if (len < 0.0) len += SP_2PI;
+    if (fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8) {
+        slice = false;
+        ellipse->end = ellipse->start + SP_2PI;
+    } else {
+        slice = true;
+    }
 
-    // TODO: add the ends of radii
+    double rx = ellipse->rx.computed;
+    double ry = ellipse->ry.computed;    
+    double cx = ellipse->cx.computed;
+    double cy = ellipse->cy.computed;
+    
+    // Snap to the 4 quadrant points of the ellipse, but only if the arc
+    // spans far enough to include them
+    double angle = 0;
+    for (angle = 0; angle < SP_2PI; angle += M_PI_2) {
+        if (angle >= ellipse->start && angle <= ellipse->end) {
+            *p = NR::Point(cx + cos(angle)*rx, cy + sin(angle)*ry) * i2d;
+        }
+    }
+    
+    // And if we have a slice, also snap to the endpoints and the centre point 
+    if (slice) {
+        // Add the centre, if we have a closed slice
+        if (ellipse->closed) {
+            *p = NR::Point(cx, cy) * i2d;
+        }
+        // Add the start point, if it's not coincident with a quadrant point
+        if (fmod(ellipse->start, M_PI_2) != 0.0 ) {    
+            *p = NR::Point(cx + cos(ellipse->start)*rx, cy + sin(ellipse->start)*ry) * i2d;
+        } 
+        // Add the end point, if it's not coincident with a quadrant point
+        if (fmod(ellipse->end, M_PI_2) != 0.0 ) {    
+            *p = NR::Point(cx + cos(ellipse->end)*rx, cy + sin(ellipse->end)*ry) * i2d;
+        }
+    }
 }
 
 void
@@ -278,13 +342,13 @@ sp_genericellipse_normalize(SPGenericEllipse *ellipse)
     /* Now we keep: 0 <= start < end <= 2*PI */
 }
 
-static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPGenericEllipse *ellipse = SP_GENERICELLIPSE(object);
 
     if (flags & SP_OBJECT_WRITE_EXT) {
         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-            repr = sp_repr_new("svg:path");
+            repr = xml_doc->createElement("svg:path");
         }
 
         sp_repr_set_svg_double(repr, "sodipodi:cx", ellipse->cx.computed);
@@ -297,7 +361,7 @@ static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::
     }
 
     if (((SPObjectClass *) ge_parent_class)->write)
-        ((SPObjectClass *) ge_parent_class)->write(object, repr, flags);
+        ((SPObjectClass *) ge_parent_class)->write(object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -308,7 +372,7 @@ static void sp_ellipse_class_init(SPEllipseClass *klass);
 static void sp_ellipse_init(SPEllipse *ellipse);
 
 static void sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static Inkscape::XML::Node *sp_ellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_ellipse_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value);
 static gchar *sp_ellipse_description(SPItem *item);
 
@@ -351,7 +415,7 @@ static void sp_ellipse_class_init(SPEllipseClass *klass)
 }
 
 static void
-sp_ellipse_init(SPEllipse *ellipse)
+sp_ellipse_init(SPEllipse */*ellipse*/)
 {
     /* Nothing special */
 }
@@ -369,14 +433,14 @@ sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *re
 }
 
 static Inkscape::XML::Node *
-sp_ellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_ellipse_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPGenericEllipse *ellipse;
 
     ellipse = SP_GENERICELLIPSE(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:ellipse");
+        repr = xml_doc->createElement("svg:ellipse");
     }
 
     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
@@ -385,7 +449,7 @@ sp_ellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     sp_repr_set_svg_double(repr, "ry", ellipse->ry.computed);
 
     if (((SPObjectClass *) ellipse_parent_class)->write)
-        (* ((SPObjectClass *) ellipse_parent_class)->write) (object, repr, flags);
+        (* ((SPObjectClass *) ellipse_parent_class)->write) (object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -425,7 +489,7 @@ sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value)
     }
 }
 
-static gchar *sp_ellipse_description(SPItem *item)
+static gchar *sp_ellipse_description(SPItem */*item*/)
 {
     return g_strdup(_("<b>Ellipse</b>"));
 }
@@ -455,7 +519,7 @@ static void sp_circle_class_init(SPCircleClass *klass);
 static void sp_circle_init(SPCircle *circle);
 
 static void sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static Inkscape::XML::Node *sp_circle_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_circle_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_circle_set(SPObject *object, unsigned int key, gchar const *value);
 static gchar *sp_circle_description(SPItem *item);
 
@@ -499,7 +563,7 @@ sp_circle_class_init(SPCircleClass *klass)
 }
 
 static void
-sp_circle_init(SPCircle *circle)
+sp_circle_init(SPCircle */*circle*/)
 {
     /* Nothing special */
 }
@@ -516,14 +580,14 @@ sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *rep
 }
 
 static Inkscape::XML::Node *
-sp_circle_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_circle_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPGenericEllipse *ellipse;
 
     ellipse = SP_GENERICELLIPSE(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:circle");
+        repr = xml_doc->createElement("svg:circle");
     }
 
     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
@@ -531,7 +595,7 @@ sp_circle_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     sp_repr_set_svg_double(repr, "r", ellipse->rx.computed);
 
     if (((SPObjectClass *) circle_parent_class)->write)
-        ((SPObjectClass *) circle_parent_class)->write(object, repr, flags);
+        ((SPObjectClass *) circle_parent_class)->write(object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -566,7 +630,7 @@ sp_circle_set(SPObject *object, unsigned int key, gchar const *value)
     }
 }
 
-static gchar *sp_circle_description(SPItem *item)
+static gchar *sp_circle_description(SPItem */*item*/)
 {
     return g_strdup(_("<b>Circle</b>"));
 }
@@ -577,7 +641,7 @@ static void sp_arc_class_init(SPArcClass *klass);
 static void sp_arc_init(SPArc *arc);
 
 static void sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static Inkscape::XML::Node *sp_arc_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_arc_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_arc_set(SPObject *object, unsigned int key, gchar const *value);
 static void sp_arc_modified(SPObject *object, guint flags);
 
@@ -624,7 +688,7 @@ sp_arc_class_init(SPArcClass *klass)
 }
 
 static void
-sp_arc_init(SPArc *arc)
+sp_arc_init(SPArc */*arc*/)
 {
     /* Nothing special */
 }
@@ -658,53 +722,45 @@ sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
 static gboolean
 sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr)
 {
-    gint fa, fs;
-    gdouble  dt;
-    Inkscape::SVGOStringStream os;
-
     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
 
+    Inkscape::SVG::PathString str;
+
     NR::Point p1 = sp_arc_get_xy(arc, ge->start);
     NR::Point p2 = sp_arc_get_xy(arc, ge->end);
+    double rx = ge->rx.computed;
+    double ry = ge->ry.computed;
 
-    dt = fmod(ge->end - ge->start, SP_2PI);
+    str.moveTo(p1);
+
+    double dt = fmod(ge->end - ge->start, SP_2PI);
     if (fabs(dt) < 1e-6) {
         NR::Point ph = sp_arc_get_xy(arc, (ge->start + ge->end) / 2.0);
-        os << "M " << p1[NR::X] << " " <<  p1[NR::Y]
-           << " A " << ge->rx.computed << " " << ge->ry.computed
-           << " 0 1 1 " << " " << ph[NR::X] << "," << ph[NR::Y]
-           << " A " << ge->rx.computed << " " << ge->ry.computed
-           << " 0 1 1 " << " " << p2[NR::X] << " " << p2[NR::Y] << " z";
+        str.arcTo(rx, ry, 0, true, true, ph)
+           .arcTo(rx, ry, 0, true, true, p2)
+           .closePath();
     } else {
-        fa = (fabs(dt) > M_PI) ? 1 : 0;
-        fs = (dt > 0) ? 1 : 0;
-#ifdef ARC_VERBOSE
-        g_print("start:%g end:%g fa=%d fs=%d\n", ge->start, ge->end, fa, fs);
-#endif
+        bool fa = (fabs(dt) > M_PI);
+        bool fs = (dt > 0);
+        str.arcTo(rx, ry, 0, fa, fs, p2);
         if (ge->closed) {
-            os << "M " << p1[NR::X] << "," << p1[NR::Y]
-               << " A " << ge->rx.computed << "," << ge->ry.computed
-               << " 0 " << fa << " " << fs << " " << p2[NR::X] << "," << p2[NR::Y]
-               << " L " << ge->cx.computed << "," << ge->cy.computed << " z";
-        } else {
-            os << "M " << p1[NR::X] << "," << p1[NR::Y]
-               << " A " << ge->rx.computed << "," << ge->ry.computed
-               << " 0 " << fa << " " << fs << " " << p2[NR::X] << "," << p2[NR::Y];
-
+            NR::Point center = NR::Point(ge->cx.computed, ge->cy.computed);
+            str.lineTo(center).closePath();
         }
     }
-    repr->setAttribute("d", os.str().c_str());
+
+    repr->setAttribute("d", str.c_str());
     return true;
 }
 
 static Inkscape::XML::Node *
-sp_arc_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_arc_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPGenericEllipse *ge = SP_GENERICELLIPSE(object);
     SPArc *arc = SP_ARC(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:path");
+        repr = xml_doc->createElement("svg:path");
     }
 
     if (flags & SP_OBJECT_WRITE_EXT) {
@@ -732,7 +788,7 @@ sp_arc_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     sp_arc_set_elliptical_path_attribute(arc, repr);
 
     if (((SPObjectClass *) arc_parent_class)->write)
-        ((SPObjectClass *) arc_parent_class)->write(object, repr, flags);
+        ((SPObjectClass *) arc_parent_class)->write(object, xml_doc, repr, flags);
 
     return repr;
 }