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