2 * fribidi-bidi-types.h - character bidi types
5 * Behdad Esfahbod, 2001, 2002, 2004
7 * Copyright (C) 2004 Sharif FarsiWeb, Inc.
8 * Copyright (C) 2001,2002 Behdad Esfahbod
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library, in a file named COPYING; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA
25 * For licensing issues, contact <fribidi.license@gmail.com>.
27 #ifndef _FRIBIDI_BIDI_TYPES_H
28 #define _FRIBIDI_BIDI_TYPES_H
30 #include "fribidi-common.h"
32 #include "fribidi-types.h"
34 #include "fribidi-begindecls.h"
36 typedef signed char FriBidiLevel;
39 * Define bit masks that bidi types are based on, each mask has
43 /* RTL mask better be the least significant bit. */
44 #define FRIBIDI_MASK_RTL 0x00000001L /* Is right to left */
45 #define FRIBIDI_MASK_ARABIC 0x00000002L /* Is arabic */
47 /* Each char can be only one of the three following. */
48 #define FRIBIDI_MASK_STRONG 0x00000010L /* Is strong */
49 #define FRIBIDI_MASK_WEAK 0x00000020L /* Is weak */
50 #define FRIBIDI_MASK_NEUTRAL 0x00000040L /* Is neutral */
51 #define FRIBIDI_MASK_SENTINEL 0x00000080L /* Is sentinel */
52 /* Sentinels are not valid chars, just identify the start/end of strings. */
54 /* Each char can be only one of the six following. */
55 #define FRIBIDI_MASK_LETTER 0x00000100L /* Is letter: L, R, AL */
56 #define FRIBIDI_MASK_NUMBER 0x00000200L /* Is number: EN, AN */
57 #define FRIBIDI_MASK_NUMSEPTER 0x00000400L /* Is separator or terminator: ES, ET, CS */
58 #define FRIBIDI_MASK_SPACE 0x00000800L /* Is space: BN, BS, SS, WS */
59 #define FRIBIDI_MASK_EXPLICIT 0x00001000L /* Is explicit mark: LRE, RLE, LRO, RLO, PDF */
60 #define FRIBIDI_MASK_ISOLATE 0x00008000L /* Is isolate mark: LRI, RLI, FSI, PDI */
62 /* Can be set only if FRIBIDI_MASK_SPACE is also set. */
63 #define FRIBIDI_MASK_SEPARATOR 0x00002000L /* Is text separator: BS, SS */
64 /* Can be set only if FRIBIDI_MASK_EXPLICIT is also set. */
65 #define FRIBIDI_MASK_OVERRIDE 0x00004000L /* Is explicit override: LRO, RLO */
66 #define FRIBIDI_MASK_FIRST 0x02000000L /* Whether direction is determined by first strong */
69 /* The following exist to make types pairwise different, some of them can
70 * be removed but are here because of efficiency (make queries faster). */
72 #define FRIBIDI_MASK_ES 0x00010000L
73 #define FRIBIDI_MASK_ET 0x00020000L
74 #define FRIBIDI_MASK_CS 0x00040000L
76 #define FRIBIDI_MASK_NSM 0x00080000L
77 #define FRIBIDI_MASK_BN 0x00100000L
79 #define FRIBIDI_MASK_BS 0x00200000L
80 #define FRIBIDI_MASK_SS 0x00400000L
81 #define FRIBIDI_MASK_WS 0x00800000L
83 /* We reserve a single bit for user's private use: we will never use it. */
84 #define FRIBIDI_MASK_PRIVATE 0x01000000L
88 * Define values for FriBidiCharType
93 /* Left-To-Right letter */
94 #define FRIBIDI_TYPE_LTR_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_LETTER )
95 /* Right-To-Left letter */
96 #define FRIBIDI_TYPE_RTL_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_LETTER \
99 #define FRIBIDI_TYPE_AL_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_LETTER \
100 | FRIBIDI_MASK_RTL | FRIBIDI_MASK_ARABIC )
101 /* Left-to-Right Embedding */
102 #define FRIBIDI_TYPE_LRE_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_EXPLICIT)
103 /* Right-to-Left Embedding */
104 #define FRIBIDI_TYPE_RLE_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_EXPLICIT \
106 /* Left-to-Right Override */
107 #define FRIBIDI_TYPE_LRO_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_EXPLICIT \
108 | FRIBIDI_MASK_OVERRIDE )
109 /* Right-to-Left Override */
110 #define FRIBIDI_TYPE_RLO_VAL ( FRIBIDI_MASK_STRONG | FRIBIDI_MASK_EXPLICIT \
111 | FRIBIDI_MASK_RTL | FRIBIDI_MASK_OVERRIDE )
115 /* Pop Directional Flag*/
116 #define FRIBIDI_TYPE_PDF_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_EXPLICIT )
117 /* European Numeral */
118 #define FRIBIDI_TYPE_EN_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_NUMBER )
120 #define FRIBIDI_TYPE_AN_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_NUMBER \
121 | FRIBIDI_MASK_ARABIC )
122 /* European number Separator */
123 #define FRIBIDI_TYPE_ES_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_NUMSEPTER \
125 /* European number Terminator */
126 #define FRIBIDI_TYPE_ET_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_NUMSEPTER \
128 /* Common Separator */
129 #define FRIBIDI_TYPE_CS_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_NUMSEPTER \
131 /* Non Spacing Mark */
132 #define FRIBIDI_TYPE_NSM_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_NSM )
133 /* Boundary Neutral */
134 #define FRIBIDI_TYPE_BN_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_SPACE \
139 /* Block Separator */
140 #define FRIBIDI_TYPE_BS_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_SPACE \
141 | FRIBIDI_MASK_SEPARATOR | FRIBIDI_MASK_BS )
142 /* Segment Separator */
143 #define FRIBIDI_TYPE_SS_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_SPACE \
144 | FRIBIDI_MASK_SEPARATOR | FRIBIDI_MASK_SS )
146 #define FRIBIDI_TYPE_WS_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_SPACE \
149 #define FRIBIDI_TYPE_ON_VAL ( FRIBIDI_MASK_NEUTRAL )
152 /* The following are used in specifying paragraph direction only. */
154 /* Weak Left-To-Right */
155 #define FRIBIDI_TYPE_WLTR_VAL ( FRIBIDI_MASK_WEAK )
156 /* Weak Right-To-Left */
157 #define FRIBIDI_TYPE_WRTL_VAL ( FRIBIDI_MASK_WEAK | FRIBIDI_MASK_RTL )
159 /* start or end of text (run list) SENTINEL. Only used internally */
160 #define FRIBIDI_TYPE_SENTINEL ( FRIBIDI_MASK_SENTINEL )
162 /* Private types for applications. More private types can be obtained by
163 * summing up from this one. */
164 #define FRIBIDI_TYPE_PRIVATE ( FRIBIDI_MASK_PRIVATE )
167 /* New types in Unicode 6.3 */
169 /* Left-to-Right Isolate */
170 #define FRIBIDI_TYPE_LRI_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_ISOLATE )
171 /* Right-to-Left Isolate */
172 #define FRIBIDI_TYPE_RLI_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_ISOLATE | FRIBIDI_MASK_RTL )
173 /* First strong isolate */
174 #define FRIBIDI_TYPE_FSI_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_ISOLATE | FRIBIDI_MASK_FIRST )
176 /* Pop Directional Isolate*/
177 #define FRIBIDI_TYPE_PDI_VAL ( FRIBIDI_MASK_NEUTRAL | FRIBIDI_MASK_WEAK | FRIBIDI_MASK_ISOLATE )
179 /* Define Enums only if sizeof(int) == 4 (UTF-32), and not compiling C++.
180 * The problem with C++ is that then casts between int32 and enum will fail!
182 #if defined(__FRIBIDI_DOC) || (FRIBIDI_SIZEOF_INT+0 == 4 && !defined(__cplusplus))
186 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) \
187 FRIBIDI_TYPE_##TYPE = FRIBIDI_TYPE_##TYPE##_VAL,
188 # include "fribidi-bidi-types-list.h"
189 # undef _FRIBIDI_ADD_TYPE
190 _FRIBIDI_TYPE_SENTINEL = FRIBIDI_TYPE_SENTINEL /* Don't use this */
195 # define _FRIBIDI_PAR_TYPES
196 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) \
197 FRIBIDI_PAR_##TYPE = FRIBIDI_TYPE_##TYPE##_VAL,
198 # include "fribidi-bidi-types-list.h"
199 # undef _FRIBIDI_ADD_TYPE
200 # undef _FRIBIDI_PAR_TYPES
201 _FRIBIDI_PAR_SENTINEL = FRIBIDI_TYPE_SENTINEL /* Don't use this */
206 typedef uint32_t FriBidiCharType;
207 # define FRIBIDI_TYPE_LTR FRIBIDI_TYPE_LTR_VAL
208 # define FRIBIDI_TYPE_RTL FRIBIDI_TYPE_RTL_VAL
209 # define FRIBIDI_TYPE_AL FRIBIDI_TYPE_AL_VAL
210 # define FRIBIDI_TYPE_EN FRIBIDI_TYPE_EN_VAL
211 # define FRIBIDI_TYPE_AN FRIBIDI_TYPE_AN_VAL
212 # define FRIBIDI_TYPE_ES FRIBIDI_TYPE_ES_VAL
213 # define FRIBIDI_TYPE_ET FRIBIDI_TYPE_ET_VAL
214 # define FRIBIDI_TYPE_CS FRIBIDI_TYPE_CS_VAL
215 # define FRIBIDI_TYPE_NSM FRIBIDI_TYPE_NSM_VAL
216 # define FRIBIDI_TYPE_BN FRIBIDI_TYPE_BN_VAL
217 # define FRIBIDI_TYPE_BS FRIBIDI_TYPE_BS_VAL
218 # define FRIBIDI_TYPE_SS FRIBIDI_TYPE_SS_VAL
219 # define FRIBIDI_TYPE_WS FRIBIDI_TYPE_WS_VAL
220 # define FRIBIDI_TYPE_ON FRIBIDI_TYPE_ON_VAL
221 # define FRIBIDI_TYPE_LRE FRIBIDI_TYPE_LRE_VAL
222 # define FRIBIDI_TYPE_RLE FRIBIDI_TYPE_RLE_VAL
223 # define FRIBIDI_TYPE_LRO FRIBIDI_TYPE_LRO_VAL
224 # define FRIBIDI_TYPE_RLO FRIBIDI_TYPE_RLO_VAL
225 # define FRIBIDI_TYPE_PDF FRIBIDI_TYPE_PDF_VAL
226 # define FRIBIDI_TYPE_LRI FRIBIDI_TYPE_LRI_VAL
227 # define FRIBIDI_TYPE_RLI FRIBIDI_TYPE_RLI_VAL
228 # define FRIBIDI_TYPE_FSI FRIBIDI_TYPE_FSI_VAL
229 # define FRIBIDI_TYPE_PDI FRIBIDI_TYPE_PDI_VAL
231 typedef uint32_t FriBidiParType;
232 # define FRIBIDI_PAR_LTR FRIBIDI_TYPE_LTR_VAL
233 # define FRIBIDI_PAR_RTL FRIBIDI_TYPE_RTL_VAL
234 # define FRIBIDI_PAR_ON FRIBIDI_TYPE_ON_VAL
235 # define FRIBIDI_PAR_WLTR FRIBIDI_TYPE_WLTR_VAL
236 # define FRIBIDI_PAR_WRTL FRIBIDI_TYPE_WRTL_VAL
240 /* Please don't use these two type names, use FRIBIDI_PAR_* form instead. */
241 #define FRIBIDI_TYPE_WLTR FRIBIDI_PAR_WLTR
242 #define FRIBIDI_TYPE_WRTL FRIBIDI_PAR_WRTL
246 * Defining macros for needed queries, It is fully dependent on the
247 * implementation of FriBidiCharType.
251 /* Is right-to-left level? */
252 #define FRIBIDI_LEVEL_IS_RTL(lev) ((lev) & 1)
254 /* Return the bidi type corresponding to the direction of the level number,
255 FRIBIDI_TYPE_LTR for evens and FRIBIDI_TYPE_RTL for odds. */
256 #define FRIBIDI_LEVEL_TO_DIR(lev) \
257 (FRIBIDI_LEVEL_IS_RTL (lev) ? FRIBIDI_TYPE_RTL : FRIBIDI_TYPE_LTR)
259 /* Return the minimum level of the direction, 0 for FRIBIDI_TYPE_LTR and
260 1 for FRIBIDI_TYPE_RTL and FRIBIDI_TYPE_AL. */
261 #define FRIBIDI_DIR_TO_LEVEL(dir) \
262 ((FriBidiLevel) (FRIBIDI_IS_RTL (dir) ? 1 : 0))
264 /* Is right to left: RTL, AL, RLE, RLO? */
265 #define FRIBIDI_IS_RTL(p) ((p) & FRIBIDI_MASK_RTL)
266 /* Is arabic: AL, AN? */
267 #define FRIBIDI_IS_ARABIC(p) ((p) & FRIBIDI_MASK_ARABIC)
270 #define FRIBIDI_IS_STRONG(p) ((p) & FRIBIDI_MASK_STRONG)
272 #define FRIBIDI_IS_WEAK(p) ((p) & FRIBIDI_MASK_WEAK)
274 #define FRIBIDI_IS_NEUTRAL(p) ((p) & FRIBIDI_MASK_NEUTRAL)
276 #define FRIBIDI_IS_SENTINEL(p) ((p) & FRIBIDI_MASK_SENTINEL)
278 /* Is letter: L, R, AL? */
279 #define FRIBIDI_IS_LETTER(p) ((p) & FRIBIDI_MASK_LETTER)
280 /* Is number: EN, AN? */
281 #define FRIBIDI_IS_NUMBER(p) ((p) & FRIBIDI_MASK_NUMBER)
282 /* Is number separator or terminator: ES, ET, CS? */
283 #define FRIBIDI_IS_NUMBER_SEPARATOR_OR_TERMINATOR(p) \
284 ((p) & FRIBIDI_MASK_NUMSEPTER)
285 /* Is space: BN, BS, SS, WS? */
286 #define FRIBIDI_IS_SPACE(p) ((p) & FRIBIDI_MASK_SPACE)
287 /* Is explicit mark: LRE, RLE, LRO, RLO, PDF? */
288 #define FRIBIDI_IS_EXPLICIT(p) ((p) & FRIBIDI_MASK_EXPLICIT)
290 #define FRIBIDI_IS_ISOLATE(p) ((p) & FRIBIDI_MASK_ISOLATE)
292 /* Is text separator: BS, SS? */
293 #define FRIBIDI_IS_SEPARATOR(p) ((p) & FRIBIDI_MASK_SEPARATOR)
295 /* Is explicit override: LRO, RLO? */
296 #define FRIBIDI_IS_OVERRIDE(p) ((p) & FRIBIDI_MASK_OVERRIDE)
300 /* Is left to right letter: LTR? */
301 #define FRIBIDI_IS_LTR_LETTER(p) \
302 ((p) & (FRIBIDI_MASK_LETTER | FRIBIDI_MASK_RTL) == FRIBIDI_MASK_LETTER)
304 /* Is right to left letter: RTL, AL? */
305 #define FRIBIDI_IS_RTL_LETTER(p) \
306 ((p) & (FRIBIDI_MASK_LETTER | FRIBIDI_MASK_RTL) \
307 == (FRIBIDI_MASK_LETTER | FRIBIDI_MASK_RTL))
309 /* Is ES or CS: ES, CS? */
310 #define FRIBIDI_IS_ES_OR_CS(p) \
311 ((p) & (FRIBIDI_MASK_ES | FRIBIDI_MASK_CS))
313 /* Is explicit or BN: LRE, RLE, LRO, RLO, PDF, BN? */
314 #define FRIBIDI_IS_EXPLICIT_OR_BN(p) \
315 ((p) & (FRIBIDI_MASK_EXPLICIT | FRIBIDI_MASK_BN))
317 /* Is explicit or BN or NSM: LRE, RLE, LRO, RLO, PDF, BN, NSM? */
318 #define FRIBIDI_IS_EXPLICIT_OR_BN_OR_NSM(p) \
319 ((p) & (FRIBIDI_MASK_EXPLICIT | FRIBIDI_MASK_BN | FRIBIDI_MASK_NSM))
321 /* Is explicit or BN or NSM: LRE, RLE, LRO, RLO, PDF, BN, NSM? */
322 #define FRIBIDI_IS_EXPLICIT_OR_ISOLATE_OR_BN_OR_NSM(p) \
323 ((p) & (FRIBIDI_MASK_EXPLICIT | FRIBIDI_MASK_ISOLATE | FRIBIDI_MASK_BN | FRIBIDI_MASK_NSM))
325 /* Is explicit or BN or WS: LRE, RLE, LRO, RLO, PDF, BN, WS? */
326 #define FRIBIDI_IS_EXPLICIT_OR_BN_OR_WS(p) \
327 ((p) & (FRIBIDI_MASK_EXPLICIT | FRIBIDI_MASK_BN | FRIBIDI_MASK_WS))
329 /* Is explicit or separator or BN or WS: LRE, RLE, LRO, RLO, PDF, BS, SS, BN, WS? */
330 #define FRIBIDI_IS_EXPLICIT_OR_SEPARATOR_OR_BN_OR_WS(p) \
331 ((p) & (FRIBIDI_MASK_EXPLICIT | FRIBIDI_MASK_SEPARATOR \
332 | FRIBIDI_MASK_BN | FRIBIDI_MASK_WS))
334 /* Is private-use type for application? */
335 #define FRIBIDI_IS_PRIVATE(p) ((p) & FRIBIDI_MASK_PRIVATE)
337 /* Define some conversions. */
339 /* Change numbers to RTL: EN,AN -> RTL. */
340 #define FRIBIDI_CHANGE_NUMBER_TO_RTL(p) \
341 (FRIBIDI_IS_NUMBER(p) ? FRIBIDI_TYPE_RTL : (p))
343 /* Override status of an explicit mark:
344 * LRO,LRE->LTR, RLO,RLE->RTL, otherwise->ON. */
345 #define FRIBIDI_EXPLICIT_TO_OVERRIDE_DIR(p) \
346 (FRIBIDI_IS_OVERRIDE(p) ? FRIBIDI_LEVEL_TO_DIR(FRIBIDI_DIR_TO_LEVEL(p)) \
349 /* Weaken type for paragraph fallback purposes:
350 * LTR->WLTR, RTL->WRTL. */
351 #define FRIBIDI_WEAK_PARAGRAPH(p) (FRIBIDI_PAR_WLTR | ((p) & FRIBIDI_MASK_RTL))
354 /* Functions finally */
357 /* fribidi_get_bidi_type - get character bidi type
359 * This function returns the bidi type of a character as defined in Table 3.7
360 * Bidirectional Character Types of the Unicode Bidirectional Algorithm
362 * http://www.unicode.org/reports/tr9/#Bidirectional_Character_Types, using
363 * data provided in file UnicodeData.txt of the Unicode Character Database
364 * available at http://www.unicode.org/Public/UNIDATA/UnicodeData.txt.
366 * There are a few macros defined in fribidi-bidi-types.h for querying a bidi
369 FRIBIDI_ENTRY FriBidiCharType
370 fribidi_get_bidi_type (
371 FriBidiChar ch /* input character */
372 ) FRIBIDI_GNUC_CONST;
374 /* fribidi_get_bidi_types - get bidi types for an string of characters
376 * This function finds the bidi types of an string of characters. See
377 * fribidi_get_bidi_type() for more information about the bidi types returned
380 FRIBIDI_ENTRY void fribidi_get_bidi_types (
381 const FriBidiChar *str, /* input string */
382 const FriBidiStrIndex len, /* input string length */
383 FriBidiCharType *btypes /* output bidi types */
386 /* fribidi_get_bidi_type_name - get bidi type name
388 * This function returns the bidi type name of a character type. The
389 * returned string is a static string and should not be freed.
391 * The type names are the same as ones defined in Table 3.7 Bidirectional
392 * Character Types of the Unicode Bidirectional Algorithm available at
393 * http://www.unicode.org/reports/tr9/#Bidirectional_Character_Types, with a
394 * few modifications: L->LTR, R->RTL, B->BS, S->SS.
396 FRIBIDI_ENTRY const char *fribidi_get_bidi_type_name (
397 FriBidiCharType t /* input bidi type */
398 ) FRIBIDI_GNUC_CONST;
400 #include "fribidi-enddecls.h"
402 #endif /* !_FRIBIDI_BIDI_TYPES_H */
403 /* Editor directions:
404 * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent