Code

Merging from trunk
[inkscape.git] / src / dyna-draw-context.cpp
index 68bb2f163efdab8bfa4cc110a183861b2a3b5210..2d4782f9b4cdf3733faa6de9fc5f651af3f6f554 100644 (file)
 #include "message-context.h"
 #include "prefs-utils.h"
 #include "pixmaps/cursor-calligraphy.xpm"
-#include "libnr/n-art-bpath.h"
-#include "libnr/nr-path.h"
 #include "libnr/nr-matrix-ops.h"
 #include "libnr/nr-scale-translate-ops.h"
+#include "libnr/nr-convert2geom.h"
 #include "xml/repr.h"
 #include "context-fns.h"
 #include "sp-item.h"
@@ -65,7 +64,8 @@
 #include "display/canvas-bpath.h"
 #include "display/canvas-arena.h"
 #include "livarot/Shape.h"
-#include "2geom/isnan.h"
+#include <2geom/isnan.h>
+#include <2geom/pathvector.h>
 
 #include "dyna-draw-context.h"
 
 
 #define DYNA_MIN_WIDTH 1.0e-6
 
-
-// FIXME: move it to some shared file to be reused by both calligraphy and dropper
-#define C1 0.552
-static NArtBpath const hatch_area_circle[] = {
-    { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
-    { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
-    { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
-    { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
-    { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
-    { NR_END, 0, 0, 0, 0, 0, 0 }
-};
-#undef C1
-
-
 static void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
 static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
 static void sp_dyna_draw_context_dispose(GObject *object);
@@ -103,14 +89,14 @@ static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *even
 
 static void clear_current(SPDynaDrawContext *dc);
 static void set_to_accumulated(SPDynaDrawContext *dc, bool unionize);
-static void add_cap(SPCurve *curve, NR::Point const &from, NR::Point const &to, double rounding);
-static void accumulate_calligraphic(SPDynaDrawContext *dc);
+static void add_cap(SPCurve *curve, Geom::Point const &from, Geom::Point const &to, double rounding);
+static bool accumulate_calligraphic(SPDynaDrawContext *dc);
 
 static void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
 
-static void sp_dyna_draw_reset(SPDynaDrawContext *ddc, NR::Point p);
-static NR::Point sp_dyna_draw_get_npoint(SPDynaDrawContext const *ddc, NR::Point v);
-static NR::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, NR::Point n);
+static void sp_dyna_draw_reset(SPDynaDrawContext *ddc, Geom::Point p);
+static Geom::Point sp_dyna_draw_get_npoint(SPDynaDrawContext const *ddc, Geom::Point v);
+static Geom::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, Geom::Point n);
 static void draw_temporary_box(SPDynaDrawContext *dc);
 
 
@@ -170,9 +156,9 @@ sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
     ddc->hatch_spacing_step = 0;
     new (&ddc->hatch_pointer_past) std::list<double>();
     new (&ddc->hatch_nearest_past) std::list<double>();
-    ddc->hatch_last_nearest = NR::Point(0,0);
-    ddc->hatch_last_pointer = NR::Point(0,0);
-    ddc->hatch_vector_accumulated = NR::Point(0,0);
+    ddc->hatch_last_nearest = Geom::Point(0,0);
+    ddc->hatch_last_pointer = Geom::Point(0,0);
+    ddc->hatch_vector_accumulated = Geom::Point(0,0);
     ddc->hatch_escaped = false;
     ddc->hatch_area = NULL;
     ddc->hatch_item = NULL;
@@ -206,11 +192,11 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
     if (((SPEventContextClass *) dd_parent_class)->setup)
         ((SPEventContextClass *) dd_parent_class)->setup(ec);
 
-    ddc->accumulated = new SPCurve(32);
-    ddc->currentcurve = new SPCurve(4);
+    ddc->accumulated = new SPCurve();
+    ddc->currentcurve = new SPCurve();
 
-    ddc->cal1 = new SPCurve(32);
-    ddc->cal2 = new SPCurve(32);
+    ddc->cal1 = new SPCurve();
+    ddc->cal2 = new SPCurve();
 
     ddc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
@@ -219,7 +205,15 @@ sp_dyna_draw_context_setup(SPEventContext *ec)
     g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
 
     {
-        SPCurve *c = SPCurve::new_from_foreign_bpath(hatch_area_circle);
+        /* TODO: this can be done either with an arcto, and should maybe also be put in a general file (other tools use this as well) */
+        SPCurve *c = new SPCurve();
+        const double C1 = 0.552;
+        c->moveto(-1,0);
+        c->curveto(-1, C1, -C1, 1, 0, 1 );
+        c->curveto(C1, 1, 1, C1, 1, 0 );
+        c->curveto(1, -C1, C1, -1, 0, -1 );
+        c->curveto(-C1, -1, -1, -C1, -1, 0 );
+        c->closepath();
         ddc->hatch_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
         c->unref();
         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->hatch_area), 0x00000000,(SPWindRule)0);
@@ -276,32 +270,32 @@ flerp(double f0, double f1, double p)
 }
 
 /* Get normalized point */
-static NR::Point
-sp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, NR::Point v)
+static Geom::Point
+sp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, Geom::Point v)
 {
-    NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
-    double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
-    return NR::Point(( v[NR::X] - drect.min()[NR::X] ) / max,  ( v[NR::Y] - drect.min()[NR::Y] ) / max);
+    Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
+    double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
+    return Geom::Point(( v[Geom::X] - drect.min()[Geom::X] ) / max,  ( v[Geom::Y] - drect.min()[Geom::Y] ) / max);
 }
 
 /* Get view point */
