]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/BidiLine.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / BidiLine.java
1 /*\r
2 *******************************************************************************\r
3 *   Copyright (C) 2001-2007, 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         /* we should never get here */\r
460         throw new IllegalStateException("Internal ICU error in getRunFromLogicalIndex");\r
461     }\r
462 \r
463     /*\r
464      * Compute the runs array from the levels array.\r
465      * After getRuns() returns true, runCount is guaranteed to be >0\r
466      * and the runs are reordered.\r
467      * Odd-level runs have visualStart on their visual right edge and\r
468      * they progress visually to the left.\r
469      * If option OPTION_INSERT_MARKS is set, insertRemove will contain the\r
470      * sum of appropriate LRM/RLM_BEFORE/AFTER flags.\r
471      * If option OPTION_REMOVE_CONTROLS is set, insertRemove will contain the\r
472      * negative number of BiDi control characters within this run.\r
473      */\r
474     static void getRuns(Bidi bidi) {\r
475         /*\r
476          * This method returns immediately if the runs are already set. This\r
477          * includes the case of length==0 (handled in setPara)..\r
478          */\r
479         if (bidi.runCount >= 0) {\r
480             return;\r
481         }\r
482         if (bidi.direction != Bidi.MIXED) {\r
483             /* simple, single-run case - this covers length==0 */\r
484             /* bidi.paraLevel is ok even for contextual multiple paragraphs */\r
485             getSingleRun(bidi, bidi.paraLevel);\r
486         } else /* Bidi.MIXED, length>0 */ {\r
487             /* mixed directionality */\r
488             int length = bidi.length, limit;\r
489             byte[] levels = bidi.levels;\r
490             int i, runCount;\r
491             byte level = Bidi.LEVEL_DEFAULT_LTR;   /* initialize with no valid level */\r
492             /*\r
493              * If there are WS characters at the end of the line\r
494              * and the run preceding them has a level different from\r
495              * paraLevel, then they will form their own run at paraLevel (L1).\r
496              * Count them separately.\r
497              * We need some special treatment for this in order to not\r
498              * modify the levels array which a line Bidi object shares\r
499              * with its paragraph parent and its other line siblings.\r
500              * In other words, for the trailing WS, it may be\r
501              * levels[]!=paraLevel but we have to treat it like it were so.\r
502              */\r
503             limit = bidi.trailingWSStart;\r
504             /* count the runs, there is at least one non-WS run, and limit>0 */\r
505             runCount = 0;\r
506             for (i = 0; i < limit; ++i) {\r
507                 /* increment runCount at the start of each run */\r
508                 if (levels[i] != level) {\r
509                     ++runCount;\r
510                     level = levels[i];\r
511                 }\r
512             }\r
513 \r
514             /*\r
515              * We don't need to see if the last run can be merged with a trailing\r
516              * WS run because setTrailingWSStart() would have done that.\r
517              */\r
518             if (runCount == 1 && limit == length) {\r
519                 /* There is only one non-WS run and no trailing WS-run. */\r
520                 getSingleRun(bidi, levels[0]);\r
521             } else /* runCount>1 || limit<length */ {\r
522                 /* allocate and set the runs */\r
523                 BidiRun[] runs;\r
524                 int runIndex, start;\r
525                 byte minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;\r
526                 byte maxLevel=0;\r
527 \r
528                 /* now, count a (non-mergeable) WS run */\r
529                 if (limit < length) {\r
530                     ++runCount;\r
531                 }\r
532 \r
533                 /* runCount > 1 */\r
534                 bidi.getRunsMemory(runCount);\r
535                 runs = bidi.runsMemory;\r
536 \r
537                 /* set the runs */\r
538                 /* FOOD FOR THOUGHT: this could be optimized, e.g.:\r
539                  * 464->444, 484->444, 575->555, 595->555\r
540                  * However, that would take longer. Check also how it would\r
541                  * interact with BiDi control removal and inserting Marks.\r
542                  */\r
543                 runIndex = 0;\r
544 \r
545                 /* search for the run limits and initialize visualLimit values with the run lengths */\r
546                 i = 0;\r
547                 do {\r
548                     /* prepare this run */\r
549                     start = i;\r
550                     level = levels[i];\r
551                     if (level < minLevel) {\r
552                         minLevel = level;\r
553                     }\r
554                     if (level > maxLevel) {\r
555                         maxLevel = level;\r
556                     }\r
557 \r
558                     /* look for the run limit */\r
559                     while (++i < limit && levels[i] == level) {}\r
560 \r
561                     /* i is another run limit */\r
562                     runs[runIndex] = new BidiRun(start, i - start, level);\r
563                     ++runIndex;\r
564                 } while (i < limit);\r
565 \r
566                 if (limit < length) {\r
567                     /* there is a separate WS run */\r
568                     runs[runIndex] = new BidiRun(limit, length - limit, bidi.paraLevel);\r
569                     /* For the trailing WS run, bidi.paraLevel is ok even\r
570                        if contextual multiple paragraphs.                   */\r
571                     if (bidi.paraLevel < minLevel) {\r
572                         minLevel = bidi.paraLevel;\r
573                     }\r
574                 }\r
575 \r
576                 /* set the object fields */\r
577                 bidi.runs = runs;\r
578                 bidi.runCount = runCount;\r
579 \r
580                 reorderLine(bidi, minLevel, maxLevel);\r
581 \r
582                 /* now add the direction flags and adjust the visualLimit's to be just that */\r
583                 /* this loop will also handle the trailing WS run */\r
584                 limit = 0;\r
585                 for (i = 0; i < runCount; ++i) {\r
586                     runs[i].level = levels[runs[i].start];\r
587                     limit = (runs[i].limit += limit);\r
588                 }\r
589 \r
590                 /* Set the embedding level for the trailing WS run. */\r
591                 /* For a RTL paragraph, it will be the *first* run in visual order. */\r
592                 /* For the trailing WS run, bidi.paraLevel is ok even if\r
593                    contextual multiple paragraphs.                          */\r
594                 if (runIndex < runCount) {\r
595                     int trailingRun = ((bidi.paraLevel & 1) != 0)? 0 : runIndex;\r
596                     runs[trailingRun].level = bidi.paraLevel;\r
597                 }\r
598             }\r
599         }\r
600 \r
601         /* handle insert LRM/RLM BEFORE/AFTER run */\r
602         if (bidi.insertPoints.size > 0) {\r
603             Bidi.Point point;\r
604             int runIndex, ip;\r
605             for (ip = 0; ip < bidi.insertPoints.size; ip++) {\r
606                 point = bidi.insertPoints.points[ip];\r
607                 runIndex = getRunFromLogicalIndex(bidi, point.pos);\r
608                 bidi.runs[runIndex].insertRemove |= point.flag;\r
609             }\r
610         }\r
611 \r
612         /* handle remove BiDi control characters */\r
613         if (bidi.controlCount > 0) {\r
614             int runIndex, ic;\r
615             char c;\r
616             for (ic = 0; ic < bidi.length; ic++) {\r
617                 c = bidi.text[ic];\r
618                 if (Bidi.IsBidiControlChar(c)) {\r
619                     runIndex = getRunFromLogicalIndex(bidi, ic);\r
620                     bidi.runs[runIndex].insertRemove--;\r
621                 }\r
622             }\r
623         }\r
624     }\r
625 \r
626     static int[] prepareReorder(byte[] levels, byte[] pMinLevel, byte[] pMaxLevel)\r
627     {\r
628         int start;\r
629         byte level, minLevel, maxLevel;\r
630 \r
631         if (levels == null || levels.length <= 0) {\r
632             return null;\r
633         }\r
634 \r
635         /* determine minLevel and maxLevel */\r
636         minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;\r
637         maxLevel = 0;\r
638         for (start = levels.length; start>0; ) {\r
639             level = levels[--start];\r
640             if (level > Bidi.MAX_EXPLICIT_LEVEL + 1) {\r
641                 return null;\r
642             }\r
643             if (level < minLevel) {\r
644                 minLevel = level;\r
645             }\r
646             if (level > maxLevel) {\r
647                 maxLevel = level;\r
648             }\r
649         }\r
650         pMinLevel[0] = minLevel;\r
651         pMaxLevel[0] = maxLevel;\r
652 \r
653         /* initialize the index map */\r
654         int[] indexMap = new int[levels.length];\r
655         for (start = levels.length; start > 0; ) {\r
656             --start;\r
657             indexMap[start] = start;\r
658         }\r
659 \r
660         return indexMap;\r
661     }\r
662 \r
663     static int[] reorderLogical(byte[] levels)\r
664     {\r
665         byte[] aMinLevel = new byte[1];\r
666         byte[] aMaxLevel = new byte[1];\r
667         int start, limit, sumOfSosEos;\r
668         byte minLevel, maxLevel;\r
669         int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);\r
670         if (indexMap == null) {\r
671             return null;\r
672         }\r
673 \r
674         minLevel = aMinLevel[0];\r
675         maxLevel = aMaxLevel[0];\r
676 \r
677         /* nothing to do? */\r
678         if (minLevel == maxLevel && (minLevel & 1) == 0) {\r
679             return indexMap;\r
680         }\r
681 \r
682         /* reorder only down to the lowest odd level */\r
683         minLevel |= 1;\r
684 \r
685         /* loop maxLevel..minLevel */\r
686         do {\r
687             start = 0;\r
688 \r
689             /* loop for all sequences of levels to reorder at the current maxLevel */\r
690             for ( ; ; ) {\r
691                 /* look for a sequence of levels that are all at >=maxLevel */\r
692                 /* look for the first index of such a sequence */\r
693                 while (start < levels.length && levels[start] < maxLevel) {\r
694                     ++start;\r
695                 }\r
696                 if (start >= levels.length) {\r
697                     break;  /* no more such sequences */\r
698                 }\r
699 \r
700                 /* look for the limit of such a sequence (the index behind it) */\r
701                 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}\r
702 \r
703                 /*\r
704                  * sos=start of sequence, eos=end of sequence\r
705                  *\r
706                  * The closed (inclusive) interval from sos to eos includes all the logical\r
707                  * and visual indexes within this sequence. They are logically and\r
708                  * visually contiguous and in the same range.\r
709                  *\r
710                  * For each run, the new visual index=sos+eos-old visual index;\r
711                  * we pre-add sos+eos into sumOfSosEos ->\r
712                  * new visual index=sumOfSosEos-old visual index;\r
713                  */\r
714                 sumOfSosEos = start + limit - 1;\r
715 \r
716                 /* reorder each index in the sequence */\r
717                 do {\r
718                     indexMap[start] = sumOfSosEos - indexMap[start];\r
719                 } while (++start < limit);\r
720 \r
721                 /* start==limit */\r
722                 if (limit == levels.length) {\r
723                     break;  /* no more such sequences */\r
724                 } else {\r
725                     start = limit + 1;\r
726                 }\r
727             }\r
728         } while (--maxLevel >= minLevel);\r
729         return indexMap;\r
730     }\r
731 \r
732     static int[] reorderVisual(byte[] levels)\r
733     {\r
734         byte[] aMinLevel = new byte[1];\r
735         byte[] aMaxLevel = new byte[1];\r
736         int start, end, limit, temp;\r
737         byte minLevel, maxLevel;\r
738 \r
739         int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);\r
740         if (indexMap == null) {\r
741             return null;\r
742         }\r
743 \r
744         minLevel = aMinLevel[0];\r
745         maxLevel = aMaxLevel[0];\r
746 \r
747         /* nothing to do? */\r
748         if (minLevel == maxLevel && (minLevel & 1) == 0) {\r
749             return indexMap;\r
750         }\r
751 \r
752         /* reorder only down to the lowest odd level */\r
753         minLevel |= 1;\r
754 \r
755         /* loop maxLevel..minLevel */\r
756         do {\r
757             start = 0;\r
758 \r
759             /* loop for all sequences of levels to reorder at the current maxLevel */\r
760             for ( ; ; ) {\r
761                 /* look for a sequence of levels that are all at >=maxLevel */\r
762                 /* look for the first index of such a sequence */\r
763                 while (start < levels.length && levels[start] < maxLevel) {\r
764                     ++start;\r
765                 }\r
766                 if (start >= levels.length) {\r
767                     break;  /* no more such runs */\r
768                 }\r
769 \r
770                 /* look for the limit of such a sequence (the index behind it) */\r
771                 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}\r
772 \r
773                 /*\r
774                  * Swap the entire interval of indexes from start to limit-1.\r
775                  * We don't need to swap the levels for the purpose of this\r
776                  * algorithm: the sequence of levels that we look at does not\r
777                  * move anyway.\r
778                  */\r
779                 end = limit - 1;\r
780                 while (start < end) {\r
781                     temp = indexMap[start];\r
782                     indexMap[start] = indexMap[end];\r
783                     indexMap[end] = temp;\r
784 \r
785                     ++start;\r
786                     --end;\r
787                 }\r
788 \r
789                 if (limit == levels.length) {\r
790                     break;  /* no more such sequences */\r
791                 } else {\r
792                     start = limit + 1;\r
793                 }\r
794             }\r
795         } while (--maxLevel >= minLevel);\r
796 \r
797         return indexMap;\r
798     }\r
799 \r
800     static int getVisualIndex(Bidi bidi, int logicalIndex)\r
801     {\r
802         int visualIndex = Bidi.MAP_NOWHERE;\r
803 \r
804         /* we can do the trivial cases without the runs array */\r
805         switch(bidi.direction) {\r
806         case Bidi.LTR:\r
807             visualIndex = logicalIndex;\r
808             break;\r
809         case Bidi.RTL:\r
810             visualIndex = bidi.length - logicalIndex - 1;\r
811             break;\r
812         default:\r
813             getRuns(bidi);\r
814             BidiRun[] runs = bidi.runs;\r
815             int i, visualStart = 0, offset, length;\r
816 \r
817             /* linear search for the run, search on the visual runs */\r
818             for (i = 0; i < bidi.runCount; ++i) {\r
819                 length = runs[i].limit - visualStart;\r
820                 offset = logicalIndex - runs[i].start;\r
821                 if (offset >= 0 && offset < length) {\r
822                     if (runs[i].isEvenRun()) {\r
823                         /* LTR */\r
824                         visualIndex = visualStart + offset;\r
825                     } else {\r
826                         /* RTL */\r
827                         visualIndex = visualStart + length - offset - 1;\r
828                     }\r
829                     break;                  /* exit for loop */\r
830                 }\r
831                 visualStart += length;\r
832             }\r
833             if (i >= bidi.runCount) {\r
834                 return Bidi.MAP_NOWHERE;\r
835             }\r
836         }\r
837 \r
838         if (bidi.insertPoints.size > 0) {\r
839             /* add the number of added marks until the calculated visual index */\r
840             BidiRun runs[] = bidi.runs;\r
841             int i, length, insertRemove;\r
842             int visualStart = 0, markFound = 0;\r
843             for (i = 0; ; i++, visualStart += length) {\r
844                 length = runs[i].limit - visualStart;\r
845                 insertRemove = runs[i].insertRemove;\r
846                 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {\r
847                     markFound++;\r
848                 }\r
849                 /* is it the run containing the visual index? */\r
850                 if (visualIndex < runs[i].limit) {\r
851                     return visualIndex + markFound;\r
852                 }\r
853                 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {\r
854                     markFound++;\r
855                 }\r
856             }\r
857         }\r
858         else if (bidi.controlCount > 0) {\r
859             /* subtract the number of controls until the calculated visual index */\r
860             BidiRun[] runs = bidi.runs;\r
861             int i, j, start, limit, length, insertRemove;\r
862             int visualStart = 0, controlFound = 0;\r
863             char uchar = bidi.text[logicalIndex];\r
864             /* is the logical index pointing to a control ? */\r
865             if (Bidi.IsBidiControlChar(uchar)) {\r
866                 return Bidi.MAP_NOWHERE;\r
867             }\r
868             /* loop on runs */\r
869             for (i = 0; ; i++, visualStart += length) {\r
870                 length = runs[i].limit - visualStart;\r
871                 insertRemove = runs[i].insertRemove;\r
872                 /* calculated visual index is beyond this run? */\r
873                 if (visualIndex >= runs[i].limit) {\r
874                     controlFound -= insertRemove;\r
875                     continue;\r
876                 }\r
877                 /* calculated visual index must be within current run */\r
878                 if (insertRemove == 0) {\r
879                     return visualIndex - controlFound;\r
880                 }\r
881                 if (runs[i].isEvenRun()) {\r
882                     /* LTR: check from run start to logical index */\r
883                     start = runs[i].start;\r
884                     limit = logicalIndex;\r
885                 } else {\r
886                     /* RTL: check from logical index to run end */\r
887                     start = logicalIndex + 1;\r
888                     limit = runs[i].start + length;\r
889                 }\r
890                 for (j = start; j < limit; j++) {\r
891                     uchar = bidi.text[j];\r
892                     if (Bidi.IsBidiControlChar(uchar)) {\r
893                         controlFound++;\r
894                     }\r
895                 }\r
896                 return visualIndex - controlFound;\r
897             }\r
898         }\r
899 \r
900         return visualIndex;\r
901     }\r
902 \r
903     static int getLogicalIndex(Bidi bidi, int visualIndex)\r
904     {\r
905         BidiRun[] runs;\r
906         int i, runCount, start;\r
907 \r
908         runs = bidi.runs;\r
909         runCount = bidi.runCount;\r
910         if (bidi.insertPoints.size > 0) {\r
911             /* handle inserted LRM/RLM */\r
912             int markFound = 0, insertRemove;\r
913             int visualStart = 0, length;\r
914             /* subtract number of marks until visual index */\r
915             for (i = 0; ; i++, visualStart += length) {\r
916                 length = runs[i].limit - visualStart;\r
917                 insertRemove = runs[i].insertRemove;\r
918                 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {\r
919                     if (visualIndex <= (visualStart+markFound)) {\r
920                         return Bidi.MAP_NOWHERE;\r
921                     }\r
922                     markFound++;\r
923                 }\r
924                 /* is adjusted visual index within this run? */\r
925                 if (visualIndex < (runs[i].limit + markFound)) {\r
926                     visualIndex -= markFound;\r
927                     break;\r
928                 }\r
929                 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {\r
930                     if (visualIndex == (visualStart + length + markFound)) {\r
931                         return Bidi.MAP_NOWHERE;\r
932                     }\r
933                     markFound++;\r
934                 }\r
935             }\r
936         }\r
937         else if (bidi.controlCount > 0) {\r
938             /* handle removed BiDi control characters */\r
939             int controlFound = 0, insertRemove, length;\r
940             int logicalStart, logicalEnd, visualStart = 0, j, k;\r
941             char uchar;\r
942             boolean evenRun;\r
943             /* add number of controls until visual index */\r
944             for (i = 0; ; i++, visualStart += length) {\r
945                 length = runs[i].limit - visualStart;\r
946                 insertRemove = runs[i].insertRemove;\r
947                 /* is adjusted visual index beyond current run? */\r
948                 if (visualIndex >= (runs[i].limit - controlFound + insertRemove)) {\r
949                     controlFound -= insertRemove;\r
950                     continue;\r
951                 }\r
952                 /* adjusted visual index is within current run */\r
953                 if (insertRemove == 0) {\r
954                     visualIndex += controlFound;\r
955                     break;\r
956                 }\r
957                 /* count non-control chars until visualIndex */\r
958                 logicalStart = runs[i].start;\r
959                 evenRun = runs[i].isEvenRun();\r
960                 logicalEnd = logicalStart + length - 1;\r
961                 for (j = 0; j < length; j++) {\r
962                     k= evenRun ? logicalStart+j : logicalEnd-j;\r
963                     uchar = bidi.text[k];\r
964                     if (Bidi.IsBidiControlChar(uchar)) {\r
965                         controlFound++;\r
966                     }\r
967                     if ((visualIndex + controlFound) == (visualStart + j)) {\r
968                         break;\r
969                     }\r
970                 }\r
971                 visualIndex += controlFound;\r
972                 break;\r
973             }\r
974         }\r
975         /* handle all cases */\r
976         if (runCount <= 10) {\r
977             /* linear search for the run */\r
978             for (i = 0; visualIndex >= runs[i].limit; ++i) {}\r
979         } else {\r
980             /* binary search for the run */\r
981             int begin = 0, limit = runCount;\r
982 \r
983             /* the middle if() is guaranteed to find the run, we don't need a loop limit */\r
984             for ( ; ; ) {\r
985                 i = (begin + limit) / 2;\r
986                 if (visualIndex >= runs[i].limit) {\r
987                     begin = i + 1;\r
988                 } else if (i==0 || visualIndex >= runs[i-1].limit) {\r
989                     break;\r
990                 } else {\r
991                     limit = i;\r
992                 }\r
993             }\r
994         }\r
995 \r
996         start= runs[i].start;\r
997         if (runs[i].isEvenRun()) {\r
998             /* LTR */\r
999             /* the offset in runs[i] is visualIndex-runs[i-1].visualLimit */\r
1000             if (i > 0) {\r
1001                 visualIndex -= runs[i - 1].limit;\r
1002             }\r
1003             return start + visualIndex;\r
1004         } else {\r
1005             /* RTL */\r
1006             return start + runs[i].limit - visualIndex - 1;\r
1007         }\r
1008     }\r
1009 \r
1010     static int[] getLogicalMap(Bidi bidi)\r
1011     {\r
1012         /* fill a logical-to-visual index map using the runs[] */\r
1013         BidiRun[] runs = bidi.runs;\r
1014         int logicalStart, visualStart, logicalLimit, visualLimit;\r
1015         int[] indexMap = new int[bidi.length];\r
1016         if (bidi.length > bidi.resultLength) {\r
1017             Arrays.fill(indexMap, Bidi.MAP_NOWHERE);\r
1018         }\r
1019 \r
1020         visualStart = 0;\r
1021         for (int j = 0; j < bidi.runCount; ++j) {\r
1022             logicalStart = runs[j].start;\r
1023             visualLimit = runs[j].limit;\r
1024             if (runs[j].isEvenRun()) {\r
1025                 do { /* LTR */\r
1026                     indexMap[logicalStart++] = visualStart++;\r
1027                 } while (visualStart < visualLimit);\r
1028             } else {\r
1029                 logicalStart += visualLimit - visualStart;  /* logicalLimit */\r
1030                 do { /* RTL */\r
1031                     indexMap[--logicalStart] = visualStart++;\r
1032                 } while (visualStart < visualLimit);\r
1033             }\r
1034             /* visualStart==visualLimit; */\r
1035         }\r
1036 \r
1037         if (bidi.insertPoints.size > 0) {\r
1038             int markFound = 0, runCount = bidi.runCount;\r
1039             int length, insertRemove, i, j;\r
1040             runs = bidi.runs;\r
1041             visualStart = 0;\r
1042             /* add number of marks found until each index */\r
1043             for (i = 0; i < runCount; i++, visualStart += length) {\r
1044                 length = runs[i].limit - visualStart;\r
1045                 insertRemove = runs[i].insertRemove;\r
1046                 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {\r
1047                     markFound++;\r
1048                 }\r
1049                 if (markFound > 0) {\r
1050                     logicalStart = runs[i].start;\r
1051                     logicalLimit = logicalStart + length;\r
1052                     for (j = logicalStart; j < logicalLimit; j++) {\r
1053                         indexMap[j] += markFound;\r
1054                     }\r
1055                 }\r
1056                 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {\r
1057                     markFound++;\r
1058                 }\r
1059             }\r
1060         }\r
1061         else if (bidi.controlCount > 0) {\r
1062             int controlFound = 0, runCount = bidi.runCount;\r
1063             int length, insertRemove, i, j, k;\r
1064             boolean evenRun;\r
1065             char uchar;\r
1066             runs = bidi.runs;\r
1067             visualStart = 0;\r
1068             /* subtract number of controls found until each index */\r
1069             for (i = 0; i < runCount; i++, visualStart += length) {\r
1070                 length = runs[i].limit - visualStart;\r
1071                 insertRemove = runs[i].insertRemove;\r
1072                 /* no control found within previous runs nor within this run */\r
1073                 if ((controlFound - insertRemove) == 0) {\r
1074                     continue;\r
1075                 }\r
1076                 logicalStart = runs[i].start;\r
1077                 evenRun = runs[i].isEvenRun();\r
1078                 logicalLimit = logicalStart + length;\r
1079                 /* if no control within this run */\r
1080                 if (insertRemove == 0) {\r
1081                     for (j = logicalStart; j < logicalLimit; j++) {\r
1082                         indexMap[j] -= controlFound;\r
1083                     }\r
1084                     continue;\r
1085                 }\r
1086                 for (j = 0; j < length; j++) {\r
1087                     k = evenRun ? logicalStart + j : logicalLimit - j - 1;\r
1088                     uchar = bidi.text[k];\r
1089                     if (Bidi.IsBidiControlChar(uchar)) {\r
1090                         controlFound++;\r
1091                         indexMap[k] = Bidi.MAP_NOWHERE;\r
1092                         continue;\r
1093                     }\r
1094                     indexMap[k] -= controlFound;\r
1095                 }\r
1096             }\r
1097         }\r
1098         return indexMap;\r
1099     }\r
1100 \r
1101     static int[] getVisualMap(Bidi bidi)\r
1102     {\r
1103         /* fill a visual-to-logical index map using the runs[] */\r
1104         BidiRun[] runs = bidi.runs;\r
1105         int logicalStart, visualStart, visualLimit;\r
1106         int allocLength = bidi.length > bidi.resultLength ? bidi.length\r
1107                                                           : bidi.resultLength;\r
1108         int[] indexMap = new int[allocLength];\r
1109 \r
1110         visualStart = 0;\r
1111         int idx = 0;\r
1112         for (int j = 0; j < bidi.runCount; ++j) {\r
1113             logicalStart = runs[j].start;\r
1114             visualLimit = runs[j].limit;\r
1115             if (runs[j].isEvenRun()) {\r
1116                 do { /* LTR */\r
1117                     indexMap[idx++] = logicalStart++;\r
1118                 } while (++visualStart < visualLimit);\r
1119             } else {\r
1120                 logicalStart += visualLimit - visualStart;  /* logicalLimit */\r
1121                 do { /* RTL */\r
1122                     indexMap[idx++] = --logicalStart;\r
1123                 } while (++visualStart < visualLimit);\r
1124             }\r
1125             /* visualStart==visualLimit; */\r
1126         }\r
1127 \r
1128         if (bidi.insertPoints.size > 0) {\r
1129             int markFound = 0, runCount = bidi.runCount;\r
1130             int insertRemove, i, j, k;\r
1131             runs = bidi.runs;\r
1132             /* count all inserted marks */\r
1133             for (i = 0; i < runCount; i++) {\r
1134                 insertRemove = runs[i].insertRemove;\r
1135                 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {\r
1136                     markFound++;\r
1137                 }\r
1138                 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {\r
1139                     markFound++;\r
1140                 }\r
1141             }\r
1142             /* move back indexes by number of preceding marks */\r
1143             k = bidi.resultLength;\r
1144             for (i = runCount - 1; i >= 0 && markFound > 0; i--) {\r
1145                 insertRemove = runs[i].insertRemove;\r
1146                 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {\r
1147                     indexMap[--k] = Bidi.MAP_NOWHERE;\r
1148                     markFound--;\r
1149                 }\r
1150                 visualStart = i > 0 ? runs[i-1].limit : 0;\r
1151                 for (j = runs[i].limit - 1; j >= visualStart && markFound > 0; j--) {\r
1152                     indexMap[--k] = indexMap[j];\r
1153                 }\r
1154                 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {\r
1155                     indexMap[--k] = Bidi.MAP_NOWHERE;\r
1156                     markFound--;\r
1157                 }\r
1158             }\r
1159         }\r
1160         else if (bidi.controlCount > 0) {\r
1161             int runCount = bidi.runCount, logicalEnd;\r
1162             int insertRemove, length, i, j, k, m;\r
1163             char uchar;\r
1164             boolean evenRun;\r
1165             runs = bidi.runs;\r
1166             visualStart = 0;\r
1167             /* move forward indexes by number of preceding controls */\r
1168             k = 0;\r
1169             for (i = 0; i < runCount; i++, visualStart += length) {\r
1170                 length = runs[i].limit - visualStart;\r
1171                 insertRemove = runs[i].insertRemove;\r
1172                 /* if no control found yet, nothing to do in this run */\r
1173                 if ((insertRemove == 0) && (k == visualStart)) {\r
1174                     k += length;\r
1175                     continue;\r
1176                 }\r
1177                 /* if no control in this run */\r
1178                 if (insertRemove == 0) {\r
1179                     visualLimit = runs[i].limit;\r
1180                     for (j = visualStart; j < visualLimit; j++) {\r
1181                         indexMap[k++] = indexMap[j];\r
1182                     }\r
1183                     continue;\r
1184                 }\r
1185                 logicalStart = runs[i].start;\r
1186                 evenRun = runs[i].isEvenRun();\r
1187                 logicalEnd = logicalStart + length - 1;\r
1188                 for (j = 0; j < length; j++) {\r
1189                     m = evenRun ? logicalStart + j : logicalEnd - j;\r
1190                     uchar = bidi.text[m];\r
1191                     if (!Bidi.IsBidiControlChar(uchar)) {\r
1192                         indexMap[k++] = m;\r
1193                     }\r
1194                 }\r
1195             }\r
1196         }\r
1197         if (allocLength == bidi.resultLength) {\r
1198             return indexMap;\r
1199         }\r
1200         int[] newMap = new int[bidi.resultLength];\r
1201         System.arraycopy(indexMap, 0, newMap, 0, bidi.resultLength);\r
1202         return newMap;\r
1203     }\r
1204 \r
1205     static int[] invertMap(int[] srcMap)\r
1206     {\r
1207         int srcLength = srcMap.length;\r
1208         int destLength = -1, count = 0, i, srcEntry;\r
1209 \r
1210         /* find highest value and count positive indexes in srcMap */\r
1211         for (i = 0; i < srcLength; i++) {\r
1212             srcEntry = srcMap[i];\r
1213             if (srcEntry > destLength) {\r
1214                 destLength = srcEntry;\r
1215             }\r
1216             if (srcEntry >= 0) {\r
1217                 count++;\r
1218             }\r
1219         }\r
1220         destLength++;           /* add 1 for origin 0 */\r
1221         int[] destMap = new int[destLength];\r
1222         if (count < destLength) {\r
1223             /* we must fill unmatched destMap entries with -1 */\r
1224             Arrays.fill(destMap, Bidi.MAP_NOWHERE);\r
1225         }\r
1226         for (i = 0; i < srcLength; i++) {\r
1227             srcEntry = srcMap[i];\r
1228             if (srcEntry >= 0) {\r
1229                 destMap[srcEntry] = i;\r
1230             }\r
1231         }\r
1232         return destMap;\r
1233     }\r
1234 }\r