"""Original CC0 document sources and authored intent, never regenerate after publication."""
from pathlib import Path
from PIL import Image
import base64,hashlib,json,io
out=Path('benchmarks/fixtures/text-office1');out.mkdir(exist_ok=False)
sha=lambda b:hashlib.sha256(b).hexdigest()
images=[]
for name,w,h,fmt in [('original.png',96,64,'PNG'),('original.jpg',80,48,'JPEG')]:
 im=Image.new('RGB',(w,h));im.putdata([((x*7+y*3)%256,(x*2+y*9)%256,(x*11+y)%256)for y in range(h)for x in range(w)])
 b=io.BytesIO();im.save(b,fmt,**({'quality':90,'subsampling':0}if fmt=='JPEG'else{}));raw=b.getvalue();(out/name).write_bytes(raw)
 decoded=Image.open(io.BytesIO(raw)).convert('RGB');images.append(dict(file=name,bytes=len(raw),sha256=sha(raw),width=w,height=h,decodedRgbSha256=sha(decoded.tobytes()),license='CC0-1.0; original deterministic arithmetic pixels'))
uri=lambda name:'data:image/'+('jpeg'if name.endswith('.jpg')else'png')+';base64,'+base64.b64encode((out/name).read_bytes()).decode()
literal='Plain text control Ω Ж 世界 😀\r\n  two leading  spaces\tcolumn\r\n\r\n# Not a heading **not bold** [not a link](https://example.invalid/)\r\n<tag> &copy; stay literal.\r\nTrailing two spaces  \r\n'
semantic='''# Research note Ω Ж 世界 😀

Alpha **bold control** and *italic control*, `literal <tag> &copy;`.

## Sequence

3. Start at three
4. Continue at four

Restart list.

<ol start="9"><li>Ninth item</li></ol>

| Name | Value |
| --- | --- |
| First cell | One Ω |
| Second cell | Two Ж |

````text
  code  spaces\tcolumn
second <line> &copy;
````

[Manual link](https://example.invalid/manual)

![Original PNG]('''+uri('original.png')+''')

PNG caption.

Water H<sub>2</sub>O and power x<sup>2</sup>.

Semantic terminus.
'''
holdout='''## Holdout document

Plain **strong** then _emphasis_ then plain.

<ol start="27"><li>Ordinal twenty-seven<ul><li>Nested bullet</li></ul></li><li value="31">Ordinal thirty-one</li></ol>

<table><thead><tr><th>Left heading</th><th>Right heading</th></tr></thead><tbody><tr><td rowspan="2">Merged source</td><td>Upper right</td></tr><tr><td>Lower right</td></tr><tr><td colspan="2">Final wide cell</td></tr></tbody></table>

Unicode Καλημέρα Привет 世界 😀.

![Original JPEG]('''+uri('original.jpg')+''')

JPEG caption.

<p><a id="end"></a>Local anchor target.</p>

[Return to target](#end)

Holdout terminus.
'''
intents={
 'literal':dict(exactLfText=literal.replace('\r\n','\n'),orderedText=['Plain text control Ω Ж 世界 😀','two leading  spaces\tcolumn','# Not a heading **not bold** [not a link](https://example.invalid/)','<tag> &copy; stay literal.','Trailing two spaces'],images=[],newTargets=['doc','rtf']),
 'semantic':dict(orderedText=['Research note Ω Ж 世界 😀','Alpha bold control and italic control, literal <tag> &copy;.','Sequence','Start at three','Continue at four','Restart list.','Ninth item','Name','Value','First cell','One Ω','Second cell','Two Ж','code  spaces\tcolumn','second <line> &copy;','Manual link','PNG caption.','Water H2O and power x2.','Semantic terminus.'],images=['original.png'],newTargets=['doc','rtf','pdf']),
 'holdout':dict(orderedText=['Holdout document','Plain strong then emphasis then plain.','Ordinal twenty-seven','Nested bullet','Ordinal thirty-one','Left heading','Right heading','Merged source','Upper right','Lower right','Final wide cell','Unicode Καλημέρα Привет 世界 😀.','JPEG caption.','Local anchor target.','Return to target','Holdout terminus.'],images=['original.jpg'],newTargets=['doc','rtf','pdf'])}
rows=[]
for name,source,raw,ext,original in [('literal',literal,literal.encode(),'txt',True),('literal-utf16be',literal,b'\xfe\xff'+literal.encode('utf-16-be'),'txt',False),('semantic',semantic,semantic.encode(),'md',True),('semantic-utf16le',semantic,b'\xff\xfe'+semantic.encode('utf-16-le'),'md',False),('holdout',holdout,holdout.encode(),'md',True)]:
 file=name+'.'+ext;(out/file).write_bytes(raw);intent=intents[name.split('-utf16')[0]];rows.append(dict(name=name,file=file,fromFormat=ext,bytes=len(raw),sha256=sha(raw),originalCorpus=original,derivedFrom=None if original else name.split('-utf16')[0],license='CC0-1.0; original authored text, structures and pictures',**intent))
(out/'LICENSE.txt').write_text('All original text, arithmetic pictures and authored validation intent in this folder are dedicated to the public domain under CC0 1.0: https://creativecommons.org/publicdomain/zero/1.0/ . Encoding derivatives are not additional original corpora. Generated once by scripts/generate-text-office-fixtures.py. No user files or third-party corpus text.\n')
(out/'manifest.json').write_text(json.dumps(dict(originalCorpora=3,encodingDerivatives=2,images=images,rows=rows,method='Authored intent is independent of all Tabmorph parsers/writers. Native semantic controls compare ordered literal strings and separately verify formatting, numbering, merged cells, images, metadata and exact plain-text whitespace. ODT source controls are existing public operations, not new pairs. Browser repeats are not new corpora or competitive comparisons.'),ensure_ascii=False,indent=2)+'\n')
print(dict(originalCorpora=3,derivatives=2,sourceFiles=len(rows),images=len(images)))
