#!/usr/bin/env python3
"""Draw count-split.html from measure.json: Word counter count (UTF-16 units) next to visible characters (graphemes) for five test drafts."""
import json
import sys

src = sys.argv[1] if len(sys.argv) > 1 else "measure.json"
rows = {r["id"]: r for r in json.load(open(src, encoding="utf-8"))["rows"]}
PICK = [
    ("bold-plain", "Hook in plain letters"),
    ("bold-unicode", "Same hook in Unicode bold letters"),
    ("emoji-bullets", "Three emoji bullets"),
    ("skin-flag-zwj", "Skin-tone wave, flag, joined emoji"),
    ("headline", "Headline with one emoji"),
]
BLUE, ORANGE = "#2a78d6", "#eb6834"
X0, PX = 60, 900 / 160  # bars start at X0; 160 characters = 900 px
y = 170
parts = []
for rid, label in PICK:
    r = rows[rid]
    parts.append(f'<text x="{X0}" y="{y}" class="lab">{label}</text>')
    for k, (key, col) in enumerate((("utf16", BLUE), ("graphemes", ORANGE))):
        v = r[key]
        by = y + 18 + k * 44
        w = v * PX
        parts.append(f'<rect x="{X0}" y="{by}" width="{w:.1f}" height="38" rx="4" fill="{col}"/>')
        parts.append(f'<text x="{X0 + w + 12:.1f}" y="{by + 30}" class="val">{v}</text>')
    y += 160
H = y - 20
html = f"""<!doctype html><html><head><meta charset="utf-8"><style>
body{{margin:0;background:#fcfcfb}} svg{{display:block}}
.lab{{font:600 30px Arial,Helvetica,sans-serif;fill:#0b0b0b}}
.val{{font:600 30px Arial,Helvetica,sans-serif;fill:#0b0b0b}}
.leg{{font:400 28px Arial,Helvetica,sans-serif;fill:#52514e}}
.ttl{{font:700 34px Arial,Helvetica,sans-serif;fill:#0b0b0b}}
</style></head><body><svg xmlns="http://www.w3.org/2000/svg" width="1200" height="{H}" viewBox="0 0 1200 {H}">
<text x="{X0}" y="56" class="ttl">Characters counted two ways, same five drafts</text>
<rect x="{X0}" y="86" width="28" height="28" rx="4" fill="{BLUE}"/><text x="{X0 + 40}" y="110" class="leg">Word counter (UTF-16 units)</text>
<rect x="{X0 + 470}" y="86" width="28" height="28" rx="4" fill="{ORANGE}"/><text x="{X0 + 510}" y="110" class="leg">Characters you see (graphemes)</text>
{''.join(parts)}
</svg></body></html>"""
open("count-split.html", "w", encoding="utf-8").write(html)
print("count-split.html", 1200, H)
