diff --git a/crates/rtf/src/doc/parse.rs b/crates/rtf/src/doc/parse.rs index c51d402a4..c0c3a4fea 100644 --- a/crates/rtf/src/doc/parse.rs +++ b/crates/rtf/src/doc/parse.rs @@ -54,9 +54,6 @@ fn parse_full(bytes: &[u8], marks: bool) -> Result { } b'}' => { r.close(); - if r.info_depth.is_some_and(|d| r.depth() < d) { - r.info_depth = None; - } first_in_group = false; starred = false; i += 1; @@ -124,9 +121,6 @@ fn parse_full(bytes: &[u8], marks: bool) -> Result { first_in_group = false; continue; } - if word == "info" { - r.info_depth = Some(r.depth()); - } r.word(word, param, starred, first_in_group); first_in_group = false; starred = false; diff --git a/crates/rtf/src/doc/reader.rs b/crates/rtf/src/doc/reader.rs index 4bf83184b..e3cac7901 100644 --- a/crates/rtf/src/doc/reader.rs +++ b/crates/rtf/src/doc/reader.rs @@ -95,9 +95,7 @@ pub(super) struct Reader { pub(super) def_cur: CellDef, pub(super) row_header: bool, pub(super) created: Option<(u32, u32, u32, u32, u32)>, - pub(super) in_creatim: bool, pub(super) rule_pending: bool, - pub(super) info_depth: Option, // Notes and comments (only when the caller wants them). pub(super) marks_on: bool, pub(super) sub: Option, @@ -370,7 +368,7 @@ impl Reader { Dest::FontTbl if !self.font_name.is_empty() => self.commit_font(), Dest::StyleSheet if !self.style_name.is_empty() => self.commit_style(), Dest::RevTbl if !self.rev_name.trim().is_empty() => self.commit_author(), - Dest::Info(k) => { + Dest::Info(k) if self.stack.last().is_none_or(|parent| parent.dest != closing) => { let t = std::mem::take(&mut self.info_text).trim().to_string(); match k { 1 => self.meta.title = t, @@ -380,7 +378,6 @@ impl Reader { 5 => self.meta.description = t, _ => {} } - self.in_creatim = false; } Dest::Atn(k) => self.commit_atn(k), Dest::ShpSv => self.commit_shape_prop(), diff --git a/crates/rtf/src/doc/state.rs b/crates/rtf/src/doc/state.rs index a39eacc2f..9091b7af1 100644 --- a/crates/rtf/src/doc/state.rs +++ b/crates/rtf/src/doc/state.rs @@ -28,7 +28,9 @@ pub(super) enum Dest { FontTbl, ColorTbl, StyleSheet, + InfoRoot, Info(u8), + Created, ListTable, ListOverride, FldInst, diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 2a7c9d42c..51429d98e 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -2180,3 +2180,29 @@ fn the_generator_names_the_writing_application() { let back = parse(odd.as_bytes()).expect("parses"); assert_eq!(texts(&back), ["Normal:Hello"]); } + +#[test] +fn metadata_text_survives_nested_formatting_without_reading_other_destinations() { + let flow = parse(br"{\rtf1\ansi{\fonttbl{\f0 Arial;}}{\info{\title Before {\b bold {\i caf\u233?}}{\fonttbl{\f0 Wrong;}}{\*\bkmkstart hidden}{\pict\pngblip 00} After}{\author Author}}\f0 Body}").unwrap(); + assert_eq!(flow.meta.title, "Before bold café After"); + assert_eq!(flow.meta.author, "Author"); + assert_eq!(texts(&flow), ["Normal:Body"]); + let FBlock::Para(body) = &flow.blocks[0] else { panic!("expected body text") }; + assert!(matches!(&body.inlines[0], Inline::Text(_, fmt) if fmt.font.as_deref() == Some("Arial"))); +} + +#[test] +fn metadata_creation_date_ignores_other_date_destinations() { + let flow = parse(br"{\rtf1\ansi{\info{\creatim\yr2026{\revtim\yr1999}{\b\mo11}\dy19}}Body}").unwrap(); + assert_eq!(flow.meta.created, "2026-11-19T00:00:00Z"); + assert_eq!(texts(&flow), ["Normal:Body"]); +} + +#[test] +fn metadata_text_controls_keep_punctuation_and_use_the_selected_font_code_page() { + let flow = + parse(br"{\rtf1\ansi{\fonttbl{\f0\fcharset0 Arial;}{\f1\fcharset204 Cyrillic;}}{\info{\title A\emdash {\f1 \'e0\plain \'e0}}}\f0 Body}") + .unwrap(); + assert_eq!(flow.meta.title, "A—аà"); + assert_eq!(texts(&flow), ["Normal:Body"]); +} diff --git a/crates/rtf/src/doc/text.rs b/crates/rtf/src/doc/text.rs index ce77a20d3..ca31e8218 100644 --- a/crates/rtf/src/doc/text.rs +++ b/crates/rtf/src/doc/text.rs @@ -310,7 +310,7 @@ impl Reader { } } Dest::ShpSn | Dest::ShpSv => self.shape_text(s), - Dest::Skip | Dest::ListTable | Dest::ListOverride | Dest::OldProps(_) | Dest::ShpInst => {} + Dest::Skip | Dest::InfoRoot | Dest::Created | Dest::ListTable | Dest::ListOverride | Dest::OldProps(_) | Dest::ShpInst => {} } } diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index 0a0df5c68..b0ef9bab3 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -21,8 +21,11 @@ impl Reader { self.finish_multibyte(); let on = p != Some(0); let pv = p.unwrap_or(0); + if self.info_word(w, p, starred, first_in_group) { + return; + } self.note_defshp(w); - if self.skip_destination_word(w, pv) { + if self.st.dest == Dest::Skip { return; } if self.shape_word(w, p, first_in_group) { @@ -311,12 +314,7 @@ impl Reader { "fonttbl" => Some(Dest::FontTbl), "colortbl" => Some(Dest::ColorTbl), "stylesheet" => Some(Dest::StyleSheet), - "info" => Some(Dest::Skip), - "title" if self.st.dest == Dest::Skip && self.in_info() => Some(Dest::Info(1)), - "author" if self.in_info() => Some(Dest::Info(2)), - "subject" if self.in_info() => Some(Dest::Info(3)), - "keywords" if self.in_info() => Some(Dest::Info(4)), - "doccomm" if self.in_info() => Some(Dest::Info(5)), + "info" => Some(Dest::InfoRoot), "listtable" => Some(Dest::ListTable), "revtbl" => Some(Dest::RevTbl), "oldcprops" => Some(Dest::OldProps(false)), @@ -348,10 +346,6 @@ impl Reader { | "nofwords" | "nofchars" | "edmins" | "vern" | "themedata" | "colorschememapping" | "latentstyles" | "datastore" | "xmlnstbl" | "rsidtbl" | "generator" | "mmathPr" | "pgdsctbl" | "filetbl" | "listpicture" | "userprops" | "docvar" | "objdata" | "fontemb" | "fontfile" | "panose" | "falt" | "mhtmltag" | "htmltag" | "pnseclvl" | "background" | "sp" | "pn" | "pgptbl" => Some(Dest::Skip), - "creatim" if self.in_info() => { - self.in_creatim = true; - Some(Dest::Skip) - } _ => None, } } @@ -410,12 +404,7 @@ impl Reader { fn destination_word(&mut self, word: &str, param: Option, on: bool, value: i32) -> bool { match self.st.dest { - Dest::Skip => { - if self.in_creatim { - self.record_created_part(word, value); - } - true - } + Dest::Skip | Dest::InfoRoot | Dest::Created => true, Dest::FontTbl if !matches!(word, "u" | "uc") => { self.font_table_word(word, value); true @@ -483,7 +472,8 @@ impl Reader { } !matches!(word, "u" | "uc") } - Dest::Info(_) | Dest::FldInst | Dest::Bookmark if !matches!(word, "u" | "uc") => true, + Dest::Info(_) if !metadata_text_word(word) => true, + Dest::FldInst | Dest::Bookmark if !matches!(word, "u" | "uc") => true, Dest::Info(_) | Dest::FldInst | Dest::Bookmark => false, Dest::OldProps(false) if matches!(word, "crauth" | "crdate") => { if word == "crauth" { @@ -578,15 +568,42 @@ impl Reader { } } - fn skip_destination_word(&mut self, word: &str, value: i32) -> bool { - let kept_info = self.in_info() && matches!(word, "title" | "author" | "subject" | "keywords" | "doccomm" | "creatim"); - if self.st.dest != Dest::Skip || kept_info { - return false; + /// Metadata groups use the same stack as the body, but never dispatch document-level + /// destinations (font tables, pictures, bookmarks and revisions) from inside a property. + fn info_word(&mut self, word: &str, value: Option, starred: bool, first_in_group: bool) -> bool { + match self.st.dest { + Dest::InfoRoot => { + if first_in_group { + self.st.dest = match word { + "title" => Dest::Info(1), + "author" => Dest::Info(2), + "subject" => Dest::Info(3), + "keywords" => Dest::Info(4), + "doccomm" => Dest::Info(5), + "creatim" => Dest::Created, + _ => Dest::Skip, + }; + } + true + } + Dest::Info(_) | Dest::Created => { + let text = metadata_text_word(word); + let date = self.st.dest == Dest::Created && matches!(word, "yr" | "mo" | "dy" | "hr" | "min"); + if first_in_group && (starred || !(text || date || is_format_word(word))) { + self.st.dest = Dest::Skip; + } else if date && let Some(value) = value { + self.record_created_part(word, value); + } else if self.st.dest != Dest::Created { + if word == "plain" { + self.st.cp = self.font_cps.get(&self.deff).copied(); + } else if text { + return false; + } + } + true + } + _ => false, } - if self.in_creatim { - self.record_created_part(word, value); - } - true } fn record_created_part(&mut self, word: &str, value: i32) { @@ -608,10 +625,27 @@ impl Reader { self.st.field = Some(self.fields.len() - 1); } } +} - fn in_info(&self) -> bool { - self.info_depth.is_some_and(|d| self.depth() > d) - } +/// Words that only select a group's font, set its Unicode fallback or emit text. +fn metadata_text_word(word: &str) -> bool { + matches!( + word, + "f" | "u" + | "uc" + | "line" + | "softline" + | "tab" + | "emdash" + | "endash" + | "bullet" + | "lquote" + | "rquote" + | "ldblquote" + | "rdblquote" + | "emspace" + | "enspace" + ) } /// The target of a `HYPERLINK` field instruction; `%22` is a quote, as the writer stores one.