Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / display / inkscape-cairo.cpp
index 9210af1f41c35ed18e6280c6c79a9ee436fb8501..fdbd49727a3df395ad3bfd0e205c2fb7f125cbc0 100644 (file)
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <libnr/n-art-bpath.h>
-#include <libnr/nr-matrix-ops.h>
-#include <libnr/nr-matrix-fns.h>
+
 #include <libnr/nr-pixblock.h>
 #include <libnr/nr-convert2geom.h>
 #include "../style.h"
 #include "nr-arena.h"
 #include "sp-canvas.h"
 #include <2geom/pathvector.h>
+#include <2geom/bezier-curve.h>
+#include <2geom/hvlinesegment.h>
 #include <2geom/matrix.h>
 #include <2geom/point.h>
 #include <2geom/path.h>
 #include <2geom/transforms.h>
 #include <2geom/sbasis-to-bezier.h>
+#include "helper/geom-curves.h"
 
 /** Creates a cairo context to render to the given pixblock on the given area */
 cairo_t *
 nr_create_cairo_context_for_data (NRRectL *area, NRRectL *buf_area, unsigned char *px, unsigned int rowstride)
 {
-    if (!nr_rect_l_test_intersect (buf_area, area))
+    if (!nr_rect_l_test_intersect_ptr(buf_area, area))
         return NULL;
 
     NRRectL clip;
@@ -67,95 +68,6 @@ nr_create_cairo_context (NRRectL *area, NRPixBlock *pb)
     return nr_create_cairo_context_for_data (area, &(pb->area), NR_PIXBLOCK_PX (pb), pb->rs);
 }
 
