use crate::dvi::Writer;
use crate::ir::Cmd;
use crate::node::{BoxNode, CharNode, GlueNode, Node, Scaled};
use crate::tfm::Tfm;
const SP: f64 = 65536.0;
#[derive(Clone, Debug, PartialEq)]
pub struct Layout {
pub measure: f64,
pub height: f64,
pub leading: f64,
pub margin: f64,
pub size: f64,
pub breaking: crate::linebreak::Params,
pub paging: PageParams,
}
impl Default for Layout {
fn default() -> Self {
Self {
measure: 469.75,
height: 643.20,
leading: 12.0,
margin: 72.0,
size: 10.0,
breaking: crate::linebreak::Params::default(),
paging: PageParams::default(),
}
}
}
const PAPER_WIDTH: f64 = 612.0;
const PAPER_HEIGHT: f64 = 792.0;
const BP_PER_PT: f64 = 72.0 / 72.27;
impl Layout {
pub fn absorb_class_options(&mut self, options: &str) {
for option in options.split(',') {
let Some(size) = option
.trim()
.strip_suffix("pt")
.and_then(|n| n.parse::<f64>().ok())
.filter(|size| *size > 0.0)
else {
continue;
};
self.size = size * BP_PER_PT;
self.leading = normal_leading(size) * BP_PER_PT;
}
}
pub fn absorb_geometry_options(&mut self, options: &str) {
for option in options.split(',') {
let Some((key, value)) = option.split_once('=') else {
continue;
};
if key.trim() != "margin" {
continue;
}
let Some(margin) = dimen_bp(value.trim()) else {
continue;
};
self.margin = margin;
self.measure = PAPER_WIDTH - 2.0 * margin;
self.height = PAPER_HEIGHT - 2.0 * margin;
}
}
}
fn normal_leading(size: f64) -> f64 {
let named = [
(8.0, 9.5),
(9.0, 11.0),
(10.0, 12.0),
(11.0, 13.6),
(12.0, 14.5),
(14.0, 17.0),
(17.0, 22.0),
(20.0, 25.0),
];
match named.iter().find(|(pt, _)| *pt == size) {
Some((_, on)) => *on,
None => size * 1.2,
}
}
fn dimen_bp(text: &str) -> Option<f64> {
let units = [
("in", 72.0),
("bp", 1.0),
("pt", BP_PER_PT),
("pc", 12.0 * BP_PER_PT),
("mm", 72.0 / 25.4),
("cm", 72.0 / 2.54),
];
let (unit, per_unit) = units.iter().find(|(unit, _)| text.ends_with(unit))?;
let number: f64 = text.strip_suffix(unit)?.trim().parse().ok()?;
Some(number * per_unit)
}
pub fn to_dvi(text: &str, font: &Tfm, font_name: &str, layout: &Layout) -> Vec<u8> {
let chain = FontChain {
fonts: vec![Loaded {
tfm: font.clone(),
name: font_name.to_string(),
}],
map: Vec::new(),
};
to_dvi_chain(text, &chain, layout)
}
pub fn to_dvi_chain(text: &str, chain: &FontChain, layout: &Layout) -> Vec<u8> {
let mut w = Writer::new("texrs");
let at = (layout.size * SP) as i32;
for (i, f) in chain.fonts.iter().enumerate() {
w.define_font(i as u32, &f.name, at, f.tfm.checksum, at);
}
let text = refs_numbered(text);
let lines = broken_lines(&text, chain, layout);
let per_page = ((layout.height / layout.leading).floor() as usize).max(1);
let encode = |(page, chunk): (usize, &[BrokenLine])| {
let counts = [page as i32 + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let page_box = page_box(chunk, page + 1, chain, layout);
let mut one = Writer::page();
crate::shipout::ship_out(
&mut one,
&page_box,
counts,
sp(layout.margin),
sp(layout.margin),
);
one.into_page()
};
let work: Vec<_> = lines.chunks(per_page).enumerate().collect();
let pages: Vec<_> = match work.len() >= PARALLEL_PAGES {
true => {
use rayon::prelude::*;
work.into_par_iter().map(encode).collect()
}
false => work.into_iter().map(encode).collect(),
};
for page in pages {
w.append_page(page);
}
w.finish()
}
fn sp(points: f64) -> Scaled {
(points * SP) as Scaled
}
const FOOTLINE_SKIP: f64 = 24.0;
fn footline_box(folio: usize, chain: &FontChain, layout: &Layout) -> BoxNode {
let mut list = vec![Node::Glue(GlueNode::new(crate::box_::fill::ss()))];
list.extend(line_hlist(&folio.to_string(), chain, layout));
list.push(Node::Glue(GlueNode::new(crate::box_::fill::ss())));
crate::pack::hpack(
list,
crate::pack::Spec::Exactly(sp(layout.measure)),
crate::pack::Tolerances::plain(),
None,
)
.node
}
fn page_box(lines: &[BrokenLine], folio: usize, chain: &FontChain, layout: &Layout) -> BoxNode {
let mut list: Vec<Node> = Vec::with_capacity(lines.len() * 2 + 2);
let mut prev_depth = crate::node::IGNORE_DEPTH;
let mut at: Scaled = 0;
for line in lines {
let b = line_box(line, chain, layout);
let skip = interline(prev_depth, b.height, layout);
at += skip + b.height + b.depth;
prev_depth = b.depth;
list.push(Node::Kern {
width: skip,
explicit: false,
});
list.push(Node::Box(b));
}
let foot = footline_box(folio, chain, layout);
let drop = sp(layout.height + FOOTLINE_SKIP) - at - foot.height;
list.push(Node::Kern {
width: drop,
explicit: false,
});
list.push(Node::Box(foot));
crate::pack::vpack(list, crate::pack::NATURAL, crate::pack::Tolerances::plain()).node
}
fn interline(prev_depth: Scaled, height: Scaled, layout: &Layout) -> Scaled {
const LINE_SKIP_LIMIT: Scaled = 0;
let above = match prev_depth <= crate::node::IGNORE_DEPTH {
true => 0,
false => prev_depth,
};
let d = sp(layout.leading) - above - height;
match d < LINE_SKIP_LIMIT {
true => sp(1.0),
false => d,
}
}
fn line_box(line: &BrokenLine, chain: &FontChain, layout: &Layout) -> BoxNode {
let spec = match line.justify {
true => crate::pack::Spec::Exactly(sp(layout.measure)),
false => crate::pack::NATURAL,
};
crate::pack::hpack(
line_hlist(&line.text, chain, layout),
spec,
crate::pack::Tolerances::plain(),
None,
)
.node
}
fn line_hlist(line: &str, chain: &FontChain, layout: &Layout) -> Vec<Node> {
let mut out: Vec<Node> = Vec::new();
let mut pending = String::new();
let mut chars = line.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\u{1}' {
append_carried(&mut out, &std::mem::take(&mut pending), chain, layout);
let mut spec = String::new();
for c in chars.by_ref() {
if c == '\u{2}' {
break;
}
spec.push(c);
}
let parts: Vec<&str> = spec.split(',').collect();
if parts.len() == 3 {
out.push(Node::Whatsit(format!(
"color push rgb {} {} {}",
parts[0], parts[1], parts[2]
)));
}
continue;
}
if ch == '\u{3}' {
append_carried(&mut out, &std::mem::take(&mut pending), chain, layout);
out.push(Node::Whatsit("color pop".to_string()));
continue;
}
if ch == FACE_PUSH {
append_carried(&mut out, &std::mem::take(&mut pending), chain, layout);
let _ = chars.next();
continue;
}
if ch == FACE_POP {
append_carried(&mut out, &std::mem::take(&mut pending), chain, layout);
continue;
}
if ch == LIST_INDENT {
append_carried(&mut out, &std::mem::take(&mut pending), chain, layout);
let _ = chars.next();
continue;
}
if ch == REF {
append_carried(&mut out, &std::mem::take(&mut pending), chain, layout);
for c in chars.by_ref() {
if c == REF {
break;
}
}
continue;
}
pending.push(ch);
}
append_carried(&mut out, &pending, chain, layout);
out
}
fn append_carried(out: &mut Vec<Node>, text: &str, chain: &FontChain, layout: &Layout) {
for item in chain.carried(text.chars()) {
match item {
Carried::Space => out.push(Node::Glue(GlueNode::new(interword_glue(chain, layout)))),
Carried::Set { font, sets } => {
for set in sets {
match set {
crate::tfm::Set::Char(code) => {
let m = chain.fonts[font].tfm.char(code).unwrap_or_default();
out.push(Node::Char(CharNode {
font,
character: char::from(code),
width: sp(m.width * layout.size),
height: sp(m.height * layout.size),
depth: sp(m.depth * layout.size),
}));
}
crate::tfm::Set::Kern(by) => out.push(Node::Kern {
width: sp(by * layout.size),
explicit: false,
}),
}
}
}
}
}
}
fn interword_glue(chain: &FontChain, layout: &Layout) -> crate::glue::Glue {
let p = chain.fonts[0].tfm.params;
crate::glue::Glue {
natural: sp(p.space * layout.size),
stretch: sp(p.stretch * layout.size),
stretch_order: 0,
shrink: sp(p.shrink * layout.size),
shrink_order: 0,
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct BrokenLine {
pub text: String,
pub justify: bool,
}
pub fn break_lines(text: &str, font: &Tfm, layout: &Layout) -> Vec<String> {
let chain = FontChain {
fonts: vec![Loaded {
tfm: font.clone(),
name: String::from("cmr10"),
}],
map: Vec::new(),
};
break_lines_chain(text, &chain, layout)
}
pub fn break_lines_chain(text: &str, chain: &FontChain, layout: &Layout) -> Vec<String> {
broken_lines(text, chain, layout)
.into_iter()
.map(|line| line.text)
.collect()
}
pub fn broken_lines(text: &str, chain: &FontChain, layout: &Layout) -> Vec<BrokenLine> {
use rayon::prelude::*;
let paras: Vec<&str> = text.split("\n\n").collect();
paras
.par_iter()
.map(|para| break_paragraph(para, chain, layout))
.reduce(Vec::new, |mut acc, mut more| {
acc.append(&mut more);
acc
})
}
fn break_paragraph(para: &str, chain: &FontChain, layout: &Layout) -> Vec<BrokenLine> {
let (params, para) = paragraph_params(para, &layout.breaking);
let para: &str = ¶
let fixed = |lines: Vec<String>| -> Vec<BrokenLine> {
lines
.into_iter()
.map(|text| BrokenLine {
text,
justify: false,
})
.collect()
};
if is_picture_para(para) {
return Vec::new();
}
if let Some(code) = listing_lines(para) {
return fixed(code.map(str::to_string).collect());
}
if para.contains(TABLE_ROW) {
return fixed(
table_entries(para)
.into_iter()
.filter_map(|entry| match entry {
Entry::Row(cells) => Some(cells.join(" ")),
Entry::Rule(_) => None,
})
.collect(),
);
}
let width_of = |text: &str, _face: Face, size: f64| chain.width_of(text, size);
let mut colours: Vec<Spec> = Vec::new();
let mut faces: Vec<Face> = Vec::new();
let mut sizes: Vec<TypeSize> = vec![TypeSize {
size: layout.size,
leading: layout.leading,
}];
let mut pieces: Vec<crate::linebreak::Piece> = Vec::new();
for word in words_carrying_refs(para) {
if let Some(previous) = pieces.last_mut() {
previous.after = crate::linebreak::After::Glue(chain.width(' ', layout.size));
}
measure_word(
&word,
&mut colours,
&mut faces,
&mut sizes,
&width_of,
&mut pieces,
);
}
let breaks = crate::linebreak::break_paragraph(&pieces, layout.measure, ¶ms);
let mut lines = Vec::with_capacity(breaks.len());
let mut from = 0usize;
for (number, end) in breaks.iter().enumerate() {
let mut text = String::new();
for (offset, piece) in pieces[from..*end].iter().enumerate() {
if offset > 0
&& matches!(
pieces[from + offset - 1].after,
crate::linebreak::After::Glue(_)
)
{
text.push(' ');
}
text.push_str(&piece.text);
}
if matches!(
pieces[*end - 1].after,
crate::linebreak::After::Discretionary(_)
) {
text.push('-');
}
from = *end;
lines.push(BrokenLine {
justify: number + 1 != breaks.len(),
text,
});
}
lines
}
pub fn find_font(name: &str) -> Option<std::path::PathBuf> {
static SEEN: std::sync::OnceLock<
std::sync::Mutex<std::collections::HashMap<String, Option<std::path::PathBuf>>>,
> = std::sync::OnceLock::new();
let seen = SEEN.get_or_init(Default::default);
if let Some(hit) = seen.lock().ok().and_then(|m| m.get(name).cloned()) {
return hit;
}
let found = find_font_uncached(name);
if let Ok(mut m) = seen.lock() {
m.insert(name.to_string(), found.clone());
}
found
}
fn find_font_uncached(name: &str) -> Option<std::path::PathBuf> {
let file = format!("{name}.tfm");
if let Some(p) = crate::kpse::locate(&file) {
return Some(p);
}
for root in [
"/usr/local/texlive",
"/Library/TeX/texbin/../../texmf-dist",
"/usr/share/texmf-dist",
] {
let hit = std::process::Command::new("find")
.args([root, "-name", &file, "-maxdepth", "8"])
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| {
String::from_utf8_lossy(&o.stdout)
.lines()
.next()
.unwrap_or("")
.to_string()
})
.filter(|p| !p.is_empty());
if let Some(p) = hit {
return Some(std::path::PathBuf::from(p));
}
}
None
}
pub struct Loaded {
pub tfm: Tfm,
pub name: String,
}
pub struct FontChain {
pub fonts: Vec<Loaded>,
map: Vec<(char, usize, u8)>,
}
const CM_SYMBOLS: &[(char, &str, u8)] = &[
('→', "cmsy10", 33),
('←', "cmsy10", 32),
('↔', "cmsy10", 36),
('−', "cmsy10", 0),
('×', "cmsy10", 2),
('÷', "cmsy10", 4),
('±', "cmsy10", 6),
('∪', "cmsy10", 91),
('∩', "cmsy10", 92),
('≤', "cmsy10", 20),
('≥', "cmsy10", 21),
('≠', "cmsy10", 54),
('·', "cmsy10", 1),
('∞', "cmsy10", 49),
('§', "cmsy10", 120),
('¶', "cmsy10", 123),
('†', "cmsy10", 121),
('‡', "cmsy10", 122),
];
const APPROXIMATIONS: &[(char, &str)] = &[
('©', "(c)"),
('®', "(R)"),
('™', "(TM)"),
('—', "---"),
('–', "--"),
('…', "..."),
('─', "-"),
('│', "|"),
('├', "|-"),
('└', "`-"),
('┌', ",-"),
('┐', "-."),
('┘', "-'"),
('┬', "-,-"),
('┴', "-'-"),
('┼', "-+-"),
('•', "*"),
('“', "``"),
('”', "''"),
('‘', "`"),
('’', "'"),
('═', "="),
('║', "|"),
('╔', ",="),
('╗', "=."),
('╚', "`="),
('╝', "='"),
('╠', "|="),
('╣', "=|"),
('╦', "=,="),
('╩', "='="),
('╬', "=+="),
('█', "#"),
('▉', "#"),
('▊', "#"),
('▋', "#"),
('▌', "|"),
('▍', "|"),
('▎', "|"),
('▏', "|"),
('▄', "_"),
('▅', "_"),
('▂', "_"),
('●', "*"),
('○', "o"),
('◆', "*"),
('◇', "o"),
('★', "*"),
('☆', "*"),
('▲', "^"),
('▼', "v"),
('►', ">"),
('▶', ">"),
('◀', "<"),
('❯', ">"),
('✓', "+"),
('✔', "+"),
('✗', "x"),
('✘', "x"),
('↕', "|"),
('⟶', "-->"),
('⟵', "<--"),
('⟷', "<->"),
('⟹', "==>"),
('⟸', "<=="),
('↦', "|->"),
('↪', "->"),
('↩', "<-"),
('↗', "/"),
('↘', "\\"),
('↙', "/"),
('↖', "\\"),
('∓', "-+"),
('∘', "o"),
('↯', "!"),
('⚠', "!"),
('ℹ', "i"),
('┤', "-|"),
('╭', ",-"),
('╮', "-."),
('╰', "`-"),
('╯', "-'"),
('▁', "_"),
('▃', "_"),
('▆', "#"),
('▇', "#"),
('░', "."),
('▒', ":"),
('▓', "#"),
('▸', ">"),
('▹', ">"),
('◂', "<"),
('⁰', "0"),
('⁴', "4"),
('⁵', "5"),
('⁶', "6"),
('⁷', "7"),
('⁸', "8"),
('⁹', "9"),
('ⁿ', "n"),
('₀', "0"),
('₁', "1"),
('₂', "2"),
('₃', "3"),
('₄', "4"),
('⌘', "Cmd"),
('⌥', "Opt"),
('⌃', "Ctrl"),
('⇧', "Shift"),
];
pub const SYMBOL_MACROS: &[(&str, char)] = &[
("rightarrow", '→'),
("to", '→'),
("leftarrow", '←'),
("gets", '←'),
("uparrow", '↑'),
("downarrow", '↓'),
("leftrightarrow", '↔'),
("updownarrow", '↕'),
("Rightarrow", '⇒'),
("Leftarrow", '⇐'),
("Leftrightarrow", '⇔'),
("Uparrow", '⇑'),
("Downarrow", '⇓'),
("longrightarrow", '⟶'),
("longleftarrow", '⟵'),
("longleftrightarrow", '⟷'),
("Longrightarrow", '⟹'),
("Longleftarrow", '⟸'),
("mapsto", '↦'),
("hookrightarrow", '↪'),
("hookleftarrow", '↩'),
("nearrow", '↗'),
("searrow", '↘'),
("swarrow", '↙'),
("nwarrow", '↖'),
("alpha", 'α'),
("beta", 'β'),
("gamma", 'γ'),
("delta", 'δ'),
("epsilon", 'ε'),
("varepsilon", 'ε'),
("zeta", 'ζ'),
("eta", 'η'),
("theta", 'θ'),
("vartheta", 'ϑ'),
("iota", 'ι'),
("kappa", 'κ'),
("lambda", 'λ'),
("mu", 'μ'),
("nu", 'ν'),
("xi", 'ξ'),
("pi", 'π'),
("varpi", 'ϖ'),
("rho", 'ρ'),
("sigma", 'σ'),
("varsigma", 'ς'),
("tau", 'τ'),
("upsilon", 'υ'),
("phi", 'φ'),
("varphi", 'ϕ'),
("chi", 'χ'),
("psi", 'ψ'),
("omega", 'ω'),
("Gamma", 'Γ'),
("Delta", 'Δ'),
("Theta", 'Θ'),
("Lambda", 'Λ'),
("Xi", 'Ξ'),
("Pi", 'Π'),
("Sigma", 'Σ'),
("Upsilon", 'Υ'),
("Phi", 'Φ'),
("Psi", 'Ψ'),
("Omega", 'Ω'),
("le", '≤'),
("leq", '≤'),
("ge", '≥'),
("geq", '≥'),
("ne", '≠'),
("neq", '≠'),
("approx", '≈'),
("equiv", '≡'),
("sim", '∼'),
("cong", '≅'),
("propto", '∝'),
("subset", '⊂'),
("supset", '⊃'),
("subseteq", '⊆'),
("supseteq", '⊇'),
("in", '∈'),
("notin", '∉'),
("ni", '∋'),
("perp", '⊥'),
("times", '×'),
("div", '÷'),
("pm", '±'),
("mp", '∓'),
("cdot", '⋅'),
("bullet", '•'),
("ast", '∗'),
("circ", '∘'),
("oplus", '⊕'),
("otimes", '⊗'),
("cup", '∪'),
("cap", '∩'),
("emptyset", '∅'),
("varnothing", '∅'),
("forall", '∀'),
("exists", '∃'),
("neg", '¬'),
("lnot", '¬'),
("land", '∧'),
("wedge", '∧'),
("lor", '∨'),
("vee", '∨'),
("sum", '∑'),
("prod", '∏'),
("int", '∫'),
("partial", '∂'),
("nabla", '∇'),
("infty", '∞'),
("surd", '√'),
("angle", '∠'),
("therefore", '∴'),
("aleph", 'ℵ'),
("prime", '′'),
("diamond", '◆'),
("star", '★'),
("dagger", '†'),
("ddagger", '‡'),
("S", '§'),
("P", '¶'),
("dag", '†'),
("ddag", '‡'),
("copyright", '©'),
("textcopyright", '©'),
("textregistered", '®'),
("texttrademark", '™'),
("pounds", '£'),
("textsterling", '£'),
("texteuro", '€'),
("euro", '€'),
("textdegree", '°'),
("textbullet", '•'),
("textperiodcentered", '·'),
("textemdash", '—'),
("textendash", '–'),
("textellipsis", '…'),
("textmu", 'µ'),
("textpm", '±'),
("texttimes", '×'),
("textdiv", '÷'),
];
pub fn symbol_char(name: &str) -> Option<char> {
SYMBOL_MACROS
.iter()
.find(|(macro_name, _)| *macro_name == name)
.map(|(_, ch)| *ch)
}
pub fn printing_chars(text: &str) -> impl Iterator<Item = char> + '_ {
let mut in_spec = false;
let mut face_code = false;
let mut in_ref = false;
let mut in_picture = false;
let mut in_size_spec = false;
let mut in_image = false;
text.chars().filter(move |&ch| match ch {
REF => {
in_ref = !in_ref;
false
}
_ if in_ref => false,
PICTURE => {
in_picture = !in_picture;
false
}
_ if in_picture => false,
IMAGE => {
in_image = !in_image;
false
}
_ if in_image => false,
'\u{1}' => {
in_spec = true;
false
}
'\u{2}' => {
in_spec = false;
false
}
'\u{3}' => false,
CENTRE | CENTRE_END | VERTICAL_SPACE | JUSTIFY => false,
TABLE_CELL | TABLE_ROW => false,
LIST_INDENT => {
face_code = true;
false
}
FACE_PUSH => {
face_code = true;
false
}
FACE_POP => false,
SIZE_PUSH => {
in_size_spec = !in_size_spec;
false
}
_ if in_size_spec => false,
SIZE_POP => false,
TABLE_MARK | LONGTABLE | TOC => {
face_code = true;
false
}
_ if face_code => {
face_code = false;
false
}
_ => !in_spec,
})
}
impl FontChain {
pub fn load(primary: &str, fallbacks: &[&str]) -> Result<FontChain, String> {
let mut fonts = Vec::new();
let path = find_font(primary).ok_or_else(|| format!("{primary}.tfm not found"))?;
fonts.push(Loaded {
tfm: Tfm::open(&path)?,
name: primary.to_string(),
});
for f in fallbacks {
if let Some(p) = find_font(f) {
if let Ok(tfm) = Tfm::open(&p) {
fonts.push(Loaded {
tfm,
name: (*f).to_string(),
});
}
}
}
let mut map = Vec::new();
for (ch, font_name, slot) in CM_SYMBOLS {
if let Some(i) = fonts.iter().position(|f| f.name == *font_name) {
if fonts[i].tfm.char(*slot).is_some() {
map.push((*ch, i, *slot));
}
}
}
Ok(FontChain { fonts, map })
}
pub fn resolve(&self, ch: char) -> Option<(usize, u8)> {
if ch.is_ascii() && self.fonts[0].tfm.char(ch as u8).is_some() {
return Some((0, ch as u8));
}
self.map
.iter()
.find(|(c, _, _)| *c == ch)
.map(|(_, f, s)| (*f, *s))
}
pub fn approximate(ch: char) -> Option<&'static str> {
APPROXIMATIONS
.iter()
.find(|(c, _)| *c == ch)
.map(|(_, s)| *s)
}
pub fn width(&self, ch: char, size: f64) -> f64 {
if matches!(ch, '\u{1}' | '\u{2}' | '\u{3}') || ch.is_control() {
return 0.0;
}
if ch == ' ' {
return self.fonts[0].tfm.params.space * size;
}
if let Some((f, slot)) = self.resolve(ch) {
return self.fonts[f].tfm.char(slot).map(|m| m.width).unwrap_or(0.0) * size;
}
if let Some(text) = Self::approximate(ch) {
return self.fonts[0].tfm.width_of(text) * size;
}
0.0
}
pub fn width_of(&self, text: &str, size: f64) -> f64 {
self.carried(printing_chars(text))
.iter()
.map(|item| match item {
Carried::Space => self.fonts[0].tfm.params.space * size,
Carried::Set { font, sets } => self.run_width(*font, sets) * size,
})
.sum()
}
fn run_width(&self, font: usize, sets: &[crate::tfm::Set]) -> f64 {
sets.iter()
.map(|set| match set {
crate::tfm::Set::Char(c) => self.fonts[font]
.tfm
.char(*c)
.map(|m| m.width)
.unwrap_or(0.0),
crate::tfm::Set::Kern(by) => *by,
})
.sum()
}
pub fn carried(&self, chars: impl Iterator<Item = char>) -> Vec<Carried> {
let mut out = Vec::new();
let mut run: Vec<u8> = Vec::new();
let mut run_font = 0usize;
for ch in chars {
if ch == ' ' {
self.push_run(&mut out, &mut run, run_font);
out.push(Carried::Space);
continue;
}
if ch.is_control() {
continue;
}
match self.resolve(ch) {
Some((f, slot)) => {
if f != run_font {
self.push_run(&mut out, &mut run, run_font);
run_font = f;
}
run.push(slot);
}
None => {
let Some(text) = Self::approximate(ch) else {
continue;
};
if run_font != 0 {
self.push_run(&mut out, &mut run, run_font);
run_font = 0;
}
run.extend(text.bytes());
}
}
}
self.push_run(&mut out, &mut run, run_font);
out
}
fn push_run(&self, out: &mut Vec<Carried>, run: &mut Vec<u8>, font: usize) {
if run.is_empty() {
return;
}
let sets = self.fonts[font].tfm.set_run(run);
run.clear();
out.push(Carried::Set { font, sets });
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Carried {
Set {
font: usize,
sets: Vec<crate::tfm::Set>,
},
Space,
}
pub const SYMBOL_FONT_NAME: &str = "Symbol";
const SYMBOL_FONT: &[(char, u8, i64)] = &[
('\u{AC}', 216, 713), ('\u{B0}', 176, 400), ('\u{B1}', 177, 549), ('\u{B5}', 109, 576), ('\u{D7}', 180, 549), ('\u{F7}', 184, 549), ('\u{192}', 166, 500), ('\u{391}', 65, 722), ('\u{392}', 66, 667), ('\u{393}', 71, 603), ('\u{395}', 69, 611), ('\u{396}', 90, 611), ('\u{397}', 72, 722), ('\u{398}', 81, 741), ('\u{399}', 73, 333), ('\u{39A}', 75, 722), ('\u{39B}', 76, 686), ('\u{39C}', 77, 889), ('\u{39D}', 78, 722), ('\u{39E}', 88, 645), ('\u{39F}', 79, 722), ('\u{3A0}', 80, 768), ('\u{3A1}', 82, 556), ('\u{3A3}', 83, 592), ('\u{3A4}', 84, 611), ('\u{3A5}', 85, 690), ('\u{3A6}', 70, 763), ('\u{3A7}', 67, 722), ('\u{3A8}', 89, 795), ('\u{3B1}', 97, 631), ('\u{3B2}', 98, 549), ('\u{3B3}', 103, 411), ('\u{3B4}', 100, 494), ('\u{3B5}', 101, 439), ('\u{3B6}', 122, 494), ('\u{3B7}', 104, 603), ('\u{3B8}', 113, 521), ('\u{3B9}', 105, 329), ('\u{3BA}', 107, 549), ('\u{3BB}', 108, 549), ('\u{3BD}', 110, 521), ('\u{3BE}', 120, 493), ('\u{3BF}', 111, 549), ('\u{3C0}', 112, 549), ('\u{3C1}', 114, 549), ('\u{3C2}', 86, 439), ('\u{3C3}', 115, 603), ('\u{3C4}', 116, 439), ('\u{3C5}', 117, 576), ('\u{3C6}', 102, 521), ('\u{3C7}', 99, 549), ('\u{3C8}', 121, 686), ('\u{3C9}', 119, 686), ('\u{3D1}', 74, 631), ('\u{3D2}', 161, 620), ('\u{3D5}', 106, 603), ('\u{3D6}', 118, 713), ('\u{2022}', 183, 460), ('\u{2026}', 188, 1000), ('\u{2032}', 162, 247), ('\u{2033}', 178, 411), ('\u{2044}', 164, 167), ('\u{2111}', 193, 686), ('\u{2118}', 195, 987), ('\u{211C}', 194, 795), ('\u{2126}', 87, 768), ('\u{2135}', 192, 823), ('\u{2190}', 172, 987), ('\u{2191}', 173, 603), ('\u{2192}', 174, 987), ('\u{2193}', 175, 603), ('\u{2194}', 171, 1042), ('\u{21B5}', 191, 658), ('\u{21D0}', 220, 987), ('\u{21D1}', 221, 603), ('\u{21D2}', 222, 987), ('\u{21D3}', 223, 603), ('\u{21D4}', 219, 1042), ('\u{2200}', 34, 713), ('\u{2202}', 182, 494), ('\u{2203}', 36, 549), ('\u{2205}', 198, 823), ('\u{2206}', 68, 612), ('\u{2207}', 209, 713), ('\u{2208}', 206, 713), ('\u{2209}', 207, 713), ('\u{220B}', 39, 439), ('\u{220F}', 213, 823), ('\u{2211}', 229, 713), ('\u{2212}', 45, 549), ('\u{2217}', 42, 500), ('\u{221A}', 214, 549), ('\u{221D}', 181, 713), ('\u{221E}', 165, 713), ('\u{2220}', 208, 768), ('\u{2227}', 217, 603), ('\u{2228}', 218, 603), ('\u{2229}', 199, 768), ('\u{222A}', 200, 768), ('\u{222B}', 242, 274), ('\u{2234}', 92, 863), ('\u{223C}', 126, 549), ('\u{2245}', 64, 549), ('\u{2248}', 187, 549), ('\u{2260}', 185, 549), ('\u{2261}', 186, 549), ('\u{2264}', 163, 549), ('\u{2265}', 179, 549), ('\u{2282}', 204, 713), ('\u{2283}', 201, 713), ('\u{2284}', 203, 713), ('\u{2286}', 205, 713), ('\u{2287}', 202, 713), ('\u{2295}', 197, 768), ('\u{2297}', 196, 768), ('\u{22A5}', 94, 658), ('\u{22C5}', 215, 250), ('\u{2320}', 243, 686), ('\u{2321}', 245, 686), ('\u{2329}', 225, 329), ('\u{232A}', 241, 329), ('\u{25CA}', 224, 494), ('\u{2660}', 170, 753), ('\u{2663}', 167, 753), ('\u{2665}', 169, 753), ('\u{2666}', 168, 753), ('\u{3BC}', 109, 576), ('\u{394}', 68, 612), ('\u{3A9}', 87, 768), ];
const WINANSI_PUNCTUATION: &[(char, u8)] = &[
('€', 0x80),
('‚', 0x82),
('ƒ', 0x83),
('„', 0x84),
('…', 0x85),
('†', 0x86),
('‡', 0x87),
('ˆ', 0x88),
('‰', 0x89),
('Š', 0x8A),
('‹', 0x8B),
('Œ', 0x8C),
('Ž', 0x8E),
('‘', 0x91),
('’', 0x92),
('“', 0x93),
('”', 0x94),
('•', 0x95),
('–', 0x96),
('—', 0x97),
('˜', 0x98),
('™', 0x99),
('š', 0x9A),
('›', 0x9B),
('œ', 0x9C),
('ž', 0x9E),
('Ÿ', 0x9F),
];
pub fn winansi_code(ch: char) -> Option<u8> {
match ch {
' '..='~' => Some(ch as u8),
'\u{A0}'..='\u{FF}' => Some(ch as u8),
_ => WINANSI_PUNCTUATION
.iter()
.find(|(c, _)| *c == ch)
.map(|(_, code)| *code),
}
}
pub fn winansi_unicode(code: u8) -> Option<char> {
match code {
0x20..=0x7E => Some(code as char),
0xA0..=0xFF => Some(code as char),
_ => WINANSI_PUNCTUATION
.iter()
.find(|(_, c)| *c == code)
.map(|(ch, _)| *ch),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Glyph {
Own(u8),
Fallback(u8),
Outside(usize, u16),
StandIn(&'static str),
}
pub fn glyph_for(
ch: char,
covers: &dyn Fn(char) -> bool,
outside: &dyn Fn(char) -> Option<(usize, u16)>,
) -> Option<Glyph> {
if let Some(code) = winansi_code(ch) {
if ch.is_ascii() || covers(ch) {
return Some(Glyph::Own(code));
}
}
if let Some((_, code, _)) = SYMBOL_FONT.iter().find(|(c, _, _)| *c == ch) {
return Some(Glyph::Fallback(*code));
}
if let Some((face, glyph)) = outside(ch) {
return Some(Glyph::Outside(face, glyph));
}
FontChain::approximate(ch).map(Glyph::StandIn)
}
#[derive(Default)]
pub struct Fallbacks {
faces: Vec<Outside>,
carried: Vec<Option<std::sync::Arc<crate::pdf::Font>>>,
}
struct Outside {
name: String,
bytes: Vec<u8>,
cmap: std::collections::BTreeMap<u32, u16>,
advances: Vec<u16>,
upem: f64,
bbox: [i64; 4],
ascent: i64,
descent: i64,
used: std::collections::BTreeMap<u16, char>,
}
impl Fallbacks {
pub fn load(families: &[String]) -> Fallbacks {
let mut faces = Vec::new();
for family in families {
let Some(path) = find_fallback_family(family) else {
continue;
};
let Some(face) = Outside::open(&path) else {
continue;
};
faces.push(face);
}
Fallbacks {
faces,
carried: Vec::new(),
}
}
pub fn glyph(&self, ch: char) -> Option<(usize, u16)> {
self.faces.iter().enumerate().find_map(|(at, face)| {
match face.cmap.get(&(ch as u32)).copied() {
Some(gid) if gid != 0 => Some((at, gid)),
_ => None,
}
})
}
pub fn reserve(&mut self, ch: char) {
if let Some((at, gid)) = self.glyph(ch) {
self.faces[at].used.insert(gid, ch);
}
}
pub fn width(&self, face: usize, glyph: u16, size: f64) -> f64 {
self.faces
.get(face)
.map(|f| f.width(glyph) as f64 / 1000.0 * size)
.unwrap_or(0.0)
}
pub fn settle(&mut self) {
self.carried = self
.faces
.iter()
.map(|face| face.font().map(std::sync::Arc::new))
.collect();
}
pub fn font(&self, face: usize) -> Option<&std::sync::Arc<crate::pdf::Font>> {
self.carried.get(face)?.as_ref()
}
pub fn is_empty(&self) -> bool {
self.faces.is_empty()
}
}
impl Outside {
fn open(path: &std::path::Path) -> Option<Outside> {
let bytes = std::fs::read(path).ok()?;
let sfnt = crate::sfnt::Sfnt::parse(bytes.clone()).ok()?;
if sfnt.is_cff() {
return None;
}
let head = sfnt.head().ok()?;
let hhea = sfnt.hhea().ok()?;
let upem = f64::from(head.units_per_em.max(1));
let scale = |v: f64| (v * 1000.0 / upem).round() as i64;
Some(Outside {
name: path.file_stem()?.to_string_lossy().replace(' ', ""),
cmap: sfnt.cmap().ok()?,
advances: sfnt.advance_widths().ok()?,
upem,
bbox: [
scale(f64::from(head.x_min)),
scale(f64::from(head.y_min)),
scale(f64::from(head.x_max)),
scale(f64::from(head.y_max)),
],
ascent: scale(f64::from(hhea.ascender)),
descent: scale(f64::from(hhea.descender)),
bytes,
used: std::collections::BTreeMap::new(),
})
}
fn width(&self, glyph: u16) -> i64 {
let adv = self
.advances
.get(glyph as usize)
.or_else(|| self.advances.last())
.copied()
.unwrap_or(0);
(f64::from(adv) * 1000.0 / self.upem).round() as i64
}
fn font(&self) -> Option<crate::pdf::Font> {
if self.used.is_empty() {
return None;
}
let sfnt = crate::sfnt::Sfnt::parse(self.bytes.clone()).ok()?;
let bytes = sfnt.subset(&self.used.keys().copied().collect()).ok()?;
Some(crate::pdf::Font::Glyphs {
name: self.name.clone(),
bytes,
glyphs: self
.used
.iter()
.map(|(gid, ch)| (*gid, *ch, self.width(*gid)))
.collect(),
bbox: self.bbox,
ascent: self.ascent,
descent: self.descent,
})
}
}
pub fn find_fallback_family(family: &str) -> Option<std::path::PathBuf> {
let wanted = normalise(family);
if wanted.len() < 4 {
return None;
}
let close = |path: &std::path::Path| {
let stem = path
.file_stem()
.map(|s| normalise(&s.to_string_lossy()))
.unwrap_or_default();
stem.len() >= 4 && (stem.starts_with(&wanted) || wanted.starts_with(&stem))
};
if let Ok(out) = std::process::Command::new("fc-match")
.args(["-f", "%{file}", family])
.output()
{
if out.status.success() {
let answer = String::from_utf8_lossy(&out.stdout).trim().to_string();
let path = std::path::Path::new(&answer);
if path.exists() && close(path) {
return Some(path.to_path_buf());
}
}
}
for dir in [
"/System/Library/Fonts",
"/System/Library/Fonts/Supplemental",
"/Library/Fonts",
"/usr/share/fonts",
"/usr/local/share/fonts",
] {
let Ok(entries) = std::fs::read_dir(dir) else {
continue;
};
for entry in entries.flatten() {
let path = entry.path();
let ext = path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_ascii_lowercase();
if ext != "ttf" && ext != "otf" {
continue;
}
if close(&path) {
return Some(path);
}
}
}
None
}
pub fn symbol_unicode(code: u8) -> Option<char> {
SYMBOL_FONT
.iter()
.find(|(_, c, _)| *c == code)
.map(|(ch, _, _)| *ch)
}
fn symbol_width(code: u8) -> i64 {
SYMBOL_FONT
.iter()
.find(|(_, c, _)| *c == code)
.map(|(_, _, w)| *w)
.unwrap_or(500)
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FontFile {
pub path: Option<String>,
pub upright: Option<String>,
pub bold: Option<String>,
pub italic: Option<String>,
pub scale: Option<String>,
pub extension: Option<String>,
}
impl FontFile {
pub fn parse(options: &str) -> FontFile {
let mut out = FontFile::default();
for (key, value) in top_level_pairs(options) {
let value = value.trim().trim_matches(|c| c == '{' || c == '}');
match key.trim() {
"Path" => out.path = Some(value.to_string()),
"UprightFont" => out.upright = Some(value.to_string()),
"BoldFont" => out.bold = Some(value.to_string()),
"ItalicFont" => out.italic = Some(value.to_string()),
"Extension" => out.extension = Some(value.to_string()),
"Scale" => out.scale = Some(value.trim().to_string()),
_ => {}
}
}
out
}
pub fn absorb_filename(&mut self, name: &str) {
let Some((stem, extension)) = split_font_filename(name.trim()) else {
return;
};
if self.upright.is_none() {
self.upright = Some(stem.to_string());
}
if self.extension.is_none() {
self.extension = Some(extension.to_string());
}
}
pub fn resolve(&self, near: Option<&std::path::Path>) -> Option<std::path::PathBuf> {
self.resolve_face(Face::MAIN, near)
}
pub fn resolve_face(
&self,
face: Face,
near: Option<&std::path::Path>,
) -> Option<std::path::PathBuf> {
let named = match (face.bold, face.italic) {
(_, true) => self.italic.as_deref().or(self.bold.as_deref()),
(true, false) => self.bold.as_deref(),
(false, false) => self.upright.as_deref(),
}
.or(self.upright.as_deref())?;
let extension = self.extension.as_deref().unwrap_or(".ttf");
let file = format!("{named}{extension}");
if let Some(path) = &self.path {
let full = std::path::Path::new(path).join(&file);
if full.is_file() {
return Some(full);
}
if let (Some(dir), Some(near)) = (last_component(path), near) {
let beside = near.join(dir).join(&file);
if beside.is_file() {
return Some(beside);
}
}
}
let beside = near?.join(&file);
beside.is_file().then_some(beside)
}
}
const FONT_FILE_EXTENSIONS: [&str; 4] = ["ttf", "otf", "ttc", "otc"];
pub fn split_font_filename(name: &str) -> Option<(&str, &str)> {
let dot = name.rfind('.')?;
let (stem, extension) = name.split_at(dot);
if stem.is_empty() {
return None;
}
FONT_FILE_EXTENSIONS
.iter()
.any(|known| extension[1..].eq_ignore_ascii_case(known))
.then_some((stem, extension))
}
pub fn font_family_name(name: &str) -> &str {
split_font_filename(name).map_or(name, |(stem, _)| stem)
}
fn last_component(path: &str) -> Option<&str> {
path.trim_end_matches('/')
.rsplit('/')
.next()
.filter(|c| !c.is_empty())
}
fn top_level_pairs(options: &str) -> Vec<(&str, &str)> {
let mut out = Vec::new();
let mut depth = 0usize;
let mut start = 0usize;
let bytes = options.as_bytes();
for (i, &b) in bytes.iter().enumerate() {
match b {
b'{' | b'[' => depth += 1,
b'}' | b']' => depth = depth.saturating_sub(1),
b',' if depth == 0 => {
if let Some(pair) = split_pair(&options[start..i]) {
out.push(pair);
}
start = i + 1;
}
_ => {}
}
}
if let Some(pair) = split_pair(&options[start..]) {
out.push(pair);
}
out
}
fn split_pair(text: &str) -> Option<(&str, &str)> {
let at = text.find('=')?;
Some((&text[..at], &text[at + 1..]))
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Families {
pub main: Option<String>,
pub main_file: FontFile,
pub sans: Option<String>,
pub sans_file: FontFile,
pub mono: Option<String>,
pub mono_file: FontFile,
pub fallbacks: Vec<String>,
}
pub fn fallback_chain(chunk: &str) -> Vec<String> {
let Some(rest) = chunk.split_once("add_fallback").map(|(_, r)| r) else {
return Vec::new();
};
let Some(table) = rest.split_once('{').and_then(|(_, r)| r.split_once('}')) else {
return Vec::new();
};
table
.0
.split(',')
.filter_map(|item| {
let quoted = item.trim().trim_matches('"');
let name = quoted.split(':').next().unwrap_or("").trim();
match name.is_empty() {
true => None,
false => Some(name.to_string()),
}
})
.collect()
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Family {
#[default]
Roman,
Sans,
Mono,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Face {
pub family: Family,
pub bold: bool,
pub italic: bool,
}
impl Face {
pub const MAIN: Face = Face {
family: Family::Roman,
bold: false,
italic: false,
};
pub const ALL: [Face; 12] = {
let mut all = [Face::MAIN; 12];
let mut i = 0;
while i < 12 {
all[i] = Face {
family: match i / 4 {
0 => Family::Roman,
1 => Family::Sans,
_ => Family::Mono,
},
bold: i % 4 >= 2,
italic: i % 2 == 1,
};
i += 1;
}
all
};
pub fn code(self) -> char {
const CODES: [char; 12] = [
'r', 'i', 'b', 'B', 's', 'j', 'S', 'J', 'm', 'k', 'M', 'K', ];
CODES[self.index()]
}
pub fn from_code(code: char, current: Face) -> Face {
match code {
'r' => Face {
family: Family::Roman,
..current
},
's' => Face {
family: Family::Sans,
..current
},
'm' => Face {
family: Family::Mono,
..current
},
'b' => Face {
bold: true,
..current
},
'i' => Face {
italic: true,
..current
},
'n' => Face::MAIN,
'B' => Face {
family: Family::Roman,
bold: true,
italic: true,
},
'j' => Face {
family: Family::Sans,
bold: false,
italic: true,
},
'S' => Face {
family: Family::Sans,
bold: true,
italic: false,
},
'J' => Face {
family: Family::Sans,
bold: true,
italic: true,
},
'k' => Face {
family: Family::Mono,
bold: false,
italic: true,
},
'M' => Face {
family: Family::Mono,
bold: true,
italic: false,
},
'K' => Face {
family: Family::Mono,
bold: true,
italic: true,
},
_ => current,
}
}
fn index(self) -> usize {
let family = match self.family {
Family::Roman => 0,
Family::Sans => 1,
Family::Mono => 2,
};
family * 4 + usize::from(self.bold) * 2 + usize::from(self.italic)
}
}
pub const FACE_PUSH: char = '\u{11}';
pub fn is_marker(c: char) -> bool {
[
FACE_PUSH,
FACE_POP,
SIZE_PUSH,
SIZE_POP,
TOC,
REF,
LIST_INDENT,
PAGE_BREAK,
PICTURE,
IMAGE,
LISTING_BREAK,
TABLE_CELL,
TABLE_ROW,
TABLE_MARK,
LONGTABLE,
CENTRE,
CENTRE_END,
JUSTIFY,
VERTICAL_SPACE,
crate::math::RUN,
]
.contains(&c)
}
pub const FACE_POP: char = '\u{12}';
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TypeSize {
pub size: f64,
pub leading: f64,
}
impl Default for TypeSize {
fn default() -> Self {
TypeSize {
size: 10.0,
leading: 12.0 * BP_PER_PT,
}
}
}
pub fn current_size(sizes: &[TypeSize]) -> TypeSize {
sizes.last().copied().unwrap_or_default()
}
const SIZE_STEPS_10: &[(&str, f64, f64)] = &[
("tiny", 5.0, 6.0),
("scriptsize", 7.0, 8.0),
("footnotesize", 8.0, 9.5),
("small", 9.0, 11.0),
("normalsize", 10.0, 12.0),
("large", 12.0, 14.0),
("Large", 14.4, 18.0),
("LARGE", 17.28, 22.0),
("huge", 20.74, 25.0),
("Huge", 24.88, 30.0),
];
const SIZE_STEPS_11: &[(&str, f64, f64)] = &[
("tiny", 6.0, 7.0),
("scriptsize", 8.0, 9.5),
("footnotesize", 9.0, 11.0),
("small", 10.0, 12.0),
("normalsize", 10.95, 13.6),
("large", 12.0, 14.0),
("Large", 14.4, 18.0),
("LARGE", 17.28, 22.0),
("huge", 20.74, 25.0),
("Huge", 24.88, 30.0),
];
const SIZE_STEPS_12: &[(&str, f64, f64)] = &[
("tiny", 6.0, 7.0),
("scriptsize", 8.0, 9.5),
("footnotesize", 10.0, 12.0),
("small", 10.95, 13.6),
("normalsize", 12.0, 14.5),
("large", 14.4, 18.0),
("Large", 17.28, 22.0),
("LARGE", 20.74, 25.0),
("huge", 24.88, 30.0),
("Huge", 24.88, 30.0),
];
pub fn size_step(name: &str, base: f64) -> Option<TypeSize> {
let table = match (base / BP_PER_PT).round() as i64 {
11 => SIZE_STEPS_11,
12 => SIZE_STEPS_12,
_ => SIZE_STEPS_10,
};
table
.iter()
.find(|(step, _, _)| *step == name)
.map(|(_, size, leading)| TypeSize {
size: size * BP_PER_PT,
leading: leading * BP_PER_PT,
})
}
pub const SIZE_PUSH: char = '\u{19}';
pub const SIZE_POP: char = '\u{1a}';
pub fn size_spec(spec: &str) -> Option<(f64, f64)> {
let (size, leading) = spec.split_once(';')?;
Some((size.trim().parse().ok()?, leading.trim().parse().ok()?))
}
pub fn size_span(size: f64, leading: f64, body: &str) -> String {
format!("{SIZE_PUSH}{size};{leading}{SIZE_PUSH}{body}{SIZE_POP}")
}
pub const BREAK_PARAMS: char = '\u{1c}';
pub fn break_params_span(p: &crate::linebreak::Params) -> String {
format!(
"{BREAK_PARAMS}{},{},{},{},{},{},{},{}{BREAK_PARAMS}",
p.pretolerance,
p.tolerance,
p.line_penalty,
p.hyphen_penalty,
p.ex_hyphen_penalty,
p.adj_demerits,
p.double_hyphen_demerits,
p.final_hyphen_demerits
)
}
pub fn paragraph_params<'a>(
para: &'a str,
base: &crate::linebreak::Params,
) -> (crate::linebreak::Params, std::borrow::Cow<'a, str>) {
if !para.contains(BREAK_PARAMS) {
return (*base, std::borrow::Cow::Borrowed(para));
}
let mut text = String::with_capacity(para.len());
let mut params = *base;
let mut parts = para.split(BREAK_PARAMS);
text.push_str(parts.next().unwrap_or(""));
while let Some(spec) = parts.next() {
if let Some(read) = break_params_spec(spec) {
params = read;
}
text.push_str(parts.next().unwrap_or(""));
}
(params, std::borrow::Cow::Owned(text))
}
fn break_params_spec(spec: &str) -> Option<crate::linebreak::Params> {
let v: Vec<f64> = spec
.split(',')
.map(|n| n.trim().parse().ok())
.collect::<Option<_>>()?;
let [pretolerance, tolerance, line_penalty, hyphen_penalty, ex_hyphen_penalty, adj_demerits, double_hyphen_demerits, final_hyphen_demerits] =
v[..]
else {
return None;
};
Some(crate::linebreak::Params {
pretolerance,
tolerance,
line_penalty,
hyphen_penalty,
ex_hyphen_penalty,
adj_demerits,
double_hyphen_demerits,
final_hyphen_demerits,
})
}
pub fn base14_for(family: &str) -> &'static str {
let f = family.to_ascii_lowercase();
let mono = [
"mono",
"courier",
"consolas",
"menlo",
"inconsolata",
"sharetechmono",
"jetbrains",
"fira code",
"source code",
];
if mono.iter().any(|m| f.contains(m)) {
return "Courier";
}
let serif = [
"times",
"serif",
"georgia",
"garamond",
"minion",
"palatino",
"book",
"charter",
"libertine",
"stix",
];
if serif.iter().any(|m| f.contains(m)) {
return "Times-Roman";
}
"Helvetica"
}
pub fn base14_face(base: &str, face: Face) -> String {
let stem = match face.family {
Family::Mono => "Courier",
Family::Sans => "Helvetica",
Family::Roman => base,
};
let (bold, italic, both) = match stem {
"Times-Roman" => ("Times-Bold", "Times-Italic", "Times-BoldItalic"),
_ => ("-Bold", "-Oblique", "-BoldOblique"),
};
match (face.bold, face.italic) {
(false, false) => stem.to_string(),
(true, false) if stem == "Times-Roman" => bold.to_string(),
(false, true) if stem == "Times-Roman" => italic.to_string(),
(true, true) if stem == "Times-Roman" => both.to_string(),
(true, false) => format!("{stem}{bold}"),
(false, true) => format!("{stem}{italic}"),
(true, true) => format!("{stem}{both}"),
}
}
pub(crate) struct Faces {
fonts: Vec<std::sync::Arc<crate::pdf::Font>>,
main_shared: std::sync::Arc<crate::pdf::Font>,
scales: Vec<f64>,
embedded_widths: Vec<Option<Vec<i64>>>,
coverage: Vec<Option<std::collections::BTreeSet<u32>>>,
fallbacks: Fallbacks,
metrics: Option<Tfm>,
}
fn covered(coverage: &[Option<std::collections::BTreeSet<u32>>], ch: char, face: Face) -> bool {
match &coverage[face.index()] {
Some(has) => has.contains(&(ch as u32)),
None => true,
}
}
impl Faces {
pub(crate) fn new(families: &Families, near: Option<&std::path::Path>, text: &str) -> Faces {
use crate::pdf::Font;
let requested = families.main.as_deref();
let embedded = families
.main_file
.resolve(near)
.and_then(|file| embed_file(&file))
.or_else(|| requested.and_then(embed_family));
let main = embedded.clone().unwrap_or_else(|| match requested {
Some(family) => Font::Base14(base14_for(family).to_string()),
None => crate::pdf::Font::computer_modern()
.unwrap_or_else(|| Font::Base14("Helvetica".to_string())),
});
let main_shared = std::sync::Arc::new(main.clone());
let base = match &main {
Font::Base14(name) => name.clone(),
_ => requested.map(base14_for).unwrap_or("Helvetica").to_string(),
};
let mono = families
.mono_file
.resolve(near)
.and_then(|file| embed_file(&file))
.or_else(|| families.mono.as_deref().and_then(embed_family))
.or_else(|| {
families
.mono
.as_deref()
.map(|f| Font::Base14(base14_for(f).to_string()))
})
.unwrap_or_else(|| main.clone());
let file_for = |family: Family| match family {
Family::Roman => &families.main_file,
Family::Sans => &families.sans_file,
Family::Mono => &families.mono_file,
};
let face_file = |face: Face| {
file_for(face.family)
.resolve_face(face, near)
.and_then(|file| embed_file(&file))
};
let sans = families
.sans_file
.resolve(near)
.and_then(|file| embed_file(&file))
.or_else(|| families.sans.as_deref().and_then(embed_family))
.or_else(|| {
families
.sans
.as_deref()
.map(|f| Font::Base14(base14_for(f).to_string()))
})
.unwrap_or_else(|| main.clone());
let upright = |family: Family| match family {
Family::Roman => main.clone(),
Family::Sans => sans.clone(),
Family::Mono => mono.clone(),
};
let fonts: Vec<std::sync::Arc<Font>> = Face::ALL
.iter()
.map(|face| {
if !face.bold && !face.italic {
return upright(face.family);
}
face_file(*face).unwrap_or_else(|| Font::Base14(base14_face(&base, *face)))
})
.map(std::sync::Arc::new)
.collect();
let main_path = families.main_file.resolve(near);
let scale_for = |file: &FontFile| {
face_scale(
file.scale.as_deref(),
file.resolve(near).as_deref(),
main_path.as_deref(),
)
};
let scales: Vec<f64> = Face::ALL
.iter()
.map(|face| scale_for(file_for(face.family)))
.collect();
let embedded_widths: Vec<Option<Vec<i64>>> = fonts
.iter()
.map(|f| match &**f {
Font::TrueType { widths, .. } => Some(widths.clone()),
_ => None,
})
.collect();
let coverage: Vec<Option<std::collections::BTreeSet<u32>>> =
fonts.iter().map(|f| face_coverage(f)).collect();
let covers = |ch: char, face: Face| covered(&coverage, ch, face);
let mut fallbacks = Fallbacks::load(&families.fallbacks);
if !fallbacks.is_empty() {
let distinct: std::collections::BTreeSet<char> =
printing_chars(text).filter(|c| !c.is_ascii()).collect();
for ch in distinct {
let drawn_already = SYMBOL_FONT.iter().any(|(c, _, _)| *c == ch)
|| Face::ALL
.iter()
.all(|face| winansi_code(ch).is_some() && covers(ch, *face));
if !drawn_already {
fallbacks.reserve(ch);
}
}
fallbacks.settle();
}
Faces {
fonts,
main_shared,
scales,
embedded_widths,
coverage,
fallbacks,
metrics: find_font("cmr10").and_then(|p| Tfm::open(&p).ok()),
}
}
fn covers(&self, ch: char, face: Face) -> bool {
covered(&self.coverage, ch, face)
}
fn outside(&self, ch: char) -> Option<(usize, u16)> {
self.fallbacks.glyph(ch)
}
fn own_width(&self, codes: &str, source: &str, face: Face, size: f64) -> f64 {
let size = size * self.scales[face.index()];
if let Some(Some(w)) = self.embedded_widths.get(face.index()) {
return codes
.chars()
.map(|c| {
let at = (c as usize).saturating_sub(32);
let mille = w.get(at).copied().unwrap_or(500);
mille as f64 / 1000.0 * size
})
.sum();
}
match &self.metrics {
Some(f) => f.width_of(source) * size,
None => source.chars().count() as f64 * size * 0.5,
}
}
fn piece_width(&self, piece: &Piece, face: Face, size: f64) -> f64 {
match piece.from {
Source::Symbol => piece
.codes
.chars()
.map(|c| symbol_width(c as u8) as f64 / 1000.0 * size)
.sum(),
Source::Outside(at) => glyph_ids(&piece.codes)
.map(|glyph| self.fallbacks.width(at, glyph, size))
.sum(),
Source::Face => self.own_width(&piece.codes, &piece.source, face, size),
}
}
pub(crate) fn width_of(&self, word: &str, face: Face, size: f64) -> f64 {
if let Some(w) = crate::math::run_width(word) {
return w;
}
if word.is_ascii() {
let plain: String = printing_chars(word).collect();
return self.own_width(&plain, &plain, face, size);
}
drawn(word, &|c| self.covers(c, face), &|c| self.outside(c))
.iter()
.map(|piece| self.piece_width(piece, face, size))
.sum()
}
}
pub fn to_pdf(
text: &str,
families: &Families,
layout: &Layout,
page_colour: Option<crate::colour::Rgb>,
near: Option<&std::path::Path>,
) -> crate::Pdf {
to_pdf_ahead(text, families, layout, page_colour, near, None)
}
pub fn to_pdf_ahead(
text: &str,
families: &Families,
layout: &Layout,
page_colour: Option<crate::colour::Rgb>,
near: Option<&std::path::Path>,
ahead: Option<Ahead>,
) -> crate::Pdf {
use crate::pdf::{document_from, Font, Page, Set};
let (faces, ahead) = match ahead.filter(|a| a.measured_as(families, layout)) {
Some(Ahead { faces, broken, .. }) => (faces, Some(broken)),
None => (Faces::new(families, near, text), None),
};
let Faces {
fonts,
main_shared,
scales,
fallbacks,
..
} = &faces;
let covers = |ch: char, face: Face| faces.covers(ch, face);
let outside = |ch: char| faces.outside(ch);
let piece_width = |piece: &Piece, face: Face, size: f64| faces.piece_width(piece, face, size);
let width_of = |word: &str, face: Face, size: f64| faces.width_of(word, face, size);
let numbered = refs_numbered(text);
let contented = contents_set(&numbered, layout, &width_of);
let text = refs_paged(&contented, layout, &width_of);
let broken = break_lines_from(&text, layout, &width_of, ahead);
let images = images_read(&broken, near);
let remeasure = |line: String| -> String {
let picture_metrics = FaceMetrics {
measure: &width_of,
size: layout.size,
};
image_remeasured(picture_remeasured(line, &picture_metrics), layout, &images)
};
let blocks = broken
.iter()
.filter(|line| {
let inner = line.trim_start();
let inner = inner.strip_prefix(CENTRE).unwrap_or(inner);
inner.starts_with([PICTURE, IMAGE])
})
.count();
let lines: Vec<String> = match blocks >= PARALLEL_BLOCKS {
true => {
use rayon::prelude::*;
broken.into_par_iter().map(remeasure).collect()
}
false => broken.into_iter().map(remeasure).collect(),
};
let colours: Vec<Spec> = vec![(
DEFAULT_COLOUR.0.to_string(),
DEFAULT_COLOUR.1.to_string(),
DEFAULT_COLOUR.2.to_string(),
)];
let faces: Vec<Face> = vec![Face::MAIN];
let sizes: Vec<TypeSize> = vec![TypeSize {
size: layout.size,
leading: layout.leading,
}];
let chunks = paginate(&lines, layout);
let starts = page_starts(&chunks, colours, faces, sizes);
let draw_page = |(chunk, start): (&Vec<&str>, &PageStart)| -> Page {
let PageStart {
mut colours,
mut faces,
mut sizes,
folio,
} = start.clone();
let picture_metrics = FaceMetrics {
measure: &width_of,
size: layout.size,
};
let mut page = Page::letter();
if let Some((r, g, b)) = page_colour {
page.content.push_str(&format!(
"{r} {g} {b} rg\n0 0 {} {} re\nf\n0 0 0 rg\n",
page.width, page.height
));
}
let mut y = layout.height + layout.margin - layout.leading;
for &line in chunk {
let line = without_longtable(line);
if is_space_line(line) {
y -= line_height(line, leading_on(line, &mut sizes.clone()));
continue;
}
if let Some((width, height, path)) = image_parts(line) {
let (Ok(w), Ok(h)) = (width.parse::<f64>(), height.parse::<f64>()) else {
continue;
};
let left = match is_centred(line) {
true => layout.margin + (layout.measure - w).max(0.0) / 2.0,
false => layout.margin,
};
if let Some(image) = images.get(&path).and_then(|read| read.image.as_ref()) {
page.image(std::sync::Arc::clone(image), left, y - h, w, h);
}
y -= h + leading_on(line, &mut sizes.clone());
continue;
}
if let Some((height, options, body)) = picture_parts(line) {
let picture = crate::tikz::parse_document(
&options,
&body,
crate::colour::Colours::predefined(),
&picture_metrics,
);
let (min_x, min_y, max_x, _) = picture.bounds();
let left = match is_centred(line) {
true => layout.margin + (layout.measure - (max_x - min_x)).max(0.0) / 2.0,
false => layout.margin,
};
let bottom = y - height;
crate::tikz::draw_on(
&picture,
&mut page,
left - min_x,
bottom - min_y,
std::sync::Arc::clone(main_shared),
);
y -= height + layout.leading;
continue;
}
if let Some(rest) = line.strip_prefix(TABLE_MARK) {
let mut rest = rest.chars();
let kind = rest.next().unwrap_or(RULE_MID);
let span = width_of(
rest.as_str(),
current_face(&faces),
current_size(&sizes).size,
);
let em = match kind == RULE_MID {
true => 0.05,
false => 0.08,
};
let weight = em * layout.size;
let (r, g, b) = colours.last().cloned().unwrap_or_else(|| {
(
DEFAULT_COLOUR.0.to_string(),
DEFAULT_COLOUR.1.to_string(),
DEFAULT_COLOUR.2.to_string(),
)
});
page.content.push_str(&format!("{r} {g} {b} rg\n"));
page.rule(layout.margin, y + layout.size * 0.25, span, weight);
y -= layout.leading;
continue;
}
let (centred, line) = match line.strip_prefix(CENTRE) {
Some(rest) => (true, rest),
None => (false, line),
};
let (justified, line) = match line.strip_prefix(JUSTIFY) {
Some(rest) => (true, rest),
None => (false, line),
};
let (depth, line) = strip_indent(line);
let indent = list_indent(depth, layout);
let width: f64 = match centred {
true => {
let (mut c, mut f, mut s) = (colours.clone(), faces.clone(), sizes.clone());
styled_runs(line, &mut c, &mut f, &mut s)
.iter()
.map(|(plain, _, face, ts)| width_of(plain, *face, ts.size))
.sum()
}
false => 0.0,
};
let extra: f64 = match justified {
true => {
let (mut c, mut f, mut s) = (colours.clone(), faces.clone(), sizes.clone());
let runs = styled_runs(line, &mut c, &mut f, &mut s);
let natural: f64 = runs
.iter()
.map(|(plain, _, face, ts)| width_of(plain, *face, ts.size))
.sum();
let spaces: usize = runs
.iter()
.map(|(plain, _, _, _)| plain.chars().filter(|c| *c == ' ').count())
.sum();
match spaces {
0 => 0.0,
n => (layout.measure - indent - natural) / n as f64,
}
}
false => 0.0,
};
let led = leading_on(line, &mut sizes.clone());
let mut x = match centred {
true => layout.margin + (layout.measure - width).max(0.0) / 2.0,
false => layout.margin + indent,
};
for (plain, (r, g, b), face, run) in
styled_runs(line, &mut colours, &mut faces, &mut sizes)
{
if plain.is_empty() {
continue;
}
page.content.push_str(&format!("{r} {g} {b} rg\n"));
if let Some(set) = crate::math::run_setting(&plain) {
for (glyph, gx, gy, size) in crate::math::glyphs(&set) {
for piece in drawn(&glyph, &|c| covers(c, face), &outside) {
let font = match piece.from {
Source::Symbol => {
std::sync::Arc::new(Font::Base14(SYMBOL_FONT_NAME.to_string()))
}
Source::Outside(at) => match fallbacks.font(at) {
Some(font) => std::sync::Arc::clone(font),
None => continue,
},
Source::Face => std::sync::Arc::clone(&fonts[face.index()]),
};
let at = Set {
natural: 0.0,
width: 0.0,
};
let drawn_at = match piece.from {
Source::Face => size * scales[face.index()],
_ => size,
};
page.text_set(font, drawn_at, x + gx, y + gy, &piece.codes, at);
}
}
for (rx, ry, rw, rh) in crate::math::rules(&set) {
page.rule(x + rx, y + ry, rw, rh);
}
x += crate::math::pt(set.width);
continue;
}
for piece in drawn(&plain, &|c| covers(c, face), &outside) {
let font = match piece.from {
Source::Symbol => {
std::sync::Arc::new(Font::Base14(SYMBOL_FONT_NAME.to_string()))
}
Source::Outside(at) => match fallbacks.font(at) {
Some(font) => std::sync::Arc::clone(font),
None => continue,
},
Source::Face => std::sync::Arc::clone(&fonts[face.index()]),
};
let natural = piece_width(&piece, face, run.size);
let spaces = match piece.from {
Source::Outside(_) => 0.0,
_ => piece.codes.chars().filter(|c| *c == ' ').count() as f64,
};
let set = Set {
natural,
width: natural + extra * spaces,
};
let drawn_at = match piece.from {
Source::Face => run.size * scales[face.index()],
_ => run.size,
};
page.text_set(font, drawn_at, x, y, &piece.codes, set);
x += set.width;
}
}
y -= led;
}
let shown = folio.to_string();
let width = width_of(&shown, Face::MAIN, layout.size);
page.text_in(
std::sync::Arc::clone(main_shared),
layout.size,
(page.width - width) / 2.0,
page.height - layout.margin - layout.height - FOOTSKIP,
&shown,
);
page
};
let work: Vec<_> = chunks.iter().zip(&starts).collect();
let pages: Vec<Page> = match work.len() >= PARALLEL_PAGES {
true => {
use rayon::prelude::*;
work.into_par_iter().map(draw_page).collect()
}
false => work.into_iter().map(draw_page).collect(),
};
let count = pages.len();
crate::Pdf {
bytes: document_from(pages),
pages: count,
}
}
struct FaceMetrics<'a> {
measure: &'a dyn Fn(&str, Face, f64) -> f64,
size: f64,
}
impl crate::tikz::Metrics for FaceMetrics<'_> {
fn width_of(&self, text: &str, size: f64) -> f64 {
(self.measure)(text, Face::MAIN, self.size) * size / self.size
}
}
struct Piece {
codes: String,
source: String,
from: Source,
}
fn glyph_ids(codes: &str) -> impl Iterator<Item = u16> + '_ {
let mut chars = codes.chars();
std::iter::from_fn(move || {
let hi = chars.next()?;
let lo = chars.next()?;
Some((hi as u16) << 8 | lo as u16)
})
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Source {
Face,
Symbol,
Outside(usize),
}
fn drawn(
text: &str,
covers: &dyn Fn(char) -> bool,
outside: &dyn Fn(char) -> Option<(usize, u16)>,
) -> Vec<Piece> {
let mut pieces: Vec<Piece> = Vec::new();
for ch in printing_chars(text) {
let Some(glyph) = glyph_for(ch, covers, outside) else {
continue;
};
let (from, codes, source) = match glyph {
Glyph::Own(code) => (Source::Face, (code as char).to_string(), ch.to_string()),
Glyph::Fallback(code) => (Source::Symbol, (code as char).to_string(), ch.to_string()),
Glyph::Outside(face, glyph) => (
Source::Outside(face),
[(glyph >> 8) as u8 as char, (glyph & 0xFF) as u8 as char]
.iter()
.collect(),
ch.to_string(),
),
Glyph::StandIn(text) => (Source::Face, text.to_string(), text.to_string()),
};
match pieces.last_mut() {
Some(last) if last.from == from => {
last.codes.push_str(&codes);
last.source.push_str(&source);
}
_ => pieces.push(Piece {
codes,
source,
from,
}),
}
}
pieces
}
fn face_coverage(font: &crate::pdf::Font) -> Option<std::collections::BTreeSet<u32>> {
let crate::pdf::Font::TrueType { bytes, .. } = font else {
return None;
};
let sfnt = crate::sfnt::Sfnt::parse(bytes.clone()).ok()?;
Some(sfnt.cmap().ok()?.into_keys().collect())
}
fn break_lines_measured(
text: &str,
layout: &Layout,
width_of: &(dyn Fn(&str, Face, f64) -> f64 + Sync),
) -> Vec<String> {
break_lines_from(text, layout, width_of, None)
}
fn break_lines_from(
text: &str,
layout: &Layout,
width_of: &(dyn Fn(&str, Face, f64) -> f64 + Sync),
ahead: Option<BrokenAhead>,
) -> Vec<String> {
let (mut broken, mut carried, rest) = match ahead {
Some(a) if text.starts_with(&a.read) => (a.paragraphs, a.carried, &text[a.read.len()..]),
_ => (Vec::new(), ParagraphStart::first(layout), text),
};
let paras: Vec<&str> = rest.split("\n\n").collect();
broken.extend(break_paragraphs(¶s, layout, &mut carried, width_of));
let mut lines = Vec::new();
for (sets_text, own) in broken {
let after_text = lines
.last()
.is_some_and(|line: &String| !is_space_line(line) && !is_break_line(line));
if after_text && sets_text {
lines.push(VERTICAL_SPACE.to_string());
}
lines.extend(own);
}
lines
}
fn break_paragraphs(
paras: &[&str],
layout: &Layout,
carried: &mut ParagraphStart,
width_of: &(dyn Fn(&str, Face, f64) -> f64 + Sync),
) -> Vec<(bool, Vec<String>)> {
match paras.len() >= PARALLEL_PARAGRAPHS && rayon::current_num_threads() > 1 {
true => {
use rayon::prelude::*;
let starts = paragraph_starts(paras, layout, carried.clone());
if let (Some(last), Some(start)) = (paras.last(), starts.last()) {
*carried = start.clone();
advance_paragraph(last, layout, carried);
}
paras
.par_iter()
.zip(starts)
.map(|(para, mut start)| break_measured(para, layout, &mut start, width_of))
.collect()
}
false => paras
.iter()
.map(|para| break_measured(para, layout, carried, width_of))
.collect(),
}
}
const PARALLEL_PARAGRAPHS: usize = 64;
pub struct Pipeline {
done: std::sync::mpsc::Receiver<std::thread::Result<Option<Ahead>>>,
}
enum Fed {
Start {
families: Box<Families>,
layout: Layout,
},
Text(String),
Settings(usize, crate::math::Settings),
}
pub struct Feed {
tx: std::sync::mpsc::Sender<Fed>,
scanned: usize,
taken: usize,
pending: String,
started: bool,
stopped: bool,
}
const FEED_BYTES: usize = 32 * 1024;
const TAIL_STEP: usize = 4 * 1024;
impl Pipeline {
pub fn start(near: Option<&std::path::Path>) -> (Feed, Pipeline) {
let (tx, rx) = std::sync::mpsc::channel();
let (done_tx, done) = std::sync::mpsc::channel();
let near = near.map(std::path::Path::to_path_buf);
rayon::spawn(move || {
let broken = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
break_fed(&rx, near.as_deref())
}));
let _ = done_tx.send(broken);
});
let feed = Feed {
tx,
scanned: 0,
taken: 0,
pending: String::new(),
started: false,
stopped: false,
};
(feed, Pipeline { done })
}
pub fn finish(self) -> Option<Ahead> {
crate::math::receive(&self.done).unwrap_or_else(|p| std::panic::resume_unwind(p))
}
}
impl Feed {
pub fn tap(&self) -> crate::math::Tap {
let tx = self.tx.clone();
std::sync::Arc::new(move |first, settings| {
let _ = tx.send(Fed::Settings(first, settings));
})
}
pub fn offer(&mut self, out: &[Cmd], families: &Families, layout: &Layout) {
if self.stopped {
return;
}
let mut end = out.len();
while end > 0 && matches!(out[end - 1], Cmd::Line(_)) {
end -= 1;
}
let Some(open) = end.checked_sub(1) else {
return;
};
while self.scanned < open {
let cmd = &out[self.scanned];
let fixed = match (self.taken, cmd) {
(0, _) => fixed_text(cmd, &mut self.pending),
(taken, Cmd::Text(t)) => {
self.pending.push_str(&t[taken..]);
true
}
_ => false,
};
if !fixed {
self.stopped = true;
return;
}
self.taken = 0;
self.scanned += 1;
}
if let Cmd::Text(t) = &out[open] {
if t.len() - self.taken >= TAIL_STEP {
if let Some(at) = t[self.taken..].rfind("\n\n") {
let upto = self.taken + at + 2;
self.pending.push_str(&t[self.taken..upto]);
self.taken = upto;
}
}
}
if self.pending.len() < FEED_BYTES {
return;
}
if !self.started {
self.started = true;
let start = Fed::Start {
families: Box::new(families.clone()),
layout: layout.clone(),
};
self.stopped = self.tx.send(start).is_err();
}
let text = std::mem::take(&mut self.pending);
self.stopped |= self.tx.send(Fed::Text(text)).is_err();
}
}
fn fixed_text(cmd: &Cmd, into: &mut String) -> bool {
match cmd {
Cmd::Text(t) => {
into.push_str(t);
true
}
Cmd::Group { body, .. } => body.iter().all(|cmd| fixed_text(cmd, into)),
Cmd::Color { .. }
| Cmd::IfNum { .. }
| Cmd::IfOdd { .. }
| Cmd::Loop { .. }
| Cmd::RustCompile(_) => false,
_ => true,
}
}
fn break_fed(rx: &std::sync::mpsc::Receiver<Fed>, near: Option<&std::path::Path>) -> Option<Ahead> {
use std::sync::mpsc::TryRecvError;
let mut start: Option<(Families, Layout)> = None;
let mut breaker: Option<Ahead> = None;
let mut settings = crate::math::SettingsTable::default();
let mut unsettled = String::new();
let mut settled = String::new();
let (mut fresh, mut closed) = (false, false);
loop {
let fed = match rx.try_recv() {
Ok(fed) => Some(fed),
Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => {
closed = true;
None
}
};
let fed = match fed {
Some(fed) => fed,
None if fresh => {
fresh = false;
let spec = |index: usize, row: usize| settings.get(index, row);
let upto =
crate::math::first_unsettled(&unsettled, &spec).unwrap_or(unsettled.len());
settled.push_str(&crate::math::filled(&unsettled[..upto], &spec));
unsettled.drain(..upto);
let (paras, used) = finished_paragraphs(&settled);
if paras.len() >= PARALLEL_PARAGRAPHS || (closed && !paras.is_empty()) {
let (families, layout) = start.as_ref()?;
breaker
.get_or_insert_with(|| Ahead::new(families, layout, near))
.break_next(¶s);
settled.drain(..used);
}
continue;
}
None if closed => break,
None => match rayon::yield_now() {
Some(rayon::Yield::Executed) => continue,
_ => match rx.recv() {
Ok(fed) => fed,
Err(_) => {
closed = true;
continue;
}
},
},
};
match fed {
Fed::Start { families, .. } if !families.fallbacks.is_empty() => return None,
Fed::Start { families, layout } => start = Some((*families, layout)),
Fed::Text(text) => unsettled.push_str(&text),
Fed::Settings(first, batch) => settings.insert(first, batch),
}
fresh = true;
}
breaker
}
fn finished_paragraphs(text: &str) -> (Vec<&str>, usize) {
let mut paras = Vec::new();
let mut from = 0;
while let Some(at) = text[from..].find("\n\n") {
paras.push(&text[from..from + at]);
from += at + 2;
}
(paras, from)
}
struct BrokenAhead {
read: String,
paragraphs: Vec<(bool, Vec<String>)>,
carried: ParagraphStart,
}
pub struct Ahead {
faces: Faces,
families: Families,
layout: Layout,
broken: BrokenAhead,
}
impl Ahead {
fn new(families: &Families, layout: &Layout, near: Option<&std::path::Path>) -> Self {
Ahead {
faces: Faces::new(families, near, ""),
families: families.clone(),
layout: layout.clone(),
broken: BrokenAhead {
read: String::new(),
paragraphs: Vec::new(),
carried: ParagraphStart::first(layout),
},
}
}
fn break_next(&mut self, paras: &[&str]) {
let faces = &self.faces;
let width_of = |word: &str, face: Face, size: f64| faces.width_of(word, face, size);
let broken = &mut self.broken;
let lines = break_paragraphs(paras, &self.layout, &mut broken.carried, &width_of);
broken.paragraphs.extend(lines);
for para in paras {
broken.read.push_str(para);
broken.read.push_str("\n\n");
}
}
fn measured_as(&self, families: &Families, layout: &Layout) -> bool {
let layout_then = Layout {
paging: layout.paging,
..self.layout.clone()
};
families.fallbacks.is_empty() && self.families == *families && layout_then == *layout
}
}
#[derive(Clone, Debug, PartialEq)]
struct ParagraphStart {
colours: Vec<Spec>,
faces: Vec<Face>,
sizes: Vec<TypeSize>,
centred: bool,
depth: usize,
}
impl ParagraphStart {
fn first(layout: &Layout) -> Self {
ParagraphStart {
colours: Vec::new(),
faces: vec![Face::MAIN],
sizes: vec![TypeSize {
size: layout.size,
leading: layout.leading,
}],
centred: false,
depth: 0,
}
}
}
fn paragraph_starts(
paras: &[&str],
layout: &Layout,
carried: ParagraphStart,
) -> Vec<ParagraphStart> {
use rayon::prelude::*;
let effects: Vec<ParagraphEffect> = paras
.par_iter()
.map(|para| ParagraphEffect::of(para, layout))
.collect();
let size = paras
.len()
.div_ceil(rayon::current_num_threads() * 4)
.max(1);
let mut state = carried;
let mut heads = Vec::with_capacity(paras.len().div_ceil(size));
for (at, (para, effect)) in paras.iter().zip(&effects).enumerate() {
if at % size == 0 {
heads.push(state.clone());
}
effect.advance(para, layout, &mut state);
}
let chunks: Vec<Vec<ParagraphStart>> = paras
.par_chunks(size)
.zip(effects.par_chunks(size))
.zip(heads)
.map(|((paras, effects), mut state)| {
paras
.iter()
.zip(effects)
.map(|(para, effect)| {
let start = state.clone();
effect.advance(para, layout, &mut state);
start
})
.collect()
})
.collect();
chunks.concat()
}
#[cfg(test)]
fn paragraph_starts_serial(
paras: &[&str],
layout: &Layout,
mut carried: ParagraphStart,
) -> Vec<ParagraphStart> {
let mut starts = Vec::with_capacity(paras.len());
for para in paras {
starts.push(carried.clone());
advance_paragraph(para, layout, &mut carried);
}
starts
}
fn advance_paragraph(para: &str, layout: &Layout, state: &mut ParagraphStart) {
let ParagraphStart {
colours,
faces,
sizes,
centred,
depth,
} = state;
let (now_centred, now_depth) = walk_paragraph(
para,
layout,
&mut Live {
colours,
faces,
sizes,
},
);
*centred = now_centred.unwrap_or(*centred);
*depth = now_depth.unwrap_or(*depth);
}
fn walk_paragraph(
para: &str,
layout: &Layout,
styles: &mut impl StyleStacks,
) -> (Option<bool>, Option<usize>) {
let (mut centred, mut depth) = (None, None);
let (_, para) = paragraph_params(para, &layout.breaking);
let para: &str = ¶
if is_block_para(para) || listing_lines(para).is_some() || para.contains(TABLE_ROW) {
return (centred, depth);
}
for part in para.split([PAGE_BREAK, VERTICAL_SPACE]) {
let mut rest = part;
loop {
let (stretch, marker) = next_stretch(rest);
if stretch.contains(STACK_MARKERS) {
for word in words_carrying_refs(stretch) {
if word.contains(STACK_MARKERS) {
walk_styles(&word, styles);
}
}
}
let Some(marker) = marker else { break };
let (mut now_centred, mut now_depth) = (false, 0);
rest = cross_region(rest, stretch, marker, &mut now_centred, &mut now_depth);
match marker {
LIST_INDENT => depth = Some(now_depth),
_ => centred = Some(now_centred),
}
}
}
(centred, depth)
}
struct ParagraphEffect {
styles: StyleEffect,
centred: Option<bool>,
depth: Option<usize>,
}
impl ParagraphEffect {
fn of(para: &str, layout: &Layout) -> Self {
let mut styles = StyleEffect::default();
let (centred, depth) = walk_paragraph(para, layout, &mut styles);
ParagraphEffect {
styles,
centred,
depth,
}
}
fn advance(&self, para: &str, layout: &Layout, state: &mut ParagraphStart) {
if !self
.styles
.apply(&mut state.colours, &mut state.faces, &mut state.sizes)
{
advance_paragraph(para, layout, state);
return;
}
state.centred = self.centred.unwrap_or(state.centred);
state.depth = self.depth.unwrap_or(state.depth);
}
}
fn break_measured(
para: &str,
layout: &Layout,
state: &mut ParagraphStart,
width_of: &dyn Fn(&str, Face, f64) -> f64,
) -> (bool, Vec<String>) {
let ParagraphStart {
colours,
faces,
sizes,
centred,
depth,
} = state;
let mut lines = Vec::new();
let (params, para) = paragraph_params(para, &layout.breaking);
let para: &str = ¶
let sets = sets_text(para);
let with_params;
let layout = match params == layout.breaking {
true => layout,
false => {
with_params = Layout {
breaking: params,
..layout.clone()
};
&with_params
}
};
if is_block_para(para) {
lines.push(match *centred {
true => format!("{CENTRE}{}", para.trim()),
false => para.trim().to_string(),
});
return (sets, lines);
}
if let Some(code) = listing_lines(para) {
lines.extend(code.map(str::to_string));
return (sets, lines);
}
if para.contains(TABLE_ROW) {
table_lines(para, layout, colours, faces, sizes, width_of, &mut lines);
return (sets, lines);
}
for (part_number, part) in para.split(PAGE_BREAK).enumerate() {
if part_number > 0 {
lines.push(PAGE_BREAK.to_string());
}
for (space_number, part) in part.split(VERTICAL_SPACE).enumerate() {
if space_number > 0 {
lines.push(VERTICAL_SPACE.to_string());
}
fill(
part, centred, depth, layout, colours, faces, sizes, width_of, &mut lines,
);
}
}
(sets, lines)
}
const STACK_MARKERS: [char; 4] = ['\u{1}', '\u{3}', FACE_PUSH, FACE_POP];
fn next_stretch(rest: &str) -> (&str, Option<char>) {
match rest.find([CENTRE, CENTRE_END, LIST_INDENT]) {
Some(at) => (&rest[..at], rest[at..].chars().next()),
None => (rest, None),
}
}
fn cross_region<'a>(
rest: &'a str,
stretch: &str,
marker: char,
centred: &mut bool,
depth: &mut usize,
) -> &'a str {
match marker {
LIST_INDENT => {
let (deeper, after) = strip_indent(&rest[stretch.len()..]);
*depth = deeper;
after
}
m => {
*centred = m == CENTRE;
&rest[stretch.len() + m.len_utf8()..]
}
}
}
#[allow(clippy::too_many_arguments)]
fn fill(
text: &str,
centred: &mut bool,
depth: &mut usize,
layout: &Layout,
colours: &mut Vec<Spec>,
faces: &mut Vec<Face>,
sizes: &mut Vec<TypeSize>,
width_of: &dyn Fn(&str, Face, f64) -> f64,
lines: &mut Vec<String>,
) {
use crate::linebreak::After;
let mut rest = text;
loop {
let (stretch, marker) = next_stretch(rest);
let indent = match *centred {
true => 0.0,
false => list_indent(*depth, layout),
};
let measure = layout.measure - indent;
let start = |centred: bool, depth: usize| match (centred, depth) {
(true, _) => CENTRE.to_string(),
(_, 0) => String::new(),
(_, deep) => indent_mark(deep),
};
let mut pieces: Vec<crate::linebreak::Piece> = Vec::new();
for word in words_carrying_refs(stretch) {
if let Some(previous) = pieces.last_mut() {
previous.after = crate::linebreak::After::Glue(width_of(
" ",
current_face(faces),
current_size(sizes).size,
));
}
measure_word(&word, colours, faces, sizes, width_of, &mut pieces);
}
let breaks = crate::linebreak::break_paragraph(&pieces, measure, &layout.breaking);
let mut from = 0usize;
for (number, end) in breaks.iter().enumerate() {
let mut line = start(*centred, *depth);
for (offset, piece) in pieces[from..*end].iter().enumerate() {
if offset > 0 && matches!(pieces[from + offset - 1].after, After::Glue(_)) {
line.push(' ');
}
line.push_str(&piece.text);
}
if matches!(pieces[*end - 1].after, After::Discretionary(_)) {
line.push('-');
}
from = *end;
let last = number + 1 == breaks.len();
lines.push(match *centred || last {
true => line,
false => format!("{JUSTIFY}{line}"),
});
}
match marker {
Some(marker) => rest = cross_region(rest, stretch, marker, centred, depth),
None => return,
}
}
}
fn measure_word(
word: &str,
colours: &mut Vec<Spec>,
faces: &mut Vec<Face>,
sizes: &mut Vec<TypeSize>,
width_of: &dyn Fn(&str, Face, f64) -> f64,
out: &mut Vec<crate::linebreak::Piece>,
) {
use crate::linebreak::{After, Piece};
let face = current_face(faces);
let size = current_size(sizes).size;
let whole = |text: &str, width: f64| Piece {
text: text.to_string(),
width,
after: After::Nothing,
};
if word.contains(['\u{1}', '\u{3}', FACE_PUSH, FACE_POP, REF]) {
let width = word_width(word, colours, faces, sizes, width_of);
out.push(whole(word, width));
return;
}
if word.contains('-') {
let parts: Vec<&str> = word.split_inclusive('-').collect();
for (number, part) in parts.iter().enumerate() {
out.push(Piece {
text: part.to_string(),
width: width_of(part, face, size),
after: match number + 1 < parts.len() {
true => After::Explicit,
false => After::Nothing,
},
});
}
return;
}
let letter = |c: char| c.is_ascii_alphabetic();
let head = word.len() - word.trim_start_matches(|c| !letter(c)).len();
let tail = word.len() - word.trim_end_matches(|c| !letter(c)).len();
let points: Vec<usize> = match head + tail <= word.len() {
true => crate::linebreak::hyphenator()
.points(&word[head..word.len() - tail])
.iter()
.map(|at| at + head)
.collect(),
false => Vec::new(),
};
if points.is_empty() {
out.push(whole(word, width_of(word, face, size)));
return;
}
let dash = width_of("-", face, size);
let mut from = 0usize;
for at in points.iter().copied().chain(std::iter::once(word.len())) {
out.push(Piece {
text: word[from..at].to_string(),
width: width_of(&word[from..at], face, size),
after: match at == word.len() {
true => After::Nothing,
false => After::Discretionary(dash),
},
});
from = at;
}
}
fn list_indent(depth: usize, layout: &Layout) -> f64 {
depth as f64 * 2.5 * layout.size
}
pub fn indent_mark(depth: usize) -> String {
let digit = char::from_digit(depth.min(9) as u32, 10).unwrap_or('0');
format!("{LIST_INDENT}{digit}")
}
fn strip_indent(line: &str) -> (usize, &str) {
let Some(rest) = line.strip_prefix(LIST_INDENT) else {
return (0, line);
};
let mut chars = rest.chars();
let depth = chars.next().and_then(|c| c.to_digit(10)).unwrap_or(0) as usize;
(depth, chars.as_str())
}
const FOOTSKIP: f64 = 30.0;
const PARAGRAPH_SPACE: f64 = 0.5;
fn restarts_here(lines: &[&str]) -> bool {
lines.iter().any(|line| {
let mut chars = line.chars();
while let Some(ch) = chars.next() {
if ch == TOC && chars.next() == Some(TOC_PAGE_ONE) {
return true;
}
}
false
})
}
fn is_space_line(line: &str) -> bool {
!line.is_empty() && line.chars().all(|c| c == VERTICAL_SPACE)
}
fn is_break_line(line: &str) -> bool {
!line.is_empty() && line.chars().all(|c| c == PAGE_BREAK)
}
fn line_leadings(lines: &[String], layout: &Layout) -> Vec<f64> {
let marked = per_line(lines.len(), |i| {
lines[i]
.bytes()
.any(|b| b == SIZE_PUSH as u8 || b == SIZE_POP as u8)
});
let mut sizes: Vec<TypeSize> = vec![TypeSize {
size: layout.size,
leading: layout.leading,
}];
lines
.iter()
.zip(marked)
.map(|(line, marked)| match marked {
true => leading_on(line, &mut sizes),
false => current_size(&sizes).leading,
})
.collect()
}
const PARALLEL_LINES: usize = 2048;
fn per_line<T: Send>(count: usize, read: impl Fn(usize) -> T + Sync + Send) -> Vec<T> {
use rayon::prelude::*;
match count >= PARALLEL_LINES && rayon::current_num_threads() > 1 {
true => (0..count).into_par_iter().map(read).collect(),
false => (0..count).map(read).collect(),
}
}
fn leading_on(line: &str, sizes: &mut Vec<TypeSize>) -> f64 {
let mut most = current_size(sizes).leading;
let mut chars = line.chars();
while let Some(ch) = chars.next() {
match ch {
SIZE_PUSH => {
let mut spec = String::new();
for c in chars.by_ref() {
if c == SIZE_PUSH {
break;
}
spec.push(c);
}
let pushed = match size_spec(&spec) {
Some((size, leading)) => TypeSize { size, leading },
None => current_size(sizes),
};
sizes.push(pushed);
most = most.max(pushed.leading);
}
SIZE_POP if sizes.len() > 1 => {
sizes.pop();
}
_ => {}
}
}
most
}
fn line_height(line: &str, leading: f64) -> f64 {
if let Some((_, height, _)) = image_parts(line) {
if let Ok(height) = height.parse::<f64>() {
return height + leading;
}
}
if let Some((height, _, _)) = picture_parts(line) {
return height + leading;
}
match is_space_line(line) {
true => leading * PARAGRAPH_SPACE,
false => leading,
}
}
fn sets_text(para: &str) -> bool {
is_block_para(para) || printing_chars(para).any(|c| !c.is_whitespace())
}
const PARALLEL_PAGES: usize = 8;
const PARALLEL_BLOCKS: usize = 8;
#[derive(Clone, Debug, PartialEq)]
struct PageStart {
colours: Vec<Spec>,
faces: Vec<Face>,
sizes: Vec<TypeSize>,
folio: usize,
}
fn page_starts(
chunks: &[Vec<&str>],
mut colours: Vec<Spec>,
mut faces: Vec<Face>,
mut sizes: Vec<TypeSize>,
) -> Vec<PageStart> {
use rayon::prelude::*;
if chunks.len() < PARALLEL_PAGES || rayon::current_num_threads() == 1 {
return page_starts_serial(chunks, colours, faces, sizes);
}
let effects: Vec<(bool, StyleEffect)> = chunks
.par_iter()
.map(|chunk| {
let mut effect = StyleEffect::default();
walk_page(chunk, &mut effect);
(restarts_here(chunk), effect)
})
.collect();
let mut folio = 1;
let mut starts = Vec::with_capacity(chunks.len());
for (chunk, (restarts, effect)) in chunks.iter().zip(&effects) {
if *restarts {
folio = 1;
}
starts.push(PageStart {
colours: colours.clone(),
faces: faces.clone(),
sizes: sizes.clone(),
folio,
});
folio += 1;
if !effect.apply(&mut colours, &mut faces, &mut sizes) {
walk_page(
chunk,
&mut Live {
colours: &mut colours,
faces: &mut faces,
sizes: &mut sizes,
},
);
}
}
starts
}
fn page_starts_serial(
chunks: &[Vec<&str>],
mut colours: Vec<Spec>,
mut faces: Vec<Face>,
mut sizes: Vec<TypeSize>,
) -> Vec<PageStart> {
let mut folio = 1;
let mut starts = Vec::with_capacity(chunks.len());
for chunk in chunks {
if restarts_here(chunk) {
folio = 1;
}
starts.push(PageStart {
colours: colours.clone(),
faces: faces.clone(),
sizes: sizes.clone(),
folio,
});
folio += 1;
walk_page(
chunk,
&mut Live {
colours: &mut colours,
faces: &mut faces,
sizes: &mut sizes,
},
);
}
starts
}
fn walk_page(chunk: &[&str], styles: &mut impl StyleStacks) {
for &line in chunk {
let line = without_longtable(line);
if is_space_line(line)
|| image_parts(line).is_some()
|| picture_parts(line).is_some()
|| line.starts_with(TABLE_MARK)
{
continue;
}
let line = line.strip_prefix(CENTRE).unwrap_or(line);
let line = line.strip_prefix(JUSTIFY).unwrap_or(line);
let (_, line) = strip_indent(line);
walk_styles(line, styles);
}
}
fn paginate<'a>(lines: &'a [String], layout: &Layout) -> Vec<Vec<&'a str>> {
let leadings = line_leadings(lines, layout);
let items = page_items(lines, layout, &leadings);
let mut pages: Vec<Vec<&str>> = Vec::new();
for Planned { from, upto, foot } in plan_pages(&items, lines, layout, &leadings) {
let mut page: Vec<&str> = Vec::new();
if items[from].repeat_head {
let (at, end) = items[from].head;
page.extend(lines[at..end].iter().map(String::as_str));
}
for item in &items[from..upto] {
if !matches!(item.kind, Kind::Set(_)) {
continue;
}
page.extend(
lines[item.at..item.at + item.len]
.iter()
.map(String::as_str),
);
}
if let Some((at, end)) = foot {
page.extend(lines[at..end].iter().map(String::as_str));
}
if !page.is_empty() {
pages.push(page);
}
}
pages
}
enum Kind {
Break,
Held,
Set(f64),
}
struct Item {
at: usize,
len: usize,
kind: Kind,
head: (usize, usize),
repeat_head: bool,
foot: (usize, usize),
}
fn run_height(lines: &[String], run: (usize, usize), leadings: &[f64]) -> f64 {
lines[run.0..run.1]
.iter()
.enumerate()
.map(|(i, l)| line_height(l, leadings[run.0 + i]))
.sum()
}
fn page_items(lines: &[String], layout: &Layout, leadings: &[f64]) -> Vec<Item> {
const EMPTY: (usize, usize) = (0, 0);
let extend = |run: (usize, usize), at: usize| match run.0 != run.1 && run.1 == at {
true => (run.0, at + 1),
false => (at, at + 1),
};
let mut head = EMPTY;
let mut foot = EMPTY;
let mut in_table = false;
let mut items = Vec::new();
let mut i = 0;
while i < lines.len() {
let line = lines[i].as_str();
if is_break_line(line) {
in_table = false;
head = EMPTY;
foot = EMPTY;
items.push(Item {
at: i,
len: 1,
kind: Kind::Break,
head,
repeat_head: false,
foot,
});
i += 1;
continue;
}
let Some(code) = longtable_code(line) else {
in_table = false;
head = EMPTY;
foot = EMPTY;
items.push(Item {
at: i,
len: 1,
kind: Kind::Set(line_height(line, leadings[i])),
head,
repeat_head: false,
foot,
});
i += 1;
continue;
};
if !in_table {
in_table = true;
head = EMPTY;
foot = EMPTY;
}
if code == LT_REPEAT || code == LT_FOOT {
match code == LT_REPEAT {
true => head = extend(head, i),
false => foot = extend(foot, i),
}
items.push(Item {
at: i,
len: 1,
kind: Kind::Held,
head,
repeat_head: false,
foot,
});
i += 1;
continue;
}
let continues = match code {
LT_HEAD => LT_HEAD,
_ => LT_CONT,
};
let mut group = 1;
while lines
.get(i + group)
.is_some_and(|next| longtable_code(next) == Some(continues))
{
group += 1;
}
if code == LT_HEAD {
head = (i, i + group);
}
let room = ((layout.height - run_height(lines, head, leadings)) / layout.leading)
.floor()
.max(1.0) as usize;
let step = group.min(room);
items.push(Item {
at: i,
len: step,
kind: Kind::Set(step as f64 * layout.leading),
head,
repeat_head: code != LT_HEAD,
foot,
});
i += step;
}
items
}
struct Planned {
from: usize,
upto: usize,
foot: Option<(usize, usize)>,
}
#[derive(Clone, Copy)]
struct Stop {
next: usize,
used: f64,
foot: Option<(usize, usize)>,
forced: bool,
}
fn page_stops(
items: &[Item],
lines: &[String],
layout: &Layout,
leadings: &[f64],
from: usize,
stops: &mut Vec<Stop>,
) {
const SLACK: f64 = 1e-6;
let mut used = match items[from].repeat_head {
true => run_height(lines, items[from].head, leadings),
false => 0.0,
};
let mut set = 0usize;
let mut j = from;
while j < items.len() {
let item = &items[j];
match item.kind {
Kind::Break if set == 0 => j += 1,
Kind::Break => {
stops.push(Stop {
next: j + 1,
used,
foot: None,
forced: true,
});
return;
}
Kind::Held => j += 1,
Kind::Set(tall) => {
if set > 0 {
let foot = (item.foot.0 != item.foot.1).then_some(item.foot);
let below = foot.map_or(0.0, |f| run_height(lines, f, leadings));
stops.push(Stop {
next: j,
used: used + below,
foot,
forced: false,
});
if used + below > layout.height + SLACK {
return;
}
}
used += tall;
set += 1;
j += 1;
}
}
}
stops.push(Stop {
next: items.len(),
used,
foot: None,
forced: true,
});
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PageParams {
pub club: f64,
pub widow: f64,
pub broken: f64,
}
impl Default for PageParams {
fn default() -> Self {
Self {
club: 150.0,
widow: 150.0,
broken: 100.0,
}
}
}
impl PageParams {
pub fn from_intpars(p: &crate::intpar::IntPars) -> Self {
let d = Self::default();
let read = |name: &str, fallback: f64| {
crate::intpar::index(name)
.and_then(|i| p.assigned(i))
.map_or(fallback, |v| v as f64)
};
Self {
club: read("clubpenalty", d.club),
widow: read("widowpenalty", d.widow),
broken: read("brokenpenalty", d.broken),
}
}
}
const SEC_PENALTY: f64 = -300.0;
const FORBID_PENALTY: f64 = 10000.0;
const SLACK_COST: f64 = 100.0;
fn break_penalties(lines: &[String], params: &PageParams) -> Vec<f64> {
let n = lines.len();
let mut cost = vec![0.0f64; n + 1];
let is_text =
|line: &str| !is_space_line(line) && !is_break_line(line) && longtable_code(line).is_none();
let class = per_line(n, |i| {
let line = lines[i].as_str();
let space = is_space_line(line);
let text = is_text(line);
let continued = text && lines.get(i + 1).is_some_and(|next| is_text(next));
(
space,
text,
text && heading_line(line),
continued && printing_chars(line).last() == Some('-'),
)
});
let space = |k: usize| class[k].0;
let text = |k: usize| class[k].1;
let heading = |k: usize| class[k].2;
let hyphen = |k: usize| class[k].3;
let mut i = 0;
while i < n {
if !text(i) {
i += 1;
continue;
}
let from = i;
let mut upto = i;
while upto < n && text(upto) {
upto += 1;
}
i = upto;
if (from..upto).any(heading) {
let mut below = upto;
while below < n && space(below) {
below += 1;
}
let held = match below < n && text(below) {
true => below + 1,
false => below,
};
cost[from + 1..=held.min(n)].fill(FORBID_PENALTY);
let mut above = from;
while above > 0 && space(above - 1) {
above -= 1;
}
cost[(above + 1).min(from)..from].fill(FORBID_PENALTY);
if cost[above] != FORBID_PENALTY {
cost[above] = SEC_PENALTY;
}
continue;
}
for (e, c) in cost.iter_mut().enumerate().take(upto).skip(from + 1) {
if e == from + 1 {
*c += params.club;
}
if e == upto - 1 {
*c += params.widow;
}
if hyphen(e - 1) {
*c += params.broken;
}
}
}
cost
}
fn heading_line(line: &str) -> bool {
let mut chars = line.chars();
while let Some(ch) = chars.next() {
if ch == TOC && chars.next().is_some_and(|code| toc_level(code).is_some()) {
return true;
}
}
false
}
#[derive(Default)]
struct Offers {
stops: Vec<Stop>,
eligible: Vec<Eligible>,
starts: Vec<Start>,
}
impl Offers {
fn clear(&mut self) {
self.stops.clear();
self.eligible.clear();
self.starts.clear();
}
}
struct Start {
eligible: usize,
fallback: (usize, f64),
}
struct Eligible {
index: usize,
next: usize,
empty: f64,
broken: f64,
}
const PLAN_BLOCK: usize = 2048;
const PLAN_RUN: usize = 16;
fn plan_pages(items: &[Item], lines: &[String], layout: &Layout, leadings: &[f64]) -> Vec<Planned> {
const SLACK: f64 = 1e-6;
let penalty = break_penalties(lines, &layout.paging);
let count = items.len();
let mut best = vec![f64::INFINITY; count + 1];
let mut chosen: Vec<Option<Stop>> = vec![None; count + 1];
best[count] = 0.0;
let gather = |run: &mut Offers, starts: std::ops::Range<usize>| {
for from in starts {
let first = run.stops.len();
page_stops(items, lines, layout, leadings, from, &mut run.stops);
let mut fallback: Option<(usize, f64)> = None;
for (index, stop) in run.stops.iter().enumerate().skip(first) {
let room = layout.height - stop.used;
let fits = room >= -SLACK;
let empty = match stop.forced {
true => 0.0,
false => SLACK_COST * room.max(0.0) / layout.leading,
};
if fits || fallback.is_none() {
fallback = Some((index, empty));
}
let broken = match stop.forced {
true => 0.0,
false => penalty[items[stop.next].at],
};
if fits && broken < FORBID_PENALTY {
run.eligible.push(Eligible {
index,
next: stop.next,
empty,
broken,
});
}
}
run.starts.push(Start {
eligible: run.eligible.len(),
fallback: fallback.expect("a page start has at least one stop"),
});
}
};
let choose = |run: &Offers, base: usize, best: &mut [f64], chosen: &mut [Option<Stop>]| {
for (at, start) in run.starts.iter().enumerate().rev() {
let first = match at {
0 => 0,
_ => run.starts[at - 1].eligible,
};
let mut least = f64::INFINITY;
let mut pick = None;
for offer in &run.eligible[first..start.eligible] {
let cost = best[offer.next] + offer.empty + offer.broken;
if cost < least {
least = cost;
pick = Some(offer.index);
}
}
let index = pick.unwrap_or_else(|| {
let (index, empty) = start.fallback;
least = best[run.stops[index].next] + empty;
index
});
best[base + at] = least;
chosen[base + at] = Some(run.stops[index]);
}
};
match count >= PARALLEL_LINES && rayon::current_num_threads() > 1 {
true => {
use rayon::prelude::*;
let blocks: Vec<std::ops::Range<usize>> = (0..count.div_ceil(PLAN_BLOCK))
.rev()
.map(|b| b * PLAN_BLOCK..((b + 1) * PLAN_BLOCK).min(count))
.collect();
let fill = |runs: &mut [Offers], block: &std::ops::Range<usize>| {
runs.par_iter_mut().enumerate().for_each(|(k, run)| {
run.clear();
let start = block.start + k * PLAN_RUN;
if start < block.end {
gather(run, start..(start + PLAN_RUN).min(block.end));
}
});
};
let fresh = || -> Vec<Offers> {
(0..PLAN_BLOCK / PLAN_RUN)
.map(|_| Offers::default())
.collect()
};
let (mut here, mut next) = (fresh(), fresh());
fill(&mut here, &blocks[0]);
for (b, block) in blocks.iter().enumerate() {
rayon::join(
|| {
for (k, run) in here.iter().enumerate().rev() {
choose(run, block.start + k * PLAN_RUN, &mut best, &mut chosen);
}
},
|| {
if let Some(after) = blocks.get(b + 1) {
fill(&mut next, after);
}
},
);
std::mem::swap(&mut here, &mut next);
}
}
false => {
let mut run = Offers::default();
for from in (0..count).rev() {
run.clear();
gather(&mut run, from..from + 1);
choose(&run, from, &mut best, &mut chosen);
}
}
}
let mut plan = Vec::new();
let mut at = 0usize;
while at < count {
let Some(stop) = chosen[at] else {
break;
};
plan.push(Planned {
from: at,
upto: stop.next.min(count),
foot: stop.foot,
});
at = stop.next;
}
plan
}
#[cfg(test)]
mod plan_tests {
use super::*;
type PagePlan = (usize, usize, Option<(usize, usize)>);
struct Rng(u64);
impl Rng {
fn below(&mut self, n: usize) -> usize {
self.0 ^= self.0 >> 12;
self.0 ^= self.0 << 25;
self.0 ^= self.0 >> 27;
(self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 33) as usize % n
}
}
fn document(seed: u64, count: usize) -> Vec<String> {
let mut rng = Rng(seed);
let mut lines = Vec::new();
while lines.len() < count {
match rng.below(40) {
0 => lines.push(PAGE_BREAK.to_string()),
1..=3 => {
lines.push(VERTICAL_SPACE.to_string().repeat(1 + rng.below(3)));
lines.push(format!("{TOC}{TOC_SECTION}Heading"));
lines.push(VERTICAL_SPACE.to_string());
}
4 => lines.push(size_span(14.4, 18.0, "Larger")),
5 => {
let code = |c: char, text: &str| format!("{LONGTABLE}{c}{text}");
lines.push(code(LT_HEAD, "head"));
lines.push(code(LT_REPEAT, "head again"));
lines.push(code(LT_FOOT, "foot"));
for _ in 0..rng.below(120) {
lines.push(code(LT_ROW, "row"));
for _ in 0..rng.below(3) {
lines.push(code(LT_CONT, "row, wrapped"));
}
}
}
_ => {
for _ in 0..1 + rng.below(9) {
lines.push(match rng.below(6) {
0 => "a line that broke inside a hyph-".to_string(),
_ => "an ordinary line of prose".to_string(),
});
}
lines.push(VERTICAL_SPACE.to_string());
}
}
}
lines
}
fn reference(
items: &[Item],
lines: &[String],
layout: &Layout,
leadings: &[f64],
) -> Vec<PagePlan> {
const SLACK: f64 = 1e-6;
let penalty = break_penalties(lines, &layout.paging);
let count = items.len();
let mut best = vec![f64::INFINITY; count + 1];
let mut chosen: Vec<Option<Stop>> = vec![None; count + 1];
best[count] = 0.0;
for from in (0..count).rev() {
let mut fallback: Option<(Stop, f64)> = None;
let mut stops = Vec::new();
page_stops(items, lines, layout, leadings, from, &mut stops);
for stop in stops {
let room = layout.height - stop.used;
let fits = room >= -SLACK;
let empty = match stop.forced {
true => 0.0,
false => SLACK_COST * room.max(0.0) / layout.leading,
};
let rest = best[stop.next] + empty;
if fits || fallback.is_none() {
fallback = Some((stop, rest));
}
let broken = match stop.forced {
true => 0.0,
false => penalty[items[stop.next].at],
};
if !fits || broken >= FORBID_PENALTY {
continue;
}
if rest + broken < best[from] {
best[from] = rest + broken;
chosen[from] = Some(stop);
}
}
if chosen[from].is_none() {
if let Some((stop, cost)) = fallback {
best[from] = cost;
chosen[from] = Some(stop);
}
}
}
let mut plan = Vec::new();
let mut at = 0;
while let Some(stop) = chosen.get(at).copied().flatten() {
plan.push((at, stop.next.min(count), stop.foot));
at = stop.next;
}
plan
}
fn in_pool<T: Send>(threads: usize, run: impl FnOnce() -> T + Send) -> T {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build()
.expect("pool")
.install(run)
}
#[test]
fn pages_planned_on_many_threads_are_the_plan_one_walk_makes() {
let layout = Layout::default();
for seed in [1, 7, 0x5eed] {
let lines = document(seed, PLAN_BLOCK * 3 + 517);
let plan = |threads| {
in_pool(threads, || {
let leadings = line_leadings(&lines, &layout);
let items = page_items(&lines, &layout, &leadings);
let plan: Vec<_> = plan_pages(&items, &lines, &layout, &leadings)
.into_iter()
.map(|p| (p.from, p.upto, p.foot))
.collect();
(leadings, break_penalties(&lines, &layout.paging), plan)
})
};
let (leadings, penalties, serial) = plan(1);
let items = page_items(&lines, &layout, &leadings);
assert_eq!(
serial,
reference(&items, &lines, &layout, &leadings),
"seed {seed}"
);
assert!(
serial.len() > 50,
"seed {seed}: too few pages to say anything"
);
assert!(
serial.iter().any(|p| p.2.is_some()),
"seed {seed}: no page ran a table past its foot"
);
let mut sizes = vec![TypeSize {
size: layout.size,
leading: layout.leading,
}];
let walked: Vec<f64> = lines.iter().map(|l| leading_on(l, &mut sizes)).collect();
assert_eq!(leadings, walked, "seed {seed}");
for threads in [2, 8] {
assert!(
plan(threads) == (leadings.clone(), penalties.clone(), serial.clone()),
"seed {seed}: {threads} threads"
);
}
}
}
}
pub const TOC: char = '\u{15}';
pub const TOC_CHAPTER: char = 'c';
pub const TOC_SECTION: char = 's';
pub const TOC_SUBSECTION: char = 'u';
pub const TOC_PAGE_ONE: char = 'p';
const CONTENTS_NAME: &str = "Contents";
const CONTENTS_PASSES: usize = 4;
fn toc_level(code: char) -> Option<usize> {
match code {
TOC_CHAPTER => Some(0),
TOC_SECTION => Some(1),
TOC_SUBSECTION => Some(2),
_ => None,
}
}
pub fn toc_entry_mark(level: usize) -> String {
match level {
0 => format!("{TOC}{TOC_CHAPTER}"),
1 => format!("{TOC}{TOC_SECTION}"),
2 => format!("{TOC}{TOC_SUBSECTION}"),
_ => String::new(),
}
}
pub fn toc_request_mark(depth: usize) -> String {
let digit = char::from_digit(depth.min(9) as u32, 10).unwrap_or('0');
format!("{TOC}{digit}")
}
pub fn toc_page_one_mark() -> String {
format!("{TOC}{TOC_PAGE_ONE}")
}
fn contents_depth(text: &str) -> Option<usize> {
let mut rest = text;
while let Some(at) = rest.find(TOC) {
let after = &rest[at + TOC.len_utf8()..];
let code = after.chars().next()?;
if let Some(depth) = code.to_digit(10) {
return Some(depth as usize);
}
rest = &after[code.len_utf8()..];
}
None
}
fn contents_entries(text: &str, depth: usize) -> Vec<(usize, String)> {
let mut entries = Vec::new();
let mut rest = text;
while let Some(at) = rest.find(TOC) {
let after = &rest[at + TOC.len_utf8()..];
let Some(code) = after.chars().next() else {
break;
};
rest = &after[code.len_utf8()..];
let Some(level) = toc_level(code) else {
continue;
};
if level > depth {
continue;
}
let title = rest.split("\n\n").next().unwrap_or("");
let plain: String = printing_chars(title).collect();
entries.push((
level,
plain.split_whitespace().collect::<Vec<_>>().join(" "),
));
}
entries
}
fn heading_pages(lines: &[String], layout: &Layout, depth: usize) -> Vec<usize> {
let mut found = Vec::new();
for (folio, page) in folio_pages(lines, layout) {
for line in page {
let mut chars = line.chars();
while let Some(ch) = chars.next() {
if ch != TOC {
continue;
}
let Some(code) = chars.next() else {
break;
};
if matches!(toc_level(code), Some(level) if level <= depth) {
found.push(folio);
}
}
}
}
found
}
fn carries_page_one(line: &str) -> bool {
let mut chars = line.chars();
while let Some(ch) = chars.next() {
if ch == TOC && chars.next() == Some(TOC_PAGE_ONE) {
return true;
}
}
false
}
fn folio_pages<'a>(lines: &'a [String], layout: &Layout) -> Vec<(usize, Vec<&'a str>)> {
let mut folio = 1usize;
let mut pages = Vec::new();
for page in paginate(lines, layout) {
let restart = page.iter().any(|line| carries_page_one(line));
pages.push((folio, page));
folio = match restart {
true => 1,
false => folio + 1,
};
}
pages
}
fn entry_lines(
level: usize,
title: &str,
page: usize,
layout: &Layout,
width_of: &dyn Fn(&str, Face, f64) -> f64,
) -> Vec<String> {
let face = Face::MAIN;
let number = page.to_string();
let indent = list_indent(level, layout);
let space = width_of(" ", face, layout.size);
let dot = width_of(".", face, layout.size).max(f64::EPSILON);
let room = layout.measure - indent - width_of(&number, face, layout.size) - 2.0 * space;
let mut lines: Vec<String> = Vec::new();
let mut current = String::new();
for word in title.split_whitespace() {
let wider = match current.is_empty() {
true => word.to_string(),
false => format!("{current} {word}"),
};
match !current.is_empty() && width_of(&wider, face, layout.size) > room {
true => lines.push(std::mem::replace(&mut current, word.to_string())),
false => current = wider,
}
}
let dots = ((room - width_of(¤t, face, layout.size)) / dot)
.floor()
.max(0.0) as usize;
lines.push(format!("{current} {} {number}", ".".repeat(dots)));
let mark = match level {
0 => String::new(),
deep => indent_mark(deep),
};
lines.into_iter().map(|l| format!("{mark}{l}")).collect()
}
fn contents_block(
entries: &[(usize, String)],
pages: &[usize],
layout: &Layout,
width_of: &dyn Fn(&str, Face, f64) -> f64,
) -> String {
let mut listing = String::new();
for (at, (level, title)) in entries.iter().enumerate() {
let page = pages.get(at).copied().unwrap_or(0);
if *level == 0 {
listing.push(VERTICAL_SPACE);
listing.push(LISTING_BREAK);
}
for line in entry_lines(*level, title, page, layout, width_of) {
listing.push_str(&line);
listing.push(LISTING_BREAK);
}
}
format!(
"\n\n{PAGE_BREAK}\n\n{VERTICAL_SPACE}\n\n{CONTENTS_NAME}\n\n{VERTICAL_SPACE}\n\n{listing}\n\n"
)
}
fn with_contents(
text: &str,
entries: &[(usize, String)],
pages: &[usize],
layout: &Layout,
width_of: &dyn Fn(&str, Face, f64) -> f64,
) -> String {
let block = contents_block(entries, pages, layout, width_of);
let mut out = String::with_capacity(text.len() + block.len());
let mut rest = text;
while let Some(at) = rest.find(TOC) {
let after = &rest[at + TOC.len_utf8()..];
let Some(code) = after.chars().next() else {
break;
};
out.push_str(&rest[..at]);
match code.is_ascii_digit() {
true => out.push_str(&block),
false => {
out.push(TOC);
out.push(code);
}
}
rest = &after[code.len_utf8()..];
}
out.push_str(rest);
out
}
fn contents_set<'a>(
text: &'a str,
layout: &Layout,
width_of: &(dyn Fn(&str, Face, f64) -> f64 + Sync),
) -> std::borrow::Cow<'a, str> {
let Some(depth) = contents_depth(text) else {
return std::borrow::Cow::Borrowed(text);
};
let entries = contents_entries(text, depth);
let mut pages: Vec<usize> = vec![0; entries.len()];
for _ in 0..CONTENTS_PASSES {
let staged = with_contents(text, &entries, &pages, layout, width_of);
let lines = break_lines_measured(&staged, layout, width_of);
let found = heading_pages(&lines, layout, depth);
if found == pages {
return std::borrow::Cow::Owned(staged);
}
pages = found;
}
std::borrow::Cow::Owned(with_contents(text, &entries, &pages, layout, width_of))
}
pub const REF: char = '\u{17}';
pub const REF_LABEL: char = 'l';
pub const REF_NUMBER: char = 'n';
pub const REF_PAGE: char = 'p';
pub const REF_VALUE: char = 'v';
pub const REF_OPEN: char = '(';
pub const REF_CLOSE: char = ')';
const UNRESOLVED: &str = "??";
const REF_PASSES: usize = 4;
pub fn ref_mark(code: char, key: &str) -> String {
let key: String = key
.chars()
.filter(|c| !c.is_whitespace() && !c.is_control())
.collect();
format!("{REF}{code}{key}{REF}")
}
struct RefSpan<'a> {
at: usize,
end: usize,
code: char,
key: &'a str,
}
fn next_ref(text: &str, from: usize) -> Option<RefSpan<'_>> {
let at = from + text[from..].find(REF)?;
let after = at + REF.len_utf8();
let code = text[after..].chars().next()?;
let key_at = after + code.len_utf8();
let close = key_at + text[key_at..].find(REF)?;
Some(RefSpan {
at,
end: close + REF.len_utf8(),
code,
key: &text[key_at..close],
})
}
fn is_ref_only(word: &str) -> bool {
let mut at = 0;
while let Some(span) = next_ref(word, at) {
if span.at != at {
return false;
}
at = span.end;
}
at > 0 && at == word.len()
}
fn words_carrying_refs(text: &str) -> Vec<std::borrow::Cow<'_, str>> {
let mut words: Vec<std::borrow::Cow<str>> = Vec::new();
let mut carried = String::new();
for word in text.split_whitespace() {
if is_ref_only(word) {
carried.push_str(word);
continue;
}
words.push(match carried.is_empty() {
true => std::borrow::Cow::Borrowed(word),
false => std::borrow::Cow::Owned(format!("{}{word}", std::mem::take(&mut carried))),
});
}
if !carried.is_empty() {
if let Some(last) = words.last_mut() {
last.to_mut().push_str(&carried);
}
}
words
}
fn has_ref(text: &str, code: char) -> bool {
let mut at = 0;
while let Some(span) = next_ref(text, at) {
if span.code == code {
return true;
}
at = span.end;
}
false
}
fn unit_numbers(text: &str) -> std::collections::HashMap<String, String> {
let mut current = String::new();
let mut saved = Vec::new();
let mut numbers = std::collections::HashMap::new();
let mut at = 0;
while let Some(span) = next_ref(text, at) {
match span.code {
REF_VALUE => current = span.key.to_string(),
REF_OPEN => saved.push(current.clone()),
REF_CLOSE => current = saved.pop().unwrap_or(current),
REF_LABEL => {
numbers.insert(span.key.to_string(), current.clone());
}
_ => {}
}
at = span.end;
}
numbers
}
pub fn refs_numbered(text: &str) -> std::borrow::Cow<'_, str> {
if !has_ref(text, REF_NUMBER) {
return std::borrow::Cow::Borrowed(text);
}
let numbers = unit_numbers(text);
let mut out = String::with_capacity(text.len());
let mut at = 0;
while let Some(span) = next_ref(text, at) {
out.push_str(&text[at..span.at]);
match span.code == REF_NUMBER {
true => out.push_str(numbers.get(span.key).map_or(UNRESOLVED, String::as_str)),
false => out.push_str(&text[span.at..span.end]),
}
at = span.end;
}
out.push_str(&text[at..]);
std::borrow::Cow::Owned(out)
}
fn label_pages(lines: &[String], layout: &Layout) -> std::collections::HashMap<String, usize> {
let mut found = std::collections::HashMap::new();
for (folio, page) in folio_pages(lines, layout) {
for line in page {
let mut at = 0;
while let Some(span) = next_ref(line, at) {
if span.code == REF_LABEL {
found.insert(span.key.to_string(), folio);
}
at = span.end;
}
}
}
found
}
fn with_pagerefs(text: &str, pages: &std::collections::HashMap<String, usize>) -> String {
let mut out = String::with_capacity(text.len());
let mut at = 0;
while let Some(span) = next_ref(text, at) {
out.push_str(&text[at..span.at]);
match span.code == REF_PAGE {
true => match pages.get(span.key) {
Some(page) => out.push_str(&page.to_string()),
None => out.push_str(UNRESOLVED),
},
false => out.push_str(&text[span.at..span.end]),
}
at = span.end;
}
out.push_str(&text[at..]);
out
}
fn refs_paged<'a>(
text: &'a str,
layout: &Layout,
width_of: &(dyn Fn(&str, Face, f64) -> f64 + Sync),
) -> std::borrow::Cow<'a, str> {
if !has_ref(text, REF_PAGE) {
return std::borrow::Cow::Borrowed(text);
}
let mut pages = std::collections::HashMap::new();
for _ in 0..REF_PASSES {
let staged = with_pagerefs(text, &pages);
let lines = break_lines_measured(&staged, layout, width_of);
let found = label_pages(&lines, layout);
if found == pages {
return std::borrow::Cow::Owned(staged);
}
pages = found;
}
std::borrow::Cow::Owned(with_pagerefs(text, &pages))
}
pub const LIST_INDENT: char = '\u{14}';
pub const PAGE_BREAK: char = '\u{c}';
pub const PICTURE: char = '\u{18}';
pub const IMAGE: char = '\u{1b}';
pub fn image_span(width: &str, height: &str, path: &str) -> String {
format!("{IMAGE}{width};{height};{}{IMAGE}", base64(path.as_bytes()))
}
pub fn image_length(value: &str) -> String {
let value = value.trim();
let relative = ["textwidth", "linewidth", "columnwidth", "textheight"]
.iter()
.find_map(|name| value.split_once(&format!("\\{name}")));
if let Some((factor, _)) = relative {
let factor = factor.trim();
let factor: f64 = match factor.is_empty() {
true => 1.0,
false => match factor.parse() {
Ok(f) => f,
Err(_) => return String::new(),
},
};
return format!("f{factor}");
}
match dimen_bp(value) {
Some(points) => points.to_string(),
None => String::new(),
}
}
pub fn image_options(options: &str) -> (String, String) {
let mut width = String::new();
let mut height = String::new();
for option in options.split(',') {
let Some((key, value)) = option.split_once('=') else {
continue;
};
match key.trim() {
"width" => width = image_length(value),
"height" => height = image_length(value),
_ => {}
}
}
(width, height)
}
pub fn image_mark(width: &str, height: &str, path: &str) -> String {
format!("\n\n{}\n\n", image_span(width, height, path))
}
pub fn image_parts(line: &str) -> Option<(String, String, String)> {
let inner = line.trim();
let inner = inner.strip_prefix(CENTRE).unwrap_or(inner);
let inner = inner.strip_prefix(IMAGE)?.strip_suffix(IMAGE)?;
let (width, rest) = inner.split_once(';')?;
let (height, path) = rest.split_once(';')?;
Some((
width.to_string(),
height.to_string(),
String::from_utf8(unbase64(path)?).ok()?,
))
}
fn image_size(
width: &str,
height: &str,
image: Option<&crate::image::Image>,
layout: &Layout,
) -> Option<(f64, f64)> {
let resolve = |spec: &str| -> Option<f64> {
match spec.strip_prefix('f') {
Some(fraction) => fraction.parse::<f64>().ok().map(|f| f * layout.measure),
None => spec.parse().ok(),
}
};
match (resolve(width), resolve(height)) {
(Some(w), Some(h)) => Some((w, h)),
(w, h) => {
let image = image?;
let (nw, nh) = (f64::from(image.width), f64::from(image.height));
if nw <= 0.0 || nh <= 0.0 {
return None;
}
match (w, h) {
(Some(w), None) => Some((w, w * nh / nw)),
(None, Some(h)) => Some((h * nw / nh, h)),
_ => {
let fit = (layout.measure / nw)
.min(layout.height / nh)
.clamp(f64::MIN_POSITIVE, 1.0);
Some((nw * fit, nh * fit))
}
}
}
}
}
fn relative_x_height(file: &std::path::Path) -> Option<f64> {
let sfnt = crate::sfnt::Sfnt::parse(std::fs::read(file).ok()?).ok()?;
let x = f64::from(sfnt.x_height()?);
let em = f64::from(sfnt.head().ok()?.units_per_em.max(1));
(x > 0.0).then_some(x / em)
}
fn face_scale(
spec: Option<&str>,
file: Option<&std::path::Path>,
main: Option<&std::path::Path>,
) -> f64 {
let Some(spec) = spec else { return 1.0 };
if let Ok(factor) = spec.parse::<f64>() {
return match factor > 0.0 {
true => factor,
false => 1.0,
};
}
if !spec.eq_ignore_ascii_case("MatchLowercase") {
return 1.0;
}
let (Some(file), Some(main)) = (file, main) else {
return 1.0;
};
match (relative_x_height(main), relative_x_height(file)) {
(Some(want), Some(have)) if have > 0.0 => want / have,
_ => 1.0,
}
}
fn image_file(path: &str, near: Option<&std::path::Path>) -> Option<std::path::PathBuf> {
let named = std::path::Path::new(path);
let found = |candidate: std::path::PathBuf| candidate.is_file().then_some(candidate);
let resolve = |candidate: &std::path::Path| -> Option<std::path::PathBuf> {
if candidate.is_absolute() {
return found(candidate.to_path_buf());
}
near.and_then(|dir| found(dir.join(candidate)))
.or_else(|| found(candidate.to_path_buf()))
};
let readable = |candidate: &std::path::Path| {
matches!(
candidate
.extension()
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase)
.as_deref(),
Some("png" | "jpg" | "jpeg")
)
};
if readable(named) {
if let Some(file) = resolve(named) {
return Some(file);
}
}
for extension in ["png", "jpg", "jpeg"] {
if let Some(file) = resolve(&named.with_extension(extension)) {
return Some(file);
}
}
resolve(named)
}
struct ImageRead {
found: bool,
image: Option<std::sync::Arc<crate::image::Image>>,
}
type Images = std::collections::HashMap<String, ImageRead>;
fn images_read(lines: &[String], near: Option<&std::path::Path>) -> Images {
let mut uses = 0usize;
let mut seen = std::collections::HashSet::new();
let mut paths: Vec<String> = Vec::new();
for line in lines {
let inner = line.trim_start();
let inner = inner.strip_prefix(CENTRE).unwrap_or(inner);
if !inner.starts_with(IMAGE) {
continue;
}
let Some((_, _, path)) = image_parts(line) else {
continue;
};
uses += 1;
if seen.insert(path.clone()) {
paths.push(path);
}
}
let read = |path: &String| {
let file = image_file(path, near);
let image = file
.as_ref()
.and_then(|file| crate::image::open(file).ok())
.map(std::sync::Arc::new);
let found = file.is_some();
(path.clone(), ImageRead { found, image })
};
match uses >= PARALLEL_BLOCKS && paths.len() > 1 {
true => {
use rayon::prelude::*;
paths.par_iter().map(read).collect()
}
false => paths.iter().map(read).collect(),
}
}
fn image_remeasured(line: String, layout: &Layout, images: &Images) -> String {
let Some((width, height, path)) = image_parts(&line) else {
return line;
};
let centred = is_centred(&line);
let Some(read) = images.get(&path).filter(|read| read.found) else {
return String::new();
};
let Some((w, h)) = image_size(&width, &height, read.image.as_deref(), layout) else {
return String::new();
};
let span = image_span(&w.to_string(), &h.to_string(), &path);
match centred {
true => format!("{CENTRE}{span}"),
false => span,
}
}
pub fn picture_mark(options: &str, body: &str) -> String {
picture_paragraph(&picture_spec(options, body))
}
pub fn picture_paragraph(spec: &str) -> String {
format!("\n\n{PICTURE}{spec}{PICTURE}\n\n")
}
pub fn picture_spec(options: &str, body: &str) -> String {
let picture = crate::tikz::parse_document(
options,
body,
crate::colour::Colours::predefined(),
&crate::tikz::Estimate,
);
let (_, height) = picture.extent();
picture_contents(height, options, body)
}
fn picture_span(height: f64, options: &str, body: &str) -> String {
format!(
"{PICTURE}{}{PICTURE}",
picture_contents(height, options, body)
)
}
fn picture_contents(height: f64, options: &str, body: &str) -> String {
format!(
"{height};{}:{}",
base64(options.as_bytes()),
base64(body.as_bytes())
)
}
fn picture_parts(line: &str) -> Option<(f64, String, String)> {
let inner = line.trim();
let inner = inner.strip_prefix(CENTRE).unwrap_or(inner);
let inner = inner.strip_prefix(PICTURE)?.strip_suffix(PICTURE)?;
let (height, source) = inner.split_once(';')?;
let (options, body) = source.split_once(':')?;
Some((
height.parse().ok()?,
String::from_utf8(unbase64(options)?).ok()?,
String::from_utf8(unbase64(body)?).ok()?,
))
}
fn is_image_para(para: &str) -> bool {
image_parts(para).is_some()
}
fn is_block_para(para: &str) -> bool {
is_picture_para(para) || is_image_para(para)
}
fn is_picture_para(para: &str) -> bool {
picture_parts(para).is_some()
}
fn is_centred(line: &str) -> bool {
line.trim_start().starts_with(CENTRE)
}
fn picture_remeasured(line: String, metrics: &dyn crate::tikz::Metrics) -> String {
let Some((_, options, body)) = picture_parts(&line) else {
return line;
};
let picture = crate::tikz::parse_document(
&options,
&body,
crate::colour::Colours::predefined(),
metrics,
);
let (_, height) = picture.extent();
let span = picture_span(height, &options, &body);
match is_centred(&line) {
true => format!("{CENTRE}{span}"),
false => span,
}
}
const BASE64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const UNBASE64: [u8; 128] = {
let mut table = [64u8; 128];
let mut at = 0;
while at < 64 {
table[BASE64[at] as usize] = at as u8;
at += 1;
}
table
};
fn base64(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
for group in bytes.chunks(3) {
let packed = group
.iter()
.enumerate()
.fold(0u32, |acc, (at, b)| acc | (*b as u32) << (16 - 8 * at));
for at in 0..4 {
out.push(match at <= group.len() {
true => BASE64[(packed >> (18 - 6 * at)) as usize & 0x3f] as char,
false => '=',
});
}
}
out
}
fn unbase64(text: &str) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(text.len() / 4 * 3);
let mut packed = 0u32;
let mut held = 0u32;
for ch in text.chars() {
if ch == '=' {
break;
}
let six = match UNBASE64.get(ch as usize) {
Some(&six) if six < 64 => u32::from(six),
_ => return None,
};
packed = packed << 6 | six;
held += 6;
if held >= 8 {
held -= 8;
out.push((packed >> held) as u8);
}
}
Some(out)
}
pub const MARKERS: &[(char, bool)] = &[
('\u{1}', false),
('\u{2}', false),
('\u{3}', false),
(LISTING_BREAK, false),
(PAGE_BREAK, false),
(CENTRE, false),
(CENTRE_END, false),
(VERTICAL_SPACE, false),
(JUSTIFY, false),
(FACE_PUSH, true),
(FACE_POP, false),
(SIZE_PUSH, false),
(SIZE_POP, false),
(BREAK_PARAMS, false),
(IMAGE, false),
(TABLE_CELL, false),
(TABLE_ROW, false),
(LIST_INDENT, true),
(TABLE_MARK, true),
(LONGTABLE, true),
(TOC, true),
(REF, true),
(PICTURE, true),
];
pub const LISTING_BREAK: char = '\u{b}';
fn listing_lines(para: &str) -> Option<impl Iterator<Item = &str>> {
para.contains(LISTING_BREAK)
.then(|| para.split_terminator(LISTING_BREAK))
}
pub const TABLE_CELL: char = '\u{1f}';
pub const TABLE_ROW: char = '\u{1e}';
pub const TABLE_MARK: char = '\u{1d}';
pub const LONGTABLE: char = '\u{16}';
const LT_HEAD: char = 'h';
const LT_REPEAT: char = 'H';
const LT_FOOT: char = 'f';
const LT_ROW: char = 'r';
const LT_CONT: char = 'c';
fn longtable_code(line: &str) -> Option<char> {
line.strip_prefix(LONGTABLE)?.chars().next()
}
fn without_longtable(line: &str) -> &str {
let Some(rest) = line.strip_prefix(LONGTABLE) else {
return line;
};
let mut chars = rest.chars();
chars.next();
chars.as_str()
}
pub const RULE_TOP: char = 't';
pub const RULE_MID: char = 'm';
pub const RULE_BOTTOM: char = 'b';
pub const HEAD_END: char = 'h';
pub const FIRST_HEAD_END: char = 'H';
pub const FOOT_END: char = 'f';
pub const LAST_FOOT_END: char = 'F';
#[derive(Clone)]
enum Entry {
Rule(char),
Row(Vec<String>),
}
#[derive(Default)]
struct Sections {
first_head: Vec<Entry>,
head: Vec<Entry>,
foot: Vec<Entry>,
last_foot: Vec<Entry>,
body: Vec<Entry>,
}
impl Sections {
fn opening_head(&self) -> &[Entry] {
match self.first_head.is_empty() {
true => &self.head,
false => &self.first_head,
}
}
fn closing_foot(&self) -> &[Entry] {
match self.last_foot.is_empty() {
true => &self.foot,
false => &self.last_foot,
}
}
}
fn table_sections(para: &str) -> Sections {
let mut sections = Sections::default();
let mut pending: Vec<Entry> = Vec::new();
let mut row: Vec<String> = Vec::new();
let mut cell = String::new();
let finish = |cell: &mut String| -> String {
let taken = std::mem::take(cell);
taken.split_whitespace().collect::<Vec<&str>>().join(" ")
};
let mut chars = para.chars();
while let Some(ch) = chars.next() {
match ch {
TABLE_MARK => {
let code = chars.next().unwrap_or(RULE_MID);
let section = match code {
FIRST_HEAD_END => &mut sections.first_head,
HEAD_END => &mut sections.head,
FOOT_END => &mut sections.foot,
LAST_FOOT_END => &mut sections.last_foot,
rule => {
pending.push(Entry::Rule(rule));
continue;
}
};
if section.is_empty() {
*section = std::mem::take(&mut pending);
}
}
TABLE_CELL => row.push(finish(&mut cell)),
TABLE_ROW => {
row.push(finish(&mut cell));
if row.iter().any(|c| !c.is_empty()) {
pending.push(Entry::Row(std::mem::take(&mut row)));
}
row.clear();
}
c => cell.push(c),
}
}
sections.body = pending;
sections
}
fn table_entries(para: &str) -> Vec<Entry> {
let sections = table_sections(para);
sections
.opening_head()
.iter()
.chain(§ions.body)
.chain(sections.closing_foot())
.cloned()
.collect()
}
fn table_lines(
para: &str,
layout: &Layout,
colours: &[Spec],
faces: &[Face],
sizes: &[TypeSize],
width_of: &dyn Fn(&str, Face, f64) -> f64,
out: &mut Vec<String>,
) {
let sections = table_sections(para);
let entries: Vec<&Entry> = sections
.first_head
.iter()
.chain(§ions.head)
.chain(§ions.foot)
.chain(§ions.last_foot)
.chain(§ions.body)
.collect();
let cols = entries
.iter()
.filter_map(|e| match e {
Entry::Row(cells) => Some(cells.len()),
Entry::Rule(_) => None,
})
.max()
.unwrap_or(0);
if cols == 0 {
return;
}
let space = width_of(" ", current_face(faces), current_size(sizes).size).max(f64::MIN_POSITIVE);
let gutter = 2.0 * space;
let measure = |text: &str| -> f64 {
let (mut c, mut f, mut s) = (colours.to_vec(), faces.to_vec(), sizes.to_vec());
styled_runs(text, &mut c, &mut f, &mut s)
.iter()
.map(|(plain, _, face, ts)| width_of(plain, *face, ts.size))
.sum()
};
let mut natural = vec![0.0f64; cols];
for entry in &entries {
if let Entry::Row(cells) = *entry {
for (j, cell) in cells.iter().enumerate() {
natural[j] = natural[j].max(measure(cell));
}
}
}
let room = (layout.measure - gutter * (cols - 1) as f64).max(space);
let asked: f64 = natural.iter().sum();
let fair = room / cols as f64;
let over: f64 = natural.iter().filter(|w| **w > fair).sum();
let under: f64 = natural.iter().filter(|w| **w <= fair).sum();
let widths: Vec<f64> = match asked <= room || over <= 0.0 {
true => natural.clone(),
false => natural
.iter()
.map(|w| match *w > fair {
true => ((room - under) * w / over).max(4.0 * space),
false => *w,
})
.collect(),
};
let mut starts = vec![0.0f64; cols];
for j in 1..cols {
starts[j] = starts[j - 1] + widths[j - 1] + gutter;
}
let span = starts[cols - 1] + widths[cols - 1];
let set = |entry: &Entry| -> Vec<String> {
let cells = match entry {
Entry::Rule(kind) => return vec![rule_line(*kind, span, space)],
Entry::Row(cells) => cells,
};
let wrapped: Vec<Vec<String>> = cells
.iter()
.enumerate()
.map(|(j, cell)| wrap_cell(cell, widths[j], colours, faces, sizes, width_of))
.collect();
let height = wrapped.iter().map(Vec::len).max().unwrap_or(0);
let mut lines = Vec::with_capacity(height);
for k in 0..height {
let mut line = String::new();
let mut at = 0.0f64;
for (j, fragments) in wrapped.iter().enumerate() {
let Some(fragment) = fragments.get(k).filter(|f| !f.is_empty()) else {
continue;
};
let want = ((starts[j] - at) / space).round() as i64;
for _ in 0..want.max(i64::from(j > 0)) {
line.push(' ');
at += space;
}
line.push_str(fragment);
at += measure(fragment);
}
lines.push(line);
}
lines
};
let mut emit = |entries: &[Entry], code: char, rows: bool| {
for entry in entries {
for (n, line) in set(entry).into_iter().enumerate() {
let mut marked = String::from(LONGTABLE);
marked.push(match rows && n > 0 {
true => LT_CONT,
false => code,
});
marked.push_str(&line);
out.push(marked);
}
}
};
if !sections.first_head.is_empty() {
emit(§ions.head, LT_REPEAT, false);
}
emit(§ions.foot, LT_FOOT, false);
let opening = match sections.first_head.is_empty() {
true => LT_HEAD,
false => LT_ROW,
};
emit(sections.opening_head(), opening, opening == LT_ROW);
emit(§ions.body, LT_ROW, true);
emit(sections.closing_foot(), LT_ROW, true);
}
fn rule_line(kind: char, span: f64, space: f64) -> String {
let mut line = String::from(TABLE_MARK);
line.push(kind);
let mut at = 0.0;
while at + space <= span {
line.push(' ');
at += space;
}
line
}
fn wrap_cell(
cell: &str,
width: f64,
colours: &[Spec],
faces: &[Face],
sizes: &[TypeSize],
width_of: &dyn Fn(&str, Face, f64) -> f64,
) -> Vec<String> {
let (mut c, mut f, mut s) = (colours.to_vec(), faces.to_vec(), sizes.to_vec());
let space = width_of(" ", current_face(&f), current_size(&s).size);
let mut open: Vec<(String, char)> = Vec::new();
let mut fragments = Vec::new();
let mut line = String::new();
let mut at = 0.0f64;
for word in cell.split(' ').filter(|w| !w.is_empty()) {
let cost: f64 = styled_runs(word, &mut c, &mut f, &mut s)
.iter()
.map(|(plain, _, face, ts)| width_of(plain, *face, ts.size))
.sum();
let need = match line.is_empty() {
true => cost,
false => at + space + cost,
};
if !line.is_empty() && need > width {
for (_, close) in open.iter().rev() {
line.push(*close);
}
fragments.push(std::mem::take(&mut line));
for (opener, _) in &open {
line.push_str(opener);
}
at = cost;
} else {
if !line.is_empty() {
line.push(' ');
}
at = need;
}
line.push_str(word);
absorb(word, &mut open);
}
if !line.is_empty() {
for (_, close) in open.iter().rev() {
line.push(*close);
}
fragments.push(line);
}
fragments
}
fn absorb(word: &str, open: &mut Vec<(String, char)>) {
fn close(open: &mut Vec<(String, char)>, kind: char) {
if let Some(at) = open.iter().rposition(|(_, c)| *c == kind) {
open.remove(at);
}
}
let mut chars = word.chars();
while let Some(ch) = chars.next() {
match ch {
'\u{1}' => {
let mut spec = String::from('\u{1}');
for c in chars.by_ref() {
spec.push(c);
if c == '\u{2}' {
break;
}
}
open.push((spec, '\u{3}'));
}
'\u{3}' => close(open, '\u{3}'),
FACE_PUSH => {
let code = chars.next().unwrap_or_else(|| Face::MAIN.code());
open.push((format!("{FACE_PUSH}{code}"), FACE_POP));
}
FACE_POP => close(open, FACE_POP),
_ => {}
}
}
}
pub const CENTRE: char = '\u{e}';
pub const CENTRE_END: char = '\u{f}';
pub const JUSTIFY: char = '\u{13}';
pub const VERTICAL_SPACE: char = '\u{10}';
type Spec = (String, String, String);
type StyledRun = (String, Spec, Face, TypeSize);
const DEFAULT_COLOUR: (&str, &str, &str) = ("0", "0", "0");
fn styled_runs(
line: &str,
stack: &mut Vec<Spec>,
faces: &mut Vec<Face>,
sizes: &mut Vec<TypeSize>,
) -> Vec<StyledRun> {
let mut drawn = Drawn {
stacks: Live {
colours: stack,
faces,
sizes,
},
runs: Vec::new(),
text: String::new(),
};
walk_styles(line, &mut drawn);
drawn.flush();
drawn.runs
}
trait StyleStacks {
fn text(&mut self, _ch: char) {}
fn math(&mut self, _spec: &str) {}
fn push_colour(&mut self, spec: Option<Spec>);
fn pop_colour(&mut self);
fn push_face(&mut self, code: char);
fn pop_face(&mut self);
fn push_size(&mut self, size: Option<TypeSize>);
fn pop_size(&mut self);
}
fn walk_styles(line: &str, stacks: &mut impl StyleStacks) {
let mut chars = line.chars();
while let Some(ch) = chars.next() {
match ch {
FACE_PUSH => {
let code = chars.next().unwrap_or_else(|| Face::MAIN.code());
stacks.push_face(code);
}
FACE_POP => stacks.pop_face(),
SIZE_PUSH => {
let mut spec = String::new();
for c in chars.by_ref() {
if c == SIZE_PUSH {
break;
}
spec.push(c);
}
stacks
.push_size(size_spec(&spec).map(|(size, leading)| TypeSize { size, leading }));
}
SIZE_POP => stacks.pop_size(),
'\u{1}' => {
let mut spec = String::new();
for c in chars.by_ref() {
if c == '\u{2}' {
break;
}
spec.push(c);
}
if crate::math::is_setting(&spec) {
for c in chars.by_ref() {
if c == '\u{3}' {
break;
}
}
stacks.math(&spec);
continue;
}
let p: Vec<&str> = spec.split(',').collect();
stacks.push_colour(match p.len() == 3 {
true => Some((p[0].to_string(), p[1].to_string(), p[2].to_string())),
false => None,
});
}
'\u{3}' => stacks.pop_colour(),
c => stacks.text(c),
}
}
}
fn current_colour(colours: &[Spec]) -> Spec {
colours.last().cloned().unwrap_or_else(|| {
(
DEFAULT_COLOUR.0.to_string(),
DEFAULT_COLOUR.1.to_string(),
DEFAULT_COLOUR.2.to_string(),
)
})
}
struct Live<'a> {
colours: &'a mut Vec<Spec>,
faces: &'a mut Vec<Face>,
sizes: &'a mut Vec<TypeSize>,
}
impl StyleStacks for Live<'_> {
fn push_colour(&mut self, spec: Option<Spec>) {
let pushed = spec.unwrap_or_else(|| current_colour(self.colours));
self.colours.push(pushed);
}
fn pop_colour(&mut self) {
if self.colours.len() > 1 {
self.colours.pop();
}
}
fn push_face(&mut self, code: char) {
self.faces
.push(Face::from_code(code, current_face(self.faces)));
}
fn pop_face(&mut self) {
if self.faces.len() > 1 {
self.faces.pop();
}
}
fn push_size(&mut self, size: Option<TypeSize>) {
let pushed = size.unwrap_or_else(|| current_size(self.sizes));
self.sizes.push(pushed);
}
fn pop_size(&mut self) {
if self.sizes.len() > 1 {
self.sizes.pop();
}
}
}
struct Drawn<'a> {
stacks: Live<'a>,
runs: Vec<StyledRun>,
text: String,
}
impl Drawn<'_> {
fn flush(&mut self) {
if !self.text.is_empty() {
let text = std::mem::take(&mut self.text);
let run = self.styled(text);
self.runs.push(run);
}
}
fn styled(&self, text: String) -> StyledRun {
(
text,
current_colour(self.stacks.colours),
current_face(self.stacks.faces),
current_size(self.stacks.sizes),
)
}
}
impl StyleStacks for Drawn<'_> {
fn text(&mut self, ch: char) {
self.text.push(ch);
}
fn math(&mut self, spec: &str) {
self.flush();
let run = self.styled(crate::math::run(spec));
self.runs.push(run);
}
fn push_colour(&mut self, spec: Option<Spec>) {
self.flush();
self.stacks.push_colour(spec);
}
fn pop_colour(&mut self) {
self.flush();
self.stacks.pop_colour();
}
fn push_face(&mut self, code: char) {
self.flush();
self.stacks.push_face(code);
}
fn pop_face(&mut self) {
self.flush();
self.stacks.pop_face();
}
fn push_size(&mut self, size: Option<TypeSize>) {
self.flush();
self.stacks.push_size(size);
}
fn pop_size(&mut self) {
self.flush();
self.stacks.pop_size();
}
}
struct StackEffect<E> {
pops: usize,
pushes: Vec<E>,
moved: bool,
}
impl<E> Default for StackEffect<E> {
fn default() -> Self {
StackEffect {
pops: 0,
pushes: Vec::new(),
moved: false,
}
}
}
impl<E> StackEffect<E> {
fn push(&mut self, entry: E) {
self.moved = true;
self.pushes.push(entry);
}
fn pop(&mut self) {
self.moved = true;
if self.pushes.pop().is_none() {
self.pops += 1;
}
}
fn apply<T>(&self, stack: &mut Vec<T>, make: impl Fn(&E, &T) -> T) {
if !self.moved {
return;
}
stack.truncate(stack.len().saturating_sub(self.pops).max(1));
let Some(below) = stack.last() else { return };
let pushed: Vec<T> = self.pushes.iter().map(|entry| make(entry, below)).collect();
stack.extend(pushed);
}
fn applies_to<T>(&self, stack: &[T]) -> bool {
!self.moved || !stack.is_empty()
}
}
#[derive(Clone)]
enum Pushed<T> {
Named(T),
InForce,
}
impl<T: Clone> Pushed<T> {
fn make(&self, below: &T) -> T {
match self {
Pushed::Named(value) => value.clone(),
Pushed::InForce => below.clone(),
}
}
}
#[derive(Clone, Copy)]
struct FaceMap([u8; 12]);
impl FaceMap {
const IDENTITY: FaceMap = FaceMap([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
fn then(self, code: char) -> FaceMap {
FaceMap(
self.0
.map(|at| Face::from_code(code, Face::ALL[at as usize]).index() as u8),
)
}
fn of(self, face: Face) -> Face {
Face::ALL[self.0[face.index()] as usize]
}
}
#[derive(Default)]
struct StyleEffect {
colours: StackEffect<Pushed<Spec>>,
faces: StackEffect<FaceMap>,
sizes: StackEffect<Pushed<TypeSize>>,
}
impl StyleEffect {
fn apply(
&self,
colours: &mut Vec<Spec>,
faces: &mut Vec<Face>,
sizes: &mut Vec<TypeSize>,
) -> bool {
if !(self.colours.applies_to(colours)
&& self.faces.applies_to(faces)
&& self.sizes.applies_to(sizes))
{
return false;
}
self.colours.apply(colours, Pushed::make);
self.faces.apply(faces, |map, below| map.of(*below));
self.sizes.apply(sizes, Pushed::make);
true
}
}
impl StyleStacks for StyleEffect {
fn push_colour(&mut self, spec: Option<Spec>) {
let pushed = match spec {
Some(spec) => Pushed::Named(spec),
None => self
.colours
.pushes
.last()
.cloned()
.unwrap_or(Pushed::InForce),
};
self.colours.push(pushed);
}
fn pop_colour(&mut self) {
self.colours.pop();
}
fn push_face(&mut self, code: char) {
let under = self
.faces
.pushes
.last()
.copied()
.unwrap_or(FaceMap::IDENTITY);
self.faces.push(under.then(code));
}
fn pop_face(&mut self) {
self.faces.pop();
}
fn push_size(&mut self, size: Option<TypeSize>) {
let pushed = match size {
Some(size) => Pushed::Named(size),
None => self.sizes.pushes.last().cloned().unwrap_or(Pushed::InForce),
};
self.sizes.push(pushed);
}
fn pop_size(&mut self) {
self.sizes.pop();
}
}
fn current_face(faces: &[Face]) -> Face {
faces.last().copied().unwrap_or_default()
}
fn word_width(
word: &str,
colours: &mut Vec<Spec>,
faces: &mut Vec<Face>,
sizes: &mut Vec<TypeSize>,
width_of: &dyn Fn(&str, Face, f64) -> f64,
) -> f64 {
if !word.contains(STACK_MARKERS) {
return width_of(word, current_face(faces), current_size(sizes).size);
}
styled_runs(word, colours, faces, sizes)
.iter()
.map(|(text, _, face, ts)| width_of(text, *face, ts.size))
.sum()
}
pub fn find_family(family: &str) -> Option<std::path::PathBuf> {
static SEEN: std::sync::OnceLock<
std::sync::Mutex<std::collections::HashMap<String, Option<std::path::PathBuf>>>,
> = std::sync::OnceLock::new();
let seen = SEEN.get_or_init(Default::default);
if let Some(hit) = seen.lock().ok().and_then(|m| m.get(family).cloned()) {
return hit;
}
let found = find_family_uncached(family);
if let Ok(mut m) = seen.lock() {
m.insert(family.to_string(), found.clone());
}
found
}
fn find_family_uncached(family: &str) -> Option<std::path::PathBuf> {
if let Ok(out) = std::process::Command::new("fc-match")
.args(["-f", "%{file}", family])
.output()
{
if out.status.success() {
let p = String::from_utf8_lossy(&out.stdout).trim().to_string();
let path = std::path::Path::new(&p);
if path.exists() && file_matches(path, family) {
return Some(path.to_path_buf());
}
}
}
let wanted = normalise(family);
for dir in [
"/System/Library/Fonts",
"/System/Library/Fonts/Supplemental",
"/Library/Fonts",
"/usr/share/fonts",
"/usr/local/share/fonts",
] {
let Ok(entries) = std::fs::read_dir(dir) else {
continue;
};
for entry in entries.flatten() {
let path = entry.path();
let ext = path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_ascii_lowercase();
if ext != "ttf" && ext != "otf" {
continue;
}
if normalise(&path.file_stem()?.to_string_lossy()).starts_with(&wanted) {
return Some(path);
}
}
}
None
}
fn file_matches(path: &std::path::Path, family: &str) -> bool {
path.file_stem()
.map(|s| normalise(&s.to_string_lossy()).starts_with(&normalise(family)))
.unwrap_or(false)
}
fn normalise(name: &str) -> String {
name.chars()
.filter(|c| c.is_ascii_alphanumeric())
.collect::<String>()
.to_ascii_lowercase()
}
pub fn embed_family(family: &str) -> Option<crate::pdf::Font> {
embed_file(&find_family(family)?)
}
pub fn embed_file(path: &std::path::Path) -> Option<crate::pdf::Font> {
let bytes = std::fs::read(path).ok()?;
let sfnt = crate::sfnt::Sfnt::parse(bytes.clone()).ok()?;
if sfnt.is_cff() {
return None;
}
let head = sfnt.head().ok()?;
let hhea = sfnt.hhea().ok()?;
let cmap = sfnt.cmap().ok()?;
let advances = sfnt.advance_widths().ok()?;
let upem = f64::from(head.units_per_em.max(1));
let scale = |v: f64| (v * 1000.0 / upem).round() as i64;
let mut widths = Vec::with_capacity(224);
for code in 32u32..=255 {
let character = u8::try_from(code).ok().and_then(winansi_unicode);
let gid = character
.and_then(|c| cmap.get(&(c as u32)))
.copied()
.unwrap_or(0) as usize;
let adv = advances
.get(gid)
.or_else(|| advances.last())
.copied()
.unwrap_or(0);
widths.push(scale(f64::from(adv)));
}
Some(crate::pdf::Font::TrueType {
name: path.file_stem()?.to_string_lossy().replace(' ', ""),
bytes,
widths,
bbox: [
scale(f64::from(head.x_min)),
scale(f64::from(head.y_min)),
scale(f64::from(head.x_max)),
scale(f64::from(head.y_max)),
],
ascent: scale(f64::from(hhea.ascender)),
descent: scale(f64::from(hhea.descender)),
})
}
#[cfg(test)]
mod font_file_tests {
use super::FontFile;
const REAL: &str = "\n Path=/private/tmp/build/scifi2/docs/.fonts/,\n Extension=.ttf,\n RawFeature={fallback=symfb},\n UprightFont=Arimo-VF,\n UprightFeatures={RawFeature={axis={wght=400}}},\n BoldFont=Arimo-VF,\n BoldFeatures={RawFeature={axis={wght=700}}},\n";
#[test]
fn the_options_a_document_ships_its_font_with() {
let spec = FontFile::parse(REAL);
assert_eq!(spec.upright.as_deref(), Some("Arimo-VF"));
assert_eq!(spec.extension.as_deref(), Some(".ttf"));
assert_eq!(
spec.path.as_deref(),
Some("/private/tmp/build/scifi2/docs/.fonts/")
);
}
#[test]
fn the_face_files_are_read_from_the_same_options() {
let spec = FontFile::parse(REAL);
assert_eq!(spec.bold.as_deref(), Some("Arimo-VF"));
assert_eq!(spec.italic.as_deref(), None, "this book names no italic");
let both = FontFile::parse("Extension=.ttf,UprightFont=A,ItalicFont=A-Italic");
assert_eq!(both.italic.as_deref(), Some("A-Italic"));
assert_eq!(
both.resolve_face(
super::Face {
bold: true,
..super::Face::MAIN
},
None
),
None
);
}
#[test]
fn a_value_holding_commas_does_not_tear_the_keys_after_it() {
let spec = FontFile::parse("Extension=.ttf,Numbers={Proportional,Lining},UprightFont=X-VF");
assert_eq!(spec.upright.as_deref(), Some("X-VF"));
assert_eq!(spec.extension.as_deref(), Some(".ttf"));
}
#[test]
fn a_stale_path_is_retried_beside_the_document() {
let dir = std::env::temp_dir().join(format!("texrs_ff_{}", std::process::id()));
let fonts = dir.join(".fonts");
std::fs::create_dir_all(&fonts).expect("mkdir");
let file = fonts.join("Arimo-VF.ttf");
std::fs::write(&file, b"not really a font").expect("write");
let spec =
FontFile::parse("Path=/gone/for/good/.fonts/,Extension=.ttf,UprightFont=Arimo-VF");
assert_eq!(spec.resolve(Some(&dir)), Some(file.clone()));
assert_eq!(spec.resolve(None), None);
let direct = format!(
"Path={}/,Extension=.ttf,UprightFont=Arimo-VF",
fonts.display()
);
assert_eq!(FontFile::parse(&direct).resolve(None), Some(file));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_mandatory_argument_ending_in_a_font_extension_names_the_file_itself() {
let mut spec = FontFile::parse("Path=/somewhere/.fonts/");
spec.absorb_filename("Arimo-VF.ttf");
assert_eq!(spec.upright.as_deref(), Some("Arimo-VF"));
assert_eq!(spec.extension.as_deref(), Some(".ttf"));
assert_eq!(spec.path.as_deref(), Some("/somewhere/.fonts/"));
let mut upper = FontFile::default();
upper.absorb_filename("Arimo-VF.TTF");
assert_eq!(upper.upright.as_deref(), Some("Arimo-VF"));
assert_eq!(upper.extension.as_deref(), Some(".TTF"));
for spelt in ["A.otf", "A.OTF", "A.ttc", "A.otc"] {
let mut one = FontFile::default();
one.absorb_filename(spelt);
assert_eq!(one.upright.as_deref(), Some("A"), "{spelt} is a filename");
}
}
#[test]
fn a_family_name_is_not_read_as_a_file_and_explicit_options_win() {
for family in ["Arimo", "NoSuchFontExistsAnywhere", "Dr. Sugiyama", ".ttf"] {
assert_eq!(super::split_font_filename(family), None, "{family}");
assert_eq!(super::font_family_name(family), family);
let mut spec = FontFile::default();
spec.absorb_filename(family);
assert_eq!(spec.upright, None, "{family} named no file");
assert_eq!(spec.extension, None, "{family} named no extension");
}
let mut both = FontFile::parse("Path=/f/,Extension=.otf,UprightFont=Arimo-Regular");
both.absorb_filename("Arimo-VF.ttf");
assert_eq!(both.upright.as_deref(), Some("Arimo-Regular"));
assert_eq!(both.extension.as_deref(), Some(".otf"));
assert_eq!(super::font_family_name("Arimo-VF.ttf"), "Arimo-VF");
}
#[test]
fn the_filename_spelling_resolves_to_the_file_beside_the_document() {
let dir = std::env::temp_dir().join(format!("texrs_ffn_{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("mkdir");
let file = dir.join("Arimo-VF.ttf");
std::fs::write(&file, b"not really a font").expect("write");
let mut spec = FontFile::parse(&format!("Path={}/", dir.display()));
spec.absorb_filename("Arimo-VF.ttf");
assert_eq!(spec.resolve(None), Some(file));
let mut missing = FontFile::parse(&format!("Path={}/", dir.display()));
missing.absorb_filename("NotThere-VF.ttf");
assert_eq!(missing.resolve(None), None, "a missing file falls back");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn options_that_name_no_file_resolve_to_nothing() {
assert_eq!(
FontFile::parse("").resolve(Some(std::path::Path::new("/tmp"))),
None
);
}
}
#[cfg(test)]
mod prescan_tests {
use super::*;
struct Rng(u64);
impl Rng {
fn below(&mut self, n: usize) -> usize {
self.0 ^= self.0 >> 12;
self.0 ^= self.0 << 25;
self.0 ^= self.0 >> 27;
(self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 33) as usize % n
}
}
fn piece(rng: &mut Rng) -> String {
match rng.below(16) {
0 => format!("\u{1}{},0,{}\u{2}", rng.below(3), rng.below(3)),
1 => "\u{1}bad\u{2}".to_string(),
2 | 3 => "\u{3}".to_string(),
4 | 5 => {
let codes = [
'r', 's', 'm', 'b', 'i', 'n', 'B', 'j', 'S', 'J', 'k', 'M', 'K', '?',
];
format!("{FACE_PUSH}{}", codes[rng.below(codes.len())])
}
6 | 7 => FACE_POP.to_string(),
8 => format!(
"{SIZE_PUSH}{};{}{SIZE_PUSH}",
8 + rng.below(10),
10 + rng.below(10)
),
9 => format!("{SIZE_PUSH}junk{SIZE_PUSH}"),
10 => SIZE_POP.to_string(),
11 => CENTRE.to_string(),
12 => CENTRE_END.to_string(),
13 => format!("{LIST_INDENT}{}", rng.below(4)),
_ => ["word", "x", "abc"][rng.below(3)].to_string(),
}
}
fn document(seed: u64, paragraphs: usize) -> Vec<String> {
let mut rng = Rng(seed);
(0..paragraphs)
.map(|_| {
let mut para = String::new();
for _ in 0..rng.below(24) {
para.push_str(&piece(&mut rng));
match rng.below(5) {
0 => para.push(' '),
1 if rng.below(8) == 0 => para.push(PAGE_BREAK),
_ => {}
}
}
para
})
.collect()
}
fn seeded(colours: Vec<Spec>, layout: &Layout) -> ParagraphStart {
ParagraphStart {
colours,
faces: vec![Face::MAIN],
sizes: vec![TypeSize {
size: layout.size,
leading: layout.leading,
}],
centred: false,
depth: 0,
}
}
#[test]
fn paragraph_starts_match_the_serial_walk() {
let layout = Layout::default();
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(4)
.build()
.unwrap();
for seed in 1..=40u64 {
let doc = document(seed, 50 + seed as usize * 7);
let paras: Vec<&str> = doc.iter().map(String::as_str).collect();
for colours in [Vec::new(), vec![current_colour(&[])]] {
let start = seeded(colours, &layout);
let serial = paragraph_starts_serial(¶s, &layout, start.clone());
let parallel = pool.install(|| paragraph_starts(¶s, &layout, start));
assert_eq!(parallel, serial, "seed {seed}");
}
}
}
#[test]
fn page_starts_match_the_serial_walk() {
let layout = Layout::default();
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(4)
.build()
.unwrap();
for seed in 1..=40u64 {
let doc = document(seed, 200);
let mut rng = Rng(seed ^ 0x9E37_79B9);
let mut chunks: Vec<Vec<&str>> = Vec::new();
let mut lines = doc.iter().map(String::as_str);
while let Some(first) = lines.next() {
let mut page = vec![first];
page.extend(lines.by_ref().take(rng.below(12)));
chunks.push(page);
}
let start = seeded(vec![current_colour(&[])], &layout);
let serial = page_starts_serial(
&chunks,
start.colours.clone(),
start.faces.clone(),
start.sizes.clone(),
);
let parallel =
pool.install(|| page_starts(&chunks, start.colours, start.faces, start.sizes));
assert!(chunks.len() >= PARALLEL_PAGES);
assert_eq!(parallel, serial, "seed {seed}");
}
}
#[test]
fn face_map_is_the_codes_applied_in_turn() {
let codes = ['s', 'b', '?', 'n', 'i', 'M', 'r'];
for below in Face::ALL {
let mut map = FaceMap::IDENTITY;
let mut face = below;
for code in codes {
map = map.then(code);
face = Face::from_code(code, face);
assert_eq!(map.of(below), face);
}
}
}
}