]>
Commit | Line | Data |
---|---|---|
73c04bcf | 1 | /* |
b75a7d8f A |
2 | ****************************************************************************** |
3 | * | |
73c04bcf | 4 | * Copyright (C) 1999-2006, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ****************************************************************************** | |
8 | * file name: ubidiln.c | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 1999aug06 | |
14 | * created by: Markus W. Scherer | |
15 | */ | |
16 | ||
17 | /* set import/export definitions */ | |
18 | #ifndef U_COMMON_IMPLEMENTATION | |
19 | # define U_COMMON_IMPLEMENTATION | |
20 | #endif | |
21 | ||
22 | #include "cmemory.h" | |
23 | #include "unicode/utypes.h" | |
24 | #include "unicode/ustring.h" | |
25 | #include "unicode/uchar.h" | |
26 | #include "unicode/ubidi.h" | |
27 | #include "ubidiimp.h" | |
28 | ||
29 | /* | |
30 | * General remarks about the functions in this file: | |
31 | * | |
32 | * These functions deal with the aspects of potentially mixed-directional | |
33 | * text in a single paragraph or in a line of a single paragraph | |
34 | * which has already been processed according to | |
35 | * the Unicode 3.0 BiDi algorithm as defined in | |
73c04bcf A |
36 | * http://www.unicode.org/unicode/reports/tr9/ , version 13, |
37 | * also described in The Unicode Standard, Version 4.0.1 . | |
b75a7d8f A |
38 | * |
39 | * This means that there is a UBiDi object with a levels | |
40 | * and a dirProps array. | |
41 | * paraLevel and direction are also set. | |
42 | * Only if the length of the text is zero, then levels==dirProps==NULL. | |
43 | * | |
44 | * The overall directionality of the paragraph | |
45 | * or line is used to bypass the reordering steps if possible. | |
46 | * Even purely RTL text does not need reordering there because | |
47 | * the ubidi_getLogical/VisualIndex() functions can compute the | |
48 | * index on the fly in such a case. | |
49 | * | |
50 | * The implementation of the access to same-level-runs and of the reordering | |
51 | * do attempt to provide better performance and less memory usage compared to | |
52 | * a direct implementation of especially rule (L2) with an array of | |
53 | * one (32-bit) integer per text character. | |
54 | * | |
55 | * Here, the levels array is scanned as soon as necessary, and a vector of | |
56 | * same-level-runs is created. Reordering then is done on this vector. | |
57 | * For each run of text positions that were resolved to the same level, | |
58 | * only 8 bytes are stored: the first text position of the run and the visual | |
59 | * position behind the run after reordering. | |
60 | * One sign bit is used to hold the directionality of the run. | |
61 | * This is inefficient if there are many very short runs. If the average run | |
62 | * length is <2, then this uses more memory. | |
63 | * | |
64 | * In a further attempt to save memory, the levels array is never changed | |
65 | * after all the resolution rules (Xn, Wn, Nn, In). | |
66 | * Many functions have to consider the field trailingWSStart: | |
67 | * if it is less than length, then there is an implicit trailing run | |
68 | * at the paraLevel, | |
69 | * which is not reflected in the levels array. | |
70 | * This allows a line UBiDi object to use the same levels array as | |
71 | * its paragraph parent object. | |
72 | * | |
73 | * When a UBiDi object is created for a line of a paragraph, then the | |
74 | * paragraph's levels and dirProps arrays are reused by way of setting | |
75 | * a pointer into them, not by copying. This again saves memory and forbids to | |
76 | * change the now shared levels for (L1). | |
77 | */ | |
78 | ||
374ca955 | 79 | /* handle trailing WS (L1) -------------------------------------------------- */ |
b75a7d8f | 80 | |
374ca955 A |
81 | /* |
82 | * setTrailingWSStart() sets the start index for a trailing | |
83 | * run of WS in the line. This is necessary because we do not modify | |
84 | * the paragraph's levels array that we just point into. | |
85 | * Using trailingWSStart is another form of performing (L1). | |
86 | * | |
87 | * To make subsequent operations easier, we also include the run | |
88 | * before the WS if it is at the paraLevel - we merge the two here. | |
73c04bcf A |
89 | * |
90 | * This function is called only from ubidi_setLine(), so pBiDi->paraLevel is | |
91 | * set correctly for the line even when contextual multiple paragraphs. | |
374ca955 | 92 | */ |
b75a7d8f | 93 | static void |
374ca955 A |
94 | setTrailingWSStart(UBiDi *pBiDi) { |
95 | /* pBiDi->direction!=UBIDI_MIXED */ | |
b75a7d8f | 96 | |
374ca955 A |
97 | const DirProp *dirProps=pBiDi->dirProps; |
98 | UBiDiLevel *levels=pBiDi->levels; | |
99 | int32_t start=pBiDi->length; | |
100 | UBiDiLevel paraLevel=pBiDi->paraLevel; | |
b75a7d8f | 101 | |
73c04bcf A |
102 | /* If the line is terminated by a block separator, all preceding WS etc... |
103 | are already set to paragraph level. | |
104 | Setting trailingWSStart to pBidi->length will avoid changing the | |
105 | level of B chars from 0 to paraLevel in ubidi_getLevels when | |
106 | orderParagraphsLTR==TRUE. | |
107 | */ | |
108 | if(NO_CONTEXT_RTL(dirProps[start-1])==B) { | |
109 | pBiDi->trailingWSStart=start; /* currently == pBiDi->length */ | |
110 | return; | |
111 | } | |
374ca955 | 112 | /* go backwards across all WS, BN, explicit codes */ |
73c04bcf | 113 | while(start>0 && DIRPROP_FLAG_NC(dirProps[start-1])&MASK_WS) { |
374ca955 A |
114 | --start; |
115 | } | |
b75a7d8f | 116 | |
374ca955 A |
117 | /* if the WS run can be merged with the previous run then do so here */ |
118 | while(start>0 && levels[start-1]==paraLevel) { | |
119 | --start; | |
120 | } | |
121 | ||
122 | pBiDi->trailingWSStart=start; | |
123 | } | |
b75a7d8f A |
124 | |
125 | /* ubidi_setLine ------------------------------------------------------------ */ | |
126 | ||
127 | U_CAPI void U_EXPORT2 | |
128 | ubidi_setLine(const UBiDi *pParaBiDi, | |
129 | int32_t start, int32_t limit, | |
130 | UBiDi *pLineBiDi, | |
131 | UErrorCode *pErrorCode) { | |
132 | int32_t length; | |
133 | ||
134 | /* check the argument values */ | |
135 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
136 | return; | |
73c04bcf | 137 | } else if(!IS_VALID_PARA(pParaBiDi) || pLineBiDi==NULL) { |
b75a7d8f A |
138 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
139 | return; | |
140 | } else if(start<0 || start>limit || limit>pParaBiDi->length) { | |
141 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
142 | return; | |
73c04bcf A |
143 | } else if(ubidi_getParagraph(pParaBiDi, start, NULL, NULL, NULL, pErrorCode) != |
144 | ubidi_getParagraph(pParaBiDi, limit-1, NULL, NULL, NULL, pErrorCode)) { | |
145 | /* the line crosses a paragraph boundary */ | |
146 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
147 | return; | |
b75a7d8f A |
148 | } |
149 | ||
150 | /* set the values in pLineBiDi from its pParaBiDi parent */ | |
73c04bcf | 151 | pLineBiDi->pParaBiDi=NULL; /* mark unfinished setLine */ |
b75a7d8f A |
152 | pLineBiDi->text=pParaBiDi->text+start; |
153 | length=pLineBiDi->length=limit-start; | |
73c04bcf A |
154 | pLineBiDi->resultLength=pLineBiDi->originalLength=length; |
155 | pLineBiDi->paraLevel=GET_PARALEVEL(pParaBiDi, start); | |
156 | pLineBiDi->paraCount=pParaBiDi->paraCount; | |
b75a7d8f A |
157 | pLineBiDi->runs=NULL; |
158 | pLineBiDi->flags=0; | |
73c04bcf A |
159 | pLineBiDi->reorderingMode=pParaBiDi->reorderingMode; |
160 | pLineBiDi->reorderingOptions=pParaBiDi->reorderingOptions; | |
161 | pLineBiDi->controlCount=0; | |
162 | if(pParaBiDi->controlCount>0) { | |
163 | int32_t j; | |
164 | for(j=start; j<limit; j++) { | |
165 | if(IS_BIDI_CONTROL_CHAR(pParaBiDi->text[j])) { | |
166 | pLineBiDi->controlCount++; | |
167 | } | |
168 | } | |
169 | } | |
b75a7d8f A |
170 | |
171 | if(length>0) { | |
172 | pLineBiDi->dirProps=pParaBiDi->dirProps+start; | |
173 | pLineBiDi->levels=pParaBiDi->levels+start; | |
174 | pLineBiDi->runCount=-1; | |
175 | ||
176 | if(pParaBiDi->direction!=UBIDI_MIXED) { | |
177 | /* the parent is already trivial */ | |
178 | pLineBiDi->direction=pParaBiDi->direction; | |
179 | ||
180 | /* | |
181 | * The parent's levels are all either | |
182 | * implicitly or explicitly ==paraLevel; | |
183 | * do the same here. | |
184 | */ | |
185 | if(pParaBiDi->trailingWSStart<=start) { | |
186 | pLineBiDi->trailingWSStart=0; | |
187 | } else if(pParaBiDi->trailingWSStart<limit) { | |
188 | pLineBiDi->trailingWSStart=pParaBiDi->trailingWSStart-start; | |
189 | } else { | |
190 | pLineBiDi->trailingWSStart=length; | |
191 | } | |
192 | } else { | |
193 | const UBiDiLevel *levels=pLineBiDi->levels; | |
194 | int32_t i, trailingWSStart; | |
195 | UBiDiLevel level; | |
196 | ||
197 | setTrailingWSStart(pLineBiDi); | |
198 | trailingWSStart=pLineBiDi->trailingWSStart; | |
199 | ||
200 | /* recalculate pLineBiDi->direction */ | |
201 | if(trailingWSStart==0) { | |
202 | /* all levels are at paraLevel */ | |
203 | pLineBiDi->direction=(UBiDiDirection)(pLineBiDi->paraLevel&1); | |
204 | } else { | |
205 | /* get the level of the first character */ | |
206 | level=(UBiDiLevel)(levels[0]&1); | |
207 | ||
208 | /* if there is anything of a different level, then the line is mixed */ | |
209 | if(trailingWSStart<length && (pLineBiDi->paraLevel&1)!=level) { | |
210 | /* the trailing WS is at paraLevel, which differs from levels[0] */ | |
211 | pLineBiDi->direction=UBIDI_MIXED; | |
212 | } else { | |
213 | /* see if levels[1..trailingWSStart-1] have the same direction as levels[0] and paraLevel */ | |
214 | i=1; | |
215 | for(;;) { | |
216 | if(i==trailingWSStart) { | |
217 | /* the direction values match those in level */ | |
218 | pLineBiDi->direction=(UBiDiDirection)level; | |
219 | break; | |
220 | } else if((levels[i]&1)!=level) { | |
221 | pLineBiDi->direction=UBIDI_MIXED; | |
222 | break; | |
223 | } | |
224 | ++i; | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | switch(pLineBiDi->direction) { | |
230 | case UBIDI_LTR: | |
231 | /* make sure paraLevel is even */ | |
232 | pLineBiDi->paraLevel=(UBiDiLevel)((pLineBiDi->paraLevel+1)&~1); | |
233 | ||
234 | /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */ | |
235 | pLineBiDi->trailingWSStart=0; | |
236 | break; | |
237 | case UBIDI_RTL: | |
238 | /* make sure paraLevel is odd */ | |
239 | pLineBiDi->paraLevel|=1; | |
240 | ||
241 | /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */ | |
242 | pLineBiDi->trailingWSStart=0; | |
243 | break; | |
244 | default: | |
245 | break; | |
246 | } | |
247 | } | |
248 | } else { | |
249 | /* create an object for a zero-length line */ | |
250 | pLineBiDi->direction=pLineBiDi->paraLevel&1 ? UBIDI_RTL : UBIDI_LTR; | |
251 | pLineBiDi->trailingWSStart=pLineBiDi->runCount=0; | |
252 | ||
253 | pLineBiDi->dirProps=NULL; | |
254 | pLineBiDi->levels=NULL; | |
255 | } | |
73c04bcf | 256 | pLineBiDi->pParaBiDi=pParaBiDi; /* mark successful setLine */ |
b75a7d8f A |
257 | return; |
258 | } | |
259 | ||
260 | U_CAPI UBiDiLevel U_EXPORT2 | |
261 | ubidi_getLevelAt(const UBiDi *pBiDi, int32_t charIndex) { | |
262 | /* return paraLevel if in the trailing WS run, otherwise the real level */ | |
73c04bcf | 263 | if(!IS_VALID_PARA_OR_LINE(pBiDi) || charIndex<0 || pBiDi->length<=charIndex) { |
b75a7d8f A |
264 | return 0; |
265 | } else if(pBiDi->direction!=UBIDI_MIXED || charIndex>=pBiDi->trailingWSStart) { | |
73c04bcf | 266 | return GET_PARALEVEL(pBiDi, charIndex); |
b75a7d8f A |
267 | } else { |
268 | return pBiDi->levels[charIndex]; | |
269 | } | |
270 | } | |
271 | ||
272 | U_CAPI const UBiDiLevel * U_EXPORT2 | |
273 | ubidi_getLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) { | |
274 | int32_t start, length; | |
275 | ||
276 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
277 | return NULL; | |
73c04bcf | 278 | } else if(!IS_VALID_PARA_OR_LINE(pBiDi) || (length=pBiDi->length)<=0) { |
b75a7d8f A |
279 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
280 | return NULL; | |
281 | } | |
282 | ||
283 | if((start=pBiDi->trailingWSStart)==length) { | |
284 | /* the current levels array reflects the WS run */ | |
285 | return pBiDi->levels; | |
286 | } | |
287 | ||
288 | /* | |
289 | * After the previous if(), we know that the levels array | |
290 | * has an implicit trailing WS run and therefore does not fully | |
291 | * reflect itself all the levels. | |
292 | * This must be a UBiDi object for a line, and | |
293 | * we need to create a new levels array. | |
294 | */ | |
295 | ||
296 | if(getLevelsMemory(pBiDi, length)) { | |
297 | UBiDiLevel *levels=pBiDi->levelsMemory; | |
298 | ||
299 | if(start>0 && levels!=pBiDi->levels) { | |
300 | uprv_memcpy(levels, pBiDi->levels, start); | |
301 | } | |
73c04bcf A |
302 | /* pBiDi->paraLevel is ok even if contextual multiple paragraphs, |
303 | since pBidi is a line object */ | |
b75a7d8f A |
304 | uprv_memset(levels+start, pBiDi->paraLevel, length-start); |
305 | ||
306 | /* this new levels array is set for the line and reflects the WS run */ | |
307 | pBiDi->trailingWSStart=length; | |
308 | return pBiDi->levels=levels; | |
309 | } else { | |
310 | /* out of memory */ | |
311 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
312 | return NULL; | |
313 | } | |
314 | } | |
315 | ||
316 | U_CAPI void U_EXPORT2 | |
317 | ubidi_getLogicalRun(const UBiDi *pBiDi, int32_t logicalStart, | |
318 | int32_t *pLogicalLimit, UBiDiLevel *pLevel) { | |
319 | int32_t length; | |
320 | ||
73c04bcf A |
321 | if(!IS_VALID_PARA_OR_LINE(pBiDi) || logicalStart<0 || |
322 | (length=pBiDi->length)<=logicalStart) { | |
b75a7d8f A |
323 | return; |
324 | } | |
325 | ||
326 | if(pBiDi->direction!=UBIDI_MIXED || logicalStart>=pBiDi->trailingWSStart) { | |
327 | if(pLogicalLimit!=NULL) { | |
328 | *pLogicalLimit=length; | |
329 | } | |
330 | if(pLevel!=NULL) { | |
73c04bcf | 331 | *pLevel=GET_PARALEVEL(pBiDi, logicalStart); |
b75a7d8f A |
332 | } |
333 | } else { | |
334 | UBiDiLevel *levels=pBiDi->levels; | |
335 | UBiDiLevel level=levels[logicalStart]; | |
336 | ||
337 | /* search for the end of the run */ | |
338 | length=pBiDi->trailingWSStart; | |
339 | while(++logicalStart<length && level==levels[logicalStart]) {} | |
340 | ||
341 | if(pLogicalLimit!=NULL) { | |
342 | *pLogicalLimit=logicalStart; | |
343 | } | |
344 | if(pLevel!=NULL) { | |
345 | *pLevel=level; | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
b75a7d8f A |
350 | /* runs API functions ------------------------------------------------------- */ |
351 | ||
352 | U_CAPI int32_t U_EXPORT2 | |
353 | ubidi_countRuns(UBiDi *pBiDi, UErrorCode *pErrorCode) { | |
354 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
355 | return -1; | |
73c04bcf A |
356 | } else if(!IS_VALID_PARA_OR_LINE(pBiDi) || |
357 | (pBiDi->runCount<0 && !ubidi_getRuns(pBiDi))) { | |
b75a7d8f A |
358 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; |
359 | return -1; | |
360 | } else { | |
361 | return pBiDi->runCount; | |
362 | } | |
363 | } | |
364 | ||
365 | U_CAPI UBiDiDirection U_EXPORT2 | |
366 | ubidi_getVisualRun(UBiDi *pBiDi, int32_t runIndex, | |
367 | int32_t *pLogicalStart, int32_t *pLength) { | |
73c04bcf | 368 | if( !IS_VALID_PARA_OR_LINE(pBiDi) || runIndex<0 || |
b75a7d8f A |
369 | (pBiDi->runCount==-1 && !ubidi_getRuns(pBiDi)) || |
370 | runIndex>=pBiDi->runCount | |
371 | ) { | |
372 | return UBIDI_LTR; | |
373 | } else { | |
374 | int32_t start=pBiDi->runs[runIndex].logicalStart; | |
375 | if(pLogicalStart!=NULL) { | |
376 | *pLogicalStart=GET_INDEX(start); | |
377 | } | |
378 | if(pLength!=NULL) { | |
379 | if(runIndex>0) { | |
380 | *pLength=pBiDi->runs[runIndex].visualLimit- | |
381 | pBiDi->runs[runIndex-1].visualLimit; | |
382 | } else { | |
383 | *pLength=pBiDi->runs[0].visualLimit; | |
384 | } | |
385 | } | |
386 | return (UBiDiDirection)GET_ODD_BIT(start); | |
387 | } | |
388 | } | |
389 | ||
374ca955 A |
390 | /* in trivial cases there is only one trivial run; called by ubidi_getRuns() */ |
391 | static void | |
392 | getSingleRun(UBiDi *pBiDi, UBiDiLevel level) { | |
393 | /* simple, single-run case */ | |
394 | pBiDi->runs=pBiDi->simpleRuns; | |
395 | pBiDi->runCount=1; | |
396 | ||
397 | /* fill and reorder the single run */ | |
398 | pBiDi->runs[0].logicalStart=MAKE_INDEX_ODD_PAIR(0, level); | |
399 | pBiDi->runs[0].visualLimit=pBiDi->length; | |
73c04bcf | 400 | pBiDi->runs[0].insertRemove=0; |
374ca955 A |
401 | } |
402 | ||
403 | /* reorder the runs array (L2) ---------------------------------------------- */ | |
404 | ||
405 | /* | |
406 | * Reorder the same-level runs in the runs array. | |
407 | * Here, runCount>1 and maxLevel>=minLevel>=paraLevel. | |
408 | * All the visualStart fields=logical start before reordering. | |
409 | * The "odd" bits are not set yet. | |
410 | * | |
411 | * Reordering with this data structure lends itself to some handy shortcuts: | |
412 | * | |
413 | * Since each run is moved but not modified, and since at the initial maxLevel | |
414 | * each sequence of same-level runs consists of only one run each, we | |
415 | * don't need to do anything there and can predecrement maxLevel. | |
416 | * In many simple cases, the reordering is thus done entirely in the | |
417 | * index mapping. | |
418 | * Also, reordering occurs only down to the lowest odd level that occurs, | |
419 | * which is minLevel|1. However, if the lowest level itself is odd, then | |
420 | * in the last reordering the sequence of the runs at this level or higher | |
421 | * will be all runs, and we don't need the elaborate loop to search for them. | |
422 | * This is covered by ++minLevel instead of minLevel|=1 followed | |
423 | * by an extra reorder-all after the reorder-some loop. | |
424 | * About a trailing WS run: | |
425 | * Such a run would need special treatment because its level is not | |
426 | * reflected in levels[] if this is not a paragraph object. | |
427 | * Instead, all characters from trailingWSStart on are implicitly at | |
428 | * paraLevel. | |
429 | * However, for all maxLevel>paraLevel, this run will never be reordered | |
430 | * and does not need to be taken into account. maxLevel==paraLevel is only reordered | |
431 | * if minLevel==paraLevel is odd, which is done in the extra segment. | |
432 | * This means that for the main reordering loop we don't need to consider | |
433 | * this run and can --runCount. If it is later part of the all-runs | |
434 | * reordering, then runCount is adjusted accordingly. | |
435 | */ | |
436 | static void | |
437 | reorderLine(UBiDi *pBiDi, UBiDiLevel minLevel, UBiDiLevel maxLevel) { | |
73c04bcf | 438 | Run *runs, tempRun; |
374ca955 | 439 | UBiDiLevel *levels; |
73c04bcf | 440 | int32_t firstRun, endRun, limitRun, runCount; |
374ca955 A |
441 | |
442 | /* nothing to do? */ | |
443 | if(maxLevel<=(minLevel|1)) { | |
444 | return; | |
445 | } | |
446 | ||
447 | /* | |
448 | * Reorder only down to the lowest odd level | |
449 | * and reorder at an odd minLevel in a separate, simpler loop. | |
450 | * See comments above for why minLevel is always incremented. | |
451 | */ | |
452 | ++minLevel; | |
453 | ||
454 | runs=pBiDi->runs; | |
455 | levels=pBiDi->levels; | |
456 | runCount=pBiDi->runCount; | |
457 | ||
458 | /* do not include the WS run at paraLevel<=old minLevel except in the simple loop */ | |
459 | if(pBiDi->trailingWSStart<pBiDi->length) { | |
460 | --runCount; | |
461 | } | |
462 | ||
463 | while(--maxLevel>=minLevel) { | |
464 | firstRun=0; | |
465 | ||
466 | /* loop for all sequences of runs */ | |
467 | for(;;) { | |
468 | /* look for a sequence of runs that are all at >=maxLevel */ | |
469 | /* look for the first run of such a sequence */ | |
470 | while(firstRun<runCount && levels[runs[firstRun].logicalStart]<maxLevel) { | |
471 | ++firstRun; | |
472 | } | |
473 | if(firstRun>=runCount) { | |
474 | break; /* no more such runs */ | |
475 | } | |
476 | ||
477 | /* look for the limit run of such a sequence (the run behind it) */ | |
478 | for(limitRun=firstRun; ++limitRun<runCount && levels[runs[limitRun].logicalStart]>=maxLevel;) {} | |
479 | ||
480 | /* Swap the entire sequence of runs from firstRun to limitRun-1. */ | |
481 | endRun=limitRun-1; | |
482 | while(firstRun<endRun) { | |
73c04bcf A |
483 | tempRun = runs[firstRun]; |
484 | runs[firstRun]=runs[endRun]; | |
485 | runs[endRun]=tempRun; | |
374ca955 A |
486 | ++firstRun; |
487 | --endRun; | |
488 | } | |
489 | ||
490 | if(limitRun==runCount) { | |
491 | break; /* no more such runs */ | |
492 | } else { | |
493 | firstRun=limitRun+1; | |
494 | } | |
495 | } | |
496 | } | |
497 | ||
498 | /* now do maxLevel==old minLevel (==odd!), see above */ | |
499 | if(!(minLevel&1)) { | |
500 | firstRun=0; | |
501 | ||
502 | /* include the trailing WS run in this complete reordering */ | |
503 | if(pBiDi->trailingWSStart==pBiDi->length) { | |
504 | --runCount; | |
505 | } | |
506 | ||
507 | /* Swap the entire sequence of all runs. (endRun==runCount) */ | |
508 | while(firstRun<runCount) { | |
73c04bcf A |
509 | tempRun=runs[firstRun]; |
510 | runs[firstRun]=runs[runCount]; | |
511 | runs[runCount]=tempRun; | |
374ca955 A |
512 | ++firstRun; |
513 | --runCount; | |
514 | } | |
515 | } | |
516 | } | |
517 | ||
b75a7d8f A |
518 | /* compute the runs array --------------------------------------------------- */ |
519 | ||
73c04bcf A |
520 | static int32_t getRunFromLogicalIndex(UBiDi *pBiDi, int32_t logicalIndex) { |
521 | Run *runs=pBiDi->runs; | |
522 | int32_t runCount=pBiDi->runCount, visualStart=0, i, length, logicalStart; | |
523 | ||
524 | for(i=0; i<runCount; i++) { | |
525 | length=runs[i].visualLimit-visualStart; | |
526 | logicalStart=GET_INDEX(runs[i].logicalStart); | |
527 | if((logicalIndex>=logicalStart) && (logicalIndex<(logicalStart+length))) { | |
528 | return i; | |
529 | } | |
530 | visualStart+=length; | |
531 | } | |
532 | /* we should never get here */ | |
533 | i=length+25; | |
534 | i/=(i-length-25); /* force program crash */ | |
535 | return 0; | |
536 | } | |
537 | ||
b75a7d8f A |
538 | /* |
539 | * Compute the runs array from the levels array. | |
540 | * After ubidi_getRuns() returns TRUE, runCount is guaranteed to be >0 | |
541 | * and the runs are reordered. | |
542 | * Odd-level runs have visualStart on their visual right edge and | |
543 | * they progress visually to the left. | |
73c04bcf A |
544 | * If option UBIDI_OPTION_INSERT_MARKS is set, insertRemove will contain the |
545 | * sum of appropriate LRM/RLM_BEFORE/AFTER flags. | |
546 | * If option UBIDI_OPTION_REMOVE_CONTROLS is set, insertRemove will contain the | |
547 | * negative number of BiDi control characters within this run. | |
b75a7d8f A |
548 | */ |
549 | U_CFUNC UBool | |
550 | ubidi_getRuns(UBiDi *pBiDi) { | |
551 | if(pBiDi->direction!=UBIDI_MIXED) { | |
552 | /* simple, single-run case - this covers length==0 */ | |
73c04bcf | 553 | /* pBiDi->paraLevel is ok even for contextual multiple paragraphs */ |
b75a7d8f A |
554 | getSingleRun(pBiDi, pBiDi->paraLevel); |
555 | } else /* UBIDI_MIXED, length>0 */ { | |
556 | /* mixed directionality */ | |
557 | int32_t length=pBiDi->length, limit; | |
558 | ||
559 | /* | |
560 | * If there are WS characters at the end of the line | |
561 | * and the run preceding them has a level different from | |
562 | * paraLevel, then they will form their own run at paraLevel (L1). | |
563 | * Count them separately. | |
564 | * We need some special treatment for this in order to not | |
565 | * modify the levels array which a line UBiDi object shares | |
566 | * with its paragraph parent and its other line siblings. | |
567 | * In other words, for the trailing WS, it may be | |
568 | * levels[]!=paraLevel but we have to treat it like it were so. | |
569 | */ | |
570 | limit=pBiDi->trailingWSStart; | |
571 | if(limit==0) { | |
572 | /* there is only WS on this line */ | |
73c04bcf | 573 | getSingleRun(pBiDi, GET_PARALEVEL(pBiDi, 0)); |
b75a7d8f A |
574 | } else { |
575 | UBiDiLevel *levels=pBiDi->levels; | |
576 | int32_t i, runCount; | |
577 | UBiDiLevel level=UBIDI_DEFAULT_LTR; /* initialize with no valid level */ | |
578 | ||
579 | /* count the runs, there is at least one non-WS run, and limit>0 */ | |
580 | runCount=0; | |
581 | for(i=0; i<limit; ++i) { | |
582 | /* increment runCount at the start of each run */ | |
583 | if(levels[i]!=level) { | |
584 | ++runCount; | |
585 | level=levels[i]; | |
586 | } | |
587 | } | |
588 | ||
589 | /* | |
590 | * We don't need to see if the last run can be merged with a trailing | |
591 | * WS run because setTrailingWSStart() would have done that. | |
592 | */ | |
593 | if(runCount==1 && limit==length) { | |
594 | /* There is only one non-WS run and no trailing WS-run. */ | |
595 | getSingleRun(pBiDi, levels[0]); | |
596 | } else /* runCount>1 || limit<length */ { | |
597 | /* allocate and set the runs */ | |
598 | Run *runs; | |
599 | int32_t runIndex, start; | |
600 | UBiDiLevel minLevel=UBIDI_MAX_EXPLICIT_LEVEL+1, maxLevel=0; | |
601 | ||
73c04bcf | 602 | /* now, count a (non-mergeable) WS run */ |
b75a7d8f A |
603 | if(limit<length) { |
604 | ++runCount; | |
605 | } | |
606 | ||
607 | /* runCount>1 */ | |
608 | if(getRunsMemory(pBiDi, runCount)) { | |
609 | runs=pBiDi->runsMemory; | |
610 | } else { | |
611 | return FALSE; | |
612 | } | |
613 | ||
614 | /* set the runs */ | |
73c04bcf A |
615 | /* FOOD FOR THOUGHT: this could be optimized, e.g.: |
616 | * 464->444, 484->444, 575->555, 595->555 | |
617 | * However, that would take longer. Check also how it would | |
618 | * interact with BiDi control removal and inserting Marks. | |
619 | */ | |
b75a7d8f A |
620 | runIndex=0; |
621 | ||
622 | /* search for the run limits and initialize visualLimit values with the run lengths */ | |
623 | i=0; | |
624 | do { | |
625 | /* prepare this run */ | |
626 | start=i; | |
627 | level=levels[i]; | |
628 | if(level<minLevel) { | |
629 | minLevel=level; | |
630 | } | |
631 | if(level>maxLevel) { | |
632 | maxLevel=level; | |
633 | } | |
634 | ||
635 | /* look for the run limit */ | |
636 | while(++i<limit && levels[i]==level) {} | |
637 | ||
638 | /* i is another run limit */ | |
639 | runs[runIndex].logicalStart=start; | |
640 | runs[runIndex].visualLimit=i-start; | |
73c04bcf | 641 | runs[runIndex].insertRemove=0; |
b75a7d8f A |
642 | ++runIndex; |
643 | } while(i<limit); | |
644 | ||
645 | if(limit<length) { | |
646 | /* there is a separate WS run */ | |
647 | runs[runIndex].logicalStart=limit; | |
648 | runs[runIndex].visualLimit=length-limit; | |
73c04bcf A |
649 | /* For the trailing WS run, pBiDi->paraLevel is ok even |
650 | if contextual multiple paragraphs. */ | |
b75a7d8f A |
651 | if(pBiDi->paraLevel<minLevel) { |
652 | minLevel=pBiDi->paraLevel; | |
653 | } | |
654 | } | |
655 | ||
656 | /* set the object fields */ | |
657 | pBiDi->runs=runs; | |
658 | pBiDi->runCount=runCount; | |
659 | ||
660 | reorderLine(pBiDi, minLevel, maxLevel); | |
661 | ||
662 | /* now add the direction flags and adjust the visualLimit's to be just that */ | |
374ca955 | 663 | /* this loop will also handle the trailing WS run */ |
73c04bcf A |
664 | limit=0; |
665 | for(i=0; i<runCount; ++i) { | |
b75a7d8f A |
666 | ADD_ODD_BIT_FROM_LEVEL(runs[i].logicalStart, levels[runs[i].logicalStart]); |
667 | limit=runs[i].visualLimit+=limit; | |
668 | } | |
669 | ||
374ca955 A |
670 | /* Set the "odd" bit for the trailing WS run. */ |
671 | /* For a RTL paragraph, it will be the *first* run in visual order. */ | |
73c04bcf A |
672 | /* For the trailing WS run, pBiDi->paraLevel is ok even if |
673 | contextual multiple paragraphs. */ | |
b75a7d8f | 674 | if(runIndex<runCount) { |
374ca955 A |
675 | int32_t trailingRun = ((pBiDi->paraLevel & 1) != 0)? 0 : runIndex; |
676 | ||
677 | ADD_ODD_BIT_FROM_LEVEL(runs[trailingRun].logicalStart, pBiDi->paraLevel); | |
b75a7d8f A |
678 | } |
679 | } | |
680 | } | |
681 | } | |
73c04bcf A |
682 | |
683 | /* handle insert LRM/RLM BEFORE/AFTER run */ | |
684 | if(pBiDi->insertPoints.size>0) { | |
685 | Point *point, *start=pBiDi->insertPoints.points, | |
686 | *limit=start+pBiDi->insertPoints.size; | |
687 | int32_t runIndex; | |
688 | for(point=start; point<limit; point++) { | |
689 | runIndex=getRunFromLogicalIndex(pBiDi, point->pos); | |
690 | pBiDi->runs[runIndex].insertRemove|=point->flag; | |
691 | } | |
692 | } | |
693 | ||
694 | /* handle remove BiDi control characters */ | |
695 | if(pBiDi->controlCount>0) { | |
696 | int32_t runIndex; | |
697 | const UChar *start=pBiDi->text, *limit=start+pBiDi->length, *pu; | |
698 | for(pu=start; pu<limit; pu++) { | |
699 | if(IS_BIDI_CONTROL_CHAR(*pu)) { | |
700 | runIndex=getRunFromLogicalIndex(pBiDi, pu-start); | |
701 | pBiDi->runs[runIndex].insertRemove--; | |
702 | } | |
703 | } | |
704 | } | |
705 | ||
b75a7d8f A |
706 | return TRUE; |
707 | } | |
708 | ||
374ca955 A |
709 | static UBool |
710 | prepareReorder(const UBiDiLevel *levels, int32_t length, | |
711 | int32_t *indexMap, | |
712 | UBiDiLevel *pMinLevel, UBiDiLevel *pMaxLevel) { | |
713 | int32_t start; | |
714 | UBiDiLevel level, minLevel, maxLevel; | |
b75a7d8f | 715 | |
374ca955 A |
716 | if(levels==NULL || length<=0) { |
717 | return FALSE; | |
b75a7d8f A |
718 | } |
719 | ||
374ca955 A |
720 | /* determine minLevel and maxLevel */ |
721 | minLevel=UBIDI_MAX_EXPLICIT_LEVEL+1; | |
722 | maxLevel=0; | |
723 | for(start=length; start>0;) { | |
724 | level=levels[--start]; | |
725 | if(level>UBIDI_MAX_EXPLICIT_LEVEL+1) { | |
726 | return FALSE; | |
b75a7d8f | 727 | } |
374ca955 A |
728 | if(level<minLevel) { |
729 | minLevel=level; | |
b75a7d8f | 730 | } |
374ca955 A |
731 | if(level>maxLevel) { |
732 | maxLevel=level; | |
b75a7d8f A |
733 | } |
734 | } | |
374ca955 A |
735 | *pMinLevel=minLevel; |
736 | *pMaxLevel=maxLevel; | |
737 | ||
738 | /* initialize the index map */ | |
739 | for(start=length; start>0;) { | |
740 | --start; | |
741 | indexMap[start]=start; | |
742 | } | |
743 | ||
744 | return TRUE; | |
b75a7d8f A |
745 | } |
746 | ||
747 | /* reorder a line based on a levels array (L2) ------------------------------ */ | |
748 | ||
749 | U_CAPI void U_EXPORT2 | |
750 | ubidi_reorderLogical(const UBiDiLevel *levels, int32_t length, int32_t *indexMap) { | |
751 | int32_t start, limit, sumOfSosEos; | |
752 | UBiDiLevel minLevel, maxLevel; | |
753 | ||
754 | if(indexMap==NULL || !prepareReorder(levels, length, indexMap, &minLevel, &maxLevel)) { | |
755 | return; | |
756 | } | |
757 | ||
758 | /* nothing to do? */ | |
759 | if(minLevel==maxLevel && (minLevel&1)==0) { | |
760 | return; | |
761 | } | |
762 | ||
763 | /* reorder only down to the lowest odd level */ | |
764 | minLevel|=1; | |
765 | ||
766 | /* loop maxLevel..minLevel */ | |
767 | do { | |
768 | start=0; | |
769 | ||
770 | /* loop for all sequences of levels to reorder at the current maxLevel */ | |
771 | for(;;) { | |
772 | /* look for a sequence of levels that are all at >=maxLevel */ | |
773 | /* look for the first index of such a sequence */ | |
774 | while(start<length && levels[start]<maxLevel) { | |
775 | ++start; | |
776 | } | |
777 | if(start>=length) { | |
778 | break; /* no more such sequences */ | |
779 | } | |
780 | ||
781 | /* look for the limit of such a sequence (the index behind it) */ | |
782 | for(limit=start; ++limit<length && levels[limit]>=maxLevel;) {} | |
783 | ||
784 | /* | |
785 | * sos=start of sequence, eos=end of sequence | |
786 | * | |
787 | * The closed (inclusive) interval from sos to eos includes all the logical | |
788 | * and visual indexes within this sequence. They are logically and | |
789 | * visually contiguous and in the same range. | |
790 | * | |
791 | * For each run, the new visual index=sos+eos-old visual index; | |
792 | * we pre-add sos+eos into sumOfSosEos -> | |
793 | * new visual index=sumOfSosEos-old visual index; | |
794 | */ | |
795 | sumOfSosEos=start+limit-1; | |
796 | ||
797 | /* reorder each index in the sequence */ | |
798 | do { | |
799 | indexMap[start]=sumOfSosEos-indexMap[start]; | |
800 | } while(++start<limit); | |
801 | ||
802 | /* start==limit */ | |
803 | if(limit==length) { | |
804 | break; /* no more such sequences */ | |
805 | } else { | |
806 | start=limit+1; | |
807 | } | |
808 | } | |
809 | } while(--maxLevel>=minLevel); | |
810 | } | |
811 | ||
812 | U_CAPI void U_EXPORT2 | |
813 | ubidi_reorderVisual(const UBiDiLevel *levels, int32_t length, int32_t *indexMap) { | |
814 | int32_t start, end, limit, temp; | |
815 | UBiDiLevel minLevel, maxLevel; | |
816 | ||
817 | if(indexMap==NULL || !prepareReorder(levels, length, indexMap, &minLevel, &maxLevel)) { | |
818 | return; | |
819 | } | |
820 | ||
821 | /* nothing to do? */ | |
822 | if(minLevel==maxLevel && (minLevel&1)==0) { | |
823 | return; | |
824 | } | |
825 | ||
826 | /* reorder only down to the lowest odd level */ | |
827 | minLevel|=1; | |
828 | ||
829 | /* loop maxLevel..minLevel */ | |
830 | do { | |
831 | start=0; | |
832 | ||
833 | /* loop for all sequences of levels to reorder at the current maxLevel */ | |
834 | for(;;) { | |
835 | /* look for a sequence of levels that are all at >=maxLevel */ | |
836 | /* look for the first index of such a sequence */ | |
837 | while(start<length && levels[start]<maxLevel) { | |
838 | ++start; | |
839 | } | |
840 | if(start>=length) { | |
841 | break; /* no more such runs */ | |
842 | } | |
843 | ||
844 | /* look for the limit of such a sequence (the index behind it) */ | |
845 | for(limit=start; ++limit<length && levels[limit]>=maxLevel;) {} | |
846 | ||
847 | /* | |
848 | * Swap the entire interval of indexes from start to limit-1. | |
849 | * We don't need to swap the levels for the purpose of this | |
850 | * algorithm: the sequence of levels that we look at does not | |
851 | * move anyway. | |
852 | */ | |
853 | end=limit-1; | |
854 | while(start<end) { | |
855 | temp=indexMap[start]; | |
856 | indexMap[start]=indexMap[end]; | |
857 | indexMap[end]=temp; | |
858 | ||
859 | ++start; | |
860 | --end; | |
861 | } | |
862 | ||
863 | if(limit==length) { | |
864 | break; /* no more such sequences */ | |
865 | } else { | |
866 | start=limit+1; | |
867 | } | |
868 | } | |
869 | } while(--maxLevel>=minLevel); | |
870 | } | |
871 | ||
b75a7d8f A |
872 | /* API functions for logical<->visual mapping ------------------------------- */ |
873 | ||
874 | U_CAPI int32_t U_EXPORT2 | |
875 | ubidi_getVisualIndex(UBiDi *pBiDi, int32_t logicalIndex, UErrorCode *pErrorCode) { | |
73c04bcf | 876 | int32_t visualIndex; |
b75a7d8f A |
877 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { |
878 | return 0; | |
73c04bcf | 879 | } else if(!IS_VALID_PARA_OR_LINE(pBiDi)) { |
b75a7d8f A |
880 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
881 | return 0; | |
882 | } else if(logicalIndex<0 || pBiDi->length<=logicalIndex) { | |
883 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
884 | return 0; | |
885 | } else { | |
886 | /* we can do the trivial cases without the runs array */ | |
887 | switch(pBiDi->direction) { | |
888 | case UBIDI_LTR: | |
73c04bcf A |
889 | visualIndex=logicalIndex; |
890 | break; | |
b75a7d8f | 891 | case UBIDI_RTL: |
73c04bcf A |
892 | visualIndex=pBiDi->length-logicalIndex-1; |
893 | break; | |
b75a7d8f A |
894 | default: |
895 | if(pBiDi->runCount<0 && !ubidi_getRuns(pBiDi)) { | |
896 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
897 | return 0; | |
898 | } else { | |
899 | Run *runs=pBiDi->runs; | |
900 | int32_t i, visualStart=0, offset, length; | |
901 | ||
902 | /* linear search for the run, search on the visual runs */ | |
903 | for(i=0;; ++i) { | |
904 | length=runs[i].visualLimit-visualStart; | |
905 | offset=logicalIndex-GET_INDEX(runs[i].logicalStart); | |
906 | if(offset>=0 && offset<length) { | |
907 | if(IS_EVEN_RUN(runs[i].logicalStart)) { | |
908 | /* LTR */ | |
73c04bcf | 909 | visualIndex=visualStart+offset; |
b75a7d8f A |
910 | } else { |
911 | /* RTL */ | |
73c04bcf | 912 | visualIndex=visualStart+length-offset-1; |
b75a7d8f | 913 | } |
73c04bcf | 914 | break; /* exit for loop */ |
b75a7d8f A |
915 | } |
916 | visualStart+=length; | |
917 | } | |
918 | } | |
919 | } | |
920 | } | |
73c04bcf A |
921 | |
922 | if(pBiDi->insertPoints.size>0) { | |
923 | /* add the number of added marks until the calculated visual index */ | |
924 | Run *runs=pBiDi->runs; | |
925 | int32_t i, length, insertRemove; | |
926 | int32_t visualStart=0, markFound=0; | |
927 | for(i=0; ; i++, visualStart+=length) { | |
928 | length=runs[i].visualLimit-visualStart; | |
929 | insertRemove=runs[i].insertRemove; | |
930 | if(insertRemove & (LRM_BEFORE|RLM_BEFORE)) { | |
931 | markFound++; | |
932 | } | |
933 | /* is it the run containing the visual index? */ | |
934 | if(visualIndex<runs[i].visualLimit) { | |
935 | return visualIndex+markFound; | |
936 | } | |
937 | if(insertRemove & (LRM_AFTER|RLM_AFTER)) { | |
938 | markFound++; | |
939 | } | |
940 | } | |
941 | } | |
942 | else if(pBiDi->controlCount>0) { | |
943 | /* subtract the number of controls until the calculated visual index */ | |
944 | Run *runs=pBiDi->runs; | |
945 | int32_t i, j, start, limit, length, insertRemove; | |
946 | int32_t visualStart=0, controlFound=0; | |
947 | UChar uchar=pBiDi->text[logicalIndex]; | |
948 | /* is the logical index pointing to a control ? */ | |
949 | if(IS_BIDI_CONTROL_CHAR(uchar)) { | |
950 | return UBIDI_MAP_NOWHERE; | |
951 | } | |
952 | /* loop on runs */ | |
953 | for(i=0; ; i++, visualStart+=length) { | |
954 | length=runs[i].visualLimit-visualStart; | |
955 | insertRemove=runs[i].insertRemove; | |
956 | /* calculated visual index is beyond this run? */ | |
957 | if(visualIndex>=runs[i].visualLimit) { | |
958 | controlFound-=insertRemove; | |
959 | continue; | |
960 | } | |
961 | /* calculated visual index must be within current run */ | |
962 | if(insertRemove==0) { | |
963 | return visualIndex-controlFound; | |
964 | } | |
965 | if(IS_EVEN_RUN(runs[i].logicalStart)) { | |
966 | /* LTR: check from run start to logical index */ | |
967 | start=runs[i].logicalStart; | |
968 | limit=logicalIndex; | |
969 | } else { | |
970 | /* RTL: check from logical index to run end */ | |
971 | start=logicalIndex+1; | |
972 | limit=runs[i].logicalStart+length; | |
973 | } | |
974 | for(j=start; j<limit; j++) { | |
975 | uchar=pBiDi->text[j]; | |
976 | if(IS_BIDI_CONTROL_CHAR(uchar)) { | |
977 | controlFound++; | |
978 | } | |
979 | } | |
980 | return visualIndex-controlFound; | |
981 | } | |
982 | } | |
983 | ||
984 | return visualIndex; | |
b75a7d8f A |
985 | } |
986 | ||
987 | U_CAPI int32_t U_EXPORT2 | |
988 | ubidi_getLogicalIndex(UBiDi *pBiDi, int32_t visualIndex, UErrorCode *pErrorCode) { | |
73c04bcf A |
989 | Run *runs; |
990 | int32_t i, runCount, start; | |
b75a7d8f A |
991 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { |
992 | return 0; | |
73c04bcf | 993 | } else if(!IS_VALID_PARA_OR_LINE(pBiDi)) { |
b75a7d8f A |
994 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
995 | return 0; | |
73c04bcf | 996 | } else if(visualIndex<0 || pBiDi->resultLength<=visualIndex) { |
b75a7d8f A |
997 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; |
998 | return 0; | |
73c04bcf A |
999 | } |
1000 | /* we can do the trivial cases without the runs array */ | |
1001 | if(pBiDi->insertPoints.size==0 && pBiDi->controlCount==0) { | |
1002 | if(pBiDi->direction==UBIDI_LTR) { | |
b75a7d8f | 1003 | return visualIndex; |
73c04bcf A |
1004 | } |
1005 | else if(pBiDi->direction==UBIDI_RTL) { | |
b75a7d8f | 1006 | return pBiDi->length-visualIndex-1; |
73c04bcf A |
1007 | } |
1008 | if(pBiDi->runCount<0 && !ubidi_getRuns(pBiDi)) { | |
1009 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; | |
1010 | return 0; | |
1011 | } | |
1012 | } | |
b75a7d8f | 1013 | |
73c04bcf A |
1014 | runs=pBiDi->runs; |
1015 | runCount=pBiDi->runCount; | |
1016 | if(pBiDi->insertPoints.size>0) { | |
1017 | /* handle inserted LRM/RLM */ | |
1018 | int32_t markFound=0, insertRemove; | |
1019 | int32_t visualStart=0, length; | |
1020 | runs=pBiDi->runs; | |
1021 | /* subtract number of marks until visual index */ | |
1022 | for(i=0; ; i++, visualStart+=length) { | |
1023 | length=runs[i].visualLimit-visualStart; | |
1024 | insertRemove=runs[i].insertRemove; | |
1025 | if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { | |
1026 | if(visualIndex<=(visualStart+markFound)) { | |
1027 | return UBIDI_MAP_NOWHERE; | |
b75a7d8f | 1028 | } |
73c04bcf A |
1029 | markFound++; |
1030 | } | |
1031 | /* is adjusted visual index within this run? */ | |
1032 | if(visualIndex<(runs[i].visualLimit+markFound)) { | |
1033 | visualIndex-=markFound; | |
1034 | break; | |
1035 | } | |
1036 | if(insertRemove&(LRM_AFTER|RLM_AFTER)) { | |
1037 | if(visualIndex==(visualStart+length+markFound)) { | |
1038 | return UBIDI_MAP_NOWHERE; | |
1039 | } | |
1040 | markFound++; | |
1041 | } | |
1042 | } | |
1043 | } | |
1044 | else if(pBiDi->controlCount>0) { | |
1045 | /* handle removed BiDi control characters */ | |
1046 | int32_t controlFound=0, insertRemove, length; | |
1047 | int32_t logicalStart, logicalEnd, visualStart=0, j, k; | |
1048 | UChar uchar; | |
1049 | UBool evenRun; | |
1050 | /* add number of controls until visual index */ | |
1051 | for(i=0; ; i++, visualStart+=length) { | |
1052 | length=runs[i].visualLimit-visualStart; | |
1053 | insertRemove=runs[i].insertRemove; | |
1054 | /* is adjusted visual index beyond current run? */ | |
1055 | if(visualIndex>=(runs[i].visualLimit-controlFound+insertRemove)) { | |
1056 | controlFound-=insertRemove; | |
1057 | continue; | |
1058 | } | |
1059 | /* adjusted visual index is within current run */ | |
1060 | if(insertRemove==0) { | |
1061 | visualIndex+=controlFound; | |
1062 | break; | |
1063 | } | |
1064 | /* count non-control chars until visualIndex */ | |
1065 | logicalStart=runs[i].logicalStart; | |
1066 | evenRun=IS_EVEN_RUN(logicalStart); | |
1067 | REMOVE_ODD_BIT(logicalStart); | |
1068 | logicalEnd=logicalStart+length-1; | |
1069 | for(j=0; j<length; j++) { | |
1070 | k= evenRun ? logicalStart+j : logicalEnd-j; | |
1071 | uchar=pBiDi->text[k]; | |
1072 | if(IS_BIDI_CONTROL_CHAR(uchar)) { | |
1073 | controlFound++; | |
1074 | } | |
1075 | if((visualIndex+controlFound)==(visualStart+j)) { | |
1076 | break; | |
b75a7d8f A |
1077 | } |
1078 | } | |
73c04bcf A |
1079 | visualIndex+=controlFound; |
1080 | break; | |
b75a7d8f A |
1081 | } |
1082 | } | |
73c04bcf A |
1083 | /* handle all cases */ |
1084 | if(runCount<=10) { | |
1085 | /* linear search for the run */ | |
1086 | for(i=0; visualIndex>=runs[i].visualLimit; ++i) {} | |
1087 | } else { | |
1088 | /* binary search for the run */ | |
1089 | int32_t begin=0, limit=runCount; | |
1090 | ||
1091 | /* the middle if() is guaranteed to find the run, we don't need a loop limit */ | |
1092 | for(;;) { | |
1093 | i=(begin+limit)/2; | |
1094 | if(visualIndex>=runs[i].visualLimit) { | |
1095 | begin=i+1; | |
1096 | } else if(i==0 || visualIndex>=runs[i-1].visualLimit) { | |
1097 | break; | |
1098 | } else { | |
1099 | limit=i; | |
1100 | } | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | start=runs[i].logicalStart; | |
1105 | if(IS_EVEN_RUN(start)) { | |
1106 | /* LTR */ | |
1107 | /* the offset in runs[i] is visualIndex-runs[i-1].visualLimit */ | |
1108 | if(i>0) { | |
1109 | visualIndex-=runs[i-1].visualLimit; | |
1110 | } | |
1111 | return start+visualIndex; | |
1112 | } else { | |
1113 | /* RTL */ | |
1114 | return GET_INDEX(start)+runs[i].visualLimit-visualIndex-1; | |
1115 | } | |
b75a7d8f A |
1116 | } |
1117 | ||
1118 | U_CAPI void U_EXPORT2 | |
1119 | ubidi_getLogicalMap(UBiDi *pBiDi, int32_t *indexMap, UErrorCode *pErrorCode) { | |
73c04bcf | 1120 | const UBiDiLevel *levels; |
b75a7d8f A |
1121 | |
1122 | /* ubidi_getLevels() checks all of its and our arguments */ | |
73c04bcf | 1123 | if((levels=ubidi_getLevels(pBiDi, pErrorCode))==NULL) { |
b75a7d8f A |
1124 | /* no op */ |
1125 | } else if(indexMap==NULL) { | |
1126 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1127 | } else { | |
1128 | ubidi_reorderLogical(levels, pBiDi->length, indexMap); | |
73c04bcf A |
1129 | |
1130 | if(pBiDi->insertPoints.size>0) { | |
1131 | int32_t markFound=0, runCount=pBiDi->runCount; | |
1132 | int32_t visualStart=0, length, insertRemove, i, j; | |
1133 | Run *runs=pBiDi->runs; | |
1134 | /* add number of marks found until each index */ | |
1135 | for(i=0; i<runCount; i++, visualStart+=length) { | |
1136 | length=runs[i].visualLimit-visualStart; | |
1137 | insertRemove=runs[i].insertRemove; | |
1138 | if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { | |
1139 | markFound++; | |
1140 | } | |
1141 | if(markFound>0) { | |
1142 | int32_t logicalStart=GET_INDEX(runs[i].logicalStart); | |
1143 | int32_t limit=logicalStart+length; | |
1144 | for(j=logicalStart; j<limit; j++) { | |
1145 | indexMap[j]+=markFound; | |
1146 | } | |
1147 | } | |
1148 | if(insertRemove&(LRM_AFTER|RLM_AFTER)) { | |
1149 | markFound++; | |
1150 | } | |
1151 | } | |
1152 | } | |
1153 | else if(pBiDi->controlCount>0) { | |
1154 | int32_t controlFound=0, runCount=pBiDi->runCount; | |
1155 | int32_t visualStart=0, length, insertRemove, i, j, k; | |
1156 | int32_t logicalStart, logicalEnd; | |
1157 | UBool evenRun; | |
1158 | UChar uchar; | |
1159 | Run *runs=pBiDi->runs; | |
1160 | /* subtract number of controls found until each index */ | |
1161 | for(i=0; i<runCount; i++, visualStart+=length) { | |
1162 | length=runs[i].visualLimit-visualStart; | |
1163 | insertRemove=runs[i].insertRemove; | |
1164 | /* no control found within previous runs nor within this run */ | |
1165 | if((controlFound-insertRemove)==0) { | |
1166 | continue; | |
1167 | } | |
1168 | logicalStart=runs[i].logicalStart; | |
1169 | evenRun=IS_EVEN_RUN(logicalStart); | |
1170 | REMOVE_ODD_BIT(logicalStart); | |
1171 | logicalEnd=logicalStart+length-1; | |
1172 | /* if no control within this run */ | |
1173 | if(insertRemove==0) { | |
1174 | for(j=logicalStart; j<=logicalEnd; j++) { | |
1175 | indexMap[j]-=controlFound; | |
1176 | } | |
1177 | continue; | |
1178 | } | |
1179 | for(j=0; j<length; j++) { | |
1180 | k= evenRun ? logicalStart+j : logicalEnd-j; | |
1181 | uchar=pBiDi->text[k]; | |
1182 | if(IS_BIDI_CONTROL_CHAR(uchar)) { | |
1183 | controlFound++; | |
1184 | indexMap[k]=UBIDI_MAP_NOWHERE; | |
1185 | continue; | |
1186 | } | |
1187 | indexMap[k]-=controlFound; | |
1188 | } | |
1189 | } | |
1190 | } | |
b75a7d8f A |
1191 | } |
1192 | } | |
1193 | ||
1194 | U_CAPI void U_EXPORT2 | |
1195 | ubidi_getVisualMap(UBiDi *pBiDi, int32_t *indexMap, UErrorCode *pErrorCode) { | |
1196 | /* ubidi_countRuns() checks all of its and our arguments */ | |
1197 | if(ubidi_countRuns(pBiDi, pErrorCode)<=0) { | |
1198 | /* no op */ | |
1199 | } else if(indexMap==NULL) { | |
1200 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1201 | } else { | |
1202 | /* fill a visual-to-logical index map using the runs[] */ | |
1203 | Run *runs=pBiDi->runs, *runsLimit=runs+pBiDi->runCount; | |
73c04bcf | 1204 | int32_t logicalStart, visualStart, visualLimit, *pi=indexMap; |
b75a7d8f A |
1205 | |
1206 | visualStart=0; | |
1207 | for(; runs<runsLimit; ++runs) { | |
1208 | logicalStart=runs->logicalStart; | |
1209 | visualLimit=runs->visualLimit; | |
1210 | if(IS_EVEN_RUN(logicalStart)) { | |
1211 | do { /* LTR */ | |
73c04bcf | 1212 | *pi++ = logicalStart++; |
b75a7d8f A |
1213 | } while(++visualStart<visualLimit); |
1214 | } else { | |
1215 | REMOVE_ODD_BIT(logicalStart); | |
1216 | logicalStart+=visualLimit-visualStart; /* logicalLimit */ | |
1217 | do { /* RTL */ | |
73c04bcf | 1218 | *pi++ = --logicalStart; |
b75a7d8f A |
1219 | } while(++visualStart<visualLimit); |
1220 | } | |
1221 | /* visualStart==visualLimit; */ | |
1222 | } | |
73c04bcf A |
1223 | |
1224 | if(pBiDi->insertPoints.size>0) { | |
1225 | int32_t markFound=0, runCount=pBiDi->runCount; | |
1226 | int32_t insertRemove, i, j, k; | |
1227 | runs=pBiDi->runs; | |
1228 | /* count all inserted marks */ | |
1229 | for(i=0; i<runCount; i++) { | |
1230 | insertRemove=runs[i].insertRemove; | |
1231 | if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { | |
1232 | markFound++; | |
1233 | } | |
1234 | if(insertRemove&(LRM_AFTER|RLM_AFTER)) { | |
1235 | markFound++; | |
1236 | } | |
1237 | } | |
1238 | /* move back indexes by number of preceding marks */ | |
1239 | k=pBiDi->resultLength; | |
1240 | for(i=runCount-1; i>=0 && markFound>0; i--) { | |
1241 | insertRemove=runs[i].insertRemove; | |
1242 | if(insertRemove&(LRM_AFTER|RLM_AFTER)) { | |
1243 | indexMap[--k]= UBIDI_MAP_NOWHERE; | |
1244 | markFound--; | |
1245 | } | |
1246 | visualStart= i>0 ? runs[i-1].visualLimit : 0; | |
1247 | for(j=runs[i].visualLimit-1; j>=visualStart && markFound>0; j--) { | |
1248 | indexMap[--k]=indexMap[j]; | |
1249 | } | |
1250 | if(insertRemove&(LRM_BEFORE|RLM_BEFORE)) { | |
1251 | indexMap[--k]= UBIDI_MAP_NOWHERE; | |
1252 | markFound--; | |
1253 | } | |
1254 | } | |
1255 | } | |
1256 | else if(pBiDi->controlCount>0) { | |
1257 | int32_t runCount=pBiDi->runCount, logicalEnd; | |
1258 | int32_t insertRemove, length, i, j, k, m; | |
1259 | UChar uchar; | |
1260 | UBool evenRun; | |
1261 | runs=pBiDi->runs; | |
1262 | visualStart=0; | |
1263 | /* move forward indexes by number of preceding controls */ | |
1264 | k=0; | |
1265 | for(i=0; i<runCount; i++, visualStart+=length) { | |
1266 | length=runs[i].visualLimit-visualStart; | |
1267 | insertRemove=runs[i].insertRemove; | |
1268 | /* if no control found yet, nothing to do in this run */ | |
1269 | if((insertRemove==0)&&(k==visualStart)) { | |
1270 | k+=length; | |
1271 | continue; | |
1272 | } | |
1273 | /* if no control in this run */ | |
1274 | if(insertRemove==0) { | |
1275 | visualLimit=runs[i].visualLimit; | |
1276 | for(j=visualStart; j<visualLimit; j++) { | |
1277 | indexMap[k++]=indexMap[j]; | |
1278 | } | |
1279 | continue; | |
1280 | } | |
1281 | logicalStart=runs[i].logicalStart; | |
1282 | evenRun=IS_EVEN_RUN(logicalStart); | |
1283 | REMOVE_ODD_BIT(logicalStart); | |
1284 | logicalEnd=logicalStart+length-1; | |
1285 | for(j=0; j<length; j++) { | |
1286 | m= evenRun ? logicalStart+j : logicalEnd-j; | |
1287 | uchar=pBiDi->text[m]; | |
1288 | if(!IS_BIDI_CONTROL_CHAR(uchar)) { | |
1289 | indexMap[k++]=m; | |
1290 | } | |
1291 | } | |
1292 | } | |
1293 | } | |
b75a7d8f A |
1294 | } |
1295 | } | |
1296 | ||
1297 | U_CAPI void U_EXPORT2 | |
1298 | ubidi_invertMap(const int32_t *srcMap, int32_t *destMap, int32_t length) { | |
73c04bcf A |
1299 | if(srcMap!=NULL && destMap!=NULL && length>0) { |
1300 | const int32_t *pi; | |
1301 | int32_t destLength=-1, count=0; | |
1302 | /* find highest value and count positive indexes in srcMap */ | |
1303 | pi=srcMap+length; | |
1304 | while(pi>srcMap) { | |
1305 | if(*--pi>destLength) { | |
1306 | destLength=*pi; | |
1307 | } | |
1308 | if(*pi>=0) { | |
1309 | count++; | |
1310 | } | |
1311 | } | |
1312 | destLength++; /* add 1 for origin 0 */ | |
1313 | if(count<destLength) { | |
1314 | /* we must fill unmatched destMap entries with -1 */ | |
1315 | uprv_memset(destMap, 0xFF, destLength*sizeof(int32_t)); | |
1316 | } | |
1317 | pi=srcMap+length; | |
b75a7d8f | 1318 | while(length>0) { |
73c04bcf A |
1319 | if(*--pi>=0) { |
1320 | destMap[*pi]=--length; | |
1321 | } else { | |
1322 | --length; | |
1323 | } | |
b75a7d8f A |
1324 | } |
1325 | } | |
1326 | } |