Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
index 86e04fb548e2904feb7676102bde39f7427b208f..67f9354d8cac97085e57fc87b6f18d90cbc29df8 100644 (file)
@@ -1,11 +1,11 @@
-#define __SP_CAIRO_RENDERER_C__
-
 /** \file
  * Rendering with Cairo.
  */
 /*
  * Author:
  *   Miklos Erdelyi <erdelyim@gmail.com>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2006 Miklos Erdelyi
  *
@@ -29,6 +29,7 @@
 #include <errno.h>
 
 #include "libnr/nr-rect.h"
+#include "libnrtype/Layout-TNG.h"
 #include <2geom/transforms.h>
 #include <2geom/pathvector.h>
 
@@ -102,6 +103,7 @@ namespace Extension {
 namespace Internal {
 
 CairoRenderer::CairoRenderer(void)
+  : _omitText(false)
 {}
 
 CairoRenderer::~CairoRenderer(void)
@@ -155,7 +157,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;
@@ -164,87 +186,93 @@ 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);
+    item->invoke_bbox( &pbox, Geom::identity(), TRUE);
 
     SPStyle* style = SP_OBJECT_STYLE (item);
-    CairoRenderer *renderer = ctx->getRenderer();
 
     Geom::PathVector const & pathv = shape->curve->get_pathvector();
+    if (pathv.empty()) return;
 
     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]));
-
+    // START marker
+    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());
+                tr = sp_shape_marker_get_transform_at_start(pathv.begin()->front());
             } else {
-                tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
+                tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
             }
-
-            if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
-                tr = Geom::Scale(style->stroke_width.computed) * tr;
-            }
-
-            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;
+            sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
         }
-
-        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())
+    }
+    // MID marker
+    for (int i = 0; i < 3; i += 2) {  // SP_MARKER_LOC and SP_MARKER_LOC_MID
+        if ( !shape->marker[i] ) continue;
+        SPMarker* marker = SP_MARKER (shape->marker[i]);
+        for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
+            // START position
+            if ( path_it != pathv.begin() 
+                 && ! ((path_it == (pathv.end()-1)) && (path_it->size_default() == 0)) ) // if this is the last path and it is a moveto-only, there is no mid marker there
             {
-                /* 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;
                 if (marker->orient_auto) {
-                    tr = sp_shape_marker_get_transform(*curve_it1, *curve_it2);
+                    tr = sp_shape_marker_get_transform_at_start(path_it->front());
                 } else {
-                    tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
+                    tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(path_it->front().pointAt(0));
                 }
-
-                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);
+            }
+            // MID position
+            if (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 */
+                    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;
+            }
+            // END position
+            if ( path_it != (pathv.end()-1) && !path_it->empty()) {
+                Geom::Curve const &lastcurve = path_it->back_default();
+                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));
+                }
+                sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
             }
         }
-
-        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 marker
+    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();
+            Geom::Path const &path_last = pathv.back();
+            unsigned int index = path_last.size_default();
             if (index > 0) {
                 index--;
             }
-            Geom::Curve const &lastcurve = (*path_it)[index];
+            Geom::Curve const &lastcurve = path_last[index];
 
             Geom::Matrix tr;
             if (marker->orient_auto) {
@@ -253,16 +281,7 @@ static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
                 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;
-            }
-
-            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;
+            sp_shape_render_invoke_marker_rendering(marker, tr, style, ctx);
         }
     }
 }
@@ -430,19 +449,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(item->i2d_affine(), 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) { 
@@ -458,7 +481,7 @@ static void sp_asbitmap_render(SPItem *item, CairoRenderContext *ctx)
                                  (Geom::Matrix)(Geom::Translate (shift_x, shift_y));
 
     // ctx matrix already includes item transformation. We must substract.
-    Geom::Matrix t_item =  sp_item_i2d_affine (item);
+    Geom::Matrix t_item =  item->i2d_affine ();
     Geom::Matrix t = t_on_document * t_item.inverse();
 
     // Do the export
@@ -467,7 +490,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 ));
@@ -546,6 +570,11 @@ CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
 void
 CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
 {
+    if ( _omitText && (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) ) {
+        // skip text if _omitText is true
+        return;
+    }
+
     ctx->pushState();
     setStateForItem(ctx, item);
 
@@ -570,18 +599,21 @@ CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
 bool
 CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool pageBoundingBox, SPItem *base)
 {
+// PLEASE note when making changes to the boundingbox and transform calculation, corresponding changes should be made to PDFLaTeXRenderer::setupDocument !!!
+
     g_assert( ctx != NULL );
 
-    if (!base)
-        base = SP_ITEM(sp_document_root(doc));
+    if (!base) {
+        base = SP_ITEM(doc->getRoot());
+    }
 
     NRRect d;
     if (pageBoundingBox) {
         d.x0 = d.y0 = 0;
-        d.x1 = ceil(sp_document_width(doc));
-        d.y1 = ceil(sp_document_height(doc));
+        d.x1 = doc->getWidth();
+        d.y1 = doc->getHeight();
     } else {
-        sp_item_invoke_bbox(base, &d, sp_item_i2r_affine(base), TRUE, SPItem::RENDERING_BBOX);
+        base->invoke_bbox( &d, base->i2d_affine(), TRUE, SPItem::RENDERING_BBOX);
     }
 
     if (ctx->_vector_based_target) {
@@ -601,7 +633,7 @@ CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool page
 
     if (ret && !pageBoundingBox)
     {
-        double high = sp_document_height(doc);
+        double high = doc->getHeight();
         if (ctx->_vector_based_target)
             high *= PT_PER_PX;
 
@@ -641,7 +673,7 @@ CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
 
     TRACE(("BEGIN clip\n"));
     SPObject *co = SP_OBJECT(cp);
-    for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
+    for ( SPObject *child = co->firstChild() ; child; child = child->getNext() ) {
         if (SP_IS_ITEM(child)) {
             SPItem *item = SP_ITEM(child);
 
@@ -699,7 +731,7 @@ CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
 
     TRACE(("BEGIN mask\n"));
     SPObject *co = SP_OBJECT(mask);
-    for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
+    for ( SPObject *child = co->firstChild() ; child; child = child->getNext() ) {
         if (SP_IS_ITEM(child)) {
             SPItem *item = SP_ITEM(child);
             renderItem(ctx, item);
@@ -782,4 +814,4 @@ calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip
   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 :