nsUnicodeProperties.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* vim:set ts=4 sw=4 sts=4 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "nsUnicodeProperties.h"
  7. #include "nsUnicodePropertyData.cpp"
  8. #include "mozilla/ArrayUtils.h"
  9. #include "nsCharTraits.h"
  10. #define UNICODE_BMP_LIMIT 0x10000
  11. #define UNICODE_LIMIT 0x110000
  12. const nsCharProps2&
  13. GetCharProps2(uint32_t aCh)
  14. {
  15. if (aCh < UNICODE_BMP_LIMIT) {
  16. return sCharProp2Values[sCharProp2Pages[0][aCh >> kCharProp2CharBits]]
  17. [aCh & ((1 << kCharProp2CharBits) - 1)];
  18. }
  19. if (aCh < (kCharProp2MaxPlane + 1) * 0x10000) {
  20. return sCharProp2Values[sCharProp2Pages[sCharProp2Planes[(aCh >> 16) - 1]]
  21. [(aCh & 0xffff) >> kCharProp2CharBits]]
  22. [aCh & ((1 << kCharProp2CharBits) - 1)];
  23. }
  24. NS_NOTREACHED("Getting CharProps for codepoint outside Unicode range");
  25. // Default values for unassigned
  26. using namespace mozilla::unicode;
  27. static const nsCharProps2 undefined = {
  28. VERTICAL_ORIENTATION_R,
  29. XIDMOD_NOT_CHARS
  30. };
  31. return undefined;
  32. }
  33. namespace mozilla {
  34. namespace unicode {
  35. /*
  36. To store properties for a million Unicode codepoints compactly, we use
  37. a three-level array structure, with the Unicode values considered as
  38. three elements: Plane, Page, and Char.
  39. Space optimization happens because multiple Planes can refer to the same
  40. Page array, and multiple Pages can refer to the same Char array holding
  41. the actual values. In practice, most of the higher planes are empty and
  42. thus share the same data; and within the BMP, there are also many pages
  43. that repeat the same data for any given property.
  44. Plane is usually zero, so we skip a lookup in this case, and require
  45. that the Plane 0 pages are always the first set of entries in the Page
  46. array.
  47. The division of the remaining 16 bits into Page and Char fields is
  48. adjusted for each property (by experiment using the generation tool)
  49. to provide the most compact storage, depending on the distribution
  50. of values.
  51. */
  52. const nsIUGenCategory::nsUGenCategory sDetailedToGeneralCategory[] = {
  53. /*
  54. * The order here corresponds to the HB_UNICODE_GENERAL_CATEGORY_* constants
  55. * of the hb_unicode_general_category_t enum in gfx/harfbuzz/src/hb-unicode.h.
  56. */
  57. /* CONTROL */ nsIUGenCategory::kOther,
  58. /* FORMAT */ nsIUGenCategory::kOther,
  59. /* UNASSIGNED */ nsIUGenCategory::kOther,
  60. /* PRIVATE_USE */ nsIUGenCategory::kOther,
  61. /* SURROGATE */ nsIUGenCategory::kOther,
  62. /* LOWERCASE_LETTER */ nsIUGenCategory::kLetter,
  63. /* MODIFIER_LETTER */ nsIUGenCategory::kLetter,
  64. /* OTHER_LETTER */ nsIUGenCategory::kLetter,
  65. /* TITLECASE_LETTER */ nsIUGenCategory::kLetter,
  66. /* UPPERCASE_LETTER */ nsIUGenCategory::kLetter,
  67. /* COMBINING_MARK */ nsIUGenCategory::kMark,
  68. /* ENCLOSING_MARK */ nsIUGenCategory::kMark,
  69. /* NON_SPACING_MARK */ nsIUGenCategory::kMark,
  70. /* DECIMAL_NUMBER */ nsIUGenCategory::kNumber,
  71. /* LETTER_NUMBER */ nsIUGenCategory::kNumber,
  72. /* OTHER_NUMBER */ nsIUGenCategory::kNumber,
  73. /* CONNECT_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  74. /* DASH_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  75. /* CLOSE_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  76. /* FINAL_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  77. /* INITIAL_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  78. /* OTHER_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  79. /* OPEN_PUNCTUATION */ nsIUGenCategory::kPunctuation,
  80. /* CURRENCY_SYMBOL */ nsIUGenCategory::kSymbol,
  81. /* MODIFIER_SYMBOL */ nsIUGenCategory::kSymbol,
  82. /* MATH_SYMBOL */ nsIUGenCategory::kSymbol,
  83. /* OTHER_SYMBOL */ nsIUGenCategory::kSymbol,
  84. /* LINE_SEPARATOR */ nsIUGenCategory::kSeparator,
  85. /* PARAGRAPH_SEPARATOR */ nsIUGenCategory::kSeparator,
  86. /* SPACE_SEPARATOR */ nsIUGenCategory::kSeparator
  87. };
  88. const hb_unicode_general_category_t sICUtoHBcategory[U_CHAR_CATEGORY_COUNT] = {
  89. HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED, // U_GENERAL_OTHER_TYPES = 0,
  90. HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER, // U_UPPERCASE_LETTER = 1,
  91. HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER, // U_LOWERCASE_LETTER = 2,
  92. HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER, // U_TITLECASE_LETTER = 3,
  93. HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER, // U_MODIFIER_LETTER = 4,
  94. HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER, // U_OTHER_LETTER = 5,
  95. HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK, // U_NON_SPACING_MARK = 6,
  96. HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK, // U_ENCLOSING_MARK = 7,
  97. HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK, // U_COMBINING_SPACING_MARK = 8,
  98. HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER, // U_DECIMAL_DIGIT_NUMBER = 9,
  99. HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER, // U_LETTER_NUMBER = 10,
  100. HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER, // U_OTHER_NUMBER = 11,
  101. HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR, // U_SPACE_SEPARATOR = 12,
  102. HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR, // U_LINE_SEPARATOR = 13,
  103. HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR, // U_PARAGRAPH_SEPARATOR = 14,
  104. HB_UNICODE_GENERAL_CATEGORY_CONTROL, // U_CONTROL_CHAR = 15,
  105. HB_UNICODE_GENERAL_CATEGORY_FORMAT, // U_FORMAT_CHAR = 16,
  106. HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE, // U_PRIVATE_USE_CHAR = 17,
  107. HB_UNICODE_GENERAL_CATEGORY_SURROGATE, // U_SURROGATE = 18,
  108. HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION, // U_DASH_PUNCTUATION = 19,
  109. HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION, // U_START_PUNCTUATION = 20,
  110. HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION, // U_END_PUNCTUATION = 21,
  111. HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION, // U_CONNECTOR_PUNCTUATION = 22,
  112. HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION, // U_OTHER_PUNCTUATION = 23,
  113. HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL, // U_MATH_SYMBOL = 24,
  114. HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL, // U_CURRENCY_SYMBOL = 25,
  115. HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL, // U_MODIFIER_SYMBOL = 26,
  116. HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL, // U_OTHER_SYMBOL = 27,
  117. HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION, // U_INITIAL_PUNCTUATION = 28,
  118. HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION, // U_FINAL_PUNCTUATION = 29,
  119. };
  120. #define DEFINE_BMP_1PLANE_MAPPING_GET_FUNC(prefix_) \
  121. uint32_t Get##prefix_(uint32_t aCh) \
  122. { \
  123. if (aCh >= UNICODE_BMP_LIMIT) { \
  124. return aCh; \
  125. } \
  126. auto page = s##prefix_##Pages[aCh >> k##prefix_##CharBits]; \
  127. auto index = aCh & ((1 << k##prefix_##CharBits) - 1); \
  128. uint32_t v = s##prefix_##Values[page][index]; \
  129. return v ? v : aCh; \
  130. }
  131. // full-width mappings only exist for BMP characters; all others are
  132. // returned unchanged
  133. DEFINE_BMP_1PLANE_MAPPING_GET_FUNC(FullWidth)
  134. DEFINE_BMP_1PLANE_MAPPING_GET_FUNC(FullWidthInverse)
  135. bool
  136. IsClusterExtender(uint32_t aCh, uint8_t aCategory)
  137. {
  138. return ((aCategory >= HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK &&
  139. aCategory <= HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) ||
  140. (aCh >= 0x200c && aCh <= 0x200d) || // ZWJ, ZWNJ
  141. (aCh >= 0xff9e && aCh <= 0xff9f)); // katakana sound marks
  142. }
  143. enum HSType {
  144. HST_NONE = U_HST_NOT_APPLICABLE,
  145. HST_L = U_HST_LEADING_JAMO,
  146. HST_V = U_HST_VOWEL_JAMO,
  147. HST_T = U_HST_TRAILING_JAMO,
  148. HST_LV = U_HST_LV_SYLLABLE,
  149. HST_LVT = U_HST_LVT_SYLLABLE
  150. };
  151. static HSType
  152. GetHangulSyllableType(uint32_t aCh)
  153. {
  154. return HSType(u_getIntPropertyValue(aCh, UCHAR_HANGUL_SYLLABLE_TYPE));
  155. }
  156. void
  157. ClusterIterator::Next()
  158. {
  159. if (AtEnd()) {
  160. NS_WARNING("ClusterIterator has already reached the end");
  161. return;
  162. }
  163. uint32_t ch = *mPos++;
  164. if (NS_IS_HIGH_SURROGATE(ch) && mPos < mLimit &&
  165. NS_IS_LOW_SURROGATE(*mPos)) {
  166. ch = SURROGATE_TO_UCS4(ch, *mPos++);
  167. } else if ((ch & ~0xff) == 0x1100 ||
  168. (ch >= 0xa960 && ch <= 0xa97f) ||
  169. (ch >= 0xac00 && ch <= 0xd7ff)) {
  170. // Handle conjoining Jamo that make Hangul syllables
  171. HSType hangulState = GetHangulSyllableType(ch);
  172. while (mPos < mLimit) {
  173. ch = *mPos;
  174. HSType hangulType = GetHangulSyllableType(ch);
  175. switch (hangulType) {
  176. case HST_L:
  177. case HST_LV:
  178. case HST_LVT:
  179. if (hangulState == HST_L) {
  180. hangulState = hangulType;
  181. mPos++;
  182. continue;
  183. }
  184. break;
  185. case HST_V:
  186. if ((hangulState != HST_NONE) && (hangulState != HST_T) &&
  187. (hangulState != HST_LVT)) {
  188. hangulState = hangulType;
  189. mPos++;
  190. continue;
  191. }
  192. break;
  193. case HST_T:
  194. if (hangulState != HST_NONE && hangulState != HST_L) {
  195. hangulState = hangulType;
  196. mPos++;
  197. continue;
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. break;
  204. }
  205. }
  206. while (mPos < mLimit) {
  207. ch = *mPos;
  208. // Check for surrogate pairs; note that isolated surrogates will just
  209. // be treated as generic (non-cluster-extending) characters here,
  210. // which is fine for cluster-iterating purposes
  211. if (NS_IS_HIGH_SURROGATE(ch) && mPos < mLimit - 1 &&
  212. NS_IS_LOW_SURROGATE(*(mPos + 1))) {
  213. ch = SURROGATE_TO_UCS4(ch, *(mPos + 1));
  214. }
  215. if (!IsClusterExtender(ch)) {
  216. break;
  217. }
  218. mPos++;
  219. if (!IS_IN_BMP(ch)) {
  220. mPos++;
  221. }
  222. }
  223. NS_ASSERTION(mText < mPos && mPos <= mLimit,
  224. "ClusterIterator::Next has overshot the string!");
  225. }
  226. void
  227. ClusterReverseIterator::Next()
  228. {
  229. if (AtEnd()) {
  230. NS_WARNING("ClusterReverseIterator has already reached the end");
  231. return;
  232. }
  233. uint32_t ch;
  234. do {
  235. ch = *--mPos;
  236. if (NS_IS_LOW_SURROGATE(ch) && mPos > mLimit &&
  237. NS_IS_HIGH_SURROGATE(*(mPos - 1))) {
  238. ch = SURROGATE_TO_UCS4(*--mPos, ch);
  239. }
  240. if (!IsClusterExtender(ch)) {
  241. break;
  242. }
  243. } while (mPos > mLimit);
  244. // XXX May need to handle conjoining Jamo
  245. NS_ASSERTION(mPos >= mLimit,
  246. "ClusterReverseIterator::Next has overshot the string!");
  247. }
  248. uint32_t
  249. CountGraphemeClusters(const char16_t* aText, uint32_t aLength)
  250. {
  251. ClusterIterator iter(aText, aLength);
  252. uint32_t result = 0;
  253. while (!iter.AtEnd()) {
  254. ++result;
  255. iter.Next();
  256. }
  257. return result;
  258. }
  259. } // end namespace unicode
  260. } // end namespace mozilla