Code

NR::Maybe => boost::optional
[inkscape.git] / src / display / nr-arena-shape.cpp
index 5676b3ce6b11f8903fb1860aae62fd3f152db907..60795becf7f2b42350161e65b23683eaa20c5b32 100644 (file)
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-
-
+#include <2geom/svg-path.h>
+#include <2geom/svg-path-parser.h>
 #include <display/canvas-arena.h>
 #include <display/nr-arena.h>
 #include <display/nr-arena-shape.h>
-#include <libnr/n-art-bpath.h>
-#include <libnr/nr-path.h>
+#include "display/curve.h"
 #include <libnr/nr-pixops.h>
 #include <libnr/nr-matrix-ops.h>
 #include <libnr/nr-matrix-fns.h>
 #include <libnr/nr-blit.h>
+#include <libnr/nr-convert2geom.h>
+#include <2geom/pathvector.h>
+#include <2geom/curves.h>
 #include <livarot/Path.h>
 #include <livarot/float-line.h>
 #include <livarot/int-line.h>
 #include <style.h>
 #include "prefs-utils.h"
 #include "inkscape-cairo.h"
-
+#include "helper/geom.h"
+#include "helper/geom-curves.h"
 #include "sp-filter.h"
 #include "sp-filter-reference.h"
 #include "display/nr-filter.h"
-
+#include <typeinfo>
 #include <cairo.h>
 
+#include <glib.h>
+#include "svg/svg.h"
+#include <fenv.h>
+
 //int  showRuns=0;
 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
 
@@ -142,7 +149,7 @@ nr_arena_shape_finalize(NRObject *object)
     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
 
     if (shape->style) sp_style_unref(shape->style);
-    if (shape->curve) sp_curve_unref(shape->curve);
+    if (shape->curve) shape->curve->unref();
 
     ((NRObjectClass *) shape_parent_class)->finalize(object);
 }
@@ -223,7 +230,7 @@ nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArena
 
 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);
+void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox);
 
 /**
  * Updates the arena shape 'item' and all of its children, including the markers.
@@ -231,7 +238,7 @@ void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
 static guint
 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
 {
-    NRRect bbox;
+    Geom::Rect boundingbox;
 
     NRArenaShape *shape = NR_ARENA_SHAPE(item);
 
@@ -248,16 +255,11 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
         shape->ctm = gc->transform;
         if (state & NR_ARENA_ITEM_STATE_BBOX) {
             if (shape->curve) {
-                NRBPath bp;
-                /* fixme: */
-                bbox.x0 = bbox.y0 = NR_HUGE;
-                bbox.x1 = bbox.y1 = -NR_HUGE;
-                bp.path = SP_CURVE_BPATH(shape->curve);
-                nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
-                item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
-                item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
-                item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
-                item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
+                boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), to_2geom(gc->transform));
+                item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
+                item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
+                item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
+                item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
             }
             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
@@ -270,45 +272,35 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
 
     shape->delayed_shp=true;
     shape->ctm = gc->transform;
-    bbox.x0 = bbox.y0 = NR_HUGE;
-    bbox.x1 = bbox.y1 = -NR_HUGE;
+    boundingbox[0][0] = boundingbox[1][0] = NR_HUGE;
+    boundingbox[0][1] = boundingbox[1][1] = -NR_HUGE;
 
     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
 
     if (shape->curve) {
-        NRBPath bp;
-        /* fixme: */
-        bbox.x0 = bbox.y0 = NR_HUGE;
-        bbox.x1 = bbox.y1 = -NR_HUGE;
-        bp.path = SP_CURVE_BPATH(shape->curve);
-        nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
+        boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), to_2geom(gc->transform));
+
         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
             float width, scale;
             scale = NR::expansion(gc->transform);
             width = MAX(0.125, shape->_stroke.width * scale);
             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
-                bbox.x0-=width;
-                bbox.x1+=width;
-                bbox.y0-=width;
-                bbox.y1+=width;
+                boundingbox.expandBy(width);
             }
             // those pesky miters, now
             float miterMax=width*shape->_stroke.mitre_limit;
             if ( miterMax > 0.01 ) {
                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
-                bbox.x0-=miterMax;
-                bbox.x1+=miterMax;
-                bbox.y0-=miterMax;
-                bbox.y1+=miterMax;
+                boundingbox.expandBy(miterMax);
             }
         }
     } else {
     }
