import os, io, numpy as np
from PIL import Image
from playwright.sync_api import sync_playwright
fams = ["JetBrains Mono","IBM Plex Mono","Roboto Mono","Space Mono","Source Code Pro","DM Mono","Fira Mono","Inconsolata","Noto Sans Mono","Martian Mono","Geist Mono","Red Hat Mono","Chivo Mono","Azeret Mono","Sometype Mono","Fragment Mono","Spline Sans Mono"]
url = "https://fonts.googleapis.com/css2?" + "&".join("family="+f.replace(" ","+")+":wght@400;500;700" for f in fams) + "&display=block"
rows="".join(f'<div class="r"><span style="font-family:\'{f}\'">01 · ETHIOPIA</span><i>{f}</i></div>' for f in fams)
html=f"""<!doctype html><html><head><meta charset=utf-8><link rel=stylesheet href="{url}">
<style>body{{margin:0;background:#222;color:#eee;font-size:40px;line-height:1.3}} .r{{padding:2px 8px}} i{{font-size:12px;font-style:normal;color:#888;font-family:sans-serif;margin-left:12px}}</style></head><body>{rows}</body></html>"""
open('work/_mono.html','w').write(html)
with sync_playwright() as p:
    b=p.chromium.launch(args=["--font-render-hinting=none"])
    pg=b.new_page(viewport={'width':900,'height':1100},device_scale_factor=2)
    pg.goto('file://'+os.path.abspath('work/_mono.html'))
    pg.wait_for_function("document.fonts.status==='loaded'"); pg.wait_for_timeout(1500)
    pg.screenshot(path='work/mono-sheet.png', full_page=True)
    b.close()
# mock crop
im=Image.open('mock.jpg').crop((40,170,1160,260)).resize((1120*2,90*2), Image.LANCZOS); im.save('work/mock-mono.png')
