import asyncio,sys,os
from playwright.async_api import async_playwright
EXE='/usr/bin/google-chrome'
async def _render(jobs):
    async with async_playwright() as p:
        b=await p.chromium.launch(executable_path=EXE)
        pg=await b.new_page()
        for svg,out,w,h in jobs:
            await pg.set_viewport_size({'width':w,'height':h})
            src=open(svg).read() if os.path.exists(svg) else svg
            await pg.set_content(f'<html><body style="margin:0;background:transparent">{src.replace("<svg ","<svg style=\"display:block;width:%dpx;height:%dpx\" "%(w,h),1)}</body></html>')
            await pg.screenshot(path=out,omit_background=True,clip={'x':0,'y':0,'width':w,'height':h})
        await b.close()
def render(jobs): asyncio.run(_render(jobs))
async def _shot(url,out,w,h,full=False,scale=1):
    async with async_playwright() as p:
        b=await p.chromium.launch(executable_path=EXE)
        pg=await b.new_page(viewport={'width':w,'height':h},device_scale_factor=scale)
        await pg.goto(url,wait_until='networkidle'); await pg.wait_for_timeout(600)
        await pg.screenshot(path=out,full_page=full); await b.close()
def shot(*a,**k): asyncio.run(_shot(*a,**k))
