-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_xcompose.py
executable file
·162 lines (143 loc) · 4.44 KB
/
gen_xcompose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /usr/bin/python3
from typing import List, Generator, Optional, Tuple
import unicodedata
import sys
small_latin_alphabet = [(chr(ord('A') + i), chr(ord('a') + i))
for i in range(26)]
capital_latin_alphabet = [(a, b.upper()) for a, b in small_latin_alphabet]
greek_alphabet = [
("alpha", "a"),
("beta", "b"),
("gamma", "g"),
("delta", "d"),
("epsilon", "e"),
("zeta", "z"),
("eta", "h"),
("theta", "T"),
("iota", "i"),
("kappa", "k"),
("lamda", "l"),
("mu", "m"),
("nu", "n"),
("xi", "X"),
("omicron", "o"),
("pi", "p"),
("rho", "r"),
("sigma", "s"),
("tau", "t"),
("upsilon", "u"),
("phi", "f"),
("chi", "x"),
("psi", "P"),
("omega", "O")
]
greek_small_alphabet = greek_alphabet + [("final sigma", "S")]
greek_capital_alphabet = greek_alphabet # + [("theta symbol", "S")]
# Other greek-ish characters that we don’t generate for now:
#"partial differential",
#"lunate epsilon symbol",
#"theta symbol",
#"kappa symbol",
#"phi symbol",
#"rho symbol",
#"pi symbol",
#"nabla"
digits = [
("zero", "0"),
("one", "1"),
("two", "2"),
("three", "3"),
("four", "4"),
("five", "5"),
("six", "6"),
("seven", "7"),
("eight", "8"),
("nine", "9")
]
latin_fonts = [
("bold", "b"),
("italic", "i"),
("bold italic", "I"),
("script", "s"),
("bold script", "S"),
("fraktur", "f"),
("double-struck", "B"),
("bold fraktur", "F"),
("monospace", "m"),
("sans-serif", "a"),
("sans-serif bold", "A"),
("sans-serif italic", "n"),
("sans-serif bold italic", "N"),
]
greek_fonts = [
("GREEK", "g"),
("bold", "b"),
("italic", "i"),
("bold italic", "I"),
("sans-serif bold", "A"),
("sans-serif bold italic", "N"),
]
digit_fonts = [
("bold", "b"),
("double-struck", "B"),
("monospace", "m"),
("sans-serif", "a"),
("sans-serif bold", "A"),
]
def lookup_char(kind: str, case: str, letter: str) -> Optional[str]:
if kind == "GREEK":
return unicodedata.lookup(f"GREEK {case} LETTER {letter}")
try:
return unicodedata.lookup(f"MATHEMATICAL {kind} {case} {letter}")
except KeyError:
pass
try:
return unicodedata.lookup(f"{kind} {case} {letter}")
except KeyError:
pass
if kind == 'FRAKTUR':
try:
return unicodedata.lookup(f"BLACK-LETTER {case} {letter}")
except KeyError:
pass
elif (kind, case, letter) == ('ITALIC', 'SMALL', 'H'):
return unicodedata.lookup(f"planck constant")
return None
def gen_compositions(
kind: str, compose_char: str, indicator: str, case: str, alphabet: List[Tuple[str, str]]) -> Generator[str, None, None]:
yield f"\n# {kind.lower()} {case.lower()} ⟨{compose_char}⟩:"
for letter, key in alphabet:
character: Optional[str] = None
character = lookup_char(kind, case, letter)
if character is None:
print(
f"Error failed to find “{kind}” version of {case} {letter}",
file=sys.stderr)
yield f"#<Multi_key> <f> {indicator}<{compose_char}> <{key}> : TODO"
continue
if kind == "GREEK":
yield f"<Multi_key> {indicator}<{key}> : \"{character}\""
else:
yield f"<Multi_key> <f> {indicator}<{compose_char}> <{key}> : \"{character}\""
def main(_: List[str]) -> int:
for indicator, case, alphabet, fonts in [("", "CAPITAL", capital_latin_alphabet, latin_fonts),
("", "SMALL", small_latin_alphabet,
latin_fonts),
("<g> ", "SMALL",
greek_small_alphabet, greek_fonts),
("<G> ",
"CAPITAL",
greek_capital_alphabet,
greek_fonts),
("", "DIGIT", digits, digit_fonts)]:
for kind, compose_char in fonts:
for s in gen_compositions(
kind.upper(), compose_char, indicator, case, alphabet):
print(s)
return 0
if __name__ == "__main__":
try:
sys.exit(main(sys.argv))
except Exception as e:
print("Error: {}".format(e))
sys.exit(-1)