mirror of
https://github.com/storytold/figcraft.git
synced 2026-10-30 05:16:56 -06:00
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 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
7c9184b023
commit
31d7d5ca7e
@@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user