[librsvg: 14/38] Make ViewBox opaque; impl Deref<Target = Rect> for it




commit cc7a54c3ccb0e06c2728cafef89a6b46f1ed6899
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Sep 22 10:21:43 2020 -0500

    Make ViewBox opaque; impl Deref<Target = Rect> for it
    
    That way we avoid `vbox.0.foo` everywhere.

 librsvg/c_api.rs                    |  8 ++++----
 librsvg_crate/src/lib.rs            |  2 +-
 rsvg_internals/src/aspect_ratio.rs  | 10 +++++-----
 rsvg_internals/src/drawing_ctx.rs   | 30 +++++++++++++++---------------
 rsvg_internals/src/filters/image.rs |  2 +-
 rsvg_internals/src/handle.rs        |  2 +-
 rsvg_internals/src/marker.rs        |  6 +++---
 rsvg_internals/src/structure.rs     |  2 +-
 rsvg_internals/src/viewbox.rs       | 20 +++++++++++++++++++-
 9 files changed, 50 insertions(+), 32 deletions(-)
---
diff --git a/librsvg/c_api.rs b/librsvg/c_api.rs
index 2a11a710..7837dccc 100644
--- a/librsvg/c_api.rs
+++ b/librsvg/c_api.rs
@@ -228,10 +228,10 @@ impl From<RsvgRectangle> for cairo::Rectangle {
 impl From<ViewBox> for RsvgRectangle {
     fn from(vb: ViewBox) -> RsvgRectangle {
         RsvgRectangle {
-            x: vb.0.x0,
-            y: vb.0.y0,
-            width: vb.0.width(),
-            height: vb.0.height(),
+            x: vb.x0,
+            y: vb.y0,
+            width: vb.width(),
+            height: vb.height(),
         }
     }
 }
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index 1313f383..38013224 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -463,7 +463,7 @@ impl<'a> CairoRenderer<'a> {
         IntrinsicDimensions {
             width: d.width.map(Into::into),
             height: d.height.map(Into::into),
-            vbox: d.vbox.map(|v| cairo::Rectangle::from(v.0)),
+            vbox: d.vbox.map(|v| cairo::Rectangle::from(*v)),
         }
     }
 
diff --git a/rsvg_internals/src/aspect_ratio.rs b/rsvg_internals/src/aspect_ratio.rs
index 67cb3e18..3dccfd98 100644
--- a/rsvg_internals/src/aspect_ratio.rs
+++ b/rsvg_internals/src/aspect_ratio.rs
@@ -128,7 +128,7 @@ impl AspectRatio {
             None => *viewport,
 
             Some(Align { x, y, fit }) => {
-                let (vb_width, vb_height) = vbox.0.size();
+                let (vb_width, vb_height) = vbox.size();
                 let (vp_width, vp_height) = viewport.size();
 
                 let w_factor = vp_width / vb_width;
@@ -180,15 +180,15 @@ impl AspectRatio {
         // the preserveAspectRatio attribute is only used if viewBox is specified
         // https://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute
         let transform = if let Some(vbox) = vbox {
-            if vbox.0.is_empty() {
+            if vbox.is_empty() {
                 // Width or height of 0 for the viewBox disables rendering of the element
                 // https://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute
                 return Ok(None);
             } else {
                 let r = self.compute(&vbox, viewport);
                 Transform::new_translate(r.x0, r.y0)
-                    .pre_scale(r.width() / vbox.0.width(), r.height() / vbox.0.height())
-                    .pre_translate(-vbox.0.x0, -vbox.0.y0)
+                    .pre_scale(r.width() / vbox.width(), r.height() / vbox.height())
+                    .pre_translate(-vbox.x0, -vbox.y0)
             }
         } else {
             Transform::new_translate(viewport.x0, viewport.y0)
@@ -343,7 +343,7 @@ mod tests {
 
     #[test]
     fn aligns() {
-        let viewbox = ViewBox(Rect::from_size(1.0, 10.0));
+        let viewbox = ViewBox::from(Rect::from_size(1.0, 10.0));
 
         let foo = AspectRatio::parse_str("xMinYMin meet").unwrap();
         let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index e1c87bc2..b596244e 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -216,7 +216,7 @@ impl DrawingCtx {
         // "... the initial viewport coordinate system (and therefore
         // the initial user coordinate system) must have its origin at
         // the top/left of the viewport"
-        let vbox = ViewBox(Rect::from_size(viewport.width(), viewport.height()));
+        let vbox = ViewBox::from(Rect::from_size(viewport.width(), viewport.height()));
         let rect = viewport;
 
         let mut view_box_stack = Vec::new();
@@ -309,7 +309,7 @@ impl DrawingCtx {
     pub fn get_view_params(&self) -> ViewParams {
         let view_box_stack = self.view_box_stack.borrow();
         let last = view_box_stack.len() - 1;
-        let top_rect = &view_box_stack[last].0;
+        let top_rect = &view_box_stack[last];
 
         ViewParams {
             dpi: self.dpi,
@@ -329,7 +329,7 @@ impl DrawingCtx {
     pub fn push_view_box(&self, width: f64, height: f64) -> ViewParams {
         self.view_box_stack
             .borrow_mut()
-            .push(ViewBox(Rect::from_size(width, height)));
+            .push(ViewBox::from(Rect::from_size(width, height)));
 
         ViewParams {
             dpi: self.dpi,
@@ -374,10 +374,10 @@ impl DrawingCtx {
                     Some(v) => {
                         rsvg_log!(
                             "ignoring viewBox ({}, {}, {}, {}) since it is not usable",
-                            v.0.x0,
-                            v.0.y0,
-                            v.0.width(),
-                            v.0.height()
+                            v.x0,
+                            v.y0,
+                            v.width(),
+                            v.height()
                         );
                     }
                 }
@@ -388,10 +388,10 @@ impl DrawingCtx {
 
                 if let Some(vbox) = vbox {
                     if let Some(ClipMode::ClipToVbox) = clip_mode {
-                        cr.rectangle(vbox.0.x0, vbox.0.y0, vbox.0.width(), vbox.0.height());
+                        cr.rectangle(vbox.x0, vbox.y0, vbox.width(), vbox.height());
                         cr.clip();
                     }
-                    self.push_view_box(vbox.0.width(), vbox.0.height())
+                    self.push_view_box(vbox.width(), vbox.height())
                 } else {
                     self.get_view_params()
                 }
@@ -1066,14 +1066,14 @@ impl DrawingCtx {
                 .get_preserve_aspect_ratio()
                 .compute(&vbox, &Rect::from_size(scaled_width, scaled_height));
 
-            let sw = r.width() / vbox.0.width();
-            let sh = r.height() / vbox.0.height();
-            let x = r.x0 - vbox.0.x0 * sw;
-            let y = r.y0 - vbox.0.y0 * sh;
+            let sw = r.width() / vbox.width();
+            let sh = r.height() / vbox.height();
+            let x = r.x0 - vbox.x0 * sw;
+            let y = r.y0 - vbox.y0 * sh;
 
             caffine = Transform::new_scale(sw, sh).pre_translate(x, y);
 
-            self.push_view_box(vbox.0.width(), vbox.0.height())
+            self.push_view_box(vbox.width(), vbox.height())
         } else if content_units == PatternContentUnits(CoordUnits::ObjectBoundingBox) {
             // If coords are in terms of the bounding box, use them
             let (bbw, bbh) = bbox.rect.unwrap().size();
@@ -1388,7 +1388,7 @@ impl DrawingCtx {
 
         let image_width = f64::from(image_width);
         let image_height = f64::from(image_height);
-        let vbox = ViewBox(Rect::from_size(image_width, image_height));
+        let vbox = ViewBox::from(Rect::from_size(image_width, image_height));
 
         let clip_mode = if !values.is_overflow() && aspect.is_slice() {
             Some(ClipMode::ClipToViewport)
diff --git a/rsvg_internals/src/filters/image.rs b/rsvg_internals/src/filters/image.rs
index 8d5665e3..1e881f28 100644
--- a/rsvg_internals/src/filters/image.rs
+++ b/rsvg_internals/src/filters/image.rs
@@ -89,7 +89,7 @@ impl FeImage {
             .map_err(|_| FilterError::InvalidInput)?;
 
         let rect = self.aspect.compute(
-            &ViewBox(Rect::from_size(
+            &ViewBox::from(Rect::from_size(
                 f64::from(image.width()),
                 f64::from(image.height()),
             )),
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 48540568..c87f8e64 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -408,7 +408,7 @@ fn get_svg_size(dimensions: &IntrinsicDimensions, cascaded: &CascadedValues, dpi
 
     match (w, h, dimensions.vbox) {
         (w, h, Some(vbox)) => {
-            let params = ViewParams::new(dpi, vbox.0.width(), vbox.0.height());
+            let params = ViewParams::new(dpi, vbox.width(), vbox.height());
 
             Some((w.normalize(values, &params), h.normalize(values, &params)))
         }
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index 7670096d..116e4e02 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -135,7 +135,7 @@ impl Marker {
         }
 
         let params = if let Some(vbox) = self.vbox {
-            if vbox.0.is_empty() {
+            if vbox.is_empty() {
                 return Ok(draw_ctx.empty_bbox());
             }
 
@@ -143,7 +143,7 @@ impl Marker {
                 .aspect
                 .compute(&vbox, &Rect::from_size(marker_width, marker_height));
 
-            let (vb_width, vb_height) = vbox.0.size();
+            let (vb_width, vb_height) = vbox.size();
             transform = transform.pre_scale(r.width() / vb_width, r.height() / vb_height);
 
             draw_ctx.push_view_box(vb_width, vb_height)
@@ -161,7 +161,7 @@ impl Marker {
         } else {
             Some(
                 self.vbox
-                    .map_or_else(|| Rect::from_size(marker_width, marker_height), |vb| vb.0),
+                    .map_or_else(|| Rect::from_size(marker_width, marker_height), |vb| *vb),
             )
         };
 
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index f4132bfd..d22e7dab 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -228,7 +228,7 @@ impl Draw for Svg {
                 // Use our viewBox if available, or try to derive one from
                 // the intrinsic dimensions.
                 self.vbox.or_else(|| {
-                    Some(ViewBox(Rect::from_size(
+                    Some(ViewBox::from(Rect::from_size(
                         svg_viewport.width(),
                         svg_viewport.height(),
                     )))
diff --git a/rsvg_internals/src/viewbox.rs b/rsvg_internals/src/viewbox.rs
index b3d40f5d..fbd2d28e 100644
--- a/rsvg_internals/src/viewbox.rs
+++ b/rsvg_internals/src/viewbox.rs
@@ -1,6 +1,7 @@
 //! Parser for the `viewBox` attribute.
 
 use cssparser::Parser;
+use std::ops::Deref;
 
 use crate::error::*;
 use crate::number_list::{NumberList, NumberListLength};
@@ -12,9 +13,26 @@ use crate::rect::Rect;
 /// A `ViewBox` is a new user-space coordinate system mapped onto the rectangle defined by
 /// the current viewport.  See https://www.w3.org/TR/SVG2/coords.html#ViewBoxAttribute
 ///
+/// `ViewBox` derefs to `Rect`, so you can use `Rect`'s methods and fields directly like
+/// `vbox.x0` or `vbox.width()`.
+///
 /// [`Rect`]: rect/type.Rect.html
 #[derive(Debug, Copy, Clone, PartialEq)]
-pub struct ViewBox(pub Rect);
+pub struct ViewBox(Rect);
+
+impl Deref for ViewBox {
+    type Target = Rect;
+
+    fn deref(&self) -> &Rect {
+        &self.0
+    }
+}
+
+impl From<Rect> for ViewBox {
+    fn from(r: Rect) -> ViewBox {
+        ViewBox(r)
+    }
+}
 
 impl Parse for ViewBox {
     // Parse a viewBox attribute


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