PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M6: a display picture with month or weekday names leaves formattedValue raw
Eric Kaden21b6267129ac
diff --git a/crates/xfa/src/data.rs b/crates/xfa/src/data.rsindex d3e1f2c1a..fc0997fac 100644--- a/crates/xfa/src/data.rs+++ b/crates/xfa/src/data.rs@@ -239,7 +239,7 @@ fn date_pattern_of(doc: &Document, f: &FieldDatum) -> Option<String> {}/// The tokens of a date pattern: `yyyy`, `yy`, `mm`, `m`, `dd`, `d`, or a literal character.-fn date_tokens(pattern: &str) -> Vec<String> {+pub(crate) fn date_tokens(pattern: &str) -> Vec<String> {let mut out = Vec::new();let chars: Vec<char> = pattern.chars().collect();let mut i = 0;diff --git a/crates/xfa/src/layout.rs b/crates/xfa/src/layout.rsindex 6a38a9743..c71f63b40 100644--- a/crates/xfa/src/layout.rs+++ b/crates/xfa/src/layout.rs@@ -408,14 +408,22 @@ fn date_pattern(picture: Option<&str>) -> String {/// What scripts read as a field's `formattedValue` when its display picture is `picture`: the/// ISO date `raw` shown in a date picture (`date{YYYYMMDD}` turns `2026-10-20` into/// `20261020`; of several alternatives, the first). `None` for other kinds of picture, for a-/// clause this can't read and for a value that isn't an ISO date: the raw value stands then.+/// clause this can't read or show (month and weekday names) and for a value that isn't an ISO+/// date: the raw value stands then.pub fn formatted_value(picture: &str, raw: &str) -> Option<String> {let first = picture.split('|').next()?.trim();let rest = first.strip_prefix("date")?;if !rest.starts_with(['{', '(']) {return None;}- let shown = iso_to_pattern(raw, &date_pattern(Some(first)));+ let pattern = date_pattern(Some(first));+ // A picture with parts this can't show yet (month or weekday names, `MMM` or `EEE`) would+ // leave them as letters: the raw value stands then.+ let shows = |t: &String| !t.chars().all(|c| c.is_ascii_alphabetic()) || matches!(t.as_str(), "yyyy" | "yy" | "mm" | "m" | "dd" | "d");+ if !crate::data::date_tokens(&pattern).iter().all(shows) {+ return None;+ }+ let shown = iso_to_pattern(raw, &pattern);(shown != raw).then_some(shown)}diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rsindex 22ddb5ed2..0a655dd15 100644--- a/crates/xfa/src/tests.rs+++ b/crates/xfa/src/tests.rs@@ -351,6 +351,9 @@ fn formatted_values_follow_date_display_pictures() {assert_eq!(crate::formatted_value("num{zzz9}", "2026-10-20"), None);assert_eq!(crate::formatted_value("date{YYYYMMDD}", "20261020"), None, "not an ISO date");assert_eq!(crate::formatted_value("date{YYYYMMDD}", ""), None);+ // Month and weekday names aren't shown yet: the raw value stands rather than `20 mmm 2026`.+ assert_eq!(crate::formatted_value("date{DD MMM YYYY}", "2026-10-20"), None);+ assert_eq!(crate::formatted_value("date{EEE MM/DD/YYYY}", "2026-10-20"), None);}/// A field's `<format><picture>` is its display picture, kept apart from its `<ui>` edit