2 *******************************************************************************
3 * Copyright (C) 2001-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
7 /* Written by Simon Montagu, Matitiahu Allouche
8 * (ported from C code written by Markus W. Scherer)
11 package com.ibm.icu.text;
14 import java.util.Arrays;
16 final class BidiLine {
19 * General remarks about the functions in this file:
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 .
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.
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.
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.
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.
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
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.
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).
68 /* handle trailing WS (L1) -------------------------------------------------- */
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).
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.
79 * This method is called only from setLine(), so paraLevel is
80 * set correctly for the line even when contextual multiple paragraphs.
83 static void setTrailingWSStart(Bidi bidi)
85 byte[] dirProps = bidi.dirProps;
86 byte[] levels = bidi.levels;
87 int start = bidi.length;
88 byte paraLevel = bidi.paraLevel;
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
96 if (dirProps[start - 1] == Bidi.B) {
97 bidi.trailingWSStart = start; /* currently == bidi.length */
100 /* go backwards across all WS, BN, explicit codes */
102 (Bidi.DirPropFlag(Bidi.PureDirProp(dirProps[start - 1])) & Bidi.MASK_WS) != 0) {
106 /* if the WS run can be merged with the previous run then do so here */
107 while (start > 0 && levels[start - 1] == paraLevel) {
111 bidi.trailingWSStart=start;
114 static Bidi setLine(Bidi paraBidi, int start, int limit) {
117 Bidi lineBidi = new Bidi();
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;
125 length = lineBidi.length = lineBidi.originalLength =
126 lineBidi.resultLength = limit - start;
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) {
137 for (j = start; j < limit; j++) {
138 if (Bidi.IsBidiControlChar(paraBidi.text[j])) {
139 lineBidi.controlCount++;
142 lineBidi.resultLength -= lineBidi.controlCount;
144 /* copy proper subset of DirProps */
145 lineBidi.getDirPropsMemory(length);
146 lineBidi.dirProps = lineBidi.dirPropsMemory;
147 System.arraycopy(paraBidi.dirProps, start, lineBidi.dirProps, 0,
149 /* copy proper subset of Levels */
150 lineBidi.getLevelsMemory(length);
151 lineBidi.levels = lineBidi.levelsMemory;
152 System.arraycopy(paraBidi.levels, start, lineBidi.levels, 0,
154 lineBidi.runCount = -1;
156 if (paraBidi.direction != Bidi.MIXED) {
157 /* the parent is already trivial */
158 lineBidi.direction = paraBidi.direction;
161 * The parent's levels are all either
162 * implicitly or explicitly ==paraLevel;
165 if (paraBidi.trailingWSStart <= start) {
166 lineBidi.trailingWSStart = 0;
167 } else if (paraBidi.trailingWSStart < limit) {
168 lineBidi.trailingWSStart = paraBidi.trailingWSStart - start;
170 lineBidi.trailingWSStart = length;
173 byte[] levels = lineBidi.levels;
174 int i, trailingWSStart;
177 setTrailingWSStart(lineBidi);
178 trailingWSStart = lineBidi.trailingWSStart;
180 /* recalculate lineBidi.direction */
181 if (trailingWSStart == 0) {
182 /* all levels are at paraLevel */
183 lineBidi.direction = (byte)(lineBidi.paraLevel & 1);
185 /* get the level of the first character */
186 level = (byte)(levels[0] & 1);
188 /* if there is anything of a different level, then the line
190 if (trailingWSStart < length &&
191 (lineBidi.paraLevel & 1) != level) {
192 /* the trailing WS is at paraLevel, which differs from
194 lineBidi.direction = Bidi.MIXED;
196 /* see if levels[1..trailingWSStart-1] have the same
197 direction as levels[0] and paraLevel */
199 if (i == trailingWSStart) {
200 /* the direction values match those in level */
201 lineBidi.direction = level;
203 } else if ((levels[i] & 1) != level) {
204 lineBidi.direction = Bidi.MIXED;
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);
217 /* all levels are implicitly at paraLevel (important for
219 lineBidi.trailingWSStart = 0;
221 case Bidi.DIRECTION_RIGHT_TO_LEFT:
222 /* make sure paraLevel is odd */
223 lineBidi.paraLevel |= 1;
225 /* all levels are implicitly at paraLevel (important for
227 lineBidi.trailingWSStart = 0;
233 lineBidi.paraBidi = paraBidi; /* mark successful setLine */
237 static byte getLevelAt(Bidi bidi, int charIndex)
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);
243 return bidi.levels[charIndex];
247 static byte[] getLevels(Bidi bidi)
249 int start = bidi.trailingWSStart;
250 int length = bidi.length;
252 if (start != length) {
253 /* the current levels array does not reflect the WS run */
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.
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);
265 /* this new levels array is set for the line and reflects the WS run */
266 bidi.trailingWSStart = length;
268 if (length < bidi.levels.length) {
269 byte[] levels = new byte[length];
270 System.arraycopy(bidi.levels, 0, levels, 0, length);
276 static BidiRun getLogicalRun(Bidi bidi, int logicalPosition)
278 /* this is done based on runs rather than on levels since levels have
279 a special interpretation when REORDER_RUNS_ONLY
281 BidiRun newRun = new BidiRun(), iRun;
283 int runCount = bidi.runCount;
284 int visualStart = 0, logicalLimit = 0;
287 for (int i = 0; i < runCount; i++) {
289 logicalLimit = iRun.start + iRun.limit - visualStart;
290 if ((logicalPosition >= iRun.start) &&
291 (logicalPosition < logicalLimit)) {
294 visualStart = iRun.limit;
296 newRun.start = iRun.start;
297 newRun.limit = logicalLimit;
298 newRun.level = iRun.level;
302 static BidiRun getVisualRun(Bidi bidi, int runIndex)
304 int start = bidi.runs[runIndex].start;
306 byte level = bidi.runs[runIndex].level;
310 bidi.runs[runIndex].limit -
311 bidi.runs[runIndex - 1].limit;
313 limit = start + bidi.runs[0].limit;
315 return new BidiRun(start, limit, level);
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;
324 /* fill and reorder the single run */
325 bidi.runs[0] = new BidiRun(0, bidi.length, level);
328 /* reorder the runs array (L2) ---------------------------------------------- */
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.
336 * Reordering with this data structure lends itself to some handy shortcuts:
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
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
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.
361 private static void reorderLine(Bidi bidi, byte minLevel, byte maxLevel) {
364 if (maxLevel<=(minLevel|1)) {
371 int firstRun, endRun, limitRun, runCount;
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.
381 levels = bidi.levels;
382 runCount = bidi.runCount;
384 /* do not include the WS run at paraLevel<=old minLevel except in the simple loop */
385 if (bidi.trailingWSStart < bidi.length) {
389 while (--maxLevel >= minLevel) {
392 /* loop for all sequences of runs */
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) {
399 if (firstRun >= runCount) {
400 break; /* no more such runs */
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; ) {}
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;
417 if (limitRun == runCount) {
418 break; /* no more such runs */
420 firstRun = limitRun + 1;
425 /* now do maxLevel==old minLevel (==odd!), see above */
426 if ((minLevel & 1) == 0) {
429 /* include the trailing WS run in this complete reordering */
430 if (bidi.trailingWSStart == bidi.length) {
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;
445 /* compute the runs array --------------------------------------------------- */
447 static int getRunFromLogicalIndex(Bidi bidi, int logicalIndex) {
448 BidiRun[] runs = bidi.runs;
449 int runCount = bidi.runCount, visualStart = 0, i, length, logicalStart;
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))) {
457 visualStart += length;
460 /* we should never get here */
461 throw new IllegalStateException("Internal ICU error in getRunFromLogicalIndex");
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.
476 static void getRuns(Bidi bidi) {
478 * This method returns immediately if the runs are already set. This
479 * includes the case of length==0 (handled in setPara)..
481 if (bidi.runCount >= 0) {
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;
493 byte level = -1; /* initialize with no valid level */
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.
505 limit = bidi.trailingWSStart;
506 /* count the runs, there is at least one non-WS run, and limit>0 */
508 for (i = 0; i < limit; ++i) {
509 /* increment runCount at the start of each run */
510 if (levels[i] != level) {
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.
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 */
527 byte minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;
530 /* now, count a (non-mergeable) WS run */
531 if (limit < length) {
536 bidi.getRunsMemory(runCount);
537 runs = bidi.runsMemory;
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.
547 /* search for the run limits and initialize visualLimit values with the run lengths */
550 /* prepare this run */
553 if (level < minLevel) {
556 if (level > maxLevel) {
560 /* look for the run limit */
561 while (++i < limit && levels[i] == level) {}
563 /* i is another run limit */
564 runs[runIndex] = new BidiRun(start, i - start, level);
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;
578 /* set the object fields */
580 bidi.runCount = runCount;
582 reorderLine(bidi, minLevel, maxLevel);
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 */
587 for (i = 0; i < runCount; ++i) {
588 runs[i].level = levels[runs[i].start];
589 limit = (runs[i].limit += limit);
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;
603 /* handle insert LRM/RLM BEFORE/AFTER run */
604 if (bidi.insertPoints.size > 0) {
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;
614 /* handle remove BiDi control characters */
615 if (bidi.controlCount > 0) {
618 for (ic = 0; ic < bidi.length; ic++) {
620 if (Bidi.IsBidiControlChar(c)) {
621 runIndex = getRunFromLogicalIndex(bidi, ic);
622 bidi.runs[runIndex].insertRemove--;
628 static int[] prepareReorder(byte[] levels, byte[] pMinLevel, byte[] pMaxLevel)
631 byte level, minLevel, maxLevel;
633 if (levels == null || levels.length <= 0) {
637 /* determine minLevel and maxLevel */
638 minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;
640 for (start = levels.length; start>0; ) {
641 level = levels[--start];
645 if (level > (Bidi.MAX_EXPLICIT_LEVEL + 1)) {
648 if (level < minLevel) {
651 if (level > maxLevel) {
655 pMinLevel[0] = minLevel;
656 pMaxLevel[0] = maxLevel;
658 /* initialize the index map */
659 int[] indexMap = new int[levels.length];
660 for (start = levels.length; start > 0; ) {
662 indexMap[start] = start;
668 static int[] reorderLogical(byte[] levels)
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) {
679 minLevel = aMinLevel[0];
680 maxLevel = aMaxLevel[0];
683 if (minLevel == maxLevel && (minLevel & 1) == 0) {
687 /* reorder only down to the lowest odd level */
690 /* loop maxLevel..minLevel */
694 /* loop for all sequences of levels to reorder at the current maxLevel */
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) {
701 if (start >= levels.length) {
702 break; /* no more such sequences */
705 /* look for the limit of such a sequence (the index behind it) */
706 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}
709 * sos=start of sequence, eos=end of sequence
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.
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;
719 sumOfSosEos = start + limit - 1;
721 /* reorder each index in the sequence */
723 indexMap[start] = sumOfSosEos - indexMap[start];
724 } while (++start < limit);
727 if (limit == levels.length) {
728 break; /* no more such sequences */
733 } while (--maxLevel >= minLevel);
737 static int[] reorderVisual(byte[] levels)
739 byte[] aMinLevel = new byte[1];
740 byte[] aMaxLevel = new byte[1];
741 int start, end, limit, temp;
742 byte minLevel, maxLevel;
744 int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);
745 if (indexMap == null) {
749 minLevel = aMinLevel[0];
750 maxLevel = aMaxLevel[0];
753 if (minLevel == maxLevel && (minLevel & 1) == 0) {
757 /* reorder only down to the lowest odd level */
760 /* loop maxLevel..minLevel */
764 /* loop for all sequences of levels to reorder at the current maxLevel */
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) {
771 if (start >= levels.length) {
772 break; /* no more such runs */
775 /* look for the limit of such a sequence (the index behind it) */
776 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}
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
785 while (start < end) {
786 temp = indexMap[start];
787 indexMap[start] = indexMap[end];
788 indexMap[end] = temp;
794 if (limit == levels.length) {
795 break; /* no more such sequences */
800 } while (--maxLevel >= minLevel);
805 static int getVisualIndex(Bidi bidi, int logicalIndex)
807 int visualIndex = Bidi.MAP_NOWHERE;
809 /* we can do the trivial cases without the runs array */
810 switch(bidi.direction) {
812 visualIndex = logicalIndex;
815 visualIndex = bidi.length - logicalIndex - 1;
819 BidiRun[] runs = bidi.runs;
820 int i, visualStart = 0, offset, length;
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()) {
829 visualIndex = visualStart + offset;
832 visualIndex = visualStart + length - offset - 1;
834 break; /* exit for loop */
836 visualStart += length;
838 if (i >= bidi.runCount) {
839 return Bidi.MAP_NOWHERE;
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) {
854 /* is it the run containing the visual index? */
855 if (visualIndex < runs[i].limit) {
856 return visualIndex + markFound;
858 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
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;
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;
882 /* calculated visual index must be within current run */
883 if (insertRemove == 0) {
884 return visualIndex - controlFound;
886 if (runs[i].isEvenRun()) {
887 /* LTR: check from run start to logical index */
888 start = runs[i].start;
889 limit = logicalIndex;
891 /* RTL: check from logical index to run end */
892 start = logicalIndex + 1;
893 limit = runs[i].start + length;
895 for (j = start; j < limit; j++) {
896 uchar = bidi.text[j];
897 if (Bidi.IsBidiControlChar(uchar)) {
901 return visualIndex - controlFound;
908 static int getLogicalIndex(Bidi bidi, int visualIndex)
911 int i, runCount, start;
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;
929 /* is adjusted visual index within this run? */
930 if (visualIndex < (runs[i].limit + markFound)) {
931 visualIndex -= markFound;
934 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
935 if (visualIndex == (visualStart + length + markFound)) {
936 return Bidi.MAP_NOWHERE;
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;
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;
957 /* adjusted visual index is within current run */
958 if (insertRemove == 0) {
959 visualIndex += controlFound;
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)) {
972 if ((visualIndex + controlFound) == (visualStart + j)) {
976 visualIndex += controlFound;
980 /* handle all cases */
981 if (runCount <= 10) {
982 /* linear search for the run */
983 for (i = 0; visualIndex >= runs[i].limit; ++i) {}
985 /* binary search for the run */
986 int begin = 0, limit = runCount;
988 /* the middle if() is guaranteed to find the run, we don't need a loop limit */
990 i = (begin + limit) >>> 1;
991 if (visualIndex >= runs[i].limit) {
993 } else if (i==0 || visualIndex >= runs[i-1].limit) {
1001 start= runs[i].start;
1002 if (runs[i].isEvenRun()) {
1004 /* the offset in runs[i] is visualIndex-runs[i-1].visualLimit */
1006 visualIndex -= runs[i - 1].limit;
1008 return start + visualIndex;
1011 return start + runs[i].limit - visualIndex - 1;
1015 static int[] getLogicalMap(Bidi bidi)
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);
1026 for (int j = 0; j < bidi.runCount; ++j) {
1027 logicalStart = runs[j].start;
1028 visualLimit = runs[j].limit;
1029 if (runs[j].isEvenRun()) {
1031 indexMap[logicalStart++] = visualStart++;
1032 } while (visualStart < visualLimit);
1034 logicalStart += visualLimit - visualStart; /* logicalLimit */
1036 indexMap[--logicalStart] = visualStart++;
1037 } while (visualStart < visualLimit);
1039 /* visualStart==visualLimit; */
1042 if (bidi.insertPoints.size > 0) {
1043 int markFound = 0, runCount = bidi.runCount;
1044 int length, insertRemove, i, j;
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) {
1054 if (markFound > 0) {
1055 logicalStart = runs[i].start;
1056 logicalLimit = logicalStart + length;
1057 for (j = logicalStart; j < logicalLimit; j++) {
1058 indexMap[j] += markFound;
1061 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
1066 else if (bidi.controlCount > 0) {
1067 int controlFound = 0, runCount = bidi.runCount;
1068 int length, insertRemove, i, j, k;
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) {
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;
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)) {
1096 indexMap[k] = Bidi.MAP_NOWHERE;
1099 indexMap[k] -= controlFound;
1106 static int[] getVisualMap(Bidi bidi)
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];
1117 for (int j = 0; j < bidi.runCount; ++j) {
1118 logicalStart = runs[j].start;
1119 visualLimit = runs[j].limit;
1120 if (runs[j].isEvenRun()) {
1122 indexMap[idx++] = logicalStart++;
1123 } while (++visualStart < visualLimit);
1125 logicalStart += visualLimit - visualStart; /* logicalLimit */
1127 indexMap[idx++] = --logicalStart;
1128 } while (++visualStart < visualLimit);
1130 /* visualStart==visualLimit; */
1133 if (bidi.insertPoints.size > 0) {
1134 int markFound = 0, runCount = bidi.runCount;
1135 int insertRemove, i, j, k;
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) {
1143 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
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;
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];
1159 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
1160 indexMap[--k] = Bidi.MAP_NOWHERE;
1165 else if (bidi.controlCount > 0) {
1166 int runCount = bidi.runCount, logicalEnd;
1167 int insertRemove, length, i, j, k, m;
1172 /* move forward indexes by number of preceding controls */
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)) {
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];
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)) {
1202 if (allocLength == bidi.resultLength) {
1205 int[] newMap = new int[bidi.resultLength];
1206 System.arraycopy(indexMap, 0, newMap, 0, bidi.resultLength);
1210 static int[] invertMap(int[] srcMap)
1212 int srcLength = srcMap.length;
1213 int destLength = -1, count = 0, i, srcEntry;
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;
1221 if (srcEntry >= 0) {
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);
1231 for (i = 0; i < srcLength; i++) {
1232 srcEntry = srcMap[i];
1233 if (srcEntry >= 0) {
1234 destMap[srcEntry] = i;