1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2004-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: ubidi_props.c
12 * tab size: 8 (not used)
15 * created on: 2004dec30
16 * created by: Markus W. Scherer
18 * Low-level Unicode bidi/shaping properties access.
21 #include "unicode/utypes.h"
22 #include "unicode/uset.h"
23 #include "unicode/udata.h" /* UDataInfo */
24 #include "ucmndata.h" /* DataHeader */
29 #include "ubidi_props.h"
34 const int32_t *indexes
;
35 const uint32_t *mirrors
;
36 const uint8_t *jgArray
;
37 const uint8_t *jgArray2
;
40 uint8_t formatVersion
[4];
43 /* ubidi_props_data.h is machine-generated by genbidi --csource */
44 #define INCLUDED_FROM_UBIDI_PROPS_C
45 #include "ubidi_props_data.h"
47 /* UBiDiProps singleton ----------------------------------------------------- */
49 U_CFUNC
const UBiDiProps
*
50 ubidi_getSingleton() {
51 return &ubidi_props_singleton
;
54 /* set of property starts for UnicodeSet ------------------------------------ */
56 static UBool U_CALLCONV
57 _enumPropertyStartsRange(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
) {
60 /* add the start code point to the USet */
61 const USetAdder
*sa
=(const USetAdder
*)context
;
62 sa
->add(sa
->set
, start
);
67 ubidi_addPropertyStarts(const UBiDiProps
*bdp
, const USetAdder
*sa
, UErrorCode
*pErrorCode
) {
69 UChar32 c
, start
, limit
;
71 const uint8_t *jgArray
;
74 if(U_FAILURE(*pErrorCode
)) {
78 /* add the start code point of each same-value range of the trie */
79 utrie2_enum(&bdp
->trie
, NULL
, _enumPropertyStartsRange
, sa
);
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);
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
];
97 sa
->add(sa
->set
, start
);
103 /* add the limit code point if the last value was not 0 (it is now start==limit) */
104 sa
->add(sa
->set
, limit
);
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
;
116 /* add code points with hardcoded properties, plus the ones following them */
118 /* (none right now) */
121 /* property access functions ------------------------------------------------ */
124 ubidi_getMaxValue(const UBiDiProps
*bdp
, UProperty which
) {
131 max
=bdp
->indexes
[UBIDI_MAX_VALUES_INDEX
];
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
;
139 case UCHAR_BIDI_PAIRED_BRACKET_TYPE
:
140 return (max
&UBIDI_BPT_MASK
)>>UBIDI_BPT_SHIFT
;
142 return -1; /* undefined */
146 U_CAPI UCharDirection
147 ubidi_getClass(const UBiDiProps
*bdp
, UChar32 c
) {
148 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
149 return (UCharDirection
)UBIDI_GET_CLASS(props
);
153 ubidi_isMirrored(const UBiDiProps
*bdp
, UChar32 c
) {
154 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
155 return (UBool
)UBIDI_GET_FLAG(props
, UBIDI_IS_MIRRORED_SHIFT
);
159 getMirror(const UBiDiProps
*bdp
, UChar32 c
, uint16_t props
) {
160 int32_t delta
=UBIDI_GET_MIRROR_DELTA(props
);
161 if(delta
!=UBIDI_ESC_MIRROR_DELTA
) {
164 /* look for mirror code point in the mirrors[] table */
165 const uint32_t *mirrors
;
170 mirrors
=bdp
->mirrors
;
171 length
=bdp
->indexes
[UBIDI_IX_MIRROR_LENGTH
];
174 for(i
=0; i
<length
; ++i
) {
176 c2
=UBIDI_GET_MIRROR_CODE_POINT(m
);
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
)]);
185 /* c not found, return it itself */
191 ubidi_getMirror(const UBiDiProps
*bdp
, UChar32 c
) {
192 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
193 return getMirror(bdp
, c
, props
);
197 ubidi_isBidiControl(const UBiDiProps
*bdp
, UChar32 c
) {
198 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
199 return (UBool
)UBIDI_GET_FLAG(props
, UBIDI_BIDI_CONTROL_SHIFT
);
203 ubidi_isJoinControl(const UBiDiProps
*bdp
, UChar32 c
) {
204 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
205 return (UBool
)UBIDI_GET_FLAG(props
, UBIDI_JOIN_CONTROL_SHIFT
);
209 ubidi_getJoiningType(const UBiDiProps
*bdp
, UChar32 c
) {
210 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
211 return (UJoiningType
)((props
&UBIDI_JT_MASK
)>>UBIDI_JT_SHIFT
);
214 U_CFUNC UJoiningGroup
215 ubidi_getJoiningGroup(const UBiDiProps
*bdp
, UChar32 c
) {
216 UChar32 start
, limit
;
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
];
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
];
228 return U_JG_NO_JOINING_GROUP
;
231 U_CFUNC UBidiPairedBracketType
232 ubidi_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
);
238 ubidi_getPairedBracket(const UBiDiProps
*bdp
, UChar32 c
) {
239 uint16_t props
=UTRIE2_GET16(&bdp
->trie
, c
);
240 if((props
&UBIDI_BPT_MASK
)==0) {
243 return getMirror(bdp
, c
, props
);
247 /* public API (see uchar.h) ------------------------------------------------- */
249 U_CFUNC UCharDirection
250 u_charDirection(UChar32 c
) {
251 return ubidi_getClass(&ubidi_props_singleton
, c
);
255 u_isMirrored(UChar32 c
) {
256 return ubidi_isMirrored(&ubidi_props_singleton
, c
);
260 u_charMirror(UChar32 c
) {
261 return ubidi_getMirror(&ubidi_props_singleton
, c
);
264 U_STABLE UChar32 U_EXPORT2
265 u_getBidiPairedBracket(UChar32 c
) {
266 return ubidi_getPairedBracket(&ubidi_props_singleton
, c
);