#!/usr/bin/env python3
"""Draw count-split.html from measure.json: UTF-16 units (the Word counter) next to graphemes for six test titles."""
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"] if r["kind"] == "title"}
PICK = [
    ("ascii-pipes", "Plain title with | separators"),
    ("accents-nfc", "Café Crème, composed accents"),
    ("accents-nfd", "Same title, decomposed accents"),
    ("emoji-one", "Title with one yarn emoji"),
    ("flag-emoji", "Title with one flag emoji"),
    ("emoji-zwj", "Title with a family emoji"),
]
BLUE, ORANGE = "#2a78d6", "#eb6834"
X0, W, SCALE = 60, 1080, 100.0  # bars start at X0; 100 characters = 900 px
PX = 900 / SCALE
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 six titles</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">Graphemes (what you see)</text>
{''.join(parts)}
</svg></body></html>"""
open("count-split.html", "w", encoding="utf-8").write(html)
print("count-split.html", 1200, H)
