from fontTools.ttLib import TTFont
from pathlib import Path
import sys, csv

def cps_from_font(p):
    f = TTFont(p)
    s = set()
    for t in f["cmap"].tables:
        s.update(t.cmap.keys())
    return sorted(c for c in s if c is not None)

def to_ranges(cps):
    if not cps:
        return ""
    out = []
    a = b = cps[0]
    for x in cps[1:]:
        if x == b + 1:
            b = x
        else:
            out.append((a, b))
            a = b = x
    out.append((a, b))
    return ", ".join(
        f"U+{a:04X}" if a == b else f"U+{a:04X}-{b:04X}"
        for a, b in out
    )

def main(argv):
    w = csv.writer(sys.stdout)
    w.writerow(["file", "unicode-range"])
    for fp in argv:
        cps = cps_from_font(fp)
        w.writerow([Path(fp).name, to_ranges(cps)])

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python3 dump_unicode_ranges.py <font1.woff2> ...", file=sys.stderr)
        sys.exit(1)
    main(sys.argv[1:])
