[librsvg/rect: 5/10] More conversion to Rect



commit 8f212582d7aa255aa1657a819825c1f7c9a2a358
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Dec 8 14:07:58 2019 +0100

    More conversion to Rect

 rsvg_internals/src/aspect_ratio.rs  | 98 ++++++++++++++++++-------------------
 rsvg_internals/src/drawing_ctx.rs   |  2 +-
 rsvg_internals/src/filters/image.rs | 25 +++++-----
 rsvg_internals/src/marker.rs        |  8 +--
 rsvg_internals/src/pattern.rs       | 12 +++--
 5 files changed, 72 insertions(+), 73 deletions(-)
---
diff --git a/rsvg_internals/src/aspect_ratio.rs b/rsvg_internals/src/aspect_ratio.rs
index feb704f9..446ad114 100644
--- a/rsvg_internals/src/aspect_ratio.rs
+++ b/rsvg_internals/src/aspect_ratio.rs
@@ -125,9 +125,9 @@ impl AspectRatio {
         }
     }
 
-    pub fn compute(&self, vbox: &ViewBox, viewport: &Rect) -> (f64, f64, f64, f64) {
+    pub fn compute(&self, vbox: &ViewBox, viewport: Rect) -> Rect {
         match self.align {
-            None => (viewport.x0, viewport.y0, viewport.width(), viewport.height()),
+            None => viewport,
 
             Some(Align { x, y, fit }) => {
                 let w_factor = viewport.width() / vbox.width;
@@ -143,7 +143,7 @@ impl AspectRatio {
                 let xpos = x.compute(viewport.x0, viewport.width(), w);
                 let ypos = y.compute(viewport.y0, viewport.height(), h);
 
-                (xpos, ypos, w, h)
+                Rect::new(xpos, ypos, xpos + w, ypos + h)
             }
         }
     }
@@ -153,7 +153,7 @@ impl AspectRatio {
     pub fn viewport_to_viewbox_transform(
         &self,
         vbox: Option<ViewBox>,
-        viewport: &Rect,
+        viewport: Rect,
     ) -> Option<cairo::Matrix> {
         // width or height set to 0 disables rendering of the element
         // https://www.w3.org/TR/SVG/struct.html#SVGElementWidthAttribute
@@ -173,10 +173,10 @@ impl AspectRatio {
                 // https://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute
                 None
             } else {
-                let (x, y, w, h) = self.compute(&vbox, viewport);
+                let r = self.compute(&vbox, viewport);
                 let mut matrix = cairo::Matrix::identity();
-                matrix.translate(x, y);
-                matrix.scale(w / vbox.width, h / vbox.height);
+                matrix.translate(r.x0, r.y0);
+                matrix.scale(r.width() / vbox.width, r.height() / vbox.height);
                 matrix.translate(-vbox.x, -vbox.y);
                 Some(matrix)
             }
@@ -252,8 +252,6 @@ impl Parse for AspectRatio {
 mod tests {
     use super::*;
     use crate::float_eq_cairo::ApproxEqCairo;
-    use crate::rect::RectangleExt;
-    use cairo::Rectangle;
 
     #[test]
     fn parsing_invalid_strings_yields_error() {
@@ -338,11 +336,11 @@ mod tests {
         );
     }
 
-    fn assert_quadruples_equal(a: &(f64, f64, f64, f64), b: &(f64, f64, f64, f64)) {
-        assert_approx_eq_cairo!(a.0, b.0);
-        assert_approx_eq_cairo!(a.1, b.1);
-        assert_approx_eq_cairo!(a.2, b.2);
-        assert_approx_eq_cairo!(a.3, b.3);
+    fn assert_rect_equal(r1: &Rect, r2: &Rect) {
+        assert_approx_eq_cairo!(r1.x0, r2.x0);
+        assert_approx_eq_cairo!(r1.y0, r2.y0);
+        assert_approx_eq_cairo!(r1.x1, r2.x1);
+        assert_approx_eq_cairo!(r1.y1, r2.y1);
     }
 
     #[test]
@@ -350,127 +348,127 @@ mod tests {
         let foo = AspectRatio::parse_str("xMinYMin meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::from_size(0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMinYMin slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, 0.0, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::from_size(10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMinYMid meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::from_size(0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMinYMid slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, -49.5, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::new(0.0, -49.5, 10.0, 100.0 - 49.5));
 
         let foo = AspectRatio::parse_str("xMinYMax meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::from_size(0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMinYMax slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, -99.0, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::new(0.0, -99.0, 10.0, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMin meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(4.95, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::new(4.95, 0.0, 4.95 + 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMin slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, 0.0, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::from_size(10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMidYMid meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(4.95, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::new(4.95, 0.0, 4.95 + 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMid slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, -49.5, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::new(0.0, -49.5, 10.0, 100.0 - 49.5));
 
         let foo = AspectRatio::parse_str("xMidYMax meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(4.95, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::new(4.95, 0.0, 4.95 + 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMax slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, -99.0, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::new(0.0, -99.0, 10.0, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMin meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(9.9, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::new(9.9, 0.0, 10.0, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMin slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, 0.0, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::from_size(10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMaxYMid meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(9.9, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::new(9.9, 0.0, 10.0, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMid slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, -49.5, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::new(0.0, -49.5, 10.0, 100.0 - 49.5));
 
         let foo = AspectRatio::parse_str("xMaxYMax meet").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(9.9, 0.0, 0.1, 1.0));
+        assert_rect_equal(&foo, &Rect::new(9.9, 0.0, 10.0, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMax slice").unwrap();
         let foo = foo.compute(
             &ViewBox::new(0.0, 0.0, 1.0, 10.0),
-            &Rectangle::new(0.0, 0.0, 10.0, 1.0),
+            Rect::from_size(10.0, 1.0),
         );
-        assert_quadruples_equal(&foo, &(0.0, -99.0, 10.0, 100.0));
+        assert_rect_equal(&foo, &Rect::new(0.0, -99.0, 10.0, 1.0));
     }
 }
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 68009607..54fae97d 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -301,7 +301,7 @@ impl DrawingCtx {
         }
 
         preserve_aspect_ratio
-            .viewport_to_viewbox_transform(vbox, &viewport)
+            .viewport_to_viewbox_transform(vbox, viewport)
             .and_then(|matrix| {
                 self.cr.transform(matrix);
 
diff --git a/rsvg_internals/src/filters/image.rs b/rsvg_internals/src/filters/image.rs
index ed4d7ea0..a443778f 100644
--- a/rsvg_internals/src/filters/image.rs
+++ b/rsvg_internals/src/filters/image.rs
@@ -5,7 +5,6 @@ use crate::allowed_url::{Fragment, Href};
 use crate::aspect_ratio::AspectRatio;
 use crate::drawing_ctx::DrawingCtx;
 use crate::error::{NodeError, RenderingError};
-use crate::float_eq_cairo::ApproxEqCairo;
 use crate::node::{CascadedValues, NodeResult, NodeTrait, RsvgNode};
 use crate::parsers::ParseValue;
 use crate::property_bag::PropertyBag;
@@ -100,8 +99,8 @@ impl FeImage {
         &self,
         ctx: &FilterContext,
         draw_ctx: &DrawingCtx,
-        bounds: &IRect,
-        unclipped_bounds: &Rect,
+        bounds: IRect,
+        unclipped_bounds: Rect,
         href: &Href,
     ) -> Result<ImageSurface, FilterError> {
         let surface = if let Href::PlainUrl(ref url) = *href {
@@ -120,7 +119,7 @@ impl FeImage {
         )?;
 
         // TODO: this goes through a f64->i32->f64 conversion.
-        let (x, y, w, h) = self.aspect.compute(
+        let r = self.aspect.compute(
             &ViewBox::new(
                 0.0,
                 0.0,
@@ -130,24 +129,24 @@ impl FeImage {
             unclipped_bounds,
         );
 
-        if w.approx_eq_cairo(0.0) || h.approx_eq_cairo(0.0) {
+        if r.is_empty() {
             return Ok(output_surface);
         }
 
         let ptn = surface.to_cairo_pattern();
         let mut matrix = cairo::Matrix::new(
-            w / f64::from(surface.width()),
-            0f64,
-            0f64,
-            h / f64::from(surface.height()),
-            x,
-            y,
+            r.width() / f64::from(surface.width()),
+            0.0,
+            0.0,
+            r.height() / f64::from(surface.height()),
+            r.x0,
+            r.y0,
         );
         matrix.invert();
         ptn.set_matrix(matrix);
 
         let cr = cairo::Context::new(&output_surface);
-        let r = cairo::Rectangle::from(*bounds);
+        let r = cairo::Rectangle::from(bounds);
         cr.rectangle(r.x, r.y, r.width, r.height);
         cr.clip();
         cr.set_source(&ptn);
@@ -198,7 +197,7 @@ impl FilterEffect for FeImage {
             let output_surface = match href {
                 Href::PlainUrl(_) => {
                     let unclipped_bounds = bounds_builder.into_rect_without_clipping(draw_ctx);
-                    self.render_external_image(ctx, draw_ctx, &bounds, &unclipped_bounds, href)?
+                    self.render_external_image(ctx, draw_ctx, bounds, unclipped_bounds, href)?
                 }
                 Href::WithFragment(ref frag) => self.render_node(ctx, draw_ctx, bounds, frag)?,
             };
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index 26fa336e..508416bd 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -150,16 +150,16 @@ impl Marker {
             }
 
             let params = if let Some(vbox) = self.vbox {
-                let (_, _, w, h) = self.aspect.compute(
+                let r = self.aspect.compute(
                     &vbox,
-                    &Rect::from_size(marker_width, marker_height),
+                    Rect::from_size(marker_width, marker_height),
                 );
 
-                if vbox.width.approx_eq_cairo(0.0) || vbox.height.approx_eq_cairo(0.0) {
+                if r.is_empty() {
                     return Ok(dc.empty_bbox());
                 }
 
-                cr.scale(w / vbox.width, h / vbox.height);
+                cr.scale(r.width() / vbox.width, r.height() / vbox.height);
 
                 dc.push_view_box(vbox.width, vbox.height)
             } else {
diff --git a/rsvg_internals/src/pattern.rs b/rsvg_internals/src/pattern.rs
index dfe6e6b4..edc4f81d 100644
--- a/rsvg_internals/src/pattern.rs
+++ b/rsvg_internals/src/pattern.rs
@@ -320,15 +320,17 @@ impl AsPaintSource for ResolvedPattern {
         // Create the pattern contents coordinate system
         let _params = if let Some(vbox) = vbox {
             // If there is a vbox, use that
-            let (mut x, mut y, w, h) = preserve_aspect_ratio.compute(
+            let r = preserve_aspect_ratio.compute(
                 &vbox,
-                &Rect::from_size(scaled_width, scaled_height),
+                Rect::from_size(scaled_width, scaled_height),
             );
 
-            x -= vbox.x * w / vbox.width;
-            y -= vbox.y * h / vbox.height;
+            let sw = r.width() / vbox.width;
+            let sh = r.height() / vbox.height;
+            let x = r.x0 - vbox.x * sw;
+            let y = r.y0 - vbox.y * sh;
 
-            caffine = cairo::Matrix::new(w / vbox.width, 0.0, 0.0, h / vbox.height, x, y);
+            caffine = cairo::Matrix::new(sw, 0.0, 0.0, sh, x, y);
 
             draw_ctx.push_view_box(vbox.width, vbox.height)
         } else if content_units == PatternContentUnits(CoordUnits::ObjectBoundingBox) {


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