Skip to content

Commit 1fb94f7

Browse files
committed
pythongh-84508: tool to generate cjk traditional chinese mappings
1 parent efc5d37 commit 1fb94f7

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

Tools/unicode/genmap_tchinese.py

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
#
2+
# genmap_tchinese.py: Traditional Chinese Codecs Map Generator
3+
#
4+
# Original Author: Hye-Shik Chang <[email protected]>
5+
#
6+
from genmap_support import (
7+
open_mapping_file,
8+
loadmap,
9+
DecodeMapWriter,
10+
EncodeMapWriter,
11+
BufferedFiller,
12+
)
13+
14+
15+
# ranges for (lead byte, follower byte)
16+
BIG5_C1 = (0xa1, 0xfe)
17+
BIG5_C2 = (0x40, 0xfe)
18+
BIG5HKSCS_C1 = (0x87, 0xfe)
19+
BIG5HKSCS_C2 = (0x40, 0xfe)
20+
21+
MAPPINGS_BIG5 = 'https://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/OTHER/BIG5.TXT'
22+
MAPPINGS_CP950 = 'http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP950.TXT'
23+
24+
HKSCS_VERSION = '2004'
25+
# The files for HKSCS mappings are available under a restrictive license.
26+
# Users of the script need to download the files from the HKSARG CCLI website:
27+
MAPPINGS_HKSCS = f'https://www.ccli.gov.hk/en/archive/terms_hkscs-{HKSCS_VERSION}-big5-iso.html'
28+
29+
30+
def bh2s(code):
31+
return ((code >> 8) - 0x87) * (0xfe - 0x40 + 1) + ((code & 0xff) - 0x40)
32+
33+
34+
def split_bytes(code):
35+
"""Split 0xABCD into 0xAB, 0xCD"""
36+
return code >> 8, code & 0xff
37+
38+
39+
def parse_hkscs_map(fo):
40+
fo.seek(0, 0)
41+
table = []
42+
for line in fo:
43+
line = line.split('#', 1)[0].strip()
44+
# We expect 4 columns in supported HKSCS files:
45+
# [1999]: unsupported
46+
# [2001]: unsupported
47+
# [2004]: Big-5; iso10646-1:1993; iso10646-1:2000; iso10646:2003+amd1
48+
# [2008]: Big-5; iso10646-1:1993; iso10646-1:2000; iso10646:2003+amd6
49+
# [2016]: not supported here--uses a json file instead
50+
#
51+
# In both supported cases, we only need the first and last column:
52+
# * Big-5 is a hex string (always 4 digits)
53+
# * iso10646:2003 is either a hex string (4 or 5 digits) or a sequence
54+
# of hex strings like: `<code_point1,code_point2>`
55+
try:
56+
hkscs_col, _, _, uni_col = line.split()
57+
hkscs = int(hkscs_col, 16)
58+
seq = tuple(int(cp, 16) for cp in uni_col.strip('<>').split(','))
59+
except ValueError:
60+
continue
61+
table.append((hkscs, seq))
62+
return table
63+
64+
65+
def make_hkscs_map(table):
66+
decode_map = {}
67+
encode_map_bmp, encode_map_notbmp = {}, {}
68+
is_bmp_map = {}
69+
sequences = []
70+
beginnings = {}
71+
single_cp_table = []
72+
# Determine multi-codepoint sequences, and sequence beginnings that encode
73+
# multiple multibyte (i.e. Big-5) codes.
74+
for mbcode, cp_seq in table:
75+
cp, *_ = cp_seq
76+
if len(cp_seq) == 1:
77+
single_cp_table.append((mbcode, cp))
78+
else:
79+
sequences.append((mbcode, cp_seq))
80+
beginnings.setdefault(cp, []).append(mbcode)
81+
# Decode table only cares about single code points (no sequences) currently
82+
for mbcode, cp in single_cp_table:
83+
b1, b2 = split_bytes(mbcode)
84+
decode_map.setdefault(b1, {})
85+
decode_map[b1][b2] = cp & 0xffff
86+
# Encode table needs to mark code points beginning a sequence as tuples.
87+
for cp, mbcodes in beginnings.items():
88+
plane = cp >> 16
89+
if plane == 0:
90+
encode_map = encode_map_bmp
91+
elif plane == 2:
92+
encode_map = encode_map_notbmp
93+
is_bmp_map[bh2s(mbcodes[0])] = 1
94+
else:
95+
assert False, 'only plane 0 (BMP) and plane 2 (SIP) allowed'
96+
if len(mbcodes) == 1:
97+
encode_value = mbcodes[0]
98+
else:
99+
encode_value = tuple(mbcodes)
100+
uni_b1, uni_b2 = split_bytes(cp & 0xffff)
101+
encode_map.setdefault(uni_b1, {})
102+
encode_map[uni_b1][uni_b2] = encode_value
103+
104+
return decode_map, encode_map_bmp, encode_map_notbmp, is_bmp_map
105+
106+
107+
def load_big5_map():
108+
mapfile = open_mapping_file('python-mappings/BIG5.txt', MAPPINGS_BIG5)
109+
with mapfile:
110+
big5decmap = loadmap(mapfile)
111+
# big5 mapping fix: use the cp950 mapping for these characters as the file
112+
# provided by unicode.org doesn't define a mapping. See notes in BIG5.txt.
113+
# Since U+5341, U+5345, U+FF0F, U+FF3C already have a big5 mapping, no
114+
# roundtrip compatibility is guaranteed for those.
115+
for m in """\
116+
0xA15A 0x2574
117+
0xA1C3 0xFFE3
118+
0xA1C5 0x02CD
119+
0xA1FE 0xFF0F
120+
0xA240 0xFF3C
121+
0xA2CC 0x5341
122+
0xA2CE 0x5345""".splitlines():
123+
bcode, ucode = list(map(eval, m.split()))
124+
big5decmap[bcode >> 8][bcode & 0xff] = ucode
125+
# encoding map
126+
big5encmap = {}
127+
for c1, m in list(big5decmap.items()):
128+
for c2, code in list(m.items()):
129+
big5encmap.setdefault(code >> 8, {})
130+
if code & 0xff not in big5encmap[code >> 8]:
131+
big5encmap[code >> 8][code & 0xff] = c1 << 8 | c2
132+
# fix unicode->big5 priority for the above-mentioned duplicate characters
133+
big5encmap[0xFF][0x0F] = 0xA241
134+
big5encmap[0xFF][0x3C] = 0xA242
135+
big5encmap[0x53][0x41] = 0xA451
136+
big5encmap[0x53][0x45] = 0xA4CA
137+
138+
return big5decmap, big5encmap
139+
140+
141+
def load_cp950_map():
142+
mapfile = open_mapping_file('python-mappings/CP950.TXT', MAPPINGS_CP950)
143+
with mapfile:
144+
cp950decmap = loadmap(mapfile)
145+
cp950encmap = {}
146+
for c1, m in list(cp950decmap.items()):
147+
for c2, code in list(m.items()):
148+
cp950encmap.setdefault(code >> 8, {})
149+
if code & 0xff not in cp950encmap[code >> 8]:
150+
cp950encmap[code >> 8][code & 0xff] = c1 << 8 | c2
151+
# fix unicode->big5 duplicated mapping priority
152+
cp950encmap[0x53][0x41] = 0xA451
153+
cp950encmap[0x53][0x45] = 0xA4CA
154+
return cp950decmap, cp950encmap
155+
156+
157+
def main_tw():
158+
big5decmap, big5encmap = load_big5_map()
159+
cp950decmap, cp950encmap = load_cp950_map()
160+
161+
# CP950 extends Big5, and the codec can use the Big5 lookup tables
162+
# for most entries. So the CP950 tables should only include entries
163+
# that are not in Big5:
164+
for c1, m in list(cp950encmap.items()):
165+
for c2, code in list(m.items()):
166+
if (c1 in big5encmap and c2 in big5encmap[c1]
167+
and big5encmap[c1][c2] == code):
168+
del cp950encmap[c1][c2]
169+
for c1, m in list(cp950decmap.items()):
170+
for c2, code in list(m.items()):
171+
if (c1 in big5decmap and c2 in big5decmap[c1]
172+
and big5decmap[c1][c2] == code):
173+
del cp950decmap[c1][c2]
174+
175+
with open('mappings_tw.h', 'w') as fp:
176+
write_big5_maps(fp, 'BIG5', 'big5', big5decmap, big5encmap)
177+
write_big5_maps(fp, 'CP950', 'cp950ext', cp950decmap, cp950encmap)
178+
179+
180+
def write_big5_maps(fp, display_name, table_name, decode_map, encode_map):
181+
print(f'Generating {display_name} decode map...')
182+
writer = DecodeMapWriter(fp, table_name, decode_map)
183+
writer.update_decode_map(BIG5_C1, BIG5_C2)
184+
writer.generate()
185+
print(f'Generating {display_name} encode map...')
186+
writer = EncodeMapWriter(fp, table_name, encode_map)
187+
writer.generate()
188+
189+
190+
class HintsWriter:
191+
filler_class = BufferedFiller
192+
193+
def __init__(self, fp, prefix, isbmpmap):
194+
self.fp = fp
195+
self.prefix = prefix
196+
self.isbmpmap = isbmpmap
197+
self.filler = self.filler_class()
198+
199+
def fillhints(self, hintfrom, hintto):
200+
name = f'{self.prefix}_phint_{hintfrom}'
201+
self.fp.write(f'static const unsigned char {name}[] = {{\n')
202+
for msbcode in range(hintfrom, hintto+1, 8):
203+
v = 0
204+
for c in range(msbcode, msbcode+8):
205+
v |= self.isbmpmap.get(c, 0) << (c - msbcode)
206+
self.filler.write('%d,' % v)
207+
self.filler.printout(self.fp)
208+
self.fp.write('};\n\n')
209+
210+
211+
def main_hkscs():
212+
filename = f'python-mappings/hkscs-{HKSCS_VERSION}-big5-iso.txt'
213+
with open_mapping_file(filename, MAPPINGS_HKSCS) as f:
214+
table = parse_hkscs_map(f)
215+
hkscsdecmap, hkscsencmap_bmp, hkscsencmap_nonbmp, isbmpmap = (
216+
make_hkscs_map(table)
217+
)
218+
with open('mappings_hk.h', 'w') as fp:
219+
print('Generating BIG5HKSCS decode map...')
220+
writer = DecodeMapWriter(fp, 'big5hkscs', hkscsdecmap)
221+
writer.update_decode_map(BIG5HKSCS_C1, BIG5HKSCS_C2)
222+
writer.generate()
223+
224+
print('Generating BIG5HKSCS decode map Unicode plane hints...')
225+
writer = HintsWriter(fp, 'big5hkscs', isbmpmap)
226+
writer.fillhints(bh2s(0x8740), bh2s(0xa0fe))
227+
writer.fillhints(bh2s(0xc6a1), bh2s(0xc8fe))
228+
writer.fillhints(bh2s(0xf9d6), bh2s(0xfefe))
229+
230+
print('Generating BIG5HKSCS encode map (BMP)...')
231+
writer = EncodeMapWriter(fp, 'big5hkscs_bmp', hkscsencmap_bmp)
232+
writer.generate()
233+
234+
print('Generating BIG5HKSCS encode map (non-BMP)...')
235+
writer = EncodeMapWriter(fp, 'big5hkscs_nonbmp', hkscsencmap_nonbmp)
236+
writer.generate()
237+
238+
239+
if __name__ == '__main__':
240+
main_tw()
241+
main_hkscs()

0 commit comments

Comments
 (0)