]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ubidi_props.cpp
ICU-66108.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/* set of property starts for UnicodeSet ------------------------------------ */
48
49static UBool U_CALLCONV
729e4ab9 50_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
f3c0d7a5
A
51 (void)end;
52 (void)value;
73c04bcf
A
53 /* add the start code point to the USet */
54 const USetAdder *sa=(const USetAdder *)context;
55 sa->add(sa->set, start);
56 return TRUE;
57}
58
46f4442e 59U_CFUNC void
0f5d89e8 60ubidi_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
73c04bcf
A
61 int32_t i, length;
62 UChar32 c, start, limit;
63
64 const uint8_t *jgArray;
65 uint8_t prev, jg;
66
67 if(U_FAILURE(*pErrorCode)) {
68 return;
69 }
70
71 /* add the start code point of each same-value range of the trie */
0f5d89e8 72 utrie2_enum(&ubidi_props_singleton.trie, NULL, _enumPropertyStartsRange, sa);
73c04bcf
A
73
74 /* add the code points from the bidi mirroring table */
0f5d89e8 75 length=ubidi_props_singleton.indexes[UBIDI_IX_MIRROR_LENGTH];
73c04bcf 76 for(i=0; i<length; ++i) {
0f5d89e8 77 c=UBIDI_GET_MIRROR_CODE_POINT(ubidi_props_singleton.mirrors[i]);
73c04bcf
A
78 sa->addRange(sa->set, c, c+1);
79 }
80
81 /* add the code points from the Joining_Group array where the value changes */
0f5d89e8
A
82 start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START];
83 limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT];
84 jgArray=ubidi_props_singleton.jgArray;
b331163b
A
85 for(;;) {
86 prev=0;
87 while(start<limit) {
88 jg=*jgArray++;
89 if(jg!=prev) {
90 sa->add(sa->set, start);
91 prev=jg;
92 }
93 ++start;
94 }
95 if(prev!=0) {
96 /* add the limit code point if the last value was not 0 (it is now start==limit) */
97 sa->add(sa->set, limit);
98 }
0f5d89e8 99 if(limit==ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT]) {
b331163b 100 /* switch to the second Joining_Group range */
0f5d89e8
A
101 start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START2];
102 limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT2];
103 jgArray=ubidi_props_singleton.jgArray2;
b331163b
A
104 } else {
105 break;
73c04bcf 106 }
73c04bcf
A
107 }
108
109 /* add code points with hardcoded properties, plus the ones following them */
110
111 /* (none right now) */
112}
113
73c04bcf
A
114/* property access functions ------------------------------------------------ */
115
116U_CFUNC int32_t
0f5d89e8
A
117ubidi_getMaxValue(UProperty which) {
118 int32_t max=ubidi_props_singleton.indexes[UBIDI_MAX_VALUES_INDEX];
73c04bcf
A
119 switch(which) {
120 case UCHAR_BIDI_CLASS:
121 return (max&UBIDI_CLASS_MASK);
122 case UCHAR_JOINING_GROUP:
123 return (max&UBIDI_MAX_JG_MASK)>>UBIDI_MAX_JG_SHIFT;
124 case UCHAR_JOINING_TYPE:
125 return (max&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT;
57a6839d
A
126 case UCHAR_BIDI_PAIRED_BRACKET_TYPE:
127 return (max&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT;
73c04bcf
A
128 default:
129 return -1; /* undefined */
130 }
131}
132
46f4442e 133U_CAPI UCharDirection
0f5d89e8
A
134ubidi_getClass(UChar32 c) {
135 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
73c04bcf
A
136 return (UCharDirection)UBIDI_GET_CLASS(props);
137}
138
46f4442e 139U_CFUNC UBool
0f5d89e8
A
140ubidi_isMirrored(UChar32 c) {
141 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
73c04bcf
A
142 return (UBool)UBIDI_GET_FLAG(props, UBIDI_IS_MIRRORED_SHIFT);
143}
144
57a6839d 145static UChar32
0f5d89e8 146getMirror(UChar32 c, uint16_t props) {
57a6839d 147 int32_t delta=UBIDI_GET_MIRROR_DELTA(props);
73c04bcf
A
148 if(delta!=UBIDI_ESC_MIRROR_DELTA) {
149 return c+delta;
150 } else {
151 /* look for mirror code point in the mirrors[] table */
152 const uint32_t *mirrors;
153 uint32_t m;
154 int32_t i, length;
155 UChar32 c2;
156
0f5d89e8
A
157 mirrors=ubidi_props_singleton.mirrors;
158 length=ubidi_props_singleton.indexes[UBIDI_IX_MIRROR_LENGTH];
73c04bcf
A
159
160 /* linear search */
161 for(i=0; i<length; ++i) {
162 m=mirrors[i];
163 c2=UBIDI_GET_MIRROR_CODE_POINT(m);
164 if(c==c2) {
165 /* found c, return its mirror code point using the index in m */
166 return UBIDI_GET_MIRROR_CODE_POINT(mirrors[UBIDI_GET_MIRROR_INDEX(m)]);
167 } else if(c<c2) {
168 break;
169 }
170 }
171
172 /* c not found, return it itself */
173 return c;
174 }
175}
176
57a6839d 177U_CFUNC UChar32
0f5d89e8
A
178ubidi_getMirror(UChar32 c) {
179 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
180 return getMirror(c, props);
57a6839d
A
181}
182
46f4442e 183U_CFUNC UBool
0f5d89e8
A
184ubidi_isBidiControl(UChar32 c) {
185 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
73c04bcf
A
186 return (UBool)UBIDI_GET_FLAG(props, UBIDI_BIDI_CONTROL_SHIFT);
187}
188
46f4442e 189U_CFUNC UBool
0f5d89e8
A
190ubidi_isJoinControl(UChar32 c) {
191 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
73c04bcf
A
192 return (UBool)UBIDI_GET_FLAG(props, UBIDI_JOIN_CONTROL_SHIFT);
193}
194
46f4442e 195U_CFUNC UJoiningType
0f5d89e8
A
196ubidi_getJoiningType(UChar32 c) {
197 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
73c04bcf
A
198 return (UJoiningType)((props&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT);
199}
200
46f4442e 201U_CFUNC UJoiningGroup
0f5d89e8 202ubidi_getJoiningGroup(UChar32 c) {
73c04bcf
A
203 UChar32 start, limit;
204
0f5d89e8
A
205 start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START];
206 limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT];
73c04bcf 207 if(start<=c && c<limit) {
0f5d89e8 208 return (UJoiningGroup)ubidi_props_singleton.jgArray[c-start];
73c04bcf 209 }
0f5d89e8
A
210 start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START2];
211 limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT2];
b331163b 212 if(start<=c && c<limit) {
0f5d89e8 213 return (UJoiningGroup)ubidi_props_singleton.jgArray2[c-start];
b331163b
A
214 }
215 return U_JG_NO_JOINING_GROUP;
73c04bcf
A
216}
217
57a6839d 218U_CFUNC UBidiPairedBracketType
0f5d89e8
A
219ubidi_getPairedBracketType(UChar32 c) {
220 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
57a6839d
A
221 return (UBidiPairedBracketType)((props&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT);
222}
223
224U_CFUNC UChar32
0f5d89e8
A
225ubidi_getPairedBracket(UChar32 c) {
226 uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
57a6839d
A
227 if((props&UBIDI_BPT_MASK)==0) {
228 return c;
229 } else {
0f5d89e8 230 return getMirror(c, props);
57a6839d
A
231 }
232}
233
73c04bcf
A
234/* public API (see uchar.h) ------------------------------------------------- */
235
46f4442e 236U_CFUNC UCharDirection
0f5d89e8
A
237u_charDirection(UChar32 c) {
238 return ubidi_getClass(c);
73c04bcf
A
239}
240
46f4442e 241U_CFUNC UBool
73c04bcf 242u_isMirrored(UChar32 c) {
0f5d89e8 243 return ubidi_isMirrored(c);
73c04bcf
A
244}
245
46f4442e 246U_CFUNC UChar32
73c04bcf 247u_charMirror(UChar32 c) {
0f5d89e8 248 return ubidi_getMirror(c);
73c04bcf 249}
57a6839d
A
250
251U_STABLE UChar32 U_EXPORT2
252u_getBidiPairedBracket(UChar32 c) {
0f5d89e8 253 return ubidi_getPairedBracket(c);
57a6839d 254}