-static NR::Point
-sp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, NR::Point n)
+static Geom::Point
+sp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, Geom::Point n)
 {
-    NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
-    double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
-    return NR::Point(n[NR::X] * max + drect.min()[NR::X], n[NR::Y] * max + drect.min()[NR::Y]);
+    Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
+    double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
+    return Geom::Point(n[Geom::X] * max + drect.min()[Geom::X], n[Geom::Y] * max + drect.min()[Geom::Y]);
 }
 
 static void
-sp_dyna_draw_reset(SPDynaDrawContext *dc, NR::Point p)
+sp_dyna_draw_reset(SPDynaDrawContext *dc, Geom::Point p)
 {
     dc->last = dc->cur = sp_dyna_draw_get_npoint(dc, p);
-    dc->vel = NR::Point(0,0);
+    dc->vel = Geom::Point(0,0);
     dc->vel_max = 0;
-    dc->acc = NR::Point(0,0);
-    dc->ang = NR::Point(0,0);
-    dc->del = NR::Point(0,0);
+    dc->acc = Geom::Point(0,0);
+    dc->ang = Geom::Point(0,0);
+    dc->del = Geom::Point(0,0);
 }
 
 static void
@@ -325,16 +319,16 @@ sp_dyna_draw_extinput(SPDynaDrawContext *dc, GdkEvent *event)
 
 
 static gboolean
-sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
+sp_dyna_draw_apply(SPDynaDrawContext *dc, Geom::Point p)
 {
-    NR::Point n = sp_dyna_draw_get_npoint(dc, p);
+    Geom::Point n = sp_dyna_draw_get_npoint(dc, p);
 
     /* Calculate mass and drag */
     double const mass = flerp(1.0, 160.0, dc->mass);
     double const drag = flerp(0.0, 0.5, dc->drag * dc->drag);
 
     /* Calculate force and acceleration */
-    NR::Point force = n - dc->cur;
+    Geom::Point force = n - dc->cur;
 
     // If force is below the absolute threshold DYNA_EPSILON,
     // or we haven't yet reached DYNA_VEL_START (i.e. at the beginning of stroke)
@@ -363,7 +357,7 @@ sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
         gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);;
 
         if (length > 0) {
-            NR::Point ang1 = NR::Point(dc->ytilt/length, dc->xtilt/length);
+            Geom::Point ang1 = Geom::Point(dc->ytilt/length, dc->xtilt/length);
             a1 = atan2(ang1);
         }
         else
@@ -372,7 +366,7 @@ sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
     else {
         // 1b. fixed dc->angle (absolutely flat nib):
         double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI;
-        NR::Point ang1 = NR::Point(-sin(radians),  cos(radians));
+        Geom::Point ang1 = Geom::Point(-sin(radians),  cos(radians));
         a1 = atan2(ang1);
     }
 
@@ -381,7 +375,7 @@ sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
     if ( mag_vel < DYNA_EPSILON ) {
         return FALSE;
     }
-    NR::Point ang2 = NR::rot90(dc->vel) / mag_vel;
+    Geom::Point ang2 = NR::rot90(dc->vel) / mag_vel;
 
     // 3. Average them using flatness parameter:
     // calculate angles
