diff options
author | Federico Mena Quintero <federico@gnome.org> | 2022-10-05 20:41:55 -0500 |
---|---|---|
committer | Federico Mena Quintero <federico@gnome.org> | 2022-10-06 19:14:26 -0500 |
commit | 51bd20e63a38e97eec36d239df7666fa95afc0ec (patch) | |
tree | 19aae8f2470ce78e0de53c8a12d33d5f269a857b | |
parent | 41390ea38cdf5bff3634c4bbf49f78a5cd3f79e5 (diff) | |
download | librsvg-51bd20e63a38e97eec36d239df7666fa95afc0ec.tar.gz |
Make rect_to_transform() an independent function, not a method of BoundingBox
Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/757>
-rw-r--r-- | src/bbox.rs | 31 | ||||
-rw-r--r-- | src/drawing_ctx.rs | 4 | ||||
-rw-r--r-- | src/gradient.rs | 3 | ||||
-rw-r--r-- | src/rect.rs | 35 |
4 files changed, 39 insertions, 34 deletions
diff --git a/src/bbox.rs b/src/bbox.rs index ab33f9a9..c851c473 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 8c426311..9dd6b66c 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 d44e4f2c..f0becdda 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 8ec4f1ed..b0bbb201 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 { |