designpaca/research/composition-practice-2026-10-26/verify.py

45 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""저장된 PDF를 다시 열어 내용·판형·폰트 포함을 검사하고 전 페이지를 렌더한다."""
import json
import re
from pathlib import Path
import pymupdf
from pypdf import PdfReader
root = Path.cwd()
out = root / "outputs/composition-practice-20261026"
pdf = out / "deliverables/composition-study.pdf"
review = out / "review"
normalize = lambda text: re.sub(r"\s+", "", text)
copy = ["밤의 독서실", "늦은 시간까지 열어 두는, 함께 읽는 자리.",
"11월 14일", "18:00–24:00", "중앙도서관 2층", "사전 신청 없이 참여", "가상 행사 · 조판 실습"]
canonical = normalize("".join(copy))
geometry = json.loads((review / "svg-geometry.json").read_text(encoding="utf-8"))
for variant in geometry:
assert normalize("".join(t["text"] for t in variant["texts"])) == canonical, variant["id"]
assert not variant["outside"] and not variant["overlaps"], variant["id"]
assert len(variant["fontFaces"]) == 2
assert all(f["status"] == "loaded" for f in variant["fontFaces"])
reader = PdfReader(pdf)
document = pymupdf.open(pdf)
assert len(reader.pages) == len(document) == 3
pages = []
for i, page in enumerate(document):
width_mm, height_mm = page.rect.width * 25.4 / 72, page.rect.height * 25.4 / 72
# Chromium 인쇄 단위 반올림을 허용한다. 실측 치수는 결과에 그대로 남긴다.
assert abs(width_mm - 297) < .25 and abs(height_mm - 420) < .25
extracted = normalize(reader.pages[i].extract_text())
assert extracted == canonical, (i, extracted)
fonts = []
for font in page.get_fonts(full=True):
name, extension, kind, data = document.extract_font(font[0])
assert len(data) > 0, (i, name)
fonts.append({"name": name, "extension": extension, "bytes": len(data)})
assert len(fonts) == 2, (i, fonts)
page.get_pixmap(matrix=pymupdf.Matrix(1, 1), alpha=False).save(review / f"pdf-page-{i+1}.png")
pages.append({"page": i+1, "size_mm": [width_mm, height_mm], "copy": "exact match after whitespace normalization", "fonts": fonts})
result = {"page_count": 3, "svg_text_checks": "passed", "pdf_pages": pages,
"rendered_pages": 3, "visual_review": "pending", "physical_print_test": "not performed"}
(review / "verification.json").write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(json.dumps(result, ensure_ascii=False))