Code

fix 1243587 and misc fixes
[inkscape.git] / src / display / nr-arena-shape.cpp
index 2f278b3de56fa9ed92e6e09f2a4d9850c10cee83..c5551695fc84514c788a005b19f49df9e0845f15 100644 (file)
@@ -14,6 +14,7 @@
 
 
 
+#include <display/canvas-arena.h>
 #include <display/nr-arena.h>
 #include <display/nr-arena-shape.h>
 #include "display/nr-filter.h"
 #include <livarot/float-line.h>
 #include <livarot/int-line.h>
 #include <style.h>
-/* prefs-utils used for deciding, whether to run filtering test or not */
 #include "prefs-utils.h"
 #include "sp-filter.h"
 #include "sp-gaussian-blur.h"
+#include "inkscape-cairo.h"
+
+#include <cairo.h>
 
 //int  showRuns=0;
 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
@@ -47,7 +50,7 @@ static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
 
 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
-static unsigned int nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
+static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
 
@@ -92,6 +95,10 @@ nr_arena_shape_class_init(NRArenaShapeClass *klass)
     item_class->pick = nr_arena_shape_pick;
 }
 
+/**
+ * Initializes the arena shape, setting all parameters to null, 0, false,
+ * or other defaults
+ */
 static void
 nr_arena_shape_init(NRArenaShape *shape)
 {
@@ -118,6 +125,9 @@ nr_arena_shape_init(NRArenaShape *shape)
     nr_matrix_set_identity(&shape->cached_sctm);
 
     shape->markers = NULL;
+
+    shape->last_pick = NULL;
+    shape->repick_after = 0;
 }
 
 static void
@@ -138,6 +148,9 @@ nr_arena_shape_finalize(NRObject *object)
     ((NRObjectClass *) shape_parent_class)->finalize(object);
 }
 
+/**
+ * Retrieves the markers from the item
+ */
 static NRArenaItem *
 nr_arena_shape_children(NRArenaItem *item)
 {
@@ -146,6 +159,11 @@ nr_arena_shape_children(NRArenaItem *item)
     return shape->markers;
 }
 
+/**
+ * Attaches child to item, and if ref is not NULL, sets it and ref->next as
+ * the prev and next items.  If ref is NULL, then it sets the item's markers
+ * as the next items.
+ */
 static void
 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
 {
@@ -160,6 +178,10 @@ nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref
     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
 }
 
+/**
+ * Removes child from the shape.  If there are no prev items in 
+ * the child, it sets items' markers to the next item in the child.
+ */
 static void
 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
 {
@@ -174,6 +196,12 @@ nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
 }
 
