Code

now that selection description includes style (filtered, clipped), we need to update...
[inkscape.git] / src / sp-star.cpp
index 2f3dcd6c1f23bf6e5767cbf65609f3d8d078e98c..c1581a6d6d7bc3baa131f52680b1bde2f0d31b00 100644 (file)
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
+#include <cstring>
+#include <string>
 #include <glibmm/i18n.h>
 
 #include "svg/svg.h"
 #include "attributes.h"
 #include "display/curve.h"
 #include "xml/repr.h"
+#include "document.h"
 
 #include "sp-star.h"
 
@@ -29,14 +34,15 @@ static void sp_star_class_init (SPStarClass *klass);
 static void sp_star_init (SPStar *star);
 
 static void sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
-static Inkscape::XML::Node *sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_star_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_star_set (SPObject *object, unsigned int key, const gchar *value);
 static void sp_star_update (SPObject *object, SPCtx *ctx, guint flags);
 
 static gchar * sp_star_description (SPItem * item);
-static void sp_star_snappoints(SPItem const *item, SnapPointsIter p);
+static void sp_star_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
 
 static void sp_star_set_shape (SPShape *shape);
+static void sp_star_update_patheffect (SPLPEItem *lpeitem, bool write);
 
 static SPShapeClass *parent_class;
 
@@ -67,13 +73,13 @@ sp_star_class_init (SPStarClass *klass)
        GObjectClass * gobject_class;
        SPObjectClass * sp_object_class;
        SPItemClass * item_class;
-       SPPathClass * path_class;
+       SPLPEItemClass * lpe_item_class;
        SPShapeClass * shape_class;
 
        gobject_class = (GObjectClass *) klass;
        sp_object_class = (SPObjectClass *) klass;
        item_class = (SPItemClass *) klass;
-       path_class = (SPPathClass *) klass;
+       lpe_item_class = (SPLPEItemClass *) klass;
        shape_class = (SPShapeClass *) klass;
 
        parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
@@ -86,6 +92,8 @@ sp_star_class_init (SPStarClass *klass)
        item_class->description = sp_star_description;
        item_class->snappoints = sp_star_snappoints;
 
+    lpe_item_class->update_patheffect = sp_star_update_patheffect;
+
        shape_class->set_shape = sp_star_set_shape;
 }
 
@@ -93,7 +101,7 @@ static void
 sp_star_init (SPStar * star)
 {
        star->sides = 5;
-       star->center = NR::Point(0, 0);
+       star->center = Geom::Point(0, 0);
        star->r[0] = 1.0;
        star->r[1] = 0.001;
        star->arg[0] = star->arg[1] = 0.0;
@@ -121,19 +129,19 @@ sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * r
 }
 
 static Inkscape::XML::Node *
-sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_star_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
        SPStar *star = SP_STAR (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) {
                repr->setAttribute("sodipodi:type", "star");
                sp_repr_set_int (repr, "sodipodi:sides", star->sides);
-               sp_repr_set_svg_double(repr, "sodipodi:cx", star->center[NR::X]);
-               sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[NR::Y]);
+               sp_repr_set_svg_double(repr, "sodipodi:cx", star->center[Geom::X]);
+               sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[Geom::Y]);
                sp_repr_set_svg_double(repr, "sodipodi:r1", star->r[0]);
                sp_repr_set_svg_double(repr, "sodipodi:r2", star->r[1]);
                sp_repr_set_svg_double(repr, "sodipodi:arg1", star->arg[0]);
@@ -143,13 +151,13 @@ sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
                sp_repr_set_svg_double(repr, "inkscape:randomized", star->randomized);
        }
 
-       sp_star_set_shape ((SPShape *) star);
-       char *d = sp_svg_write_path (SP_CURVE_BPATH(((SPShape *) star)->curve));
-       repr->setAttribute("d", d);
-       g_free (d);
+    sp_star_set_shape ((SPShape *) star);
+    char *d = sp_svg_write_path (((SPShape *) star)->curve->get_pathvector());
+    repr->setAttribute("d", d);
+    g_free (d);
 
        if (((SPObjectClass *) (parent_class))->write)
-               ((SPObjectClass *) (parent_class))->write (object, repr, flags);
+               ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
 
        return repr;
 }
@@ -173,20 +181,20 @@ sp_star_set (SPObject *object, unsigned int key, const gchar *value)
                object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
                break;
        case SP_ATTR_SODIPODI_CX:
-               if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::X]) ||
+               if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[Geom::X]) ||
                    (unit == SVGLength::EM) ||
                    (unit == SVGLength::EX) ||
                    (unit == SVGLength::PERCENT)) {
-                       star->center[NR::X] = 0.0;
+                       star->center[Geom::X] = 0.0;
                }
                object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
                break;
        case SP_ATTR_SODIPODI_CY:
-               if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::Y]) ||
+               if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[Geom::Y]) ||
                    (unit == SVGLength::EM) ||
                    (unit == SVGLength::EX) ||
                    (unit == SVGLength::PERCENT)) {
-                       star->center[NR::Y] = 0.0;
+                       star->center[Geom::Y] = 0.0;
                }
                object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
                break;
@@ -267,6 +275,26 @@ sp_star_update (SPObject *object, SPCtx *ctx, guint flags)
                ((SPObjectClass *) parent_class)->update (object, ctx, flags);
 }
 
+static void
+sp_star_update_patheffect(SPLPEItem *lpeitem, bool write)
+{
+    SPShape *shape = (SPShape *) lpeitem;
+    sp_star_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);
+}
+
 static gchar *
 sp_star_description (SPItem *item)
 {
@@ -288,10 +316,10 @@ sp_star_description (SPItem *item)
 /**
 Returns a unit-length vector at 90 degrees to the direction from o to n
  */
-static NR::Point
-rot90_rel (NR::Point o, NR::Point n)
+static Geom::Point
+rot90_rel (Geom::Point o, Geom::Point n)
 {
-       return ((1/NR::L2(n - o)) * NR::Point ((n - o)[NR::Y],  (o - n)[NR::X]));
+       return ((1/Geom::L2(n - o)) * Geom::Point ((n - o)[Geom::Y],  (o - n)[Geom::X]));
 }
 
 /**
@@ -301,13 +329,13 @@ Obvious (but acceptable for my purposes) limits to uniqueness:
 - returned value is unchanged when the point is moved by less than 1/1024 of px
 */
 static guint32
-point_unique_int (NR::Point o)
+point_unique_int (Geom::Point o)
 {
        return ((guint32)
        65536 *
-               (((int) floor (o[NR::X] * 64)) % 1024 + ((int) floor (o[NR::X] * 1024)) % 64)
+               (((int) floor (o[Geom::X] * 64)) % 1024 + ((int) floor (o[Geom::X] * 1024)) % 64)
        +
-               (((int) floor (o[NR::Y] * 64)) % 1024 + ((int) floor (o[NR::Y] * 1024)) % 64)
+               (((int) floor (o[Geom::Y] * 64)) % 1024 + ((int) floor (o[Geom::Y] * 1024)) % 64)
        );
 }
 
@@ -334,11 +362,11 @@ rnd (guint32 const seed, unsigned steps) {
        return ( lcg / 4294967296. ) - 0.5;
 }
 
-static NR::Point
+static Geom::Point
 sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
 {
        // the point whose neighboring curve handle we're calculating
-       NR::Point o = sp_star_get_xy (star, point, index);
+       Geom::Point o = sp_star_get_xy (star, point, index);
 
        // indices of previous and next points
        gint pi = (index > 0)? (index - 1) : (star->sides - 1);
@@ -348,25 +376,25 @@ sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
        SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);
 
        // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
-       NR::Point prev = (star->flatsided? sp_star_get_xy (star, point, pi) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT2? index : pi));
-       NR::Point next = (star->flatsided? sp_star_get_xy (star, point, ni) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT1? index : ni));
+       Geom::Point prev = (star->flatsided? sp_star_get_xy (star, point, pi) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT2? index : pi));
+       Geom::Point next = (star->flatsided? sp_star_get_xy (star, point, ni) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT1? index : ni));
 
        // prev-next midpoint
-       NR::Point mid =  0.5 * (prev + next);
+       Geom::Point mid =  0.5 * (prev + next);
 
        // point to which we direct the bissector of the curve handles;
        // it's far enough outside the star on the perpendicular to prev-next through mid
-       NR::Point biss =  mid + 100000 * rot90_rel (mid, next);
+       Geom::Point biss =  mid + 100000 * rot90_rel (mid, next);
 
        // lengths of vectors to prev and next
-       gdouble prev_len = NR::L2 (prev - o);
-       gdouble next_len = NR::L2 (next - o);
+       gdouble prev_len = Geom::L2 (prev - o);
+       gdouble next_len = Geom::L2 (next - o);
 
        // unit-length vector perpendicular to o-biss
-       NR::Point rot = rot90_rel (o, biss);
+       Geom::Point rot = rot90_rel (o, biss);
 
        // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
-       NR::Point ret;
+       Geom::Point ret;
        if (previ) {
                ret = (star->rounded * prev_len) * rot;
        } else {
@@ -381,11 +409,11 @@ sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
                guint32 seed = point_unique_int (o);
 
                // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
-               ret = ret * NR::Matrix (NR::rotate (star->randomized * M_PI * rnd (seed, 3)));
+               ret = ret * Geom::Matrix (Geom::Rotate (star->randomized * M_PI * rnd (seed, 3)));
                ret *= ( 1 + star->randomized * rnd (seed, 4));
 
                // the randomized corner point
-               NR::Point o_randomized = sp_star_get_xy (star, point, index, true);
+               Geom::Point o_randomized = sp_star_get_xy (star, point, index, true);
 
                return o_randomized + ret;
        }
@@ -400,8 +428,8 @@ sp_star_set_shape (SPShape *shape)
 {
        SPStar *star = SP_STAR (shape);
 
-       SPCurve *c = sp_curve_new ();
-       
+       SPCurve *c = new SPCurve ();
+
        gint sides = star->sides;
        bool not_rounded = (fabs (star->rounded) < 1e-4);
 
@@ -409,13 +437,12 @@ sp_star_set_shape (SPShape *shape)
        // other places that call that function (e.g. the knotholder) need the exact point
 
        // draw 1st segment
-       sp_curve_moveto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
+       c->moveto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
        if (star->flatsided == false) {
                if (not_rounded) {
-                       sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
+                       c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
                } else {
-                       sp_curve_curveto (c,
-                               sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
+                       c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
                                sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, 0, PREV),
                                sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
                }
@@ -424,16 +451,14 @@ sp_star_set_shape (SPShape *shape)
        // draw all middle segments
        for (gint i = 1; i < sides; i++) {
                if (not_rounded) {
-                       sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
+                       c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
                } else {
                        if (star->flatsided == false) {
-                               sp_curve_curveto (c,
-                                               sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i - 1, NEXT),
+                               c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i - 1, NEXT),
                                                sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
                                                sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
                        } else {
-                               sp_curve_curveto (c,
-                                               sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
+                               c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
                                                sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
                                                sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
                        }
@@ -441,44 +466,52 @@ sp_star_set_shape (SPShape *shape)
                if (star->flatsided == false) {
 
                        if (not_rounded) {
-                       sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
+                       c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
                        } else {
-                               sp_curve_curveto (c,
-                                       sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
+                               c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
                                        sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i, PREV),
                                        sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
                        }
                }
        }
-       
+
        // draw last segment
                if (not_rounded) {
-                       sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
+                       c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
                } else {
                        if (star->flatsided == false) {
-                       sp_curve_curveto (c,
-                               sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
+                       c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
                                sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
                                sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
                        } else {
-                       sp_curve_curveto (c,
-                               sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
+                       c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
                                sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
                                sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
                        }
                }
 
-       sp_curve_closepath (c);
-       sp_shape_set_curve_insync (SP_SHAPE (star), c, TRUE);
-       sp_curve_unref (c);
+    c->closepath();
+
+    /* Reset the shape'scurve to the "original_curve"
+     * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
+    sp_shape_set_curve_insync (shape, c, TRUE);
+    if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
+        SPCurve *c_lpe = c->copy();
+        bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
+        if (success) {
+            sp_shape_set_curve_insync (shape, c_lpe, TRUE);
+        }
+        c_lpe->unref();
+    }
+    c->unref();
 }
 
 void
-sp_star_position_set (SPStar *star, gint sides, NR::Point center, gdouble r1, gdouble r2, gdouble arg1, gdouble arg2, bool isflat, double rounded, double randomized)
+sp_star_position_set (SPStar *star, gint sides, Geom::Point center, gdouble r1, gdouble r2, gdouble arg1, gdouble arg2, bool isflat, double rounded, double randomized)
 {
        g_return_if_fail (star != NULL);
        g_return_if_fail (SP_IS_STAR (star));
-       
+
        star->sides = CLAMP (sides, 3, 1024);
        star->center = center;
        star->r[0] = MAX (r1, 0.001);
@@ -495,12 +528,25 @@ sp_star_position_set (SPStar *star, gint sides, NR::Point center, gdouble r1, gd
        SP_OBJECT(star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
-/* fixme: We should use all corners of star (Lauris) */
-
-static void sp_star_snappoints(SPItem const *item, SnapPointsIter p)
+static void sp_star_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
 {
+       // We will determine the star's midpoint ourselves, instead of trusting on the base class
+       // Therefore setSnapObjectMidpoints() is set to false temporarily
+       Inkscape::SnapPreferences local_snapprefs = *snapprefs;
+       local_snapprefs.setSnapObjectMidpoints(false);
+
        if (((SPItemClass *) parent_class)->snappoints) {
-               ((SPItemClass *) parent_class)->snappoints (item, p);
+               ((SPItemClass *) parent_class)->snappoints (item, p, &local_snapprefs);
+       }
+
+       // Help enforcing strict snapping, i.e. only return nodes when we're snapping nodes to nodes or a guide to nodes
+       if (!(snapprefs->getSnapModeNode() || snapprefs->getSnapModeGuide())) {
+               return;
+       }
+
+       if (snapprefs->getSnapObjectMidpoints()) {
+               Geom::Matrix const i2d (sp_item_i2d_affine (item));
+               *p = SP_STAR(item)->center * i2d;
        }
 }
 
@@ -515,7 +561,7 @@ static void sp_star_snappoints(SPItem const *item, SnapPointsIter p)
  * Initial item coordinate system is same as document coordinate system.
  */
 
-NR::Point
+Geom::Point
 sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
 {
        gdouble darg = 2.0 * M_PI / (double) star->sides;
@@ -523,7 +569,7 @@ sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
        double arg = star->arg[point];
        arg += index * darg;
 
-       NR::Point xy = star->r[point] * NR::Point(cos(arg), sin(arg)) + star->center;
+       Geom::Point xy = star->r[point] * Geom::Point(cos(arg), sin(arg)) + star->center;
 
        if (!randomized || star->randomized == 0) {
                // return the exact point
@@ -534,7 +580,7 @@ sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
                // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
                double range = 2 * MAX (star->r[0], star->r[1]);
                // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
-               NR::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
+               Geom::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
                // add the shift to the exact point
                return xy + shift;
        }