@@ -403,13 +397,13 @@ sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
 
     // Try to detect a sudden flip when the new angle differs too much from the previous for the
     // current velocity; in that case discard this move
-    double angle_delta = NR::L2(NR::Point (cos (new_ang), sin (new_ang)) - dc->ang);
+    double angle_delta = NR::L2(Geom::Point (cos (new_ang), sin (new_ang)) - dc->ang);
     if ( angle_delta / NR::L2(dc->vel) > 4000 ) {
         return FALSE;
     }
 
     // convert to point
-    dc->ang = NR::Point (cos (new_ang), sin (new_ang));
+    dc->ang = Geom::Point (cos (new_ang), sin (new_ang));
 
 //    g_print ("force %g  acc %g  vel_max %g  vel %g  a1 %g  a2 %g  new_ang %g\n", NR::L2(force), NR::L2(dc->acc), dc->vel_max, NR::L2(dc->vel), a1, a2, new_ang);
 
@@ -436,8 +430,8 @@ sp_dyna_draw_brush(SPDynaDrawContext *dc)
 
     // get the real brush point, not the same as pointer (affected by hatch tracking and/or mass
     // drag)
-    NR::Point brush = sp_dyna_draw_get_vpoint(dc, dc->cur);
-    NR::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush); 
+    Geom::Point brush = sp_dyna_draw_get_vpoint(dc, dc->cur);
+    Geom::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush); 
 
     double trace_thick = 1;
     if (dc->trace_bg) {
@@ -492,8 +486,8 @@ sp_dyna_draw_brush(SPDynaDrawContext *dc)
         dezoomify_factor /= SP_EVENT_CONTEXT(dc)->desktop->current_zoom();
     }
 
-    NR::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang;
-    NR::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang;
+    Geom::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang;
+    Geom::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang;
 
     dc->point1[dc->npoints] = brush + del_left;
     dc->point2[dc->npoints] = brush - del_right;
@@ -549,9 +543,9 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                     return TRUE;
                 }
 
