diff --git a/src/eval/value.rs b/src/eval/value.rs index d1f0be76..44e62c50 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -531,7 +531,7 @@ mod tests { test(Length::pt(5.5), "5.5pt"); test(Angle::deg(90.0), "90deg"); test(Relative::one() / 2.0, "50%"); - test(Relative::new(0.3) + Length::cm(2.0), "30% + 2cm"); + test(Relative::new(0.3) + Length::cm(2.0), "30% + 56.69pt"); test(Fractional::one() * 7.55, "7.55fr"); test(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "#010101"); diff --git a/src/geom/angle.rs b/src/geom/angle.rs index df2aca17..acf3803d 100644 --- a/src/geom/angle.rs +++ b/src/geom/angle.rs @@ -58,15 +58,7 @@ impl Angle { impl Debug for Angle { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - // Format with the unit that yields the shortest output, preferring - // degrees when tied. - let unit = [AngularUnit::Deg, AngularUnit::Rad] - .iter() - .copied() - .min_by_key(|&unit| self.to_unit(unit).to_string().len()) - .unwrap(); - - write!(f, "{}{:?}", self.to_unit(unit), unit) + write!(f, "{}deg", round_2(self.to_deg())) } } diff --git a/src/geom/fr.rs b/src/geom/fr.rs index e7cd276d..6aa0f5c7 100644 --- a/src/geom/fr.rs +++ b/src/geom/fr.rs @@ -48,7 +48,7 @@ impl Fractional { impl Debug for Fractional { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}fr", self.get()) + write!(f, "{}fr", round_2(self.get())) } } diff --git a/src/geom/length.rs b/src/geom/length.rs index b01a7123..a095b9ad 100644 --- a/src/geom/length.rs +++ b/src/geom/length.rs @@ -139,17 +139,7 @@ impl Length { impl Debug for Length { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - use LengthUnit::*; - - // Format with the unit that yields the shortest output, preferring - // larger / metric units when tied. - let unit = [Cm, Mm, In, Pt] - .iter() - .copied() - .min_by_key(|&unit| self.to_unit(unit).to_string().len()) - .unwrap(); - - write!(f, "{}{:?}", self.to_unit(unit), unit) + write!(f, "{}pt", round_2(self.to_pt())) } } @@ -264,12 +254,4 @@ mod tests { fn test_length_unit_conversion() { assert!((Length::mm(150.0).to_cm() - 15.0) < 1e-4); } - - #[test] - fn test_length_formatting() { - assert_eq!(format!("{:?}", Length::pt(23.0)), "23pt"); - assert_eq!(format!("{:?}", Length::pt(-28.3465)), "-1cm"); - assert_eq!(format!("{:?}", Length::cm(12.728)), "12.728cm"); - assert_eq!(format!("{:?}", Length::cm(4.5)), "45mm"); - } } diff --git a/src/geom/mod.rs b/src/geom/mod.rs index a03e88b0..1a48534c 100644 --- a/src/geom/mod.rs +++ b/src/geom/mod.rs @@ -59,3 +59,8 @@ pub trait Get { *self.get_mut(index) = component; } } + +/// Round a float to two decimal places. +fn round_2(value: f64) -> f64 { + (value * 100.0).round() / 100.0 +} diff --git a/src/geom/relative.rs b/src/geom/relative.rs index 6f6b152f..4b638805 100644 --- a/src/geom/relative.rs +++ b/src/geom/relative.rs @@ -51,7 +51,7 @@ impl Relative { impl Debug for Relative { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}%", 100.0 * self.get()) + write!(f, "{}%", round_2(100.0 * self.get())) } } diff --git a/tests/ref/code/repr.png b/tests/ref/code/repr.png index 47f21b03..be502800 100644 Binary files a/tests/ref/code/repr.png and b/tests/ref/code/repr.png differ