+/**
+ * Detaches child from item, and if there are no previous items in child, it 
+ * sets item's markers to the child.  It then attaches the child back onto the item.
+ * If ref is null, it sets the markers to be the next item, otherwise it uses
+ * the next/prev items in ref.
+ */
 static void
 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
 {
@@ -198,6 +226,9 @@ void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
 
+/**
+ * Updates the arena shape 'item' and all of its children, including the markers.
+ */
 static guint
 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
 {
@@ -243,6 +274,8 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
     bbox.x0 = bbox.y0 = NR_HUGE;
     bbox.x1 = bbox.y1 = -NR_HUGE;
 
+    bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
+
     if (shape->curve) {
         NRBPath bp;
         /* fixme: */
@@ -250,7 +283,7 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
         bbox.x1 = bbox.y1 = -NR_HUGE;
         bp.path = SP_CURVE_BPATH(shape->curve);
         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
-        if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) {
+        if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
             float width, scale;
             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
             width = MAX(0.125, shape->_stroke.width * scale);
@@ -299,11 +332,13 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
         shape->stroke_painter = NULL;
     }
 
-    if (!shape->curve || !shape->style) return NR_ARENA_ITEM_STATE_ALL;
-    if (sp_curve_is_empty(shape->curve)) return NR_ARENA_ITEM_STATE_ALL;
-    if ( ( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
-         ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE ) )
+    if (!shape->curve || 
+        !shape->style ||
+        sp_curve_is_empty(shape->curve) ||
+        (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
+         ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
     {
+        item->bbox = shape->approx_bbox;
         return NR_ARENA_ITEM_STATE_ALL;
     }
 
@@ -348,11 +383,12 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
         }
         item->render_opacity = FALSE;
     }
-    if ( item->render_opacity == TRUE
-         && shape->_fill.paint.type()   != NRArenaShape::Paint::NONE
-         && shape->_stroke.paint.type() != NRArenaShape::Paint::NONE )
+    if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
+           shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
+          || (shape->markers)
+        )
     {
-        // don't merge item opacity with paint opacity if there is a stroke on the fill
+        // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
         item->render_opacity = FALSE;
     }
 
@@ -486,13 +522,27 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
 
     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
 
-    if (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE ||
+    bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
+
+    if (outline) {
+        // cairo does not need the livarot path for rendering
+        return; 
+    }
+
+    // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
+    // (since it will only be used for picking, not for rendering)
+
+    if (outline ||
         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
 
-        float width = MAX(0.125, shape->_stroke.width * scale);
-        if (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE)
+        float style_width = MAX(0.125, shape->_stroke.width * scale);
+        float width;
+        if (outline) {
             width = 0.5; // 1 pixel wide, independent of zoom
+        } else {
+            width = style_width;
+        }
 
         NR::Matrix  cached_to_new = NR::identity();
 
@@ -506,6 +556,10 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
             }
             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
                 isometry = 0;
+            if (0 != isometry && width != shape->cached_width) { 
+                // if this happens without setting style, we have just switched to outline or back
+                isometry = 0; 
+            } 
         }
 
         if ( isometry == 0 ) {
@@ -524,16 +578,16 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
             padded_area.x1 += (NR::ICoord)width;
             padded_area.y0 -= (NR::ICoord)width;
             padded_area.y1 += (NR::ICoord)width;
-            if ((style->stroke_dash.n_dash && NR_ARENA_ITEM(shape)->arena->rendermode != RENDERMODE_OUTLINE) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
-                thePath->Convert((NR_ARENA_ITEM(shape)->arena->rendermode != RENDERMODE_OUTLINE) ? 1.0 : 4.0);
+            if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
+                thePath->Convert((outline) ? 4.0 : 1.0);
                 shape->cached_spartialy = false;
             }
             else {
-                thePath->Convert(&padded_area, (NR_ARENA_ITEM(shape)->arena->rendermode != RENDERMODE_OUTLINE) ? 1.0 : 4.0);
+                thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
                 shape->cached_spartialy = true;
             }
 
-            if (style->stroke_dash.n_dash && NR_ARENA_ITEM(shape)->arena->rendermode != RENDERMODE_OUTLINE) {
+            if (style->stroke_dash.n_dash && !outline) {
                 thePath->DashPolylineFromStyle(style, scale, 1.0);
             }
 
@@ -562,7 +616,7 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
                     break;
             }
 
-            if (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE) {
+            if (outline) {
                 butt = butt_straight;
                 join = join_straight;
             }
@@ -571,13 +625,15 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
                             0.5*width*shape->_stroke.mitre_limit);
 
 
-            if (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE) {
+            if (outline) {
                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
                 shape->cached_stroke->Copy(theShape);
             } else {
                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
             }
 
+            shape->cached_width = width;
+
             shape->cached_sctm=gc->transform;
             shape->cached_sarea = *area;
             delete thePath;
@@ -673,14 +729,142 @@ nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
         }
     }
 }
