Files
codex/codex-rs/tui/src/markdown_render/math/render.rs
T
Eric Traut 639d2478cc Render aligned equations and optimization notation in TUI math (#47191)
## Why

Aligned equations and common optimization notation previously fell back to TeX source. Scripts without Unicode equivalents also prevented otherwise supported expressions from rendering.

## What changed

- Add display layouts for `aligned` and `\boxed`, preserving fraction geometry and alignment with surrounding math.
- Stack sum limits and `\underset`/`\overset` annotations in display math, with compact forms for inline math and nested arguments.
- Support `\substack`, uppercase `\mathcal`, and single-grapheme `\widehat`.
- Preserve scripts without Unicode equivalents using explicit grouping, such as `x_{gpt}` and `x^{q+1}`.
- Keep layouts bounded and retain source fallback for unsupported input or expressions that do not fit.

## Testing

Add snapshots and regression tests for optimization models, display limits, grouped scripts, fraction alignment, narrow-width fallback, malformed input, and layout bounds. Add streaming coverage comparing character-by-character output with complete rendering at wide and narrow widths in rich and raw modes.

GitOrigin-RevId: 83937880a309375f472228368ebe885d4f50bf9c
2026-09-22 04:33:58 +00:00

491 lines
16 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Bounded Unicode layout for a deliberately small TeX math subset.
//! Accents apply only to single graphemes so their scope survives terminal rendering.
//! Unsupported Unicode scripts use explicit grouping; structured layouts remain bounded.
use crate::width::display_width;
use unicode_segmentation::UnicodeSegmentation;
mod structured;
const MAX_ROWS: usize = 16;
const MAX_COLUMNS: usize = 256;
struct Layout {
rows: Vec<String>,
baseline: usize,
}
impl Layout {
fn text(text: impl Into<String>) -> Self {
Self {
rows: vec![text.into()],
baseline: 0,
}
}
fn width(&self) -> usize {
self.rows
.iter()
.map(|row| display_width(row))
.max()
.unwrap_or_default()
}
fn join(self, right: Self) -> Option<Self> {
let baseline = self.baseline.max(right.baseline);
let height = (baseline + self.rows.len() - self.baseline)
.max(baseline + right.rows.len() - right.baseline);
// Separate neighboring fraction bars so their numerators cannot become one number.
let width = self.width() + usize::from(self.rows.len() > 1 && right.rows.len() > 1);
if height > MAX_ROWS || width + right.width() > MAX_COLUMNS {
return None;
}
let mut rows = vec![String::new(); height];
for (index, row) in rows.iter_mut().enumerate() {
if let Some(left) = index
.checked_sub(baseline - self.baseline)
.and_then(|i| self.rows.get(i))
{
row.push_str(left);
}
row.push_str(&" ".repeat(width - display_width(row)));
if let Some(right) = index
.checked_sub(baseline - right.baseline)
.and_then(|i| right.rows.get(i))
{
row.push_str(right);
}
}
Some(Self { rows, baseline })
}
fn single(&self) -> Option<&str> {
(self.rows.len() == 1).then(|| self.rows[0].as_str())
}
}
pub(super) fn render(source: &str, display: bool) -> Option<String> {
let mut parser = MathParser {
remaining: source.trim(),
depth: 0,
display,
stack_annotations: display,
};
let result = parser.sequence(SequenceEnd::Input)?;
(!result.rows.iter().all(|row| row.trim().is_empty())).then(|| result.rows.join("\n"))
}
struct MathParser<'a> {
remaining: &'a str,
depth: usize,
display: bool,
stack_annotations: bool,
}
#[derive(Clone, Copy, PartialEq)]
enum SequenceEnd {
Input,
Group,
Cell,
}
impl MathParser<'_> {
fn take(&mut self) -> Option<char> {
let ch = self.remaining.chars().next()?;
self.remaining = &self.remaining[ch.len_utf8()..];
Some(ch)
}
fn sequence(&mut self, end: SequenceEnd) -> Option<Layout> {
if self.depth >= 32 {
return None;
}
self.depth += 1;
let mut result = Layout::text("");
let mut scripts = 0;
let mut has_base = false;
while let Some(ch) = self.remaining.chars().next() {
let remaining = self.remaining.trim_start();
if end == SequenceEnd::Cell
&& (remaining.starts_with(['&', '}'])
|| remaining.starts_with(r"\\")
|| remaining.starts_with(r"\end{"))
{
self.remaining = remaining;
self.depth -= 1;
return Some(result);
}
if ch == '}' {
if end != SequenceEnd::Group {
return None;
}
self.take();
self.depth -= 1;
return Some(result);
}
if ch == '^' || ch == '_' {
let script = if ch == '^' { 1 } else { 2 };
if !has_base || scripts & script != 0 {
return None;
}
scripts |= script;
} else if !ch.is_whitespace() {
scripts = 0;
}
let atom = self.atom()?;
if !ch.is_whitespace() && ch != '^' && ch != '_' {
// Flattening a compound base would change the scope of a following script.
has_base = atom
.single()
.is_some_and(|text| text.graphemes(/*is_extended*/ true).count() == 1);
}
result = result.join(atom)?;
}
self.depth -= 1;
(end == SequenceEnd::Input).then_some(result)
}
fn argument(&mut self) -> Option<Layout> {
self.remaining = self.remaining.trim_start();
if self.remaining.starts_with(['^', '_']) {
return None;
}
self.atom()
}
fn atom(&mut self) -> Option<Layout> {
if self.depth >= 32 {
return None;
}
self.depth += 1;
let result = self.atom_inner();
self.depth -= 1;
result
}
fn atom_inner(&mut self) -> Option<Layout> {
let ch = self.take()?;
match ch {
'{' => self.sequence(SequenceEnd::Group),
'\\' => self.command(),
'^' | '_' => {
let arg = self.compact_argument()?;
let (plain, alphabet) = if ch == '^' {
(
"0123456789+-=()abcdefghijklmnoprstuvwxyz",
"⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸᶻ",
)
} else {
(
"0123456789+-=()aehijklmnoprstuvx",
"₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ",
)
};
let text = arg.single()?;
let output: Option<String> = text
.chars()
.map(|value| {
let index = plain.chars().position(|ch| ch == value)?;
alphabet.chars().nth(index)
})
.collect();
Some(Layout::text(
output.unwrap_or_else(|| format!("{ch}{{{text}}}")),
))
}
'}' | '$' | '%' | '#' | '&' | '`' => None,
ch if ch.is_whitespace() => {
self.remaining = self.remaining.trim_start();
Some(Layout::text(" "))
}
ch if ch.is_control() => None,
ch => Some(Layout::text(ch.to_string())),
}
}
fn command(&mut self) -> Option<Layout> {
let length = self
.remaining
.bytes()
.take_while(u8::is_ascii_alphabetic)
.count();
if length == 0 {
return match self.take()? {
',' | ';' | ':' | ' ' => Some(Layout::text(" ")),
'!' => Some(Layout::text("")),
'{' => Some(Layout::text("{")),
'}' => Some(Layout::text("}")),
'|' => Some(Layout::text("‖")),
_ => None,
};
}
let name = &self.remaining[..length];
self.remaining = &self.remaining[length..];
match name {
"sum" if self.stack_annotations => self.display_sum(),
"begin" | "boxed" | "underset" | "overset" | "substack" | "mathcal" => {
self.structured_command(name)
}
"frac" | "dfrac" | "tfrac" => {
let numerator = self.compact_argument()?;
let denominator = self.compact_argument()?;
if !self.display {
return Some(Layout::text(format!(
"(({})/({}))",
numerator.single()?,
denominator.single()?
)));
}
// Nested bars need a richer layout to preserve fraction hierarchy.
let numerator = numerator.single()?;
let denominator = denominator.single()?;
let width = display_width(numerator)
.max(display_width(denominator))
.max(/*other*/ 1);
if width > MAX_COLUMNS {
return None;
}
Some(Layout {
rows: vec![
format!(
"{}{numerator}",
" ".repeat((width - display_width(numerator)) / 2)
),
"─".repeat(width),
format!(
"{}{denominator}",
" ".repeat((width - display_width(denominator)) / 2)
),
],
baseline: 1,
})
}
"sqrt" => {
if self.remaining.trim_start().starts_with('[') {
return None;
}
let radicand = self.compact_argument()?;
Some(Layout::text(format!("√({})", radicand.single()?)))
}
"mathbb" => {
let arg = self.argument()?;
let text = match arg.single()? {
"R" => "ℝ",
"C" => "ℂ",
"N" => "ℕ",
"Z" => "ℤ",
"Q" => "ℚ",
"P" => "ℙ",
_ => return None,
};
Some(Layout::text(text))
}
"hat" | "widehat" | "bar" | "tilde" | "vec" | "dot" | "ddot" => {
let arg = self.argument()?;
let text = arg.single()?;
if text.graphemes(/*is_extended*/ true).count() != 1
|| text.trim().is_empty()
|| display_width(text) == 0
{
return None;
}
let accent = match name {
"hat" | "widehat" => '\u{0302}',
"bar" => '\u{0304}',
"tilde" => '\u{0303}',
"vec" => '\u{20d7}',
"dot" => '\u{0307}',
"ddot" => '\u{0308}',
_ => unreachable!(),
};
Some(Layout::text(format!("{text}{accent}")))
}
"mathrm" | "mathbf" | "mathit" => self.argument(),
"text" | "operatorname" => {
self.remaining = self.remaining.trim_start().strip_prefix('{')?;
let end = self.remaining.find('}')?;
let text = &self.remaining[..end];
if text.contains(['{', '\\', '$', '%', '#', '&'])
|| text.chars().any(char::is_control)
{
return None;
}
self.remaining = &self.remaining[end + 1..];
Some(Layout::text(text))
}
"left" | "right" => {
self.remaining = self.remaining.trim_start();
match self.take()? {
'.' => Some(Layout::text("")),
ch @ ('(' | ')' | '[' | ']' | '|') => Some(Layout::text(ch.to_string())),
'<' => Some(Layout::text("⟨")),
'>' => Some(Layout::text("⟩")),
'\\' => {
let length = self
.remaining
.bytes()
.take_while(u8::is_ascii_alphabetic)
.count()
.max(/*other*/ 1);
let (name, remaining) = self.remaining.split_at_checked(length)?;
self.remaining = remaining;
delimiter(name).map(Layout::text)
}
_ => None,
}
}
"quad" | "qquad" => Some(Layout::text(" ")),
"sin" | "cos" | "tan" | "log" | "ln" | "exp" | "lim" | "max" | "min" => {
Some(Layout::text(name))
}
_ => symbol(name).map(Layout::text),
}
}
}
fn symbol(name: &str) -> Option<&'static str> {
Some(match name {
"alpha" => "α",
"beta" => "β",
"gamma" => "γ",
"delta" => "δ",
"epsilon" => "ϵ",
"varepsilon" => "ε",
"zeta" => "ζ",
"eta" => "η",
"theta" => "θ",
"vartheta" => "ϑ",
"iota" => "ι",
"kappa" => "κ",
"varkappa" => "ϰ",
"lambda" => "λ",
"mu" => "μ",
"nu" => "ν",
"xi" => "ξ",
"pi" => "π",
"varpi" => "ϖ",
"rho" => "ρ",
"varrho" => "ϱ",
"sigma" => "σ",
"varsigma" => "ς",
"tau" => "τ",
"upsilon" => "υ",
"phi" => "ϕ",
"varphi" => "φ",
"chi" => "χ",
"psi" => "ψ",
"omega" => "ω",
"Gamma" => "Γ",
"Delta" => "Δ",
"Theta" => "Θ",
"Lambda" => "Λ",
"Xi" => "Ξ",
"Pi" => "Π",
"Sigma" => "Σ",
"Upsilon" => "Υ",
"Phi" => "Φ",
"Psi" => "Ψ",
"Omega" => "Ω",
"sum" => "∑",
"prod" => "∏",
"coprod" => "∐",
"int" => "∫",
"iint" => "∬",
"iiint" => "∭",
"oint" => "∮",
"infty" => "∞",
"partial" => "∂",
"nabla" => "∇",
"hbar" => "ℏ",
"ell" => "ℓ",
"Re" => "ℜ",
"Im" => "ℑ",
"aleph" => "ℵ",
"imath" => "ı",
"jmath" => "ȷ",
"prime" => "′",
"angle" => "∠",
"dagger" => "†",
"ddagger" => "‡",
"pm" => "±",
"mp" => "∓",
"times" => "×",
"cdot" => "·",
"div" => "÷",
"circ" => "∘",
"bullet" => "∙",
"oplus" => "⊕",
"otimes" => "⊗",
"odot" => "⊙",
"le" | "leq" => "≤",
"ge" | "geq" => "≥",
"ne" | "neq" => "≠",
"approx" => "≈",
"propto" => "∝",
"sim" => "∼",
"simeq" => "≃",
"cong" => "≅",
"lesssim" => "≲",
"gtrsim" => "≳",
"ll" => "≪",
"gg" => "≫",
"perp" => "⊥",
"parallel" => "∥",
"equiv" => "≡",
"in" => "∈",
"notin" => "∉", // codespell:ignore notin
"ni" => "∋",
"subset" => "⊂",
"subseteq" => "⊆",
"supset" => "⊃",
"supseteq" => "⊇",
"cup" => "∪",
"cap" => "∩",
"bigcup" => "⋃",
"bigcap" => "⋂",
"setminus" => "∖",
"emptyset" | "varnothing" => "∅",
"land" | "wedge" => "∧",
"lor" | "vee" => "∨",
"neg" | "lnot" => "¬",
"top" => "⊤",
"bot" => "⊥",
"forall" => "∀",
"exists" => "∃",
"nexists" => "∄",
"to" | "rightarrow" => "→",
"leftarrow" => "←",
"leftrightarrow" => "↔",
"mapsto" => "↦",
"uparrow" => "↑",
"downarrow" => "↓",
"updownarrow" => "↕",
"Leftarrow" | "impliedby" => "⇐",
"Rightarrow" | "implies" => "⇒",
"Leftrightarrow" | "iff" => "⇔",
"ldots" | "dots" => "…",
"cdots" => "⋯",
"vdots" => "⋮",
"ddots" => "⋱",
_ => return delimiter(name),
})
}
fn delimiter(name: &str) -> Option<&'static str> {
Some(match name {
"langle" => "⟨",
"rangle" => "⟩",
"lbrace" | "{" => "{",
"rbrace" | "}" => "}",
"lbrack" => "[",
"rbrack" => "]",
"vert" | "lvert" | "rvert" => "|",
"Vert" | "lVert" | "rVert" | "|" => "‖",
"lfloor" => "⌊",
"rfloor" => "⌋",
"lceil" => "⌈",
"rceil" => "⌉",
_ => return None,
})
}