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

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