from pathlib import Path
import io,json,hashlib
from reportlab.pdfgen import canvas
from pypdf import PdfReader,PdfWriter
from pypdf.generic import RectangleObject
W=Path(__file__).resolve().parent;F=W/'fixtures';F.mkdir(exist_ok=True)
def make(name, pages, painted=False):
 b=io.BytesIO();c=canvas.Canvas(b,pagesize=(180,120),invariant=1)
 for w,h in pages:
  c.setPageSize((w,h))
  if painted: c.setFillColorRGB(1,1,1);c.rect(0,0,w,h,fill=1,stroke=0)
  c.setFillColorRGB(.08,.62,.59);c.roundRect(w*.2,h*.2,w*.6,h*.6,min(w,h)*.12,fill=1,stroke=0)
  c.setFillColorRGB(1,.69,.45);c.circle(w*.5,h*.5,min(w,h)*.18,fill=1,stroke=0)
  c.showPage()
 c.save();(F/(name+'.ai')).write_bytes(b.getvalue())
make('unpainted',[(180,120)])
make('painted-white',[(180,120)],True)
make('three-boards',[(180,120),(90,160),(210,90)])
make('large-board',[(3000,1800)])
r=PdfReader(F/'unpainted.ai');p=r.pages[0];p.cropbox=RectangleObject([30,20,150,100]);p.rotate(90);w=PdfWriter();w.add_page(p)
with (F/'crop-rotate.ai').open('wb') as f:w.write(f)
eps=b'%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 180 120\n%%Creator: Filepetal synthetic EPS-compatible AI boundary fixture\n%%EndComments\n0.08 0.62 0.59 setrgbcolor\n36 24 108 72 rectfill\n1 0.69 0.45 setrgbcolor\n90 60 22 0 360 arc fill\nshowpage\n%%EOF\n'
(F/'eps-compatible.ai').write_bytes(eps)
(F/'invalid.ai').write_bytes(b'This is not an Illustrator document. Synthetic invalid-signature case.')
(F/'broken-pdf.ai').write_bytes(b'%PDF-1.7\nThis has no PDF catalog or pages.\n%%EOF\n')
manifest={'date':'2026-10-22','provenance':'Original synthetic PDF representations stored with .ai extensions, plus a hand-written EPS-compatible .ai. NOT Illustrator-authored, no native editing data; these isolate the renderer branches, not the full AI format.','inputs':[]}
for p in sorted(F.glob('*.ai')):
 row={'name':p.name,'bytes':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()}
 if p.read_bytes().startswith(b'%PDF-') and p.name!='broken-pdf.ai':
  row['pages']=[{'media_box':list(map(float,x.mediabox)),'crop_box':list(map(float,x.cropbox)),'rotation':x.rotation} for x in PdfReader(p).pages]
 manifest['inputs'].append(row)
(W/'input-manifest.json').write_text(json.dumps(manifest,indent=2),encoding='utf8')
(W/'outputs').mkdir(exist_ok=True)
print(json.dumps(manifest,indent=2))
