[librsvg: 9/10] Move RsvgRectangle in the legacy C api



commit f87fccf6614a641059569173ee6f2f4750cb8dbc
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Nov 24 20:35:07 2019 +0100

    Move RsvgRectangle in the legacy C api

 librsvg/c_api.rs                  | 46 ++++++++++++++++++++++++++++++++++++++-
 rsvg_internals/src/drawing_ctx.rs | 42 -----------------------------------
 rsvg_internals/src/handle.rs      | 45 +++++++++++++++-----------------------
 rsvg_internals/src/lib.rs         |  4 ++--
 4 files changed, 65 insertions(+), 72 deletions(-)
---
diff --git a/librsvg/c_api.rs b/librsvg/c_api.rs
index 4bd2ed0d..9b05d4aa 100644
--- a/librsvg/c_api.rs
+++ b/librsvg/c_api.rs
@@ -36,7 +36,7 @@ use gobject_sys::{self, GEnumValue, GFlagsValue};
 use rsvg_internals::{
     rsvg_log, set_gerror, DefsLookupErrorKind, Dpi, Handle, IntrinsicDimensions,
     LoadOptions, LoadingError, RenderingError, RsvgDimensionData, RsvgLength, RsvgPositionData,
-    RsvgRectangle, RsvgSizeFunc, SharedImageSurface, SizeCallback, SurfaceType, RSVG_ERROR_FAILED,
+    RsvgSizeFunc, SharedImageSurface, SizeCallback, SurfaceType, ViewBox, RSVG_ERROR_FAILED,
 };
 
 use crate::pixbuf_utils::{empty_pixbuf, pixbuf_from_surface};
@@ -194,6 +194,48 @@ impl BaseUrl {
     }
 }
 
+#[derive(Default, Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+pub struct RsvgRectangle {
+    pub x: f64,
+    pub y: f64,
+    pub width: f64,
+    pub height: f64,
+}
+
+impl From<cairo::Rectangle> for RsvgRectangle {
+    fn from(r: cairo::Rectangle) -> RsvgRectangle {
+        RsvgRectangle {
+            x: r.x,
+            y: r.y,
+            width: r.width,
+            height: r.height,
+        }
+    }
+}
+
+impl From<RsvgRectangle> for cairo::Rectangle {
+    fn from(r: RsvgRectangle) -> cairo::Rectangle {
+        cairo::Rectangle {
+            x: r.x,
+            y: r.y,
+            width: r.width,
+            height: r.height,
+        }
+    }
+}
+
+impl From<ViewBox> for RsvgRectangle {
+    fn from(vb: ViewBox) -> RsvgRectangle {
+        RsvgRectangle {
+            x: vb.x,
+            y: vb.y,
+            width: vb.width,
+            height: vb.height,
+        }
+    }
+}
+
 /// Contains all the interior mutability for a RsvgHandle to be called
 /// from the C API.
 pub struct CHandle {
@@ -716,6 +758,7 @@ impl CHandle {
         let inner = self.inner.borrow();
         handle
             .get_geometry_for_layer(id, viewport, inner.dpi, inner.is_testing)
+            .map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))
             .map_err(warn_on_invalid_id)
     }
 
@@ -742,6 +785,7 @@ impl CHandle {
         let inner = self.inner.borrow();
         handle
             .get_geometry_for_element(id, inner.dpi, inner.is_testing)
+            .map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))
             .map_err(warn_on_invalid_id)
     }
 
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 95661d52..991d9363 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -1034,48 +1034,6 @@ impl From<ShapeRendering> for cairo::Antialias {
     }
 }
 
-#[derive(Default, Clone, Copy, Debug, PartialEq)]
-#[repr(C)]
-pub struct RsvgRectangle {
-    pub x: f64,
-    pub y: f64,
-    pub width: f64,
-    pub height: f64,
-}
-
-impl From<cairo::Rectangle> for RsvgRectangle {
-    fn from(r: cairo::Rectangle) -> RsvgRectangle {
-        RsvgRectangle {
-            x: r.x,
-            y: r.y,
-            width: r.width,
-            height: r.height,
-        }
-    }
-}
-
-impl From<ViewBox> for RsvgRectangle {
-    fn from(vb: ViewBox) -> RsvgRectangle {
-        RsvgRectangle {
-            x: vb.x,
-            y: vb.y,
-            width: vb.width,
-            height: vb.height,
-        }
-    }
-}
-
-impl From<RsvgRectangle> for cairo::Rectangle {
-    fn from(r: RsvgRectangle) -> cairo::Rectangle {
-        cairo::Rectangle {
-            x: r.x,
-            y: r.y,
-            width: r.width,
-            height: r.height,
-        }
-    }
-}
-
 pub struct AcquiredNode {
     stack: Option<Rc<RefCell<NodeStack>>>,
     node: RsvgNode,
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index a09e16f3..6f50844a 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -12,9 +12,10 @@ use crate::allowed_url::{AllowedUrl, Href};
 use crate::bbox::BoundingBox;
 use crate::document::Document;
 use crate::dpi::Dpi;
-use crate::drawing_ctx::{DrawingCtx, RsvgRectangle};
+use crate::drawing_ctx::DrawingCtx;
 use crate::error::{DefsLookupErrorKind, LoadingError, RenderingError};
 use crate::node::{CascadedValues, RsvgNode};
+use crate::rect::RectangleExt;
 use crate::structure::{IntrinsicDimensions, Svg};
 use url::Url;
 
@@ -278,7 +279,7 @@ impl Handle {
         viewport: &cairo::Rectangle,
         dpi: Dpi,
         is_testing: bool,
-    ) -> Result<(RsvgRectangle, RsvgRectangle), RenderingError> {
+    ) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
         let target = ImageSurface::create(cairo::Format::Rgb24, 1, 1)?;
         let cr = cairo::Context::new(&target);
         let mut draw_ctx = DrawingCtx::new(
@@ -294,8 +295,8 @@ impl Handle {
 
         let bbox = draw_ctx.draw_node_from_stack(&CascadedValues::new_from_node(&root), &root, false)?;
 
-        let ink_rect = bbox.ink_rect.map(RsvgRectangle::from).unwrap_or_default();
-        let logical_rect = bbox.rect.map(RsvgRectangle::from).unwrap_or_default();
+        let ink_rect = bbox.ink_rect.unwrap_or_else(|| cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0));
+        let logical_rect = bbox.rect.unwrap_or_else(|| cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0));
 
         Ok((ink_rect, logical_rect))
     }
