From c614d33e7df649df22406309b74d24352f2a7889 Mon Sep 17 00:00:00 2001 From: asun0204 Date: Thu, 26 Dec 2024 11:48:40 +0800 Subject: [PATCH] Add `from_f32_px_truncated` and `from_f64_px_truncated` methods Signed-off-by: asun0204 --- Cargo.toml | 2 +- src/app_unit.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f39090c..53d1106 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "app_units" -version = "0.7.6" +version = "0.7.7" authors = ["The Servo Project Developers"] description = "Servo app units type (Au)" documentation = "https://docs.rs/app_units/" diff --git a/src/app_unit.rs b/src/app_unit.rs index 07473c9..afaa036 100644 --- a/src/app_unit.rs +++ b/src/app_unit.rs @@ -273,6 +273,18 @@ impl Au { Au::from_f64_au(float) } + #[inline] + pub fn from_f32_px_by_trunc(px: f32) -> Au { + let float = (px * AU_PER_PX as f32).trunc(); + Au::from_f64_au(float as f64) + } + + #[inline] + pub fn from_f64_px_by_trunc(px: f64) -> Au { + let float = (px * AU_PER_PX as f64).trunc(); + Au::from_f64_au(float) + } + #[inline] pub fn abs(self) -> Self { Au(self.0.abs()) @@ -407,6 +419,22 @@ fn convert() { assert_eq!(Au::from_f64_px(6.), Au(360)); assert_eq!(Au::from_f64_px(6.12), Au(367)); assert_eq!(Au::from_f64_px(6.13), Au(368)); + + assert_eq!(Au::from_f32_px_truncate(5.0), Au(300)); + assert_eq!(Au::from_f32_px_truncate(5.2), Au(312)); + assert_eq!(Au::from_f32_px_truncate(5.5), Au(330)); + assert_eq!(Au::from_f32_px_truncate(5.8), Au(348)); + assert_eq!(Au::from_f32_px_truncate(6.), Au(360)); + assert_eq!(Au::from_f32_px_truncate(6.12), Au(367)); + assert_eq!(Au::from_f32_px_truncate(6.13), Au(367)); + + assert_eq!(Au::from_f64_px_truncate(5.), Au(300)); + assert_eq!(Au::from_f64_px_truncate(5.2), Au(312)); + assert_eq!(Au::from_f64_px_truncate(5.5), Au(330)); + assert_eq!(Au::from_f64_px_truncate(5.8), Au(348)); + assert_eq!(Au::from_f64_px_truncate(6.), Au(360)); + assert_eq!(Au::from_f64_px_truncate(6.12), Au(367)); + assert_eq!(Au::from_f64_px_truncate(6.13), Au(367)); } #[test]