"""Controlla che ogni carattere usato in it.txt abbia il disegno nel font del suo stile. Da lanciare dopo ogni modifica."""
import re
from fontTools.ttLib import TTFont
from fontTools.pens.boundsPen import BoundsPen
ALIAS = {'S': 'BD', 'Si': 'BDi'}
used = {}
for line in open('it.txt', encoding='utf-8'):
    if line.startswith('#') or not line.strip(): continue
    txt = line.rstrip('\n').split('|', 4)[4]
    txt = re.sub(r'<[^>]+>|@[rc]?[\d.]+:|\^-?[\d.]+:', '', txt.replace('\\n', '').replace('||', '')).replace('~', ' ')
    for m in re.finditer(r'\{_?([A-Za-z]+)[\d.]+(?::[0-9a-f/]+)?\}([^{]*)', txt):
        st = ALIAS.get(m.group(1), m.group(1))
        used.setdefault(st, set()).update(m.group(2))
ok = True
for st, chars in sorted(used.items()):
    t = TTFont(f'fonts/{st}_it.ttf'); gs = t.getGlyphSet(); cm = t.getBestCmap()
    miss = []
    for c in sorted(chars):
        if c.isspace(): continue
        g = cm.get(ord(c))
        if not g: miss.append(c); continue
        bp = BoundsPen(gs); gs[g].draw(bp)
        if not bp.bounds: miss.append(c)
    print(f'{st:4} {len(chars):3} caratteri, mancano: {" ".join(miss) or "nessuno"}')
    ok &= not miss
print('OK' if ok else 'ATTENZIONE: lettere mancanti')
