Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Measurement Scale: large pixel lengths print their real value (#7016) (#7017)

David Mann8ef5337c29b0
diff --git a/crates/doc/src/analysis.rs b/crates/doc/src/analysis.rs
index 840bf5499..9b1d8efdb 100644
--- a/crates/doc/src/analysis.rs
+++ b/crates/doc/src/analysis.rs
@@ -30,14 +30,12 @@ impl MeasurementScale {
}
/// Photoshop's "Scale" column text, e.g. `1 pixels = 1.0000 pixels`.
pub fn describe(&self) -> String {
- format!("{} pixels = {:.4} {}", trim(self.pixel_length), self.logical_length, self.units)
+ // f64 Display already drops a whole number's `.0` (`5`, `0.5`) and, unlike an i64 cast,
+ // prints lengths past i64::MAX in full.
+ format!("{} pixels = {:.4} {}", self.pixel_length, self.logical_length, self.units)
}
}
-fn trim(v: f64) -> String {
- if v.fract() == 0.0 { format!("{}", v as i64) } else { format!("{v}") }
-}
-
/// One Count tool group: its numbered markers (document pixel coordinates, in click order).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
@@ -133,6 +131,17 @@ mod tests {
assert_eq!(MeasurementScale { pixel_length: 0.0, ..Default::default() }.factor(), 1.0);
}
+ #[test]
+ fn scale_text_prints_large_and_fractional_pixel_lengths() {
+ let big = MeasurementScale { pixel_length: 1e20, logical_length: 1.0, units: "mm".into() };
+ assert_eq!(big.describe(), "100000000000000000000 pixels = 1.0000 mm");
+ assert_eq!(MeasurementScale::default().describe(), "1 pixels = 1.0000 pixels");
+ let s = |p: f64| MeasurementScale { pixel_length: p, ..Default::default() }.describe();
+ assert_eq!(s(5.0), "5 pixels = 1.0000 pixels");
+ assert_eq!(s(0.5), "0.5 pixels = 1.0000 pixels");
+ assert_eq!(s(12.25), "12.25 pixels = 1.0000 pixels");
+ }
+
#[test]
fn serde_defaults_fill_missing_fields() {
let m: Measurement = serde_json::from_str("{}").unwrap();