]>
Commit | Line | Data |
---|---|---|
73c04bcf | 1 | /* |
b75a7d8f A |
2 | ****************************************************************************** |
3 | * | |
b331163b | 4 | * Copyright (C) 1999-2015, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ****************************************************************************** | |
8 | * file name: ubidi.c | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 1999jul27 | |
46f4442e | 14 | * created by: Markus W. Scherer, updated by Matitiahu Allouche |
57a6839d | 15 | * |
b75a7d8f A |
16 | */ |
17 | ||
b75a7d8f A |
18 | #include "cmemory.h" |
19 | #include "unicode/utypes.h" | |
20 | #include "unicode/ustring.h" | |
21 | #include "unicode/uchar.h" | |
22 | #include "unicode/ubidi.h" | |
4388f060 | 23 | #include "unicode/utf16.h" |
73c04bcf | 24 | #include "ubidi_props.h" |
b75a7d8f | 25 | #include "ubidiimp.h" |
46f4442e | 26 | #include "uassert.h" |
b75a7d8f A |
27 | |
28 | /* | |
29 | * General implementation notes: | |
30 | * | |
31 | * Throughout the implementation, there are comments like (W2) that refer to | |
57a6839d A |
32 | * rules of the BiDi algorithm, in this example to the second rule of the |
33 | * resolution of weak types. | |
b75a7d8f A |
34 | * |
35 | * For handling surrogate pairs, where two UChar's form one "abstract" (or UTF-32) | |
36 | * character according to UTF-16, the second UChar gets the directional property of | |
37 | * the entire character assigned, while the first one gets a BN, a boundary | |
38 | * neutral, type, which is ignored by most of the algorithm according to | |
39 | * rule (X9) and the implementation suggestions of the BiDi algorithm. | |
40 | * | |
41 | * Later, adjustWSLevels() will set the level for each BN to that of the | |
42 | * following character (UChar), which results in surrogate pairs getting the | |
43 | * same level on each of their surrogates. | |
44 | * | |
45 | * In a UTF-8 implementation, the same thing could be done: the last byte of | |
46 | * a multi-byte sequence would get the "real" property, while all previous | |
47 | * bytes of that sequence would get BN. | |
48 | * | |
49 | * It is not possible to assign all those parts of a character the same real | |
50 | * property because this would fail in the resolution of weak types with rules | |
51 | * that look at immediately surrounding types. | |
52 | * | |
53 | * As a related topic, this implementation does not remove Boundary Neutral | |
73c04bcf | 54 | * types from the input, but ignores them wherever this is relevant. |
b75a7d8f A |
55 | * For example, the loop for the resolution of the weak types reads |
56 | * types until it finds a non-BN. | |
57 | * Also, explicit embedding codes are neither changed into BN nor removed. | |
58 | * They are only treated the same way real BNs are. | |
59 | * As stated before, adjustWSLevels() takes care of them at the end. | |
60 | * For the purpose of conformance, the levels of all these codes | |
61 | * do not matter. | |
62 | * | |
57a6839d A |
63 | * Note that this implementation modifies the dirProps |
64 | * after the initial setup, when applying X5c (replace FSI by LRI or RLI), | |
65 | * X6, N0 (replace paired brackets by L or R). | |
b75a7d8f | 66 | * |
57a6839d A |
67 | * In this implementation, the resolution of weak types (W1 to W6), |
68 | * neutrals (N1 and N2), and the assignment of the resolved level (In) | |
b75a7d8f A |
69 | * are all done in one single loop, in resolveImplicitLevels(). |
70 | * Changes of dirProp values are done on the fly, without writing | |
71 | * them back to the dirProps array. | |
72 | * | |
73 | * | |
74 | * This implementation contains code that allows to bypass steps of the | |
75 | * algorithm that are not needed on the specific paragraph | |
76 | * in order to speed up the most common cases considerably, | |
77 | * like text that is entirely LTR, or RTL text without numbers. | |
78 | * | |
79 | * Most of this is done by setting a bit for each directional property | |
80 | * in a flags variable and later checking for whether there are | |
81 | * any LTR characters or any RTL characters, or both, whether | |
82 | * there are any explicit embedding codes, etc. | |
83 | * | |
84 | * If the (Xn) steps are performed, then the flags are re-evaluated, | |
85 | * because they will then not contain the embedding codes any more | |
86 | * and will be adjusted for override codes, so that subsequently | |
87 | * more bypassing may be possible than what the initial flags suggested. | |
88 | * | |
89 | * If the text is not mixed-directional, then the | |
90 | * algorithm steps for the weak type resolution are not performed, | |
91 | * and all levels are set to the paragraph level. | |
92 | * | |
93 | * If there are no explicit embedding codes, then the (Xn) steps | |
94 | * are not performed. | |
95 | * | |
96 | * If embedding levels are supplied as a parameter, then all | |
97 | * explicit embedding codes are ignored, and the (Xn) steps | |
98 | * are not performed. | |
99 | * | |
100 | * White Space types could get the level of the run they belong to, | |
101 | * and are checked with a test of (flags&MASK_EMBEDDING) to | |
102 | * consider if the paragraph direction should be considered in | |
103 | * the flags variable. | |
104 | * | |
105 | * If there are no White Space types in the paragraph, then | |
106 | * (L1) is not necessary in adjustWSLevels(). | |
107 | */ | |
108 | ||
b75a7d8f A |
109 | /* to avoid some conditional statements, use tiny constant arrays */ |
110 | static const Flags flagLR[2]={ DIRPROP_FLAG(L), DIRPROP_FLAG(R) }; | |
111 | static const Flags flagE[2]={ DIRPROP_FLAG(LRE), DIRPROP_FLAG(RLE) }; | |
112 | static const Flags flagO[2]={ DIRPROP_FLAG(LRO), DIRPROP_FLAG(RLO) }; | |
113 | ||
114 | #define DIRPROP_FLAG_LR(level) flagLR[(level)&1] | |
57a6839d A |
115 | #define DIRPROP_FLAG_E(level) flagE[(level)&1] |
116 | #define DIRPROP_FLAG_O(level) flagO[(level)&1] | |
117 | ||
118 | #define DIR_FROM_STRONG(strong) ((strong)==L ? L : R) | |
119 | ||
120 | #define NO_OVERRIDE(level) ((level)&~UBIDI_LEVEL_OVERRIDE) | |
b75a7d8f A |
121 | |
122 | /* UBiDi object management -------------------------------------------------- */ | |
123 | ||
124 | U_CAPI UBiDi * U_EXPORT2 | |
73c04bcf | 125 | ubidi_open(void) |
b75a7d8f A |
126 | { |
127 | UErrorCode errorCode=U_ZERO_ERROR; | |
128 | return ubidi_openSized(0, 0, &errorCode); | |
129 | } | |
130 | ||
131 | U_CAPI UBiDi * U_EXPORT2 | |
132 | ubidi_openSized(int32_t maxLength, int32_t maxRunCount, UErrorCode *pErrorCode) { | |
133 | UBiDi *pBiDi; | |
134 | ||
135 | /* check the argument values */ | |
136 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
137 | return NULL; | |
138 | } else if(maxLength<0 || maxRunCount<0) { | |
139 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
140 | return NULL; /* invalid arguments */ | |
141 | } | |
142 | ||
143 | /* allocate memory for the object */ | |
144 | pBiDi=(UBiDi *)uprv_malloc(sizeof(UBiDi)); | |
145 | if(pBiDi==NULL) { | |
146 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
147 | return NULL; | |
148 | } | |
149 | ||
150 | /* reset the object, all pointers NULL, all flags FALSE, all sizes 0 */ | |
151 | uprv_memset(pBiDi, 0, sizeof(UBiDi)); | |
152 | ||
73c04bcf | 153 | /* get BiDi properties */ |
729e4ab9 | 154 | pBiDi->bdp=ubidi_getSingleton(); |
73c04bcf | 155 | |
b75a7d8f A |
156 | /* allocate memory for arrays as requested */ |
157 | if(maxLength>0) { | |
158 | if( !getInitialDirPropsMemory(pBiDi, maxLength) || | |
159 | !getInitialLevelsMemory(pBiDi, maxLength) | |
160 | ) { | |
161 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
162 | } | |
163 | } else { | |
164 | pBiDi->mayAllocateText=TRUE; | |
165 | } | |
166 | ||
167 | if(maxRunCount>0) { | |
168 | if(maxRunCount==1) { | |
169 | /* use simpleRuns[] */ | |
170 | pBiDi->runsSize=sizeof(Run); | |
171 | } else if(!getInitialRunsMemory(pBiDi, maxRunCount)) { | |
172 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
173 | } | |
174 | } else { | |
175 | pBiDi->mayAllocateRuns=TRUE; | |
176 | } | |
177 | ||
178 | if(U_SUCCESS(*pErrorCode)) { | |
179 | return pBiDi; | |
180 | } else { | |
181 | ubidi_close(pBiDi); | |
182 | return NULL; | |
183 | } | |
184 | } | |
185 | ||
186 | /* | |
187 | * We are allowed to allocate memory if memory==NULL or | |
188 | * mayAllocate==TRUE for each array that we need. | |
46f4442e | 189 | * We also try to grow memory as needed if we |
b75a7d8f A |
190 | * allocate it. |
191 | * | |
192 | * Assume sizeNeeded>0. | |
193 | * If *pMemory!=NULL, then assume *pSize>0. | |
194 | * | |
195 | * ### this realloc() may unnecessarily copy the old data, | |
196 | * which we know we don't need any more; | |
197 | * is this the best way to do this?? | |
198 | */ | |
199 | U_CFUNC UBool | |
46f4442e A |
200 | ubidi_getMemory(BidiMemoryForAllocation *bidiMem, int32_t *pSize, UBool mayAllocate, int32_t sizeNeeded) { |
201 | void **pMemory = (void **)bidiMem; | |
b75a7d8f A |
202 | /* check for existing memory */ |
203 | if(*pMemory==NULL) { | |
204 | /* we need to allocate memory */ | |
205 | if(mayAllocate && (*pMemory=uprv_malloc(sizeNeeded))!=NULL) { | |
206 | *pSize=sizeNeeded; | |
207 | return TRUE; | |
208 | } else { | |
209 | return FALSE; | |
210 | } | |
211 | } else { | |
46f4442e A |
212 | if(sizeNeeded<=*pSize) { |
213 | /* there is already enough memory */ | |
214 | return TRUE; | |
215 | } | |
216 | else if(!mayAllocate) { | |
b75a7d8f A |
217 | /* not enough memory, and we must not allocate */ |
218 | return FALSE; | |
46f4442e A |
219 | } else { |
220 | /* we try to grow */ | |
b75a7d8f | 221 | void *memory; |
46f4442e A |
222 | /* in most cases, we do not need the copy-old-data part of |
223 | * realloc, but it is needed when adding runs using getRunsMemory() | |
224 | * in setParaRunsOnly() | |
225 | */ | |
b75a7d8f A |
226 | if((memory=uprv_realloc(*pMemory, sizeNeeded))!=NULL) { |
227 | *pMemory=memory; | |
228 | *pSize=sizeNeeded; | |
229 | return TRUE; | |
230 | } else { | |
231 | /* we failed to grow */ | |
232 | return FALSE; | |
233 | } | |
b75a7d8f A |
234 | } |
235 | } | |
236 | } | |
237 | ||
238 | U_CAPI void U_EXPORT2 | |
239 | ubidi_close(UBiDi *pBiDi) { | |
240 | if(pBiDi!=NULL) { | |
73c04bcf | 241 | pBiDi->pParaBiDi=NULL; /* in case one tries to reuse this block */ |
2ca993e8 A |
242 | if(pBiDi->dirInsertMemory!=NULL) { |
243 | uprv_free(pBiDi->dirInsertMemory); | |
244 | } | |
b75a7d8f A |
245 | if(pBiDi->dirPropsMemory!=NULL) { |
246 | uprv_free(pBiDi->dirPropsMemory); | |
247 | } | |
248 | if(pBiDi->levelsMemory!=NULL) { | |
249 | uprv_free(pBiDi->levelsMemory); | |
250 | } | |
57a6839d A |
251 | if(pBiDi->openingsMemory!=NULL) { |
252 | uprv_free(pBiDi->openingsMemory); | |
b75a7d8f | 253 | } |
73c04bcf A |
254 | if(pBiDi->parasMemory!=NULL) { |
255 | uprv_free(pBiDi->parasMemory); | |
256 | } | |
57a6839d A |
257 | if(pBiDi->runsMemory!=NULL) { |
258 | uprv_free(pBiDi->runsMemory); | |
259 | } | |
260 | if(pBiDi->isolatesMemory!=NULL) { | |
261 | uprv_free(pBiDi->isolatesMemory); | |
262 | } | |
73c04bcf A |
263 | if(pBiDi->insertPoints.points!=NULL) { |
264 | uprv_free(pBiDi->insertPoints.points); | |
265 | } | |
266 | ||
b75a7d8f A |
267 | uprv_free(pBiDi); |
268 | } | |
269 | } | |
270 | ||
271 | /* set to approximate "inverse BiDi" ---------------------------------------- */ | |
272 | ||
273 | U_CAPI void U_EXPORT2 | |
274 | ubidi_setInverse(UBiDi *pBiDi, UBool isInverse) { | |
275 | if(pBiDi!=NULL) { | |
276 | pBiDi->isInverse=isInverse; | |
73c04bcf A |
277 | pBiDi->reorderingMode = isInverse ? UBIDI_REORDER_INVERSE_NUMBERS_AS_L |
278 | : UBIDI_REORDER_DEFAULT; | |
b75a7d8f A |
279 | } |
280 | } | |
281 | ||
282 | U_CAPI UBool U_EXPORT2 | |
283 | ubidi_isInverse(UBiDi *pBiDi) { | |
284 | if(pBiDi!=NULL) { | |
285 | return pBiDi->isInverse; | |
286 | } else { | |
287 | return FALSE; | |
288 | } | |
289 | } | |
290 | ||
73c04bcf A |
291 | /* FOOD FOR THOUGHT: currently the reordering modes are a mixture of |
292 | * algorithm for direct BiDi, algorithm for inverse BiDi and the bizarre | |
293 | * concept of RUNS_ONLY which is a double operation. | |
294 | * It could be advantageous to divide this into 3 concepts: | |
295 | * a) Operation: direct / inverse / RUNS_ONLY | |
46f4442e | 296 | * b) Direct algorithm: default / NUMBERS_SPECIAL / GROUP_NUMBERS_WITH_R |
73c04bcf A |
297 | * c) Inverse algorithm: default / INVERSE_LIKE_DIRECT / NUMBERS_SPECIAL |
298 | * This would allow combinations not possible today like RUNS_ONLY with | |
299 | * NUMBERS_SPECIAL. | |
300 | * Also allow to set INSERT_MARKS for the direct step of RUNS_ONLY and | |
301 | * REMOVE_CONTROLS for the inverse step. | |
302 | * Not all combinations would be supported, and probably not all do make sense. | |
303 | * This would need to document which ones are supported and what are the | |
304 | * fallbacks for unsupported combinations. | |
305 | */ | |
306 | U_CAPI void U_EXPORT2 | |
307 | ubidi_setReorderingMode(UBiDi *pBiDi, UBiDiReorderingMode reorderingMode) { | |
46f4442e | 308 | if ((pBiDi!=NULL) && (reorderingMode >= UBIDI_REORDER_DEFAULT) |
73c04bcf A |
309 | && (reorderingMode < UBIDI_REORDER_COUNT)) { |
310 | pBiDi->reorderingMode = reorderingMode; | |
46f4442e | 311 | pBiDi->isInverse = (UBool)(reorderingMode == UBIDI_REORDER_INVERSE_NUMBERS_AS_L); |
73c04bcf A |
312 | } |
313 | } | |
314 | ||
315 | U_CAPI UBiDiReorderingMode U_EXPORT2 | |
316 | ubidi_getReorderingMode(UBiDi *pBiDi) { | |
46f4442e | 317 | if (pBiDi!=NULL) { |
73c04bcf A |
318 | return pBiDi->reorderingMode; |
319 | } else { | |
320 | return UBIDI_REORDER_DEFAULT; | |
321 | } | |
322 | } | |
323 | ||
324 | U_CAPI void U_EXPORT2 | |
325 | ubidi_setReorderingOptions(UBiDi *pBiDi, uint32_t reorderingOptions) { | |
326 | if (reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) { | |
327 | reorderingOptions&=~UBIDI_OPTION_INSERT_MARKS; | |
328 | } | |
46f4442e A |
329 | if (pBiDi!=NULL) { |
330 | pBiDi->reorderingOptions=reorderingOptions; | |
73c04bcf A |
331 | } |
332 | } | |
333 | ||
334 | U_CAPI uint32_t U_EXPORT2 | |
335 | ubidi_getReorderingOptions(UBiDi *pBiDi) { | |
46f4442e | 336 | if (pBiDi!=NULL) { |
73c04bcf A |
337 | return pBiDi->reorderingOptions; |
338 | } else { | |
339 | return 0; | |
340 | } | |
341 | } | |
342 | ||
729e4ab9 A |
343 | U_CAPI UBiDiDirection U_EXPORT2 |
344 | ubidi_getBaseDirection(const UChar *text, | |
345 | int32_t length){ | |
346 | ||
347 | int32_t i; | |
348 | UChar32 uchar; | |
349 | UCharDirection dir; | |
4388f060 | 350 | |
729e4ab9 A |
351 | if( text==NULL || length<-1 ){ |
352 | return UBIDI_NEUTRAL; | |
353 | } | |
354 | ||
355 | if(length==-1) { | |
356 | length=u_strlen(text); | |
357 | } | |
358 | ||
359 | for( i = 0 ; i < length; ) { | |
360 | /* i is incremented by U16_NEXT */ | |
361 | U16_NEXT(text, i, length, uchar); | |
362 | dir = u_charDirection(uchar); | |
363 | if( dir == U_LEFT_TO_RIGHT ) | |
364 | return UBIDI_LTR; | |
365 | if( dir == U_RIGHT_TO_LEFT || dir ==U_RIGHT_TO_LEFT_ARABIC ) | |
366 | return UBIDI_RTL; | |
367 | } | |
368 | return UBIDI_NEUTRAL; | |
369 | } | |
370 | ||
b75a7d8f A |
371 | /* perform (P2)..(P3) ------------------------------------------------------- */ |
372 | ||
57a6839d A |
373 | /** |
374 | * Returns the directionality of the first strong character | |
375 | * after the last B in prologue, if any. | |
376 | * Requires prologue!=null. | |
377 | */ | |
4388f060 A |
378 | static DirProp |
379 | firstL_R_AL(UBiDi *pBiDi) { | |
4388f060 A |
380 | const UChar *text=pBiDi->prologue; |
381 | int32_t length=pBiDi->proLength; | |
382 | int32_t i; | |
383 | UChar32 uchar; | |
384 | DirProp dirProp, result=ON; | |
385 | for(i=0; i<length; ) { | |
386 | /* i is incremented by U16_NEXT */ | |
387 | U16_NEXT(text, i, length, uchar); | |
388 | dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar); | |
389 | if(result==ON) { | |
390 | if(dirProp==L || dirProp==R || dirProp==AL) { | |
391 | result=dirProp; | |
392 | } | |
393 | } else { | |
394 | if(dirProp==B) { | |
395 | result=ON; | |
396 | } | |
397 | } | |
398 | } | |
399 | return result; | |
400 | } | |
401 | ||
b75a7d8f | 402 | /* |
57a6839d | 403 | * Check that there are enough entries in the array pointed to by pBiDi->paras |
b75a7d8f | 404 | */ |
57a6839d A |
405 | static UBool |
406 | checkParaCount(UBiDi *pBiDi) { | |
407 | int32_t count=pBiDi->paraCount; | |
408 | if(pBiDi->paras==pBiDi->simpleParas) { | |
b331163b | 409 | if(count<=SIMPLE_PARAS_COUNT) |
57a6839d | 410 | return TRUE; |
b331163b | 411 | if(!getInitialParasMemory(pBiDi, SIMPLE_PARAS_COUNT * 2)) |
57a6839d A |
412 | return FALSE; |
413 | pBiDi->paras=pBiDi->parasMemory; | |
b331163b | 414 | uprv_memcpy(pBiDi->parasMemory, pBiDi->simpleParas, SIMPLE_PARAS_COUNT * sizeof(Para)); |
57a6839d A |
415 | return TRUE; |
416 | } | |
417 | if(!getInitialParasMemory(pBiDi, count * 2)) | |
418 | return FALSE; | |
419 | pBiDi->paras=pBiDi->parasMemory; | |
420 | return TRUE; | |
421 | } | |
422 | ||
2ca993e8 A |
423 | /* |
424 | * Get the directional properties for the inserted bidi controls. | |
425 | */ | |
426 | ||
427 | /* subset of bidi properties, fit in 4 bits */ | |
428 | enum { /* correspondence to standard class */ | |
429 | Insert_none = 0, /* 0 all others */ | |
430 | Insert_L, /* 1 L = U_LEFT_TO_RIGHT */ | |
431 | Insert_R, /* 2 R = U_RIGHT_TO_LEFT */ | |
432 | Insert_AL, /* 3 AL = U_RIGHT_TO_LEFT_ARABIC */ | |
433 | Insert_LRE, /* 4 LRE = U_LEFT_TO_RIGHT_EMBEDDING */ | |
434 | Insert_LRO, /* 5 LRO = U_LEFT_TO_RIGHT_OVERRIDE */ | |
435 | Insert_RLE, /* 6 RLE = U_RIGHT_TO_LEFT_EMBEDDING */ | |
436 | Insert_RLO, /* 7 RLO = U_RIGHT_TO_LEFT_OVERRIDE */ | |
437 | Insert_PDF, /* 8 PDF = U_POP_DIRECTIONAL_FORMAT */ | |
438 | Insert_FSI, /* 9 FSI = U_FIRST_STRONG_ISOLATE */ | |
439 | Insert_LRI, /* 10 LRI = U_LEFT_TO_RIGHT_ISOLATE */ | |
440 | Insert_RLI, /* 11 RLI = U_RIGHT_TO_LEFT_ISOLATE */ | |
441 | Insert_PDI, /* 12 PDI = U_POP_DIRECTIONAL_ISOLATE */ | |
442 | Insert_B, /* 13 B = U_BLOCK_SEPARATOR */ | |
443 | Insert_S, /* 14 S = U_SEGMENT_SEPARATOR */ | |
444 | Insert_WS, /* 15 WS = U_WHITE_SPACE_NEUTRAL */ | |
445 | Insert_count /* 16 */ | |
446 | }; | |
447 | ||
448 | /* map standard dir class to special 4-bit insert value (Insert_none as default) */ | |
449 | static const uint16_t insertDirFromStdDir[dirPropCount] = { | |
450 | Insert_none, /* L= U_LEFT_TO_RIGHT */ | |
451 | Insert_none, /* R= U_RIGHT_TO_LEFT, */ | |
452 | Insert_none, /* EN= U_EUROPEAN_NUMBER */ | |
453 | Insert_none, /* ES= U_EUROPEAN_NUMBER_SEPARATOR */ | |
454 | Insert_none, /* ET= U_EUROPEAN_NUMBER_TERMINATOR */ | |
455 | Insert_none, /* AN= U_ARABIC_NUMBER */ | |
456 | Insert_none, /* CS= U_COMMON_NUMBER_SEPARATOR */ | |
457 | Insert_none, /* B= U_BLOCK_SEPARATOR */ | |
458 | Insert_none, /* S= U_SEGMENT_SEPARATOR */ | |
459 | Insert_none, /* WS= U_WHITE_SPACE_NEUTRAL */ | |
460 | Insert_none, /* ON= U_OTHER_NEUTRAL */ | |
461 | Insert_LRE, /* LRE=U_LEFT_TO_RIGHT_EMBEDDING */ | |
462 | Insert_LRO, /* LRO=U_LEFT_TO_RIGHT_OVERRIDE */ | |
463 | Insert_none, /* AL= U_RIGHT_TO_LEFT_ARABIC */ | |
464 | Insert_RLE, /* RLE=U_RIGHT_TO_LEFT_EMBEDDING */ | |
465 | Insert_RLO, /* RLO=U_RIGHT_TO_LEFT_OVERRIDE */ | |
466 | Insert_PDF, /* PDF=U_POP_DIRECTIONAL_FORMAT */ | |
467 | Insert_none, /* NSM=U_DIR_NON_SPACING_MARK */ | |
468 | Insert_none, /* BN= U_BOUNDARY_NEUTRAL */ | |
469 | Insert_FSI, /* FSI=U_FIRST_STRONG_ISOLATE */ | |
470 | Insert_LRI, /* LRI=U_LEFT_TO_RIGHT_ISOLATE */ | |
471 | Insert_RLI, /* RLI=U_RIGHT_TO_LEFT_ISOLATE */ | |
472 | Insert_PDI, /* PDI=U_POP_DIRECTIONAL_ISOLATE */ | |
473 | Insert_none, /* ENL */ | |
474 | Insert_none, /* ENR */ | |
475 | }; | |
476 | ||
477 | /* map special 4-bit insert direction class to standard dir class (ON as default) */ | |
478 | static const uint8_t stdDirFromInsertDir[Insert_count] = { | |
479 | ON, /* Insert_none > ON */ | |
480 | L, /* Insert_L */ | |
481 | R, /* Insert_R */ | |
482 | AL, /* Insert_AL */ | |
483 | LRE, /* Insert_LRE */ | |
484 | LRO, /* Insert_LRO */ | |
485 | RLE, /* Insert_RLE */ | |
486 | RLO, /* Insert_RLO */ | |
487 | PDF, /* Insert_PDF */ | |
488 | FSI, /* Insert_FSI */ | |
489 | LRI, /* Insert_LRI */ | |
490 | RLI, /* Insert_RLI */ | |
491 | PDI, /* Insert_PDI */ | |
492 | B, /* Insert_B */ | |
493 | S, /* Insert_S */ | |
494 | WS, /* Insert_WS */ | |
495 | }; | |
496 | ||
497 | enum { kMaxControlStringLen = 4 }; | |
498 | ||
499 | static UBool | |
500 | getDirInsert(UBiDi *pBiDi, | |
501 | const int32_t *offsets, int32_t offsetCount, | |
502 | const int32_t *controlStringIndices, | |
503 | const UChar * const * controlStrings) { | |
504 | int32_t offset, offsetsIndex; | |
505 | uint16_t *dirInsert = pBiDi->dirInsert; | |
506 | /* initialize dirInsert */ | |
507 | for (offset = 0; offset < pBiDi->length; offset++) { | |
508 | dirInsert[offset] = 0; | |
509 | } | |
510 | for (offsetsIndex = 0; offsetsIndex < offsetCount; offsetsIndex++) { | |
511 | const UChar * controlString; | |
512 | UChar uchar; | |
513 | int32_t controlStringIndex, dirInsertIndex = 0; | |
514 | uint16_t dirInsertValue = 0; | |
515 | offset = offsets[offsetsIndex]; | |
516 | if (offset < 0 || offset >= pBiDi->length) { | |
517 | return FALSE; /* param err in offsets array */ | |
518 | } | |
519 | controlStringIndex = (controlStringIndices == NULL)? offsetsIndex: controlStringIndices[offsetsIndex]; | |
520 | controlString = controlStrings[controlStringIndex]; | |
521 | if (controlString == NULL) { | |
522 | return FALSE; /* param err in controlStrings array */ | |
523 | } | |
524 | while ((uchar = *controlString++) != 0) { | |
525 | uint16_t insertValue = (U16_IS_SURROGATE(uchar))? Insert_none: | |
526 | insertDirFromStdDir[(uint32_t)ubidi_getCustomizedClass(pBiDi, uchar)]; | |
527 | if (dirInsertIndex >= kMaxControlStringLen || insertValue == Insert_none) { | |
528 | return FALSE; /* param err in controlStrings array */ | |
529 | } | |
530 | dirInsertValue |= (insertValue << (4 * dirInsertIndex++)); | |
531 | } | |
532 | dirInsert[offset] = dirInsertValue; | |
533 | } | |
534 | return TRUE; | |
535 | } | |
536 | ||
57a6839d A |
537 | /* |
538 | * Get the directional properties for the text, calculate the flags bit-set, and | |
539 | * determine the paragraph level if necessary (in pBiDi->paras[i].level). | |
540 | * FSI initiators are also resolved and their dirProp replaced with LRI or RLI. | |
541 | * When encountering an FSI, it is initially replaced with an LRI, which is the | |
542 | * default. Only if a strong R or AL is found within its scope will the LRI be | |
543 | * replaced by an RLI. | |
544 | */ | |
545 | static UBool | |
73c04bcf A |
546 | getDirProps(UBiDi *pBiDi) { |
547 | const UChar *text=pBiDi->text; | |
b75a7d8f | 548 | DirProp *dirProps=pBiDi->dirPropsMemory; /* pBiDi->dirProps is const */ |
2ca993e8 | 549 | uint16_t *dirInsert = pBiDi->dirInsert; /* may be NULL */ |
b75a7d8f | 550 | |
57a6839d | 551 | int32_t i=0, originalLength=pBiDi->originalLength; |
b75a7d8f A |
552 | Flags flags=0; /* collect all directionalities in the text */ |
553 | UChar32 uchar; | |
57a6839d | 554 | DirProp dirProp=0, defaultParaLevel=0; /* initialize to avoid compiler warnings */ |
2ca993e8 A |
555 | int32_t dirInsertValue; |
556 | int8_t dirInsertIndex; /* position within dirInsertValue, if any */ | |
73c04bcf A |
557 | UBool isDefaultLevel=IS_DEFAULT_LEVEL(pBiDi->paraLevel); |
558 | /* for inverse BiDi, the default para level is set to RTL if there is a | |
57a6839d | 559 | strong R or AL character at either end of the text */ |
46f4442e | 560 | UBool isDefaultLevelInverse=isDefaultLevel && (UBool) |
73c04bcf A |
561 | (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT || |
562 | pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL); | |
563 | int32_t lastArabicPos=-1; | |
564 | int32_t controlCount=0; | |
46f4442e A |
565 | UBool removeBiDiControls = (UBool)(pBiDi->reorderingOptions & |
566 | UBIDI_OPTION_REMOVE_CONTROLS); | |
73c04bcf A |
567 | |
568 | typedef enum { | |
57a6839d A |
569 | NOT_SEEKING_STRONG, /* 0: not contextual paraLevel, not after FSI */ |
570 | SEEKING_STRONG_FOR_PARA, /* 1: looking for first strong char in para */ | |
571 | SEEKING_STRONG_FOR_FSI, /* 2: looking for first strong after FSI */ | |
572 | LOOKING_FOR_PDI /* 3: found strong after FSI, looking for PDI */ | |
73c04bcf A |
573 | } State; |
574 | State state; | |
57a6839d A |
575 | DirProp lastStrong=ON; /* for default level & inverse BiDi */ |
576 | /* The following stacks are used to manage isolate sequences. Those | |
577 | sequences may be nested, but obviously never more deeply than the | |
578 | maximum explicit embedding level. | |
579 | lastStack is the index of the last used entry in the stack. A value of -1 | |
580 | means that there is no open isolate sequence. | |
581 | lastStack is reset to -1 on paragraph boundaries. */ | |
582 | /* The following stack contains the position of the initiator of | |
583 | each open isolate sequence */ | |
584 | int32_t isolateStartStack[UBIDI_MAX_EXPLICIT_LEVEL+1]; | |
2ca993e8 | 585 | int8_t isolateStartInsertIndex[UBIDI_MAX_EXPLICIT_LEVEL+1]; |
57a6839d A |
586 | /* The following stack contains the last known state before |
587 | encountering the initiator of an isolate sequence */ | |
588 | int8_t previousStateStack[UBIDI_MAX_EXPLICIT_LEVEL+1]; | |
589 | int32_t stackLast=-1; | |
590 | ||
591 | if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) | |
73c04bcf | 592 | pBiDi->length=0; |
57a6839d | 593 | defaultParaLevel=pBiDi->paraLevel&1; |
73c04bcf | 594 | if(isDefaultLevel) { |
57a6839d A |
595 | pBiDi->paras[0].level=defaultParaLevel; |
596 | lastStrong=defaultParaLevel; | |
597 | if(pBiDi->proLength>0 && /* there is a prologue */ | |
598 | (dirProp=firstL_R_AL(pBiDi))!=ON) { /* with a strong character */ | |
599 | if(dirProp==L) | |
600 | pBiDi->paras[0].level=0; /* set the default para level */ | |
601 | else | |
602 | pBiDi->paras[0].level=1; /* set the default para level */ | |
603 | state=NOT_SEEKING_STRONG; | |
4388f060 | 604 | } else { |
57a6839d | 605 | state=SEEKING_STRONG_FOR_PARA; |
4388f060 | 606 | } |
374ca955 | 607 | } else { |
57a6839d A |
608 | pBiDi->paras[0].level=pBiDi->paraLevel; |
609 | state=NOT_SEEKING_STRONG; | |
b75a7d8f | 610 | } |
73c04bcf A |
611 | /* count paragraphs and determine the paragraph level (P2..P3) */ |
612 | /* | |
613 | * see comment in ubidi.h: | |
57a6839d | 614 | * the UBIDI_DEFAULT_XXX values are designed so that |
73c04bcf A |
615 | * their bit 0 alone yields the intended default |
616 | */ | |
2ca993e8 A |
617 | dirInsertValue = 0; |
618 | dirInsertIndex = -1; /* indicate that we have not checked dirInsert yet */ | |
57a6839d | 619 | for( /* i=0 above */ ; i<originalLength; ) { |
2ca993e8 A |
620 | if (dirInsert != NULL && dirInsertIndex < 0) { |
621 | dirInsertValue = dirInsert[i]; | |
622 | } | |
623 | if (dirInsertValue > 0) { | |
624 | dirInsertIndex++; | |
625 | dirProp = (DirProp)stdDirFromInsertDir[dirInsertValue & 0x000F]; | |
626 | dirInsertValue >>= 4; | |
627 | flags|=DIRPROP_FLAG(dirProp); | |
628 | uchar = 0; | |
629 | } else { | |
630 | dirInsertIndex = -1; | |
631 | /* i is incremented by U16_NEXT */ | |
632 | U16_NEXT(text, i, originalLength, uchar); | |
633 | flags|=DIRPROP_FLAG(dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar)); | |
634 | dirProps[i-1]=dirProp; | |
635 | if(uchar>0xffff) { /* set the lead surrogate's property to BN */ | |
636 | flags|=DIRPROP_FLAG(BN); | |
637 | dirProps[i-2]=BN; | |
638 | } | |
57a6839d A |
639 | } |
640 | if(removeBiDiControls && IS_BIDI_CONTROL_CHAR(uchar)) | |
641 | controlCount++; | |
642 | if(dirProp==L) { | |
643 | if(state==SEEKING_STRONG_FOR_PARA) { | |
644 | pBiDi->paras[pBiDi->paraCount-1].level=0; | |
645 | state=NOT_SEEKING_STRONG; | |
73c04bcf | 646 | } |
57a6839d A |
647 | else if(state==SEEKING_STRONG_FOR_FSI) { |
648 | if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) { | |
649 | /* no need for next statement, already set by default */ | |
650 | /* dirProps[isolateStartStack[stackLast]]=LRI; */ | |
651 | flags|=DIRPROP_FLAG(LRI); | |
73c04bcf | 652 | } |
57a6839d | 653 | state=LOOKING_FOR_PDI; |
73c04bcf | 654 | } |
57a6839d A |
655 | lastStrong=L; |
656 | continue; | |
73c04bcf | 657 | } |
57a6839d A |
658 | if(dirProp==R || dirProp==AL) { |
659 | if(state==SEEKING_STRONG_FOR_PARA) { | |
660 | pBiDi->paras[pBiDi->paraCount-1].level=1; | |
661 | state=NOT_SEEKING_STRONG; | |
662 | } | |
663 | else if(state==SEEKING_STRONG_FOR_FSI) { | |
664 | if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) { | |
2ca993e8 A |
665 | if (isolateStartInsertIndex[stackLast] < 0) { |
666 | dirProps[isolateStartStack[stackLast]]=RLI; | |
667 | } else { | |
668 | dirInsert[stackLast] &= ~(0x000F << (4*isolateStartInsertIndex[stackLast])); | |
669 | dirInsert[stackLast] |= (Insert_RLI << (4*isolateStartInsertIndex[stackLast])); | |
670 | } | |
57a6839d A |
671 | flags|=DIRPROP_FLAG(RLI); |
672 | } | |
673 | state=LOOKING_FOR_PDI; | |
674 | } | |
675 | lastStrong=R; | |
676 | if(dirProp==AL) | |
677 | lastArabicPos=i-1; | |
678 | continue; | |
73c04bcf | 679 | } |
57a6839d A |
680 | if(dirProp>=FSI && dirProp<=RLI) { /* FSI, LRI or RLI */ |
681 | stackLast++; | |
682 | if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) { | |
2ca993e8 A |
683 | isolateStartStack[stackLast]= (dirInsertIndex < 0)? i-1: i /* we have not incremented with U16_NEXT yet */; |
684 | isolateStartInsertIndex[stackLast] = dirInsertIndex; | |
57a6839d A |
685 | previousStateStack[stackLast]=state; |
686 | } | |
687 | if(dirProp==FSI) { | |
2ca993e8 A |
688 | if (dirInsertIndex < 0) { |
689 | dirProps[i-1]=LRI; /* default if no strong char */ | |
690 | } else { | |
691 | dirInsert[i] &= ~(0x000F << (4*dirInsertIndex)); | |
692 | dirInsert[i] |= (Insert_LRI << (4*dirInsertIndex)); | |
693 | } | |
57a6839d A |
694 | state=SEEKING_STRONG_FOR_FSI; |
695 | } | |
696 | else | |
697 | state=LOOKING_FOR_PDI; | |
698 | continue; | |
73c04bcf | 699 | } |
57a6839d A |
700 | if(dirProp==PDI) { |
701 | if(state==SEEKING_STRONG_FOR_FSI) { | |
702 | if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) { | |
703 | /* no need for next statement, already set by default */ | |
704 | /* dirProps[isolateStartStack[stackLast]]=LRI; */ | |
705 | flags|=DIRPROP_FLAG(LRI); | |
706 | } | |
707 | } | |
708 | if(stackLast>=0) { | |
709 | if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) | |
710 | state=previousStateStack[stackLast]; | |
711 | stackLast--; | |
712 | } | |
713 | continue; | |
73c04bcf | 714 | } |
57a6839d A |
715 | if(dirProp==B) { |
716 | if(i<originalLength && uchar==CR && text[i]==LF) /* do nothing on the CR */ | |
717 | continue; | |
718 | pBiDi->paras[pBiDi->paraCount-1].limit=i; | |
719 | if(isDefaultLevelInverse && lastStrong==R) | |
720 | pBiDi->paras[pBiDi->paraCount-1].level=1; | |
73c04bcf | 721 | if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) { |
57a6839d A |
722 | /* When streaming, we only process whole paragraphs |
723 | thus some updates are only done on paragraph boundaries */ | |
73c04bcf | 724 | pBiDi->length=i; /* i is index to next character */ |
57a6839d | 725 | pBiDi->controlCount=controlCount; |
73c04bcf | 726 | } |
57a6839d A |
727 | if(i<originalLength) { /* B not last char in text */ |
728 | pBiDi->paraCount++; | |
729 | if(checkParaCount(pBiDi)==FALSE) /* not enough memory for a new para entry */ | |
730 | return FALSE; | |
73c04bcf | 731 | if(isDefaultLevel) { |
57a6839d A |
732 | pBiDi->paras[pBiDi->paraCount-1].level=defaultParaLevel; |
733 | state=SEEKING_STRONG_FOR_PARA; | |
734 | lastStrong=defaultParaLevel; | |
735 | } else { | |
736 | pBiDi->paras[pBiDi->paraCount-1].level=pBiDi->paraLevel; | |
737 | state=NOT_SEEKING_STRONG; | |
73c04bcf | 738 | } |
57a6839d | 739 | stackLast=-1; |
73c04bcf | 740 | } |
57a6839d | 741 | continue; |
73c04bcf | 742 | } |
b75a7d8f | 743 | } |
57a6839d A |
744 | /* Ignore still open isolate sequences with overflow */ |
745 | if(stackLast>UBIDI_MAX_EXPLICIT_LEVEL) { | |
746 | stackLast=UBIDI_MAX_EXPLICIT_LEVEL; | |
747 | state=SEEKING_STRONG_FOR_FSI; /* to be on the safe side */ | |
748 | } | |
749 | /* Resolve direction of still unresolved open FSI sequences */ | |
750 | while(stackLast>=0) { | |
751 | if(state==SEEKING_STRONG_FOR_FSI) { | |
752 | /* no need for next statement, already set by default */ | |
753 | /* dirProps[isolateStartStack[stackLast]]=LRI; */ | |
754 | flags|=DIRPROP_FLAG(LRI); | |
755 | break; | |
73c04bcf | 756 | } |
57a6839d A |
757 | state=previousStateStack[stackLast]; |
758 | stackLast--; | |
73c04bcf | 759 | } |
57a6839d | 760 | /* When streaming, ignore text after the last paragraph separator */ |
73c04bcf | 761 | if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) { |
57a6839d | 762 | if(pBiDi->length<originalLength) |
73c04bcf | 763 | pBiDi->paraCount--; |
57a6839d A |
764 | } else { |
765 | pBiDi->paras[pBiDi->paraCount-1].limit=originalLength; | |
766 | pBiDi->controlCount=controlCount; | |
767 | } | |
768 | /* For inverse bidi, default para direction is RTL if there is | |
769 | a strong R or AL at either end of the paragraph */ | |
770 | if(isDefaultLevelInverse && lastStrong==R) { | |
771 | pBiDi->paras[pBiDi->paraCount-1].level=1; | |
73c04bcf | 772 | } |
57a6839d A |
773 | if(isDefaultLevel) { |
774 | pBiDi->paraLevel=pBiDi->paras[0].level; | |
775 | } | |
776 | /* The following is needed to resolve the text direction for default level | |
777 | paragraphs containing no strong character */ | |
778 | for(i=0; i<pBiDi->paraCount; i++) | |
779 | flags|=DIRPROP_FLAG_LR(pBiDi->paras[i].level); | |
73c04bcf A |
780 | |
781 | if(pBiDi->orderParagraphsLTR && (flags&DIRPROP_FLAG(B))) { | |
782 | flags|=DIRPROP_FLAG(L); | |
b75a7d8f | 783 | } |
b75a7d8f | 784 | pBiDi->flags=flags; |
73c04bcf | 785 | pBiDi->lastArabicPos=lastArabicPos; |
57a6839d A |
786 | return TRUE; |
787 | } | |
788 | ||
789 | /* determine the paragraph level at position index */ | |
790 | U_CFUNC UBiDiLevel | |
791 | ubidi_getParaLevelAtIndex(const UBiDi *pBiDi, int32_t pindex) { | |
792 | int32_t i; | |
793 | for(i=0; i<pBiDi->paraCount; i++) | |
794 | if(pindex<pBiDi->paras[i].limit) | |
795 | break; | |
796 | if(i>=pBiDi->paraCount) | |
797 | i=pBiDi->paraCount-1; | |
798 | return (UBiDiLevel)(pBiDi->paras[i].level); | |
799 | } | |
800 | ||
801 | /* Functions for handling paired brackets ----------------------------------- */ | |
802 | ||
803 | /* In the isoRuns array, the first entry is used for text outside of any | |
804 | isolate sequence. Higher entries are used for each more deeply nested | |
805 | isolate sequence. isoRunLast is the index of the last used entry. The | |
806 | openings array is used to note the data of opening brackets not yet | |
807 | matched by a closing bracket, or matched but still susceptible to change | |
808 | level. | |
809 | Each isoRun entry contains the index of the first and | |
810 | one-after-last openings entries for pending opening brackets it | |
811 | contains. The next openings entry to use is the one-after-last of the | |
812 | most deeply nested isoRun entry. | |
813 | isoRun entries also contain their current embedding level and the last | |
814 | encountered strong character, since these will be needed to resolve | |
815 | the level of paired brackets. */ | |
816 | ||
817 | static void | |
818 | bracketInit(UBiDi *pBiDi, BracketData *bd) { | |
819 | bd->pBiDi=pBiDi; | |
820 | bd->isoRunLast=0; | |
821 | bd->isoRuns[0].start=0; | |
822 | bd->isoRuns[0].limit=0; | |
823 | bd->isoRuns[0].level=GET_PARALEVEL(pBiDi, 0); | |
824 | bd->isoRuns[0].lastStrong=bd->isoRuns[0].lastBase=bd->isoRuns[0].contextDir=GET_PARALEVEL(pBiDi, 0)&1; | |
825 | bd->isoRuns[0].contextPos=0; | |
826 | if(pBiDi->openingsMemory) { | |
827 | bd->openings=pBiDi->openingsMemory; | |
b331163b | 828 | bd->openingsCount=pBiDi->openingsSize / sizeof(Opening); |
57a6839d A |
829 | } else { |
830 | bd->openings=bd->simpleOpenings; | |
b331163b | 831 | bd->openingsCount=SIMPLE_OPENINGS_COUNT; |
57a6839d A |
832 | } |
833 | bd->isNumbersSpecial=bd->pBiDi->reorderingMode==UBIDI_REORDER_NUMBERS_SPECIAL || | |
834 | bd->pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL; | |
835 | } | |
836 | ||
837 | /* paragraph boundary */ | |
838 | static void | |
839 | bracketProcessB(BracketData *bd, UBiDiLevel level) { | |
840 | bd->isoRunLast=0; | |
841 | bd->isoRuns[0].limit=0; | |
842 | bd->isoRuns[0].level=level; | |
843 | bd->isoRuns[0].lastStrong=bd->isoRuns[0].lastBase=bd->isoRuns[0].contextDir=level&1; | |
844 | bd->isoRuns[0].contextPos=0; | |
845 | } | |
846 | ||
847 | /* LRE, LRO, RLE, RLO, PDF */ | |
848 | static void | |
2ca993e8 | 849 | bracketProcessBoundary(BracketData *bd, int32_t lastCcPos, DirProp lastCcDirProp, |
57a6839d A |
850 | UBiDiLevel contextLevel, UBiDiLevel embeddingLevel) { |
851 | IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
2ca993e8 | 852 | if(DIRPROP_FLAG(lastCcDirProp)&MASK_ISO) /* after an isolate */ |
57a6839d A |
853 | return; |
854 | if(NO_OVERRIDE(embeddingLevel)>NO_OVERRIDE(contextLevel)) /* not a PDF */ | |
855 | contextLevel=embeddingLevel; | |
856 | pLastIsoRun->limit=pLastIsoRun->start; | |
857 | pLastIsoRun->level=embeddingLevel; | |
858 | pLastIsoRun->lastStrong=pLastIsoRun->lastBase=pLastIsoRun->contextDir=contextLevel&1; | |
859 | pLastIsoRun->contextPos=lastCcPos; | |
860 | } | |
861 | ||
862 | /* LRI or RLI */ | |
863 | static void | |
864 | bracketProcessLRI_RLI(BracketData *bd, UBiDiLevel level) { | |
865 | IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
866 | int16_t lastLimit; | |
867 | pLastIsoRun->lastBase=ON; | |
868 | lastLimit=pLastIsoRun->limit; | |
869 | bd->isoRunLast++; | |
870 | pLastIsoRun++; | |
871 | pLastIsoRun->start=pLastIsoRun->limit=lastLimit; | |
872 | pLastIsoRun->level=level; | |
873 | pLastIsoRun->lastStrong=pLastIsoRun->lastBase=pLastIsoRun->contextDir=level&1; | |
874 | pLastIsoRun->contextPos=0; | |
875 | } | |
876 | ||
877 | /* PDI */ | |
878 | static void | |
879 | bracketProcessPDI(BracketData *bd) { | |
880 | IsoRun *pLastIsoRun; | |
881 | bd->isoRunLast--; | |
882 | pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
883 | pLastIsoRun->lastBase=ON; | |
884 | } | |
885 | ||
886 | /* newly found opening bracket: create an openings entry */ | |
887 | static UBool /* return TRUE if success */ | |
888 | bracketAddOpening(BracketData *bd, UChar match, int32_t position) { | |
889 | IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
890 | Opening *pOpening; | |
b331163b | 891 | if(pLastIsoRun->limit>=bd->openingsCount) { /* no available new entry */ |
57a6839d A |
892 | UBiDi *pBiDi=bd->pBiDi; |
893 | if(!getInitialOpeningsMemory(pBiDi, pLastIsoRun->limit * 2)) | |
894 | return FALSE; | |
895 | if(bd->openings==bd->simpleOpenings) | |
896 | uprv_memcpy(pBiDi->openingsMemory, bd->simpleOpenings, | |
b331163b | 897 | SIMPLE_OPENINGS_COUNT * sizeof(Opening)); |
57a6839d | 898 | bd->openings=pBiDi->openingsMemory; /* may have changed */ |
b331163b | 899 | bd->openingsCount=pBiDi->openingsSize / sizeof(Opening); |
57a6839d A |
900 | } |
901 | pOpening=&bd->openings[pLastIsoRun->limit]; | |
902 | pOpening->position=position; | |
903 | pOpening->match=match; | |
904 | pOpening->contextDir=pLastIsoRun->contextDir; | |
905 | pOpening->contextPos=pLastIsoRun->contextPos; | |
906 | pOpening->flags=0; | |
907 | pLastIsoRun->limit++; | |
908 | return TRUE; | |
909 | } | |
910 | ||
911 | /* change N0c1 to N0c2 when a preceding bracket is assigned the embedding level */ | |
912 | static void | |
913 | fixN0c(BracketData *bd, int32_t openingIndex, int32_t newPropPosition, DirProp newProp) { | |
914 | /* This function calls itself recursively */ | |
915 | IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
916 | Opening *qOpening; | |
917 | DirProp *dirProps=bd->pBiDi->dirProps; | |
918 | int32_t k, openingPosition, closingPosition; | |
919 | for(k=openingIndex+1, qOpening=&bd->openings[k]; k<pLastIsoRun->limit; k++, qOpening++) { | |
920 | if(qOpening->match>=0) /* not an N0c match */ | |
921 | continue; | |
922 | if(newPropPosition<qOpening->contextPos) | |
923 | break; | |
924 | if(newPropPosition>=qOpening->position) | |
925 | continue; | |
926 | if(newProp==qOpening->contextDir) | |
927 | break; | |
928 | openingPosition=qOpening->position; | |
929 | dirProps[openingPosition]=newProp; | |
930 | closingPosition=-(qOpening->match); | |
931 | dirProps[closingPosition]=newProp; | |
932 | qOpening->match=0; /* prevent further changes */ | |
933 | fixN0c(bd, k, openingPosition, newProp); | |
934 | fixN0c(bd, k, closingPosition, newProp); | |
935 | } | |
936 | } | |
937 | ||
938 | /* process closing bracket */ | |
939 | static DirProp /* return L or R if N0b or N0c, ON if N0d */ | |
940 | bracketProcessClosing(BracketData *bd, int32_t openIdx, int32_t position) { | |
941 | IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
942 | Opening *pOpening, *qOpening; | |
943 | UBiDiDirection direction; | |
944 | UBool stable; | |
945 | DirProp newProp; | |
946 | pOpening=&bd->openings[openIdx]; | |
947 | direction=pLastIsoRun->level&1; | |
948 | stable=TRUE; /* assume stable until proved otherwise */ | |
949 | ||
950 | /* The stable flag is set when brackets are paired and their | |
951 | level is resolved and cannot be changed by what will be | |
952 | found later in the source string. | |
953 | An unstable match can occur only when applying N0c, where | |
954 | the resolved level depends on the preceding context, and | |
955 | this context may be affected by text occurring later. | |
956 | Example: RTL paragraph containing: abc[(latin) HEBREW] | |
957 | When the closing parenthesis is encountered, it appears | |
958 | that N0c1 must be applied since 'abc' sets an opposite | |
959 | direction context and both parentheses receive level 2. | |
960 | However, when the closing square bracket is processed, | |
961 | N0b applies because of 'HEBREW' being included within the | |
962 | brackets, thus the square brackets are treated like R and | |
963 | receive level 1. However, this changes the preceding | |
964 | context of the opening parenthesis, and it now appears | |
965 | that N0c2 must be applied to the parentheses rather than | |
966 | N0c1. */ | |
967 | ||
968 | if((direction==0 && pOpening->flags&FOUND_L) || | |
969 | (direction==1 && pOpening->flags&FOUND_R)) { /* N0b */ | |
970 | newProp=direction; | |
971 | } | |
972 | else if(pOpening->flags&(FOUND_L|FOUND_R)) { /* N0c */ | |
973 | /* it is stable if there is no containing pair or in | |
974 | conditions too complicated and not worth checking */ | |
975 | stable=(openIdx==pLastIsoRun->start); | |
976 | if(direction!=pOpening->contextDir) | |
977 | newProp=pOpening->contextDir; /* N0c1 */ | |
978 | else | |
979 | newProp=direction; /* N0c2 */ | |
980 | } else { | |
981 | /* forget this and any brackets nested within this pair */ | |
982 | pLastIsoRun->limit=openIdx; | |
983 | return ON; /* N0d */ | |
984 | } | |
985 | bd->pBiDi->dirProps[pOpening->position]=newProp; | |
986 | bd->pBiDi->dirProps[position]=newProp; | |
987 | /* Update nested N0c pairs that may be affected */ | |
988 | fixN0c(bd, openIdx, pOpening->position, newProp); | |
989 | if(stable) { | |
990 | pLastIsoRun->limit=openIdx; /* forget any brackets nested within this pair */ | |
991 | /* remove lower located synonyms if any */ | |
992 | while(pLastIsoRun->limit>pLastIsoRun->start && | |
993 | bd->openings[pLastIsoRun->limit-1].position==pOpening->position) | |
994 | pLastIsoRun->limit--; | |
995 | } else { | |
996 | int32_t k; | |
997 | pOpening->match=-position; | |
998 | /* neutralize lower located synonyms if any */ | |
999 | k=openIdx-1; | |
1000 | while(k>=pLastIsoRun->start && | |
1001 | bd->openings[k].position==pOpening->position) | |
1002 | bd->openings[k--].match=0; | |
1003 | /* neutralize any unmatched opening between the current pair; | |
1004 | this will also neutralize higher located synonyms if any */ | |
1005 | for(k=openIdx+1; k<pLastIsoRun->limit; k++) { | |
1006 | qOpening=&bd->openings[k]; | |
1007 | if(qOpening->position>=position) | |
1008 | break; | |
1009 | if(qOpening->match>0) | |
1010 | qOpening->match=0; | |
1011 | } | |
1012 | } | |
1013 | return newProp; | |
1014 | } | |
1015 | ||
1016 | /* handle strong characters, digits and candidates for closing brackets */ | |
1017 | static UBool /* return TRUE if success */ | |
1018 | bracketProcessChar(BracketData *bd, int32_t position) { | |
1019 | IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast]; | |
1020 | DirProp *dirProps, dirProp, newProp; | |
1021 | UBiDiLevel level; | |
1022 | dirProps=bd->pBiDi->dirProps; | |
1023 | dirProp=dirProps[position]; | |
1024 | if(dirProp==ON) { | |
1025 | UChar c, match; | |
1026 | int32_t idx; | |
1027 | /* First see if it is a matching closing bracket. Hopefully, this is | |
1028 | more efficient than checking if it is a closing bracket at all */ | |
1029 | c=bd->pBiDi->text[position]; | |
1030 | for(idx=pLastIsoRun->limit-1; idx>=pLastIsoRun->start; idx--) { | |
1031 | if(bd->openings[idx].match!=c) | |
1032 | continue; | |
1033 | /* We have a match */ | |
1034 | newProp=bracketProcessClosing(bd, idx, position); | |
1035 | if(newProp==ON) { /* N0d */ | |
1036 | c=0; /* prevent handling as an opening */ | |
1037 | break; | |
1038 | } | |
1039 | pLastIsoRun->lastBase=ON; | |
1040 | pLastIsoRun->contextDir=newProp; | |
1041 | pLastIsoRun->contextPos=position; | |
1042 | level=bd->pBiDi->levels[position]; | |
1043 | if(level&UBIDI_LEVEL_OVERRIDE) { /* X4, X5 */ | |
1044 | uint16_t flag; | |
1045 | int32_t i; | |
1046 | newProp=level&1; | |
1047 | pLastIsoRun->lastStrong=newProp; | |
1048 | flag=DIRPROP_FLAG(newProp); | |
1049 | for(i=pLastIsoRun->start; i<idx; i++) | |
1050 | bd->openings[i].flags|=flag; | |
1051 | /* matching brackets are not overridden by LRO/RLO */ | |
1052 | bd->pBiDi->levels[position]&=~UBIDI_LEVEL_OVERRIDE; | |
1053 | } | |
1054 | /* matching brackets are not overridden by LRO/RLO */ | |
1055 | bd->pBiDi->levels[bd->openings[idx].position]&=~UBIDI_LEVEL_OVERRIDE; | |
1056 | return TRUE; | |
1057 | } | |
1058 | /* We get here only if the ON character is not a matching closing | |
1059 | bracket or it is a case of N0d */ | |
1060 | /* Now see if it is an opening bracket */ | |
1061 | if(c) | |
1062 | match=u_getBidiPairedBracket(c); /* get the matching char */ | |
1063 | else | |
1064 | match=0; | |
1065 | if(match!=c && /* has a matching char */ | |
1066 | ubidi_getPairedBracketType(bd->pBiDi->bdp, c)==U_BPT_OPEN) { /* opening bracket */ | |
1067 | /* special case: process synonyms | |
1068 | create an opening entry for each synonym */ | |
1069 | if(match==0x232A) { /* RIGHT-POINTING ANGLE BRACKET */ | |
1070 | if(!bracketAddOpening(bd, 0x3009, position)) | |
1071 | return FALSE; | |
1072 | } | |
1073 | else if(match==0x3009) { /* RIGHT ANGLE BRACKET */ | |
1074 | if(!bracketAddOpening(bd, 0x232A, position)) | |
1075 | return FALSE; | |
1076 | } | |
1077 | if(!bracketAddOpening(bd, match, position)) | |
1078 | return FALSE; | |
1079 | } | |
1080 | } | |
1081 | level=bd->pBiDi->levels[position]; | |
1082 | if(level&UBIDI_LEVEL_OVERRIDE) { /* X4, X5 */ | |
1083 | newProp=level&1; | |
1084 | if(dirProp!=S && dirProp!=WS && dirProp!=ON) | |
1085 | dirProps[position]=newProp; | |
1086 | pLastIsoRun->lastBase=newProp; | |
1087 | pLastIsoRun->lastStrong=newProp; | |
1088 | pLastIsoRun->contextDir=newProp; | |
1089 | pLastIsoRun->contextPos=position; | |
1090 | } | |
1091 | else if(dirProp<=R || dirProp==AL) { | |
1092 | newProp=DIR_FROM_STRONG(dirProp); | |
1093 | pLastIsoRun->lastBase=dirProp; | |
1094 | pLastIsoRun->lastStrong=dirProp; | |
1095 | pLastIsoRun->contextDir=newProp; | |
1096 | pLastIsoRun->contextPos=position; | |
1097 | } | |
1098 | else if(dirProp==EN) { | |
1099 | pLastIsoRun->lastBase=EN; | |
1100 | if(pLastIsoRun->lastStrong==L) { | |
1101 | newProp=L; /* W7 */ | |
1102 | if(!bd->isNumbersSpecial) | |
1103 | dirProps[position]=ENL; | |
1104 | pLastIsoRun->contextDir=L; | |
1105 | pLastIsoRun->contextPos=position; | |
1106 | } | |
1107 | else { | |
1108 | newProp=R; /* N0 */ | |
1109 | if(pLastIsoRun->lastStrong==AL) | |
1110 | dirProps[position]=AN; /* W2 */ | |
1111 | else | |
1112 | dirProps[position]=ENR; | |
1113 | pLastIsoRun->contextDir=R; | |
1114 | pLastIsoRun->contextPos=position; | |
1115 | } | |
1116 | } | |
1117 | else if(dirProp==AN) { | |
1118 | newProp=R; /* N0 */ | |
1119 | pLastIsoRun->lastBase=AN; | |
1120 | pLastIsoRun->contextDir=R; | |
1121 | pLastIsoRun->contextPos=position; | |
1122 | } | |
1123 | else if(dirProp==NSM) { | |
1124 | /* if the last real char was ON, change NSM to ON so that it | |
1125 | will stay ON even if the last real char is a bracket which | |
1126 | may be changed to L or R */ | |
1127 | newProp=pLastIsoRun->lastBase; | |
1128 | if(newProp==ON) | |
1129 | dirProps[position]=newProp; | |
1130 | } | |
1131 | else { | |
1132 | newProp=dirProp; | |
1133 | pLastIsoRun->lastBase=dirProp; | |
1134 | } | |
1135 | if(newProp<=R || newProp==AL) { | |
1136 | int32_t i; | |
1137 | uint16_t flag=DIRPROP_FLAG(DIR_FROM_STRONG(newProp)); | |
1138 | for(i=pLastIsoRun->start; i<pLastIsoRun->limit; i++) | |
1139 | if(position>bd->openings[i].position) | |
1140 | bd->openings[i].flags|=flag; | |
1141 | } | |
1142 | return TRUE; | |
b75a7d8f A |
1143 | } |
1144 | ||
1145 | /* perform (X1)..(X9) ------------------------------------------------------- */ | |
1146 | ||
374ca955 A |
1147 | /* determine if the text is mixed-directional or single-directional */ |
1148 | static UBiDiDirection | |
73c04bcf A |
1149 | directionFromFlags(UBiDi *pBiDi) { |
1150 | Flags flags=pBiDi->flags; | |
374ca955 A |
1151 | /* if the text contains AN and neutrals, then some neutrals may become RTL */ |
1152 | if(!(flags&MASK_RTL || ((flags&DIRPROP_FLAG(AN)) && (flags&MASK_POSSIBLE_N)))) { | |
1153 | return UBIDI_LTR; | |
1154 | } else if(!(flags&MASK_LTR)) { | |
1155 | return UBIDI_RTL; | |
1156 | } else { | |
1157 | return UBIDI_MIXED; | |
1158 | } | |
1159 | } | |
1160 | ||
b75a7d8f A |
1161 | /* |
1162 | * Resolve the explicit levels as specified by explicit embedding codes. | |
1163 | * Recalculate the flags to have them reflect the real properties | |
1164 | * after taking the explicit embeddings into account. | |
1165 | * | |
1166 | * The BiDi algorithm is designed to result in the same behavior whether embedding | |
1167 | * levels are externally specified (from "styled text", supposedly the preferred | |
57a6839d A |
1168 | * method) or set by explicit embedding codes (LRx, RLx, PDF, FSI, PDI) in the plain text. |
1169 | * That is why (X9) instructs to remove all not-isolate explicit codes (and BN). | |
1170 | * However, in a real implementation, the removal of these codes and their index | |
b75a7d8f A |
1171 | * positions in the plain text is undesirable since it would result in |
1172 | * reallocated, reindexed text. | |
1173 | * Instead, this implementation leaves the codes in there and just ignores them | |
1174 | * in the subsequent processing. | |
57a6839d | 1175 | * In order to get the same reordering behavior, positions with a BN or a not-isolate |
b75a7d8f A |
1176 | * explicit embedding code just get the same level assigned as the last "real" |
1177 | * character. | |
1178 | * | |
1179 | * Some implementations, not this one, then overwrite some of these | |
1180 | * directionality properties at "real" same-level-run boundaries by | |
1181 | * L or R codes so that the resolution of weak types can be performed on the | |
1182 | * entire paragraph at once instead of having to parse it once more and | |
1183 | * perform that resolution on same-level-runs. | |
1184 | * This limits the scope of the implicit rules in effectively | |
1185 | * the same way as the run limits. | |
1186 | * | |
57a6839d A |
1187 | * Instead, this implementation does not modify these codes, except for |
1188 | * paired brackets whose properties (ON) may be replaced by L or R. | |
b75a7d8f A |
1189 | * On one hand, the paragraph has to be scanned for same-level-runs, but |
1190 | * on the other hand, this saves another loop to reset these codes, | |
1191 | * or saves making and modifying a copy of dirProps[]. | |
1192 | * | |
1193 | * | |
1194 | * Note that (Pn) and (Xn) changed significantly from version 4 of the BiDi algorithm. | |
1195 | * | |
1196 | * | |
1197 | * Handling the stack of explicit levels (Xn): | |
1198 | * | |
57a6839d A |
1199 | * With the BiDi stack of explicit levels, as pushed with each |
1200 | * LRE, RLE, LRO, RLO, LRI, RLI and FSI and popped with each PDF and PDI, | |
1201 | * the explicit level must never exceed UBIDI_MAX_EXPLICIT_LEVEL. | |
b75a7d8f A |
1202 | * |
1203 | * In order to have a correct push-pop semantics even in the case of overflows, | |
57a6839d A |
1204 | * overflow counters and a valid isolate counter are used as described in UAX#9 |
1205 | * section 3.3.2 "Explicit Levels and Directions". | |
b75a7d8f A |
1206 | * |
1207 | * This implementation assumes that UBIDI_MAX_EXPLICIT_LEVEL is odd. | |
57a6839d A |
1208 | * |
1209 | * Returns normally the direction; -1 if there was a memory shortage | |
1210 | * | |
b75a7d8f | 1211 | */ |
b75a7d8f | 1212 | static UBiDiDirection |
57a6839d A |
1213 | resolveExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) { |
1214 | DirProp *dirProps=pBiDi->dirProps; | |
2ca993e8 | 1215 | uint16_t *dirInsert = pBiDi->dirInsert; /* may be NULL */ |
b75a7d8f | 1216 | UBiDiLevel *levels=pBiDi->levels; |
73c04bcf A |
1217 | const UChar *text=pBiDi->text; |
1218 | ||
b75a7d8f A |
1219 | int32_t i=0, length=pBiDi->length; |
1220 | Flags flags=pBiDi->flags; /* collect all directionalities in the text */ | |
1221 | DirProp dirProp; | |
2ca993e8 A |
1222 | int32_t dirInsertValue; |
1223 | int8_t dirInsertIndex; /* position within dirInsertValue, if any */ | |
73c04bcf | 1224 | UBiDiLevel level=GET_PARALEVEL(pBiDi, 0); |
b75a7d8f | 1225 | UBiDiDirection direction; |
57a6839d A |
1226 | pBiDi->isolateCount=0; |
1227 | ||
1228 | if(U_FAILURE(*pErrorCode)) { return UBIDI_LTR; } | |
b75a7d8f A |
1229 | |
1230 | /* determine if the text is mixed-directional or single-directional */ | |
73c04bcf | 1231 | direction=directionFromFlags(pBiDi); |
b75a7d8f | 1232 | |
57a6839d A |
1233 | /* we may not need to resolve any explicit levels */ |
1234 | if((direction!=UBIDI_MIXED)) { | |
b75a7d8f | 1235 | /* not mixed directionality: levels don't matter - trailingWSStart will be 0 */ |
57a6839d A |
1236 | return direction; |
1237 | } | |
1238 | if(pBiDi->reorderingMode > UBIDI_REORDER_LAST_LOGICAL_TO_VISUAL) { | |
1239 | /* inverse BiDi: mixed, but all characters are at the same embedding level */ | |
b75a7d8f | 1240 | /* set all levels to the paragraph level */ |
57a6839d A |
1241 | int32_t paraIndex, start, limit; |
1242 | for(paraIndex=0; paraIndex<pBiDi->paraCount; paraIndex++) { | |
1243 | if(paraIndex==0) | |
1244 | start=0; | |
1245 | else | |
1246 | start=pBiDi->paras[paraIndex-1].limit; | |
1247 | limit=pBiDi->paras[paraIndex].limit; | |
1248 | level=pBiDi->paras[paraIndex].level; | |
1249 | for(i=start; i<limit; i++) | |
1250 | levels[i]=level; | |
b75a7d8f | 1251 | } |
57a6839d A |
1252 | return direction; /* no bracket matching for inverse BiDi */ |
1253 | } | |
1254 | if(!(flags&(MASK_EXPLICIT|MASK_ISO))) { | |
1255 | /* no embeddings, set all levels to the paragraph level */ | |
1256 | /* we still have to perform bracket matching */ | |
1257 | int32_t paraIndex, start, limit; | |
1258 | BracketData bracketData; | |
1259 | bracketInit(pBiDi, &bracketData); | |
1260 | for(paraIndex=0; paraIndex<pBiDi->paraCount; paraIndex++) { | |
1261 | if(paraIndex==0) | |
1262 | start=0; | |
1263 | else | |
1264 | start=pBiDi->paras[paraIndex-1].limit; | |
1265 | limit=pBiDi->paras[paraIndex].limit; | |
1266 | level=pBiDi->paras[paraIndex].level; | |
1267 | for(i=start; i<limit; i++) { | |
1268 | levels[i]=level; | |
1269 | dirProp=dirProps[i]; | |
1270 | if(dirProp==BN) | |
1271 | continue; | |
1272 | if(dirProp==B) { | |
1273 | if((i+1)<length) { | |
1274 | if(text[i]==CR && text[i+1]==LF) | |
1275 | continue; /* skip CR when followed by LF */ | |
1276 | bracketProcessB(&bracketData, level); | |
1277 | } | |
1278 | continue; | |
1279 | } | |
1280 | if(!bracketProcessChar(&bracketData, i)) { | |
1281 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
1282 | return UBIDI_LTR; | |
1283 | } | |
1284 | } | |
1285 | } | |
1286 | return direction; | |
1287 | } | |
1288 | { | |
b75a7d8f A |
1289 | /* continue to perform (Xn) */ |
1290 | ||
1291 | /* (X1) level is set for all codes, embeddingLevel keeps track of the push/pop operations */ | |
1292 | /* both variables may carry the UBIDI_LEVEL_OVERRIDE flag to indicate the override status */ | |
57a6839d A |
1293 | UBiDiLevel embeddingLevel=level, newLevel; |
1294 | UBiDiLevel previousLevel=level; /* previous level for regular (not CC) characters */ | |
1295 | int32_t lastCcPos=0; /* index of last effective LRx,RLx, PDx */ | |
2ca993e8 | 1296 | DirProp lastCcDirProp=0; /* dirProp of last effective LRx,RLx, PDx */ |
57a6839d A |
1297 | |
1298 | /* The following stack remembers the embedding level and the ISOLATE flag of level runs. | |
1299 | stackLast points to its current entry. */ | |
1300 | uint16_t stack[UBIDI_MAX_EXPLICIT_LEVEL+2]; /* we never push anything >=UBIDI_MAX_EXPLICIT_LEVEL | |
1301 | but we need one more entry as base */ | |
1302 | uint32_t stackLast=0; | |
1303 | int32_t overflowIsolateCount=0; | |
1304 | int32_t overflowEmbeddingCount=0; | |
1305 | int32_t validIsolateCount=0; | |
1306 | BracketData bracketData; | |
1307 | bracketInit(pBiDi, &bracketData); | |
1308 | stack[0]=level; /* initialize base entry to para level, no override, no isolate */ | |
b75a7d8f A |
1309 | |
1310 | /* recalculate the flags */ | |
1311 | flags=0; | |
1312 | ||
2ca993e8 A |
1313 | dirInsertValue = 0; |
1314 | dirInsertIndex = -1; /* indicate that we have not checked dirInsert yet */ | |
1315 | for(i=0; i<length; ) { /* now conditionally increment at end */ | |
1316 | if (dirInsert != NULL && dirInsertIndex < 0) { | |
1317 | dirInsertValue = dirInsert[i]; | |
1318 | } | |
1319 | if (dirInsertValue > 0) { | |
1320 | dirInsertIndex++; | |
1321 | dirProp = (DirProp)stdDirFromInsertDir[dirInsertValue & 0x000F]; | |
1322 | dirInsertValue >>= 4; | |
1323 | } else { | |
1324 | dirInsertIndex = -1; | |
1325 | dirProp=dirProps[i]; | |
1326 | } | |
b75a7d8f A |
1327 | switch(dirProp) { |
1328 | case LRE: | |
b75a7d8f | 1329 | case RLE: |
57a6839d | 1330 | case LRO: |
b75a7d8f | 1331 | case RLO: |
57a6839d A |
1332 | /* (X2, X3, X4, X5) */ |
1333 | flags|=DIRPROP_FLAG(BN); | |
1334 | levels[i]=previousLevel; | |
1335 | if (dirProp==LRE || dirProp==LRO) | |
1336 | /* least greater even level */ | |
1337 | newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1)); | |
1338 | else | |
1339 | /* least greater odd level */ | |
1340 | newLevel=(UBiDiLevel)((NO_OVERRIDE(embeddingLevel)+1)|1); | |
1341 | if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL && overflowIsolateCount==0 && | |
1342 | overflowEmbeddingCount==0) { | |
1343 | lastCcPos=i; | |
2ca993e8 | 1344 | lastCcDirProp = dirProp; |
b75a7d8f | 1345 | embeddingLevel=newLevel; |
57a6839d | 1346 | if(dirProp==LRO || dirProp==RLO) |
b75a7d8f | 1347 | embeddingLevel|=UBIDI_LEVEL_OVERRIDE; |
57a6839d A |
1348 | stackLast++; |
1349 | stack[stackLast]=embeddingLevel; | |
1350 | /* we don't need to set UBIDI_LEVEL_OVERRIDE off for LRE and RLE | |
73c04bcf A |
1351 | since this has already been done for newLevel which is |
1352 | the source for embeddingLevel. | |
1353 | */ | |
b75a7d8f | 1354 | } else { |
57a6839d A |
1355 | if(overflowIsolateCount==0) |
1356 | overflowEmbeddingCount++; | |
b75a7d8f | 1357 | } |
b75a7d8f A |
1358 | break; |
1359 | case PDF: | |
1360 | /* (X7) */ | |
57a6839d A |
1361 | flags|=DIRPROP_FLAG(BN); |
1362 | levels[i]=previousLevel; | |
b75a7d8f | 1363 | /* handle all the overflow cases first */ |
57a6839d A |
1364 | if(overflowIsolateCount) { |
1365 | break; | |
b75a7d8f | 1366 | } |
57a6839d A |
1367 | if(overflowEmbeddingCount) { |
1368 | overflowEmbeddingCount--; | |
1369 | break; | |
1370 | } | |
1371 | if(stackLast>0 && stack[stackLast]<ISOLATE) { /* not an isolate entry */ | |
1372 | lastCcPos=i; | |
2ca993e8 | 1373 | lastCcDirProp = dirProp; |
57a6839d A |
1374 | stackLast--; |
1375 | embeddingLevel=(UBiDiLevel)stack[stackLast]; | |
1376 | } | |
1377 | break; | |
1378 | case LRI: | |
1379 | case RLI: | |
1380 | flags|=(DIRPROP_FLAG(ON)|DIRPROP_FLAG_LR(embeddingLevel)); | |
1381 | levels[i]=NO_OVERRIDE(embeddingLevel); | |
1382 | if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) { | |
2ca993e8 | 1383 | bracketProcessBoundary(&bracketData, lastCcPos, lastCcDirProp, |
57a6839d A |
1384 | previousLevel, embeddingLevel); |
1385 | flags|=DIRPROP_FLAG_MULTI_RUNS; | |
1386 | } | |
1387 | previousLevel=embeddingLevel; | |
1388 | /* (X5a, X5b) */ | |
1389 | if(dirProp==LRI) | |
1390 | /* least greater even level */ | |
1391 | newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1)); | |
1392 | else | |
1393 | /* least greater odd level */ | |
1394 | newLevel=(UBiDiLevel)((NO_OVERRIDE(embeddingLevel)+1)|1); | |
1395 | if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL && overflowIsolateCount==0 && | |
1396 | overflowEmbeddingCount==0) { | |
1397 | flags|=DIRPROP_FLAG(dirProp); | |
1398 | lastCcPos=i; | |
2ca993e8 | 1399 | lastCcDirProp = dirProp; |
57a6839d A |
1400 | validIsolateCount++; |
1401 | if(validIsolateCount>pBiDi->isolateCount) | |
1402 | pBiDi->isolateCount=validIsolateCount; | |
1403 | embeddingLevel=newLevel; | |
1404 | /* we can increment stackLast without checking because newLevel | |
1405 | will exceed UBIDI_MAX_EXPLICIT_LEVEL before stackLast overflows */ | |
1406 | stackLast++; | |
1407 | stack[stackLast]=embeddingLevel+ISOLATE; | |
1408 | bracketProcessLRI_RLI(&bracketData, embeddingLevel); | |
1409 | } else { | |
1410 | /* make it WS so that it is handled by adjustWSLevels() */ | |
2ca993e8 A |
1411 | if (dirInsertIndex < 0) { |
1412 | dirProps[i]=WS; | |
1413 | } else { | |
1414 | dirInsert[i] &= ~(0x000F << (4*dirInsertIndex)); | |
1415 | dirInsert[i] |= (Insert_WS << (4*dirInsertIndex)); | |
1416 | } | |
57a6839d A |
1417 | overflowIsolateCount++; |
1418 | } | |
1419 | break; | |
1420 | case PDI: | |
1421 | if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) { | |
2ca993e8 | 1422 | bracketProcessBoundary(&bracketData, lastCcPos, lastCcDirProp, |
57a6839d A |
1423 | previousLevel, embeddingLevel); |
1424 | flags|=DIRPROP_FLAG_MULTI_RUNS; | |
1425 | } | |
1426 | /* (X6a) */ | |
1427 | if(overflowIsolateCount) { | |
1428 | overflowIsolateCount--; | |
1429 | /* make it WS so that it is handled by adjustWSLevels() */ | |
2ca993e8 A |
1430 | if (dirInsertIndex < 0) { |
1431 | dirProps[i]=WS; | |
1432 | } else { | |
1433 | dirInsert[i] &= ~(0x000F << (4*dirInsertIndex)); | |
1434 | dirInsert[i] |= (Insert_WS << (4*dirInsertIndex)); | |
1435 | } | |
57a6839d A |
1436 | } |
1437 | else if(validIsolateCount) { | |
1438 | flags|=DIRPROP_FLAG(PDI); | |
1439 | lastCcPos=i; | |
2ca993e8 | 1440 | lastCcDirProp = dirProp; |
57a6839d A |
1441 | overflowEmbeddingCount=0; |
1442 | while(stack[stackLast]<ISOLATE) /* pop embedding entries */ | |
1443 | stackLast--; /* until the last isolate entry */ | |
1444 | stackLast--; /* pop also the last isolate entry */ | |
1445 | validIsolateCount--; | |
1446 | bracketProcessPDI(&bracketData); | |
1447 | } else | |
1448 | /* make it WS so that it is handled by adjustWSLevels() */ | |
2ca993e8 A |
1449 | if (dirInsertIndex < 0) { |
1450 | dirProps[i]=WS; | |
1451 | } else { | |
1452 | dirInsert[i] &= ~(0x000F << (4*dirInsertIndex)); | |
1453 | dirInsert[i] |= (Insert_WS << (4*dirInsertIndex)); | |
1454 | } | |
57a6839d A |
1455 | embeddingLevel=(UBiDiLevel)stack[stackLast]&~ISOLATE; |
1456 | flags|=(DIRPROP_FLAG(ON)|DIRPROP_FLAG_LR(embeddingLevel)); | |
1457 | previousLevel=embeddingLevel; | |
1458 | levels[i]=NO_OVERRIDE(embeddingLevel); | |
b75a7d8f A |
1459 | break; |
1460 | case B: | |
57a6839d A |
1461 | flags|=DIRPROP_FLAG(B); |
1462 | levels[i]=GET_PARALEVEL(pBiDi, i); | |
73c04bcf | 1463 | if((i+1)<length) { |
57a6839d A |
1464 | if(text[i]==CR && text[i+1]==LF) |
1465 | break; /* skip CR when followed by LF */ | |
1466 | overflowEmbeddingCount=overflowIsolateCount=0; | |
1467 | validIsolateCount=0; | |
1468 | stackLast=0; | |
1469 | previousLevel=embeddingLevel=GET_PARALEVEL(pBiDi, i+1); | |
1470 | stack[0]=embeddingLevel; /* initialize base entry to para level, no override, no isolate */ | |
1471 | bracketProcessB(&bracketData, embeddingLevel); | |
73c04bcf | 1472 | } |
b75a7d8f A |
1473 | break; |
1474 | case BN: | |
1475 | /* BN, LRE, RLE, and PDF are supposed to be removed (X9) */ | |
1476 | /* they will get their levels set correctly in adjustWSLevels() */ | |
57a6839d | 1477 | levels[i]=previousLevel; |
b75a7d8f A |
1478 | flags|=DIRPROP_FLAG(BN); |
1479 | break; | |
1480 | default: | |
57a6839d A |
1481 | /* all other types are normal characters and get the "real" level */ |
1482 | if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) { | |
2ca993e8 | 1483 | bracketProcessBoundary(&bracketData, lastCcPos, lastCcDirProp, |
57a6839d A |
1484 | previousLevel, embeddingLevel); |
1485 | flags|=DIRPROP_FLAG_MULTI_RUNS; | |
1486 | if(embeddingLevel&UBIDI_LEVEL_OVERRIDE) | |
1487 | flags|=DIRPROP_FLAG_O(embeddingLevel); | |
1488 | else | |
1489 | flags|=DIRPROP_FLAG_E(embeddingLevel); | |
b75a7d8f | 1490 | } |
57a6839d A |
1491 | previousLevel=embeddingLevel; |
1492 | levels[i]=embeddingLevel; | |
1493 | if(!bracketProcessChar(&bracketData, i)) | |
1494 | return -1; | |
1495 | /* the dirProp may have been changed in bracketProcessChar() */ | |
1496 | flags|=DIRPROP_FLAG(dirProps[i]); | |
b75a7d8f A |
1497 | break; |
1498 | } | |
2ca993e8 A |
1499 | if (dirInsertIndex < 0) { |
1500 | ++i; | |
1501 | } | |
b75a7d8f | 1502 | } |
57a6839d | 1503 | if(flags&MASK_EMBEDDING) |
b75a7d8f | 1504 | flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel); |
57a6839d | 1505 | if(pBiDi->orderParagraphsLTR && (flags&DIRPROP_FLAG(B))) |
73c04bcf | 1506 | flags|=DIRPROP_FLAG(L); |
b75a7d8f A |
1507 | /* again, determine if the text is mixed-directional or single-directional */ |
1508 | pBiDi->flags=flags; | |
73c04bcf | 1509 | direction=directionFromFlags(pBiDi); |
b75a7d8f A |
1510 | } |
1511 | return direction; | |
1512 | } | |
1513 | ||
1514 | /* | |
1515 | * Use a pre-specified embedding levels array: | |
1516 | * | |
1517 | * Adjust the directional properties for overrides (->LEVEL_OVERRIDE), | |
1518 | * ignore all explicit codes (X9), | |
1519 | * and check all the preset levels. | |
1520 | * | |
1521 | * Recalculate the flags to have them reflect the real properties | |
1522 | * after taking the explicit embeddings into account. | |
1523 | */ | |
1524 | static UBiDiDirection | |
1525 | checkExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) { | |
57a6839d | 1526 | DirProp *dirProps=pBiDi->dirProps; |
73c04bcf | 1527 | DirProp dirProp; |
b75a7d8f | 1528 | UBiDiLevel *levels=pBiDi->levels; |
57a6839d | 1529 | int32_t isolateCount=0; |
73c04bcf | 1530 | |
b75a7d8f A |
1531 | int32_t i, length=pBiDi->length; |
1532 | Flags flags=0; /* collect all directionalities in the text */ | |
73c04bcf | 1533 | UBiDiLevel level; |
57a6839d | 1534 | pBiDi->isolateCount=0; |
b75a7d8f A |
1535 | |
1536 | for(i=0; i<length; ++i) { | |
1537 | level=levels[i]; | |
57a6839d A |
1538 | dirProp=dirProps[i]; |
1539 | if(dirProp==LRI || dirProp==RLI) { | |
1540 | isolateCount++; | |
1541 | if(isolateCount>pBiDi->isolateCount) | |
1542 | pBiDi->isolateCount=isolateCount; | |
1543 | } | |
1544 | else if(dirProp==PDI) | |
1545 | isolateCount--; | |
1546 | else if(dirProp==B) | |
1547 | isolateCount=0; | |
b75a7d8f A |
1548 | if(level&UBIDI_LEVEL_OVERRIDE) { |
1549 | /* keep the override flag in levels[i] but adjust the flags */ | |
1550 | level&=~UBIDI_LEVEL_OVERRIDE; /* make the range check below simpler */ | |
1551 | flags|=DIRPROP_FLAG_O(level); | |
1552 | } else { | |
1553 | /* set the flags */ | |
73c04bcf | 1554 | flags|=DIRPROP_FLAG_E(level)|DIRPROP_FLAG(dirProp); |
b75a7d8f | 1555 | } |
73c04bcf A |
1556 | if((level<GET_PARALEVEL(pBiDi, i) && |
1557 | !((0==level)&&(dirProp==B))) || | |
1558 | (UBIDI_MAX_EXPLICIT_LEVEL<level)) { | |
b75a7d8f A |
1559 | /* level out of bounds */ |
1560 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1561 | return UBIDI_LTR; | |
1562 | } | |
1563 | } | |
57a6839d | 1564 | if(flags&MASK_EMBEDDING) |
b75a7d8f | 1565 | flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel); |
b75a7d8f A |
1566 | /* determine if the text is mixed-directional or single-directional */ |
1567 | pBiDi->flags=flags; | |
73c04bcf A |
1568 | return directionFromFlags(pBiDi); |
1569 | } | |
1570 | ||
46f4442e A |
1571 | /****************************************************************** |
1572 | The Properties state machine table | |
1573 | ******************************************************************* | |
1574 | ||
1575 | All table cells are 8 bits: | |
1576 | bits 0..4: next state | |
1577 | bits 5..7: action to perform (if > 0) | |
1578 | ||
1579 | Cells may be of format "n" where n represents the next state | |
1580 | (except for the rightmost column). | |
1581 | Cells may also be of format "s(x,y)" where x represents an action | |
1582 | to perform and y represents the next state. | |
1583 | ||
1584 | ******************************************************************* | |
1585 | Definitions and type for properties state table | |
1586 | ******************************************************************* | |
1587 | */ | |
57a6839d | 1588 | #define IMPTABPROPS_COLUMNS 16 |
73c04bcf A |
1589 | #define IMPTABPROPS_RES (IMPTABPROPS_COLUMNS - 1) |
1590 | #define GET_STATEPROPS(cell) ((cell)&0x1f) | |
1591 | #define GET_ACTIONPROPS(cell) ((cell)>>5) | |
46f4442e | 1592 | #define s(action, newState) ((uint8_t)(newState+(action<<5))) |
73c04bcf A |
1593 | |
1594 | static const uint8_t groupProp[] = /* dirProp regrouped */ | |
1595 | { | |
57a6839d A |
1596 | /* L R EN ES ET AN CS B S WS ON LRE LRO AL RLE RLO PDF NSM BN FSI LRI RLI PDI ENL ENR */ |
1597 | 0, 1, 2, 7, 8, 3, 9, 6, 5, 4, 4, 10, 10, 12, 10, 10, 10, 11, 10, 4, 4, 4, 4, 13, 14 | |
73c04bcf | 1598 | }; |
46f4442e A |
1599 | enum { DirProp_L=0, DirProp_R=1, DirProp_EN=2, DirProp_AN=3, DirProp_ON=4, DirProp_S=5, DirProp_B=6 }; /* reduced dirProp */ |
1600 | ||
1601 | /****************************************************************** | |
1602 | ||
1603 | PROPERTIES STATE TABLE | |
1604 | ||
1605 | In table impTabProps, | |
57a6839d | 1606 | - the ON column regroups ON and WS, FSI, RLI, LRI and PDI |
46f4442e A |
1607 | - the BN column regroups BN, LRE, RLE, LRO, RLO, PDF |
1608 | - the Res column is the reduced property assigned to a run | |
1609 | ||
1610 | Action 1: process current run1, init new run1 | |
1611 | 2: init new run2 | |
1612 | 3: process run1, process run2, init new run1 | |
1613 | 4: process run1, set run1=run2, init new run2 | |
1614 | ||
1615 | Notes: | |
1616 | 1) This table is used in resolveImplicitLevels(). | |
1617 | 2) This table triggers actions when there is a change in the Bidi | |
1618 | property of incoming characters (action 1). | |
1619 | 3) Most such property sequences are processed immediately (in | |
1620 | fact, passed to processPropertySeq(). | |
1621 | 4) However, numbers are assembled as one sequence. This means | |
1622 | that undefined situations (like CS following digits, until | |
1623 | it is known if the next char will be a digit) are held until | |
1624 | following chars define them. | |
1625 | Example: digits followed by CS, then comes another CS or ON; | |
1626 | the digits will be processed, then the CS assigned | |
1627 | as the start of an ON sequence (action 3). | |
1628 | 5) There are cases where more than one sequence must be | |
1629 | processed, for instance digits followed by CS followed by L: | |
1630 | the digits must be processed as one sequence, and the CS | |
1631 | must be processed as an ON sequence, all this before starting | |
1632 | assembling chars for the opening L sequence. | |
1633 | ||
1634 | ||
1635 | */ | |
73c04bcf A |
1636 | static const uint8_t impTabProps[][IMPTABPROPS_COLUMNS] = |
1637 | { | |
57a6839d A |
1638 | /* L , R , EN , AN , ON , S , B , ES , ET , CS , BN , NSM , AL , ENL , ENR , Res */ |
1639 | /* 0 Init */ { 1 , 2 , 4 , 5 , 7 , 15 , 17 , 7 , 9 , 7 , 0 , 7 , 3 , 18 , 21 , DirProp_ON }, | |
1640 | /* 1 L */ { 1 , s(1,2), s(1,4), s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), s(1,9), s(1,7), 1 , 1 , s(1,3),s(1,18),s(1,21), DirProp_L }, | |
1641 | /* 2 R */ { s(1,1), 2 , s(1,4), s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), s(1,9), s(1,7), 2 , 2 , s(1,3),s(1,18),s(1,21), DirProp_R }, | |
1642 | /* 3 AL */ { s(1,1), s(1,2), s(1,6), s(1,6), s(1,8),s(1,16),s(1,17), s(1,8), s(1,8), s(1,8), 3 , 3 , 3 ,s(1,18),s(1,21), DirProp_R }, | |
1643 | /* 4 EN */ { s(1,1), s(1,2), 4 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,10), 11 ,s(2,10), 4 , 4 , s(1,3), 18 , 21 , DirProp_EN }, | |
1644 | /* 5 AN */ { s(1,1), s(1,2), s(1,4), 5 , s(1,7),s(1,15),s(1,17), s(1,7), s(1,9),s(2,12), 5 , 5 , s(1,3),s(1,18),s(1,21), DirProp_AN }, | |
1645 | /* 6 AL:EN/AN */ { s(1,1), s(1,2), 6 , 6 , s(1,8),s(1,16),s(1,17), s(1,8), s(1,8),s(2,13), 6 , 6 , s(1,3), 18 , 21 , DirProp_AN }, | |
1646 | /* 7 ON */ { s(1,1), s(1,2), s(1,4), s(1,5), 7 ,s(1,15),s(1,17), 7 ,s(2,14), 7 , 7 , 7 , s(1,3),s(1,18),s(1,21), DirProp_ON }, | |
1647 | /* 8 AL:ON */ { s(1,1), s(1,2), s(1,6), s(1,6), 8 ,s(1,16),s(1,17), 8 , 8 , 8 , 8 , 8 , s(1,3),s(1,18),s(1,21), DirProp_ON }, | |
1648 | /* 9 ET */ { s(1,1), s(1,2), 4 , s(1,5), 7 ,s(1,15),s(1,17), 7 , 9 , 7 , 9 , 9 , s(1,3), 18 , 21 , DirProp_ON }, | |
1649 | /*10 EN+ES/CS */ { s(3,1), s(3,2), 4 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 10 , s(4,7), s(3,3), 18 , 21 , DirProp_EN }, | |
1650 | /*11 EN+ET */ { s(1,1), s(1,2), 4 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 11 , s(1,7), 11 , 11 , s(1,3), 18 , 21 , DirProp_EN }, | |
1651 | /*12 AN+CS */ { s(3,1), s(3,2), s(3,4), 5 , s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 12 , s(4,7), s(3,3),s(3,18),s(3,21), DirProp_AN }, | |
1652 | /*13 AL:EN/AN+CS */ { s(3,1), s(3,2), 6 , 6 , s(4,8),s(3,16),s(3,17), s(4,8), s(4,8), s(4,8), 13 , s(4,8), s(3,3), 18 , 21 , DirProp_AN }, | |
1653 | /*14 ON+ET */ { s(1,1), s(1,2), s(4,4), s(1,5), 7 ,s(1,15),s(1,17), 7 , 14 , 7 , 14 , 14 , s(1,3),s(4,18),s(4,21), DirProp_ON }, | |
1654 | /*15 S */ { s(1,1), s(1,2), s(1,4), s(1,5), s(1,7), 15 ,s(1,17), s(1,7), s(1,9), s(1,7), 15 , s(1,7), s(1,3),s(1,18),s(1,21), DirProp_S }, | |
1655 | /*16 AL:S */ { s(1,1), s(1,2), s(1,6), s(1,6), s(1,8), 16 ,s(1,17), s(1,8), s(1,8), s(1,8), 16 , s(1,8), s(1,3),s(1,18),s(1,21), DirProp_S }, | |
1656 | /*17 B */ { s(1,1), s(1,2), s(1,4), s(1,5), s(1,7),s(1,15), 17 , s(1,7), s(1,9), s(1,7), 17 , s(1,7), s(1,3),s(1,18),s(1,21), DirProp_B }, | |
1657 | /*18 ENL */ { s(1,1), s(1,2), 18 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,19), 20 ,s(2,19), 18 , 18 , s(1,3), 18 , 21 , DirProp_L }, | |
1658 | /*19 ENL+ES/CS */ { s(3,1), s(3,2), 18 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 19 , s(4,7), s(3,3), 18 , 21 , DirProp_L }, | |
1659 | /*20 ENL+ET */ { s(1,1), s(1,2), 18 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 20 , s(1,7), 20 , 20 , s(1,3), 18 , 21 , DirProp_L }, | |
1660 | /*21 ENR */ { s(1,1), s(1,2), 21 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,22), 23 ,s(2,22), 21 , 21 , s(1,3), 18 , 21 , DirProp_AN }, | |
1661 | /*22 ENR+ES/CS */ { s(3,1), s(3,2), 21 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 22 , s(4,7), s(3,3), 18 , 21 , DirProp_AN }, | |
1662 | /*23 ENR+ET */ { s(1,1), s(1,2), 21 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 23 , s(1,7), 23 , 23 , s(1,3), 18 , 21 , DirProp_AN } | |
73c04bcf A |
1663 | }; |
1664 | ||
57a6839d | 1665 | /* we must undef macro s because the levels tables have a different |
73c04bcf A |
1666 | * structure (4 bits for action and 4 bits for next state. |
1667 | */ | |
46f4442e A |
1668 | #undef s |
1669 | ||
1670 | /****************************************************************** | |
1671 | The levels state machine tables | |
1672 | ******************************************************************* | |
1673 | ||
1674 | All table cells are 8 bits: | |
1675 | bits 0..3: next state | |
1676 | bits 4..7: action to perform (if > 0) | |
1677 | ||
1678 | Cells may be of format "n" where n represents the next state | |
1679 | (except for the rightmost column). | |
1680 | Cells may also be of format "s(x,y)" where x represents an action | |
1681 | to perform and y represents the next state. | |
1682 | ||
1683 | This format limits each table to 16 states each and to 15 actions. | |
1684 | ||
1685 | ******************************************************************* | |
1686 | Definitions and type for levels state tables | |
1687 | ******************************************************************* | |
1688 | */ | |
1689 | #define IMPTABLEVELS_COLUMNS (DirProp_B + 2) | |
73c04bcf A |
1690 | #define IMPTABLEVELS_RES (IMPTABLEVELS_COLUMNS - 1) |
1691 | #define GET_STATE(cell) ((cell)&0x0f) | |
1692 | #define GET_ACTION(cell) ((cell)>>4) | |
46f4442e | 1693 | #define s(action, newState) ((uint8_t)(newState+(action<<4))) |
73c04bcf A |
1694 | |
1695 | typedef uint8_t ImpTab[][IMPTABLEVELS_COLUMNS]; | |
1696 | typedef uint8_t ImpAct[]; | |
1697 | ||
1698 | /* FOOD FOR THOUGHT: each ImpTab should have its associated ImpAct, | |
1699 | * instead of having a pair of ImpTab and a pair of ImpAct. | |
1700 | */ | |
1701 | typedef struct ImpTabPair { | |
46f4442e A |
1702 | const void * pImpTab[2]; |
1703 | const void * pImpAct[2]; | |
73c04bcf A |
1704 | } ImpTabPair; |
1705 | ||
46f4442e A |
1706 | /****************************************************************** |
1707 | ||
1708 | LEVELS STATE TABLES | |
1709 | ||
1710 | In all levels state tables, | |
1711 | - state 0 is the initial state | |
1712 | - the Res column is the increment to add to the text level | |
1713 | for this property sequence. | |
1714 | ||
1715 | The impAct arrays for each table of a pair map the local action | |
1716 | numbers of the table to the total list of actions. For instance, | |
1717 | action 2 in a given table corresponds to the action number which | |
1718 | appears in entry [2] of the impAct array for that table. | |
1719 | The first entry of all impAct arrays must be 0. | |
1720 | ||
1721 | Action 1: init conditional sequence | |
1722 | 2: prepend conditional sequence to current sequence | |
1723 | 3: set ON sequence to new level - 1 | |
1724 | 4: init EN/AN/ON sequence | |
1725 | 5: fix EN/AN/ON sequence followed by R | |
1726 | 6: set previous level sequence to level 2 | |
1727 | ||
1728 | Notes: | |
1729 | 1) These tables are used in processPropertySeq(). The input | |
1730 | is property sequences as determined by resolveImplicitLevels. | |
1731 | 2) Most such property sequences are processed immediately | |
1732 | (levels are assigned). | |
1733 | 3) However, some sequences cannot be assigned a final level till | |
1734 | one or more following sequences are received. For instance, | |
1735 | ON following an R sequence within an even-level paragraph. | |
1736 | If the following sequence is R, the ON sequence will be | |
1737 | assigned basic run level+1, and so will the R sequence. | |
1738 | 4) S is generally handled like ON, since its level will be fixed | |
1739 | to paragraph level in adjustWSLevels(). | |
1740 | ||
1741 | */ | |
73c04bcf A |
1742 | |
1743 | static const ImpTab impTabL_DEFAULT = /* Even paragraph level */ | |
57a6839d | 1744 | /* In this table, conditional sequences receive the lower possible level |
73c04bcf A |
1745 | until proven otherwise. |
1746 | */ | |
1747 | { | |
1748 | /* L , R , EN , AN , ON , S , B , Res */ | |
1749 | /* 0 : init */ { 0 , 1 , 0 , 2 , 0 , 0 , 0 , 0 }, | |
46f4442e A |
1750 | /* 1 : R */ { 0 , 1 , 3 , 3 , s(1,4), s(1,4), 0 , 1 }, |
1751 | /* 2 : AN */ { 0 , 1 , 0 , 2 , s(1,5), s(1,5), 0 , 2 }, | |
1752 | /* 3 : R+EN/AN */ { 0 , 1 , 3 , 3 , s(1,4), s(1,4), 0 , 2 }, | |
57a6839d A |
1753 | /* 4 : R+ON */ { 0 , s(2,1), s(3,3), s(3,3), 4 , 4 , 0 , 0 }, |
1754 | /* 5 : AN+ON */ { 0 , s(2,1), 0 , s(3,2), 5 , 5 , 0 , 0 } | |
73c04bcf A |
1755 | }; |
1756 | static const ImpTab impTabR_DEFAULT = /* Odd paragraph level */ | |
1757 | /* In this table, conditional sequences receive the lower possible level | |
1758 | until proven otherwise. | |
1759 | */ | |
1760 | { | |
1761 | /* L , R , EN , AN , ON , S , B , Res */ | |
1762 | /* 0 : init */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 0 }, | |
46f4442e | 1763 | /* 1 : L */ { 1 , 0 , 1 , 3 , s(1,4), s(1,4), 0 , 1 }, |
73c04bcf A |
1764 | /* 2 : EN/AN */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 1 }, |
1765 | /* 3 : L+AN */ { 1 , 0 , 1 , 3 , 5 , 5 , 0 , 1 }, | |
46f4442e | 1766 | /* 4 : L+ON */ { s(2,1), 0 , s(2,1), 3 , 4 , 4 , 0 , 0 }, |
73c04bcf A |
1767 | /* 5 : L+AN+ON */ { 1 , 0 , 1 , 3 , 5 , 5 , 0 , 0 } |
1768 | }; | |
57a6839d | 1769 | static const ImpAct impAct0 = {0,1,2,3,4}; |
46f4442e A |
1770 | static const ImpTabPair impTab_DEFAULT = {{&impTabL_DEFAULT, |
1771 | &impTabR_DEFAULT}, | |
1772 | {&impAct0, &impAct0}}; | |
73c04bcf A |
1773 | |
1774 | static const ImpTab impTabL_NUMBERS_SPECIAL = /* Even paragraph level */ | |
57a6839d | 1775 | /* In this table, conditional sequences receive the lower possible level |
73c04bcf A |
1776 | until proven otherwise. |
1777 | */ | |
1778 | { | |
1779 | /* L , R , EN , AN , ON , S , B , Res */ | |
57a6839d A |
1780 | /* 0 : init */ { 0 , 2 , s(1,1), s(1,1), 0 , 0 , 0 , 0 }, |
1781 | /* 1 : L+EN/AN */ { 0 , s(4,2), 1 , 1 , 0 , 0 , 0 , 0 }, | |
1782 | /* 2 : R */ { 0 , 2 , 4 , 4 , s(1,3), s(1,3), 0 , 1 }, | |
1783 | /* 3 : R+ON */ { 0 , s(2,2), s(3,4), s(3,4), 3 , 3 , 0 , 0 }, | |
1784 | /* 4 : R+EN/AN */ { 0 , 2 , 4 , 4 , s(1,3), s(1,3), 0 , 2 } | |
1785 | }; | |
46f4442e A |
1786 | static const ImpTabPair impTab_NUMBERS_SPECIAL = {{&impTabL_NUMBERS_SPECIAL, |
1787 | &impTabR_DEFAULT}, | |
1788 | {&impAct0, &impAct0}}; | |
73c04bcf A |
1789 | |
1790 | static const ImpTab impTabL_GROUP_NUMBERS_WITH_R = | |
1791 | /* In this table, EN/AN+ON sequences receive levels as if associated with R | |
1792 | until proven that there is L or sor/eor on both sides. AN is handled like EN. | |
1793 | */ | |
1794 | { | |
1795 | /* L , R , EN , AN , ON , S , B , Res */ | |
46f4442e A |
1796 | /* 0 init */ { 0 , 3 , s(1,1), s(1,1), 0 , 0 , 0 , 0 }, |
1797 | /* 1 EN/AN */ { s(2,0), 3 , 1 , 1 , 2 , s(2,0), s(2,0), 2 }, | |
1798 | /* 2 EN/AN+ON */ { s(2,0), 3 , 1 , 1 , 2 , s(2,0), s(2,0), 1 }, | |
1799 | /* 3 R */ { 0 , 3 , 5 , 5 , s(1,4), 0 , 0 , 1 }, | |
1800 | /* 4 R+ON */ { s(2,0), 3 , 5 , 5 , 4 , s(2,0), s(2,0), 1 }, | |
1801 | /* 5 R+EN/AN */ { 0 , 3 , 5 , 5 , s(1,4), 0 , 0 , 2 } | |
73c04bcf A |
1802 | }; |
1803 | static const ImpTab impTabR_GROUP_NUMBERS_WITH_R = | |
1804 | /* In this table, EN/AN+ON sequences receive levels as if associated with R | |
1805 | until proven that there is L on both sides. AN is handled like EN. | |
1806 | */ | |
1807 | { | |
1808 | /* L , R , EN , AN , ON , S , B , Res */ | |
1809 | /* 0 init */ { 2 , 0 , 1 , 1 , 0 , 0 , 0 , 0 }, | |
1810 | /* 1 EN/AN */ { 2 , 0 , 1 , 1 , 0 , 0 , 0 , 1 }, | |
46f4442e A |
1811 | /* 2 L */ { 2 , 0 , s(1,4), s(1,4), s(1,3), 0 , 0 , 1 }, |
1812 | /* 3 L+ON */ { s(2,2), 0 , 4 , 4 , 3 , 0 , 0 , 0 }, | |
1813 | /* 4 L+EN/AN */ { s(2,2), 0 , 4 , 4 , 3 , 0 , 0 , 1 } | |
73c04bcf A |
1814 | }; |
1815 | static const ImpTabPair impTab_GROUP_NUMBERS_WITH_R = { | |
46f4442e A |
1816 | {&impTabL_GROUP_NUMBERS_WITH_R, |
1817 | &impTabR_GROUP_NUMBERS_WITH_R}, | |
1818 | {&impAct0, &impAct0}}; | |
73c04bcf A |
1819 | |
1820 | ||
1821 | static const ImpTab impTabL_INVERSE_NUMBERS_AS_L = | |
1822 | /* This table is identical to the Default LTR table except that EN and AN are | |
1823 | handled like L. | |
1824 | */ | |
1825 | { | |
1826 | /* L , R , EN , AN , ON , S , B , Res */ | |
1827 | /* 0 : init */ { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 }, | |
46f4442e A |
1828 | /* 1 : R */ { 0 , 1 , 0 , 0 , s(1,4), s(1,4), 0 , 1 }, |
1829 | /* 2 : AN */ { 0 , 1 , 0 , 0 , s(1,5), s(1,5), 0 , 2 }, | |
1830 | /* 3 : R+EN/AN */ { 0 , 1 , 0 , 0 , s(1,4), s(1,4), 0 , 2 }, | |
1831 | /* 4 : R+ON */ { s(2,0), 1 , s(2,0), s(2,0), 4 , 4 , s(2,0), 1 }, | |
1832 | /* 5 : AN+ON */ { s(2,0), 1 , s(2,0), s(2,0), 5 , 5 , s(2,0), 1 } | |
73c04bcf A |
1833 | }; |
1834 | static const ImpTab impTabR_INVERSE_NUMBERS_AS_L = | |
1835 | /* This table is identical to the Default RTL table except that EN and AN are | |
1836 | handled like L. | |
1837 | */ | |
1838 | { | |
1839 | /* L , R , EN , AN , ON , S , B , Res */ | |
1840 | /* 0 : init */ { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 }, | |
46f4442e | 1841 | /* 1 : L */ { 1 , 0 , 1 , 1 , s(1,4), s(1,4), 0 , 1 }, |
73c04bcf A |
1842 | /* 2 : EN/AN */ { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 1 }, |
1843 | /* 3 : L+AN */ { 1 , 0 , 1 , 1 , 5 , 5 , 0 , 1 }, | |
46f4442e | 1844 | /* 4 : L+ON */ { s(2,1), 0 , s(2,1), s(2,1), 4 , 4 , 0 , 0 }, |
73c04bcf A |
1845 | /* 5 : L+AN+ON */ { 1 , 0 , 1 , 1 , 5 , 5 , 0 , 0 } |
1846 | }; | |
1847 | static const ImpTabPair impTab_INVERSE_NUMBERS_AS_L = { | |
46f4442e A |
1848 | {&impTabL_INVERSE_NUMBERS_AS_L, |
1849 | &impTabR_INVERSE_NUMBERS_AS_L}, | |
1850 | {&impAct0, &impAct0}}; | |
73c04bcf A |
1851 | |
1852 | static const ImpTab impTabR_INVERSE_LIKE_DIRECT = /* Odd paragraph level */ | |
1853 | /* In this table, conditional sequences receive the lower possible level | |
1854 | until proven otherwise. | |
1855 | */ | |
1856 | { | |
1857 | /* L , R , EN , AN , ON , S , B , Res */ | |
1858 | /* 0 : init */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 0 }, | |
46f4442e | 1859 | /* 1 : L */ { 1 , 0 , 1 , 2 , s(1,3), s(1,3), 0 , 1 }, |
73c04bcf | 1860 | /* 2 : EN/AN */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 1 }, |
46f4442e A |
1861 | /* 3 : L+ON */ { s(2,1), s(3,0), 6 , 4 , 3 , 3 , s(3,0), 0 }, |
1862 | /* 4 : L+ON+AN */ { s(2,1), s(3,0), 6 , 4 , 5 , 5 , s(3,0), 3 }, | |
1863 | /* 5 : L+AN+ON */ { s(2,1), s(3,0), 6 , 4 , 5 , 5 , s(3,0), 2 }, | |
1864 | /* 6 : L+ON+EN */ { s(2,1), s(3,0), 6 , 4 , 3 , 3 , s(3,0), 1 } | |
73c04bcf | 1865 | }; |
57a6839d | 1866 | static const ImpAct impAct1 = {0,1,13,14}; |
73c04bcf A |
1867 | /* FOOD FOR THOUGHT: in LTR table below, check case "JKL 123abc" |
1868 | */ | |
1869 | static const ImpTabPair impTab_INVERSE_LIKE_DIRECT = { | |
46f4442e A |
1870 | {&impTabL_DEFAULT, |
1871 | &impTabR_INVERSE_LIKE_DIRECT}, | |
1872 | {&impAct0, &impAct1}}; | |
73c04bcf A |
1873 | |
1874 | static const ImpTab impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS = | |
1875 | /* The case handled in this table is (visually): R EN L | |
1876 | */ | |
1877 | { | |
1878 | /* L , R , EN , AN , ON , S , B , Res */ | |
46f4442e A |
1879 | /* 0 : init */ { 0 , s(6,3), 0 , 1 , 0 , 0 , 0 , 0 }, |
1880 | /* 1 : L+AN */ { 0 , s(6,3), 0 , 1 , s(1,2), s(3,0), 0 , 4 }, | |
1881 | /* 2 : L+AN+ON */ { s(2,0), s(6,3), s(2,0), 1 , 2 , s(3,0), s(2,0), 3 }, | |
1882 | /* 3 : R */ { 0 , s(6,3), s(5,5), s(5,6), s(1,4), s(3,0), 0 , 3 }, | |
1883 | /* 4 : R+ON */ { s(3,0), s(4,3), s(5,5), s(5,6), 4 , s(3,0), s(3,0), 3 }, | |
1884 | /* 5 : R+EN */ { s(3,0), s(4,3), 5 , s(5,6), s(1,4), s(3,0), s(3,0), 4 }, | |
1885 | /* 6 : R+AN */ { s(3,0), s(4,3), s(5,5), 6 , s(1,4), s(3,0), s(3,0), 4 } | |
73c04bcf A |
1886 | }; |
1887 | static const ImpTab impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS = | |
1888 | /* The cases handled in this table are (visually): R EN L | |
1889 | R L AN L | |
1890 | */ | |
1891 | { | |
1892 | /* L , R , EN , AN , ON , S , B , Res */ | |
46f4442e A |
1893 | /* 0 : init */ { s(1,3), 0 , 1 , 1 , 0 , 0 , 0 , 0 }, |
1894 | /* 1 : R+EN/AN */ { s(2,3), 0 , 1 , 1 , 2 , s(4,0), 0 , 1 }, | |
1895 | /* 2 : R+EN/AN+ON */ { s(2,3), 0 , 1 , 1 , 2 , s(4,0), 0 , 0 }, | |
1896 | /* 3 : L */ { 3 , 0 , 3 , s(3,6), s(1,4), s(4,0), 0 , 1 }, | |
1897 | /* 4 : L+ON */ { s(5,3), s(4,0), 5 , s(3,6), 4 , s(4,0), s(4,0), 0 }, | |
1898 | /* 5 : L+ON+EN */ { s(5,3), s(4,0), 5 , s(3,6), 4 , s(4,0), s(4,0), 1 }, | |
1899 | /* 6 : L+AN */ { s(5,3), s(4,0), 6 , 6 , 4 , s(4,0), s(4,0), 3 } | |
73c04bcf | 1900 | }; |
57a6839d A |
1901 | static const ImpAct impAct2 = {0,1,2,5,6,7,8}; |
1902 | static const ImpAct impAct3 = {0,1,9,10,11,12}; | |
73c04bcf | 1903 | static const ImpTabPair impTab_INVERSE_LIKE_DIRECT_WITH_MARKS = { |
46f4442e A |
1904 | {&impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS, |
1905 | &impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS}, | |
57a6839d | 1906 | {&impAct2, &impAct3}}; |
73c04bcf A |
1907 | |
1908 | static const ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL = { | |
46f4442e A |
1909 | {&impTabL_NUMBERS_SPECIAL, |
1910 | &impTabR_INVERSE_LIKE_DIRECT}, | |
1911 | {&impAct0, &impAct1}}; | |
73c04bcf A |
1912 | |
1913 | static const ImpTab impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS = | |
1914 | /* The case handled in this table is (visually): R EN L | |
1915 | */ | |
1916 | { | |
1917 | /* L , R , EN , AN , ON , S , B , Res */ | |
46f4442e A |
1918 | /* 0 : init */ { 0 , s(6,2), 1 , 1 , 0 , 0 , 0 , 0 }, |
1919 | /* 1 : L+EN/AN */ { 0 , s(6,2), 1 , 1 , 0 , s(3,0), 0 , 4 }, | |
1920 | /* 2 : R */ { 0 , s(6,2), s(5,4), s(5,4), s(1,3), s(3,0), 0 , 3 }, | |
1921 | /* 3 : R+ON */ { s(3,0), s(4,2), s(5,4), s(5,4), 3 , s(3,0), s(3,0), 3 }, | |
1922 | /* 4 : R+EN/AN */ { s(3,0), s(4,2), 4 , 4 , s(1,3), s(3,0), s(3,0), 4 } | |
73c04bcf A |
1923 | }; |
1924 | static const ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS = { | |
46f4442e A |
1925 | {&impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS, |
1926 | &impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS}, | |
57a6839d | 1927 | {&impAct2, &impAct3}}; |
73c04bcf | 1928 | |
46f4442e | 1929 | #undef s |
73c04bcf A |
1930 | |
1931 | typedef struct { | |
46f4442e A |
1932 | const ImpTab * pImpTab; /* level table pointer */ |
1933 | const ImpAct * pImpAct; /* action map array */ | |
73c04bcf A |
1934 | int32_t startON; /* start of ON sequence */ |
1935 | int32_t startL2EN; /* start of level 2 sequence */ | |
1936 | int32_t lastStrongRTL; /* index of last found R or AL */ | |
1937 | int32_t state; /* current state */ | |
57a6839d | 1938 | int32_t runStart; /* start position of the run */ |
73c04bcf A |
1939 | UBiDiLevel runLevel; /* run level before implicit solving */ |
1940 | } LevState; | |
1941 | ||
1942 | /*------------------------------------------------------------------------*/ | |
1943 | ||
1944 | static void | |
1945 | addPoint(UBiDi *pBiDi, int32_t pos, int32_t flag) | |
1946 | /* param pos: position where to insert | |
1947 | param flag: one of LRM_BEFORE, LRM_AFTER, RLM_BEFORE, RLM_AFTER | |
1948 | */ | |
1949 | { | |
1950 | #define FIRSTALLOC 10 | |
1951 | Point point; | |
1952 | InsertPoints * pInsertPoints=&(pBiDi->insertPoints); | |
1953 | ||
1954 | if (pInsertPoints->capacity == 0) | |
1955 | { | |
1956 | pInsertPoints->points=uprv_malloc(sizeof(Point)*FIRSTALLOC); | |
1957 | if (pInsertPoints->points == NULL) | |
1958 | { | |
1959 | pInsertPoints->errorCode=U_MEMORY_ALLOCATION_ERROR; | |
1960 | return; | |
1961 | } | |
1962 | pInsertPoints->capacity=FIRSTALLOC; | |
1963 | } | |
1964 | if (pInsertPoints->size >= pInsertPoints->capacity) /* no room for new point */ | |
1965 | { | |
1966 | void * savePoints=pInsertPoints->points; | |
1967 | pInsertPoints->points=uprv_realloc(pInsertPoints->points, | |
1968 | pInsertPoints->capacity*2*sizeof(Point)); | |
1969 | if (pInsertPoints->points == NULL) | |
1970 | { | |
1971 | pInsertPoints->points=savePoints; | |
1972 | pInsertPoints->errorCode=U_MEMORY_ALLOCATION_ERROR; | |
1973 | return; | |
1974 | } | |
1975 | else pInsertPoints->capacity*=2; | |
1976 | } | |
1977 | point.pos=pos; | |
1978 | point.flag=flag; | |
1979 | pInsertPoints->points[pInsertPoints->size]=point; | |
1980 | pInsertPoints->size++; | |
1981 | #undef FIRSTALLOC | |
b75a7d8f A |
1982 | } |
1983 | ||
57a6839d A |
1984 | static void |
1985 | setLevelsOutsideIsolates(UBiDi *pBiDi, int32_t start, int32_t limit, UBiDiLevel level) | |
1986 | { | |
1987 | DirProp *dirProps=pBiDi->dirProps, dirProp; | |
2ca993e8 | 1988 | uint16_t *dirInsert = pBiDi->dirInsert; /* may be NULL */ |
57a6839d | 1989 | UBiDiLevel *levels=pBiDi->levels; |
2ca993e8 A |
1990 | int32_t dirInsertValue; |
1991 | int8_t dirInsertIndex; /* position within dirInsertValue, if any */ | |
57a6839d | 1992 | int32_t isolateCount=0, k; |
2ca993e8 A |
1993 | dirInsertValue = 0; |
1994 | dirInsertIndex = -1; /* indicate that we have not checked dirInsert yet */ | |
57a6839d | 1995 | for(k=start; k<limit; k++) { |
2ca993e8 A |
1996 | if (dirInsert != NULL && dirInsertIndex < 0) { |
1997 | dirInsertValue = dirInsert[k]; | |
1998 | } | |
1999 | if (dirInsertValue > 0) { | |
2000 | dirInsertIndex++; | |
2001 | dirProp = (DirProp)stdDirFromInsertDir[dirInsertValue & 0x000F]; | |
2002 | dirInsertValue >>= 4; | |
2003 | } else { | |
2004 | dirInsertIndex = -1; | |
2005 | dirProp=dirProps[k]; | |
2006 | } | |
57a6839d A |
2007 | if(dirProp==PDI) |
2008 | isolateCount--; | |
2009 | if(isolateCount==0) | |
2010 | levels[k]=level; | |
2011 | if(dirProp==LRI || dirProp==RLI) | |
2012 | isolateCount++; | |
2013 | } | |
2014 | } | |
2015 | ||
b75a7d8f A |
2016 | /* perform rules (Wn), (Nn), and (In) on a run of the text ------------------ */ |
2017 | ||
2018 | /* | |
2019 | * This implementation of the (Wn) rules applies all rules in one pass. | |
2020 | * In order to do so, it needs a look-ahead of typically 1 character | |
2021 | * (except for W5: sequences of ET) and keeps track of changes | |
2022 | * in a rule Wp that affect a later Wq (p<q). | |
2023 | * | |
b75a7d8f A |
2024 | * The (Nn) and (In) rules are also performed in that same single loop, |
2025 | * but effectively one iteration behind for white space. | |
2026 | * | |
2027 | * Since all implicit rules are performed in one step, it is not necessary | |
2028 | * to actually store the intermediate directional properties in dirProps[]. | |
2029 | */ | |
2030 | ||
b75a7d8f | 2031 | static void |
73c04bcf A |
2032 | processPropertySeq(UBiDi *pBiDi, LevState *pLevState, uint8_t _prop, |
2033 | int32_t start, int32_t limit) { | |
2034 | uint8_t cell, oldStateSeq, actionSeq; | |
46f4442e A |
2035 | const ImpTab * pImpTab=pLevState->pImpTab; |
2036 | const ImpAct * pImpAct=pLevState->pImpAct; | |
73c04bcf A |
2037 | UBiDiLevel * levels=pBiDi->levels; |
2038 | UBiDiLevel level, addLevel; | |
2039 | InsertPoints * pInsertPoints; | |
2040 | int32_t start0, k; | |
2041 | ||
2042 | start0=start; /* save original start position */ | |
46f4442e | 2043 | oldStateSeq=(uint8_t)pLevState->state; |
73c04bcf A |
2044 | cell=(*pImpTab)[oldStateSeq][_prop]; |
2045 | pLevState->state=GET_STATE(cell); /* isolate the new state */ | |
2046 | actionSeq=(*pImpAct)[GET_ACTION(cell)]; /* isolate the action */ | |
2047 | addLevel=(*pImpTab)[pLevState->state][IMPTABLEVELS_RES]; | |
2048 | ||
2049 | if(actionSeq) { | |
2050 | switch(actionSeq) { | |
2051 | case 1: /* init ON seq */ | |
2052 | pLevState->startON=start0; | |
b75a7d8f | 2053 | break; |
b75a7d8f | 2054 | |
73c04bcf A |
2055 | case 2: /* prepend ON seq to current seq */ |
2056 | start=pLevState->startON; | |
2057 | break; | |
b75a7d8f | 2058 | |
57a6839d A |
2059 | case 3: /* EN/AN after R+ON */ |
2060 | level=pLevState->runLevel+1; | |
2061 | setLevelsOutsideIsolates(pBiDi, pLevState->startON, start0, level); | |
2062 | break; | |
2063 | ||
2064 | case 4: /* EN/AN before R for NUMBERS_SPECIAL */ | |
2065 | level=pLevState->runLevel+2; | |
2066 | setLevelsOutsideIsolates(pBiDi, pLevState->startON, start0, level); | |
2067 | break; | |
2068 | ||
2069 | case 5: /* L or S after possible relevant EN/AN */ | |
73c04bcf A |
2070 | /* check if we had EN after R/AL */ |
2071 | if (pLevState->startL2EN >= 0) { | |
2072 | addPoint(pBiDi, pLevState->startL2EN, LRM_BEFORE); | |
b75a7d8f | 2073 | } |
73c04bcf A |
2074 | pLevState->startL2EN=-1; /* not within previous if since could also be -2 */ |
2075 | /* check if we had any relevant EN/AN after R/AL */ | |
2076 | pInsertPoints=&(pBiDi->insertPoints); | |
2077 | if ((pInsertPoints->capacity == 0) || | |
2078 | (pInsertPoints->size <= pInsertPoints->confirmed)) | |
2079 | { | |
2080 | /* nothing, just clean up */ | |
2081 | pLevState->lastStrongRTL=-1; | |
2082 | /* check if we have a pending conditional segment */ | |
2083 | level=(*pImpTab)[oldStateSeq][IMPTABLEVELS_RES]; | |
2084 | if ((level & 1) && (pLevState->startON > 0)) { /* after ON */ | |
2085 | start=pLevState->startON; /* reset to basic run level */ | |
b75a7d8f | 2086 | } |
46f4442e | 2087 | if (_prop == DirProp_S) /* add LRM before S */ |
73c04bcf A |
2088 | { |
2089 | addPoint(pBiDi, start0, LRM_BEFORE); | |
2090 | pInsertPoints->confirmed=pInsertPoints->size; | |
b75a7d8f | 2091 | } |
73c04bcf | 2092 | break; |
b75a7d8f | 2093 | } |
73c04bcf A |
2094 | /* reset previous RTL cont to level for LTR text */ |
2095 | for (k=pLevState->lastStrongRTL+1; k<start0; k++) | |
2096 | { | |
2097 | /* reset odd level, leave runLevel+2 as is */ | |
2098 | levels[k]=(levels[k] - 2) & ~1; | |
b75a7d8f | 2099 | } |
73c04bcf A |
2100 | /* mark insert points as confirmed */ |
2101 | pInsertPoints->confirmed=pInsertPoints->size; | |
2102 | pLevState->lastStrongRTL=-1; | |
46f4442e | 2103 | if (_prop == DirProp_S) /* add LRM before S */ |
73c04bcf A |
2104 | { |
2105 | addPoint(pBiDi, start0, LRM_BEFORE); | |
2106 | pInsertPoints->confirmed=pInsertPoints->size; | |
b75a7d8f | 2107 | } |
73c04bcf | 2108 | break; |
b75a7d8f | 2109 | |
57a6839d | 2110 | case 6: /* R/AL after possible relevant EN/AN */ |
73c04bcf A |
2111 | /* just clean up */ |
2112 | pInsertPoints=&(pBiDi->insertPoints); | |
2113 | if (pInsertPoints->capacity > 0) | |
2114 | /* remove all non confirmed insert points */ | |
2115 | pInsertPoints->size=pInsertPoints->confirmed; | |
2116 | pLevState->startON=-1; | |
2117 | pLevState->startL2EN=-1; | |
2118 | pLevState->lastStrongRTL=limit - 1; | |
2119 | break; | |
2120 | ||
57a6839d | 2121 | case 7: /* EN/AN after R/AL + possible cont */ |
73c04bcf | 2122 | /* check for real AN */ |
57a6839d | 2123 | if ((_prop == DirProp_AN) && (pBiDi->dirProps[start0] == AN) && |
73c04bcf A |
2124 | (pBiDi->reorderingMode!=UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL)) |
2125 | { | |
2126 | /* real AN */ | |
2127 | if (pLevState->startL2EN == -1) /* if no relevant EN already found */ | |
2128 | { | |
2129 | /* just note the righmost digit as a strong RTL */ | |
2130 | pLevState->lastStrongRTL=limit - 1; | |
2131 | break; | |
b75a7d8f | 2132 | } |
73c04bcf A |
2133 | if (pLevState->startL2EN >= 0) /* after EN, no AN */ |
2134 | { | |
2135 | addPoint(pBiDi, pLevState->startL2EN, LRM_BEFORE); | |
2136 | pLevState->startL2EN=-2; | |
2137 | } | |
2138 | /* note AN */ | |
2139 | addPoint(pBiDi, start0, LRM_BEFORE); | |
2140 | break; | |
2141 | } | |
2142 | /* if first EN/AN after R/AL */ | |
2143 | if (pLevState->startL2EN == -1) { | |
2144 | pLevState->startL2EN=start0; | |
b75a7d8f | 2145 | } |
73c04bcf | 2146 | break; |
b75a7d8f | 2147 | |
57a6839d | 2148 | case 8: /* note location of latest R/AL */ |
73c04bcf A |
2149 | pLevState->lastStrongRTL=limit - 1; |
2150 | pLevState->startON=-1; | |
b75a7d8f | 2151 | break; |
73c04bcf | 2152 | |
57a6839d | 2153 | case 9: /* L after R+ON/EN/AN */ |
73c04bcf A |
2154 | /* include possible adjacent number on the left */ |
2155 | for (k=start0-1; k>=0 && !(levels[k]&1); k--); | |
2156 | if(k>=0) { | |
2157 | addPoint(pBiDi, k, RLM_BEFORE); /* add RLM before */ | |
2158 | pInsertPoints=&(pBiDi->insertPoints); | |
2159 | pInsertPoints->confirmed=pInsertPoints->size; /* confirm it */ | |
2160 | } | |
2161 | pLevState->startON=start0; | |
b75a7d8f | 2162 | break; |
73c04bcf | 2163 | |
57a6839d | 2164 | case 10: /* AN after L */ |
73c04bcf A |
2165 | /* AN numbers between L text on both sides may be trouble. */ |
2166 | /* tentatively bracket with LRMs; will be confirmed if followed by L */ | |
2167 | addPoint(pBiDi, start0, LRM_BEFORE); /* add LRM before */ | |
2168 | addPoint(pBiDi, start0, LRM_AFTER); /* add LRM after */ | |
b75a7d8f | 2169 | break; |
b75a7d8f | 2170 | |
57a6839d | 2171 | case 11: /* R after L+ON/EN/AN */ |
73c04bcf A |
2172 | /* false alert, infirm LRMs around previous AN */ |
2173 | pInsertPoints=&(pBiDi->insertPoints); | |
2174 | pInsertPoints->size=pInsertPoints->confirmed; | |
46f4442e | 2175 | if (_prop == DirProp_S) /* add RLM before S */ |
73c04bcf A |
2176 | { |
2177 | addPoint(pBiDi, start0, RLM_BEFORE); | |
2178 | pInsertPoints->confirmed=pInsertPoints->size; | |
2179 | } | |
2180 | break; | |
b75a7d8f | 2181 | |
57a6839d | 2182 | case 12: /* L after L+ON/AN */ |
73c04bcf A |
2183 | level=pLevState->runLevel + addLevel; |
2184 | for(k=pLevState->startON; k<start0; k++) { | |
2185 | if (levels[k]<level) | |
2186 | levels[k]=level; | |
b75a7d8f | 2187 | } |
73c04bcf A |
2188 | pInsertPoints=&(pBiDi->insertPoints); |
2189 | pInsertPoints->confirmed=pInsertPoints->size; /* confirm inserts */ | |
2190 | pLevState->startON=start0; | |
2191 | break; | |
2192 | ||
57a6839d | 2193 | case 13: /* L after L+ON+EN/AN/ON */ |
73c04bcf A |
2194 | level=pLevState->runLevel; |
2195 | for(k=start0-1; k>=pLevState->startON; k--) { | |
2196 | if(levels[k]==level+3) { | |
2197 | while(levels[k]==level+3) { | |
2198 | levels[k--]-=2; | |
b75a7d8f | 2199 | } |
73c04bcf A |
2200 | while(levels[k]==level) { |
2201 | k--; | |
b75a7d8f A |
2202 | } |
2203 | } | |
73c04bcf A |
2204 | if(levels[k]==level+2) { |
2205 | levels[k]=level; | |
2206 | continue; | |
b75a7d8f | 2207 | } |
73c04bcf | 2208 | levels[k]=level+1; |
b75a7d8f | 2209 | } |
73c04bcf | 2210 | break; |
b75a7d8f | 2211 | |
57a6839d | 2212 | case 14: /* R after L+ON+EN/AN/ON */ |
73c04bcf A |
2213 | level=pLevState->runLevel+1; |
2214 | for(k=start0-1; k>=pLevState->startON; k--) { | |
2215 | if(levels[k]>level) { | |
2216 | levels[k]-=2; | |
b75a7d8f | 2217 | } |
b75a7d8f | 2218 | } |
73c04bcf | 2219 | break; |
b75a7d8f | 2220 | |
73c04bcf | 2221 | default: /* we should never get here */ |
46f4442e | 2222 | U_ASSERT(FALSE); |
73c04bcf | 2223 | break; |
b75a7d8f A |
2224 | } |
2225 | } | |
73c04bcf A |
2226 | if((addLevel) || (start < start0)) { |
2227 | level=pLevState->runLevel + addLevel; | |
57a6839d A |
2228 | if(start>=pLevState->runStart) { |
2229 | for(k=start; k<limit; k++) { | |
2230 | levels[k]=level; | |
2231 | } | |
2232 | } else { | |
2233 | setLevelsOutsideIsolates(pBiDi, start, limit, level); | |
73c04bcf A |
2234 | } |
2235 | } | |
2236 | } | |
b75a7d8f | 2237 | |
57a6839d A |
2238 | /** |
2239 | * Returns the directionality of the last strong character at the end of the prologue, if any. | |
2240 | * Requires prologue!=null. | |
2241 | */ | |
4388f060 A |
2242 | static DirProp |
2243 | lastL_R_AL(UBiDi *pBiDi) { | |
4388f060 A |
2244 | const UChar *text=pBiDi->prologue; |
2245 | int32_t length=pBiDi->proLength; | |
2246 | int32_t i; | |
2247 | UChar32 uchar; | |
2248 | DirProp dirProp; | |
2249 | for(i=length; i>0; ) { | |
2250 | /* i is decremented by U16_PREV */ | |
2251 | U16_PREV(text, 0, i, uchar); | |
2252 | dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar); | |
2253 | if(dirProp==L) { | |
2254 | return DirProp_L; | |
2255 | } | |
2256 | if(dirProp==R || dirProp==AL) { | |
2257 | return DirProp_R; | |
2258 | } | |
2259 | if(dirProp==B) { | |
2260 | return DirProp_ON; | |
2261 | } | |
2262 | } | |
2263 | return DirProp_ON; | |
2264 | } | |
2265 | ||
57a6839d A |
2266 | /** |
2267 | * Returns the directionality of the first strong character, or digit, in the epilogue, if any. | |
2268 | * Requires epilogue!=null. | |
2269 | */ | |
4388f060 A |
2270 | static DirProp |
2271 | firstL_R_AL_EN_AN(UBiDi *pBiDi) { | |
4388f060 A |
2272 | const UChar *text=pBiDi->epilogue; |
2273 | int32_t length=pBiDi->epiLength; | |
2274 | int32_t i; | |
2275 | UChar32 uchar; | |
2276 | DirProp dirProp; | |
2277 | for(i=0; i<length; ) { | |
2278 | /* i is incremented by U16_NEXT */ | |
2279 | U16_NEXT(text, i, length, uchar); | |
2280 | dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar); | |
2281 | if(dirProp==L) { | |
2282 | return DirProp_L; | |
2283 | } | |
2284 | if(dirProp==R || dirProp==AL) { | |
2285 | return DirProp_R; | |
2286 | } | |
2287 | if(dirProp==EN) { | |
2288 | return DirProp_EN; | |
2289 | } | |
2290 | if(dirProp==AN) { | |
2291 | return DirProp_AN; | |
2292 | } | |
2293 | } | |
2294 | return DirProp_ON; | |
2295 | } | |
2296 | ||
73c04bcf A |
2297 | static void |
2298 | resolveImplicitLevels(UBiDi *pBiDi, | |
2299 | int32_t start, int32_t limit, | |
2300 | DirProp sor, DirProp eor) { | |
2301 | const DirProp *dirProps=pBiDi->dirProps; | |
2ca993e8 | 2302 | uint16_t *dirInsert = pBiDi->dirInsert; /* may be NULL */ |
57a6839d | 2303 | DirProp dirProp; |
2ca993e8 A |
2304 | int32_t dirInsertValue; |
2305 | int8_t dirInsertIndex; /* position within dirInsertValue, if any */ | |
73c04bcf A |
2306 | LevState levState; |
2307 | int32_t i, start1, start2; | |
57a6839d | 2308 | uint16_t oldStateImp, stateImp, actionImp; |
73c04bcf A |
2309 | uint8_t gprop, resProp, cell; |
2310 | UBool inverseRTL; | |
2311 | DirProp nextStrongProp=R; | |
2312 | int32_t nextStrongPos=-1; | |
2313 | ||
2314 | /* check for RTL inverse BiDi mode */ | |
2315 | /* FOOD FOR THOUGHT: in case of RTL inverse BiDi, it would make sense to | |
2316 | * loop on the text characters from end to start. | |
2317 | * This would need a different properties state table (at least different | |
2318 | * actions) and different levels state tables (maybe very similar to the | |
2319 | * LTR corresponding ones. | |
2320 | */ | |
46f4442e A |
2321 | inverseRTL=(UBool) |
2322 | ((start<pBiDi->lastArabicPos) && (GET_PARALEVEL(pBiDi, start) & 1) && | |
2323 | (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT || | |
2324 | pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL)); | |
57a6839d A |
2325 | |
2326 | /* initialize for property and levels state tables */ | |
73c04bcf A |
2327 | levState.startL2EN=-1; /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */ |
2328 | levState.lastStrongRTL=-1; /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */ | |
57a6839d | 2329 | levState.runStart=start; |
73c04bcf | 2330 | levState.runLevel=pBiDi->levels[start]; |
46f4442e A |
2331 | levState.pImpTab=(const ImpTab*)((pBiDi->pImpTabPair)->pImpTab)[levState.runLevel&1]; |
2332 | levState.pImpAct=(const ImpAct*)((pBiDi->pImpTabPair)->pImpAct)[levState.runLevel&1]; | |
4388f060 A |
2333 | if(start==0 && pBiDi->proLength>0) { |
2334 | DirProp lastStrong=lastL_R_AL(pBiDi); | |
2335 | if(lastStrong!=DirProp_ON) { | |
2336 | sor=lastStrong; | |
2337 | } | |
2338 | } | |
57a6839d A |
2339 | /* The isolates[] entries contain enough information to |
2340 | resume the bidi algorithm in the same state as it was | |
2341 | when it was interrupted by an isolate sequence. */ | |
2ca993e8 A |
2342 | dirInsertValue = 0; |
2343 | if (dirInsert != NULL) { | |
2344 | dirInsertValue = dirInsert[start]; | |
2345 | while (dirInsertValue > 0) { | |
2346 | if ((dirInsertValue & 0x000F) == Insert_PDI) { | |
2347 | break; | |
2348 | } | |
2349 | dirInsertValue >>= 4; | |
2350 | } | |
2351 | } | |
2352 | if((dirProps[start]==PDI || dirInsertValue>0) && pBiDi->isolateCount >= 0) { | |
57a6839d A |
2353 | levState.startON=pBiDi->isolates[pBiDi->isolateCount].startON; |
2354 | start1=pBiDi->isolates[pBiDi->isolateCount].start1; | |
2355 | stateImp=pBiDi->isolates[pBiDi->isolateCount].stateImp; | |
2356 | levState.state=pBiDi->isolates[pBiDi->isolateCount].state; | |
2357 | pBiDi->isolateCount--; | |
73c04bcf | 2358 | } else { |
57a6839d A |
2359 | levState.startON=-1; |
2360 | start1=start; | |
2361 | if(dirProps[start]==NSM) | |
2362 | stateImp = 1 + sor; | |
2363 | else | |
2364 | stateImp=0; | |
2365 | levState.state=0; | |
2366 | processPropertySeq(pBiDi, &levState, sor, start, start); | |
73c04bcf | 2367 | } |
57a6839d | 2368 | start2=start; /* to make Java compiler happy */ |
73c04bcf A |
2369 | |
2370 | for(i=start; i<=limit; i++) { | |
2371 | if(i>=limit) { | |
57a6839d | 2372 | int32_t k; |
2ca993e8 A |
2373 | dirInsertValue = 0; |
2374 | for(k=limit-1; k>start && dirInsertValue <= 0; k--) { | |
2375 | dirProp = dirProps[k]; | |
2376 | if ((DIRPROP_FLAG(dirProp)&MASK_BN_EXPLICIT) == 0) { | |
2377 | break; | |
2378 | } | |
2379 | dirProp = ON; | |
2380 | if (dirInsert != NULL) { | |
2381 | dirInsertValue = dirInsert[k]; | |
2382 | while (dirInsertValue > 0) { | |
2383 | dirProp = (DirProp)stdDirFromInsertDir[dirInsertValue & 0x000F]; | |
2384 | if ((DIRPROP_FLAG(dirProp)&MASK_BN_EXPLICIT) == 0) { | |
2385 | break; | |
2386 | } | |
2387 | dirInsertValue >>= 4; | |
2388 | } | |
2389 | } | |
2390 | } | |
2391 | if (k == start) { | |
2392 | dirProp = dirProps[k]; | |
2393 | } | |
57a6839d A |
2394 | if(dirProp==LRI || dirProp==RLI) |
2395 | break; /* no forced closing for sequence ending with LRI/RLI */ | |
73c04bcf | 2396 | gprop=eor; |
b75a7d8f | 2397 | } else { |
73c04bcf | 2398 | DirProp prop, prop1; |
57a6839d | 2399 | prop=dirProps[i]; |
b331163b A |
2400 | if(prop==B) { |
2401 | pBiDi->isolateCount=-1; /* current isolates stack entry == none */ | |
2402 | } | |
73c04bcf A |
2403 | if(inverseRTL) { |
2404 | if(prop==AL) { | |
2405 | /* AL before EN does not make it AN */ | |
2406 | prop=R; | |
2407 | } else if(prop==EN) { | |
2408 | if(nextStrongPos<=i) { | |
2409 | /* look for next strong char (L/R/AL) */ | |
2410 | int32_t j; | |
2411 | nextStrongProp=R; /* set default */ | |
2412 | nextStrongPos=limit; | |
2413 | for(j=i+1; j<limit; j++) { | |
57a6839d | 2414 | prop1=dirProps[j]; |
73c04bcf A |
2415 | if(prop1==L || prop1==R || prop1==AL) { |
2416 | nextStrongProp=prop1; | |
2417 | nextStrongPos=j; | |
2418 | break; | |
2419 | } | |
2420 | } | |
2421 | } | |
2422 | if(nextStrongProp==AL) { | |
2423 | prop=AN; | |
2424 | } | |
b75a7d8f A |
2425 | } |
2426 | } | |
73c04bcf | 2427 | gprop=groupProp[prop]; |
b75a7d8f | 2428 | } |
73c04bcf A |
2429 | oldStateImp=stateImp; |
2430 | cell=impTabProps[oldStateImp][gprop]; | |
2431 | stateImp=GET_STATEPROPS(cell); /* isolate the new state */ | |
2432 | actionImp=GET_ACTIONPROPS(cell); /* isolate the action */ | |
2433 | if((i==limit) && (actionImp==0)) { | |
2434 | /* there is an unprocessed sequence if its property == eor */ | |
2435 | actionImp=1; /* process the last sequence */ | |
2436 | } | |
2437 | if(actionImp) { | |
2438 | resProp=impTabProps[oldStateImp][IMPTABPROPS_RES]; | |
2439 | switch(actionImp) { | |
2440 | case 1: /* process current seq1, init new seq1 */ | |
2441 | processPropertySeq(pBiDi, &levState, resProp, start1, i); | |
2442 | start1=i; | |
2443 | break; | |
2444 | case 2: /* init new seq2 */ | |
2445 | start2=i; | |
2446 | break; | |
2447 | case 3: /* process seq1, process seq2, init new seq1 */ | |
2448 | processPropertySeq(pBiDi, &levState, resProp, start1, start2); | |
46f4442e | 2449 | processPropertySeq(pBiDi, &levState, DirProp_ON, start2, i); |
73c04bcf A |
2450 | start1=i; |
2451 | break; | |
2452 | case 4: /* process seq1, set seq1=seq2, init new seq2 */ | |
2453 | processPropertySeq(pBiDi, &levState, resProp, start1, start2); | |
2454 | start1=start2; | |
2455 | start2=i; | |
2456 | break; | |
2457 | default: /* we should never get here */ | |
46f4442e | 2458 | U_ASSERT(FALSE); |
73c04bcf A |
2459 | break; |
2460 | } | |
b75a7d8f A |
2461 | } |
2462 | } | |
57a6839d | 2463 | |
73c04bcf | 2464 | /* flush possible pending sequence, e.g. ON */ |
4388f060 A |
2465 | if(limit==pBiDi->length && pBiDi->epiLength>0) { |
2466 | DirProp firstStrong=firstL_R_AL_EN_AN(pBiDi); | |
2467 | if(firstStrong!=DirProp_ON) { | |
2468 | eor=firstStrong; | |
2469 | } | |
2470 | } | |
57a6839d A |
2471 | |
2472 | /* look for the last char not a BN or LRE/RLE/LRO/RLO/PDF */ | |
2ca993e8 A |
2473 | dirInsertValue = 0; |
2474 | for(i=limit-1; i>start && dirInsertValue <= 0; i--) { | |
2475 | dirProp=dirProps[i]; | |
2476 | if ((DIRPROP_FLAG(dirProp)&MASK_BN_EXPLICIT) == 0) { | |
2477 | break; | |
2478 | } | |
2479 | dirProp = ON; | |
2480 | if (dirInsert != NULL) { | |
2481 | dirInsertValue = dirInsert[i]; | |
2482 | while (dirInsertValue > 0) { | |
2483 | dirProp = (DirProp)stdDirFromInsertDir[dirInsertValue & 0x000F]; | |
2484 | if ((DIRPROP_FLAG(dirProp)&MASK_BN_EXPLICIT) == 0) { | |
2485 | break; | |
2486 | } | |
2487 | dirInsertValue >>= 4; | |
2488 | } | |
2489 | } | |
2490 | } | |
2491 | if (i == start) { | |
2492 | dirProp=dirProps[i]; | |
2493 | } | |
57a6839d A |
2494 | if((dirProp==LRI || dirProp==RLI) && limit<pBiDi->length) { |
2495 | pBiDi->isolateCount++; | |
2496 | pBiDi->isolates[pBiDi->isolateCount].stateImp=stateImp; | |
2497 | pBiDi->isolates[pBiDi->isolateCount].state=levState.state; | |
2498 | pBiDi->isolates[pBiDi->isolateCount].start1=start1; | |
2499 | pBiDi->isolates[pBiDi->isolateCount].startON=levState.startON; | |
2500 | } | |
2501 | else | |
2502 | processPropertySeq(pBiDi, &levState, eor, limit, limit); | |
b75a7d8f A |
2503 | } |
2504 | ||
2505 | /* perform (L1) and (X9) ---------------------------------------------------- */ | |
2506 | ||
2507 | /* | |
2508 | * Reset the embedding levels for some non-graphic characters (L1). | |
2509 | * This function also sets appropriate levels for BN, and | |
2510 | * explicit embedding types that are supposed to have been removed | |
2511 | * from the paragraph in (X9). | |
2512 | */ | |
2513 | static void | |
2514 | adjustWSLevels(UBiDi *pBiDi) { | |
2515 | const DirProp *dirProps=pBiDi->dirProps; | |
2516 | UBiDiLevel *levels=pBiDi->levels; | |
2517 | int32_t i; | |
2518 | ||
2519 | if(pBiDi->flags&MASK_WS) { | |
73c04bcf | 2520 | UBool orderParagraphsLTR=pBiDi->orderParagraphsLTR; |
b75a7d8f A |
2521 | Flags flag; |
2522 | ||
2523 | i=pBiDi->trailingWSStart; | |
2524 | while(i>0) { | |
2525 | /* reset a sequence of WS/BN before eop and B/S to the paragraph paraLevel */ | |
57a6839d | 2526 | while(i>0 && (flag=DIRPROP_FLAG(dirProps[--i]))&MASK_WS) { |
73c04bcf A |
2527 | if(orderParagraphsLTR&&(flag&DIRPROP_FLAG(B))) { |
2528 | levels[i]=0; | |
2529 | } else { | |
2530 | levels[i]=GET_PARALEVEL(pBiDi, i); | |
2531 | } | |
b75a7d8f A |
2532 | } |
2533 | ||
2534 | /* reset BN to the next character's paraLevel until B/S, which restarts above loop */ | |
2535 | /* here, i+1 is guaranteed to be <length */ | |
2536 | while(i>0) { | |
57a6839d | 2537 | flag=DIRPROP_FLAG(dirProps[--i]); |
b75a7d8f A |
2538 | if(flag&MASK_BN_EXPLICIT) { |
2539 | levels[i]=levels[i+1]; | |
73c04bcf A |
2540 | } else if(orderParagraphsLTR&&(flag&DIRPROP_FLAG(B))) { |
2541 | levels[i]=0; | |
2542 | break; | |
b75a7d8f | 2543 | } else if(flag&MASK_B_S) { |
73c04bcf | 2544 | levels[i]=GET_PARALEVEL(pBiDi, i); |
b75a7d8f A |
2545 | break; |
2546 | } | |
2547 | } | |
2548 | } | |
2549 | } | |
2550 | } | |
2551 | ||
51004dcb | 2552 | U_CAPI void U_EXPORT2 |
4388f060 A |
2553 | ubidi_setContext(UBiDi *pBiDi, |
2554 | const UChar *prologue, int32_t proLength, | |
2555 | const UChar *epilogue, int32_t epiLength, | |
2556 | UErrorCode *pErrorCode) { | |
2557 | /* check the argument values */ | |
2558 | RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); | |
2559 | if(pBiDi==NULL || proLength<-1 || epiLength<-1 || | |
2560 | (prologue==NULL && proLength!=0) || (epilogue==NULL && epiLength!=0)) { | |
2561 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
2562 | return; | |
2563 | } | |
2564 | ||
2565 | if(proLength==-1) { | |
2566 | pBiDi->proLength=u_strlen(prologue); | |
2567 | } else { | |
2568 | pBiDi->proLength=proLength; | |
2569 | } | |
2570 | if(epiLength==-1) { | |
2571 | pBiDi->epiLength=u_strlen(epilogue); | |
2572 | } else { | |
2573 | pBiDi->epiLength=epiLength; | |
2574 | } | |
2575 | pBiDi->prologue=prologue; | |
2576 | pBiDi->epilogue=epilogue; | |
2577 | } | |
2578 | ||
2579 | static void | |
2580 | setParaSuccess(UBiDi *pBiDi) { | |
2581 | pBiDi->proLength=0; /* forget the last context */ | |
2582 | pBiDi->epiLength=0; | |
2583 | pBiDi->pParaBiDi=pBiDi; /* mark successful setPara */ | |
2584 | } | |
2585 | ||
73c04bcf A |
2586 | #define BIDI_MIN(x, y) ((x)<(y) ? (x) : (y)) |
2587 | #define BIDI_ABS(x) ((x)>=0 ? (x) : (-(x))) | |
57a6839d | 2588 | |
73c04bcf A |
2589 | static void |
2590 | setParaRunsOnly(UBiDi *pBiDi, const UChar *text, int32_t length, | |
2591 | UBiDiLevel paraLevel, UErrorCode *pErrorCode) { | |
b331163b | 2592 | void *runsOnlyMemory = NULL; |
73c04bcf A |
2593 | int32_t *visualMap; |
2594 | UChar *visualText; | |
46f4442e | 2595 | int32_t saveLength, saveTrailingWSStart; |
73c04bcf A |
2596 | const UBiDiLevel *levels; |
2597 | UBiDiLevel *saveLevels; | |
46f4442e A |
2598 | UBiDiDirection saveDirection; |
2599 | UBool saveMayAllocateText; | |
73c04bcf A |
2600 | Run *runs; |
2601 | int32_t visualLength, i, j, visualStart, logicalStart, | |
2602 | runCount, runLength, addedRuns, insertRemove, | |
2603 | start, limit, step, indexOddBit, logicalPos, | |
729e4ab9 | 2604 | index0, index1; |
73c04bcf A |
2605 | uint32_t saveOptions; |
2606 | ||
2607 | pBiDi->reorderingMode=UBIDI_REORDER_DEFAULT; | |
2608 | if(length==0) { | |
2609 | ubidi_setPara(pBiDi, text, length, paraLevel, NULL, pErrorCode); | |
2610 | goto cleanup3; | |
2611 | } | |
2612 | /* obtain memory for mapping table and visual text */ | |
2613 | runsOnlyMemory=uprv_malloc(length*(sizeof(int32_t)+sizeof(UChar)+sizeof(UBiDiLevel))); | |
2614 | if(runsOnlyMemory==NULL) { | |
2615 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
2616 | goto cleanup3; | |
2617 | } | |
2618 | visualMap=runsOnlyMemory; | |
2619 | visualText=(UChar *)&visualMap[length]; | |
2620 | saveLevels=(UBiDiLevel *)&visualText[length]; | |
2621 | saveOptions=pBiDi->reorderingOptions; | |
2622 | if(saveOptions & UBIDI_OPTION_INSERT_MARKS) { | |
2623 | pBiDi->reorderingOptions&=~UBIDI_OPTION_INSERT_MARKS; | |
2624 | pBiDi->reorderingOptions|=UBIDI_OPTION_REMOVE_CONTROLS; | |
2625 | } | |
46f4442e | 2626 | paraLevel&=1; /* accept only 0 or 1 */ |
73c04bcf | 2627 | ubidi_setPara(pBiDi, text, length, paraLevel, NULL, pErrorCode); |
46f4442e A |
2628 | if(U_FAILURE(*pErrorCode)) { |
2629 | goto cleanup3; | |
2630 | } | |
2631 | /* we cannot access directly pBiDi->levels since it is not yet set if | |
2632 | * direction is not MIXED | |
2633 | */ | |
73c04bcf | 2634 | levels=ubidi_getLevels(pBiDi, pErrorCode); |
a62d09fc | 2635 | uprv_memcpy(saveLevels, levels, (size_t)pBiDi->length*sizeof(UBiDiLevel)); |
46f4442e A |
2636 | saveTrailingWSStart=pBiDi->trailingWSStart; |
2637 | saveLength=pBiDi->length; | |
2638 | saveDirection=pBiDi->direction; | |
73c04bcf A |
2639 | |
2640 | /* FOOD FOR THOUGHT: instead of writing the visual text, we could use | |
2641 | * the visual map and the dirProps array to drive the second call | |
2642 | * to ubidi_setPara (but must make provision for possible removal of | |
2643 | * BiDi controls. Alternatively, only use the dirProps array via | |
2644 | * customized classifier callback. | |
2645 | */ | |
2646 | visualLength=ubidi_writeReordered(pBiDi, visualText, length, | |
2647 | UBIDI_DO_MIRRORING, pErrorCode); | |
73c04bcf A |
2648 | ubidi_getVisualMap(pBiDi, visualMap, pErrorCode); |
2649 | if(U_FAILURE(*pErrorCode)) { | |
2650 | goto cleanup2; | |
2651 | } | |
46f4442e | 2652 | pBiDi->reorderingOptions=saveOptions; |
73c04bcf A |
2653 | |
2654 | pBiDi->reorderingMode=UBIDI_REORDER_INVERSE_LIKE_DIRECT; | |
46f4442e A |
2655 | paraLevel^=1; |
2656 | /* Because what we did with reorderingOptions, visualText may be shorter | |
2657 | * than the original text. But we don't want the levels memory to be | |
2658 | * reallocated shorter than the original length, since we need to restore | |
2659 | * the levels as after the first call to ubidi_setpara() before returning. | |
2660 | * We will force mayAllocateText to FALSE before the second call to | |
2661 | * ubidi_setpara(), and will restore it afterwards. | |
2662 | */ | |
2663 | saveMayAllocateText=pBiDi->mayAllocateText; | |
2664 | pBiDi->mayAllocateText=FALSE; | |
73c04bcf | 2665 | ubidi_setPara(pBiDi, visualText, visualLength, paraLevel, NULL, pErrorCode); |
46f4442e A |
2666 | pBiDi->mayAllocateText=saveMayAllocateText; |
2667 | ubidi_getRuns(pBiDi, pErrorCode); | |
73c04bcf A |
2668 | if(U_FAILURE(*pErrorCode)) { |
2669 | goto cleanup1; | |
2670 | } | |
73c04bcf A |
2671 | /* check if some runs must be split, count how many splits */ |
2672 | addedRuns=0; | |
2673 | runCount=pBiDi->runCount; | |
2674 | runs=pBiDi->runs; | |
2675 | visualStart=0; | |
2676 | for(i=0; i<runCount; i++, visualStart+=runLength) { | |
2677 | runLength=runs[i].visualLimit-visualStart; | |
2678 | if(runLength<2) { | |
2679 | continue; | |
2680 | } | |
2681 | logicalStart=GET_INDEX(runs[i].logicalStart); | |
2682 | for(j=logicalStart+1; j<logicalStart+runLength; j++) { | |
729e4ab9 | 2683 | index0=visualMap[j]; |
73c04bcf | 2684 | index1=visualMap[j-1]; |
729e4ab9 | 2685 | if((BIDI_ABS(index0-index1)!=1) || (saveLevels[index0]!=saveLevels[index1])) { |
73c04bcf A |
2686 | addedRuns++; |
2687 | } | |
2688 | } | |
2689 | } | |
2690 | if(addedRuns) { | |
2691 | if(getRunsMemory(pBiDi, runCount+addedRuns)) { | |
2692 | if(runCount==1) { | |
2693 | /* because we switch from UBiDi.simpleRuns to UBiDi.runs */ | |
2694 | pBiDi->runsMemory[0]=runs[0]; | |
2695 | } | |
2696 | runs=pBiDi->runs=pBiDi->runsMemory; | |
2697 | pBiDi->runCount+=addedRuns; | |
2698 | } else { | |
2699 | goto cleanup1; | |
2700 | } | |
2701 | } | |
2702 | /* split runs which are not consecutive in source text */ | |
2703 | for(i=runCount-1; i>=0; i--) { | |
2704 | runLength= i==0 ? runs[0].visualLimit : | |
2705 | runs[i].visualLimit-runs[i-1].visualLimit; | |
2706 | logicalStart=runs[i].logicalStart; | |
2707 | indexOddBit=GET_ODD_BIT(logicalStart); | |
2708 | logicalStart=GET_INDEX(logicalStart); | |
2709 | if(runLength<2) { | |
2710 | if(addedRuns) { | |
2711 | runs[i+addedRuns]=runs[i]; | |
2712 | } | |
2713 | logicalPos=visualMap[logicalStart]; | |
2714 | runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos, | |
2715 | saveLevels[logicalPos]^indexOddBit); | |
2716 | continue; | |
2717 | } | |
2718 | if(indexOddBit) { | |
2719 | start=logicalStart; | |
2720 | limit=logicalStart+runLength-1; | |
2721 | step=1; | |
2722 | } else { | |
2723 | start=logicalStart+runLength-1; | |
2724 | limit=logicalStart; | |
2725 | step=-1; | |
2726 | } | |
2727 | for(j=start; j!=limit; j+=step) { | |
729e4ab9 | 2728 | index0=visualMap[j]; |
73c04bcf | 2729 | index1=visualMap[j+step]; |
729e4ab9 A |
2730 | if((BIDI_ABS(index0-index1)!=1) || (saveLevels[index0]!=saveLevels[index1])) { |
2731 | logicalPos=BIDI_MIN(visualMap[start], index0); | |
73c04bcf A |
2732 | runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos, |
2733 | saveLevels[logicalPos]^indexOddBit); | |
2734 | runs[i+addedRuns].visualLimit=runs[i].visualLimit; | |
2735 | runs[i].visualLimit-=BIDI_ABS(j-start)+1; | |
2736 | insertRemove=runs[i].insertRemove&(LRM_AFTER|RLM_AFTER); | |
2737 | runs[i+addedRuns].insertRemove=insertRemove; | |
2738 | runs[i].insertRemove&=~insertRemove; | |
2739 | start=j+step; | |
2740 | addedRuns--; | |
2741 | } | |
2742 | } | |
2743 | if(addedRuns) { | |
2744 | runs[i+addedRuns]=runs[i]; | |
2745 | } | |
2746 | logicalPos=BIDI_MIN(visualMap[start], visualMap[limit]); | |
2747 | runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos, | |
2748 | saveLevels[logicalPos]^indexOddBit); | |
2749 | } | |
2750 | ||
2751 | cleanup1: | |
2752 | /* restore initial paraLevel */ | |
2753 | pBiDi->paraLevel^=1; | |
2754 | cleanup2: | |
2755 | /* restore real text */ | |
2756 | pBiDi->text=text; | |
46f4442e A |
2757 | pBiDi->length=saveLength; |
2758 | pBiDi->originalLength=length; | |
2759 | pBiDi->direction=saveDirection; | |
2760 | /* the saved levels should never excess levelsSize, but we check anyway */ | |
2761 | if(saveLength>pBiDi->levelsSize) { | |
2762 | saveLength=pBiDi->levelsSize; | |
2763 | } | |
a62d09fc | 2764 | uprv_memcpy(pBiDi->levels, saveLevels, (size_t)saveLength*sizeof(UBiDiLevel)); |
46f4442e | 2765 | pBiDi->trailingWSStart=saveTrailingWSStart; |
46f4442e A |
2766 | if(pBiDi->runCount>1) { |
2767 | pBiDi->direction=UBIDI_MIXED; | |
2768 | } | |
73c04bcf | 2769 | cleanup3: |
b331163b A |
2770 | /* free memory for mapping table and visual text */ |
2771 | uprv_free(runsOnlyMemory); | |
2772 | ||
73c04bcf A |
2773 | pBiDi->reorderingMode=UBIDI_REORDER_RUNS_ONLY; |
2774 | } | |
2775 | ||
2ca993e8 A |
2776 | /* -------------------------------------------------------------------------- */ |
2777 | /* internal proptotype */ | |
2778 | ||
2779 | static void | |
2780 | ubidi_setParaInternal(UBiDi *pBiDi, | |
2781 | const UChar *text, int32_t length, | |
2782 | UBiDiLevel paraLevel, | |
2783 | UBiDiLevel *embeddingLevels, | |
2784 | const int32_t *offsets, int32_t offsetCount, | |
2785 | const int32_t *controlStringIndices, | |
2786 | const UChar * const * controlStrings, | |
2787 | UErrorCode *pErrorCode); | |
2788 | ||
374ca955 A |
2789 | /* ubidi_setPara ------------------------------------------------------------ */ |
2790 | ||
2791 | U_CAPI void U_EXPORT2 | |
2792 | ubidi_setPara(UBiDi *pBiDi, const UChar *text, int32_t length, | |
2793 | UBiDiLevel paraLevel, UBiDiLevel *embeddingLevels, | |
2794 | UErrorCode *pErrorCode) { | |
2ca993e8 A |
2795 | RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); |
2796 | ubidi_setParaInternal(pBiDi, text, length, paraLevel, | |
2797 | embeddingLevels, | |
2798 | NULL, 0, NULL, NULL, | |
2799 | pErrorCode); | |
2800 | } | |
2801 | ||
2802 | /* ubidi_setParaWithControls ------------------------------------------------ */ | |
2803 | ||
2804 | U_CAPI void U_EXPORT2 | |
2805 | ubidi_setParaWithControls(UBiDi *pBiDi, | |
2806 | const UChar *text, int32_t length, | |
2807 | UBiDiLevel paraLevel, | |
2808 | const int32_t *offsets, int32_t offsetCount, | |
2809 | const int32_t *controlStringIndices, | |
2810 | const UChar * const * controlStrings, | |
2811 | UErrorCode *pErrorCode) { | |
2812 | RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); | |
2813 | /* check the argument values that are not already checked in ubidi_setParaInternal */ | |
2814 | if ( offsetCount < 0 || (offsetCount > 0 && (offsets == NULL || controlStrings == NULL)) ) { | |
2815 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
2816 | return; | |
2817 | } | |
2818 | ubidi_setParaInternal(pBiDi, text, length, paraLevel, | |
2819 | NULL, | |
2820 | offsets, offsetCount, controlStringIndices, controlStrings, | |
2821 | pErrorCode); | |
2822 | } | |
2823 | ||
2824 | /* ubidi_setParaInternal ---------------------------------------------------- */ | |
2825 | ||
2826 | void | |
2827 | ubidi_setParaInternal(UBiDi *pBiDi, | |
2828 | const UChar *text, int32_t length, | |
2829 | UBiDiLevel paraLevel, | |
2830 | UBiDiLevel *embeddingLevels, | |
2831 | const int32_t *offsets, int32_t offsetCount, | |
2832 | const int32_t *controlStringIndices, | |
2833 | const UChar * const * controlStrings, | |
2834 | UErrorCode *pErrorCode) { | |
374ca955 | 2835 | UBiDiDirection direction; |
57a6839d | 2836 | DirProp *dirProps; |
374ca955 | 2837 | |
2ca993e8 | 2838 | /* check the argument values (pErrorCode status alrecy checked before getting here) */ |
46f4442e A |
2839 | if(pBiDi==NULL || text==NULL || length<-1 || |
2840 | (paraLevel>UBIDI_MAX_EXPLICIT_LEVEL && paraLevel<UBIDI_DEFAULT_LTR)) { | |
374ca955 A |
2841 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
2842 | return; | |
2843 | } | |
2844 | ||
2845 | if(length==-1) { | |
2846 | length=u_strlen(text); | |
2847 | } | |
2ca993e8 A |
2848 | if (offsetCount > 0 && pBiDi->reorderingMode > UBIDI_REORDER_GROUP_NUMBERS_WITH_R) { |
2849 | offsetCount = 0; | |
2850 | } | |
374ca955 | 2851 | |
73c04bcf A |
2852 | /* special treatment for RUNS_ONLY mode */ |
2853 | if(pBiDi->reorderingMode==UBIDI_REORDER_RUNS_ONLY) { | |
2854 | setParaRunsOnly(pBiDi, text, length, paraLevel, pErrorCode); | |
2855 | return; | |
2856 | } | |
2857 | ||
374ca955 | 2858 | /* initialize the UBiDi structure */ |
73c04bcf | 2859 | pBiDi->pParaBiDi=NULL; /* mark unfinished setPara */ |
374ca955 | 2860 | pBiDi->text=text; |
73c04bcf | 2861 | pBiDi->length=pBiDi->originalLength=pBiDi->resultLength=length; |
374ca955 | 2862 | pBiDi->paraLevel=paraLevel; |
57a6839d | 2863 | pBiDi->direction=paraLevel&1; |
73c04bcf | 2864 | pBiDi->paraCount=1; |
374ca955 | 2865 | |
2ca993e8 | 2866 | pBiDi->dirInsert=NULL; |
374ca955 A |
2867 | pBiDi->dirProps=NULL; |
2868 | pBiDi->levels=NULL; | |
2869 | pBiDi->runs=NULL; | |
73c04bcf A |
2870 | pBiDi->insertPoints.size=0; /* clean up from last call */ |
2871 | pBiDi->insertPoints.confirmed=0; /* clean up from last call */ | |
2872 | ||
2873 | /* | |
2874 | * Save the original paraLevel if contextual; otherwise, set to 0. | |
2875 | */ | |
57a6839d | 2876 | pBiDi->defaultParaLevel=IS_DEFAULT_LEVEL(paraLevel); |
374ca955 A |
2877 | |
2878 | if(length==0) { | |
2879 | /* | |
2880 | * For an empty paragraph, create a UBiDi object with the paraLevel and | |
2881 | * the flags and the direction set but without allocating zero-length arrays. | |
2882 | * There is nothing more to do. | |
2883 | */ | |
2884 | if(IS_DEFAULT_LEVEL(paraLevel)) { | |
2885 | pBiDi->paraLevel&=1; | |
73c04bcf | 2886 | pBiDi->defaultParaLevel=0; |
374ca955 | 2887 | } |
57a6839d | 2888 | pBiDi->flags=DIRPROP_FLAG_LR(paraLevel); |
374ca955 | 2889 | pBiDi->runCount=0; |
46f4442e | 2890 | pBiDi->paraCount=0; |
4388f060 | 2891 | setParaSuccess(pBiDi); /* mark successful setPara */ |
374ca955 A |
2892 | return; |
2893 | } | |
2894 | ||
2895 | pBiDi->runCount=-1; | |
2896 | ||
57a6839d A |
2897 | /* allocate paras memory */ |
2898 | if(pBiDi->parasMemory) | |
2899 | pBiDi->paras=pBiDi->parasMemory; | |
2900 | else | |
2901 | pBiDi->paras=pBiDi->simpleParas; | |
2902 | ||
2ca993e8 A |
2903 | /* |
2904 | * Get the inserted directional properties | |
2905 | * if necessary. | |
2906 | */ | |
2907 | if (offsetCount > 0) { | |
2908 | if(getDirInsertMemory(pBiDi, length)) { | |
2909 | pBiDi->dirInsert=pBiDi->dirInsertMemory; | |
2910 | if(!getDirInsert(pBiDi, offsets, offsetCount, controlStringIndices, controlStrings)) { | |
2911 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
2912 | return; | |
2913 | } | |
2914 | } else { | |
2915 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
2916 | return; | |
2917 | } | |
2918 | } | |
2919 | ||
374ca955 A |
2920 | /* |
2921 | * Get the directional properties, | |
2922 | * the flags bit-set, and | |
73c04bcf | 2923 | * determine the paragraph level if necessary. |
374ca955 A |
2924 | */ |
2925 | if(getDirPropsMemory(pBiDi, length)) { | |
2926 | pBiDi->dirProps=pBiDi->dirPropsMemory; | |
57a6839d A |
2927 | if(!getDirProps(pBiDi)) { |
2928 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
2929 | return; | |
2930 | } | |
374ca955 A |
2931 | } else { |
2932 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
2933 | return; | |
2934 | } | |
57a6839d | 2935 | dirProps=pBiDi->dirProps; |
73c04bcf A |
2936 | /* the processed length may have changed if UBIDI_OPTION_STREAMING */ |
2937 | length= pBiDi->length; | |
2938 | pBiDi->trailingWSStart=length; /* the levels[] will reflect the WS run */ | |
374ca955 A |
2939 | |
2940 | /* are explicit levels specified? */ | |
2941 | if(embeddingLevels==NULL) { | |
2942 | /* no: determine explicit levels according to the (Xn) rules */\ | |
2943 | if(getLevelsMemory(pBiDi, length)) { | |
2944 | pBiDi->levels=pBiDi->levelsMemory; | |
57a6839d A |
2945 | direction=resolveExplicitLevels(pBiDi, pErrorCode); |
2946 | if(U_FAILURE(*pErrorCode)) { | |
2947 | return; | |
2948 | } | |
374ca955 A |
2949 | } else { |
2950 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
2951 | return; | |
2952 | } | |
2953 | } else { | |
73c04bcf | 2954 | /* set BN for all explicit codes, check that all levels are 0 or paraLevel..UBIDI_MAX_EXPLICIT_LEVEL */ |
374ca955 A |
2955 | pBiDi->levels=embeddingLevels; |
2956 | direction=checkExplicitLevels(pBiDi, pErrorCode); | |
2957 | if(U_FAILURE(*pErrorCode)) { | |
2958 | return; | |
2959 | } | |
2960 | } | |
2961 | ||
57a6839d | 2962 | /* allocate isolate memory */ |
b331163b | 2963 | if(pBiDi->isolateCount<=SIMPLE_ISOLATES_COUNT) |
57a6839d A |
2964 | pBiDi->isolates=pBiDi->simpleIsolates; |
2965 | else | |
2966 | if((int32_t)(pBiDi->isolateCount*sizeof(Isolate))<=pBiDi->isolatesSize) | |
2967 | pBiDi->isolates=pBiDi->isolatesMemory; | |
2968 | else { | |
2969 | if(getInitialIsolatesMemory(pBiDi, pBiDi->isolateCount)) { | |
2970 | pBiDi->isolates=pBiDi->isolatesMemory; | |
2971 | } else { | |
2972 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
2973 | return; | |
2974 | } | |
2975 | } | |
2976 | pBiDi->isolateCount=-1; /* current isolates stack entry == none */ | |
2977 | ||
374ca955 A |
2978 | /* |
2979 | * The steps after (X9) in the UBiDi algorithm are performed only if | |
2980 | * the paragraph text has mixed directionality! | |
2981 | */ | |
2982 | pBiDi->direction=direction; | |
2983 | switch(direction) { | |
2984 | case UBIDI_LTR: | |
374ca955 A |
2985 | /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */ |
2986 | pBiDi->trailingWSStart=0; | |
2987 | break; | |
2988 | case UBIDI_RTL: | |
374ca955 A |
2989 | /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */ |
2990 | pBiDi->trailingWSStart=0; | |
2991 | break; | |
2992 | default: | |
73c04bcf A |
2993 | /* |
2994 | * Choose the right implicit state table | |
2995 | */ | |
2996 | switch(pBiDi->reorderingMode) { | |
2997 | case UBIDI_REORDER_DEFAULT: | |
2998 | pBiDi->pImpTabPair=&impTab_DEFAULT; | |
2999 | break; | |
3000 | case UBIDI_REORDER_NUMBERS_SPECIAL: | |
3001 | pBiDi->pImpTabPair=&impTab_NUMBERS_SPECIAL; | |
3002 | break; | |
3003 | case UBIDI_REORDER_GROUP_NUMBERS_WITH_R: | |
3004 | pBiDi->pImpTabPair=&impTab_GROUP_NUMBERS_WITH_R; | |
3005 | break; | |
73c04bcf A |
3006 | case UBIDI_REORDER_INVERSE_NUMBERS_AS_L: |
3007 | pBiDi->pImpTabPair=&impTab_INVERSE_NUMBERS_AS_L; | |
3008 | break; | |
3009 | case UBIDI_REORDER_INVERSE_LIKE_DIRECT: | |
3010 | if (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) { | |
3011 | pBiDi->pImpTabPair=&impTab_INVERSE_LIKE_DIRECT_WITH_MARKS; | |
3012 | } else { | |
3013 | pBiDi->pImpTabPair=&impTab_INVERSE_LIKE_DIRECT; | |
3014 | } | |
3015 | break; | |
3016 | case UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL: | |
3017 | if (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) { | |
3018 | pBiDi->pImpTabPair=&impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS; | |
3019 | } else { | |
3020 | pBiDi->pImpTabPair=&impTab_INVERSE_FOR_NUMBERS_SPECIAL; | |
3021 | } | |
3022 | break; | |
3023 | default: | |
46f4442e A |
3024 | /* we should never get here */ |
3025 | U_ASSERT(FALSE); | |
73c04bcf A |
3026 | break; |
3027 | } | |
374ca955 A |
3028 | /* |
3029 | * If there are no external levels specified and there | |
3030 | * are no significant explicit level codes in the text, | |
3031 | * then we can treat the entire paragraph as one run. | |
3032 | * Otherwise, we need to perform the following rules on runs of | |
3033 | * the text with the same embedding levels. (X10) | |
3034 | * "Significant" explicit level codes are ones that actually | |
3035 | * affect non-BN characters. | |
3036 | * Examples for "insignificant" ones are empty embeddings | |
3037 | * LRE-PDF, LRE-RLE-PDF-PDF, etc. | |
3038 | */ | |
46f4442e A |
3039 | if(embeddingLevels==NULL && pBiDi->paraCount<=1 && |
3040 | !(pBiDi->flags&DIRPROP_FLAG_MULTI_RUNS)) { | |
374ca955 | 3041 | resolveImplicitLevels(pBiDi, 0, length, |
73c04bcf A |
3042 | GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, 0)), |
3043 | GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, length-1))); | |
374ca955 A |
3044 | } else { |
3045 | /* sor, eor: start and end types of same-level-run */ | |
3046 | UBiDiLevel *levels=pBiDi->levels; | |
3047 | int32_t start, limit=0; | |
3048 | UBiDiLevel level, nextLevel; | |
3049 | DirProp sor, eor; | |
3050 | ||
3051 | /* determine the first sor and set eor to it because of the loop body (sor=eor there) */ | |
73c04bcf | 3052 | level=GET_PARALEVEL(pBiDi, 0); |
374ca955 A |
3053 | nextLevel=levels[0]; |
3054 | if(level<nextLevel) { | |
3055 | eor=GET_LR_FROM_LEVEL(nextLevel); | |
3056 | } else { | |
3057 | eor=GET_LR_FROM_LEVEL(level); | |
3058 | } | |
3059 | ||
3060 | do { | |
3061 | /* determine start and limit of the run (end points just behind the run) */ | |
3062 | ||
3063 | /* the values for this run's start are the same as for the previous run's end */ | |
374ca955 A |
3064 | start=limit; |
3065 | level=nextLevel; | |
57a6839d | 3066 | if((start>0) && (dirProps[start-1]==B)) { |
73c04bcf A |
3067 | /* except if this is a new paragraph, then set sor = para level */ |
3068 | sor=GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, start)); | |
3069 | } else { | |
3070 | sor=eor; | |
3071 | } | |
374ca955 A |
3072 | |
3073 | /* search for the limit of this run */ | |
57a6839d A |
3074 | while((++limit<length) && |
3075 | ((levels[limit]==level) || | |
3076 | (DIRPROP_FLAG(dirProps[limit])&MASK_BN_EXPLICIT))) {} | |
374ca955 A |
3077 | |
3078 | /* get the correct level of the next run */ | |
3079 | if(limit<length) { | |
3080 | nextLevel=levels[limit]; | |
3081 | } else { | |
73c04bcf | 3082 | nextLevel=GET_PARALEVEL(pBiDi, length-1); |
374ca955 A |
3083 | } |
3084 | ||
3085 | /* determine eor from max(level, nextLevel); sor is last run's eor */ | |
57a6839d | 3086 | if(NO_OVERRIDE(level)<NO_OVERRIDE(nextLevel)) { |
374ca955 A |
3087 | eor=GET_LR_FROM_LEVEL(nextLevel); |
3088 | } else { | |
3089 | eor=GET_LR_FROM_LEVEL(level); | |
3090 | } | |
3091 | ||
3092 | /* if the run consists of overridden directional types, then there | |
3093 | are no implicit types to be resolved */ | |
3094 | if(!(level&UBIDI_LEVEL_OVERRIDE)) { | |
3095 | resolveImplicitLevels(pBiDi, start, limit, sor, eor); | |
3096 | } else { | |
3097 | /* remove the UBIDI_LEVEL_OVERRIDE flags */ | |
3098 | do { | |
3099 | levels[start++]&=~UBIDI_LEVEL_OVERRIDE; | |
3100 | } while(start<limit); | |
3101 | } | |
3102 | } while(limit<length); | |
3103 | } | |
73c04bcf A |
3104 | /* check if we got any memory shortage while adding insert points */ |
3105 | if (U_FAILURE(pBiDi->insertPoints.errorCode)) | |
3106 | { | |
3107 | *pErrorCode=pBiDi->insertPoints.errorCode; | |
3108 | return; | |
3109 | } | |
374ca955 A |
3110 | /* reset the embedding levels for some non-graphic characters (L1), (X9) */ |
3111 | adjustWSLevels(pBiDi); | |
374ca955 A |
3112 | break; |
3113 | } | |
46f4442e A |
3114 | /* add RLM for inverse Bidi with contextual orientation resolving |
3115 | * to RTL which would not round-trip otherwise | |
3116 | */ | |
3117 | if((pBiDi->defaultParaLevel>0) && | |
3118 | (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) && | |
3119 | ((pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT) || | |
3120 | (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL))) { | |
3121 | int32_t i, j, start, last; | |
57a6839d | 3122 | UBiDiLevel level; |
46f4442e A |
3123 | DirProp dirProp; |
3124 | for(i=0; i<pBiDi->paraCount; i++) { | |
57a6839d A |
3125 | last=(pBiDi->paras[i].limit)-1; |
3126 | level=pBiDi->paras[i].level; | |
3127 | if(level==0) | |
46f4442e | 3128 | continue; /* LTR paragraph */ |
57a6839d | 3129 | start= i==0 ? 0 : pBiDi->paras[i-1].limit; |
46f4442e | 3130 | for(j=last; j>=start; j--) { |
57a6839d | 3131 | dirProp=dirProps[j]; |
46f4442e A |
3132 | if(dirProp==L) { |
3133 | if(j<last) { | |
57a6839d | 3134 | while(dirProps[last]==B) { |
46f4442e A |
3135 | last--; |
3136 | } | |
3137 | } | |
3138 | addPoint(pBiDi, last, RLM_BEFORE); | |
3139 | break; | |
3140 | } | |
3141 | if(DIRPROP_FLAG(dirProp) & MASK_R_AL) { | |
3142 | break; | |
3143 | } | |
3144 | } | |
3145 | } | |
3146 | } | |
3147 | ||
73c04bcf A |
3148 | if(pBiDi->reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) { |
3149 | pBiDi->resultLength -= pBiDi->controlCount; | |
3150 | } else { | |
3151 | pBiDi->resultLength += pBiDi->insertPoints.size; | |
3152 | } | |
4388f060 | 3153 | setParaSuccess(pBiDi); /* mark successful setPara */ |
73c04bcf A |
3154 | } |
3155 | ||
2ca993e8 A |
3156 | /* -------------------------------------------------------------------------- */ |
3157 | ||
73c04bcf A |
3158 | U_CAPI void U_EXPORT2 |
3159 | ubidi_orderParagraphsLTR(UBiDi *pBiDi, UBool orderParagraphsLTR) { | |
3160 | if(pBiDi!=NULL) { | |
3161 | pBiDi->orderParagraphsLTR=orderParagraphsLTR; | |
3162 | } | |
3163 | } | |
3164 | ||
3165 | U_CAPI UBool U_EXPORT2 | |
3166 | ubidi_isOrderParagraphsLTR(UBiDi *pBiDi) { | |
3167 | if(pBiDi!=NULL) { | |
3168 | return pBiDi->orderParagraphsLTR; | |
3169 | } else { | |
3170 | return FALSE; | |
3171 | } | |
374ca955 | 3172 | } |
b75a7d8f A |
3173 | |
3174 | U_CAPI UBiDiDirection U_EXPORT2 | |
3175 | ubidi_getDirection(const UBiDi *pBiDi) { | |
73c04bcf | 3176 | if(IS_VALID_PARA_OR_LINE(pBiDi)) { |
b75a7d8f A |
3177 | return pBiDi->direction; |
3178 | } else { | |
3179 | return UBIDI_LTR; | |
3180 | } | |
3181 | } | |
3182 | ||
3183 | U_CAPI const UChar * U_EXPORT2 | |
3184 | ubidi_getText(const UBiDi *pBiDi) { | |
73c04bcf | 3185 | if(IS_VALID_PARA_OR_LINE(pBiDi)) { |
b75a7d8f A |
3186 | return pBiDi->text; |
3187 | } else { | |
3188 | return NULL; | |
3189 | } | |
3190 | } | |
3191 | ||
3192 | U_CAPI int32_t U_EXPORT2 | |
3193 | ubidi_getLength(const UBiDi *pBiDi) { | |
73c04bcf A |
3194 | if(IS_VALID_PARA_OR_LINE(pBiDi)) { |
3195 | return pBiDi->originalLength; | |
3196 | } else { | |
3197 | return 0; | |
3198 | } | |
3199 | } | |
3200 | ||
3201 | U_CAPI int32_t U_EXPORT2 | |
3202 | ubidi_getProcessedLength(const UBiDi *pBiDi) { | |
3203 | if(IS_VALID_PARA_OR_LINE(pBiDi)) { | |
b75a7d8f A |
3204 | return pBiDi->length; |
3205 | } else { | |
3206 | return 0; | |
3207 | } | |
3208 | } | |
3209 | ||
73c04bcf A |
3210 | U_CAPI int32_t U_EXPORT2 |
3211 | ubidi_getResultLength(const UBiDi *pBiDi) { | |
3212 | if(IS_VALID_PARA_OR_LINE(pBiDi)) { | |
3213 | return pBiDi->resultLength; | |
3214 | } else { | |
3215 | return 0; | |
3216 | } | |
3217 | } | |
3218 | ||
3219 | /* paragraphs API functions ------------------------------------------------- */ | |
3220 | ||
b75a7d8f A |
3221 | U_CAPI UBiDiLevel U_EXPORT2 |
3222 | ubidi_getParaLevel(const UBiDi *pBiDi) { | |
73c04bcf | 3223 | if(IS_VALID_PARA_OR_LINE(pBiDi)) { |
b75a7d8f A |
3224 | return pBiDi->paraLevel; |
3225 | } else { | |
3226 | return 0; | |
3227 | } | |
3228 | } | |
3229 | ||
73c04bcf A |
3230 | U_CAPI int32_t U_EXPORT2 |
3231 | ubidi_countParagraphs(UBiDi *pBiDi) { | |
3232 | if(!IS_VALID_PARA_OR_LINE(pBiDi)) { | |
3233 | return 0; | |
3234 | } else { | |
3235 | return pBiDi->paraCount; | |
3236 | } | |
3237 | } | |
b75a7d8f | 3238 | |
73c04bcf A |
3239 | U_CAPI void U_EXPORT2 |
3240 | ubidi_getParagraphByIndex(const UBiDi *pBiDi, int32_t paraIndex, | |
3241 | int32_t *pParaStart, int32_t *pParaLimit, | |
3242 | UBiDiLevel *pParaLevel, UErrorCode *pErrorCode) { | |
3243 | int32_t paraStart; | |
b75a7d8f | 3244 | |
73c04bcf | 3245 | /* check the argument values */ |
46f4442e A |
3246 | RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); |
3247 | RETURN_VOID_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode); | |
3248 | RETURN_VOID_IF_BAD_RANGE(paraIndex, 0, pBiDi->paraCount, *pErrorCode); | |
3249 | ||
73c04bcf A |
3250 | pBiDi=pBiDi->pParaBiDi; /* get Para object if Line object */ |
3251 | if(paraIndex) { | |
57a6839d | 3252 | paraStart=pBiDi->paras[paraIndex-1].limit; |
73c04bcf A |
3253 | } else { |
3254 | paraStart=0; | |
3255 | } | |
3256 | if(pParaStart!=NULL) { | |
3257 | *pParaStart=paraStart; | |
3258 | } | |
3259 | if(pParaLimit!=NULL) { | |
57a6839d | 3260 | *pParaLimit=pBiDi->paras[paraIndex].limit; |
73c04bcf A |
3261 | } |
3262 | if(pParaLevel!=NULL) { | |
3263 | *pParaLevel=GET_PARALEVEL(pBiDi, paraStart); | |
3264 | } | |
73c04bcf | 3265 | } |
b75a7d8f | 3266 | |
73c04bcf A |
3267 | U_CAPI int32_t U_EXPORT2 |
3268 | ubidi_getParagraph(const UBiDi *pBiDi, int32_t charIndex, | |
3269 | int32_t *pParaStart, int32_t *pParaLimit, | |
3270 | UBiDiLevel *pParaLevel, UErrorCode *pErrorCode) { | |
57a6839d | 3271 | int32_t paraIndex; |
b75a7d8f | 3272 | |
73c04bcf A |
3273 | /* check the argument values */ |
3274 | /* pErrorCode will be checked by the call to ubidi_getParagraphByIndex */ | |
46f4442e A |
3275 | RETURN_IF_NULL_OR_FAILING_ERRCODE(pErrorCode, -1); |
3276 | RETURN_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode, -1); | |
73c04bcf | 3277 | pBiDi=pBiDi->pParaBiDi; /* get Para object if Line object */ |
46f4442e A |
3278 | RETURN_IF_BAD_RANGE(charIndex, 0, pBiDi->length, *pErrorCode, -1); |
3279 | ||
57a6839d | 3280 | for(paraIndex=0; charIndex>=pBiDi->paras[paraIndex].limit; paraIndex++); |
73c04bcf A |
3281 | ubidi_getParagraphByIndex(pBiDi, paraIndex, pParaStart, pParaLimit, pParaLevel, pErrorCode); |
3282 | return paraIndex; | |
3283 | } | |
b75a7d8f | 3284 | |
73c04bcf A |
3285 | U_CAPI void U_EXPORT2 |
3286 | ubidi_setClassCallback(UBiDi *pBiDi, UBiDiClassCallback *newFn, | |
3287 | const void *newContext, UBiDiClassCallback **oldFn, | |
3288 | const void **oldContext, UErrorCode *pErrorCode) | |
3289 | { | |
46f4442e A |
3290 | RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode); |
3291 | if(pBiDi==NULL) { | |
73c04bcf A |
3292 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
3293 | return; | |
3294 | } | |
3295 | if( oldFn ) | |
3296 | { | |
3297 | *oldFn = pBiDi->fnClassCallback; | |
3298 | } | |
3299 | if( oldContext ) | |
3300 | { | |
3301 | *oldContext = pBiDi->coClassCallback; | |
3302 | } | |
3303 | pBiDi->fnClassCallback = newFn; | |
3304 | pBiDi->coClassCallback = newContext; | |
3305 | } | |
b75a7d8f | 3306 | |
73c04bcf A |
3307 | U_CAPI void U_EXPORT2 |
3308 | ubidi_getClassCallback(UBiDi *pBiDi, UBiDiClassCallback **fn, const void **context) | |
3309 | { | |
46f4442e A |
3310 | if(pBiDi==NULL) { |
3311 | return; | |
3312 | } | |
73c04bcf A |
3313 | if( fn ) |
3314 | { | |
3315 | *fn = pBiDi->fnClassCallback; | |
3316 | } | |
3317 | if( context ) | |
3318 | { | |
3319 | *context = pBiDi->coClassCallback; | |
3320 | } | |
3321 | } | |
b75a7d8f | 3322 | |
73c04bcf A |
3323 | U_CAPI UCharDirection U_EXPORT2 |
3324 | ubidi_getCustomizedClass(UBiDi *pBiDi, UChar32 c) | |
3325 | { | |
3326 | UCharDirection dir; | |
b75a7d8f | 3327 | |
73c04bcf A |
3328 | if( pBiDi->fnClassCallback == NULL || |
3329 | (dir = (*pBiDi->fnClassCallback)(pBiDi->coClassCallback, c)) == U_BIDI_CLASS_DEFAULT ) | |
3330 | { | |
57a6839d A |
3331 | dir = ubidi_getClass(pBiDi->bdp, c); |
3332 | } | |
3333 | if(dir >= U_CHAR_DIRECTION_COUNT) { | |
3334 | dir = ON; | |
b75a7d8f | 3335 | } |
57a6839d | 3336 | return dir; |
b75a7d8f | 3337 | } |