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