Files
mama-box/tools/share-card/render.py

30 lines
1.9 KiB
Python

import asyncio,os,json,sys
from playwright.async_api import async_playwright
M=json.load(open('/work/mama-box/tools/share-card/manifest.json'));I=json.load(open('/work/mama-box/tools/share-card/icons.json'))
OUT='/work/mama-box/assets/share/'
GEN={'calc':'<rect x="5" y="2.5" width="14" height="19" rx="3"/><rect x="7.5" y="5" width="9" height="4" rx="1" fill="rgba(0,0,0,.22)"/><g fill="rgba(0,0,0,.22)"><circle cx="9" cy="12.5" r="1.2"/><circle cx="12" cy="12.5" r="1.2"/><circle cx="15" cy="12.5" r="1.2"/><circle cx="9" cy="16" r="1.2"/><circle cx="12" cy="16" r="1.2"/><circle cx="15" cy="16" r="1.2"/></g>',
'quiz':'<path d="M12 2.8l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 16.8l-5.6 2.9 1-6.1-4.4-4.3 6.1-.9z"/>',
'plaza':'<path d="M4 4h16a2 2 0 012 2v10a2 2 0 01-2 2h-8l-5 4v-4H4a2 2 0 01-2-2V6a2 2 0 012-2z"/><path d="M7 9h10M7 13h6" stroke="rgba(0,0,0,.22)" stroke-width="1.8" stroke-linecap="round"/>'}
def svg(x):
ic=x.get('ic') or ''
if ic.startswith('test:'): p=I['TI'][ic[5:]]
elif ic in GEN: p=GEN[ic]
else: p=I['IC'].get(ic) or GEN['quiz']
return '<svg viewBox="0 0 24 24" fill="#fff">'+p+'</svg>'
async def main():
only=sys.argv[1:]
async with async_playwright() as p:
b=await p.chromium.launch(executable_path=os.environ.get('CH'),args=['--allow-file-access-from-files'])
pg=await b.new_page(viewport={'width':750,'height':600})
await pg.goto('file:///work/mama-box/tools/share-card/tpl.html')
n=0
for x in M:
if only and x['k'] not in only and not any(x['k'].startswith(o) for o in only): continue
await pg.evaluate("([x,s])=>window.draw(x,s)",[x,svg(x)])
await pg.evaluate("document.fonts.ready")
fn=OUT+x['k']+'.jpg'; os.makedirs(os.path.dirname(fn),exist_ok=True)
await pg.screenshot(path=fn,type='jpeg',quality=84,clip={'x':0,'y':0,'width':750,'height':600}); n+=1
print('rendered',n)
await b.close()
asyncio.run(main())