Code

fix 1243587 and misc fixes
[inkscape.git] / src / display / nr-arena-shape.cpp
index b8bfee05c3447ceb26d35919b2554192fdee8c5f..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"
@@ -124,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
@@ -379,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;
     }
 
@@ -520,9 +525,8 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
 
     if (outline) {
-        // cairo does not need the livarot path for rendering... but unfortunately it's still used for picking
-        // FIXME: switch picking to using cairo_in_stroke? 
-        //return; 
+        // cairo does not need the livarot path for rendering
+        return; 
     }
 
     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
@@ -728,7 +732,7 @@ 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::Point shift)
+cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
 {
     NRArenaShape *shape = NR_ARENA_SHAPE(item);
 
@@ -744,7 +748,7 @@ cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Point shift
     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), shift);
+    feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
 
     cairo_stroke(ct);
 
@@ -827,7 +831,7 @@ cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb
     cairo_set_tolerance(ct, 0.1);
     cairo_new_path(ct);
 
-    feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
+    feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
 
     cairo_stroke(ct);
 
@@ -853,10 +857,13 @@ nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock
 
     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
 
-    if (outline) { 
+    if (outline) { // cairo outline rendering
+
         pb->empty = FALSE;
-        return cairo_arena_shape_render_outline (ct, item, NR::Point(pb->area.x0, pb->area.y0));
-    }
+        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) ) {
@@ -972,10 +979,11 @@ nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock
         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(ct, 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;
     }
 
@@ -1000,7 +1008,7 @@ cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
 
         cairo_new_path(ct);
 
-        feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
+        feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
 
         cairo_fill(ct);
 
@@ -1071,77 +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);
 
-    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 || outline)) {
-            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;
+    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;
+    }
+
+    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 || outline)) {
-                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 || outline) {
-            /* 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;
 }