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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = ucd_rootdir.split('-')[-1]
  23. unicode_chars = {}
  24. for data in ucd.parse_ucd_data(ucd_rootdir, 'UnicodeData'):
  25. if isinstance(data['CodePoint'], ucd.CodePoint):
  26. unicode_chars[data['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. category_tables = {}
  44. for codepoints, category, comment in category_sets:
  45. if not category:
  46. table = {}
  47. table_entry = None
  48. table_codepoint = None
  49. is_unassigned = True
  50. for i, codepoint in enumerate(codepoints):
  51. if (i % 256) == 0:
  52. if table_entry:
  53. if is_unassigned:
  54. table[table_codepoint] = None
  55. else:
  56. table[table_codepoint] = table_entry
  57. table_entry = []
  58. table_codepoint = codepoint
  59. is_unassigned = True
  60. try:
  61. category = unicode_chars[codepoint]
  62. is_unassigned = False
  63. except KeyError:
  64. category = 'Cn' # Unassigned
  65. table_entry.append(category)
  66. if table_entry:
  67. if is_unassigned:
  68. table[table_codepoint] = None
  69. else:
  70. table[table_codepoint] = table_entry
  71. category_tables['%s_%s' % (codepoints.first, codepoints.last)] = table
  72. if __name__ == '__main__':
  73. sys.stdout.write("""/* Unicode General Categories
  74. *
  75. * Copyright (C) 2012 Reece H. Dunn
  76. *
  77. * This file is part of ucd-tools.
  78. *
  79. * ucd-tools is free software: you can redistribute it and/or modify
  80. * it under the terms of the GNU General Public License as published by
  81. * the Free Software Foundation, either version 3 of the License, or
  82. * (at your option) any later version.
  83. *
  84. * ucd-tools is distributed in the hope that it will be useful,
  85. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  86. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  87. * GNU General Public License for more details.
  88. *
  89. * You should have received a copy of the GNU General Public License
  90. * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
  91. */
  92. // NOTE: This file is automatically generated from the UnicodeData.txt file in
  93. // the Unicode Character database by the ucd-tools/tools/categories.py script.
  94. #include "ucd/ucd.h"
  95. #include <stddef.h>
  96. using namespace ucd;
  97. // Unicode Character Data %s
  98. """ % ucd_version)
  99. for codepoints, category, comment in category_sets:
  100. if not category:
  101. tables = category_tables['%s_%s' % (codepoints.first, codepoints.last)]
  102. for codepoint in sorted(tables.keys()):
  103. table = tables[codepoint]
  104. if not table:
  105. continue
  106. sys.stdout.write('\n')
  107. sys.stdout.write('static const ucd::category categories_%s[256] =\n' % codepoint)
  108. sys.stdout.write('{')
  109. for i, category in enumerate(table):
  110. if (i % 16) == 0:
  111. sys.stdout.write('\n\t/* %02X */' % i)
  112. sys.stdout.write(' %s,' % category)
  113. sys.stdout.write('\n};\n')
  114. for codepoints, category, comment in category_sets:
  115. if not category:
  116. table_index = '%s_%s' % (codepoints.first, codepoints.last)
  117. sys.stdout.write('\n')
  118. sys.stdout.write('static const ucd::category *categories_%s[] =\n' % table_index)
  119. sys.stdout.write('{\n')
  120. for codepoint, table in sorted(category_tables[table_index].items()):
  121. if table:
  122. sys.stdout.write('\tcategories_%s,\n' % codepoint)
  123. else:
  124. sys.stdout.write('\tNULL, // %s : Unassigned\n' % codepoint)
  125. sys.stdout.write('};\n')
  126. sys.stdout.write('\n')
  127. sys.stdout.write('ucd::category ucd::lookup_category(codepoint_t c)\n')
  128. sys.stdout.write('{\n')
  129. for codepoints, category, comment in category_sets:
  130. if category:
  131. sys.stdout.write('\tif (c <= 0x%s) return %s; // %s : %s\n' % (codepoints.last, category, codepoints, comment))
  132. else:
  133. sys.stdout.write('\tif (c <= 0x%s) // %s\n' % (codepoints.last, codepoints))
  134. sys.stdout.write('\t{\n')
  135. sys.stdout.write('\t\tconst ucd::category *table = categories_%s_%s[(c - 0x%s) / 256];\n' % (codepoints.first, codepoints.last, codepoints.first))
  136. sys.stdout.write('\t\treturn table ? table[c % 256] : Cn;\n')
  137. sys.stdout.write('\t}\n')
  138. sys.stdout.write('\treturn Ci;\n')
  139. sys.stdout.write('}\n')