initial commit
This commit is contained in:
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Author deterministic unembedded Japanese CID H/V fixtures (ReportLab)."""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from pypdf import PdfReader
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
|
||||
from reportlab.pdfgen.canvas import Canvas
|
||||
|
||||
ROOT = Path(__file__).resolve().parent / "fonts"
|
||||
ROOT.mkdir(exist_ok=True)
|
||||
OUTPUT = ROOT / "unembedded-japanese.pdf"
|
||||
TEXTS = ["日本語の文書を表示します。", "漢字とひらがな、カタカナ。", "「縦書き」の句読点を確認。"]
|
||||
for face in ("HeiseiMin-W3", "HeiseiKakuGo-W5"):
|
||||
for vertical in (False, True):
|
||||
font = UnicodeCIDFont(face, isVertical=vertical)
|
||||
font.name = font.fontName = face + ("-V" if vertical else "-H")
|
||||
pdfmetrics.registerFont(font)
|
||||
|
||||
canvas = Canvas(str(OUTPUT), pagesize=(600, 760), invariant=1, pageCompression=0)
|
||||
canvas.setTitle("DocView unembedded Japanese CID H/V fixture")
|
||||
canvas.setAuthor("DocView synthetic test corpus")
|
||||
for page, vertical in enumerate((False, True), 1):
|
||||
canvas.setFont("Helvetica-Bold", 16)
|
||||
canvas.drawString(36, 722, "Unembedded Japanese CID fonts")
|
||||
canvas.setFont("Helvetica", 11)
|
||||
canvas.drawString(36, 700, "UniJIS-UCS2-" + ("V: native vertical writing mode" if vertical else "H: horizontal writing mode"))
|
||||
for row, face in enumerate(("HeiseiMin-W3", "HeiseiKakuGo-W5")):
|
||||
canvas.setFont("Helvetica", 10)
|
||||
if vertical:
|
||||
canvas.drawString(330 - row * 280, 660, face)
|
||||
for col, text in enumerate(TEXTS):
|
||||
canvas.setFont(face + "-V", 20)
|
||||
canvas.drawString(520 - row * 280 - col * 62, 620, text)
|
||||
else:
|
||||
canvas.drawString(36, 648 - row * 260, face)
|
||||
canvas.setFont(face + "-H", 20)
|
||||
for line, text in enumerate(TEXTS):
|
||||
canvas.drawString(36, 610 - row * 260 - line * 42, text)
|
||||
canvas.setFont("Helvetica", 9)
|
||||
canvas.drawString(36, 28, f"Synthetic fixture / page {page} / no font program embedded")
|
||||
canvas.showPage()
|
||||
canvas.save()
|
||||
|
||||
reader = PdfReader(OUTPUT)
|
||||
encodings = set()
|
||||
for page in reader.pages:
|
||||
for ref in page["/Resources"]["/Font"].values():
|
||||
font = ref.get_object()
|
||||
if font.get("/Subtype") != "/Type0":
|
||||
continue
|
||||
encodings.add(str(font["/Encoding"]))
|
||||
for descendant in font["/DescendantFonts"]:
|
||||
descriptor = descendant.get_object()["/FontDescriptor"].get_object()
|
||||
assert not any(key in descriptor for key in ("/FontFile", "/FontFile2", "/FontFile3"))
|
||||
assert encodings == {"/UniJIS-UCS2-H", "/UniJIS-UCS2-V"}, encodings
|
||||
manifest = {
|
||||
"schemaVersion": 1,
|
||||
"generator": "generate_fonts.py (ReportLab UnicodeCIDFont, invariant PDF)",
|
||||
"documents": [{
|
||||
"file": OUTPUT.name,
|
||||
"sha256": hashlib.sha256(OUTPUT.read_bytes()).hexdigest(),
|
||||
"pages": 2,
|
||||
"sizePt": [600, 760],
|
||||
"fontProgramsEmbedded": False,
|
||||
"encodings": sorted(encodings),
|
||||
"faces": ["HeiseiMin-W3", "HeiseiKakuGo-W5"],
|
||||
"expectedText": TEXTS,
|
||||
"writingModes": ["horizontal", "vertical"],
|
||||
}],
|
||||
}
|
||||
(ROOT / "manifest.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n")
|
||||
print(json.dumps(manifest, ensure_ascii=False, indent=2))
|
||||
Reference in New Issue
Block a user