-    shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
-    shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
-    shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
-    shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
-    if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
+    shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
+    shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
+    shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
+    shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
+    if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
 
     /* Release state data */
     if (TRUE || !NR::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
@@ -333,7 +325,7 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
 
     if (!shape->curve || 
         !shape->style ||
-        sp_curve_is_empty(shape->curve) ||
+        shape->curve->is_empty() ||
         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
     {
@@ -348,21 +340,22 @@ nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, g
         nr_arena_shape_update_stroke(shape, gc, area);
         nr_arena_shape_update_fill(shape, gc, area);
 
-        bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
-        nr_arena_shape_add_bboxes(shape,bbox);
+        boundingbox[0][0] = boundingbox[0][1] = boundingbox[1][0] = boundingbox[1][1] = 0.0;
+        nr_arena_shape_add_bboxes(shape, boundingbox);
 
-        shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
-        shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
-        shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
-        shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
+        shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
+        shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
+        shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
+        shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
     }
 
-    if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
+    if (boundingbox.isEmpty())
+        return NR_ARENA_ITEM_STATE_ALL;
 
-    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);
+    item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
+    item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
+    item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.0F);
+    item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.0F);
     nr_arena_request_render_rect(item->arena, &item->bbox);
 
     item->render_opacity = TRUE;
@@ -426,12 +419,32 @@ static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
 }
 
+/* returns true if the pathvector has a region that needs fill.
+ * is for optimizing purposes, so should be fast and can falsely return true. 
+ * CANNOT falsely return false. */
+static bool has_inner_area(Geom::PathVector const & pv) {
+    // return false for the cases where there is surely no region to be filled
+    if (pv.empty())
+        return false;
+
+    if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
+        // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
+        if ( is_straight_curve(pv.front().front()) )
+        {
+            return false;
+        }
+    }
+
+    return true; //too costly to see if it has region to be filled, so return true.
+}
+
 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
 void
 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
 {
     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
-        ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
+//        ((shape->curve->get_length() > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {  // <-- this used to be the old code, i think it has to determine that the path has a sort of 'internal region' where fill would occur
+          has_inner_area(shape->curve->get_pathvector()) ) {
         if (TRUE || !shape->fill_shp) {
             NR::Matrix  cached_to_new = NR::identity();
             int isometry = 0;
@@ -452,8 +465,8 @@ nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool fo
                 Path*  thePath=new Path;
                 Shape* theShape=new Shape;
                 {
-                    NR::Matrix   tempMat(gc->transform);
-                    thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
+                    Geom::Matrix tempMat(to_2geom(gc->transform));
+                    thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
                 }
 
                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
@@ -567,8 +580,8 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
             Path*  thePath = new Path;
             Shape* theShape = new Shape;
             {
-                NR::Matrix   tempMat(gc->transform);
-                thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
+                Geom::Matrix   tempMat( to_2geom(gc->transform) );
+                thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
             }
 
             // add some padding to the rendering area, so clipped path does not go into a render area
@@ -671,67 +684,39 @@ nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
 
 
 void
-nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
+nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox)
 {
+    /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
+     * if so, then this can be written much more compact ! */
+
     if ( shape->stroke_shp ) {
-        shape->stroke_shp->CalcBBox();
-        shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
-        shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
-        shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
-        shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
-        if ( bbox.x0 >= bbox.x1 ) {
-            if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
-                bbox.x0=shape->stroke_shp->leftX;
-                bbox.x1=shape->stroke_shp->rightX;
-            }
-        } else {
-            if ( shape->stroke_shp->leftX < bbox.x0 )
-                bbox.x0=shape->stroke_shp->leftX;
-            if ( shape->stroke_shp->rightX > bbox.x1 )
-                bbox.x1=shape->stroke_shp->rightX;
-        }
-        if ( bbox.y0 >= bbox.y1 ) {
-            if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
-                bbox.y0=shape->stroke_shp->topY;
-                bbox.y1=shape->stroke_shp->bottomY;
-            }
-        } else {
-            if ( shape->stroke_shp->topY < bbox.y0 )
-                bbox.y0=shape->stroke_shp->topY;
-            if ( shape->stroke_shp->bottomY > bbox.y1 )
-                bbox.y1=shape->stroke_shp->bottomY;
-        }
+        Shape *larger = shape->stroke_shp;
+        larger->CalcBBox();
+        larger->leftX   = floor(larger->leftX);
+        larger->rightX  = ceil(larger->rightX);
+        larger->topY    = floor(larger->topY);
+        larger->bottomY = ceil(larger->bottomY);
+        Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
+                                Geom::Interval(larger->topY,  larger->bottomY) );
+        bbox.unionWith(stroke_bbox);
     }
