Merge pull request #1507 from Silvertree2010/fix/h3859-export-size-label-rounds-the-sca

Keep full precision in export size labels so they parse back
This commit is contained in:
BFlat
2026-10-30 00:19:59 +07:00
committed by GitHub
2 changed files with 24 additions and 5 deletions
+6 -5
View File
@@ -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"),
}
}
+18
View File
@@ -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");
}