From 34e77833997cc50a606622fdb7947843a924067b Mon Sep 17 00:00:00 2001 From: Jeremy James Date: Mon, 6 Jan 2025 10:46:31 +0000 Subject: [PATCH] Add scale_by/scale_to functions to Size (#146) These match the implementations for for IntSize, making it simpler to implement floating point support in the resvg main binary. This is part of a fix for [resvg #810](linebender/resvg/issues/810). --- path/src/size.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/path/src/size.rs b/path/src/size.rs index b06d6ff..b314cae 100644 --- a/path/src/size.rs +++ b/path/src/size.rs @@ -163,6 +163,26 @@ impl Size { size_scale_f64(*self, to, true) } + /// Scales current size by the specified factor. + #[inline] + pub fn scale_by(&self, factor: f32) -> Option { + Self::from_wh(self.width() * factor, self.height() * factor) + } + + /// Scales current size to the specified width. + #[inline] + pub fn scale_to_width(&self, new_width: f32) -> Option { + let new_height = new_width * self.height() / self.width(); + Self::from_wh(new_width, new_height) + } + + /// Scales current size to the specified height. + #[inline] + pub fn scale_to_height(&self, new_height: f32) -> Option { + let new_width = new_height * self.width() / self.height(); + Self::from_wh(new_width, new_height) + } + /// Converts into [`IntSize`]. pub fn to_int_size(&self) -> IntSize { IntSize::from_wh(