[librsvg] Implement draw_in_viewport() indirectly so we can have some tests



commit a4d8b49128bea1a2f513e8661351a4482ee8f30a
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Sep 27 13:10:39 2017 -0500

    Implement draw_in_viewport() indirectly so we can have some tests

 rust/src/viewport.rs |   75 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 67 insertions(+), 8 deletions(-)
---
diff --git a/rust/src/viewport.rs b/rust/src/viewport.rs
index 12eb90b..0a56546 100644
--- a/rust/src/viewport.rs
+++ b/rust/src/viewport.rs
@@ -12,11 +12,70 @@ pub fn draw_in_viewport<F>(vx: f64, vy: f64, vw: f64, vh: f64,
                            do_clip: bool,
                            vbox: Option<ViewBox>,
                            preserve_aspect_ratio: AspectRatio,
-                           mut affine: cairo::Matrix,
+                           affine: cairo::Matrix,
                            draw_ctx: *const RsvgDrawingCtx,
                            draw_fn: F)
     where F: FnOnce()
 {
+    let mut ctx = RsvgDrawingCtxWrapper(draw_ctx);
+
+    in_viewport(&mut ctx,
+                vx, vy, vw, vh,
+                clip_before_layer_push,
+                do_clip,
+                vbox,
+                preserve_aspect_ratio,
+                affine,
+                draw_fn);
+}
+
+trait ViewportCtx {
+    fn push_view_box(&mut self, width: f64, height: f64);
+    fn pop_view_box(&mut self, );
+    fn push_discrete_layer(&mut self);
+    fn pop_discrete_layer(&mut self);
+    fn add_clipping_rect(&mut self, x: f64, y: f64, w: f64, h: f64);
+    fn set_affine(&mut self, affine: cairo::Matrix);
+}
+
+struct RsvgDrawingCtxWrapper(pub *const RsvgDrawingCtx);
+
+impl ViewportCtx for RsvgDrawingCtxWrapper {
+    fn push_view_box(&mut self, width: f64, height: f64) {
+        drawing_ctx::push_view_box(self.0, width, height);
+    }
+
+    fn pop_view_box(&mut self) {
+        drawing_ctx::pop_view_box(self.0);
+    }
+
+    fn push_discrete_layer(&mut self) {
+        drawing_ctx::push_discrete_layer(self.0);
+    }
+
+    fn pop_discrete_layer(&mut self) {
+        drawing_ctx::pop_discrete_layer(self.0);
+    }
+
+    fn add_clipping_rect(&mut self, x: f64, y: f64, w: f64, h: f64) {
+        drawing_ctx::add_clipping_rect(self.0, x, y, w, h);
+    }
+
+    fn set_affine(&mut self, affine: cairo::Matrix) {
+        drawing_ctx::set_current_state_affine(self.0, affine);
+    }
+}
+
+fn in_viewport<F>(ctx: &mut ViewportCtx,
+                  vx: f64, vy: f64, vw: f64, vh: f64,
+                  clip_before_layer_push: bool,
+                  do_clip: bool,
+                  vbox: Option<ViewBox>,
+                  preserve_aspect_ratio: AspectRatio,
+                  mut affine: cairo::Matrix,
+                  draw_fn: F)
+    where F: FnOnce()
+{
     // width or height set to 0 disables rendering of the element
     // https://www.w3.org/TR/SVG/struct.html#SVGElementWidthAttribute
     // https://www.w3.org/TR/SVG/struct.html#UseElementWidthAttribute
@@ -43,24 +102,24 @@ pub fn draw_in_viewport<F>(vx: f64, vy: f64, vw: f64, vh: f64,
         vbox_size = (vbox.0.width, vbox.0.height);
 
         if clip_before_layer_push && do_clip {
-            drawing_ctx::add_clipping_rect(draw_ctx, vbox.0.x, vbox.0.y, vbox.0.width, vbox.0.height);
+            ctx.add_clipping_rect(vbox.0.x, vbox.0.y, vbox.0.width, vbox.0.height);
         }
     } else {
         affine.translate(vx, vy);
         vbox_size = (vw, vh);
     }
 
-    drawing_ctx::push_view_box(draw_ctx, vbox_size.0, vbox_size.1);
-    drawing_ctx::push_discrete_layer(draw_ctx);
+    ctx.push_view_box(vbox_size.0, vbox_size.1);
+    ctx.push_discrete_layer();
 
     if !clip_before_layer_push && do_clip {
-        drawing_ctx::add_clipping_rect(draw_ctx, vx, vy, vw, vh);
+        ctx.add_clipping_rect(vx, vy, vw, vh);
     }
 
-    drawing_ctx::set_current_state_affine(draw_ctx, affine);
+    ctx.set_affine(affine);
 
     draw_fn();
 
-    drawing_ctx::pop_discrete_layer(draw_ctx);
-    drawing_ctx::pop_view_box(draw_ctx);
+    ctx.pop_discrete_layer();
+    ctx.pop_view_box();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]