[librsvg: 3/9] Make rect_to_transform() an independent function, not a method of BoundingBox




commit 51bd20e63a38e97eec36d239df7666fa95afc0ec
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Oct 5 20:41:55 2022 -0500

    Make rect_to_transform() an independent function, not a method of BoundingBox
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/757>

 src/bbox.rs        | 31 -------------------------------
 src/drawing_ctx.rs |  4 ++--
 src/gradient.rs    |  3 ++-
 src/rect.rs        | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 39 insertions(+), 34 deletions(-)
---
diff --git a/src/bbox.rs b/src/bbox.rs
index ab33f9a91..c851c4733 100644
--- a/src/bbox.rs
+++ b/src/bbox.rs
@@ -1,6 +1,5 @@
 //! Bounding boxes that know their coordinate space.
 
-use crate::coord_units::CoordUnits;
 use crate::rect::Rect;
 use crate::transform::Transform;
 
@@ -62,36 +61,6 @@ impl BoundingBox {
     pub fn clip(&mut self, src: &BoundingBox) {
         self.combine(src, true);
     }
-
-    /// Creates a transform to map to the `self.rect`.
-    ///
-    /// This depends on a `CoordUnits` parameter.  When this is
-    /// `CoordUnits::ObjectBoundingBox`, the bounding box must not be
-    /// empty, since the calling code would then not have a usable
-    /// size to work with.  In that case, if the bbox is empty, this
-    /// function returns `Err(())`.
-    ///
-    /// Usually calling code can simply ignore the action it was about
-    /// to take if this function returns an error.
-    pub fn rect_to_transform(&self, units: CoordUnits) -> Result<Transform, ()> {
-        match units {
-            CoordUnits::UserSpaceOnUse => Ok(Transform::identity()),
-            CoordUnits::ObjectBoundingBox => {
-                if self.rect.as_ref().map_or(true, |r| r.is_empty()) {
-                    Err(())
-                } else {
-                    let r = self.rect.as_ref().unwrap();
-                    let t = Transform::new_unchecked(r.width(), 0.0, 0.0, r.height(), r.x0, r.y0);
-
-                    if t.is_invertible() {
-                        Ok(t)
-                    } else {
-                        Err(())
-                    }
-                }
-            }
-        }
-    }
 }
 
 fn combine_rects(
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 8c4263119..9dd6b66cc 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -37,7 +37,7 @@ use crate::properties::{
     ClipRule, ComputedValues, FillRule, Filter, Isolation, MaskType, MixBlendMode, Opacity,
     Overflow, PaintTarget, ShapeRendering, StrokeLinecap, StrokeLinejoin, TextRendering,
 };
-use crate::rect::{IRect, Rect};
+use crate::rect::{rect_to_transform, IRect, Rect};
 use crate::session::Session;
 use crate::surface_utils::{
     shared_surface::ExclusiveImageSurface, shared_surface::SharedImageSurface,
@@ -538,7 +538,7 @@ impl DrawingCtx {
         let node = clip_node.as_ref().unwrap();
         let units = borrow_element_as!(node, ClipPath).get_units();
 
-        if let Ok(transform) = bbox.rect_to_transform(units) {
+        if let Ok(transform) = rect_to_transform(&bbox.rect, units) {
             let cascaded = CascadedValues::new_from_node(node);
             let values = cascaded.get();
 
diff --git a/src/gradient.rs b/src/gradient.rs
index d44e4f2cb..f0becddaa 100644
--- a/src/gradient.rs
+++ b/src/gradient.rs
@@ -17,6 +17,7 @@ use crate::node::{CascadedValues, Node, NodeBorrow};
 use crate::paint_server::resolve_color;
 use crate::parsers::{Parse, ParseValue};
 use crate::properties::ComputedValues;
+use crate::rect::rect_to_transform;
 use crate::session::Session;
 use crate::transform::{Transform, TransformAttribute};
 use crate::unit_interval::UnitInterval;
@@ -683,7 +684,7 @@ impl ResolvedGradient {
         values: &ComputedValues,
     ) -> Option<UserSpaceGradient> {
         let units = self.units.0;
-        let transform = bbox.rect_to_transform(units).ok()?;
+        let transform = rect_to_transform(&bbox.rect, units).ok()?;
         let view_params = current_params.with_units(units);
         let params = NormalizeParams::new(values, &view_params);
 
diff --git a/src/rect.rs b/src/rect.rs
index 8ec4f1ed2..b0bbb201f 100644
--- a/src/rect.rs
+++ b/src/rect.rs
@@ -1,5 +1,8 @@
 //! Types for rectangles.
 
+use crate::coord_units::CoordUnits;
+use crate::transform::Transform;
+
 #[allow(clippy::module_inception)]
 mod rect {
     use crate::float_eq_cairo::ApproxEqCairo;
@@ -205,6 +208,38 @@ impl From<Rect> for cairo::Rectangle {
     }
 }
 
+/// Creates a transform to map to a rectangle.
+///
+/// The rectangle is an `Option<Rect>` to indicate the possibility that there is no
+/// bounding box from where the rectangle could be obtained.
+///
+/// This depends on a `CoordUnits` parameter.  When this is
+/// `CoordUnits::ObjectBoundingBox`, the bounding box must not be empty, since the calling
+/// code would then not have a usable size to work with.  In that case, if the bbox is
+/// empty, this function returns `Err(())`.
+///
+/// Usually calling code can simply ignore the action it was about to take if this
+/// function returns an error.
+pub fn rect_to_transform(rect: &Option<Rect>, units: CoordUnits) -> Result<Transform, ()> {
+    match units {
+        CoordUnits::UserSpaceOnUse => Ok(Transform::identity()),
+        CoordUnits::ObjectBoundingBox => {
+            if rect.as_ref().map_or(true, |r| r.is_empty()) {
+                Err(())
+            } else {
+                let r = rect.as_ref().unwrap();
+                let t = Transform::new_unchecked(r.width(), 0.0, 0.0, r.height(), r.x0, r.y0);
+
+                if t.is_invertible() {
+                    Ok(t)
+                } else {
+                    Err(())
+                }
+            }
+        }
+    }
+}
+
 pub type IRect = rect::Rect<i32>;
 
 impl From<IRect> for Rect {


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