[librsvg/rustify-rsvg-convert: 6/7] c-api: rework the LegacySizing helper trait




commit 60917addf8e33252138f396dc1f0fdd8cab0bcb9
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Jan 17 15:58:52 2021 +0100

    c-api: rework the LegacySizing helper trait
    
    This fixes a regression introduced when extending the trait for
    use in rsvg-convert and also cleans it up.

 src/bin/rsvg-convert.rs   |  2 +-
 src/c_api/handle.rs       |  5 +---
 src/c_api/pixbuf_utils.rs |  2 +-
 src/c_api/sizing.rs       | 70 +++++++++++++++++++++++++++--------------------
 4 files changed, 44 insertions(+), 35 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 929be141..37306f29 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -399,7 +399,7 @@ impl Converter {
 
     fn create_surface(&self, renderer: &CairoRenderer, input: &Input) -> Surface {
         let (width, height) = renderer
-            .legacy_layer_size_in_pixels(self.export_id.as_deref())
+            .legacy_layer_size(self.export_id.as_deref())
             .unwrap_or_else(|e| match e {
                 RenderingError::IdNotFound => exit!(
                     "File {} does not have an object with id \"{}\")",
diff --git a/src/c_api/handle.rs b/src/c_api/handle.rs
index c328d0af..8bcf0208 100644
--- a/src/c_api/handle.rs
+++ b/src/c_api/handle.rs
@@ -975,10 +975,7 @@ impl CHandle {
         let handle = self.get_handle_ref()?;
         let renderer = self.make_renderer(&handle);
 
-        Ok(renderer.legacy_layer_size_in_pixels(id).map(|(w, h)| {
-            let rect = cairo::Rectangle::from_size(w, h);
-            (rect, rect)
-        })?)
+        Ok(renderer.legacy_layer_geometry(id)?)
     }
 
     fn set_stylesheet(&self, css: &str) -> Result<(), LoadingError> {
diff --git a/src/c_api/pixbuf_utils.rs b/src/c_api/pixbuf_utils.rs
index a132a593..017d0dcb 100644
--- a/src/c_api/pixbuf_utils.rs
+++ b/src/c_api/pixbuf_utils.rs
@@ -166,7 +166,7 @@ unsafe fn pixbuf_from_file_with_size_mode(
     let dpi = Dpi::default();
     let renderer = CairoRenderer::new(&handle).with_dpi(dpi.x(), dpi.y());
 
-    let (document_width, document_height) = match renderer.legacy_document_size_in_pixels() {
+    let (document_width, document_height) = match renderer.legacy_document_size() {
         Ok(dim) => dim,
         Err(e) => {
             set_gerror(error, 0, &format!("{}", e));
diff --git a/src/c_api/sizing.rs b/src/c_api/sizing.rs
index 213f5d4f..238706e9 100644
--- a/src/c_api/sizing.rs
+++ b/src/c_api/sizing.rs
@@ -3,43 +3,55 @@ use float_cmp::approx_eq;
 
 use super::handle::CairoRectangleExt;
 
+/// Extension methods to compute the SVG's size suitable for the legacy C API.
+///
+/// The legacy C API can compute an SVG document's size from the
+/// `width`, `height`, and `viewBox` attributes of the toplevel `<svg>`
+/// element.  If these are not available, then the size must be computed
+/// by actually measuring the geometries of elements in the document.
+///
+/// See https://www.w3.org/TR/css-images-3/#sizing-terms for terminology and logic.
 pub trait LegacySize {
-    /// Returns the SVG's size suitable for the legacy C API.
-    ///
-    /// The legacy C API can compute an SVG document's size from the
-    /// `width`, `height`, and `viewBox` attributes of the toplevel `<svg>`
-    /// element.  If these are not available, then the size must be computed
-    /// by actually measuring the geometries of elements in the document.
-    ///
-    /// See https://www.w3.org/TR/css-images-3/#sizing-terms for terminology and logic.
-    fn legacy_document_size_in_pixels(&self) -> Result<(f64, f64), RenderingError>;
+    fn legacy_document_size(&self) -> Result<(f64, f64), RenderingError> {
+        let (ink_r, _) = self.legacy_layer_geometry(None)?;
+        Ok((ink_r.width, ink_r.height))
+    }
+
+    fn legacy_layer_size(&self, id: Option<&str>) -> Result<(f64, f64), RenderingError> {
+        let (ink_r, _) = self.legacy_layer_geometry(id)?;
+        Ok((ink_r.width, ink_r.height))
+    }
 
-    /// Returns the specified layer's size suitable for the legacy C API.
-    fn legacy_layer_size_in_pixels(&self, id: Option<&str>) -> Result<(f64, f64), RenderingError>;
+    fn legacy_layer_geometry(
+        &self,
+        id: Option<&str>,
+    ) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError>;
 }
 
 impl<'a> LegacySize for CairoRenderer<'a> {
-    fn legacy_document_size_in_pixels(&self) -> Result<(f64, f64), RenderingError> {
-        let size_from_intrinsic_dimensions = self.intrinsic_size_in_pixels().or_else(|| {
-            size_in_pixels_from_percentage_width_and_height(&self.intrinsic_dimensions())
-        });
+    fn legacy_layer_geometry(
+        &self,
+        id: Option<&str>,
+    ) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
+        match id {
+            Some(id) => Ok(self.geometry_for_layer(Some(id), &unit_rectangle())?),
 
-        if let Some(dim) = size_from_intrinsic_dimensions {
-            // We have a size directly computed from the <svg> attributes
-            Ok(dim)
-        } else {
-            let (ink_r, _) = self.geometry_for_layer(None, &unit_rectangle())?;
-            Ok((ink_r.width, ink_r.height))
-        }
-    }
+            None => {
+                let size_from_intrinsic_dimensions =
+                    self.intrinsic_size_in_pixels().or_else(|| {
+                        size_in_pixels_from_percentage_width_and_height(
+                            &self.intrinsic_dimensions(),
+                        )
+                    });
 
-    fn legacy_layer_size_in_pixels(&self, id: Option<&str>) -> Result<(f64, f64), RenderingError> {
-        match id {
-            Some(_) => {
-                let (ink_r, _) = self.geometry_for_layer(id, &unit_rectangle())?;
-                Ok((ink_r.width, ink_r.height))
+                if let Some((w, h)) = size_from_intrinsic_dimensions {
+                    // We have a size directly computed from the <svg> attributes
+                    let rect = cairo::Rectangle::from_size(w, h);
+                    Ok((rect, rect))
+                } else {
+                    self.geometry_for_layer(None, &unit_rectangle())
+                }
             }
-            None => self.legacy_document_size_in_pixels(),
         }
     }
 }


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