2 ******************************************************************************
4 * Copyright (C) 1999-2001, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ubidiimp.h
10 * tab size: 8 (not used)
13 * created on: 1999aug06
14 * created by: Markus W. Scherer
20 /* set import/export definitions */
21 #ifdef U_COMMON_IMPLEMENTATION
23 #include "unicode/utypes.h"
24 #include "unicode/uchar.h"
26 /* miscellaneous definitions ---------------------------------------------- */
28 typedef uint8_t DirProp
;
29 typedef uint32_t Flags
;
31 /* Comparing the description of the BiDi algorithm with this implementation
32 is easier with the same names for the BiDi types in the code as there.
33 See UCharDirection in uchar.h .
38 EN
= U_EUROPEAN_NUMBER
,
39 ES
= U_EUROPEAN_NUMBER_SEPARATOR
,
40 ET
= U_EUROPEAN_NUMBER_TERMINATOR
,
42 CS
= U_COMMON_NUMBER_SEPARATOR
,
44 S
= U_SEGMENT_SEPARATOR
,
45 WS
= U_WHITE_SPACE_NEUTRAL
,
47 LRE
=U_LEFT_TO_RIGHT_EMBEDDING
,
48 LRO
=U_LEFT_TO_RIGHT_OVERRIDE
,
49 AL
= U_RIGHT_TO_LEFT_ARABIC
,
50 RLE
=U_RIGHT_TO_LEFT_EMBEDDING
,
51 RLO
=U_RIGHT_TO_LEFT_OVERRIDE
,
52 PDF
=U_POP_DIRECTIONAL_FORMAT
,
53 NSM
=U_DIR_NON_SPACING_MARK
,
54 BN
= U_BOUNDARY_NEUTRAL
,
59 * Sometimes, bit values are more appropriate
60 * to deal with directionality properties.
61 * Abbreviations in these macro names refer to names
62 * used in the BiDi algorithm.
64 #define DIRPROP_FLAG(dir) (1UL<<(dir))
66 /* special flag for multiple runs from explicit embedding codes */
67 #define DIRPROP_FLAG_MULTI_RUNS (1UL<<31)
69 /* are there any characters that are LTR or RTL? */
70 #define MASK_LTR (DIRPROP_FLAG(L)|DIRPROP_FLAG(EN)|DIRPROP_FLAG(AN)|DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO))
71 #define MASK_RTL (DIRPROP_FLAG(R)|DIRPROP_FLAG(AL)|DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO))
73 /* explicit embedding codes */
74 #define MASK_LRX (DIRPROP_FLAG(LRE)|DIRPROP_FLAG(LRO))
75 #define MASK_RLX (DIRPROP_FLAG(RLE)|DIRPROP_FLAG(RLO))
76 #define MASK_OVERRIDE (DIRPROP_FLAG(LRO)|DIRPROP_FLAG(RLO))
78 #define MASK_EXPLICIT (MASK_LRX|MASK_RLX|DIRPROP_FLAG(PDF))
79 #define MASK_BN_EXPLICIT (DIRPROP_FLAG(BN)|MASK_EXPLICIT)
81 /* paragraph and segment separators */
82 #define MASK_B_S (DIRPROP_FLAG(B)|DIRPROP_FLAG(S))
84 /* all types that are counted as White Space or Neutral in some steps */
85 #define MASK_WS (MASK_B_S|DIRPROP_FLAG(WS)|MASK_BN_EXPLICIT)
86 #define MASK_N (DIRPROP_FLAG(ON)|MASK_WS)
88 /* all types that are included in a sequence of European Terminators for (W5) */
89 #define MASK_ET_NSM_BN (DIRPROP_FLAG(ET)|DIRPROP_FLAG(NSM)|MASK_BN_EXPLICIT)
91 /* types that are neutrals or could becomes neutrals in (Wn) */
92 #define MASK_POSSIBLE_N (DIRPROP_FLAG(CS)|DIRPROP_FLAG(ES)|DIRPROP_FLAG(ET)|MASK_N)
95 * These types may be changed to "e",
96 * the embedding type (L or R) of the run,
97 * in the BiDi algorithm (N2)
99 #define MASK_EMBEDDING (DIRPROP_FLAG(NSM)|MASK_POSSIBLE_N)
101 /* the dirProp's L and R are defined to 0 and 1 values in UCharDirection */
102 #define GET_LR_FROM_LEVEL(level) ((DirProp)((level)&1))
104 #define IS_DEFAULT_LEVEL(level) (((level)&0xfe)==0xfe)
106 /* Run structure for reordering --------------------------------------------- */
109 int32_t logicalStart
, /* first character of the run; b31 indicates even/odd level */
110 visualLimit
; /* last visual position of the run +1 */
113 /* in a Run, logicalStart will get this bit set if the run level is odd */
114 #define INDEX_ODD_BIT (1UL<<31)
116 #define MAKE_INDEX_ODD_PAIR(index, level) (index|((int32_t)level<<31))
117 #define ADD_ODD_BIT_FROM_LEVEL(x, level) ((x)|=((int32_t)level<<31))
118 #define REMOVE_ODD_BIT(x) ((x)&=~INDEX_ODD_BIT)
120 #define GET_INDEX(x) (x&~INDEX_ODD_BIT)
121 #define GET_ODD_BIT(x) ((uint32_t)x>>31)
122 #define IS_ODD_RUN(x) ((x&INDEX_ODD_BIT)!=0)
123 #define IS_EVEN_RUN(x) ((x&INDEX_ODD_BIT)==0)
126 ubidi_getRuns(UBiDi
*pBiDi
);
128 /* UBiDi structure ----------------------------------------------------------- */
131 /* alias pointer to the current text */
134 /* length of the current text */
137 /* memory sizes in bytes */
138 int32_t dirPropsSize
, levelsSize
, runsSize
;
140 /* allocated memory */
141 DirProp
*dirPropsMemory
;
142 UBiDiLevel
*levelsMemory
;
145 /* indicators for whether memory may be allocated after ubidi_open() */
146 UBool mayAllocateText
, mayAllocateRuns
;
148 /* arrays with one value per text-character */
149 const DirProp
*dirProps
;
152 /* are we performing an approximation of the "inverse BiDi" algorithm? */
155 /* the paragraph level */
156 UBiDiLevel paraLevel
;
158 /* the overall paragraph or line directionality - see UBiDiDirection */
159 UBiDiDirection direction
;
161 /* flags is a bit set for which directional properties are in the text */
164 /* characters after trailingWSStart are WS and are */
165 /* implicitly at the paraLevel (rule (L1)) - levels may not reflect that */
166 int32_t trailingWSStart
;
168 /* fields for line reordering */
169 int32_t runCount
; /* ==-1: runs not set up yet */
172 /* for non-mixed text, we only need a tiny array of runs (no malloc()) */
176 /* helper function to (re)allocate memory if allowed */
178 ubidi_getMemory(void **pMemory
, int32_t *pSize
, UBool mayAllocate
, int32_t sizeNeeded
);
180 /* helper macros for each allocated array in UBiDi */
181 #define getDirPropsMemory(pBiDi, length) \
182 ubidi_getMemory((void **)&(pBiDi)->dirPropsMemory, &(pBiDi)->dirPropsSize, \
183 (pBiDi)->mayAllocateText, (length))
185 #define getLevelsMemory(pBiDi, length) \
186 ubidi_getMemory((void **)&(pBiDi)->levelsMemory, &(pBiDi)->levelsSize, \
187 (pBiDi)->mayAllocateText, (length))
189 #define getRunsMemory(pBiDi, length) \
190 ubidi_getMemory((void **)&(pBiDi)->runsMemory, &(pBiDi)->runsSize, \
191 (pBiDi)->mayAllocateRuns, (length)*sizeof(Run))
193 /* additional macros used by ubidi_open() - always allow allocation */
194 #define getInitialDirPropsMemory(pBiDi, length) \
195 ubidi_getMemory((void **)&(pBiDi)->dirPropsMemory, &(pBiDi)->dirPropsSize, \
198 #define getInitialLevelsMemory(pBiDi, length) \
199 ubidi_getMemory((void **)&(pBiDi)->levelsMemory, &(pBiDi)->levelsSize, \
202 #define getInitialRunsMemory(pBiDi, length) \
203 ubidi_getMemory((void **)&(pBiDi)->runsMemory, &(pBiDi)->runsSize, \
204 TRUE, (length)*sizeof(Run))