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.

categories.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, 'UnicodeData'):
  25. for codepoint in data['CodePoint']:
  26. unicode_chars[codepoint] = data['GeneralCategory']
  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. category_sets = [
  31. (ucd.CodeRange('000000..00D7FF'), None, 'Multiple Blocks'),
  32. (ucd.CodeRange('00D800..00DFFF'), 'Cs', 'Surrogates'),
  33. (ucd.CodeRange('00E000..00F8FF'), 'Co', 'Private Use Area'),
  34. (ucd.CodeRange('00F900..02FAFF'), None, 'Multiple Blocks'),
  35. (ucd.CodeRange('02FB00..0DFFFF'), 'Cn', 'Unassigned'),
  36. (ucd.CodeRange('0E0000..0E01FF'), None, 'Multiple Blocks'),
  37. (ucd.CodeRange('0E0200..0EFFFF'), 'Cn', 'Unassigned'),
  38. (ucd.CodeRange('0F0000..0FFFFD'), 'Co', 'Plane 15 Private Use'),
  39. (ucd.CodeRange('0FFFFE..0FFFFF'), 'Cn', 'Plane 15 Private Use'),
  40. (ucd.CodeRange('100000..10FFFD'), 'Co', 'Plane 16 Private Use'),
  41. (ucd.CodeRange('10FFFE..10FFFF'), 'Cn', 'Plane 16 Private Use'),
  42. ]
  43. # These categories have many pages consisting of just this category:
  44. # Cn -- Unassigned
  45. # Lo -- CJK Ideographs
  46. special_categories = ['Cn', 'Lo', 'Sm', 'So']
  47. category_tables = {}
  48. for codepoints, category, comment in category_sets:
  49. if not category:
  50. table = {}
  51. table_entry = None
  52. table_codepoint = None
  53. table_category = None
  54. for i, codepoint in enumerate(codepoints):
  55. try:
  56. category = unicode_chars[codepoint]
  57. except KeyError:
  58. category = 'Cn' # Unassigned
  59. if (i % 256) == 0:
  60. if table_entry:
  61. if table_category in special_categories:
  62. table[table_codepoint] = table_category
  63. elif table_category:
  64. raise Exception('%s only table not in the special_categories list.' % table_category)
  65. else:
  66. table[table_codepoint] = table_entry
  67. table_entry = []
  68. table_codepoint = codepoint
  69. table_category = category
  70. if category != table_category:
  71. table_category = None
  72. table_entry.append(category)
  73. if table_entry:
  74. if table_category in special_categories:
  75. table[table_codepoint] = table_category
  76. else:
  77. table[table_codepoint] = table_entry
  78. category_tables['%s_%s' % (codepoints.first, codepoints.last)] = table
  79. if __name__ == '__main__':
  80. sys.stdout.write("""/* Unicode General Categories
  81. *
  82. * Copyright (C) 2012 Reece H. Dunn
  83. *
  84. * This file is part of ucd-tools.
  85. *
  86. * ucd-tools is free software: you can redistribute it and/or modify
  87. * it under the terms of the GNU General Public License as published by
  88. * the Free Software Foundation, either version 3 of the License, or
  89. * (at your option) any later version.
  90. *
  91. * ucd-tools is distributed in the hope that it will be useful,
  92. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  93. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  94. * GNU General Public License for more details.
  95. *
  96. * You should have received a copy of the GNU General Public License
  97. * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
  98. */
  99. // NOTE: This file is automatically generated from the UnicodeData.txt file in
  100. // the Unicode Character database by the ucd-tools/tools/categories.py script.
  101. #include "ucd/ucd.h"
  102. #include <stddef.h>
  103. using namespace ucd;
  104. // Unicode Character Data %s
  105. """ % ucd_version)
  106. for category in special_categories:
  107. sys.stdout.write('\n')
  108. sys.stdout.write('static const uint8_t categories_%s[256] =\n' % category)
  109. sys.stdout.write('{')
  110. for i in range(0, 256):
  111. if (i % 16) == 0:
  112. sys.stdout.write('\n\t/* %02X */' % i)
  113. sys.stdout.write(' %s,' % category)
  114. sys.stdout.write('\n};\n')
  115. for codepoints, category, comment in category_sets:
  116. if not category:
  117. tables = category_tables['%s_%s' % (codepoints.first, codepoints.last)]
  118. for codepoint in sorted(tables.keys()):
  119. table = tables[codepoint]
  120. if table in special_categories:
  121. continue
  122. sys.stdout.write('\n')
  123. sys.stdout.write('static const uint8_t categories_%s[256] =\n' % codepoint)
  124. sys.stdout.write('{')
  125. for i, category in enumerate(table):
  126. if (i % 16) == 0:
  127. sys.stdout.write('\n\t/* %02X */' % i)
  128. sys.stdout.write(' %s,' % category)
  129. sys.stdout.write('\n};\n')
  130. for codepoints, category, comment in category_sets:
  131. if not category:
  132. table_index = '%s_%s' % (codepoints.first, codepoints.last)
  133. sys.stdout.write('\n')
  134. sys.stdout.write('static const uint8_t *categories_%s[] =\n' % table_index)
  135. sys.stdout.write('{\n')
  136. for codepoint, table in sorted(category_tables[table_index].items()):
  137. if isinstance(table, str):
  138. sys.stdout.write('\tcategories_%s, // %s\n' % (table, codepoint))
  139. else:
  140. sys.stdout.write('\tcategories_%s,\n' % codepoint)
  141. sys.stdout.write('};\n')
  142. sys.stdout.write('\n')
  143. sys.stdout.write('ucd::category ucd::lookup_category(codepoint_t c)\n')
  144. sys.stdout.write('{\n')
  145. for codepoints, category, comment in category_sets:
  146. if category:
  147. sys.stdout.write('\tif (c <= 0x%s) return %s; // %s : %s\n' % (codepoints.last, category, codepoints, comment))
  148. else:
  149. sys.stdout.write('\tif (c <= 0x%s) // %s\n' % (codepoints.last, codepoints))
  150. sys.stdout.write('\t{\n')
  151. sys.stdout.write('\t\tconst uint8_t *table = categories_%s_%s[(c - 0x%s) / 256];\n' % (codepoints.first, codepoints.last, codepoints.first))
  152. sys.stdout.write('\t\treturn (ucd::category)table[c % 256];\n')
  153. sys.stdout.write('\t}\n')
  154. sys.stdout.write('\treturn Ii; // Invalid Unicode Codepoint\n')
  155. sys.stdout.write('}\n')
  156. sys.stdout.write("""
  157. ucd::category_group ucd::lookup_category_group(category c)
  158. {
  159. switch (c)
  160. {
  161. case Cc: case Cf: case Cn: case Co: case Cs:
  162. return C;
  163. case Ll: case Lm: case Lo: case Lt: case Lu:
  164. return L;
  165. case Mc: case Me: case Mn:
  166. return M;
  167. case Nd: case Nl: case No:
  168. return N;
  169. case Pc: case Pd: case Pe: case Pf: case Pi: case Po: case Ps:
  170. return P;
  171. case Sc: case Sk: case Sm: case So:
  172. return S;
  173. case Zl: case Zp: case Zs:
  174. return Z;
  175. case Ii:
  176. return I;
  177. }
  178. }
  179. ucd::category_group ucd::lookup_category_group(codepoint_t c)
  180. {
  181. return lookup_category_group(lookup_category(c));
  182. }
  183. """)