-/** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
-void
-feed_curve_to_cairo (cairo_t *ct, NArtBpath const *bpath, NR::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
-{
-    NR::Point next(0,0), last(0,0);
-    if (!area || area->isEmpty())
-        return;
-    NR::Point shift = area->min();
-    NR::Rect view = *area;
-    view.growBy (stroke_width);
-    NR::Rect swept;
-    bool  closed = false;
-    NR::Point startpath(0,0);
-    for (int i = 0; bpath[i].code != NR_END; i++) {
-        switch (bpath[i].code) {
-            case NR_MOVETO_OPEN:
-            case NR_MOVETO:
-                if (closed) {
-                    // we cannot use close_path because some of the curves/lines may have been optimized out
-                    cairo_line_to(ct, startpath[NR::X], startpath[NR::Y]);
-                }
-                next[NR::X] = bpath[i].x3;
-                next[NR::Y] = bpath[i].y3;
-                next *= trans;
-                last = next;
-                next -= shift;
-                if (bpath[i].code == NR_MOVETO) {
-                    // remember the start point of the subpath, for closing it later
-                    closed = true;
-                    startpath = next;
-                } else {
-                    closed = false;
-                }
-                cairo_move_to(ct, next[NR::X], next[NR::Y]);
-                break;
-
-            case NR_LINETO:
-                next[NR::X] = bpath[i].x3;
-                next[NR::Y] = bpath[i].y3;
-                next *= trans;
-                if (optimize_stroke) {
-                    swept = NR::Rect(last, next);
-                    //std::cout << "swept: " << swept;
-                    //std::cout << "view: " << view;
-                    //std::cout << "intersects? " << (swept.intersects(view)? "YES" : "NO") << "\n";
-                }
-                last = next;
-                next -= shift;
-                if (!optimize_stroke || swept.intersects(view))
-                    cairo_line_to(ct, next[NR::X], next[NR::Y]);
-                else
-                    cairo_move_to(ct, next[NR::X], next[NR::Y]);
-                break;
-
-            case NR_CURVETO: {
-                NR::Point  tm1, tm2, tm3;
-                tm1[0]=bpath[i].x1;
-                tm1[1]=bpath[i].y1;
-                tm2[0]=bpath[i].x2;
-                tm2[1]=bpath[i].y2;
-                tm3[0]=bpath[i].x3;
-                tm3[1]=bpath[i].y3;
-                tm1 *= trans;
-                tm2 *= trans;
-                tm3 *= trans;
-                if (optimize_stroke) {
-                    swept = NR::Rect(last, last);
-                    swept.expandTo(tm1);
-                    swept.expandTo(tm2);
-                    swept.expandTo(tm3);
-                }
-                last = tm3;
-                tm1 -= shift;
-                tm2 -= shift;
-                tm3 -= shift;
-                if (!optimize_stroke || swept.intersects(view))
-                    cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
-                else
-                    cairo_move_to(ct, tm3[NR::X], tm3[NR::Y]);
-                break;
-            }
-
-            default:
-                break;
-        }
-    }
-}
-
-
 /*
  * Can be called recursively.
  * If optimize_stroke == false, the view Rect is not used.
@@ -163,9 +75,7 @@ feed_curve_to_cairo (cairo_t *ct, NArtBpath const *bpath, NR::Matrix trans, NR::
 static void
 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix const & trans, Geom::Rect view, bool optimize_stroke)
 {
-    if( dynamic_cast<Geom::LineSegment const*>(&c) ||
-        dynamic_cast<Geom::HLineSegment const*>(&c) ||
-        dynamic_cast<Geom::VLineSegment const*>(&c) )
+    if( is_straight_curve(c) )
     {
         Geom::Point end_tr = c.finalPoint() * trans;
         if (!optimize_stroke) {
@@ -218,7 +128,7 @@ feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix const & tran
             }
         }
     }
-//    else if(Geom::EllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::EllipticalArc *>(c)) {
+//    else if(Geom::SVGEllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::SVGEllipticalArc *>(c)) {
 //        //TODO: get at the innards and spit them out to cairo
 //    }
     else {
@@ -233,8 +143,8 @@ feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix const & tran
 }
 
 
-/** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
-void
+/** Feeds path-creating calls to the cairo context translating them from the Path */
+static void
 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path)
 {
     if (path.empty())
@@ -247,31 +157,24 @@ feed_path_to_cairo (cairo_t *ct, Geom::Path const &path)
     }
 
     if (path.closed()) {
-        cairo_line_to(ct, path.initialPoint()[0], path.initialPoint()[1]);
-//        cairo_close_path(ct);
-        /* I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
-           According to cairo documentation:
-           The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
-           in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
-           there is a line join connecting the final and initial segments of the sub-path. 
-        */
+        cairo_close_path(ct);
     }
 }
 
 /** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
-void
-feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
+static void
+feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Matrix trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
 {
-    if (!area || area->isEmpty())
+    if (!area)
         return;
     if (path.empty())
         return;
 
     // Transform all coordinates to coords within "area"
-    Geom::Point shift = to_2geom(area->min());
-    NR::Rect view = *area;
-    view.growBy (stroke_width);
-    view = view * from_2geom(Geom::Translate(-shift));
+    Geom::Point shift = area->min();
+    Geom::Rect view = *area;
+    view.expandBy (stroke_width);
+    view = view * (Geom::Matrix)Geom::Translate(-shift);
     //  Pass transformation to feed_curve, so that we don't need to create a whole new path.
     Geom::Matrix transshift(trans * Geom::Translate(-shift));
 
@@ -279,26 +182,38 @@ feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Matrix trans, NR:
     cairo_move_to(ct, initial[0], initial[1] );
 
     for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
-        feed_curve_to_cairo(ct, *cit, transshift, to_2geom(view), optimize_stroke);
+        feed_curve_to_cairo(ct, *cit, transshift, view, optimize_stroke);
     }
 
     if (path.closed()) {
-        cairo_line_to(ct, initial[0], initial[1]);
-//        cairo_close_path(ct);
-        /* I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
-           According to cairo documentation:
-           The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
-           in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
-           there is a line join connecting the final and initial segments of the sub-path. 
-        */
+        if (!optimize_stroke) {
+            cairo_close_path(ct);
+        } else {
+            cairo_line_to(ct, initial[0], initial[1]);
+            /* We cannot use cairo_close_path(ct) here because some parts of the path may have been
+               clipped and not drawn (maybe the before last segment was outside view area), which 
+               would result in closing the "subpath" after the last interruption, not the entire path.
+
+               However, according to cairo documentation:
+               The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
+               in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
+               there is a line join connecting the final and initial segments of the sub-path. 
+
+               The correct fix will be possible when cairo introduces methods for moving without
+               ending/starting subpaths, which we will use for skipping invisible segments; then we
+               will be able to use cairo_close_path here. This issue also affects ps/eps/pdf export,
+               see bug 168129
+            */
+        }
     }
 }
 
-/** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift */
+/** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
+ *  One must have done cairo_new_path(ct); before calling this function. */
 void
-feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
+feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Matrix trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
 {
-    if (!area || area->isEmpty())
+    if (!area)
         return;
     if (pathv.empty())
         return;
@@ -308,7 +223,8 @@ feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Matr
     }
 }
 
-/** Feeds path-creating calls to the cairo context translating them from the PathVector */
+/** Feeds path-creating calls to the cairo context translating them from the PathVector
+ *  One must have done cairo_new_path(ct); before calling this function. */
 void
 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
 {
@@ -329,4 +245,4 @@ feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :