From 31d7d5ca7e1952aff4cacf20db87d44c63174e5c Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:06:47 +0100 Subject: [PATCH] Keep full precision in export size labels so they parse back Found while testing crates/doc/src/props.rs:1341 (ExportSize::label, via trim_num at crates/doc/src/text.rs:62) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Opus 5.5 --- crates/doc/src/props.rs | 11 ++++++----- crates/engine/src/import_export_tests.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) 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"); +}