pub mod align;
pub mod font;
pub mod mlist;
pub mod noad;
pub mod number;
pub mod parse;
pub mod set;
use crate::expand::{Engine, TexError};
use crate::ir::Cmd;
use crate::lexer::Lexer;
use crate::token::Token;
use noad::{Noad, Scaled, Style, DISPLAY_STYLE, TEXT_STYLE};
use set::Setting;
pub const SETTING: char = 'M';
pub const RUN: char = '\u{17}';
pub fn pt(sp: Scaled) -> f64 {
sp as f64 / 65536.0
}
pub fn is_setting(spec: &str) -> bool {
spec.starts_with(SETTING)
}
pub fn run(spec: &str) -> String {
format!("{RUN}{spec}")
}
pub fn run_setting(text: &str) -> Option<Setting> {
let rest = text.strip_prefix(RUN)?;
set::decode(rest.strip_prefix(SETTING)?)
}
pub fn run_width(text: &str) -> Option<f64> {
run_setting(text).map(|s| pt(s.width))
}
pub fn glyphs(s: &Setting) -> Vec<(String, f64, f64, f64)> {
s.glyphs
.iter()
.map(|g| (g.ch.to_string(), pt(g.x), pt(g.y), pt(g.size)))
.collect()
}
pub fn rules(s: &Setting) -> Vec<(f64, f64, f64, f64)> {
s.rules
.iter()
.map(|r| (pt(r.x), pt(r.y), pt(r.width), pt(r.height)))
.collect()
}
pub fn set_mlist(list: &[Noad], style: Style, size: f64) -> Option<Setting> {
let fonts = fonts_at(size)?;
let b = mlist::set(fonts, list, style);
Some(set::flatten(&b, &|i| set::font_size(fonts, i)))
}
fn set_formula(fonts: &font::MathFonts, list: &[Noad], style: Style, display: bool) -> Setting {
let b = match display {
true => mlist::set(fonts, list, style),
false => mlist::set_with_penalties(fonts, list, style),
};
set::flatten(&b, &|i| set::font_size(fonts, i))
}
pub fn set_display(
f: &parse::Formula,
style: Style,
size: f64,
environment: &str,
measure: f64,
) -> Option<Vec<Setting>> {
Some(display_rows(
fonts_at(size)?,
f,
&eqno(f),
f.number_left,
style,
environment,
measure,
))
}
fn eqno(f: &parse::Formula) -> Vec<(usize, Vec<Noad>)> {
f.number
.iter()
.map(|n| (f.rows.len().saturating_sub(1), n.clone()))
.collect()
}
#[allow(clippy::too_many_arguments)]
fn display_rows(
fonts: &font::MathFonts,
f: &parse::Formula,
numbers: &[(usize, Vec<Noad>)],
left: bool,
style: Style,
environment: &str,
measure: f64,
) -> Vec<Setting> {
let widest = f.rows.iter().map(|r| r.len()).max().unwrap_or(1);
let columns = align::columns(environment, widest);
let mut boxes = align::rows(fonts, &f.rows, style, &columns);
let z = (measure * 65536.0).round() as noad::Scaled;
for (row, number) in numbers {
let Some(at) = boxes.get_mut(*row) else {
continue;
};
let a = mlist::set(fonts, number, TEXT_STYLE);
let body = std::mem::take(at);
*at = align::numbered(fonts, body, a, z, left);
}
boxes
.iter()
.map(|b| set::flatten(b, &|i| set::font_size(fonts, i)))
.collect()
}
pub fn parse_formula(source: &str) -> Result<Vec<Noad>, TexError> {
Ok(read_formula(source)?.flat())
}
pub fn read_formula(source: &str) -> Result<parse::Formula, TexError> {
let mut eng = Engine::new();
eng.cats.set('{', crate::catcode::Cat::BeginGroup);
eng.cats.set('}', crate::catcode::Cat::EndGroup);
eng.cats.set('&', crate::catcode::Cat::AlignTab);
let mut lx = Lexer::new(source);
parse::formula(&mut eng, &mut lx, parse::Stop::MathShift)
}
fn fonts_at(size: f64) -> Option<&'static font::MathFonts> {
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
static CACHE: OnceLock<Mutex<HashMap<u64, Option<&'static font::MathFonts>>>> = OnceLock::new();
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
let key = size.to_bits();
let mut held = cache.lock().ok()?;
if let Some(hit) = held.get(&key) {
return *hit;
}
let loaded = font::MathFonts::load(size);
let answer = match loaded.usable() {
true => Some(&*Box::leak(Box::new(loaded))),
false => None,
};
held.insert(key, answer);
answer
}
const ENVIRONMENTS: &[&str] = &[
"equation",
"equation*",
"displaymath",
"align",
"align*",
"aligned",
"alignat",
"alignat*",
"gather",
"gather*",
"gathered",
"multline",
"multline*",
"eqnarray",
"eqnarray*",
"math",
BODY,
];
const BODY: &str = "document";
#[inline]
pub fn lower_math(
eng: &mut Engine,
size: f64,
measure: f64,
lx: &mut Lexer,
tok: &Token,
out: &mut Vec<Cmd>,
deferred: &mut Formulas,
) -> Result<bool, TexError> {
if matches!(tok, Token::Char(_, cat) if *cat != crate::catcode::Cat::MathShift) {
return Ok(false);
}
lower_math_at(eng, size, measure, lx, tok, out, deferred)
}
fn lower_math_at(
eng: &mut Engine,
size: f64,
measure: f64,
lx: &mut Lexer,
tok: &Token,
out: &mut Vec<Cmd>,
deferred: &mut Formulas,
) -> Result<bool, TexError> {
if let Some(marks) = deferred.numbering.watch(eng, lx, tok) {
push(out, &marks);
}
let Some((f, style, display, environment)) = read(eng, lx, tok)? else {
return Ok(false);
};
let numbers = numbered(eng, out, &mut deferred.numbering, &f, environment);
let left = number::left(eng);
emit(
out,
deferred,
f,
numbers,
left,
style,
size,
display,
environment,
measure,
);
Ok(true)
}
fn numbered(
eng: &Engine,
out: &mut Vec<Cmd>,
numbering: &mut number::Numbering,
f: &parse::Formula,
environment: &str,
) -> Vec<(usize, String)> {
use crate::typeset::{ref_mark, REF_LABEL, REF_VALUE};
let mut numbers = Vec::new();
let mut marks = String::new();
for place in number::places(f, environment) {
let shown = match place.number {
number::Number::Step => {
numbering.step(eng, "equation");
let n = numbering.the(eng, "equation");
marks.push_str(&ref_mark(REF_VALUE, &n));
Some(format!("({n})"))
}
number::Number::Tag(tag, starred) => {
let t = number::spelled(tag);
marks.push_str(&ref_mark(REF_VALUE, &t));
Some(match starred {
true => t,
false => format!("({t})"),
})
}
number::Number::Unnumbered => None,
};
for key in place.labels {
marks.push_str(&ref_mark(REF_LABEL, &number::spelled(key)));
}
if let (Some(shown), None) = (shown, &f.number) {
numbers.push((place.row, shown));
}
}
if !marks.is_empty() {
use crate::typeset::{REF_CLOSE, REF_OPEN};
push(
out,
&format!(
"{}{marks}{}",
ref_mark(REF_OPEN, ""),
ref_mark(REF_CLOSE, "")
),
);
}
numbers
}
pub fn pass_over_math(eng: &mut Engine, lx: &mut Lexer, tok: &Token) -> Result<bool, TexError> {
let Some((f, _, _, environment)) = read(eng, lx, tok)? else {
return Ok(false);
};
let equation: Vec<Token> = "equation"
.chars()
.map(|c| Token::Char(c, crate::catcode::Cat::Letter))
.collect();
let mut back = Vec::new();
for place in number::places(&f, environment) {
match place.number {
number::Number::Step => call(&mut back, "refstepcounter", &equation),
number::Number::Tag(tag, _) => {
back.push(Token::cs("def"));
back.push(Token::cs("@currentlabel"));
back.push(Token::Char('{', crate::catcode::Cat::BeginGroup));
back.extend_from_slice(tag);
back.push(Token::Char('}', crate::catcode::Cat::EndGroup));
}
number::Number::Unnumbered => {}
}
for key in place.labels {
call(&mut back, "label", key);
}
}
lx.push_back(&back);
Ok(true)
}
fn call(back: &mut Vec<Token>, name: &str, arg: &[Token]) {
back.push(Token::cs(name));
back.push(Token::Char('{', crate::catcode::Cat::BeginGroup));
back.extend_from_slice(arg);
back.push(Token::Char('}', crate::catcode::Cat::EndGroup));
}
fn read(
eng: &mut Engine,
lx: &mut Lexer,
tok: &Token,
) -> Result<Option<(parse::Formula, Style, bool, &'static str)>, TexError> {
let (style, stop) = match tok {
Token::Char(_, crate::catcode::Cat::MathShift) => match second_shift(eng, lx) {
true => (DISPLAY_STYLE, parse::Stop::MathShift),
false => (TEXT_STYLE, parse::Stop::MathShift),
},
Token::Cs(name) => match name.name() {
"(" => (TEXT_STYLE, parse::Stop::Cs(")")),
"[" => (DISPLAY_STYLE, parse::Stop::Cs("]")),
"begin" => match environment(lx) {
Some(BODY) => {
eng.cats.set('$', crate::catcode::Cat::MathShift);
return Ok(None);
}
Some(name) => {
consume_environment_name(eng, lx);
let f = parse::formula(eng, lx, parse::Stop::Cs("end"))?;
consume_end(eng, lx);
return Ok(Some((f, DISPLAY_STYLE, true, name)));
}
None => return Ok(None),
},
_ => return Ok(None),
},
_ => return Ok(None),
};
let f = parse::formula(eng, lx, stop)?;
if style == DISPLAY_STYLE && stop == parse::Stop::MathShift {
let _ = second_shift(eng, lx);
}
Ok(Some((f, style, style == DISPLAY_STYLE, "")))
}
fn second_shift(eng: &mut Engine, lx: &mut Lexer) -> bool {
match lx.next_token(&eng.cats) {
Some(Token::Char(_, crate::catcode::Cat::MathShift)) => true,
Some(t) => {
lx.push_back(&[t]);
false
}
None => false,
}
}
fn environment(lx: &Lexer) -> Option<&'static str> {
if !lx.pending.is_empty() {
return None;
}
let chars = lx.chars();
let mut at = lx.pos();
while chars.get(at) == Some(&' ') {
at += 1;
}
if chars.get(at) != Some(&'{') {
return None;
}
at += 1;
let mut name = String::new();
while let Some(c) = chars.get(at) {
if *c == '}' {
return ENVIRONMENTS.iter().find(|e| **e == name).copied();
}
name.push(*c);
at += 1;
if name.len() > 32 {
return None;
}
}
None
}
fn consume_environment_name(eng: &mut Engine, lx: &mut Lexer) {
while let Some(t) = lx.next_token(&eng.cats) {
if matches!(t, Token::Char(_, crate::catcode::Cat::EndGroup)) {
break;
}
}
}
fn consume_end(eng: &mut Engine, lx: &mut Lexer) {
match lx.next_token(&eng.cats) {
Some(Token::Cs(c)) if c.name() == "end" => consume_environment_name(eng, lx),
Some(t) => lx.push_back(&[t]),
None => {}
}
}
#[allow(clippy::too_many_arguments)]
fn emit(
out: &mut Vec<Cmd>,
deferred: &mut Formulas,
f: parse::Formula,
numbers: Vec<(usize, String)>,
left: bool,
style: Style,
size: f64,
display: bool,
environment: &'static str,
measure: f64,
) {
let Some(fonts) = fonts_at(size) else {
push(out, &set::plain(&f.flat()));
return;
};
let index = deferred.next_index();
let (numbers, left): (Vec<(usize, Vec<Noad>, String)>, bool) = match numbers.is_empty() {
true => (
eqno(&f)
.into_iter()
.map(|(row, n)| {
let spelled = set::plain(&n);
(row, n, spelled)
})
.collect(),
f.number_left,
),
false => (
numbers
.into_iter()
.map(|(row, n)| (row, number::mlist(&n), n))
.collect(),
left,
),
};
if let (Some(list), true) = (f.single(), numbers.is_empty()) {
push_setting(out, &slot(index, 0), &set::plain(list), display);
} else {
for (at, row) in f.rows.iter().enumerate() {
let mut spelled = String::new();
for cell in row {
set::plain_into(&mut spelled, cell);
}
for (_, _, number) in numbers.iter().filter(|(r, _, _)| *r == at) {
spelled.push_str(number);
}
push_setting(out, &slot(index, at), &spelled, display);
}
}
deferred.push(Deferred::Formula(UnsetFormula {
formula: f,
numbers: numbers.into_iter().map(|(row, n, _)| (row, n)).collect(),
left,
fonts,
style,
display,
environment,
measure,
}));
}
pub enum Deferred {
Formula(UnsetFormula),
Picture {
options: String,
body: String,
},
}
impl Deferred {
fn specs(&self) -> Vec<String> {
match self {
Deferred::Formula(formula) => formula.specs(),
Deferred::Picture { options, body } => {
vec![crate::typeset::picture_spec(options, body)]
}
}
}
}
pub struct UnsetFormula {
formula: parse::Formula,
numbers: Vec<(usize, Vec<Noad>)>,
left: bool,
fonts: &'static font::MathFonts,
style: Style,
display: bool,
environment: &'static str,
measure: f64,
}
impl UnsetFormula {
fn specs(&self) -> Vec<String> {
let f = &self.formula;
let settings = match (f.single(), self.numbers.is_empty()) {
(Some(list), true) => vec![set_formula(self.fonts, list, self.style, self.display)],
_ => display_rows(
self.fonts,
f,
&self.numbers,
self.left,
self.style,
self.environment,
self.measure,
),
};
settings.iter().map(set::encode).collect()
}
}
const BATCH: usize = 32;
pub type Settings = std::sync::Arc<Vec<Vec<String>>>;
type Batch = (usize, std::thread::Result<Settings>);
pub type Tap = std::sync::Arc<dyn Fn(usize, Settings) + Send + Sync>;
#[derive(Default)]
pub struct SettingsTable {
batches: Vec<Option<Settings>>,
}
impl SettingsTable {
pub fn insert(&mut self, first: usize, settings: Settings) {
let at = first / BATCH;
if self.batches.len() <= at {
self.batches.resize(at + 1, None);
}
self.batches[at] = Some(settings);
}
pub fn get(&self, index: usize, row: usize) -> Option<&str> {
let batch = self.batches.get(index / BATCH)?.as_ref()?;
batch.get(index % BATCH)?.get(row).map(String::as_str)
}
}
#[derive(Default)]
pub struct Formulas {
pending: Vec<Deferred>,
read: usize,
sent: usize,
results: Option<(
std::sync::mpsc::Sender<Batch>,
std::sync::mpsc::Receiver<Batch>,
)>,
tap: Option<Tap>,
numbering: number::Numbering,
}
impl Formulas {
fn next_index(&self) -> usize {
self.read
}
pub fn picture(&mut self, options: &str, body: &str) -> String {
let index = self.next_index();
self.push(Deferred::Picture {
options: options.to_string(),
body: body.to_string(),
});
slot(index, 0)
}
fn push(&mut self, d: Deferred) {
self.pending.push(d);
self.read += 1;
if self.pending.len() == BATCH && rayon::current_num_threads() > 1 {
self.send();
}
}
pub fn tap(&mut self, tap: Tap) {
self.tap = Some(tap);
}
fn send(&mut self) {
let batch = std::mem::replace(&mut self.pending, Vec::with_capacity(BATCH));
let first = self.read - batch.len();
let (tx, _) = self.results.get_or_insert_with(std::sync::mpsc::channel);
let tx = tx.clone();
let tap = self.tap.clone();
self.sent += 1;
rayon::spawn(move || {
let specs = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
Settings::new(batch.iter().map(Deferred::specs).collect())
}));
if let (Some(tap), Ok(specs)) = (&tap, &specs) {
tap(first, Settings::clone(specs));
}
let _ = tx.send((first, specs));
});
}
pub fn fill(mut self, cmds: &mut [Cmd]) {
if self.read == 0 {
return;
}
let tail = std::mem::take(&mut self.pending);
let tail_first = self.read - tail.len();
let tail_specs: Vec<Vec<String>> = tail.iter().map(Deferred::specs).collect();
let mut batches = SettingsTable::default();
if let Some((tx, rx)) = self.results.take() {
drop(tx);
for _ in 0..self.sent {
let (first, batch) = receive(&rx);
let batch = batch.unwrap_or_else(|p| std::panic::resume_unwind(p));
batches.insert(first, batch);
}
}
let spec = |index: usize, row: usize| match index.checked_sub(tail_first) {
Some(at) => tail_specs.get(at)?.get(row).map(String::as_str),
None => batches.get(index, row),
};
fill_cmds(cmds, &spec);
match self.sent > 0 {
true => rayon::spawn(move || drop((batches, tail_specs, tail))),
false => drop((batches, tail_specs, tail)),
}
}
}
pub(crate) fn receive<T>(rx: &std::sync::mpsc::Receiver<T>) -> T {
loop {
if let Ok(b) = rx.try_recv() {
return b;
}
if rayon::yield_now() != Some(rayon::Yield::Executed) {
return rx.recv().expect("every job handed out reports back");
}
}
}
const SLOT: char = '#';
const FORMULA_OPENING: &str = "\u{1}M";
fn next_slot(t: &str, from: usize) -> Option<(usize, usize)> {
let mut at = from;
while let Some(found) = t[at..].find(SLOT) {
let hash = at + found;
let before = &t[..hash];
if before.ends_with(FORMULA_OPENING) {
return Some((hash - FORMULA_OPENING.len(), hash));
}
if before.ends_with(crate::typeset::PICTURE) {
return Some((hash - crate::typeset::PICTURE.len_utf8(), hash));
}
at = hash + SLOT.len_utf8();
}
None
}
fn slot(index: usize, row: usize) -> String {
format!("#{index}.{row}")
}
type SpecOf<'s> = dyn Fn(usize, usize) -> Option<&'s str> + 's;
fn fill_cmds<'s>(cmds: &mut [Cmd], specs: &SpecOf<'s>) {
for cmd in cmds {
match cmd {
Cmd::Text(t) => fill_text(t, specs),
Cmd::Color { body, .. } | Cmd::Group { body, .. } | Cmd::Loop { body, .. } => {
fill_cmds(body, specs)
}
Cmd::IfNum {
then_branch,
else_branch,
..
}
| Cmd::IfOdd {
then_branch,
else_branch,
..
} => {
fill_cmds(then_branch, specs);
fill_cmds(else_branch, specs);
}
_ => {}
}
}
}
fn fill_text<'s>(t: &mut String, specs: &SpecOf<'s>) {
if t.contains(SLOT) {
*t = filled(t, specs);
}
}
pub fn filled<'s>(t: &str, specs: &SpecOf<'s>) -> String {
let mut out = String::with_capacity(t.len() * 2);
let mut from = 0;
while let Some((_, hash)) = next_slot(t, from) {
out.push_str(&t[from..hash]);
let name = hash + SLOT.len_utf8();
let (named, end) = slot_named(&t[name..]);
match named.and_then(|(i, r)| specs(i, r)) {
Some(spec) => {
out.push_str(spec);
from = name + end;
}
None => {
out.push(SLOT);
from = name;
}
}
}
out.push_str(&t[from..]);
out
}
pub fn first_unsettled<'s>(t: &str, specs: &SpecOf<'s>) -> Option<usize> {
let mut from = 0;
while let Some((opens, hash)) = next_slot(t, from) {
let name = hash + SLOT.len_utf8();
let (named, end) = slot_named(&t[name..]);
if named.and_then(|(i, r)| specs(i, r)).is_none() {
return Some(opens);
}
from = name + end;
}
None
}
fn slot_named(after: &str) -> (Option<(usize, usize)>, usize) {
let end = after
.find(|c: char| !(c.is_ascii_digit() || c == '.'))
.unwrap_or(after.len());
let named = after[..end]
.split_once('.')
.and_then(|(i, r)| Some((i.parse::<usize>().ok()?, r.parse::<usize>().ok()?)));
(named, end)
}
fn push_setting(out: &mut Vec<Cmd>, spec: &str, spelled: &str, display: bool) {
let marked = format!("\u{1}{SETTING}{spec}\u{2}{spelled}\u{3}");
match display {
true => push(
out,
&format!(
"\n\n{}{marked}{}\n\n",
crate::typeset::CENTRE,
crate::typeset::CENTRE_END
),
),
false => push(out, &marked),
}
}
fn push(out: &mut Vec<Cmd>, text: &str) {
let mut at = out.len();
while at > 0 && matches!(out[at - 1], Cmd::Line(_)) {
at -= 1;
}
match at.checked_sub(1).and_then(|i| out.get_mut(i)) {
Some(Cmd::Text(t)) => t.push_str(text),
_ => out.push(Cmd::Text(text.to_string())),
}
}