Code

Added missing (and very important) file.
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
index cccb10a8d8d1ae03ece8765ab10cbe660d9fcf9c..da88a5eae779307c1b4f98fd985085a7a8bd345e 100644 (file)
 #include <signal.h>
 #include <errno.h>
 
-#include <libnr/nr-matrix-ops.h>
-#include <libnr/nr-matrix-fns.h>
-#include <libnr/nr-matrix-translate-ops.h>
-#include <libnr/nr-scale-matrix-ops.h>
-
-#include "libnr/nr-matrix-rotate-ops.h"
-#include "libnr/nr-matrix-translate-ops.h"
-#include "libnr/nr-rotate-fns.h"
-#include "libnr/nr-scale-ops.h"
-#include "libnr/nr-scale-translate-ops.h"
-#include "libnr/nr-translate-matrix-ops.h"
-#include "libnr/nr-translate-scale-ops.h"
-#include "libnr/nr-convert2geom.h"
+#include "libnr/nr-rect.h"
 #include <2geom/transforms.h>
 #include <2geom/pathvector.h>
 
@@ -167,7 +155,27 @@ static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
 static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
 static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx);
 
-/* TODO FIXME: this does not render painting-marker-01-f.svg of SVG1.1 Test suite correctly. (orientation of one of the markers middle left ) */
+static void sp_shape_render_invoke_marker_rendering(SPMarker* marker, Geom::Matrix tr, SPStyle* style, CairoRenderContext *ctx)
+{
+    bool render = true;
+    if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
+        if (style->stroke_width.computed > 1e-9) {
+            tr = Geom::Scale(style->stroke_width.computed) * tr;
+        } else {
+            render = false; // stroke width zero and marker is thus scaled down to zero, skip
+        }
+    }
+
+    if (render) {
+        SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (marker));
+        tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
+        Geom::Matrix old_tr = marker_item->transform;
+        marker_item->transform = tr;
+        ctx->getRenderer()->renderItem (ctx, marker_item);
+        marker_item->transform = old_tr;
+    }
+}
+
 static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
 {
     NRRect pbox;
@@ -176,90 +184,79 @@ static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
 
     if (!shape->curve) return;
 
-    /* fixme: Think (Lauris) */
     sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
 
     SPStyle* style = SP_OBJECT_STYLE (item);
-    CairoRenderer *renderer = ctx->getRenderer();
 
     Geom::PathVector const & pathv = shape->curve->get_pathvector();
 
     ctx->renderPathVector(pathv, style, &pbox);
 
-    /* TODO: make code prettier: lots of variables can be taken out of the loop! */
     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
-        if ( shape->marker[SP_MARKER_LOC_START] ) {
-            SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
-            SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
-
-            Geom::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
-
-            if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
-                tr = Geom::Scale(style->stroke_width.computed) * tr;
+      // START position
+        for (int i = 0; i < 2; i++) {  // SP_MARKER_LOC and SP_MARKER_LOC_START
+            if ( shape->marker[i] ) {
+                SPMarker* marker = SP_MARKER (shape->marker[i]);
+                Geom::Matrix tr;
+                if (marker->orient_auto) {
+                    tr = sp_shape_marker_get_transform_at_start(path_it->front());
+                } else {
+                    tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
+                }
+                sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
             }
-
-            tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
-
-            Geom::Matrix old_tr = marker_item->transform;
-            marker_item->transform = tr;
-            renderer->renderItem (ctx, marker_item);
-            marker_item->transform = old_tr;
         }
 
-        if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
-            Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
-            Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
-            while (curve_it2 != path_it->end_default())
-            {
-                /* Put marker between curve_it1 and curve_it2.
-                 * Loop to end_default (so including closing segment), because when a path is closed,
-                 * there should be a midpoint marker between last segment and closing straight line segment */
-
-                SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
-                SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
-
-                Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
-
-                if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
-                    tr = Geom::Scale(style->stroke_width.computed) * tr;
+      // MID position
+        for (int i = 0; i < 3; i += 2) {  // SP_MARKER_LOC and SP_MARKER_LOC_MID
+            if ( shape->marker[i] && (path_it->size_default() > 1) ) {
+                Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
+                Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
+                while (curve_it2 != path_it->end_default())
+                {
+                    /* Put marker between curve_it1 and curve_it2.
+                     * Loop to end_default (so including closing segment), because when a path is closed,
+                     * there should be a midpoint marker between last segment and closing straight line segment */
+
+                    SPMarker* marker = SP_MARKER (shape->marker[i]);
+
+                    Geom::Matrix tr;
+                    if (marker->orient_auto) {
+                        tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
+                    } else {
+                        tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
+                    }
+
+                    sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
+
+                    ++curve_it1;
+                    ++curve_it2;
                 }
-
-                tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
-
-                Geom::Matrix old_tr = marker_item->transform;
-                marker_item->transform = tr;
-                renderer->renderItem (ctx, marker_item);
-                marker_item->transform = old_tr;
-
-                ++curve_it1;
-                ++curve_it2;
             }
         }
 
-        if ( shape->marker[SP_MARKER_LOC_END] ) {
-            SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
-            SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
+      // END position
+        for (int i = 0; i < 4; i += 3) {  // SP_MARKER_LOC and SP_MARKER_LOC_END
+            if ( shape->marker[i] ) {
+                SPMarker* marker = SP_MARKER (shape->marker[i]);
 
-            /* Get reference to last curve in the path.
-             * For moveto-only path, this returns the "closing line segment". */
-            unsigned int index = path_it->size_default();
-            if (index > 0) {
-                index--;
-            }
-            Geom::Curve const &lastcurve = (*path_it)[index];
+                /* Get reference to last curve in the path.
+                 * For moveto-only path, this returns the "closing line segment". */
+                unsigned int index = path_it->size_default();
+                if (index > 0) {
+                    index--;
+                }
+                Geom::Curve const &lastcurve = (*path_it)[index];
 
-            Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
+                Geom::Matrix tr;
+                if (marker->orient_auto) {
+                    tr = sp_shape_marker_get_transform_at_end(lastcurve);
+                } else {
+                    tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
+                }
 
-            if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
-                tr = Geom::Scale(style->stroke_width.computed) * tr;
+                sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
             }
-
-            tr = (Geom::Matrix)marker_item->transform * (Geom::Matrix)marker->c2p * tr;
-
-            Geom::Matrix old_tr = marker_item->transform;
-            marker_item->transform = tr;
-            renderer->renderItem (ctx, marker_item);
-            marker_item->transform = old_tr;
         }
     }
 }
@@ -427,19 +424,23 @@ static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
     TRACE(("sp_asbitmap_render: resolution: %f\n", res ));
 
     // Get the bounding box of the selection in document coordinates.
-    NRRect bbox(item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX ));
+    Geom::OptRect bbox = 
+           item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
+
+    if (!bbox) // no bbox, e.g. empty group
+        return;
 
     // The width and height of the bitmap in pixels
-    unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * (res / PX_PER_IN));
-    unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * (res / PX_PER_IN));
+    unsigned width = (unsigned) floor ((bbox->max()[Geom::X] - bbox->min()[Geom::X]) * (res / PX_PER_IN));
+    unsigned height =(unsigned) floor ((bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) * (res / PX_PER_IN));
     
     // Scale to exactly fit integer bitmap inside bounding box
