Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Absolute-page math gate for the arXiv fixture (stdlib + Poppler).
|
||||
|
||||
This selects the fixture's wide fraction rule from vector output and also
|
||||
compares the formula ink bounds at 144dpi. No rendering offsets are fitted.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def run(*args):
|
||||
return subprocess.check_output([str(a) for a in args])
|
||||
|
||||
|
||||
def native_rule(pdf):
|
||||
data = Path(pdf).read_bytes()
|
||||
paper = re.search(rb"/MediaBox\s*\[0 0 ([\d.]+) ([\d.]+)\]", data)
|
||||
height = float(paper[2])
|
||||
candidates = []
|
||||
for stream in re.findall(rb"stream\r?\n(.*?)endstream", data, re.S):
|
||||
if b"BT /M" not in stream:
|
||||
continue
|
||||
origin = re.search(rb"1 0 0 1 ([-\d.]+) ([-\d.]+) cm", stream)
|
||||
dx, dy = map(float, origin.groups()) if origin else (0, 0)
|
||||
for rect in re.finditer(rb"([-\d.]+) ([-\d.]+) ([-\d.]+) ([-\d.]+) re f", stream):
|
||||
x, y, width, thickness = map(float, rect.groups())
|
||||
if 150 < width < 200 and x + dx > 250 and 0 < thickness < 1:
|
||||
candidates.append(dict(x=x + dx, y=height - y - dy - thickness / 2,
|
||||
width=width, thickness=thickness))
|
||||
assert len(candidates) == 1, candidates
|
||||
return candidates[0]
|
||||
|
||||
|
||||
def reference_rule(pdf, directory):
|
||||
svg = directory / "reference.svg"
|
||||
run("pdftocairo", "-f", 2, "-l", 2, "-svg", pdf, svg)
|
||||
candidates = []
|
||||
for path in re.findall(r"<path\b[^>]*>", svg.read_text()):
|
||||
line = re.search(r'd="M ([-\d.]+) ([-\d.]+) L ([-\d.]+) ([-\d.]+)\s*"', path)
|
||||
matrix = re.search(r'transform="matrix\(([^)]+)\)"', path)
|
||||
stroke = re.search(r'stroke-width="([-\d.]+)"', path)
|
||||
if not (line and matrix and stroke):
|
||||
continue
|
||||
x1, y1, x2, y2 = map(float, line.groups())
|
||||
a, b, c, d, e, f = map(float, matrix[1].split(","))
|
||||
left, right = sorted((a*x1 + c*y1 + e, a*x2 + c*y2 + e))
|
||||
width = right - left
|
||||
if 150 < width < 200 and left > 250 and float(stroke[1]) < 1:
|
||||
candidates.append(dict(x=left, y=b*(x1+x2)/2+d*(y1+y2)/2+f,
|
||||
width=width, thickness=float(stroke[1])))
|
||||
assert len(candidates) == 1, candidates
|
||||
return candidates[0]
|
||||
|
||||
|
||||
def ink_bounds(pdf, output, crop):
|
||||
run("pdftoppm", "-f", 2, "-l", 2, "-singlefile", "-r", 144, "-gray", pdf, output)
|
||||
data = output.with_suffix(".pgm").read_bytes()
|
||||
header = re.match(rb"P5\s+(\d+)\s+(\d+)\s+255\s", data)
|
||||
assert header
|
||||
width, height = map(int, header.groups())
|
||||
pixels = data[header.end():]
|
||||
assert len(pixels) == width * height
|
||||
left, top, right, bottom = crop
|
||||
points = [(x, y) for y in range(top, bottom) for x in range(left, right)
|
||||
if pixels[y * width + x] < 200]
|
||||
assert points
|
||||
xs, ys = zip(*points)
|
||||
return [min(xs), min(ys), max(xs), max(ys)]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--reference", type=Path, required=True)
|
||||
parser.add_argument("--candidate", type=Path, required=True)
|
||||
parser.add_argument("--json", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
with tempfile.TemporaryDirectory(prefix="texrra-absolute-math-") as temporary:
|
||||
directory = Path(temporary)
|
||||
reference = reference_rule(args.reference, directory)
|
||||
native = native_rule(args.candidate)
|
||||
errors = {key: native[key] - reference[key] for key in reference}
|
||||
crop = (240, int((reference["y"] - 17) * 2), 1100, int((reference["y"] + 18) * 2))
|
||||
reference_ink = ink_bounds(args.reference, directory / "reference", crop)
|
||||
native_ink = ink_bounds(args.candidate, directory / "native", crop)
|
||||
pages = [int(re.search(rb"Pages:\s*(\d+)", run("pdfinfo", pdf))[1])
|
||||
for pdf in (args.reference, args.candidate)]
|
||||
passed = (pages == [3, 3] and all(abs(value) <= 1 for value in errors.values())
|
||||
and all(abs(a-b) <= 2 for a, b in zip(reference_ink, native_ink)))
|
||||
result = dict(passed=passed, pages=pages, reference_rule_bp=reference,
|
||||
native_rule_bp=native, error_bp=errors,
|
||||
reference_ink_bbox_144dpi=reference_ink,
|
||||
native_ink_bbox_144dpi=native_ink,
|
||||
tolerances=dict(vector_bp=1, ink_bbox_pixels=2))
|
||||
args.json.write_text(json.dumps(result, indent=2) + "\n")
|
||||
print(json.dumps(result, indent=2))
|
||||
assert passed, "absolute-page math acceptance failed"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user