ubidi_props.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2004-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: ubidi_props.c
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2004dec30
  16. * created by: Markus W. Scherer
  17. *
  18. * Low-level Unicode bidi/shaping properties access.
  19. */
  20. #include "unicode/utypes.h"
  21. #include "unicode/uset.h"
  22. #include "unicode/udata.h" /* UDataInfo */
  23. #include "ucmndata.h" /* DataHeader */
  24. #include "udatamem.h"
  25. #include "uassert.h"
  26. #include "cmemory.h"
  27. #include "utrie2.h"
  28. #include "ubidi_props.h"
  29. #include "ucln_cmn.h"
  30. struct UBiDiProps {
  31. UDataMemory *mem;
  32. const int32_t *indexes;
  33. const uint32_t *mirrors;
  34. const uint8_t *jgArray;
  35. const uint8_t *jgArray2;
  36. UTrie2 trie;
  37. uint8_t formatVersion[4];
  38. };
  39. /* ubidi_props_data.h is machine-generated by genbidi --csource */
  40. #define INCLUDED_FROM_UBIDI_PROPS_C
  41. #include "ubidi_props_data.h"
  42. /* set of property starts for UnicodeSet ------------------------------------ */
  43. static UBool U_CALLCONV
  44. _enumPropertyStartsRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
  45. (void)end;
  46. (void)value;
  47. /* add the start code point to the USet */
  48. const USetAdder *sa=(const USetAdder *)context;
  49. sa->add(sa->set, start);
  50. return true;
  51. }
  52. U_CFUNC void
  53. ubidi_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
  54. int32_t i, length;
  55. UChar32 c, start, limit;
  56. const uint8_t *jgArray;
  57. uint8_t prev, jg;
  58. if(U_FAILURE(*pErrorCode)) {
  59. return;
  60. }
  61. /* add the start code point of each same-value range of the trie */
  62. utrie2_enum(&ubidi_props_singleton.trie, nullptr, _enumPropertyStartsRange, sa);
  63. /* add the code points from the bidi mirroring table */
  64. length=ubidi_props_singleton.indexes[UBIDI_IX_MIRROR_LENGTH];
  65. for(i=0; i<length; ++i) {
  66. c=UBIDI_GET_MIRROR_CODE_POINT(ubidi_props_singleton.mirrors[i]);
  67. sa->addRange(sa->set, c, c+1);
  68. }
  69. /* add the code points from the Joining_Group array where the value changes */
  70. start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START];
  71. limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT];
  72. jgArray=ubidi_props_singleton.jgArray;
  73. for(;;) {
  74. prev=0;
  75. while(start<limit) {
  76. jg=*jgArray++;
  77. if(jg!=prev) {
  78. sa->add(sa->set, start);
  79. prev=jg;
  80. }
  81. ++start;
  82. }
  83. if(prev!=0) {
  84. /* add the limit code point if the last value was not 0 (it is now start==limit) */
  85. sa->add(sa->set, limit);
  86. }
  87. if(limit==ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT]) {
  88. /* switch to the second Joining_Group range */
  89. start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START2];
  90. limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT2];
  91. jgArray=ubidi_props_singleton.jgArray2;
  92. } else {
  93. break;
  94. }
  95. }
  96. /* add code points with hardcoded properties, plus the ones following them */
  97. /* (none right now) */
  98. }
  99. /* property access functions ------------------------------------------------ */
  100. U_CFUNC int32_t
  101. ubidi_getMaxValue(UProperty which) {
  102. int32_t max=ubidi_props_singleton.indexes[UBIDI_MAX_VALUES_INDEX];
  103. switch(which) {
  104. case UCHAR_BIDI_CLASS:
  105. return (max&UBIDI_CLASS_MASK);
  106. case UCHAR_JOINING_GROUP:
  107. return (max&UBIDI_MAX_JG_MASK)>>UBIDI_MAX_JG_SHIFT;
  108. case UCHAR_JOINING_TYPE:
  109. return (max&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT;
  110. case UCHAR_BIDI_PAIRED_BRACKET_TYPE:
  111. return (max&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT;
  112. default:
  113. return -1; /* undefined */
  114. }
  115. }
  116. U_CAPI UCharDirection
  117. ubidi_getClass(UChar32 c) {
  118. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  119. return (UCharDirection)UBIDI_GET_CLASS(props);
  120. }
  121. U_CFUNC UBool
  122. ubidi_isMirrored(UChar32 c) {
  123. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  124. return (UBool)UBIDI_GET_FLAG(props, UBIDI_IS_MIRRORED_SHIFT);
  125. }
  126. static UChar32
  127. getMirror(UChar32 c, uint16_t props) {
  128. int32_t delta=UBIDI_GET_MIRROR_DELTA(props);
  129. if(delta!=UBIDI_ESC_MIRROR_DELTA) {
  130. return c+delta;
  131. } else {
  132. /* look for mirror code point in the mirrors[] table */
  133. const uint32_t *mirrors;
  134. uint32_t m;
  135. int32_t i, length;
  136. UChar32 c2;
  137. mirrors=ubidi_props_singleton.mirrors;
  138. length=ubidi_props_singleton.indexes[UBIDI_IX_MIRROR_LENGTH];
  139. /* linear search */
  140. for(i=0; i<length; ++i) {
  141. m=mirrors[i];
  142. c2=UBIDI_GET_MIRROR_CODE_POINT(m);
  143. if(c==c2) {
  144. /* found c, return its mirror code point using the index in m */
  145. return UBIDI_GET_MIRROR_CODE_POINT(mirrors[UBIDI_GET_MIRROR_INDEX(m)]);
  146. } else if(c<c2) {
  147. break;
  148. }
  149. }
  150. /* c not found, return it itself */
  151. return c;
  152. }
  153. }
  154. U_CFUNC UChar32
  155. ubidi_getMirror(UChar32 c) {
  156. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  157. return getMirror(c, props);
  158. }
  159. U_CFUNC UBool
  160. ubidi_isBidiControl(UChar32 c) {
  161. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  162. return (UBool)UBIDI_GET_FLAG(props, UBIDI_BIDI_CONTROL_SHIFT);
  163. }
  164. U_CFUNC UBool
  165. ubidi_isJoinControl(UChar32 c) {
  166. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  167. return (UBool)UBIDI_GET_FLAG(props, UBIDI_JOIN_CONTROL_SHIFT);
  168. }
  169. U_CFUNC UJoiningType
  170. ubidi_getJoiningType(UChar32 c) {
  171. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  172. return (UJoiningType)((props&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT);
  173. }
  174. U_CFUNC UJoiningGroup
  175. ubidi_getJoiningGroup(UChar32 c) {
  176. UChar32 start, limit;
  177. start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START];
  178. limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT];
  179. if(start<=c && c<limit) {
  180. return (UJoiningGroup)ubidi_props_singleton.jgArray[c-start];
  181. }
  182. start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START2];
  183. limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT2];
  184. if(start<=c && c<limit) {
  185. return (UJoiningGroup)ubidi_props_singleton.jgArray2[c-start];
  186. }
  187. return U_JG_NO_JOINING_GROUP;
  188. }
  189. U_CFUNC UBidiPairedBracketType
  190. ubidi_getPairedBracketType(UChar32 c) {
  191. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  192. return (UBidiPairedBracketType)((props&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT);
  193. }
  194. U_CFUNC UChar32
  195. ubidi_getPairedBracket(UChar32 c) {
  196. uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
  197. if((props&UBIDI_BPT_MASK)==0) {
  198. return c;
  199. } else {
  200. return getMirror(c, props);
  201. }
  202. }
  203. /* public API (see uchar.h) ------------------------------------------------- */
  204. U_CFUNC UCharDirection
  205. u_charDirection(UChar32 c) {
  206. return ubidi_getClass(c);
  207. }
  208. U_CFUNC UBool
  209. u_isMirrored(UChar32 c) {
  210. return ubidi_isMirrored(c);
  211. }
  212. U_CFUNC UChar32
  213. u_charMirror(UChar32 c) {
  214. return ubidi_getMirror(c);
  215. }
  216. U_CAPI UChar32 U_EXPORT2
  217. u_getBidiPairedBracket(UChar32 c) {
  218. return ubidi_getPairedBracket(c);
  219. }