-    double scale_x = (bbox.x1 - bbox.x0) / width;
-    double scale_y = (bbox.y1 - bbox.y0) / height;
+    double scale_x = (bbox->max()[Geom::X] - bbox->min()[Geom::X]) / width;
+    double scale_y = (bbox->max()[Geom::Y] - bbox->min()[Geom::Y]) / height;
 
     // Location of bounding box in document coordinates.
-    double shift_x = bbox.x0;
-    double shift_y = bbox.y1;
+    double shift_x = bbox->min()[Geom::X];
+    double shift_y = bbox->max()[Geom::Y];
 
     // For default 90 dpi, snap bitmap to pixel grid
     if (res == PX_PER_IN) { 
@@ -464,7 +465,8 @@ static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
     items = g_slist_append(items, item);
 
     GdkPixbuf *pb = sp_generate_internal_bitmap(document, NULL,
-        bbox.x0, bbox.y0, bbox.x1, bbox.y1, width, height, res, res, (guint32) 0xffffff00, items );
+        bbox->min()[Geom::X], bbox->min()[Geom::Y], bbox->max()[Geom::X], bbox->max()[Geom::Y], 
+        width, height, res, res, (guint32) 0xffffff00, items );
 
     if (pb) {
         TEST(gdk_pixbuf_save( pb, "bitmap.png", "png", NULL, NULL ));
@@ -569,42 +571,42 @@ CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool page
 {
     g_assert( ctx != NULL );
 
-    if (ctx->_vector_based_target) {
-        // width and height in pt
-        ctx->_width = sp_document_width(doc) * PT_PER_PX;
-        ctx->_height = sp_document_height(doc) * PT_PER_PX;
-    } else {
-        ctx->_width = sp_document_width(doc);
-        ctx->_height = sp_document_height(doc);
-    }
+    if (!base)
+        base = SP_ITEM(sp_document_root(doc));
 
     NRRect d;
-    if (pageBoundingBox || !base) {
+    if (pageBoundingBox) {
         d.x0 = d.y0 = 0;
-        d.x1 = ceil(ctx->_width);
-        d.y1 = ceil(ctx->_height);
+        d.x1 = ceil(sp_document_width(doc));
+        d.y1 = ceil(sp_document_height(doc));
     } else {
-        sp_item_invoke_bbox(base, &d, sp_item_i2r_affine(base), TRUE);
-        if (ctx->_vector_based_target) {
-            // convert from px to pt
-            d.x0 *= PT_PER_PX;
-            d.x1 *= PT_PER_PX;
-            d.y0 *= PT_PER_PX;
-            d.y1 *= PT_PER_PX;
-        }
+        sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
+    }
+
+    if (ctx->_vector_based_target) {
+        // convert from px to pt
+        d.x0 *= PT_PER_PX;
+        d.x1 *= PT_PER_PX;
+        d.y0 *= PT_PER_PX;
+        d.y1 *= PT_PER_PX;
     }
+
+    ctx->_width = d.x1-d.x0;
+    ctx->_height = d.y1-d.y0;
+
     TRACE(("setupDocument: %f x %f\n", ctx->_width, ctx->_height));
 
-    bool ret = ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
+    bool ret = ctx->setupSurface(ctx->_width, ctx->_height);
 
-    if (ret && !pageBoundingBox && base)
+    if (ret && !pageBoundingBox)
     {
+        double high = sp_document_height(doc);
+        if (ctx->_vector_based_target)
+            high *= PT_PER_PX;
+
         Geom::Matrix tp(Geom::Translate(-d.x0 * (ctx->_vector_based_target ? PX_PER_PT : 1.0),
-                                    (d.y1 - ctx->_height) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
+                                    (d.y1 - high) * (ctx->_vector_based_target ? PX_PER_PT : 1.0)));
         ctx->transform(&tp);
-
-        ctx->_width  = d.x1 - d.x0;
-        ctx->_height = d.y1 - d.y0;
     }
     
     return ret;