+
+// cairo outline rendering:
+static unsigned int
+cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
+{
+    NRArenaShape *shape = NR_ARENA_SHAPE(item);
+
+    if (!ct) 
+        return item->state;
+
+    guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
+    // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
+    // by setting color channels in the "wrong" order
+    cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
+
+    cairo_set_line_width(ct, 0.5);
+    cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
+    cairo_new_path(ct);
+
+    feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
+
+    cairo_stroke(ct);
+
+    return item->state;
+}
+
+// cairo stroke rendering (flat color only so far!):
+// works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
+// (need to switch them to premul before this can be enabled)
+void
+cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
+{
+    NRArenaShape *shape = NR_ARENA_SHAPE(item);
+    SPStyle const *style = shape->style;
+
+    float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
+
+    if (fabs(shape->_stroke.width * scale) < 0.01)
+        return;
+
+    cairo_t *ct = nr_create_cairo_context (area, pb);
+
+    if (!ct) 
+        return;
+
+    guint32 rgba;
+    if ( item->render_opacity ) {
+        rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
+                                          shape->_stroke.opacity *
+                                          SP_SCALE24_TO_FLOAT(style->opacity.value));
+    } else {
+        rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
+                                          shape->_stroke.opacity);
+    }
+
+    // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
+    // by setting color channels in the "wrong" order
+    cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
+
+    float style_width = MAX(0.125, shape->_stroke.width * scale);
+    cairo_set_line_width(ct, style_width);
+
+    switch (shape->_stroke.cap) {
+        case NRArenaShape::BUTT_CAP:
+            cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
+            break;
+        case NRArenaShape::ROUND_CAP:
+            cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
+            break;
+        case NRArenaShape::SQUARE_CAP:
+            cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
+            break;
+    }
+    switch (shape->_stroke.join) {
+        case NRArenaShape::MITRE_JOIN:
+            cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
+            break;
+        case NRArenaShape::ROUND_JOIN:
+            cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
+            break;
+        case NRArenaShape::BEVEL_JOIN:
+            cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
+            break;
+    }
+
+    cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
+
+    if (style->stroke_dash.n_dash) {
+        NRVpathDash dash;
+        dash.offset = style->stroke_dash.offset * scale;
+        dash.n_dash = style->stroke_dash.n_dash;
+        dash.dash = g_new(double, dash.n_dash);
+        for (int i = 0; i < dash.n_dash; i++) {
+            dash.dash[i] = style->stroke_dash.dash[i] * scale;
+        }
+        cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
+        g_free(dash.dash);
+    }
+
+    cairo_set_tolerance(ct, 0.1);
+    cairo_new_path(ct);
+
+    feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
+
+    cairo_stroke(ct);
+
+    cairo_surface_t *cst = cairo_get_target(ct);
+    cairo_destroy (ct);
+    cairo_surface_finish (cst);
+    cairo_surface_destroy (cst);
+
+    pb->empty = FALSE;
+}
+
+
+/**
+ * Renders the item.  Markers are just composed into the parent buffer.
+ */
 static unsigned int
-nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
+nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
 {
     NRArenaShape *shape = NR_ARENA_SHAPE(item);
 
     if (!shape->curve) return item->state;
     if (!shape->style) return item->state;
 
+    bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
+
+    if (outline) { // cairo outline rendering
+
+        pb->empty = FALSE;
+        unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
+        if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
+
+    } else {
+
     if ( shape->delayed_shp ) {
         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
             NRGC   tempGC(NULL);
@@ -702,11 +886,18 @@ nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned
     }
 
     SPStyle const *style = shape->style;
-    if ( shape->fill_shp && NR_ARENA_ITEM(shape)->arena->rendermode != RENDERMODE_OUTLINE) {
+    if (shape->fill_shp) {
         NRPixBlock m;
         guint32 rgba;
 
         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
+
+        // if memory allocation failed, abort render
+        if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
+            nr_pixblock_release (&m);
+            return (item->state);
+        }
+
         m.visible_area = pb->visible_area; 
         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
         m.empty = FALSE;
@@ -733,20 +924,26 @@ nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned
         nr_pixblock_release(&m);
     }
 
-    if ( shape->stroke_shp ) {
-        NRPixBlock m;
+    if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
+
+        // cairo_arena_shape_render_stroke(item, area, pb);
+
         guint32 rgba;
+        NRPixBlock m;
 
         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
+
+        // if memory allocation failed, abort render
+        if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
+            nr_pixblock_release (&m);
+            return (item->state);
+        }
+
         m.visible_area = pb->visible_area; 
         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
         m.empty = FALSE;
 
-        if (shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR ||
-            NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE) {
-            if ( NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE) {
-                rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
-            } else if ( item->render_opacity ) {
+            if ( item->render_opacity ) {
                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
                                                   shape->_stroke.opacity *
                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
@@ -756,28 +953,81 @@ nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned
             }
             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
             pb->empty = FALSE;
-        } else if (shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
-            if (shape->stroke_painter) {
-                nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
-            }
+
+        nr_pixblock_release(&m);
+
+    } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
+
+        NRPixBlock m;
+
+        nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
+
+        // if memory allocation failed, abort render
+        if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
+            nr_pixblock_release (&m);
+            return (item->state);
+        }
+
+        m.visible_area = pb->visible_area; 
+        nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
+        m.empty = FALSE;
+
+        if (shape->stroke_painter) {
+            nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
         }
 
         nr_pixblock_release(&m);
     }
 
-    /* Just compose children into parent buffer */
+    } // non-cairo non-outline branch
+
+    /* Render markers into parent buffer */
     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
-        unsigned int ret;
-        ret = nr_arena_item_invoke_render(child, area, pb, flags);
+        unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
     }
 
     return item->state;
 }
 
+
+// cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
+// reenable this when the bug is fixed and remove the rest of this function
+static guint
+cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
+{
+    NRArenaShape *shape = NR_ARENA_SHAPE(item);
+    if (!shape->curve) return item->state;
+
+        cairo_t *ct = nr_create_cairo_context (area, pb);
+
+        if (!ct) 
+            return item->state;
+
+        cairo_set_source_rgba(ct, 0, 0, 0, 1);
+
+        cairo_new_path(ct);
+
+        feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
+
+        cairo_fill(ct);
+
+        cairo_surface_t *cst = cairo_get_target(ct);
+        cairo_destroy (ct);
+        cairo_surface_finish (cst);
+        cairo_surface_destroy (cst);
+
+        pb->empty = FALSE;
+
+        return item->state;
+}
+
+
 static guint
 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
 {
+    //return cairo_arena_shape_clip(item, area, pb);
+
     NRArenaShape *shape = NR_ARENA_SHAPE(item);
     if (!shape->curve) return item->state;
 
@@ -797,6 +1047,13 @@ nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
 
         /* fixme: We can OR in one step (Lauris) */
         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
+
+        // if memory allocation failed, abort 
+        if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
+            nr_pixblock_release (&m);
+            return (item->state);
+        }
+
         m.visible_area = pb->visible_area; 
         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
 
