mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-05 05:23:36 -05:00
Verify swatches from the captured PostScript instead of tray preferences
This commit is contained in:
1 parent
ba0f59e3b9
commit
df3f32dfb4
2 files changed
+47
-11
No files matched your search
@@ -1,17 +1,22 @@
|
||||
"""Verify raw Windows PostScript and decode its numbered sheets independently."""
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
|
||||
ROOT = Path("captured-postscript")
|
||||
VERIFY = Path("shared/crates/print/examples/print_acceptance/verify.py")
|
||||
CASES = {"baseline": [1, 2, 3, 4], "range": [2, 4]}
|
||||
spec = importlib.util.spec_from_file_location("numbered_sheets", VERIFY)
|
||||
numbered_sheets = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(numbered_sheets)
|
||||
|
||||
|
||||
def verify(folder, case, expected, grayscale):
|
||||
def verify(folder, case, expected):
|
||||
raw = folder / f"{case}.prn"
|
||||
captured = raw.read_bytes()
|
||||
# A printer may wrap the PostScript in PJL. Keep the spool bytes intact and
|
||||
@@ -40,23 +45,40 @@ def verify(folder, case, expected, grayscale):
|
||||
document.write_bytes(payload)
|
||||
pdf = folder / f"{case}-rendered.pdf"
|
||||
subprocess.run(
|
||||
["gs", "-q", "-dSAFER", "-dBATCH", "-dNOPAUSE", "-sDEVICE=pdfwrite", f"-sOutputFile={pdf}", document],
|
||||
["gs", "-q", "-dSAFER", "-dBATCH", "-dNOPAUSE", "-dNoOutputFonts", "-sDEVICE=pdfwrite", f"-sOutputFile={pdf}", document],
|
||||
check=True, timeout=60,
|
||||
)
|
||||
subprocess.run(
|
||||
["python3", VERIFY, pdf, *map(str, expected), *(["--gray"] if grayscale else [])],
|
||||
check=True, timeout=180,
|
||||
)
|
||||
with tempfile.TemporaryDirectory(prefix="craft-postscript-") as directory:
|
||||
root = Path(directory)
|
||||
subprocess.run(["pdftoppm", "-r", "36", pdf, root / "page"], check=True, timeout=60)
|
||||
sheets = sorted(root.glob("page-*.ppm"), key=lambda path: int(path.stem.split("-")[-1]))
|
||||
assert len(sheets) == len(expected), (pdf, len(sheets), expected)
|
||||
# Windows' Color preference isn't reliable for the output PS class
|
||||
# driver. Verify whichever mode the physical swatches actually use.
|
||||
try:
|
||||
numbered_sheets.verify_page(sheets[0], expected[0], plotter=False, gray=False)
|
||||
grayscale = False
|
||||
except ValueError:
|
||||
numbered_sheets.verify_page(sheets[0], expected[0], plotter=False, gray=True)
|
||||
grayscale = True
|
||||
for sheet, page in zip(sheets[1:], expected[1:]):
|
||||
numbered_sheets.verify_page(sheet, page, plotter=False, gray=grayscale)
|
||||
print(f"{pdf}: verified IDs {expected} with {'gray' if grayscale else 'color'} swatches", flush=True)
|
||||
info = subprocess.check_output(["pdfinfo", pdf], text=True)
|
||||
(folder / f"{case}.pdfinfo").write_text(info)
|
||||
|
||||
|
||||
def main():
|
||||
failures = []
|
||||
for model in ("ps-class", "imagesetter"):
|
||||
folder = ROOT / model
|
||||
configuration = json.loads((folder / "configuration.json").read_text(encoding="utf-8-sig"))
|
||||
for case, expected in CASES.items():
|
||||
verify(folder, case, expected, grayscale=configuration["Color"] is False)
|
||||
try:
|
||||
verify(folder, case, expected)
|
||||
except (AssertionError, OSError, ValueError, subprocess.SubprocessError) as error:
|
||||
failures.append(f"{model}/{case}: {error}")
|
||||
if failures:
|
||||
raise SystemExit("\n".join(failures))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user