@@ -306,7 +307,7 @@ impl Handle {
         id: Option<&str>,
         dpi: Dpi,
         is_testing: bool,
-    ) -> Result<(RsvgRectangle, RsvgRectangle), RenderingError> {
+    ) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
         let node = self.get_node_or_root(id)?;
 
         let root = self.document.root();
@@ -319,12 +320,10 @@ impl Handle {
             if let Some((root_width, root_height)) =
                 node.borrow().get_impl::<Svg>().get_size(&values, dpi)
             {
-                let ink_r = RsvgRectangle {
-                    x: 0.0,
-                    y: 0.0,
-                    width: f64::from(root_width),
-                    height: f64::from(root_height),
-                };
+                let ink_r = cairo::Rectangle::from_size(
+                    f64::from(root_width),
+                    f64::from(root_height),
+                );
 
                 let logical_r = ink_r;
 
@@ -349,7 +348,7 @@ impl Handle {
         viewport: &cairo::Rectangle,
         dpi: Dpi,
         is_testing: bool,
-    ) -> Result<(RsvgRectangle, RsvgRectangle), RenderingError> {
+    ) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
         let node = self.get_node_or_root(id)?;
         self.get_node_geometry_with_viewport(&node, viewport, dpi, is_testing)
     }
@@ -488,26 +487,18 @@ impl Handle {
         id: Option<&str>,
         dpi: Dpi,
         is_testing: bool,
-    ) -> Result<(RsvgRectangle, RsvgRectangle), RenderingError> {
+    ) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
         let node = self.get_node_or_root(id)?;
 
         let bbox = self.get_bbox_for_element(&node, dpi, is_testing)?;
 
-        let mut ink_rect = bbox.ink_rect.map(RsvgRectangle::from).unwrap_or_default();
-        let mut logical_rect = bbox.rect.map(RsvgRectangle::from).unwrap_or_default();
+        let ink_rect = bbox.ink_rect.unwrap_or_else(|| cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0));
+        let logical_rect = bbox.rect.unwrap_or_else(|| cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0));
 
         // Translate so ink_rect is always at offset (0, 0)
+        let ofs = (-ink_rect.x, -ink_rect.y);
 
-        let xofs = ink_rect.x;
-        let yofs = ink_rect.y;
-
-        ink_rect.x -= xofs;
-        ink_rect.y -= yofs;
-
-        logical_rect.x -= xofs;
-        logical_rect.y -= yofs;
-
-        Ok((ink_rect, logical_rect))
+        Ok((ink_rect.translate(ofs), logical_rect.translate(ofs)))
     }
 
     pub fn render_element(
@@ -529,9 +520,9 @@ impl Handle {
             return Ok(());
         }
 
-        let ink_r = bbox.ink_rect.map(RsvgRectangle::from).unwrap_or_default();
+        let ink_r = bbox.ink_rect.unwrap_or_else(|| cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0));
 
-        if ink_r.width == 0.0 || ink_r.height == 0.0 {
+        if ink_r.is_empty() {
             return Ok(());
         }
 
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 579cf935..87f930cc 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -44,8 +44,6 @@ pub use crate::color::{rsvg_css_parse_color, ColorKind, ColorSpec};
 
 pub use crate::dpi::{rsvg_rust_set_default_dpi_x_y, Dpi};
 
-pub use crate::drawing_ctx::RsvgRectangle;
-
 pub use crate::error::{
     rsvg_rust_error_quark, set_gerror, DefsLookupErrorKind, HrefError, LoadingError,
     RenderingError, RSVG_ERROR_FAILED,
@@ -68,6 +66,8 @@ pub use crate::surface_utils::{
     },
 };
 
+pub use crate::viewbox::ViewBox;
+
 #[macro_use]
 pub mod log;
 


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