Code

convert all SPCurve's points and matrices arguments and return types to 2Geom
[inkscape.git] / src / draw-context.cpp
index 6742ddaf3325d3153297bbe4a6b88ac900fd798b..19551dd6a6dbe38a0683a919b62f09415742fea0 100644 (file)
@@ -25,6 +25,7 @@
 #include "svg/svg.h"
 #include <glibmm/i18n.h>
 #include "libnr/n-art-bpath.h"
+#include "display/curve.h"
 #include "desktop.h"
 #include "desktop-affine.h"
 #include "desktop-handles.h"
@@ -40,6 +41,8 @@
 #include "snap.h"
 #include "sp-path.h"
 #include "sp-namedview.h"
+#include "live_effects/lpe-patternalongpath.h"
+#include "style.h"
 
 static void sp_draw_context_class_init(SPDrawContextClass *klass);
 static void sp_draw_context_init(SPDrawContext *dc);
@@ -114,6 +117,8 @@ sp_draw_context_init(SPDrawContext *dc)
     dc->green_color = 0x00ff007f;
     dc->red_curve_is_valid = false;
 
+    dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
+
     new (&dc->sel_changed_connection) sigc::connection();
     new (&dc->sel_modified_connection) sigc::connection();
 }
@@ -135,6 +140,8 @@ sp_draw_context_dispose(GObject *object)
         dc->selection = NULL;
     }
 
+    dc->waiting_LPE_type = Inkscape::LivePathEffect::INVALID_LPE;
+
     spdc_free_colors(dc);
 
     G_OBJECT_CLASS(draw_parent_class)->dispose(object);
@@ -164,16 +171,16 @@ sp_draw_context_setup(SPEventContext *ec)
     dc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->red_bpath), dc->red_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
     /* Create red curve */
-    dc->red_curve = sp_curve_new_sized(4);
+    dc->red_curve = new SPCurve();
 
     /* Create blue bpath */
     dc->blue_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->blue_bpath), dc->blue_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
     /* Create blue curve */
-    dc->blue_curve = sp_curve_new_sized(8);
+    dc->blue_curve = new SPCurve();
 
     /* Create green curve */
-    dc->green_curve = sp_curve_new_sized(64);
+    dc->green_curve = new SPCurve();
     /* No green anchor by default */
     dc->green_anchor = NULL;
     dc->green_closed = FALSE;
@@ -202,7 +209,7 @@ sp_draw_context_finish(SPEventContext *ec)
 }
 
 static void
-sp_draw_context_set(SPEventContext *ec, const gchar *key, const gchar *value)
+sp_draw_context_set(SPEventContext */*ec*/, const gchar */*key*/, const gchar */*value*/)
 {
 }
 
@@ -240,6 +247,106 @@ sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
     return ret;
 }
 
+static void
+spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
+{
+    using namespace Inkscape::LivePathEffect;
+
+    // TODO: Don't paste path if nothing is on the clipboard
+
+    Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
+    Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
+    gchar *svgd = sp_svg_write_path(c->get_pathvector());
+    static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
+}
+
+/*
+ * If we have an item and a waiting LPE, apply the effect to the item
+ * (spiro spline mode is treated separately)
+ */
+void
+spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
+{
+    using namespace Inkscape::LivePathEffect;
+
+    if (item && SP_IS_LPE_ITEM(item)) {
+        if (prefs_get_int_attribute("tools.freehand", "spiro-spline-mode", 0)) {
+            Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
+        }
+
+        int shape = prefs_get_int_attribute("tools.freehand", "shape", 0);
+        bool shape_applied = false;
+        SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS);
+        const char *cstroke = sp_repr_css_property(css_item, "stroke", "none");
+
+        switch (shape) {
+            case 0:
+                // don't apply any shape
+                break;
+            case 1:
+            {
+                // take shape from clipboard; TODO: catch the case where clipboard is empty
+                Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
+                Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
+                static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
+
+                shape_applied = true;
+                break;
+            }
+            case 2:
+            {
+                // "crescendo"
+                // TODO: this is only for illustration (we create a "crescendo"-shaped path
+                //       manually; eventually we should read the path from a separate file)
+                SPCurve *c = new SPCurve();
+                c->moveto(0,5);
+                c->lineto(200,10);
+                c->lineto(200,0);
+                c->closepath();
+                spdc_paste_curve_as_freehand_shape(c, dc, item);
+                c->unref();
+
+                shape_applied = true;
+                break;
+            }
+            case 3:
+            {
+                // "decrescendo"
+                // TODO: this is only for illustration (we create a "decrescendo"-shaped path
+                //       manually; eventually we should read the path from a separate file)
+                SPCurve *c = new SPCurve();
+                c->moveto(0,0);
+                c->lineto(0,10);
+                c->lineto(200,5);
+                c->closepath();
+                spdc_paste_curve_as_freehand_shape(c, dc, item);
+                c->unref();
+
+                shape_applied = true;
+                break;
+            }
+            default:
+                break;
+        }
+        if (shape_applied) {
+            // apply original stroke color as fill and unset stroke; then return
+            SPCSSAttr *css = sp_repr_css_attr_new();
+            sp_repr_css_set_property (css, "fill", cstroke);
+            sp_repr_css_set_property (css, "stroke", "none");
+            sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true);
+            sp_repr_css_attr_unref(css);
+            return;
+        }
+
+        if (dc->waiting_LPE_type != INVALID_LPE) {
+            Effect::createAndApply(dc->waiting_LPE_type, dc->desktop->doc(), item);
+            dc->waiting_LPE_type = INVALID_LPE;
+        }
+        if (SP_IS_PEN_CONTEXT(dc)) {
+            SP_PEN_CONTEXT(dc)->polylines_only = false;
+        }
+    }
+}
 
 /*
  * Selection handlers
@@ -256,7 +363,7 @@ spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
 /* fixme: We have to ensure this is not delayed (Lauris) */
 
 static void
-spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc)
+spdc_selection_modified(Inkscape::Selection *sel, guint /*flags*/, SPDrawContext *dc)
 {
     if (dc->attach) {
         spdc_attach_selection(dc, sel);
@@ -264,7 +371,7 @@ spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc
 }
 
 static void
-spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel)
+spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/)
 {
     /* We reset white and forget white/start/end anchors */
     spdc_reset_white(dc);
@@ -279,24 +386,21 @@ spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel)
         dc->white_item = item;
         /* Curve list */
         /* We keep it in desktop coordinates to eliminate calculation errors */
-        SPCurve *norm = sp_shape_get_curve(SP_SHAPE(item));
-        sp_curve_transform(norm, sp_item_i2d_affine(dc->white_item));
+        SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item));
+        norm->transform(sp_item_i2d_affine(dc->white_item));
         g_return_if_fail( norm != NULL );
-        dc->white_curves = g_slist_reverse(sp_curve_split(norm));
-        sp_curve_unref(norm);
+        dc->white_curves = g_slist_reverse(norm->split());
+        norm->unref();
         /* Anchor list */
         for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
             SPCurve *c;
             c = (SPCurve*)l->data;
-            g_return_if_fail( c->end > 1 );
-            if ( SP_CURVE_BPATH(c)->code == NR_MOVETO_OPEN ) {
-                NArtBpath *s, *e;
+            g_return_if_fail( c->get_segment_count() > 0 );
+            if ( !c->is_closed() ) {
                 SPDrawAnchor *a;
-                s = sp_curve_first_bpath(c);
-                e = sp_curve_last_bpath(c);
-                a = sp_draw_anchor_new(dc, c, TRUE, NR::Point(s->x3, s->y3));
+                a = sp_draw_anchor_new(dc, c, TRUE, c->first_point());
                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
-                a = sp_draw_anchor_new(dc, c, FALSE, NR::Point(e->x3, e->y3));
+                a = sp_draw_anchor_new(dc, c, FALSE, c->last_point());
                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
             }
         }
@@ -353,9 +457,9 @@ void spdc_endpoint_snap_rotation(SPEventContext const *const ec, NR::Point &p, N
         p = o + bdot * best;
 
         /* Snap it along best vector */
-        SnapManager const &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
-        p = m.constrainedSnap(Inkscape::Snapper::SNAPPOINT_NODE | Inkscape::Snapper::SNAPPOINT_BBOX,
-                              p, Inkscape::Snapper::ConstraintLine(best), NULL).getPoint();
+        SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
+        m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
+        m.constrainedSnapReturnByRef( Inkscape::Snapper::SNAPPOINT_NODE, p, Inkscape::Snapper::ConstraintLine(best));
     }
 }
 
@@ -367,16 +471,16 @@ void spdc_endpoint_snap_free(SPEventContext const * const ec, NR::Point& p, guin
         return;
     }
 
-    /* FIXME: this should be doing bbox snap as well */
-    SnapManager const &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
-    p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_BBOX | Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
+    SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
+    m.setup(SP_EVENT_CONTEXT_DESKTOP(ec), NULL);
+    m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p);
 }
 
 static SPCurve *
 reverse_then_unref(SPCurve *orig)
 {
-    SPCurve *ret = sp_curve_reverse(orig);
-    sp_curve_unref(orig);
+    SPCurve *ret = orig->create_reverse();
+    orig->unref();
     return ret;
 }
 
@@ -392,24 +496,24 @@ spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
     SPCurve *c = dc->green_curve;
 
     /* Green */
-    dc->green_curve = sp_curve_new_sized(64);
+    dc->green_curve = new SPCurve();
     while (dc->green_bpaths) {
         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
     }
     /* Blue */
-    sp_curve_append_continuous(c, dc->blue_curve, 0.0625);
-    sp_curve_reset(dc->blue_curve);
+    c->append_continuous(dc->blue_curve, 0.0625);
+    dc->blue_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
     /* Red */
     if (dc->red_curve_is_valid) {
-        sp_curve_append_continuous(c, dc->red_curve, 0.0625);
+        c->append_continuous(dc->red_curve, 0.0625);
     }
-    sp_curve_reset(dc->red_curve);
+    dc->red_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
 
-    if (sp_curve_empty(c)) {
-        sp_curve_unref(c);
+    if (c->is_empty()) {
+        c->unref();
         return;
     }
 
@@ -417,10 +521,10 @@ spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
         // We hit green anchor, closing Green-Blue-Red
         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
-        sp_curve_closepath_current(c);
+        c->closepath_current();
         /* Closed path, just flush */
         spdc_flush_white(dc, c);
-        sp_curve_unref(c);
+        c->unref();
         return;
     }
 
@@ -428,16 +532,16 @@ spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
     if ( dc->sa && dc->ea
          && ( dc->sa->curve == dc->ea->curve )
          && ( ( dc->sa != dc->ea )
-              || dc->sa->curve->closed ) )
+              || dc->sa->curve->is_closed() ) )
     {
         // We hit bot start and end of single curve, closing paths
         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
-        if (dc->sa->start && !(dc->sa->curve->closed) ) {
+        if (dc->sa->start && !(dc->sa->curve->is_closed()) ) {
             c = reverse_then_unref(c);
         }
-        sp_curve_append_continuous(dc->sa->curve, c, 0.0625);
-        sp_curve_unref(c);
-        sp_curve_closepath_current(dc->sa->curve);
+        dc->sa->curve->append_continuous(c, 0.0625);
+        c->unref();
+        dc->sa->curve->closepath_current();
         spdc_flush_white(dc, NULL);
         return;
     }
@@ -449,8 +553,8 @@ spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
         if (dc->sa->start) {
             s = reverse_then_unref(s);
         }
-        sp_curve_append_continuous(s, c, 0.0625);
-        sp_curve_unref(c);
+        s->append_continuous(c, 0.0625);
+        c->unref();
         c = s;
     } else /* Step D - test end */ if (dc->ea) {
         SPCurve *e = dc->ea->curve;
@@ -458,14 +562,14 @@ spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
         if (!dc->ea->start) {
             e = reverse_then_unref(e);
         }
-        sp_curve_append_continuous(c, e, 0.0625);
-        sp_curve_unref(e);
+        c->append_continuous(e, 0.0625);
+        e->unref();
     }
 
 
     spdc_flush_white(dc, c);
 
-    sp_curve_unref(c);
+    c->unref();
 }
 
 static char const *
@@ -491,43 +595,48 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
 
     if (dc->white_curves) {
         g_assert(dc->white_item);
-        c = sp_curve_concat(dc->white_curves);
+        c = SPCurve::concat(dc->white_curves);
         g_slist_free(dc->white_curves);
         dc->white_curves = NULL;
         if (gc) {
-            sp_curve_append(c, gc, FALSE);
+            c->append(gc, FALSE);
         }
     } else if (gc) {
         c = gc;
-        sp_curve_ref(c);
+        c->ref();
     } else {
         return;
     }
 
     /* Now we have to go back to item coordinates at last */
-    sp_curve_transform(c, ( dc->white_item
+    c->transform(( dc->white_item
                             ? sp_item_dt2i_affine(dc->white_item)
-                            : sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc)) ));
+                            : to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) ));
 
     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
     SPDocument *doc = sp_desktop_document(desktop);
     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
 
-    if ( c && !sp_curve_empty(c) ) {
+    if ( c && !c->is_empty() ) {
         /* We actually have something to write */
 
+        bool has_lpe = false;
         Inkscape::XML::Node *repr;
         if (dc->white_item) {
             repr = SP_OBJECT_REPR(dc->white_item);
+            has_lpe = sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(dc->white_item));
         } else {
             repr = xml_doc->createElement("svg:path");
             /* Set style */
             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
         }
 
-        gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
+        gchar *str = sp_svg_write_path( c->get_pathvector() );
         g_assert( str != NULL );
-        repr->setAttribute("d", str);
+        if (has_lpe)
+            repr->setAttribute("inkscape:original-d", str);
+        else
+            repr->setAttribute("d", str);
         g_free(str);
 
         if (!dc->white_item) {
@@ -535,15 +644,27 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
             dc->selection->set(repr);
             Inkscape::GC::release(repr);
-            item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
+            item->transform = from_2geom(i2i_affine(desktop->currentRoot(), desktop->currentLayer()));
             item->updateRepr();
         }
 
+
+        // we finished the path; now apply any waiting LPEs or freehand shapes
+        // FIXME: placing this here seems to cause issues with undo!
+        spdc_check_for_and_apply_waiting_LPE(dc, dc->selection->singleItem());
+
         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, 
                          _("Draw path"));
+
+        // When quickly drawing several subpaths with Shift, the next subpath may be finished and
+        // flushed before the selection_modified signal is fired by the previous change, which
+        // results in the tool losing all of the selected path's curve except that last subpath. To
+        // fix this, we force the selection_modified callback now, to make sure the tool's curve is
+        // in sync immediately.
+        spdc_selection_modified(sp_desktop_selection(desktop), 0, dc);
     }
 
-    sp_curve_unref(c);
+    c->unref();
 
     /* Flush pending updates */
     sp_document_ensure_up_to_date(doc);
@@ -580,7 +701,7 @@ spdc_reset_white(SPDrawContext *dc)
         dc->white_item = NULL;
     }
     while (dc->white_curves) {
-        sp_curve_unref((SPCurve *) dc->white_curves->data);
+        reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
     }
     while (dc->white_anchors) {
@@ -598,7 +719,7 @@ spdc_free_colors(SPDrawContext *dc)
         dc->red_bpath = NULL;
     }
     if (dc->red_curve) {
-        dc->red_curve = sp_curve_unref(dc->red_curve);
+        dc->red_curve = dc->red_curve->unref();
     }
     /* Blue */
     if (dc->blue_bpath) {
@@ -606,7 +727,7 @@ spdc_free_colors(SPDrawContext *dc)
         dc->blue_bpath = NULL;
     }
     if (dc->blue_curve) {
-        dc->blue_curve = sp_curve_unref(dc->blue_curve);
+        dc->blue_curve = dc->blue_curve->unref();
     }
     /* Green */
     while (dc->green_bpaths) {
@@ -614,7 +735,7 @@ spdc_free_colors(SPDrawContext *dc)
         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
     }
     if (dc->green_curve) {
-        dc->green_curve = sp_curve_unref(dc->green_curve);
+        dc->green_curve = dc->green_curve->unref();
     }
     if (dc->green_anchor) {
         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
@@ -625,7 +746,7 @@ spdc_free_colors(SPDrawContext *dc)
         dc->white_item = NULL;
     }
     while (dc->white_curves) {
-        sp_curve_unref((SPCurve *) dc->white_curves->data);
+        reinterpret_cast<SPCurve *>(dc->white_curves->data)->unref();
         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
     }
     while (dc->white_anchors) {