emojiprops.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // © 2021 and later: Unicode, Inc. and others.
  2. // License & terms of use: https://www.unicode.org/copyright.html
  3. // emojiprops.cpp
  4. // created: 2021sep04 Markus W. Scherer
  5. #include "unicode/utypes.h"
  6. #include "unicode/uchar.h"
  7. #include "unicode/ucharstrie.h"
  8. #include "unicode/ucptrie.h"
  9. #include "unicode/udata.h"
  10. #include "unicode/ustringtrie.h"
  11. #include "unicode/utf16.h"
  12. #include "emojiprops.h"
  13. #include "ucln.h"
  14. #include "ucln_cmn.h"
  15. #include "umutex.h"
  16. #include "uset_imp.h"
  17. U_NAMESPACE_BEGIN
  18. namespace {
  19. EmojiProps *singleton = nullptr;
  20. icu::UInitOnce emojiInitOnce {};
  21. UBool U_CALLCONV emojiprops_cleanup() {
  22. delete singleton;
  23. singleton = nullptr;
  24. emojiInitOnce.reset();
  25. return true;
  26. }
  27. void U_CALLCONV initSingleton(UErrorCode &errorCode) {
  28. if (U_FAILURE(errorCode)) { return; }
  29. singleton = new EmojiProps(errorCode);
  30. if (singleton == nullptr) {
  31. errorCode = U_MEMORY_ALLOCATION_ERROR;
  32. } else if (U_FAILURE(errorCode)) {
  33. delete singleton;
  34. singleton = nullptr;
  35. }
  36. ucln_common_registerCleanup(UCLN_COMMON_EMOJIPROPS, emojiprops_cleanup);
  37. }
  38. // TODO: turn this into a shared helper function
  39. // Requires the major version to match, and then requires at least the minor version.
  40. UBool udata_isAcceptableMajorMinor(
  41. const UDataInfo &info, const char16_t *dataFormat, uint8_t major, uint8_t minor) {
  42. return
  43. info.size >= 20 &&
  44. info.isBigEndian == U_IS_BIG_ENDIAN &&
  45. info.charsetFamily == U_CHARSET_FAMILY &&
  46. info.dataFormat[0] == dataFormat[0] &&
  47. info.dataFormat[1] == dataFormat[1] &&
  48. info.dataFormat[2] == dataFormat[2] &&
  49. info.dataFormat[3] == dataFormat[3] &&
  50. info.formatVersion[0] == major &&
  51. info.formatVersion[1] >= minor;
  52. }
  53. } // namespace
  54. EmojiProps::~EmojiProps() {
  55. udata_close(memory);
  56. ucptrie_close(cpTrie);
  57. }
  58. const EmojiProps *
  59. EmojiProps::getSingleton(UErrorCode &errorCode) {
  60. if (U_FAILURE(errorCode)) { return nullptr; }
  61. umtx_initOnce(emojiInitOnce, &initSingleton, errorCode);
  62. return singleton;
  63. }
  64. UBool U_CALLCONV
  65. EmojiProps::isAcceptable(void * /*context*/, const char * /*type*/, const char * /*name*/,
  66. const UDataInfo *pInfo) {
  67. return udata_isAcceptableMajorMinor(*pInfo, u"Emoj", 1, 0);
  68. }
  69. void
  70. EmojiProps::load(UErrorCode &errorCode) {
  71. memory = udata_openChoice(nullptr, "icu", "uemoji", isAcceptable, this, &errorCode);
  72. if (U_FAILURE(errorCode)) { return; }
  73. const uint8_t *inBytes = (const uint8_t *)udata_getMemory(memory);
  74. const int32_t *inIndexes = (const int32_t *)inBytes;
  75. int32_t indexesLength = inIndexes[IX_CPTRIE_OFFSET] / 4;
  76. if (indexesLength <= IX_RGI_EMOJI_ZWJ_SEQUENCE_TRIE_OFFSET) {
  77. errorCode = U_INVALID_FORMAT_ERROR; // Not enough indexes.
  78. return;
  79. }
  80. int32_t i = IX_CPTRIE_OFFSET;
  81. int32_t offset = inIndexes[i++];
  82. int32_t nextOffset = inIndexes[i];
  83. cpTrie = ucptrie_openFromBinary(UCPTRIE_TYPE_FAST, UCPTRIE_VALUE_BITS_8,
  84. inBytes + offset, nextOffset - offset, nullptr, &errorCode);
  85. if (U_FAILURE(errorCode)) {
  86. return;
  87. }
  88. for (i = IX_BASIC_EMOJI_TRIE_OFFSET; i <= IX_RGI_EMOJI_ZWJ_SEQUENCE_TRIE_OFFSET; ++i) {
  89. offset = inIndexes[i];
  90. nextOffset = inIndexes[i + 1];
  91. // Set/leave nullptr if there is no UCharsTrie.
  92. const char16_t *p = nextOffset > offset ? (const char16_t *)(inBytes + offset) : nullptr;
  93. stringTries[getStringTrieIndex(i)] = p;
  94. }
  95. }
  96. void
  97. EmojiProps::addPropertyStarts(const USetAdder *sa, UErrorCode & /*errorCode*/) const {
  98. // Add the start code point of each same-value range of the trie.
  99. UChar32 start = 0, end;
  100. uint32_t value;
  101. while ((end = ucptrie_getRange(cpTrie, start, UCPMAP_RANGE_NORMAL, 0,
  102. nullptr, nullptr, &value)) >= 0) {
  103. sa->add(sa->set, start);
  104. start = end + 1;
  105. }
  106. }
  107. UBool
  108. EmojiProps::hasBinaryProperty(UChar32 c, UProperty which) {
  109. UErrorCode errorCode = U_ZERO_ERROR;
  110. const EmojiProps *ep = getSingleton(errorCode);
  111. return U_SUCCESS(errorCode) && ep->hasBinaryPropertyImpl(c, which);
  112. }
  113. UBool
  114. EmojiProps::hasBinaryPropertyImpl(UChar32 c, UProperty which) const {
  115. if (which < UCHAR_EMOJI || UCHAR_RGI_EMOJI < which) {
  116. return false;
  117. }
  118. // Note: UCHAR_REGIONAL_INDICATOR is a single, hardcoded range implemented elsewhere.
  119. static constexpr int8_t bitFlags[] = {
  120. BIT_EMOJI, // UCHAR_EMOJI=57
  121. BIT_EMOJI_PRESENTATION, // UCHAR_EMOJI_PRESENTATION=58
  122. BIT_EMOJI_MODIFIER, // UCHAR_EMOJI_MODIFIER=59
  123. BIT_EMOJI_MODIFIER_BASE, // UCHAR_EMOJI_MODIFIER_BASE=60
  124. BIT_EMOJI_COMPONENT, // UCHAR_EMOJI_COMPONENT=61
  125. -1, // UCHAR_REGIONAL_INDICATOR=62
  126. -1, // UCHAR_PREPENDED_CONCATENATION_MARK=63
  127. BIT_EXTENDED_PICTOGRAPHIC, // UCHAR_EXTENDED_PICTOGRAPHIC=64
  128. BIT_BASIC_EMOJI, // UCHAR_BASIC_EMOJI=65
  129. -1, // UCHAR_EMOJI_KEYCAP_SEQUENCE=66
  130. -1, // UCHAR_RGI_EMOJI_MODIFIER_SEQUENCE=67
  131. -1, // UCHAR_RGI_EMOJI_FLAG_SEQUENCE=68
  132. -1, // UCHAR_RGI_EMOJI_TAG_SEQUENCE=69
  133. -1, // UCHAR_RGI_EMOJI_ZWJ_SEQUENCE=70
  134. BIT_BASIC_EMOJI, // UCHAR_RGI_EMOJI=71
  135. };
  136. int32_t bit = bitFlags[which - UCHAR_EMOJI];
  137. if (bit < 0) {
  138. return false; // not a property that we support in this function
  139. }
  140. uint8_t bits = UCPTRIE_FAST_GET(cpTrie, UCPTRIE_8, c);
  141. return (bits >> bit) & 1;
  142. }
  143. UBool
  144. EmojiProps::hasBinaryProperty(const char16_t *s, int32_t length, UProperty which) {
  145. UErrorCode errorCode = U_ZERO_ERROR;
  146. const EmojiProps *ep = getSingleton(errorCode);
  147. return U_SUCCESS(errorCode) && ep->hasBinaryPropertyImpl(s, length, which);
  148. }
  149. UBool
  150. EmojiProps::hasBinaryPropertyImpl(const char16_t *s, int32_t length, UProperty which) const {
  151. if (s == nullptr && length != 0) { return false; }
  152. if (length <= 0 && (length == 0 || *s == 0)) { return false; } // empty string
  153. // The caller should have delegated single code points to hasBinaryProperty(c, which).
  154. if (which < UCHAR_BASIC_EMOJI || UCHAR_RGI_EMOJI < which) {
  155. return false;
  156. }
  157. UProperty firstProp = which, lastProp = which;
  158. if (which == UCHAR_RGI_EMOJI) {
  159. // RGI_Emoji is the union of the other emoji properties of strings.
  160. firstProp = UCHAR_BASIC_EMOJI;
  161. lastProp = UCHAR_RGI_EMOJI_ZWJ_SEQUENCE;
  162. }
  163. for (int32_t prop = firstProp; prop <= lastProp; ++prop) {
  164. const char16_t *trieUChars = stringTries[prop - UCHAR_BASIC_EMOJI];
  165. if (trieUChars != nullptr) {
  166. UCharsTrie trie(trieUChars);
  167. UStringTrieResult result = trie.next(s, length);
  168. if (USTRINGTRIE_HAS_VALUE(result)) {
  169. return true;
  170. }
  171. }
  172. }
  173. return false;
  174. }
  175. void
  176. EmojiProps::addStrings(const USetAdder *sa, UProperty which, UErrorCode &errorCode) const {
  177. if (U_FAILURE(errorCode)) { return; }
  178. if (which < UCHAR_BASIC_EMOJI || UCHAR_RGI_EMOJI < which) {
  179. return;
  180. }
  181. UProperty firstProp = which, lastProp = which;
  182. if (which == UCHAR_RGI_EMOJI) {
  183. // RGI_Emoji is the union of the other emoji properties of strings.
  184. firstProp = UCHAR_BASIC_EMOJI;
  185. lastProp = UCHAR_RGI_EMOJI_ZWJ_SEQUENCE;
  186. }
  187. for (int32_t prop = firstProp; prop <= lastProp; ++prop) {
  188. const char16_t *trieUChars = stringTries[prop - UCHAR_BASIC_EMOJI];
  189. if (trieUChars != nullptr) {
  190. UCharsTrie::Iterator iter(trieUChars, 0, errorCode);
  191. while (iter.next(errorCode)) {
  192. const UnicodeString &s = iter.getString();
  193. sa->addString(sa->set, s.getBuffer(), s.length());
  194. }
  195. }
  196. }
  197. }
  198. U_NAMESPACE_END