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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/python
  2. # Copyright (C) 2012 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. for data in ucd.parse_ucd_data('supplemental', 'Klingon'):
  28. for codepoint in data['CodePoint']:
  29. unicode_chars[codepoint] = data['Script']
  30. # This map is a combination of the information in the UnicodeData and Blocks
  31. # data files. It is intended to reduce the number of character tables that
  32. # need to be generated.
  33. script_sets = [
  34. (ucd.CodeRange('000000..00D7FF'), None, 'Multiple Blocks'),
  35. (ucd.CodeRange('00D800..00F7FF'), 'Zzzz', 'Surrogates / Private Use Area'),
  36. (ucd.CodeRange('00F800..02FAFF'), None, 'Multiple Blocks'),
  37. (ucd.CodeRange('02FB00..0DFFFF'), 'Zzzz', 'Unassigned'),
  38. (ucd.CodeRange('0E0000..0E01FF'), None, 'Multiple Blocks'),
  39. (ucd.CodeRange('0E0200..10FFFF'), 'Zzzz', 'Unassigned'),
  40. ]
  41. # These scripts have many pages consisting of just this script:
  42. special_scripts = []
  43. script_tables = {}
  44. for codepoints, script, comment in script_sets:
  45. if not script:
  46. table = {}
  47. table_entry = None
  48. table_codepoint = None
  49. table_script = None
  50. for i, codepoint in enumerate(codepoints):
  51. try:
  52. script = unicode_chars[codepoint]
  53. except KeyError:
  54. script = 'Zzzz' # Unknown
  55. if (i % 256) == 0:
  56. if table_entry:
  57. if table_script in special_scripts:
  58. table[table_codepoint] = table_script
  59. elif table_script:
  60. special_scripts.append(table_script)
  61. table[table_codepoint] = table_script
  62. else:
  63. table[table_codepoint] = table_entry
  64. table_entry = []
  65. table_codepoint = codepoint
  66. table_script = script
  67. if script != table_script:
  68. table_script = None
  69. table_entry.append(script)
  70. if table_entry:
  71. if table_script in special_scripts:
  72. table[table_codepoint] = table_script
  73. else:
  74. table[table_codepoint] = table_entry
  75. script_tables['%s_%s' % (codepoints.first, codepoints.last)] = table
  76. if __name__ == '__main__':
  77. sys.stdout.write("""/* Unicode Scripts
  78. *
  79. * Copyright (C) 2012 Reece H. Dunn
  80. *
  81. * This file is part of ucd-tools.
  82. *
  83. * ucd-tools is free software: you can redistribute it and/or modify
  84. * it under the terms of the GNU General Public License as published by
  85. * the Free Software Foundation, either version 3 of the License, or
  86. * (at your option) any later version.
  87. *
  88. * ucd-tools is distributed in the hope that it will be useful,
  89. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  90. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  91. * GNU General Public License for more details.
  92. *
  93. * You should have received a copy of the GNU General Public License
  94. * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
  95. */
  96. // NOTE: This file is automatically generated from the Scripts.txt file in
  97. // the Unicode Character database by the ucd-tools/tools/scripts.py script.
  98. #include "ucd/ucd.h"
  99. #include <stddef.h>
  100. using namespace ucd;
  101. // Unicode Character Data %s
  102. """ % ucd_version)
  103. for script in special_scripts:
  104. sys.stdout.write('\n')
  105. sys.stdout.write('static const uint8_t scripts_%s[256] =\n' % script)
  106. sys.stdout.write('{')
  107. for i in range(0, 256):
  108. if (i % 16) == 0:
  109. sys.stdout.write('\n\t/* %02X */' % i)
  110. sys.stdout.write(' %s,' % script)
  111. sys.stdout.write('\n};\n')
  112. for codepoints, script, comment in script_sets:
  113. if not script:
  114. tables = script_tables['%s_%s' % (codepoints.first, codepoints.last)]
  115. for codepoint in sorted(tables.keys()):
  116. table = tables[codepoint]
  117. if table in special_scripts:
  118. continue
  119. sys.stdout.write('\n')
  120. sys.stdout.write('static const uint8_t scripts_%s[256] =\n' % codepoint)
  121. sys.stdout.write('{')
  122. for i, script in enumerate(table):
  123. if (i % 16) == 0:
  124. sys.stdout.write('\n\t/* %02X */' % i)
  125. sys.stdout.write(' %s,' % script)
  126. sys.stdout.write('\n};\n')
  127. for codepoints, script, comment in script_sets:
  128. if not script:
  129. table_index = '%s_%s' % (codepoints.first, codepoints.last)
  130. sys.stdout.write('\n')
  131. sys.stdout.write('static const uint8_t *scripts_%s[] =\n' % table_index)
  132. sys.stdout.write('{\n')
  133. for codepoint, table in sorted(script_tables[table_index].items()):
  134. if isinstance(table, str):
  135. sys.stdout.write('\tscripts_%s, // %s\n' % (table, codepoint))
  136. else:
  137. sys.stdout.write('\tscripts_%s,\n' % codepoint)
  138. sys.stdout.write('};\n')
  139. sys.stdout.write('\n')
  140. sys.stdout.write('ucd::script ucd::lookup_script(codepoint_t c)\n')
  141. sys.stdout.write('{\n')
  142. for codepoints, script, comment in script_sets:
  143. if script:
  144. sys.stdout.write('\tif (c <= 0x%s) return %s; // %s : %s\n' % (codepoints.last, script, codepoints, comment))
  145. else:
  146. sys.stdout.write('\tif (c <= 0x%s) // %s\n' % (codepoints.last, codepoints))
  147. sys.stdout.write('\t{\n')
  148. sys.stdout.write('\t\tconst uint8_t *table = scripts_%s_%s[(c - 0x%s) / 256];\n' % (codepoints.first, codepoints.last, codepoints.first))
  149. sys.stdout.write('\t\treturn (ucd::script)table[c % 256];\n')
  150. sys.stdout.write('\t}\n')
  151. sys.stdout.write('\treturn Zzzz; // Invalid Unicode Codepoint\n')
  152. sys.stdout.write('}\n')