mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 16:54:45 +00:00
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.
39 lines
1.7 KiB
Rust
39 lines
1.7 KiB
Rust
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}");
|
|
}
|