Debian Codegraph @

src/ucd-tools/tools/scripts.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright (C) 2012-2018 Reece H. Dunn
4 #
5 # This file is part of ucd-tools.
6 #
7 # ucd-tools is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # ucd-tools is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sys
22 import ucd
23
24 ucd_rootdir = sys.argv[1]
25 ucd_version = sys.argv[2]
26
27 unicode_chars = {}
28 for data in ucd.parse_ucd_data(ucd_rootdir, 'Scripts'):
29 for codepoint in data['Range']:
30 unicode_chars[codepoint] = data['Script']
31 if '--with-csur' in sys.argv:
32 for csur in ['Klingon']:
33 for data in ucd.parse_ucd_data('data/csur', csur):
34 for codepoint in data['CodePoint']:
35 unicode_chars[codepoint] = data['Script']
36
37 # This map is a combination of the information in the UnicodeData and Blocks
38 # data files. It is intended to reduce the number of character tables that
39 # need to be generated.
40 script_sets = [
41 (ucd.CodeRange('000000..00D7FF'), None, 'Multiple Blocks'),
42 (ucd.CodeRange('00D800..00F7FF'), 'Zzzz', 'Surrogates / Private Use Area'),
43 (ucd.CodeRange('00F800..02FAFF'), None, 'Multiple Blocks'),
44 (ucd.CodeRange('02FB00..0DFFFF'), 'Zzzz', 'Unassigned'),
45 (ucd.CodeRange('0E0000..0E01FF'), None, 'Multiple Blocks'),
46 (ucd.CodeRange('0E0200..10FFFF'), 'Zzzz', 'Unassigned'),
47 ]
48
49 # These scripts have many pages consisting of just this script:
50 special_scripts = []
51
52 script_tables = {}
53 for codepoints, script, comment in script_sets:
54 if not script:
55 table = {}
56 table_entry = None
57 table_codepoint = None
58 table_script = None
59 for i, codepoint in enumerate(codepoints):
60 try:
61 script = unicode_chars[codepoint]
62 except KeyError:
63 script = 'Zzzz' # Unknown
64 if (i % 256) == 0:
65 if table_entry:
66 if table_script in special_scripts:
67 table[table_codepoint] = table_script
68 elif table_script:
69 special_scripts.append(table_script)
70 table[table_codepoint] = table_script
71 else:
72 table[table_codepoint] = table_entry
73 table_entry = []
74 table_codepoint = codepoint
75 table_script = script
76 if script != table_script:
77 table_script = None
78 table_entry.append(script)
79 if table_entry:
80 if table_script in special_scripts:
81 table[table_codepoint] = table_script
82 else:
83 table[table_codepoint] = table_entry
84 script_tables['%s_%s' % (codepoints.first, codepoints.last)] = table
85
86 if __name__ == '__main__':
87 sys.stdout.write("""/* Unicode Scripts
88 *
89 * Copyright (C) 2012-2018 Reece H. Dunn
90 *
91 * This file is part of ucd-tools.
92 *
93 * ucd-tools is free software: you can redistribute it and/or modify
94 * it under the terms of the GNU General Public License as published by
95 * the Free Software Foundation, either version 3 of the License, or
96 * (at your option) any later version.
97 *
98 * ucd-tools is distributed in the hope that it will be useful,
99 * but WITHOUT ANY WARRANTY; without even the implied warranty of
100 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101 * GNU General Public License for more details.
102 *
103 * You should have received a copy of the GNU General Public License
104 * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
105 */
106
107 /* NOTE: This file is automatically generated from the Scripts.txt file in
108 * the Unicode Character database by the ucd-tools/tools/scripts.py script.
109 */
110
111 #include "ucd/ucd.h"
112
113 #include <stddef.h>
114
115 #define Adlm UCD_SCRIPT_Adlm
116 #define Afak UCD_SCRIPT_Afak
117 #define Aghb UCD_SCRIPT_Aghb
118 #define Ahom UCD_SCRIPT_Ahom
119 #define Arab UCD_SCRIPT_Arab
120 #define Armi UCD_SCRIPT_Armi
121 #define Armn UCD_SCRIPT_Armn
122 #define Avst UCD_SCRIPT_Avst
123 #define Bali UCD_SCRIPT_Bali
124 #define Bamu UCD_SCRIPT_Bamu
125 #define Bass UCD_SCRIPT_Bass
126 #define Batk UCD_SCRIPT_Batk
127 #define Beng UCD_SCRIPT_Beng
128 #define Bhks UCD_SCRIPT_Bhks
129 #define Blis UCD_SCRIPT_Blis
130 #define Bopo UCD_SCRIPT_Bopo
131 #define Brah UCD_SCRIPT_Brah
132 #define Brai UCD_SCRIPT_Brai
133 #define Bugi UCD_SCRIPT_Bugi
134 #define Buhd UCD_SCRIPT_Buhd
135 #define Cakm UCD_SCRIPT_Cakm
136 #define Cans UCD_SCRIPT_Cans
137 #define Cari UCD_SCRIPT_Cari
138 #define Cham UCD_SCRIPT_Cham
139 #define Cher UCD_SCRIPT_Cher
140 #define Cirt UCD_SCRIPT_Cirt
141 #define Copt UCD_SCRIPT_Copt
142 #define Cprt UCD_SCRIPT_Cprt
143 #define Cyrl UCD_SCRIPT_Cyrl
144 #define Cyrs UCD_SCRIPT_Cyrs
145 #define Deva UCD_SCRIPT_Deva
146 #define Dogr UCD_SCRIPT_Dogr
147 #define Dsrt UCD_SCRIPT_Dsrt
148 #define Dupl UCD_SCRIPT_Dupl
149 #define Egyd UCD_SCRIPT_Egyd
150 #define Egyh UCD_SCRIPT_Egyh
151 #define Egyp UCD_SCRIPT_Egyp
152 #define Elba UCD_SCRIPT_Elba
153 #define Ethi UCD_SCRIPT_Ethi
154 #define Geok UCD_SCRIPT_Geok
155 #define Geor UCD_SCRIPT_Geor
156 #define Glag UCD_SCRIPT_Glag
157 #define Gong UCD_SCRIPT_Gong
158 #define Gonm UCD_SCRIPT_Gonm
159 #define Goth UCD_SCRIPT_Goth
160 #define Gran UCD_SCRIPT_Gran
161 #define Grek UCD_SCRIPT_Grek
162 #define Gujr UCD_SCRIPT_Gujr
163 #define Guru UCD_SCRIPT_Guru
164 #define Hang UCD_SCRIPT_Hang
165 #define Hani UCD_SCRIPT_Hani
166 #define Hano UCD_SCRIPT_Hano
167 #define Hans UCD_SCRIPT_Hans
168 #define Hant UCD_SCRIPT_Hant
169 #define Hatr UCD_SCRIPT_Hatr
170 #define Hebr UCD_SCRIPT_Hebr
171 #define Hira UCD_SCRIPT_Hira
172 #define Hluw UCD_SCRIPT_Hluw
173 #define Hmng UCD_SCRIPT_Hmng
174 #define Hrkt UCD_SCRIPT_Hrkt
175 #define Hung UCD_SCRIPT_Hung
176 #define Inds UCD_SCRIPT_Inds
177 #define Ital UCD_SCRIPT_Ital
178 #define Java UCD_SCRIPT_Java
179 #define Jpan UCD_SCRIPT_Jpan
180 #define Jurc UCD_SCRIPT_Jurc
181 #define Kali UCD_SCRIPT_Kali
182 #define Kana UCD_SCRIPT_Kana
183 #define Khar UCD_SCRIPT_Khar
184 #define Khmr UCD_SCRIPT_Khmr
185 #define Khoj UCD_SCRIPT_Khoj
186 #define Knda UCD_SCRIPT_Knda
187 #define Kore UCD_SCRIPT_Kore
188 #define Kpel UCD_SCRIPT_Kpel
189 #define Kthi UCD_SCRIPT_Kthi
190 #define Lana UCD_SCRIPT_Lana
191 #define Laoo UCD_SCRIPT_Laoo
192 #define Latf UCD_SCRIPT_Latf
193 #define Latg UCD_SCRIPT_Latg
194 #define Latn UCD_SCRIPT_Latn
195 #define Lepc UCD_SCRIPT_Lepc
196 #define Limb UCD_SCRIPT_Limb
197 #define Lina UCD_SCRIPT_Lina
198 #define Linb UCD_SCRIPT_Linb
199 #define Lisu UCD_SCRIPT_Lisu
200 #define Loma UCD_SCRIPT_Loma
201 #define Lyci UCD_SCRIPT_Lyci
202 #define Lydi UCD_SCRIPT_Lydi
203 #define Mahj UCD_SCRIPT_Mahj
204 #define Maka UCD_SCRIPT_Maka
205 #define Mand UCD_SCRIPT_Mand
206 #define Mani UCD_SCRIPT_Mani
207 #define Marc UCD_SCRIPT_Marc
208 #define Maya UCD_SCRIPT_Maya
209 #define Medf UCD_SCRIPT_Medf
210 #define Mend UCD_SCRIPT_Mend
211 #define Merc UCD_SCRIPT_Merc
212 #define Mero UCD_SCRIPT_Mero
213 #define Mlym UCD_SCRIPT_Mlym
214 #define Modi UCD_SCRIPT_Modi
215 #define Mong UCD_SCRIPT_Mong
216 #define Moon UCD_SCRIPT_Moon
217 #define Mroo UCD_SCRIPT_Mroo
218 #define Mtei UCD_SCRIPT_Mtei
219 #define Mult UCD_SCRIPT_Mult
220 #define Mymr UCD_SCRIPT_Mymr
221 #define Narb UCD_SCRIPT_Narb
222 #define Nbat UCD_SCRIPT_Nbat
223 #define Newa UCD_SCRIPT_Newa
224 #define Nkgb UCD_SCRIPT_Nkgb
225 #define Nkoo UCD_SCRIPT_Nkoo
226 #define Nshu UCD_SCRIPT_Nshu
227 #define Ogam UCD_SCRIPT_Ogam
228 #define Olck UCD_SCRIPT_Olck
229 #define Orkh UCD_SCRIPT_Orkh
230 #define Orya UCD_SCRIPT_Orya
231 #define Osge UCD_SCRIPT_Osge
232 #define Osma UCD_SCRIPT_Osma
233 #define Palm UCD_SCRIPT_Palm
234 #define Pauc UCD_SCRIPT_Pauc
235 #define Perm UCD_SCRIPT_Perm
236 #define Phag UCD_SCRIPT_Phag
237 #define Phli UCD_SCRIPT_Phli
238 #define Phlp UCD_SCRIPT_Phlp
239 #define Phlv UCD_SCRIPT_Phlv
240 #define Phnx UCD_SCRIPT_Phnx
241 #define Plrd UCD_SCRIPT_Plrd
242 #define Prti UCD_SCRIPT_Prti
243 #define Qaak UCD_SCRIPT_Qaak
244 #define Rjng UCD_SCRIPT_Rjng
245 #define Rohg UCD_SCRIPT_Rohg
246 #define Roro UCD_SCRIPT_Roro
247 #define Runr UCD_SCRIPT_Runr
248 #define Samr UCD_SCRIPT_Samr
249 #define Sara UCD_SCRIPT_Sara
250 #define Sarb UCD_SCRIPT_Sarb
251 #define Saur UCD_SCRIPT_Saur
252 #define Sgnw UCD_SCRIPT_Sgnw
253 #define Shaw UCD_SCRIPT_Shaw
254 #define Shrd UCD_SCRIPT_Shrd
255 #define Sidd UCD_SCRIPT_Sidd
256 #define Sind UCD_SCRIPT_Sind
257 #define Sinh UCD_SCRIPT_Sinh
258 #define Sogd UCD_SCRIPT_Sogd
259 #define Sogo UCD_SCRIPT_Sogo
260 #define Sora UCD_SCRIPT_Sora
261 #define Soyo UCD_SCRIPT_Soyo
262 #define Sund UCD_SCRIPT_Sund
263 #define Sylo UCD_SCRIPT_Sylo
264 #define Syrc UCD_SCRIPT_Syrc
265 #define Syre UCD_SCRIPT_Syre
266 #define Syrj UCD_SCRIPT_Syrj
267 #define Syrn UCD_SCRIPT_Syrn
268 #define Tagb UCD_SCRIPT_Tagb
269 #define Takr UCD_SCRIPT_Takr
270 #define Tale UCD_SCRIPT_Tale
271 #define Talu UCD_SCRIPT_Talu
272 #define Taml UCD_SCRIPT_Taml
273 #define Tang UCD_SCRIPT_Tang
274 #define Tavt UCD_SCRIPT_Tavt
275 #define Telu UCD_SCRIPT_Telu
276 #define Teng UCD_SCRIPT_Teng
277 #define Tfng UCD_SCRIPT_Tfng
278 #define Tglg UCD_SCRIPT_Tglg
279 #define Thaa UCD_SCRIPT_Thaa
280 #define Thai UCD_SCRIPT_Thai
281 #define Tibt UCD_SCRIPT_Tibt
282 #define Tirh UCD_SCRIPT_Tirh
283 #define Ugar UCD_SCRIPT_Ugar
284 #define Vaii UCD_SCRIPT_Vaii
285 #define Visp UCD_SCRIPT_Visp
286 #define Wara UCD_SCRIPT_Wara
287 #define Wole UCD_SCRIPT_Wole
288 #define Xpeo UCD_SCRIPT_Xpeo
289 #define Xsux UCD_SCRIPT_Xsux
290 #define Yiii UCD_SCRIPT_Yiii
291 #define Zanb UCD_SCRIPT_Zanb
292 #define Zinh UCD_SCRIPT_Zinh
293 #define Zmth UCD_SCRIPT_Zmth
294 #define Zsym UCD_SCRIPT_Zsym
295 #define Zxxx UCD_SCRIPT_Zxxx
296 #define Zyyy UCD_SCRIPT_Zyyy
297 #define Zzzz UCD_SCRIPT_Zzzz
298
299 /* Unicode Character Data %s */
300 """ % ucd_version)
301
302 for script in special_scripts:
303 sys.stdout.write('\n')
304 sys.stdout.write('static const uint8_t scripts_%s[256] =\n' % script)
305 sys.stdout.write('{')
306 for i in range(0, 256):
307 if (i % 16) == 0:
308 sys.stdout.write('\n\t/* %02X */' % i)
309 sys.stdout.write(' %s,' % script)
310 sys.stdout.write('\n};\n')
311
312 for codepoints, script, comment in script_sets:
313 if not script:
314 tables = script_tables['%s_%s' % (codepoints.first, codepoints.last)]
315 for codepoint in sorted(tables.keys()):
316 table = tables[codepoint]
317 if table in special_scripts:
318 continue
319
320 sys.stdout.write('\n')
321 sys.stdout.write('static const uint8_t scripts_%s[256] =\n' % codepoint)
322 sys.stdout.write('{')
323 for i, script in enumerate(table):
324 if (i % 16) == 0:
325 sys.stdout.write('\n\t/* %02X */' % i)
326 sys.stdout.write(' %s,' % script)
327 sys.stdout.write('\n};\n')
328
329 for codepoints, script, comment in script_sets:
330 if not script:
331 table_index = '%s_%s' % (codepoints.first, codepoints.last)
332 sys.stdout.write('\n')
333 sys.stdout.write('static const uint8_t *scripts_%s[] =\n' % table_index)
334 sys.stdout.write('{\n')
335 for codepoint, table in sorted(script_tables[table_index].items()):
336 if isinstance(table, str):
337 sys.stdout.write('\tscripts_%s, /* %s */\n' % (table, codepoint))
338 else:
339 sys.stdout.write('\tscripts_%s,\n' % codepoint)
340 sys.stdout.write('};\n')
341
342 sys.stdout.write('\n')
343 sys.stdout.write('ucd_script ucd_lookup_script(codepoint_t c)\n')
344 sys.stdout.write('{\n')
345 for codepoints, script, comment in script_sets:
346 if script:
347 sys.stdout.write('\tif (c <= 0x%s) return %s; /* %s : %s */\n' % (codepoints.last, script, codepoints, comment))
348 else:
349 sys.stdout.write('\tif (c <= 0x%s) /* %s */\n' % (codepoints.last, codepoints))
350 sys.stdout.write('\t{\n')
351 sys.stdout.write('\t\tconst uint8_t *table = scripts_%s_%s[(c - 0x%s) / 256];\n' % (codepoints.first, codepoints.last, codepoints.first))
352 sys.stdout.write('\t\treturn (ucd_script)table[c % 256];\n')
353 sys.stdout.write('\t}\n')
354 sys.stdout.write('\treturn Zzzz; /* Invalid Unicode Codepoint */\n')
355 sys.stdout.write('}\n')