LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Print: match paper names past punctuation and resolve aliases
midasdf6b0c89133344
diff --git a/crates/engine/src/print/layout.rs b/crates/engine/src/print/layout.rsindex c267c0107..be4438424 100644--- a/crates/engine/src/print/layout.rs+++ b/crates/engine/src/print/layout.rs@@ -88,33 +88,38 @@ pub fn paper_names() -> impl Iterator<Item = &'static str> {PAPER.iter().map(|p| p.0)}-/// The form [`paper_size`] matches names in: lower case, with whitespace, hyphens, underscores and-/// parentheses ignored and `×`/`*` read as `x`, so `L判`, `l-size` and `l` are one name and `4 × 6`-/// is `4x6`.+/// The form [`paper_name`] matches names in: lower case, letters and digits only, and `×`/`*` read+/// as `x`, so `L判`, `L判。`, `l-size` and `(l)` are one name and `4 × 6` is `4x6`.fn normalize_paper(name: &str) -> String {name.chars()- .filter(|c| !c.is_whitespace() && !matches!(c, '-' | '_' | '(' | ')'))- .map(|c| match c {- '×' | '*' => 'x',- c => c,+ .filter_map(|c| match c {+ '×' | '*' => Some('x'),+ c if c.is_alphanumeric() => Some(c),+ _ => None,}).collect::<String>().to_lowercase()}-/// Portrait size in inches of a named paper, `None` for unknown names and `custom`. Case, spaces-/// and punctuation don't matter, and the Japanese customary names (`L判`, `2L判`, `KG判`, `はがき`,-/// `往復はがき`, `六切`, `四切`, `名刺`) are aliases of the papers above.-pub fn paper_size(name: &str) -> Option<(f64, f64)> {+/// The canonical name of the paper `name` stands for, `None` for unknown names and `custom`.+/// `L判` is `l`, `はがき` is `postcard`, `六切` is `6cut`, and so on.+pub fn paper_name(name: &str) -> Option<&'static str> {let n = normalize_paper(name);if n.is_empty() {return None;}- let canonical = PAPER+ PAPER.iter().find(|p| normalize_paper(p.0) == n).map(|p| p.0)- .or_else(|| PAPER_ALIASES.iter().find(|(alias, _)| normalize_paper(alias) == n).map(|(_, name)| *name))?;+ .or_else(|| PAPER_ALIASES.iter().find(|(alias, _)| normalize_paper(alias) == n).map(|(_, name)| *name))+}++/// Portrait size in inches of a named paper, `None` for unknown names and `custom`. Case, spaces+/// and punctuation don't matter, and the Japanese customary names (`L判`, `2L判`, `KG判`, `はがき`,+/// `往復はがき`, `六切`, `四切`, `名刺`) are aliases of the papers above.+pub fn paper_size(name: &str) -> Option<(f64, f64)> {+ let canonical = paper_name(name)?;PAPER.iter().find(|p| p.0 == canonical).map(|p| (p.1, p.2))}@@ -1036,6 +1041,12 @@ mod tests {] {assert_eq!(paper_size(a), paper_size(b), "{a} vs {b}");}+ // Punctuation is ignored, including the periods and Japanese marks a pasted name may carry.+ for (name, canonical) in [("L判。", "l"), ("L判、", "l"), ("(L判)", "l"), ("l-size.", "l"), ("はがき。", "postcard"), ("6切。", "6cut")]+ {+ assert_eq!(paper_name(name), Some(canonical), "{name}");+ }+ assert_eq!(paper_name("custom"), None);assert!(paper_size("poster").is_none());// The papers that were already there keep their sizes.assert_eq!(pt("letter"), (612.0, 792.0));diff --git a/crates/ui-egui/src/print_view.rs b/crates/ui-egui/src/print_view.rsindex 918287bdc..fd111b207 100644--- a/crates/ui-egui/src/print_view.rs+++ b/crates/ui-egui/src/print_view.rs@@ -154,6 +154,12 @@ fn paper_label(name: &str) -> String {}}+/// The paper the selector shows as current: `custom`, or the canonical name an alias stands for,+/// so a `print.set` with `はがき` still selects the `postcard` option.+fn current_paper(paper: &str) -> &str {+ if paper.eq_ignore_ascii_case("custom") { "custom" } else { layout::paper_name(paper).unwrap_or(paper) }+}+/// The dialog's body: the settings column and the page preview side by side.pub fn body(app: &mut LightcraftApp, ui: &mut Ui, t: &Tokens) {let ctx = ui.ctx().clone();@@ -383,8 +389,8 @@ fn settings(app: &mut LightcraftApp, ui: &mut Ui, t: &Tokens, pu: &mut PrintUi,heading(ui, t, "Page Setup");row(ui, t, "Paper", |ui| {- let current = if l.page.paper.eq_ignore_ascii_case("custom") { "custom".to_string() } else { l.page.paper.to_ascii_lowercase() };- let c = egui::ComboBox::from_id_salt("printPaper").selected_text(paper_label(¤t)).show_ui(ui, |ui| {+ let current = current_paper(&l.page.paper);+ let c = egui::ComboBox::from_id_salt("printPaper").selected_text(paper_label(current)).show_ui(ui, |ui| {for name in layout::paper_names().chain(std::iter::once("custom")) {if ui.selectable_label(current == name, paper_label(name)).clicked() {patch(app, &ctx, json!({"layout": {"page": {"paper": name}}}));@@ -974,5 +980,11 @@ mod tests {assert_eq!(paper_label("double-postcard"), "往復はがき (148 × 200 mm)");assert_eq!(paper_label("6cut"), "六切 (203 × 254 mm)");assert_eq!(paper_label("4cut"), "四切 (254 × 305 mm)");+ // The selector resolves an alias stored by print.set to the canonical option.+ assert_eq!(current_paper("はがき"), "postcard");+ assert_eq!(current_paper("L判"), "l");+ assert_eq!(current_paper("Custom"), "custom");+ assert_eq!(paper_label(current_paper("はがき")), "はがき (100 × 148 mm)");+ assert_eq!(paper_label(current_paper("a4")), "A4");}}