From 74495a579ad1247a149229d5b4185467aaa33f00 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 21:04:34 -0500 Subject: [PATCH] craft-rtf: look up fonts, colours and authors through an index (#1466) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both writers found each run's font and colour by scanning the table so far (write.rs `Tables`, doc/export.rs `Tables` and `author` for `\revtbl`), so a document with n distinct fonts or colours took O(n²) comparisons. 3,000 runs with their own font and colour took 18,006,000 steps in the small writer and 13,498,500 in the word-model writer. Each table stays a Vec, in first-use order, so the output is unchanged. `Interned` adds a map from key to place beside it: the colour, the font name lowercased (as `eq_ignore_ascii_case` matched), or the trimmed author. Tests count lookups through a test-only counter, as PdfCraft's own RTF writer does: at most four per run for 3,000 runs in each writer, and font names still match without regard to ASCII case. --- crates/rtf/src/doc/export.rs | 55 +++++++--------------- crates/rtf/src/doc/tests.rs | 33 ++++++++++++- crates/rtf/src/write.rs | 89 ++++++++++++++++++++++++++++++----- crates/rtf/src/write_tests.rs | 38 +++++++++++++++ 4 files changed, 165 insertions(+), 50 deletions(-) create mode 100644 crates/rtf/src/write_tests.rs diff --git a/crates/rtf/src/doc/export.rs b/crates/rtf/src/doc/export.rs index 69fab3d76..c99261718 100644 --- a/crates/rtf/src/doc/export.rs +++ b/crates/rtf/src/doc/export.rs @@ -8,32 +8,27 @@ use crate::date::{date_words as rtf_date, iso_to_dttm}; // --------------------------------------------------------------------------------------------- // Export +use crate::write::{Interned, font_key}; pub use crate::{esc, esc_font}; pub(super) struct Tables { - pub(super) fonts: Vec, - pub(super) colors: Vec, + pub(super) fonts: Interned, + pub(super) colors: Interned, } impl Tables { + /// Tables whose font 0 is `base_font`, with no colours yet. + pub(super) fn new(base_font: String) -> Tables { + let mut t = Tables { fonts: Interned::new(), colors: Interned::new() }; + t.fonts.insert(font_key(&base_font), || base_font); + t + } pub(super) fn font(&mut self, name: &str) -> usize { - match self.fonts.iter().position(|f| f.eq_ignore_ascii_case(name)) { - Some(i) => i, - None => { - self.fonts.push(name.to_string()); - self.fonts.len() - 1 - } - } + self.fonts.insert(font_key(name), || name.to_string()) } /// Colour table index (1-based; 0 is "auto"). fn color(&mut self, c: Rgb) -> usize { - match self.colors.iter().position(|x| *x == c) { - Some(i) => i + 1, - None => { - self.colors.push(c); - self.colors.len() - } - } + self.colors.insert(c, || c) + 1 } } @@ -65,7 +60,7 @@ pub(super) struct Writer { /// List definitions: per list, the ordered flag per level. pub(super) lists: Vec<[bool; 9]>, /// Authors of tracked changes (`\revtbl` entries after "Unknown"), when the text has any. - pub(super) authors: Option>, + pub(super) authors: Option>, /// The character formatting the current paragraph's style gives its runs (heading bold and /// size, code font…): what a run's formatting without a direct value resolves to. pub(super) base: Fmt, @@ -306,18 +301,12 @@ impl Writer { /// `\revtbl` index of a tracked change's author (0 = "Unknown"). fn author(&mut self, a: &str) -> usize { - let authors = self.authors.get_or_insert_with(Vec::new); + let authors = self.authors.get_or_insert_with(Interned::new); let a = a.trim(); if a.is_empty() { return 0; } - match authors.iter().position(|x| x == a) { - Some(i) => i + 1, - None => { - authors.push(a.to_string()); - authors.len() - } - } + authors.insert(a.to_string(), || a.to_string()) + 1 } /// Control words of a tracked insertion and deletion (`\revised`, `\deleted`). @@ -858,15 +847,7 @@ pub fn export(doc: &Document) -> String { let doc = marked.as_ref().unwrap_or(doc); let flow = model::from_doc_tracked(doc); let base_font = doc.styles.default_chr.font.clone().unwrap_or_else(|| craft_wordmodel::styles::BODY_FONT.to_string()); - let mut w = Writer { - marks, - t: Tables { fonts: vec![base_font], colors: Vec::new() }, - lists: Vec::new(), - authors: None, - base: Fmt::default(), - sections: Vec::new(), - sect_i: 0, - }; + let mut w = Writer { marks, t: Tables::new(base_font), lists: Vec::new(), authors: None, base: Fmt::default(), sections: Vec::new(), sect_i: 0 }; let mut body = String::new(); // Every section starts with its own `\sectd`; `\sect` breaks only where there are several. let secs: Vec = doc.sections().into_iter().map(|(_, s)| s.clone()).collect(); @@ -882,12 +863,12 @@ pub fn export(doc: &Document) -> String { fn export_with(m: &Meta, w: &Writer, body: &str, sec: &craft_wordmodel::SectionProps) -> String { let mut out = String::from("{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1\n{\\fonttbl"); - for (i, f) in w.t.fonts.iter().enumerate() { + for (i, f) in w.t.fonts.items.iter().enumerate() { let fam = if model::is_mono(f) { "\\fmodern" } else { "\\fswiss" }; out.push_str(&format!("{{\\f{i}{fam}\\fcharset0 {};}}", esc_font(f))); } out.push_str("}\n{\\colortbl;"); - for c in &w.t.colors { + for c in &w.t.colors.items { out.push_str(&format!("\\red{}\\green{}\\blue{};", c.0, c.1, c.2)); } out.push_str("}\n{\\*\\generator WordCraft;}\n{\\stylesheet{\\s0\\snext0 Normal;}"); @@ -916,7 +897,7 @@ fn export_with(m: &Meta, w: &Writer, body: &str, sec: &craft_wordmodel::SectionP } if let Some(authors) = &w.authors { out.push_str("{\\*\\revtbl {Unknown;}"); - for a in authors { + for a in &authors.items { out.push_str(&format!("{{{};}}", esc(&a.replace(';', ",")))); } out.push_str("}\n"); diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 7c1075a86..50d2d0d61 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -1168,7 +1168,7 @@ fn old_formatting_in_a_code_paragraph_has_the_code_font() { p.push_text("x", &Fmt { font: Some("Arial".into()), fmt_change: Some(Box::new(FmtChange { mark, old: Fmt::default() })), ..Default::default() }); let mut w = Writer { marks: Vec::new(), - t: Tables { fonts: vec!["Aptos".into()], colors: Vec::new() }, + t: Tables::new("Aptos".into()), lists: Vec::new(), authors: None, base: Fmt::default(), @@ -1181,6 +1181,37 @@ fn old_formatting_in_a_code_paragraph_has_the_code_font() { assert!(out.contains(&format!("{{\\*\\oldcprops \\f{mono}\\crauth1}}")), "{out}"); } +#[test] +fn many_distinct_fonts_colours_and_authors_take_constant_steps_each() { + use model::FmtChange; + // Each run has its own font, colour and tracked-change author. + let n = 3000; + let mut p = Para::default(); + for i in 0..n { + let mark = RevMark { author: format!("Author {i}"), date: String::new(), id: None }; + let color = Rgb((i >> 16) as u8, (i >> 8) as u8, i as u8); + let font: Arc = format!("Font {i}").into(); + p.push_text( + "x", + &Fmt { font: Some(font), color: Some(color), fmt_change: Some(Box::new(FmtChange { mark, old: Fmt::default() })), ..Default::default() }, + ); + } + let mut w = Writer { + marks: Vec::new(), + t: Tables::new("Aptos".into()), + lists: Vec::new(), + authors: None, + base: Fmt::default(), + sections: Vec::new(), + sect_i: 0, + }; + let mut out = String::new(); + let steps = crate::write::lookup_steps(|| w.para(&p, false, None, "\\par", &mut out)); + assert!(steps <= 4 * n, "{steps} steps for {n} runs"); + assert!(out.contains(&format!("\\f{n}\\cf{n}")), "the last run names the last font and colour"); + assert!(out.contains(&format!("\\crauth{n}")), "the last run names the last author"); +} + /// The text runs of `rtf`'s paragraphs, in order, with their formatting. fn text_runs(rtf: &[u8]) -> Vec<(String, Fmt)> { let f = parse(rtf).unwrap(); diff --git a/crates/rtf/src/write.rs b/crates/rtf/src/write.rs index 0b8f1dc8d..ce9b1610a 100644 --- a/crates/rtf/src/write.rs +++ b/crates/rtf/src/write.rs @@ -1,6 +1,9 @@ //! The model as RTF, and the escaping every RTF writer needs. +use std::borrow::Borrow; +use std::collections::HashMap; use std::fmt::Write; +use std::hash::Hash; use crate::model::{Align, Block, COLUMN_BREAK, CharFmt, Document, LINE_BREAK, PAGE_BREAK, Para, VertAlign}; @@ -48,31 +51,89 @@ pub fn esc_font(name: &str) -> String { esc(&name.replace(char::is_control, "")).replace(';', "\\'3b") } +#[cfg(test)] +thread_local! { + /// Lookups the writers make in their font, colour and author tables, counted in tests: each + /// must take one step however long the table is, so a document with thousands of fonts or + /// colours is written in linear time. + static STEPS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The lookup steps `f` takes (see [`STEPS`]). +#[cfg(test)] +pub(crate) fn lookup_steps(f: impl FnOnce()) -> usize { + STEPS.with(|s| s.set(0)); + f(); + STEPS.with(|s| s.get()) +} + +/// A table written in the order its entries were first used (`\fonttbl`, `\colortbl`, +/// `\revtbl`), with an index from each entry's key to its place, so that a lookup doesn't scan +/// the table: a document can use thousands of fonts or colours. +pub(crate) struct Interned { + pub(crate) items: Vec, + index: HashMap, +} + +impl Interned { + pub(crate) fn new() -> Self { + Interned { items: Vec::new(), index: HashMap::new() } + } + + /// The place of the entry with `key`. + pub(crate) fn get(&self, key: &Q) -> Option + where + K: Borrow, + { + #[cfg(test)] + STEPS.with(|s| s.set(s.get() + 1)); + self.index.get(key).copied() + } + + /// The place of the entry with `key`, adding `item()` at the end when there is none. + pub(crate) fn insert(&mut self, key: K, item: impl FnOnce() -> T) -> usize { + if let Some(i) = self.get(&key) { + return i; + } + self.items.push(item()); + let i = self.items.len() - 1; + self.index.insert(key, i); + i + } +} + +/// A font table's key for `name`: RTF writers match font names without regard to ASCII case, +/// and the table keeps the first spelling. +pub(crate) fn font_key(name: &str) -> String { + name.to_ascii_lowercase() +} + /// The fonts and colours a document uses, in the order of their tables. Font 0 is the default /// font; colour 0 is the default (auto) colour, so the first real colour is `\cf1`. struct Tables { - fonts: Vec, - colors: Vec<[u8; 3]>, + fonts: Interned, + colors: Interned<[u8; 3], [u8; 3]>, } impl Tables { fn of(doc: &Document, default_font: &str) -> Tables { - let mut t = Tables { fonts: vec![default_font.to_string()], colors: Vec::new() }; + let mut t = Tables { fonts: Interned::new(), colors: Interned::new() }; + t.fonts.insert(font_key(default_font), || default_font.to_string()); for fmt in doc.paras().flat_map(|p| &p.runs).map(|r| &r.fmt) { - if let Some(f) = fmt.font.as_deref().filter(|f| t.font(f).is_none()) { - t.fonts.push(f.to_string()); + if let Some(f) = fmt.font.as_deref() { + t.fonts.insert(font_key(f), || f.to_string()); } - if let Some(c) = fmt.color.filter(|c| t.color(*c).is_none()) { - t.colors.push(c); + if let Some(c) = fmt.color { + t.colors.insert(c, || c); } } t } fn font(&self, name: &str) -> Option { - self.fonts.iter().position(|f| f.eq_ignore_ascii_case(name)) + self.fonts.get(font_key(name).as_str()) } fn color(&self, c: [u8; 3]) -> Option { - self.colors.iter().position(|k| *k == c).map(|i| i + 1) + self.colors.get(&c).map(|i| i + 1) } fn fmt_words(&self, f: &CharFmt) -> String { @@ -148,13 +209,13 @@ impl Tables { pub fn write(doc: &Document, default_font: &str) -> String { let t = Tables::of(doc, default_font); let mut o = String::from("{\\rtf1\\ansi\\ansicpg1252\\deff0\\uc1{\\fonttbl"); - for (i, f) in t.fonts.iter().enumerate() { + for (i, f) in t.fonts.items.iter().enumerate() { let _ = write!(o, "{{\\f{i}\\fnil {};}}", esc_font(f)); } o.push_str("}\n"); - if !t.colors.is_empty() { + if !t.colors.items.is_empty() { o.push_str("{\\colortbl;"); - for [r, g, b] in &t.colors { + for [r, g, b] in &t.colors.items { let _ = write!(o, "\\red{r}\\green{g}\\blue{b};"); } o.push_str("}\n"); @@ -196,3 +257,7 @@ pub fn write(doc: &Document, default_font: &str) -> String { o.push('}'); o } + +#[cfg(test)] +#[path = "write_tests.rs"] +mod tests; diff --git a/crates/rtf/src/write_tests.rs b/crates/rtf/src/write_tests.rs new file mode 100644 index 000000000..ff208c88d --- /dev/null +++ b/crates/rtf/src/write_tests.rs @@ -0,0 +1,38 @@ +use super::*; +use crate::model::Run; + +/// A paragraph of `n` runs, each in its own font and colour. +fn varied(n: usize) -> Document { + let runs = (0..n) + .map(|i| Run { + text: "x".into(), + fmt: CharFmt { font: Some(format!("Font {i}")), color: Some([(i >> 16) as u8, (i >> 8) as u8, i as u8]), ..CharFmt::default() }, + }) + .collect(); + Document { blocks: vec![Block::Para(Para { runs, ..Para::default() })], pictures_left_out: 0 } +} + +#[test] +fn many_distinct_fonts_and_colours_take_constant_steps_each() { + let n = 3000; + let doc = varied(n); + let mut out = String::new(); + let steps = lookup_steps(|| out = write(&doc, "Helvetica")); + // The default font, then two lookups per run to build the tables and two to write it. + assert!(steps <= 4 * n + 1, "{steps} steps for {n} runs"); + assert!(out.contains(&format!("{{\\f{n}\\fnil Font {};}}", n - 1)), "the last font has the last slot"); + assert!(out.contains(&format!("\\f{n}\\cf{n} x")), "the last run names the last font and colour"); +} + +#[test] +fn fonts_match_without_regard_to_ascii_case() { + let run = |font: &str| Run { text: "x".into(), fmt: CharFmt { font: Some(font.into()), ..CharFmt::default() } }; + let doc = Document { + blocks: vec![Block::Para(Para { runs: vec![run("Arial"), run("ARIAL"), run("helvetica")], ..Para::default() })], + pictures_left_out: 0, + }; + let out = write(&doc, "Helvetica"); + assert!(out.contains("{\\fonttbl{\\f0\\fnil Helvetica;}{\\f1\\fnil Arial;}}"), "{out}"); + // Both spellings of Arial are font 1; the default font's run names no font. + assert!(out.contains("{\\f1 x}{\\f1 x}x\\par"), "{out}"); +}