Merge pull request #1467 from keithcsqueensu/rtf-writer-lookup-1466

craft-rtf: look up fonts, colours and authors through an index (#1466)
This commit is contained in:
Dylan M. Taylor
2026-11-15 06:03:00 -05:00
committed by GitHub
4 changed files with 179 additions and 50 deletions
+24 -37
View File
@@ -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<String>,
pub(super) colors: Vec<Rgb>,
pub(super) fonts: Interned<String, String>,
pub(super) colors: Interned<Rgb, Rgb>,
}
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<Vec<String>>,
pub(super) authors: Option<Interned<String, String>>,
/// 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,18 @@ 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()
}
}
// A borrowed lookup first: most calls name an author already in the table, and need no
// `String` for the key.
let i = match authors.get(a) {
Some(i) => i,
None => authors.insert(a.to_string(), || a.to_string()),
};
i + 1
}
/// Control words of a tracked insertion and deletion (`\revised`, `\deleted`).
@@ -858,15 +853,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<craft_wordmodel::SectionProps> = doc.sections().into_iter().map(|(_, s)| s.clone()).collect();
@@ -882,12 +869,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 +903,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");
+34 -1
View File
@@ -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,39 @@ 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<str> = 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));
// One lookup each for the font and the colour, and two for a new author: the borrowed
// lookup, then the insert.
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();
+83 -12
View File
@@ -1,6 +1,10 @@
//! The model as RTF, and the escaping every RTF writer needs.
use std::borrow::Borrow;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
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 +52,94 @@ 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<usize> = 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<T, K> {
pub(crate) items: Vec<T>,
index: HashMap<K, usize>,
}
impl<T, K: Eq + Hash> Interned<T, K> {
pub(crate) fn new() -> Self {
Interned { items: Vec::new(), index: HashMap::new() }
}
/// The place of the entry with `key`.
pub(crate) fn get<Q: Eq + Hash + ?Sized>(&self, key: &Q) -> Option<usize>
where
K: Borrow<Q>,
{
#[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: one
/// lookup in the index either way.
pub(crate) fn insert(&mut self, key: K, item: impl FnOnce() -> T) -> usize {
#[cfg(test)]
STEPS.with(|s| s.set(s.get() + 1));
match self.index.entry(key) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
self.items.push(item());
let i = self.items.len() - 1;
e.insert(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<String>,
colors: Vec<[u8; 3]>,
fonts: Interned<String, String>,
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<usize> {
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<usize> {
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 +215,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 +263,7 @@ pub fn write(doc: &Document, default_font: &str) -> String {
o.push('}');
o
}
#[cfg(test)]
#[path = "write_tests.rs"]
mod tests;
+38
View File
@@ -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}");
}