Code

Patch for 456148 by Johan
[inkscape.git] / src / extension / internal / cairo-renderer.cpp
index 5d6315894da32f5f0f8bbbb87b1b7db098ffcb9d..8cc38613538c1371b212c423750722649c230340 100644 (file)
@@ -155,6 +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);
 
+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;
@@ -166,95 +187,89 @@ static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
     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]));
-
+    // 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));
-            }
-
-            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) {
-                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;
+                tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(pathv.begin()->front().pointAt(0));
             }
+            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));
                 }
-
-                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;
+                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 {
-                        render = false; // stroke width zero and marker is thus scaled down to zero, skip
+                        tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(curve_it1->pointAt(1));
                     }
-                }
 
-                if (render) {
-                    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);
 
-                ++curve_it1;
-                ++curve_it2;
+                    ++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) {
@@ -263,22 +278,7 @@ static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
                 tr = Geom::Rotate::from_degrees(marker->orient) * Geom::Translate(lastcurve.pointAt(1));
             }
 
-            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) {
-                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);
         }
     }
 }
@@ -446,7 +446,7 @@ 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.
-    boost::optional<Geom::Rect> bbox = 
+    Geom::OptRect bbox = 
            item->getBounds(sp_item_i2d_affine(item), SPItem::RENDERING_BBOX);
 
     if (!bbox) // no bbox, e.g. empty group
@@ -602,7 +602,7 @@ CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc, bool page
         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, SPItem::RENDERING_BBOX);
+        sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
     }
 
     if (ctx->_vector_based_target) {