#!/usr/bin/env python3
"""Independent real PK Type3 device, bitmap, text and raster oracle.
Both engines read the same source, TFM, map and PK bytes. No mktexpk or font
substitution is used by the native product. Requires test-only pypdf/Poppler.
"""
import argparse,hashlib,importlib.util,json,os,re,subprocess
from pathlib import Path
from pypdf import PdfReader
import pypdf
ROOT=Path(__file__).resolve().parents[1]
def module(name,file):
spec=importlib.util.spec_from_file_location(name,ROOT/'scripts'/file);m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m);return m
INK=module('pk_ink','check-native-pages.py')
def sha(data):return hashlib.sha256(data).hexdigest()
def run(args,cwd,env):
r=subprocess.run([str(a)for a in args],cwd=cwd,env=env,capture_output=True,timeout=40)
if r.returncode:raise RuntimeError(f'{args[0]} failed: '+(r.stdout+r.stderr).decode(errors='replace'))
return (r.stdout+r.stderr).decode(errors='replace')
def raw_words(pdf):
# PK slot labels can contain control bytes in Poppler's otherwise XML-like
# output. Preserve those exact UTF-8 labels; do not sanitize or remap them.
data=subprocess.check_output(['pdftotext','-bbox',str(pdf),'-'])
pdf.with_suffix('.bbox.xml').write_bytes(data)
pages=[]
for page in re.findall(rb']*>(.*?)',data,re.S):
words=[]
for attrs,label in re.findall(rb']*)>(.*?)',page,re.S):
coords={k.decode():float(v)for k,v in re.findall(rb'(xMin|xMax|yMin|yMax)="([^"]+)"',attrs)};assert len(coords)==4
words.append((label.decode('utf-8'),coords))
assert len(words)==page.count(b'