+
     if ( shape->fill_shp ) {
-        shape->fill_shp->CalcBBox();
-        shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
-        shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
-        shape->fill_shp->topY=floor(shape->fill_shp->topY);
-        shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
-        if ( bbox.x0 >= bbox.x1 ) {
-            if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
-                bbox.x0=shape->fill_shp->leftX;
-                bbox.x1=shape->fill_shp->rightX;
-            }
-        } else {
-            if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
-            if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
-        }
-        if ( bbox.y0 >= bbox.y1 ) {
-            if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
-                bbox.y0=shape->fill_shp->topY;
-                bbox.y1=shape->fill_shp->bottomY;
-            }
-        } else {
-            if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
-            if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
-        }
+        Shape *larger = shape->fill_shp;
+        larger->CalcBBox();
+        larger->leftX   = floor(larger->leftX);
+        larger->rightX  = ceil(larger->rightX);
+        larger->topY    = floor(larger->topY);
+        larger->bottomY = ceil(larger->bottomY);
+        Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
+                              Geom::Interval(larger->topY,  larger->bottomY) );
+        bbox.unionWith(fill_bbox);
     }
 }
 
 // cairo outline rendering:
 static unsigned int
-cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
+cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, boost::optional<NR::Rect> area)
 {
     NRArenaShape *shape = NR_ARENA_SHAPE(item);
 
@@ -747,7 +732,7 @@ cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::R
     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);
+    feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area, true, 0);
 
     cairo_stroke(ct);
 
@@ -828,7 +813,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), area->upgrade(), true, style_width);
+    feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area->upgrade(), true, style_width);
 
     cairo_stroke(ct);
 
@@ -863,7 +848,7 @@ nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock
     } else {
 
     if ( shape->delayed_shp ) {
-        if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
+        if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
             NRGC   tempGC(NULL);
             tempGC.transform=shape->ctm;
             shape->delayed_shp = false;
@@ -1003,7 +988,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), (area)->upgrade(), false, 0);
+        feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), (area)->upgrade(), false, 0);
 
         cairo_fill(ct);
 
@@ -1028,7 +1013,7 @@ nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
     if (!shape->curve) return item->state;
 
     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
-        if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
+        if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
             NRGC   tempGC(NULL);
             tempGC.transform=shape->ctm;
             shape->delayed_shp = false;
@@ -1083,14 +1068,16 @@ nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /
 
     if (!shape->curve) return NULL;
     if (!shape->style) return NULL;
-    if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
+
+    bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
+
+    if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
+        // fully transparent, no pick unless outline mode
         return NULL;
 
     GTimeVal tstart, tfinish;
     g_get_current_time (&tstart);
 
-    bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
-
     double width;
     if (outline) {
         width = 0.5;
@@ -1101,19 +1088,17 @@ nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /
         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);
+        Geom::Rect viewbox = to_2geom(item->arena->canvasarena->item.canvas->getViewbox());
+        viewbox.expandBy (width);
+        pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), to_2geom(shape->ctm), to_2geom(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);
+        pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), to_2geom(shape->ctm), to_2geom(p), NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
     }
 
     g_get_current_time (&tfinish);
@@ -1188,13 +1173,13 @@ void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
 
     if (shape->curve) {
-        sp_curve_unref(shape->curve);
+        shape->curve->unref();
         shape->curve = NULL;
     }
 
     if (curve) {
         shape->curve = curve;
-        sp_curve_ref(curve);
+        curve->ref();
     }
 
     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);