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