@@ -822,75 +1079,89 @@ nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /
 {
     NRArenaShape *shape = NR_ARENA_SHAPE(item);
 
+    if (shape->repick_after > 0)
+        shape->repick_after--;
+
+    if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
+        return shape->last_pick;
+
     if (!shape->curve) return NULL;
     if (!shape->style) return NULL;
-    if ( shape->delayed_shp ) {
-        NRRectL  area, updateArea;
-        area.x0=(int)floor(p[NR::X]);
-        area.x1=(int)ceil(p[NR::X]);
-        area.y0=(int)floor(p[NR::Y]);
-        area.y1=(int)ceil(p[NR::Y]);
-        int idelta = (int)ceil(delta) + 1;
-        // njh: inset rect
-        area.x0-=idelta;
-        area.x1+=idelta;
-        area.y0-=idelta;
-        area.y1+=idelta;
-        if ( nr_rect_l_test_intersect(&area, &item->bbox) ) {
-            NRGC   tempGC(NULL);
-            tempGC.transform=shape->ctm;
-            updateArea = item->bbox;
-            if (shape->cached_stroke)
-                nr_rect_l_intersect (&updateArea, &updateArea, &shape->cached_sarea);
+    if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
+        return NULL;
 
-            shape->delayed_shp = false;
-            nr_arena_shape_update_stroke(shape, &tempGC, &updateArea);
-            nr_arena_shape_update_fill(shape, &tempGC, &updateArea);
-            /*      NRRect bbox;
-                    bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
-                    nr_arena_shape_add_bboxes(shape,bbox);
-                    item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
-                    item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
-                    item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
-                    item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
-                    shape->approx_bbox=item->bbox;*/
-        }
+    GTimeVal tstart, tfinish;
+    g_get_current_time (&tstart);
+
+    bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
+
+    double width;
+    if (outline) {
+        width = 0.5;
+    } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
+        float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
+        width = MAX(0.125, shape->_stroke.width * scale) / 2;
+    } else {
+        width = 0;
     }
 
-    if (item->state & NR_ARENA_ITEM_STATE_RENDER) {
-        if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
-            if (shape->fill_shp->PtWinding(p) > 0 ) return item;
-        }
-        if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)) {
-            if (shape->stroke_shp->PtWinding(p) > 0 ) return item;
-        }
-        if (delta > 1e-3) {
-            if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
-                if (distanceLessThanOrEqual(shape->fill_shp, p, delta)) return item;
+    NRBPath bp;
+    bp.path = SP_CURVE_BPATH(shape->curve);
+    double dist = NR_HUGE;
+    int wind = 0;
+    bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
+             && shape->_fill.opacity > 1e-3 && !outline);
+
+    if (item->arena->canvasarena) {
+        NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
+        viewbox.growBy (width);
+        nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
+    } else {
+        nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
+    }
+
+    g_get_current_time (&tfinish);
+    glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
+    //g_print ("pick time %lu\n", this_pick);
+
+    if (this_pick > 10000) { // slow picking, remember to skip several new picks
+        shape->repick_after = this_pick / 5000;
+    }
+
+    // covered by fill?
+    if (needfill) {
+        if (!shape->style->fill_rule.computed) {
+            if (wind != 0) {
+                shape->last_pick = item;
+                return item;
             }
-            if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)) {
-                if (distanceLessThanOrEqual(shape->stroke_shp, p, delta)) return item;
+        } else {
+            if (wind & 0x1) {
+                shape->last_pick = item;
+                return item;
             }
         }
-    } else {
-        NRBPath bp;
-        bp.path = SP_CURVE_BPATH(shape->curve);
-        double dist = NR_HUGE;
-        int wind = 0;
-        nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, &wind, &dist, NR_EPSILON);
-        if (shape->_fill.paint.type() != NRArenaShape::Paint::NONE) {
-            if (!shape->style->fill_rule.computed) {
-                if (wind != 0) return item;
-            } else {
-                if (wind & 0x1) return item;
-            }
+    }
+
+    // close to the edge, as defined by strokewidth and delta?
+    // this ignores dashing (as if the stroke is solid) and always works as if caps are round
+    if (needfill || width > 0) { // if either fill or stroke visible,
+        if ((dist - width) < delta) {
+            shape->last_pick = item;
+            return item;
         }
-        if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) {
-            /* fixme: We do not take stroke width into account here (Lauris) */
-            if (dist < delta) return item;
+    }
+
+    // if not picked on the shape itself, try its markers
+    for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
+        NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
+        if (ret) {
+            shape->last_pick = item;
+            return item;
         }
     }
 
+    shape->last_pick = NULL;
     return NULL;
 }
 
@@ -1103,8 +1374,7 @@ nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
         {
             SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
             //if primitive is gaussianblur
-//            if(SP_IS_GAUSSIANBLUR(primitive))
-            {
+            if(SP_IS_GAUSSIANBLUR(primitive)) {
                 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
                 float num = spblur->stdDeviation.getNumber();