#!/usr/bin/env python3 """Regenerate isolated CM math-box probes with pdfTeX (no PDF integration). Run from any directory. Requires pdftex; Cargo tests consume the checked-in JSON. TeX dimensions are integer scaled points, not PDF big points (72/72.27). """ import json from pathlib import Path import subprocess import tempfile ROOT = Path(__file__).resolve().parent.parent STYLES = [ ("Display", r"\displaystyle"), ("Text", r"\textstyle"), ("Script", r"\scriptstyle"), ("ScriptScript", r"\scriptscriptstyle"), ] CASES = [ "x", r"\alpha", r"\Omega", "x+y=z", "-x--y", "(x,y)", "x/y", "x.y", "x!y?z", "x;y", "xz", "x+(-y)", r"\epsilon+\varepsilon", r"\phi+\varphi", r"\theta+\vartheta", r"\pi+\varpi", r"\rho+\varrho", r"\sigma+\varsigma", r"\Gamma+\Delta+\Theta+\Lambda+\Xi+\Pi+\Sigma+\Upsilon+\Phi+\Psi+\Omega", "x_i", "x^2", "x_i^2", "f_j^N", "x_{i_j}^{N^2}", r"\displaystyle x_i^2", r"\textstyle x_i^2", r"\scriptstyle x+y=z", r"\scriptscriptstyle x+y=z", r"x+{\scriptstyle y+z}+w", r"\sum\limits_{i=1}^N", r"\sum\nolimits_{i=1}^N", r"\sum_{i=1}^N\limits", r"\int\limits_0^1", r"\int\displaylimits_0^1", r"a\,b\:c\;d\!e", r"a\mskip3mu b", r"a\mkern-1.5mu b", r"\varGamma", r"\varOmega^2", r"\tfrac{a}{b}", r"\dfrac{a}{b}", r"\frac{a}{x^2}", r"\frac{a}{x_i^2}", r"\frac{a}{b}", r"\frac{x_i^2}{y_j}", r"\frac{a}{\frac{b}{c}}", r"{x_i\atop y_j}", r"\sum", r"\sum_{i=1}^N", r"\prod_{j=1}^N", r"\int_0^1", r"\sin(x)", r"\mathinner{x}\mathinner{y}", # The fraction body of the arXiv fixture; the rule width is its widest child. r"\frac{\alpha_i(t)a^{w_t}_{ij}\beta_j(t+1)b^{v_{t+1}}_j(y_{t+1})}" r"{\sum_{i=1}^N\sum_{j=1}^N\alpha_i(t)a^{w_t}_{ij}" r"\beta_j(t+1)b^{v_{t+1}}_j(y_{t+1})}", ] CLASSES = ["mathord", "mathop", "mathbin", "mathrel", "mathopen", "mathclose", "mathpunct", "mathinner"] # Every class pair also tests TeX's context-dependent binary normalization. CASES += [rf"\{left}{{x}}\{right}{{y}}" for left in CLASSES for right in CLASSES] ARXIV = (ROOT / "fixtures/arxiv-template/template.tex").read_text() CASES.append(ARXIV.split(r"\begin{equation}", 1)[1].split(r"\end{equation}", 1)[0].strip()) def main(): lines = [r"\def\frac#1#2{{#1\over#2}}", r"\def\dfrac#1#2{{\displaystyle\frac{#1}{#2}}}", r"\def\tfrac#1#2{{\textstyle\frac{#1}{#2}}}", r'\mathchardef\varGamma="0100 \mathchardef\varOmega="010A', r"\def\:{\mskip4mu}", r"\newwrite\probes", r"\immediate\openout\probes=metrics.csv"] records = [] for style, command in STYLES: for source in CASES: expression = command + " " + source index = len(records) lines += [r"\setbox0=\hbox{$" + expression + "$}", rf"\immediate\write\probes{{{index},\number\wd0,\number\ht0,\number\dp0}}"] records.append({"style": style, "source": source}) lines += [r"\immediate\closeout\probes", r"\bye"] with tempfile.TemporaryDirectory(prefix="texrra-math-oracle-") as directory: path = Path(directory) (path / "probes.tex").write_text("\n".join(lines) + "\n") result = subprocess.run(["pdftex", "-interaction=nonstopmode", "-halt-on-error", "probes.tex"], cwd=path, capture_output=True, text=True) if result.returncode: raise RuntimeError(result.stdout) for line in (path / "metrics.csv").read_text().splitlines(): index, width, height, depth = map(int, line.split(",")) records[index].update(width=width, height=height, depth=depth) output = ROOT / "crates/texrra-tex-core/tests/fixtures/math-tex-oracle.json" output.parent.mkdir(parents=True, exist_ok=True) output.write_text("[\n" + ",\n".join(" " + json.dumps(record) for record in records) + "\n]\n") print(f"{len(records)} pdfTeX probes: {output}") if __name__ == "__main__": main()