"""Original CC0 PDF/PNG boundary fixtures, created 2026-10-05. Requires pypdf."""
from pathlib import Path
import hashlib, json
from pypdf import PdfWriter
from pypdf.generic import DictionaryObject, NameObject, DecodedStreamObject, RectangleObject

HERE = Path(__file__).resolve().parent
def make(name, pages, encrypted=False):
    writer = PdfWriter()
    descriptions = []
    for width, height, crop, rotation in pages:
        page = writer.add_blank_page(width=width, height=height)
        # Deliberately no painted page background: PNG capability is not a promise of alpha.
        stream = DecodedStreamObject()
        stream.set_data(f'1 0 0 rg 0 0 {width:g} {height:g} re s\n0 0 1 rg {width/4:g} {height/4:g} {width/2:g} {height/2:g} re f\n'.encode('ascii'))
        page[NameObject('/Contents')] = writer._add_object(stream)
        if crop:
            page.cropbox = RectangleObject(crop)
        if rotation:
            page.rotate(rotation)
        descriptions.append({'media_box_pt':[0,0,width,height], 'crop_box_pt':crop or [0,0,width,height], 'rotation_degrees':rotation, 'painted_background':False})
    writer.add_metadata({'/Title':'Filepetal original PDF to PNG check', '/Subject':'CC0 synthetic geometric fixtures; 2026-10-05'})
    if encrypted:
        writer.encrypt('filepetal-sample-only')
    path = HERE/(name+'.pdf')
    with path.open('wb') as out:
        writer.write(out)
    return {'file':path.name, 'bytes':path.stat().st_size,'sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'pages':descriptions,'encrypted':encrypted}

def main():
    HERE.mkdir(parents=True,exist_ok=True)
    cases = [
        make('unpainted-background',[(200,120,None,0)]),
        make('crop-and-rotate',[(200,120,[20,10,180,110],0),(200,120,[20,10,180,110],90)]),
        make('wide-vector',[(10000,100,None,0)]),
        make('combined-pixels',[(1300,1300,None,0)]*6),
        make('twenty-one-pages',[(72,72,None,0)]*21),
        make('password-protected',[(200,120,None,0)],True)
    ]
    invalid=HERE/'invalid-header.pdf';invalid.write_bytes(b'Not a PDF. Filepetal synthetic rejection check.\n')
    cases.append({'file':invalid.name,'bytes':invalid.stat().st_size,'sha256':hashlib.sha256(invalid.read_bytes()).hexdigest(),'pages':[],'expected_rejection':'invalid PDF header'})
    (HERE/'sample-manifest.json').write_text(json.dumps({'created_date':'2026-10-05','license':'CC0-1.0','synthetic':True,'cases':cases},indent=2)+'\n',encoding='utf-8')
    print(json.dumps({'inputs_created':len(cases)}))
if __name__ == '__main__': main()
