# Draws fig-counts.html (1200 x 700) from counts.jsonl: UTF-16 units against graphemes for three strings, with the 2,200 line.
import json, sys
c = {}
for l in open(sys.argv[1]):
    d = json.loads(l); c[d["id"]] = d
rows = [("1,100 emoji", "croissant-1100"), ("1,101 emoji", "croissant-1101"), ("2,100 letters + 100 emoji", "letters-100-croissant")]
X0, W, MAX = 40, 760, 2400.0          # bar start, width for MAX units
def x(v): return X0 + W * v / MAX
h = ['<!doctype html><meta charset=utf-8><body style="margin:0;background:#fff;font-family:Arial,Helvetica,sans-serif"><div style="position:relative;width:1200px;height:700px;background:#fff">']
def t(left, top, txt, size=28, color="#17211c", bold=False):
    h.append(f'<div style="position:absolute;white-space:nowrap;left:{left}px;top:{top}px;font-size:{size}px;line-height:{int(size*1.25)}px;color:{color};{"font-weight:700;" if bold else ""}">{txt}</div>')
def bar(left, top, w, color): h.append(f'<div style="position:absolute;left:{left}px;top:{top}px;width:{w:.0f}px;height:50px;background:{color}"></div>')
t(40, 24, "The same caption under two counts", 34, bold=True)
t(40, 74, "Dark: UTF-16 units (Word counter). Light: graphemes (what you see).", 28, "#3d4a43")
y = 150
for label, k in rows:
    t(40, y - 40, label, 28)
    u, g = c[k]["utf16_units"], c[k]["graphemes"]
    bar(X0, y, x(u) - X0, "#2f6b4f"); t(x(u) + 12, y + 8, f"{u:,}", 28, bold=True)
    bar(X0, y + 58, x(g) - X0, "#a9c4b6"); t(x(g) + 12, y + 66, f"{g:,}", 28, "#3d4a43", True)
    y += 190
lx = x(2200)
h.append(f'<div style="position:absolute;left:{lx:.0f}px;top:128px;width:3px;height:512px;background:#b3412c"></div>')
t(lx - 130, 650, "2,200", 28, "#b3412c", True)
h.append('</div>')
open("fig-counts.html", "w").write("".join(h))
