diff --git a/crates/doc/src/props.rs b/crates/doc/src/props.rs index 0d4361b2..403620d6 100644 --- a/crates/doc/src/props.rs +++ b/crates/doc/src/props.rs @@ -1337,13 +1337,14 @@ impl ExportSize { (value.is_finite() && value > 0.0 && value <= max).then_some(self) } - /// "2x", "512w", "512h". + /// "2x", "0.125x", "512w", "512h". Not rounded, so `parse` reads it back as the same size. pub fn label(self) -> String { - let n = |v: f64| crate::text::trim_num(v); + // `{}` prints the shortest text that parses back to the same `f64`, and whole numbers + // without a fraction ("2", not "2.0"). match self { - ExportSize::Scale { value } => format!("{}x", n(value)), - ExportSize::Width { value } => format!("{}w", n(value)), - ExportSize::Height { value } => format!("{}h", n(value)), + ExportSize::Scale { value } => format!("{value}x"), + ExportSize::Width { value } => format!("{value}w"), + ExportSize::Height { value } => format!("{value}h"), } } diff --git a/crates/engine/src/import_export_tests.rs b/crates/engine/src/import_export_tests.rs index 5560e9fd..a60f85b0 100644 --- a/crates/engine/src/import_export_tests.rs +++ b/crates/engine/src/import_export_tests.rs @@ -232,3 +232,21 @@ fn export_sizes_parse_and_label() { assert_eq!(e(ExportSize::Scale { value: 2.0 }, "-big", ExportFormat::Png).file_suffix(), "-big"); assert_eq!(e(ExportSize::Width { value: 64.0 }, "", ExportFormat::Png).file_suffix(), ""); } + +#[test] +fn export_size_label_parses_back_exactly() { + for want in [ + ExportSize::Scale { value: 0.125 }, + ExportSize::Scale { value: 0.004 }, + ExportSize::Scale { value: 0.001 }, + ExportSize::Scale { value: 63.99 }, + ExportSize::Scale { value: 1.0 / 3.0 }, + ExportSize::Width { value: 512.125 }, + ExportSize::Height { value: 0.005 }, + ] { + assert_eq!(ExportSize::parse(&want.label()), Some(want), "{}", want.label()); + } + assert_eq!(ExportSize::Scale { value: 0.125 }.label(), "0.125x"); + assert_eq!(ExportSize::Scale { value: 2.0 }.label(), "2x"); + assert_eq!(ExportSize::Width { value: 512.0 }.label(), "512w"); +}