kmap-compose.py (2475B)
1 #!/usr/bin/env python3 2 # kmap-compose.py: writes compose.kmap, what each dead key makes of the 3 # letters the layouts type, from unicode's own composition. it was run once, 4 # after kmap-vendor: python3 scripts/kmap/kmap-compose.py share/kmap 5 import os, sys, unicodedata 6 7 COMBINING = { 8 "grave": 0x300, "acute": 0x301, "circumflex": 0x302, "tilde": 0x303, 9 "macron": 0x304, "breve": 0x306, "abovedot": 0x307, "diaeresis": 0x308, 10 "hook": 0x309, "abovering": 0x30A, "doubleacute": 0x30B, "caron": 0x30C, 11 "doublegrave": 0x30F, "invertedbreve": 0x311, "abovecomma": 0x313, 12 "abovereversedcomma": 0x314, "horn": 0x31B, "belowdot": 0x323, 13 "belowcomma": 0x326, "cedilla": 0x327, "ogonek": 0x328, 14 "belowmacron": 0x331, "iota": 0x345, 15 } 16 SPACING = { 17 "grave": "`", "acute": "´", "circumflex": "^", "tilde": "~", 18 "macron": "¯", "breve": "˘", "abovedot": "˙", 19 "diaeresis": "¨", "abovering": "˚", "doubleacute": "˝", 20 "caron": "ˇ", "cedilla": "¸", "ogonek": "˛", "iota": "ͺ", 21 } 22 # a stroke is no combining mark unicode composes with 23 STROKE = dict(zip("bdghiloOtzBDGHILTZ", "ƀđǥħɨłøØŧƶɃĐǤĦƗŁŦƵ")) 24 25 def word(c): 26 if c == "'": 27 return "''''" 28 if c == "#": 29 return "'#'" 30 if c == " ": 31 return "space" 32 return c 33 34 root = sys.argv[1] 35 bases = set() 36 for d, _, files in os.walk(root): 37 for f in files: 38 if not f.endswith(".kmap") or f == "compose.kmap": 39 continue 40 for line in open(os.path.join(d, f), encoding="utf-8"): 41 w = line.split() 42 if w and w[0] == "key": 43 for s in w[2:6]: 44 if len(s) == 1 and unicodedata.category(s).startswith("L"): 45 bases.add(s) 46 out = ["kmap 1", "id compose", 47 "# what a dead key followed by a letter types, for every layout"] 48 for name in sorted(COMBINING): 49 for b in sorted(bases): 50 r = unicodedata.normalize("NFC", b + chr(COMBINING[name])) 51 if len(r) == 1 and r != b: 52 out.append("compose dead_%s %s %s" % (name, word(b), word(r))) 53 if name in SPACING: 54 out.append("compose dead_%s space %s" % (name, word(SPACING[name]))) 55 out.append("compose dead_%s dead_%s %s" % (name, name, word(SPACING[name]))) 56 for b, r in STROKE.items(): 57 out.append("compose dead_stroke %s %s" % (b, r)) 58 open(os.path.join(root, "compose.kmap"), "w", encoding="utf-8").write("\n".join(out) + "\n") 59 print(len(out) - 3, "compositions")