-                NR::Point const button_w(event->button.x,
+                Geom::Point const button_w(event->button.x,
                                          event->button.y);
-                NR::Point const button_dt(desktop->w2d(button_w));
+                Geom::Point const button_dt(desktop->w2d(button_w));
                 sp_dyna_draw_reset(dc, button_dt);
                 sp_dyna_draw_extinput(dc, event);
                 sp_dyna_draw_apply(dc, button_dt);
@@ -579,19 +573,19 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
             break;
         case GDK_MOTION_NOTIFY:
         {
-            NR::Point const motion_w(event->motion.x,
+            Geom::Point const motion_w(event->motion.x,
                                      event->motion.y);
-            NR::Point motion_dt(desktop->w2d(motion_w));
+            Geom::Point motion_dt(desktop->w2d(motion_w));
             sp_dyna_draw_extinput(dc, event);
 
             dc->_message_context->clear();
 
             // for hatching:
             double hatch_dist = 0;
-            NR::Point hatch_unit_vector(0,0);
-            NR::Point nearest(0,0);
-            NR::Point pointer(0,0);
-            NR::Matrix motion_to_curve(NR::identity());
+            Geom::Point hatch_unit_vector(0,0);
+            Geom::Point nearest(0,0);
+            Geom::Point pointer(0,0);
+            Geom::Matrix motion_to_curve(Geom::identity());
 
             if (event->motion.state & GDK_CONTROL_MASK) { // hatching - sense the item
 
@@ -609,11 +603,11 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                     }
 
                     // calculate pointer point in the guide item's coords
-                    motion_to_curve = from_2geom(sp_item_dt2i_affine(selected) * sp_item_i2doc_affine(selected));
+                    motion_to_curve = sp_item_dt2i_affine(selected) * sp_item_i2doc_affine(selected);
                     pointer = motion_dt * motion_to_curve;
 
                     // calculate the nearest point on the guide path
-                    NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
+                    boost::optional<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
                     nearest = get_point_on_Path(dc->hatch_livarot_path, position->piece, position->t);
 
 
@@ -709,7 +703,7 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
                             }
 
                             // This is the track pointer that we will use instead of the real one
-                            NR::Point new_pointer = nearest + target * hatch_unit_vector;
+                            Geom::Point new_pointer = nearest + target * hatch_unit_vector;
 
                             // some limited feedback: allow persistent pulling to slightly change
                             // the spacing
@@ -752,31 +746,31 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
             if (event->motion.state & GDK_CONTROL_MASK) { 
                 if (dc->hatch_spacing == 0 && hatch_dist != 0) { 
                     // Haven't set spacing yet: gray, center free, update radius live
-                    NR::Point c = desktop->w2d(motion_w);
-                    NR::Matrix const sm (NR::scale(hatch_dist, hatch_dist) * NR::translate(c));
+                    Geom::Point c = desktop->w2d(motion_w);
+                    NR::Matrix const sm (Geom::Scale(hatch_dist, hatch_dist) * Geom::Translate(c));
                     sp_canvas_item_affine_absolute(dc->hatch_area, sm);
                     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
                     sp_canvas_item_show(dc->hatch_area);
                 } else if (dc->dragging && !dc->hatch_escaped) {
                     // Tracking: green, center snapped, fixed radius
-                    NR::Point c = motion_dt;
-                    NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
+                    Geom::Point c = motion_dt;
+                    NR::Matrix const sm (Geom::Scale(dc->hatch_spacing, dc->hatch_spacing) * Geom::Translate(c));
                     sp_canvas_item_affine_absolute(dc->hatch_area, sm);
                     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x00FF00ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
                     sp_canvas_item_show(dc->hatch_area);
                 } else if (dc->dragging && dc->hatch_escaped) {
                     // Tracking escaped: red, center free, fixed radius
-                    NR::Point c = desktop->w2d(motion_w);
-                    NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
+                    Geom::Point c = desktop->w2d(motion_w);
+                    NR::Matrix const sm (Geom::Scale(dc->hatch_spacing, dc->hatch_spacing) * Geom::Translate(c));
 
                     sp_canvas_item_affine_absolute(dc->hatch_area, sm);
                     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0xFF0000ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
                     sp_canvas_item_show(dc->hatch_area);
                 } else {
                     // Not drawing but spacing set: gray, center snapped, fixed radius
-                    NR::Point c = (nearest + dc->hatch_spacing * hatch_unit_vector) * motion_to_curve.inverse();
+                    Geom::Point c = (nearest + dc->hatch_spacing * hatch_unit_vector) * motion_to_curve.inverse();
                     if (!IS_NAN(c[NR::X]) && !IS_NAN(c[NR::Y])) {
-                        NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
+                        NR::Matrix const sm (Geom::Scale(dc->hatch_spacing, dc->hatch_spacing) * Geom::Translate(c));
                         sp_canvas_item_affine_absolute(dc->hatch_area, sm);
                         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
                         sp_canvas_item_show(dc->hatch_area);
@@ -791,8 +785,8 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
     case GDK_BUTTON_RELEASE:
     {
-        NR::Point const motion_w(event->button.x, event->button.y);
-        NR::Point const motion_dt(desktop->w2d(motion_w));
+        Geom::Point const motion_w(event->button.x, event->button.y);
+        Geom::Point const motion_dt(desktop->w2d(motion_w));
 
         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
         sp_canvas_end_forced_full_redraws(desktop->canvas);
@@ -811,8 +805,10 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
             /* Create object */
             fit_and_split(dc, TRUE);
-            accumulate_calligraphic(dc);
-            set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
+            if (accumulate_calligraphic(dc))
+                set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
+            else
+                g_warning ("Failed to create path: invalid data in dc->cal1 or dc->cal2");
 
             /* reset accumulated curve */
             dc->accumulated->reset();
@@ -824,9 +820,9 @@ sp_dyna_draw_context_root_handler(SPEventContext *event_context,
 
             if (!dc->hatch_pointer_past.empty()) dc->hatch_pointer_past.clear();
             if (!dc->hatch_nearest_past.empty()) dc->hatch_nearest_past.clear();
-            dc->hatch_last_nearest = NR::Point(0,0);
-            dc->hatch_last_pointer = NR::Point(0,0);
-            dc->hatch_vector_accumulated = NR::Point(0,0);
+            dc->hatch_last_nearest = Geom::Point(0,0);
+            dc->hatch_last_pointer = Geom::Point(0,0);
+            dc->hatch_vector_accumulated = Geom::Point(0,0);
             dc->hatch_escaped = false;
             dc->hatch_item = NULL;
             dc->hatch_livarot_path = NULL;
@@ -971,9 +967,6 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
     SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
 
     if (!dc->accumulated->is_empty()) {
-        NArtBpath *abp;
-        gchar *str;
-
         if (!dc->repr) {
             /* Create object */
             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
@@ -989,16 +982,15 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
             item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
             item->updateRepr();
         }
-        abp = nr_artpath_affine(dc->accumulated->get_bpath(), sp_desktop_dt2root_affine(desktop));
-        str = sp_svg_write_path(abp);
+        Geom::PathVector pathv = dc->accumulated->get_pathvector() * sp_desktop_dt2root_affine(desktop);
+        gchar *str = sp_svg_write_path(pathv);
         g_assert( str != NULL );
-        g_free(abp);
         dc->repr->setAttribute("d", str);
         g_free(str);
 
         if (unionize) {
             sp_desktop_selection(desktop)->add(dc->repr);
-            sp_selected_path_union_skip_undo();
+            sp_selected_path_union_skip_undo(desktop);
         } else {
             if (dc->keep_selected) {
                 sp_desktop_selection(desktop)->set(dc->repr);
@@ -1018,47 +1010,70 @@ set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
 
 static void
 add_cap(SPCurve *curve,
-        NR::Point const &from,
-        NR::Point const &to, 
+        Geom::Point const &from,
+        Geom::Point const &to, 
         double rounding)
 {
     if (NR::L2( to - from ) > DYNA_EPSILON) {
-        NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
+        Geom::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
         double mag = NR::L2(vel);
 
-        NR::Point v = mag * NR::rot90( to - from ) / NR::L2( to - from );
+        Geom::Point v = mag * NR::rot90( to - from ) / NR::L2( to - from );
         curve->curveto(from + v, to + v, to);
     }
 }
 
-static void
+static bool
 accumulate_calligraphic(SPDynaDrawContext *dc)
 {
-    if ( !dc->cal1->is_empty() && !dc->cal2->is_empty() ) {
-        dc->accumulated->reset(); /*  Is this required ?? */
-        SPCurve *rev_cal2 = dc->cal2->create_reverse();
+        if (
+            dc->cal1->is_empty() ||
+            dc->cal2->is_empty() ||
+            (dc->cal1->get_segment_count() <= 0) ||
+            dc->cal1->first_path()->closed() 
+            ) {
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
 
-        g_assert(dc->cal1->get_segment_count() > 0);
-        g_assert(rev_cal2->get_segment_count() > 0);
-        g_assert( ! dc->cal1->first_path()->closed() );
-        g_assert( ! rev_cal2->first_path()->closed() );
+        SPCurve *rev_cal2 = dc->cal2->create_reverse();
+        if (
+            (rev_cal2->get_segment_count() <= 0) ||
+            rev_cal2->first_path()->closed() 
+            ) {
+            rev_cal2->unref();
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
 
         Geom::CubicBezier const * dc_cal1_firstseg  = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->first_segment() );
         Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->first_segment() );
         Geom::CubicBezier const * dc_cal1_lastseg   = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->last_segment() );
         Geom::CubicBezier const * rev_cal2_lastseg  = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
-        g_assert( dc_cal1_firstseg );
-        g_assert( rev_cal2_firstseg );
-        g_assert( dc_cal1_lastseg );
-        g_assert( rev_cal2_lastseg );
+
+        if (
+            !dc_cal1_firstseg ||
+            !rev_cal2_firstseg ||
+            !dc_cal1_lastseg ||
+            !rev_cal2_lastseg 
+            ) {
+            rev_cal2->unref();
+            dc->cal1->reset();
+            dc->cal2->reset();
+            return false; // failure
+        }
+
+        dc->accumulated->reset(); /*  Is this required ?? */
 
         dc->accumulated->append(dc->cal1, false);
 
-        add_cap(dc->accumulated, (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[3], dc->cap_rounding);
+        add_cap(dc->accumulated, (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], dc->cap_rounding);
 
         dc->accumulated->append(rev_cal2, true);
 
-        add_cap(dc->accumulated, (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[3], dc->cap_rounding);
+        add_cap(dc->accumulated, (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[0], dc->cap_rounding);
 
         dc->accumulated->closepath();
 
@@ -1066,7 +1081,8 @@ accumulate_calligraphic(SPDynaDrawContext *dc)
 
         dc->cal1->reset();
         dc->cal2->reset();
-    }
+
+        return true; // success
 }
 
 static double square(double const x)
@@ -1077,7 +1093,9 @@ static double square(double const x)
 static void
 fit_and_split(SPDynaDrawContext *dc, gboolean release)
 {
-    double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_CALLIGRAPHIC );
+    SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
+
+    double const tolerance_sq = square( desktop->w2d().descrim() * TOLERANCE_CALLIGRAPHIC );
 
 #ifdef DYNA_DRAW_VERBOSE
     g_print("[F&S:R=%c]", release?'T':'F');
@@ -1097,7 +1115,7 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
 #endif
 
         /* Current calligraphic */
-        if ( dc->cal1->get_length() == 0 || dc->cal2->get_length() == 0 ) {
+        if ( dc->cal1->is_empty() || dc->cal2->is_empty() ) {
             /* dc->npoints > 0 */
             /* g_print("calligraphics(1|2) reset\n"); */
             dc->cal1->reset();
@@ -1107,12 +1125,12 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
             dc->cal2->moveto(dc->point2[0]);
         }
 
-        NR::Point b1[BEZIER_MAX_LENGTH];
+        Geom::Point b1[BEZIER_MAX_LENGTH];
         gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
                                                tolerance_sq, BEZIER_MAX_BEZIERS);
         g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) );
 
-        NR::Point b2[BEZIER_MAX_LENGTH];
+        Geom::Point b2[BEZIER_MAX_LENGTH];
         gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
                                                tolerance_sq, BEZIER_MAX_BEZIERS);
         g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) );
@@ -1126,12 +1144,11 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
             if (! release) {
                 dc->currentcurve->reset();
                 dc->currentcurve->moveto(b1[0]);
-                for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
-                    dc->currentcurve->curveto(bp1[1],
-                                     bp1[2], bp1[3]);
+                for (Geom::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
+                    dc->currentcurve->curveto(bp1[1], bp1[2], bp1[3]);
                 }
                 dc->currentcurve->lineto(b2[BEZIER_SIZE*(nb2-1) + 3]);
-                for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
+                for (Geom::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
                     dc->currentcurve->curveto(bp2[2], bp2[1], bp2[0]);
                 }
                 // FIXME: dc->segments is always NULL at this point??
@@ -1143,10 +1160,10 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
             }
 
             /* Current calligraphic */
-            for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
+            for (Geom::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
                 dc->cal1->curveto(bp1[1], bp1[2], bp1[3]);
             }
-            for (NR::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
+            for (Geom::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
                 dc->cal2->curveto(bp2[1], bp2[2], bp2[3]);
             }
         } else {
@@ -1171,24 +1188,24 @@ fit_and_split(SPDynaDrawContext *dc, gboolean release)
         if (!release) {
             g_assert(!dc->currentcurve->is_empty());
 
-            SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(SP_EVENT_CONTEXT(dc)->desktop),
+            SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(desktop),
                                                    SP_TYPE_CANVAS_BPATH,
                                                    NULL);
             SPCurve *curve = dc->currentcurve->copy();
             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
             curve->unref();
 
-            guint32 fillColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", true);
-            //guint32 strokeColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", false);
-            double opacity = sp_desktop_get_master_opacity_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic");
-            double fillOpacity = sp_desktop_get_opacity_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", true);
-            //double strokeOpacity = sp_desktop_get_opacity_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", false);
+            guint32 fillColor = sp_desktop_get_color_tool (desktop, "tools.calligraphic", true);
+            //guint32 strokeColor = sp_desktop_get_color_tool (desktop, "tools.calligraphic", false);
+            double opacity = sp_desktop_get_master_opacity_tool (desktop, "tools.calligraphic");
+            double fillOpacity = sp_desktop_get_opacity_tool (desktop, "tools.calligraphic", true);
+            //double strokeOpacity = sp_desktop_get_opacity_tool (desktop, "tools.calligraphic", false);
             sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), ((fillColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*fillOpacity)), SP_WIND_RULE_EVENODD);
             //on second thougtht don't do stroke yet because we don't have stoke-width yet and because stoke appears between segments while drawing
             //sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), ((strokeColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*strokeOpacity)), 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
             sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
             /* fixme: Cannot we cascade it to root more clearly? */
-            g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), SP_EVENT_CONTEXT(dc)->desktop);
+            g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), desktop);
 
             dc->segments = g_slist_prepend(dc->segments, cbp);
         }