#!/usr/bin/env python3 """Generate the UTF-8 -> ASCII table for the screen. Why this exists: glibc's iconv ASCII//TRANSLIT handles punctuation (— becomes --, … becomes ...) but not every accented letter - "grün" came back as "gr?n" while "weiß" became "weiss". A harness whose answers turn into question marks is unreadable, so the letters are mapped here, from Unicode's own decomposition, and iconv is used only for what is left. Coverage: U+00A0..U+024F (Latin-1 Supplement and Latin Extended-A), which is every European letter this UI will meet. Usage: gen_decomp.py > decomp_table.c """ import sys import unicodedata # Letters whose ASCII form is not "base letter without accent". SPECIAL = { # German convention: an umlaut written out is ae/oe/ue, not a/e/u. 0x00C4: "Ae", 0x00E4: "ae", 0x00D6: "Oe", 0x00F6: "oe", 0x00DC: "Ue", 0x00FC: "ue", 0x00C6: "AE", 0x00E6: "ae", # AE ligature 0x00D0: "D", 0x00F0: "d", # eth 0x00D8: "O", 0x00F8: "o", # o slash 0x00DE: "Th", 0x00FE: "th", # thorn 0x00DF: "ss", # sharp s 0x0110: "D", 0x0111: "d", # d with stroke 0x0126: "H", 0x0127: "h", # h with stroke 0x0131: "i", # dotless i 0x0138: "k", 0x0141: "L", 0x0142: "l", # l with stroke 0x0152: "OE", 0x0153: "oe", # OE ligature 0x0166: "T", 0x0167: "t", # t with stroke 0x017F: "s", # long s } def ascii_for(cp: int) -> str: if cp in SPECIAL: return SPECIAL[cp] ch = chr(cp) decomposed = unicodedata.normalize("NFD", ch) base = "".join(c for c in decomposed if not unicodedata.combining(c)) if len(base) == 1 and base.isascii() and base.isprintable(): return base return "" def main() -> int: rows = [] for cp in range(0x00A0, 0x0250): a = ascii_for(cp) if not a: continue rows.append((cp, a)) w = sys.stdout.write w("/*\n" " * Generated by tools/gen_decomp.py from Unicode decompositions.\n" " * Do not edit by hand. The UI transliterates with this table because the\n" " * vendored curses writes Latin-1 bytes for wide characters.\n" " */\n" "#include \n" "#include \"decomp.h\"\n\n" "const struct sp_decomp sp_decomp_table[] = {\n") for cp, a in rows: esc = a.replace("\\", "\\\\").replace('"', '\\"') w("\t{ 0x%04x, \"%s\" },\n" % (cp, esc)) w("};\n\n" "const size_t sp_decomp_count = " "sizeof(sp_decomp_table) / sizeof(sp_decomp_table[0]);\n") return 0 if __name__ == "__main__": sys.exit(main())