50 lines
3.9 KiB
Python
50 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Compare all 128 real CMR10 OT1 slots with pdfTeX at actual page positions.
|
|
|
|
The native/runtime engine never calls TeX. This external fixture oracle checks
|
|
per-cell ink at 144dpi; metadata labels cannot hide a substituted font or slot.
|
|
"""
|
|
import argparse,json,re,subprocess
|
|
from pathlib import Path
|
|
|
|
def run(args,cwd):
|
|
r=subprocess.run([str(a) for a in args],cwd=cwd,capture_output=True,text=True)
|
|
if r.returncode:raise RuntimeError(r.stdout+'\n'+r.stderr)
|
|
return r.stdout+r.stderr
|
|
|
|
def raster(pdf,base):
|
|
run(['pdftoppm','-singlefile','-gray','-r',144,pdf,base],pdf.parent)
|
|
data=base.with_suffix('.pgm').read_bytes();m=re.match(rb'P5\s+(\d+)\s+(\d+)\s+255\s',data)
|
|
assert m;w,h=map(int,m.groups());pixels=data[m.end():];assert len(pixels)==w*h
|
|
return w,h,pixels
|
|
|
|
def main():
|
|
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--binary',type=Path,required=True);p.add_argument('--output-dir',type=Path,required=True);p.add_argument('--json',type=Path,required=True);a=p.parse_args()
|
|
root=Path(__file__).resolve().parent.parent;out=a.output_dir.resolve();out.mkdir(parents=True,exist_ok=True)
|
|
source=(root/'examples/native-bootstrap/font-slots.tex').read_text();(out/'reference.tex').write_text('\\pdfoutput=1 \\pdfpagewidth=612bp \\pdfpageheight=792bp \\vsize=648bp \\topskip=10bp\n'+source)
|
|
(out/'native-cli.txt').write_text(run([a.binary.resolve(),'native','--initex','--etex',root/'examples/native-bootstrap/font-slots.tex',out/'slots.pdf'],out))
|
|
(out/'reference-cli.txt').write_text(run(['pdftex','-ini','-etex','-interaction=nonstopmode','-halt-on-error','reference.tex'],out))
|
|
native=raster(out/'slots.pdf',out/'native');reference=raster(out/'reference.pdf',out/'reference');assert native[:2]==reference[:2]
|
|
w,h,_=native;cells=[]
|
|
for slot in range(128):
|
|
row,col=divmod(slot,16);left=144+int(col*20*144/72.27);right=144+int((col+1)*20*144/72.27)
|
|
top=144+int(row*18*144/72.27);bottom=144+int((row+1)*18*144/72.27)
|
|
indices=[y*w+x for y in range(top,bottom) for x in range(left,right)]
|
|
ni={i for i in indices if native[2][i]<200};ri={i for i in indices if reference[2][i]<200};diff=len(ni^ri)
|
|
# Absolute subpixel text positioning can change edge coverage in
|
|
# Poppler. Require every ink pixel to have a counterpart within one
|
|
# pixel, matching bounding boxes, and a strict whole-grid XOR bound.
|
|
def unmatched(a,b):
|
|
return sum(not any(i+dy*w+dx in b for dy in [-1,0,1] for dx in [-1,0,1]) for i in a)
|
|
def bbox(a):
|
|
return [min(i%w for i in a),min(i//w for i in a),max(i%w for i in a),max(i//w for i in a)] if a else None
|
|
nb,rb=bbox(ni),bbox(ri)
|
|
bounds_ok=(nb is None and rb is None) or (nb is not None and rb is not None and all(abs(a-b)<=1 for a,b in zip(nb,rb)))
|
|
un,ur=unmatched(ni,ri),unmatched(ri,ni)
|
|
cells.append(dict(slot=slot,native_ink=len(ni),reference_ink=len(ri),different_pixels=diff,unmatched_native=un,unmatched_reference=ur,native_bbox=nb,reference_bbox=rb,passed=bounds_ok and un==0 and ur==0))
|
|
pages=[int(re.search(r'Pages:\s*(\d+)',run(['pdfinfo',out/name],out))[1]) for name in ['slots.pdf','reference.pdf']]
|
|
ink=sum(c['reference_ink'] for c in cells);different=sum(c['different_pixels'] for c in cells)
|
|
result=dict(passed=all(c['passed'] for c in cells) and pages==[1,1] and different<=ink*.01,pages=pages,slots=128,dpi=144,cells=cells,total_different_pixels=different,reference_ink_pixels=ink,tolerances=dict(coverage_radius_pixels=1,bbox_pixels=1,max_xor_fraction=.01),scope='CMR10 raw OT1 slot grid; real TFM/Type1 resources, no Unicode-font substitution')
|
|
a.json.resolve().write_text(json.dumps(result,indent=2)+'\n');print(json.dumps({k:result[k] for k in ['passed','pages','slots','dpi','total_different_pixels']}));assert result['passed'],[c for c in cells if not c['passed']]
|
|
if __name__=='__main__':main()
|