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
|
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only
# TODO: Approximate {sub,super}script using other characters?
from sys import stderr
with open("UnicodeData.txt") as f:
data = f.read()
data = data.split("\n")
superscripts = {}
def line_to_char(line):
return chr(int(line.split(";", 1)[0], 16))
for line in data:
if "SUPERSCRIPT" in line:
normal_name = line.split("SUPERSCRIPT ", 1)[1].split(";", 1)[0]
prefix = line.split(";", 1)[1].split("SUPERSCRIPT", 1)[0]
normal_line = None
for line2 in data:
if (
normal_name in line2
and prefix in line2
and line != line2
and "<control>" not in line2
):
normal_line = line2
break
if not normal_line:
print("not found:", line, file=stderr)
continue
char = line_to_char(line)
normal = line_to_char(normal_line)
if normal not in superscripts:
superscripts[normal] = char
elif "MODIFIER LETTER" in line:
name = line.split("MODIFIER LETTER ", 1)[1].split(";", 1)[0]
try_names = [name]
if "SMALL GREEK" in name:
try_names.append("GREEK SMALL LETTER" + name.split("SMALL GREEK", 1)[1])
elif "SMALL CYRILLIC" in name:
try_names.append(
"CYRILLIC SMALL LETTER" + name.split("SMALL CYRILLIC", 1)[1]
)
elif "CYRILLIC SMALL" in name:
try_names.append(
"CYRILLIC SMALL LETTER" + name.split("CYRILLIC SMALL", 1)[1]
)
elif "SMALL CAPITAL" in name:
try_names.append(
"LATIN LETTER SMALL CAPITAL" + name.split("SMALL CAPITAL", 1)[1]
)
elif "SMALL" in name:
s = name.split("SMALL", 1)[1]
if "LIGATURE OE" in name:
try_names.append("LATIN SMALL" + s)
else:
try_names.append("LATIN SMALL LETTER" + s)
elif "CAPITAL" in name:
s = name.split("CAPITAL", 1)[1]
if "BARRED B" in name:
try_names.append("LATIN LETTER SMALL CAPITAL" + s)
elif "REVERSED N" in name:
try_names.append("LATIN LETTER SMALL CAPITAL" + s)
else:
try_names.append("LATIN CAPITAL LETTER" + s)
normal_line = None
for line2 in data:
if (
any(map(lambda x: ";" + x + ";" in line2, try_names))
and line != line2
and "<control>" not in line2
):
normal_line = line2
break
if not normal_line:
print("not found:", line, file=stderr)
continue
char = line_to_char(line)
normal = line_to_char(normal_line)
if normal not in superscripts:
superscripts[normal] = char
superscripts[" "] = " "
import json
print("window.unicodeMaps.toSuperscript =", json.dumps(superscripts), ";")
print(superscripts, file=stderr)
|