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