"""CC0 synthetic PDF limit fixtures, created October 5, 2026. Requires pypdf and Pillow.

The large fixture contains a real uncompressed RGB image, not file padding.
Its encoded file size and rendered page pixels deliberately measure different things.
"""
from pathlib import Path
import hashlib
import json
from PIL import Image
from pypdf import PdfWriter, PdfReader
from pypdf.generic import DictionaryObject, NameObject, NumberObject, DecodedStreamObject

ROOT = Path(__file__).resolve().parent


def save(writer, name):
    writer.add_metadata({"/Title": "Filepetal PDF limit reproduction", "/Subject": "CC0 synthetic fixture; 2026-10-05"})
    path = ROOT / name
    with path.open("wb") as output:
        writer.write(output)
    return path


def page_count():
    writer = PdfWriter()
    font = writer._add_object(DictionaryObject({NameObject("/Type"): NameObject("/Font"), NameObject("/Subtype"): NameObject("/Type1"), NameObject("/BaseFont"): NameObject("/Helvetica")}))
    for number in range(1, 51):
        page = writer.add_blank_page(width=72, height=72)
        page[NameObject("/Resources")] = DictionaryObject({NameObject("/Font"): DictionaryObject({NameObject("/F1"): font})})
        stream = DecodedStreamObject()
        stream.set_data(f"0 0 1 rg 10 10 {number} 10 re f BT /F1 12 Tf 10 42 Td (Page {number}) Tj ET\n".encode("ascii"))
        page[NameObject("/Contents")] = writer._add_object(stream)
    return save(writer, "fifty-small-pages.pdf")


def file_bytes():
    writer = PdfWriter()
    page = writer.add_blank_page(width=128, height=128)
    width, height = 3072, 2560
    # A blue background and red left-half marker, stored as uncompressed RGB.
    image = Image.new("RGB", (width, height), (0, 0, 255))
    image.paste((255, 0, 0), (0, 0, width // 2, height))
    obj = DecodedStreamObject()
    obj.set_data(image.tobytes())
    obj.update({NameObject("/Type"): NameObject("/XObject"), NameObject("/Subtype"): NameObject("/Image"), NameObject("/Width"): NumberObject(width), NameObject("/Height"): NumberObject(height), NameObject("/ColorSpace"): NameObject("/DeviceRGB"), NameObject("/BitsPerComponent"): NumberObject(8)})
    page[NameObject("/Resources")] = DictionaryObject({NameObject("/XObject"): DictionaryObject({NameObject("/Im1"): writer._add_object(obj)})})
    content = DecodedStreamObject()
    content.set_data(b"q 128 0 0 128 0 0 cm /Im1 Do Q\n")
    page[NameObject("/Contents")] = writer._add_object(content)
    return save(writer, "over-20-mib-image.pdf")


if __name__ == "__main__":
    records = []
    for path in [page_count(), file_bytes()]:
        reader = PdfReader(path)
        records.append({"file": path.name, "bytes": path.stat().st_size, "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), "pages": len(reader.pages), "page_dimensions_pt": [[float(p.mediabox.width), float(p.mediabox.height)] for p in reader.pages], "license": "CC0", "created_date": "2026-10-05"})
    (ROOT / "input-manifest.json").write_text(json.dumps({"date": "2026-10-05", "scope": "Synthetic fixtures; not a maximum-capacity benchmark", "inputs": records}, indent=2) + "\n", encoding="utf-8")
    print(json.dumps([{k: v for k, v in r.items() if k != "page_dimensions_pt"} for r in records]))
