Define __ASSUME_ALIGNED_REGISTER_PAIRS for missing ports
[glibc.git] / localedata / unicode-gen / gen_translit_cjk_compat.py
blobeecc9cc6eed23d79315732156a861344df1981e8
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
4 # Generate a translit_cjk_compat file from a UnicodeData file.
5 # Copyright (C) 2015-2016 Free Software Foundation, Inc.
6 # This file is part of the GNU C Library.
8 # The GNU C Library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # The GNU C Library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with the GNU C Library; if not, see
20 # <http://www.gnu.org/licenses/>.
22 '''
23 Generate a translit_cjk_compat file from UnicodeData.txt
25 To see how this script is used, call it with the “-h” option:
27 $ ./gen_translit_cjk_compat -h
28 … prints usage message …
29 '''
31 import argparse
32 import time
33 import sys
34 import unicode_utils
36 def read_input_file(filename):
37 '''Reads the original glibc translit_cjk_compat file to get the
38 original head and tail.
40 We want to replace only the part of the file between
41 “translit_start” and “translit_end”
42 '''
43 head = tail = ''
44 with open(filename, mode='r') as translit_file:
45 for line in translit_file:
46 head = head + line
47 if line.startswith('translit_start'):
48 break
49 for line in translit_file:
50 if line.startswith('translit_end'):
51 tail = line
52 break
53 for line in translit_file:
54 tail = tail + line
55 return (head, tail)
57 def output_head(translit_file, unicode_version, head=''):
58 '''Write the header of the output file, i.e. the part of the file
59 before the “translit_start” line.
60 '''
61 if ARGS.input_file and head:
62 translit_file.write(head)
63 else:
64 translit_file.write('escape_char /\n')
65 translit_file.write('comment_char %\n')
66 translit_file.write('\n')
67 translit_file.write('% Transliterations of CJK compatibility ')
68 translit_file.write('characters.\n')
69 translit_file.write('% Generated automatically from UnicodeData.txt '
70 + 'by gen_translit_cjk_compat.py '
71 + 'on {:s} '.format(time.strftime('%Y-%m-%d'))
72 + 'for Unicode {:s}.\n'.format(unicode_version))
73 translit_file.write('\n')
74 translit_file.write('LC_CTYPE\n')
75 translit_file.write('\n')
76 translit_file.write('translit_start\n')
78 def output_tail(translit_file, tail=''):
79 '''Write the tail of the output file'''
80 if ARGS.input_file and tail:
81 translit_file.write(tail)
82 else:
83 translit_file.write('translit_end\n')
84 translit_file.write('\n')
85 translit_file.write('END LC_CTYPE\n')
87 def special_decompose(code_point_list):
88 '''
89 Decompositions which are not in UnicodeData.txt at all but which
90 were used in the original translit_cjk_compat file in glibc and
91 which seem to make sense. I want to keep the update of
92 translit_cjk_compat close to the spirit of the original file,
93 therefore I added this special decomposition rules here.
94 '''
95 special_decompose_dict = {
96 (0x2215,): [0x002F], # ∕ → /
97 (0x00B2,): [0x005E, 0x0032], # ² → ^2
98 (0x03BC,): [0x00B5], # μ → µ (GREEK SMALL LETTER MU → MICRO SIGN)
99 (0x2113,): [0x006C], # ℓ → l
100 (0x00B3,): [0x005E, 0x0033], # ³ → ^3
101 (0x00B5,): [0x0075], # µ → u
102 (0x03BC, 0x2113): [0x03BC, 0x006C], # μℓ → μl
103 (0x0072, 0x0061, 0x0064, 0x2215, 0x0073, 0x00B2): [
104 0x0072, 0x0061, 0x0064, 0x002F, 0x0073, 0x00B2],
105 (0x006D, 0x2215, 0x0073, 0x00B2): [0x006D, 0x002F, 0x0073, 0x00B2],
107 if tuple(code_point_list) in special_decompose_dict:
108 return special_decompose_dict[tuple(code_point_list)]
109 else:
110 return code_point_list
112 def output_transliteration(translit_file):
113 '''Write the new transliteration to the output file'''
114 translit_file.write('\n')
115 for code_point in sorted(unicode_utils.UNICODE_ATTRIBUTES):
116 name = unicode_utils.UNICODE_ATTRIBUTES[code_point]['name']
117 decomposition = unicode_utils.UNICODE_ATTRIBUTES[
118 code_point]['decomposition']
119 if decomposition.startswith('<square>'):
120 decomposition = decomposition[9:]
121 decomposed_code_points = [[int(x, 16)
122 for x in decomposition.split(' ')]]
123 if decomposed_code_points[0]:
124 while True:
125 special_decomposed_code_points = special_decompose(
126 decomposed_code_points[-1])
127 if (special_decomposed_code_points
128 != decomposed_code_points[-1]):
129 decomposed_code_points.append(
130 special_decomposed_code_points)
131 continue
132 special_decomposed_code_points = []
133 for decomposed_code_point in decomposed_code_points[-1]:
134 special_decomposed_code_points += special_decompose(
135 [decomposed_code_point])
136 if (special_decomposed_code_points
137 == decomposed_code_points[-1]):
138 break
139 decomposed_code_points.append(
140 special_decomposed_code_points)
141 translit_file.write('% {:s}\n'.format(name))
142 translit_file.write('{:s} '.format(
143 unicode_utils.ucs_symbol(code_point)))
144 for index in range(0, len(decomposed_code_points)):
145 if index > 0:
146 translit_file.write(';')
147 if len(decomposed_code_points[index]) > 1:
148 translit_file.write('"')
149 for decomposed_code_point in decomposed_code_points[index]:
150 translit_file.write('{:s}'.format(
151 unicode_utils.ucs_symbol(decomposed_code_point)))
152 if len(decomposed_code_points[index]) > 1:
153 translit_file.write('"')
154 translit_file.write('\n')
155 for code_point in sorted(unicode_utils.UNICODE_ATTRIBUTES):
156 name = unicode_utils.UNICODE_ATTRIBUTES[code_point]['name']
157 decomposition = unicode_utils.UNICODE_ATTRIBUTES[
158 code_point]['decomposition']
159 if decomposition and name.startswith('CJK COMPATIBILITY IDEOGRAPH'):
160 decomposed_code_points = [int(x, 16)
161 for x in decomposition.split(' ')]
162 if len(decomposed_code_points) != 1:
163 sys.stderr.write(
164 'Unexpected decomposition length {:x} {:s} {:s}\n'.format(
165 code_point, name, decomposition))
166 exit(1)
167 translit_file.write('% {:s}\n'.format(name))
168 translit_file.write('{:s} '.format(
169 unicode_utils.ucs_symbol(code_point)))
170 for decomposed_code_point in decomposed_code_points:
171 translit_file.write('{:s}'.format(
172 unicode_utils.ucs_symbol(decomposed_code_point)))
173 translit_file.write('\n')
174 translit_file.write('\n')
176 if __name__ == "__main__":
177 PARSER = argparse.ArgumentParser(
178 description='''
179 Generate a translit_cjk_compat file from UnicodeData.txt.
180 ''')
181 PARSER.add_argument(
182 '-u', '--unicode_data_file',
183 nargs='?',
184 type=str,
185 default='UnicodeData.txt',
186 help=('The UnicodeData.txt file to read, '
187 + 'default: %(default)s'))
188 PARSER.add_argument(
189 '-i', '--input_file',
190 nargs='?',
191 type=str,
192 help=''' The original glibc/localedata/locales/translit_cjk_compat
193 file.''')
194 PARSER.add_argument(
195 '-o', '--output_file',
196 nargs='?',
197 type=str,
198 default='translit_cjk_compat.new',
199 help='''The new translit_cjk_compat file, default: %(default)s. If the
200 original glibc/localedata/locales/translit_cjk_compat file has
201 been given as an option, the header up to the
202 “translit_start” line and the tail from the “translit_end”
203 line to the end of the file will be copied unchanged into the
204 output file. ''')
205 PARSER.add_argument(
206 '--unicode_version',
207 nargs='?',
208 required=True,
209 type=str,
210 help='The Unicode version of the input files used.')
211 ARGS = PARSER.parse_args()
213 unicode_utils.fill_attributes(ARGS.unicode_data_file)
214 HEAD = TAIL = ''
215 if ARGS.input_file:
216 (HEAD, TAIL) = read_input_file(ARGS.input_file)
217 with open(ARGS.output_file, mode='w') as TRANSLIT_FILE:
218 output_head(TRANSLIT_FILE, ARGS.unicode_version, head=HEAD)
219 output_transliteration(TRANSLIT_FILE)
220 output_tail(TRANSLIT_FILE, tail=TAIL)