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