import json, sys
from playwright.sync_api import sync_playwright

fams = sys.argv[1:]
url = "https://fonts.googleapis.com/css2?" + "&".join(
    "family=" + f.replace(" ", "+") + ":wght@400" for f in fams) + "&display=block"
html = f"""<!doctype html><html><head><meta charset=utf-8>
<link rel=stylesheet href="{url}"></head><body></body></html>"""
open('work/_m.html','w').write(html)
with sync_playwright() as p:
    b = p.chromium.launch(args=["--font-render-hinting=none"])
    pg = b.new_page()
    pg.goto("file://" + __import__('os').path.abspath('work/_m.html'))
    pg.wait_for_timeout(2500)
    out = pg.evaluate("""async (fams) => {
      const res = {};
      for (const f of fams) {
        await document.fonts.load(`400 100px "${f}"`);
        await document.fonts.ready;
        const cv = document.createElement('canvas');
        const c = cv.getContext('2d');
        c.font = `400 100px "${f}"`;
        const m = t => { const x = c.measureText(t); return [x.actualBoundingBoxAscent, x.actualBoundingBoxDescent]; };
        res[f] = {H: m('H'), b: m('b'), x: m('x'), g: m('g'), T: m('T')};
      }
      return res;
    }""", fams)
    print(json.dumps(out, indent=1))
    b.close()
