eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scripts.py 11KB

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