2 *******************************************************************************
\r
3 * Copyright (C) 2001-2009, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
5 *******************************************************************************
\r
7 /* Written by Simon Montagu, Matitiahu Allouche
\r
8 * (ported from C code written by Markus W. Scherer)
\r
11 package com.ibm.icu.text;
\r
14 import java.util.Arrays;
\r
16 final class BidiLine {
\r
19 * General remarks about the functions in this file:
\r
21 * These functions deal with the aspects of potentially mixed-directional
\r
22 * text in a single paragraph or in a line of a single paragraph
\r
23 * which has already been processed according to
\r
24 * the Unicode 3.0 Bidi algorithm as defined in
\r
25 * http://www.unicode.org/unicode/reports/tr9/ , version 13,
\r
26 * also described in The Unicode Standard, Version 4.0.1 .
\r
28 * This means that there is a Bidi object with a levels
\r
29 * and a dirProps array.
\r
30 * paraLevel and direction are also set.
\r
31 * Only if the length of the text is zero, then levels==dirProps==NULL.
\r
33 * The overall directionality of the paragraph
\r
34 * or line is used to bypass the reordering steps if possible.
\r
35 * Even purely RTL text does not need reordering there because
\r
36 * the getLogical/VisualIndex() methods can compute the
\r
37 * index on the fly in such a case.
\r
39 * The implementation of the access to same-level-runs and of the reordering
\r
40 * do attempt to provide better performance and less memory usage compared to
\r
41 * a direct implementation of especially rule (L2) with an array of
\r
42 * one (32-bit) integer per text character.
\r
44 * Here, the levels array is scanned as soon as necessary, and a vector of
\r
45 * same-level-runs is created. Reordering then is done on this vector.
\r
46 * For each run of text positions that were resolved to the same level,
\r
47 * only 8 bytes are stored: the first text position of the run and the visual
\r
48 * position behind the run after reordering.
\r
49 * One sign bit is used to hold the directionality of the run.
\r
50 * This is inefficient if there are many very short runs. If the average run
\r
51 * length is <2, then this uses more memory.
\r
53 * In a further attempt to save memory, the levels array is never changed
\r
54 * after all the resolution rules (Xn, Wn, Nn, In).
\r
55 * Many methods have to consider the field trailingWSStart:
\r
56 * if it is less than length, then there is an implicit trailing run
\r
58 * which is not reflected in the levels array.
\r
59 * This allows a line Bidi object to use the same levels array as
\r
60 * its paragraph parent object.
\r
62 * When a Bidi object is created for a line of a paragraph, then the
\r
63 * paragraph's levels and dirProps arrays are reused by way of setting
\r
64 * a pointer into them, not by copying. This again saves memory and forbids to
\r
65 * change the now shared levels for (L1).
\r
68 /* handle trailing WS (L1) -------------------------------------------------- */
\r
71 * setTrailingWSStart() sets the start index for a trailing
\r
72 * run of WS in the line. This is necessary because we do not modify
\r
73 * the paragraph's levels array that we just point into.
\r
74 * Using trailingWSStart is another form of performing (L1).
\r
76 * To make subsequent operations easier, we also include the run
\r
77 * before the WS if it is at the paraLevel - we merge the two here.
\r
79 * This method is called only from setLine(), so paraLevel is
\r
80 * set correctly for the line even when contextual multiple paragraphs.
\r
83 static void setTrailingWSStart(Bidi bidi)
\r
85 byte[] dirProps = bidi.dirProps;
\r
86 byte[] levels = bidi.levels;
\r
87 int start = bidi.length;
\r
88 byte paraLevel = bidi.paraLevel;
\r
90 /* If the line is terminated by a block separator, all preceding WS etc...
\r
91 are already set to paragraph level.
\r
92 Setting trailingWSStart to pBidi->length will avoid changing the
\r
93 level of B chars from 0 to paraLevel in getLevels when
\r
94 orderParagraphsLTR==TRUE
\r
96 if (Bidi.NoContextRTL(dirProps[start - 1]) == Bidi.B) {
\r
97 bidi.trailingWSStart = start; /* currently == bidi.length */
\r
100 /* go backwards across all WS, BN, explicit codes */
\r
101 while (start > 0 &&
\r
102 (Bidi.DirPropFlagNC(dirProps[start - 1]) & Bidi.MASK_WS) != 0) {
\r
106 /* if the WS run can be merged with the previous run then do so here */
\r
107 while (start > 0 && levels[start - 1] == paraLevel) {
\r
111 bidi.trailingWSStart=start;
\r
114 static Bidi setLine(Bidi paraBidi, int start, int limit) {
\r
117 Bidi lineBidi = new Bidi();
\r
119 /* set the values in lineBidi from its paraBidi parent */
\r
120 /* class members are already initialized to 0 */
\r
121 // lineBidi.paraBidi = null; /* mark unfinished setLine */
\r
122 // lineBidi.flags = 0;
\r
123 // lineBidi.controlCount = 0;
\r
125 length = lineBidi.length = lineBidi.originalLength =
\r
126 lineBidi.resultLength = limit - start;
\r
128 lineBidi.text = new char[length];
\r
129 System.arraycopy(paraBidi.text, start, lineBidi.text, 0, length);
\r
130 lineBidi.paraLevel = paraBidi.GetParaLevelAt(start);
\r
131 lineBidi.paraCount = paraBidi.paraCount;
\r
132 lineBidi.runs = new BidiRun[0];
\r
133 lineBidi.reorderingMode = paraBidi.reorderingMode;
\r
134 lineBidi.reorderingOptions = paraBidi.reorderingOptions;
\r
135 if (paraBidi.controlCount > 0) {
\r
137 for (j = start; j < limit; j++) {
\r
138 if (Bidi.IsBidiControlChar(paraBidi.text[j])) {
\r
139 lineBidi.controlCount++;
\r
142 lineBidi.resultLength -= lineBidi.controlCount;
\r
144 /* copy proper subset of DirProps */
\r
145 lineBidi.getDirPropsMemory(length);
\r
146 lineBidi.dirProps = lineBidi.dirPropsMemory;
\r
147 System.arraycopy(paraBidi.dirProps, start, lineBidi.dirProps, 0,
\r
149 /* copy proper subset of Levels */
\r
150 lineBidi.getLevelsMemory(length);
\r
151 lineBidi.levels = lineBidi.levelsMemory;
\r
152 System.arraycopy(paraBidi.levels, start, lineBidi.levels, 0,
\r
154 lineBidi.runCount = -1;
\r
156 if (paraBidi.direction != Bidi.MIXED) {
\r
157 /* the parent is already trivial */
\r
158 lineBidi.direction = paraBidi.direction;
\r
161 * The parent's levels are all either
\r
162 * implicitly or explicitly ==paraLevel;
\r
163 * do the same here.
\r
165 if (paraBidi.trailingWSStart <= start) {
\r
166 lineBidi.trailingWSStart = 0;
\r
167 } else if (paraBidi.trailingWSStart < limit) {
\r
168 lineBidi.trailingWSStart = paraBidi.trailingWSStart - start;
\r
170 lineBidi.trailingWSStart = length;
\r
173 byte[] levels = lineBidi.levels;
\r
174 int i, trailingWSStart;
\r
177 setTrailingWSStart(lineBidi);
\r
178 trailingWSStart = lineBidi.trailingWSStart;
\r
180 /* recalculate lineBidi.direction */
\r
181 if (trailingWSStart == 0) {
\r
182 /* all levels are at paraLevel */
\r
183 lineBidi.direction = (byte)(lineBidi.paraLevel & 1);
\r
185 /* get the level of the first character */
\r
186 level = (byte)(levels[0] & 1);
\r
188 /* if there is anything of a different level, then the line
\r
190 if (trailingWSStart < length &&
\r
191 (lineBidi.paraLevel & 1) != level) {
\r
192 /* the trailing WS is at paraLevel, which differs from
\r
194 lineBidi.direction = Bidi.MIXED;
\r
196 /* see if levels[1..trailingWSStart-1] have the same
\r
197 direction as levels[0] and paraLevel */
\r
198 for (i = 1; ; i++) {
\r
199 if (i == trailingWSStart) {
\r
200 /* the direction values match those in level */
\r
201 lineBidi.direction = level;
\r
203 } else if ((levels[i] & 1) != level) {
\r
204 lineBidi.direction = Bidi.MIXED;
\r
211 switch(lineBidi.direction) {
\r
212 case Bidi.DIRECTION_LEFT_TO_RIGHT:
\r
213 /* make sure paraLevel is even */
\r
214 lineBidi.paraLevel = (byte)
\r
215 ((lineBidi.paraLevel + 1) & ~1);
\r
217 /* all levels are implicitly at paraLevel (important for
\r
219 lineBidi.trailingWSStart = 0;
\r
221 case Bidi.DIRECTION_RIGHT_TO_LEFT:
\r
222 /* make sure paraLevel is odd */
\r
223 lineBidi.paraLevel |= 1;
\r
225 /* all levels are implicitly at paraLevel (important for
\r
227 lineBidi.trailingWSStart = 0;
\r
233 lineBidi.paraBidi = paraBidi; /* mark successful setLine */
\r
237 static byte getLevelAt(Bidi bidi, int charIndex)
\r
239 /* return paraLevel if in the trailing WS run, otherwise the real level */
\r
240 if (bidi.direction != Bidi.MIXED || charIndex >= bidi.trailingWSStart) {
\r
241 return bidi.GetParaLevelAt(charIndex);
\r
243 return bidi.levels[charIndex];
\r
247 static byte[] getLevels(Bidi bidi)
\r
249 int start = bidi.trailingWSStart;
\r
250 int length = bidi.length;
\r
252 if (start != length) {
\r
253 /* the current levels array does not reflect the WS run */
\r
255 * After the previous if(), we know that the levels array
\r
256 * has an implicit trailing WS run and therefore does not fully
\r
257 * reflect itself all the levels.
\r
258 * This must be a Bidi object for a line, and
\r
259 * we need to create a new levels array.
\r
261 /* bidi.paraLevel is ok even if contextual multiple paragraphs,
\r
262 since bidi is a line object */
\r
263 Arrays.fill(bidi.levels, start, length, bidi.paraLevel);
\r
265 /* this new levels array is set for the line and reflects the WS run */
\r
266 bidi.trailingWSStart = length;
\r
268 if (length < bidi.levels.length) {
\r
269 byte[] levels = new byte[length];
\r
270 System.arraycopy(bidi.levels, 0, levels, 0, length);
\r
273 return bidi.levels;
\r
276 static BidiRun getLogicalRun(Bidi bidi, int logicalPosition)
\r
278 /* this is done based on runs rather than on levels since levels have
\r
279 a special interpretation when REORDER_RUNS_ONLY
\r
281 BidiRun newRun = new BidiRun(), iRun;
\r
283 int runCount = bidi.runCount;
\r
284 int visualStart = 0, logicalLimit = 0;
\r
285 iRun = bidi.runs[0];
\r
287 for (int i = 0; i < runCount; i++) {
\r
288 iRun = bidi.runs[i];
\r
289 logicalLimit = iRun.start + iRun.limit - visualStart;
\r
290 if ((logicalPosition >= iRun.start) &&
\r
291 (logicalPosition < logicalLimit)) {
\r
294 visualStart = iRun.limit;
\r
296 newRun.start = iRun.start;
\r
297 newRun.limit = logicalLimit;
\r
298 newRun.level = iRun.level;
\r
302 static BidiRun getVisualRun(Bidi bidi, int runIndex)
\r
304 int start = bidi.runs[runIndex].start;
\r
306 byte level = bidi.runs[runIndex].level;
\r
308 if (runIndex > 0) {
\r
310 bidi.runs[runIndex].limit -
\r
311 bidi.runs[runIndex - 1].limit;
\r
313 limit = start + bidi.runs[0].limit;
\r
315 return new BidiRun(start, limit, level);
\r
318 /* in trivial cases there is only one trivial run; called by getRuns() */
\r
319 static void getSingleRun(Bidi bidi, byte level) {
\r
320 /* simple, single-run case */
\r
321 bidi.runs = bidi.simpleRuns;
\r
324 /* fill and reorder the single run */
\r
325 bidi.runs[0] = new BidiRun(0, bidi.length, level);
\r
328 /* reorder the runs array (L2) ---------------------------------------------- */
\r
331 * Reorder the same-level runs in the runs array.
\r
332 * Here, runCount>1 and maxLevel>=minLevel>=paraLevel.
\r
333 * All the visualStart fields=logical start before reordering.
\r
334 * The "odd" bits are not set yet.
\r
336 * Reordering with this data structure lends itself to some handy shortcuts:
\r
338 * Since each run is moved but not modified, and since at the initial maxLevel
\r
339 * each sequence of same-level runs consists of only one run each, we
\r
340 * don't need to do anything there and can predecrement maxLevel.
\r
341 * In many simple cases, the reordering is thus done entirely in the
\r
343 * Also, reordering occurs only down to the lowest odd level that occurs,
\r
344 * which is minLevel|1. However, if the lowest level itself is odd, then
\r
345 * in the last reordering the sequence of the runs at this level or higher
\r
346 * will be all runs, and we don't need the elaborate loop to search for them.
\r
347 * This is covered by ++minLevel instead of minLevel|=1 followed
\r
348 * by an extra reorder-all after the reorder-some loop.
\r
349 * About a trailing WS run:
\r
350 * Such a run would need special treatment because its level is not
\r
351 * reflected in levels[] if this is not a paragraph object.
\r
352 * Instead, all characters from trailingWSStart on are implicitly at
\r
354 * However, for all maxLevel>paraLevel, this run will never be reordered
\r
355 * and does not need to be taken into account. maxLevel==paraLevel is only reordered
\r
356 * if minLevel==paraLevel is odd, which is done in the extra segment.
\r
357 * This means that for the main reordering loop we don't need to consider
\r
358 * this run and can --runCount. If it is later part of the all-runs
\r
359 * reordering, then runCount is adjusted accordingly.
\r
361 private static void reorderLine(Bidi bidi, byte minLevel, byte maxLevel) {
\r
363 /* nothing to do? */
\r
364 if (maxLevel<=(minLevel|1)) {
\r
371 int firstRun, endRun, limitRun, runCount;
\r
374 * Reorder only down to the lowest odd level
\r
375 * and reorder at an odd minLevel in a separate, simpler loop.
\r
376 * See comments above for why minLevel is always incremented.
\r
381 levels = bidi.levels;
\r
382 runCount = bidi.runCount;
\r
384 /* do not include the WS run at paraLevel<=old minLevel except in the simple loop */
\r
385 if (bidi.trailingWSStart < bidi.length) {
\r
389 while (--maxLevel >= minLevel) {
\r
392 /* loop for all sequences of runs */
\r
394 /* look for a sequence of runs that are all at >=maxLevel */
\r
395 /* look for the first run of such a sequence */
\r
396 while (firstRun < runCount && levels[runs[firstRun].start] < maxLevel) {
\r
399 if (firstRun >= runCount) {
\r
400 break; /* no more such runs */
\r
403 /* look for the limit run of such a sequence (the run behind it) */
\r
404 for (limitRun = firstRun; ++limitRun < runCount &&
\r
405 levels[runs[limitRun].start]>=maxLevel; ) {}
\r
407 /* Swap the entire sequence of runs from firstRun to limitRun-1. */
\r
408 endRun = limitRun - 1;
\r
409 while (firstRun < endRun) {
\r
410 tempRun = runs[firstRun];
\r
411 runs[firstRun] = runs[endRun];
\r
412 runs[endRun] = tempRun;
\r
417 if (limitRun == runCount) {
\r
418 break; /* no more such runs */
\r
420 firstRun = limitRun + 1;
\r
425 /* now do maxLevel==old minLevel (==odd!), see above */
\r
426 if ((minLevel & 1) == 0) {
\r
429 /* include the trailing WS run in this complete reordering */
\r
430 if (bidi.trailingWSStart == bidi.length) {
\r
434 /* Swap the entire sequence of all runs. (endRun==runCount) */
\r
435 while (firstRun < runCount) {
\r
436 tempRun = runs[firstRun];
\r
437 runs[firstRun] = runs[runCount];
\r
438 runs[runCount] = tempRun;
\r
445 /* compute the runs array --------------------------------------------------- */
\r
447 static int getRunFromLogicalIndex(Bidi bidi, int logicalIndex) {
\r
448 BidiRun[] runs = bidi.runs;
\r
449 int runCount = bidi.runCount, visualStart = 0, i, length, logicalStart;
\r
451 for (i = 0; i < runCount; i++) {
\r
452 length = runs[i].limit - visualStart;
\r
453 logicalStart = runs[i].start;
\r
454 if ((logicalIndex >= logicalStart) && (logicalIndex < (logicalStart+length))) {
\r
457 visualStart += length;
\r
460 /* we should never get here */
\r
461 throw new IllegalStateException("Internal ICU error in getRunFromLogicalIndex");
\r
466 * Compute the runs array from the levels array.
\r
467 * After getRuns() returns true, runCount is guaranteed to be >0
\r
468 * and the runs are reordered.
\r
469 * Odd-level runs have visualStart on their visual right edge and
\r
470 * they progress visually to the left.
\r
471 * If option OPTION_INSERT_MARKS is set, insertRemove will contain the
\r
472 * sum of appropriate LRM/RLM_BEFORE/AFTER flags.
\r
473 * If option OPTION_REMOVE_CONTROLS is set, insertRemove will contain the
\r
474 * negative number of BiDi control characters within this run.
\r
476 static void getRuns(Bidi bidi) {
\r
478 * This method returns immediately if the runs are already set. This
\r
479 * includes the case of length==0 (handled in setPara)..
\r
481 if (bidi.runCount >= 0) {
\r
484 if (bidi.direction != Bidi.MIXED) {
\r
485 /* simple, single-run case - this covers length==0 */
\r
486 /* bidi.paraLevel is ok even for contextual multiple paragraphs */
\r
487 getSingleRun(bidi, bidi.paraLevel);
\r
488 } else /* Bidi.MIXED, length>0 */ {
\r
489 /* mixed directionality */
\r
490 int length = bidi.length, limit;
\r
491 byte[] levels = bidi.levels;
\r
493 byte level = Bidi.LEVEL_DEFAULT_LTR; /* initialize with no valid level */
\r
495 * If there are WS characters at the end of the line
\r
496 * and the run preceding them has a level different from
\r
497 * paraLevel, then they will form their own run at paraLevel (L1).
\r
498 * Count them separately.
\r
499 * We need some special treatment for this in order to not
\r
500 * modify the levels array which a line Bidi object shares
\r
501 * with its paragraph parent and its other line siblings.
\r
502 * In other words, for the trailing WS, it may be
\r
503 * levels[]!=paraLevel but we have to treat it like it were so.
\r
505 limit = bidi.trailingWSStart;
\r
506 /* count the runs, there is at least one non-WS run, and limit>0 */
\r
508 for (i = 0; i < limit; ++i) {
\r
509 /* increment runCount at the start of each run */
\r
510 if (levels[i] != level) {
\r
517 * We don't need to see if the last run can be merged with a trailing
\r
518 * WS run because setTrailingWSStart() would have done that.
\r
520 if (runCount == 1 && limit == length) {
\r
521 /* There is only one non-WS run and no trailing WS-run. */
\r
522 getSingleRun(bidi, levels[0]);
\r
523 } else /* runCount>1 || limit<length */ {
\r
524 /* allocate and set the runs */
\r
526 int runIndex, start;
\r
527 byte minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;
\r
530 /* now, count a (non-mergeable) WS run */
\r
531 if (limit < length) {
\r
536 bidi.getRunsMemory(runCount);
\r
537 runs = bidi.runsMemory;
\r
540 /* FOOD FOR THOUGHT: this could be optimized, e.g.:
\r
541 * 464->444, 484->444, 575->555, 595->555
\r
542 * However, that would take longer. Check also how it would
\r
543 * interact with BiDi control removal and inserting Marks.
\r
547 /* search for the run limits and initialize visualLimit values with the run lengths */
\r
550 /* prepare this run */
\r
553 if (level < minLevel) {
\r
556 if (level > maxLevel) {
\r
560 /* look for the run limit */
\r
561 while (++i < limit && levels[i] == level) {}
\r
563 /* i is another run limit */
\r
564 runs[runIndex] = new BidiRun(start, i - start, level);
\r
566 } while (i < limit);
\r
568 if (limit < length) {
\r
569 /* there is a separate WS run */
\r
570 runs[runIndex] = new BidiRun(limit, length - limit, bidi.paraLevel);
\r
571 /* For the trailing WS run, bidi.paraLevel is ok even
\r
572 if contextual multiple paragraphs. */
\r
573 if (bidi.paraLevel < minLevel) {
\r
574 minLevel = bidi.paraLevel;
\r
578 /* set the object fields */
\r
580 bidi.runCount = runCount;
\r
582 reorderLine(bidi, minLevel, maxLevel);
\r
584 /* now add the direction flags and adjust the visualLimit's to be just that */
\r
585 /* this loop will also handle the trailing WS run */
\r
587 for (i = 0; i < runCount; ++i) {
\r
588 runs[i].level = levels[runs[i].start];
\r
589 limit = (runs[i].limit += limit);
\r
592 /* Set the embedding level for the trailing WS run. */
\r
593 /* For a RTL paragraph, it will be the *first* run in visual order. */
\r
594 /* For the trailing WS run, bidi.paraLevel is ok even if
\r
595 contextual multiple paragraphs. */
\r
596 if (runIndex < runCount) {
\r
597 int trailingRun = ((bidi.paraLevel & 1) != 0)? 0 : runIndex;
\r
598 runs[trailingRun].level = bidi.paraLevel;
\r
603 /* handle insert LRM/RLM BEFORE/AFTER run */
\r
604 if (bidi.insertPoints.size > 0) {
\r
607 for (ip = 0; ip < bidi.insertPoints.size; ip++) {
\r
608 point = bidi.insertPoints.points[ip];
\r
609 runIndex = getRunFromLogicalIndex(bidi, point.pos);
\r
610 bidi.runs[runIndex].insertRemove |= point.flag;
\r
614 /* handle remove BiDi control characters */
\r
615 if (bidi.controlCount > 0) {
\r
618 for (ic = 0; ic < bidi.length; ic++) {
\r
620 if (Bidi.IsBidiControlChar(c)) {
\r
621 runIndex = getRunFromLogicalIndex(bidi, ic);
\r
622 bidi.runs[runIndex].insertRemove--;
\r
628 static int[] prepareReorder(byte[] levels, byte[] pMinLevel, byte[] pMaxLevel)
\r
631 byte level, minLevel, maxLevel;
\r
633 if (levels == null || levels.length <= 0) {
\r
637 /* determine minLevel and maxLevel */
\r
638 minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;
\r
640 for (start = levels.length; start>0; ) {
\r
641 level = levels[--start];
\r
642 if (level > Bidi.MAX_EXPLICIT_LEVEL + 1) {
\r
645 if (level < minLevel) {
\r
648 if (level > maxLevel) {
\r
652 pMinLevel[0] = minLevel;
\r
653 pMaxLevel[0] = maxLevel;
\r
655 /* initialize the index map */
\r
656 int[] indexMap = new int[levels.length];
\r
657 for (start = levels.length; start > 0; ) {
\r
659 indexMap[start] = start;
\r
665 static int[] reorderLogical(byte[] levels)
\r
667 byte[] aMinLevel = new byte[1];
\r
668 byte[] aMaxLevel = new byte[1];
\r
669 int start, limit, sumOfSosEos;
\r
670 byte minLevel, maxLevel;
\r
671 int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);
\r
672 if (indexMap == null) {
\r
676 minLevel = aMinLevel[0];
\r
677 maxLevel = aMaxLevel[0];
\r
679 /* nothing to do? */
\r
680 if (minLevel == maxLevel && (minLevel & 1) == 0) {
\r
684 /* reorder only down to the lowest odd level */
\r
687 /* loop maxLevel..minLevel */
\r
691 /* loop for all sequences of levels to reorder at the current maxLevel */
\r
693 /* look for a sequence of levels that are all at >=maxLevel */
\r
694 /* look for the first index of such a sequence */
\r
695 while (start < levels.length && levels[start] < maxLevel) {
\r
698 if (start >= levels.length) {
\r
699 break; /* no more such sequences */
\r
702 /* look for the limit of such a sequence (the index behind it) */
\r
703 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}
\r
706 * sos=start of sequence, eos=end of sequence
\r
708 * The closed (inclusive) interval from sos to eos includes all the logical
\r
709 * and visual indexes within this sequence. They are logically and
\r
710 * visually contiguous and in the same range.
\r
712 * For each run, the new visual index=sos+eos-old visual index;
\r
713 * we pre-add sos+eos into sumOfSosEos ->
\r
714 * new visual index=sumOfSosEos-old visual index;
\r
716 sumOfSosEos = start + limit - 1;
\r
718 /* reorder each index in the sequence */
\r
720 indexMap[start] = sumOfSosEos - indexMap[start];
\r
721 } while (++start < limit);
\r
724 if (limit == levels.length) {
\r
725 break; /* no more such sequences */
\r
730 } while (--maxLevel >= minLevel);
\r
734 static int[] reorderVisual(byte[] levels)
\r
736 byte[] aMinLevel = new byte[1];
\r
737 byte[] aMaxLevel = new byte[1];
\r
738 int start, end, limit, temp;
\r
739 byte minLevel, maxLevel;
\r
741 int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);
\r
742 if (indexMap == null) {
\r
746 minLevel = aMinLevel[0];
\r
747 maxLevel = aMaxLevel[0];
\r
749 /* nothing to do? */
\r
750 if (minLevel == maxLevel && (minLevel & 1) == 0) {
\r
754 /* reorder only down to the lowest odd level */
\r
757 /* loop maxLevel..minLevel */
\r
761 /* loop for all sequences of levels to reorder at the current maxLevel */
\r
763 /* look for a sequence of levels that are all at >=maxLevel */
\r
764 /* look for the first index of such a sequence */
\r
765 while (start < levels.length && levels[start] < maxLevel) {
\r
768 if (start >= levels.length) {
\r
769 break; /* no more such runs */
\r
772 /* look for the limit of such a sequence (the index behind it) */
\r
773 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}
\r
776 * Swap the entire interval of indexes from start to limit-1.
\r
777 * We don't need to swap the levels for the purpose of this
\r
778 * algorithm: the sequence of levels that we look at does not
\r
782 while (start < end) {
\r
783 temp = indexMap[start];
\r
784 indexMap[start] = indexMap[end];
\r
785 indexMap[end] = temp;
\r
791 if (limit == levels.length) {
\r
792 break; /* no more such sequences */
\r
797 } while (--maxLevel >= minLevel);
\r
802 static int getVisualIndex(Bidi bidi, int logicalIndex)
\r
804 int visualIndex = Bidi.MAP_NOWHERE;
\r
806 /* we can do the trivial cases without the runs array */
\r
807 switch(bidi.direction) {
\r
809 visualIndex = logicalIndex;
\r
812 visualIndex = bidi.length - logicalIndex - 1;
\r
816 BidiRun[] runs = bidi.runs;
\r
817 int i, visualStart = 0, offset, length;
\r
819 /* linear search for the run, search on the visual runs */
\r
820 for (i = 0; i < bidi.runCount; ++i) {
\r
821 length = runs[i].limit - visualStart;
\r
822 offset = logicalIndex - runs[i].start;
\r
823 if (offset >= 0 && offset < length) {
\r
824 if (runs[i].isEvenRun()) {
\r
826 visualIndex = visualStart + offset;
\r
829 visualIndex = visualStart + length - offset - 1;
\r
831 break; /* exit for loop */
\r
833 visualStart += length;
\r
835 if (i >= bidi.runCount) {
\r
836 return Bidi.MAP_NOWHERE;
\r
840 if (bidi.insertPoints.size > 0) {
\r
841 /* add the number of added marks until the calculated visual index */
\r
842 BidiRun runs[] = bidi.runs;
\r
843 int i, length, insertRemove;
\r
844 int visualStart = 0, markFound = 0;
\r
845 for (i = 0; ; i++, visualStart += length) {
\r
846 length = runs[i].limit - visualStart;
\r
847 insertRemove = runs[i].insertRemove;
\r
848 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
851 /* is it the run containing the visual index? */
\r
852 if (visualIndex < runs[i].limit) {
\r
853 return visualIndex + markFound;
\r
855 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
860 else if (bidi.controlCount > 0) {
\r
861 /* subtract the number of controls until the calculated visual index */
\r
862 BidiRun[] runs = bidi.runs;
\r
863 int i, j, start, limit, length, insertRemove;
\r
864 int visualStart = 0, controlFound = 0;
\r
865 char uchar = bidi.text[logicalIndex];
\r
866 /* is the logical index pointing to a control ? */
\r
867 if (Bidi.IsBidiControlChar(uchar)) {
\r
868 return Bidi.MAP_NOWHERE;
\r
871 for (i = 0; ; i++, visualStart += length) {
\r
872 length = runs[i].limit - visualStart;
\r
873 insertRemove = runs[i].insertRemove;
\r
874 /* calculated visual index is beyond this run? */
\r
875 if (visualIndex >= runs[i].limit) {
\r
876 controlFound -= insertRemove;
\r
879 /* calculated visual index must be within current run */
\r
880 if (insertRemove == 0) {
\r
881 return visualIndex - controlFound;
\r
883 if (runs[i].isEvenRun()) {
\r
884 /* LTR: check from run start to logical index */
\r
885 start = runs[i].start;
\r
886 limit = logicalIndex;
\r
888 /* RTL: check from logical index to run end */
\r
889 start = logicalIndex + 1;
\r
890 limit = runs[i].start + length;
\r
892 for (j = start; j < limit; j++) {
\r
893 uchar = bidi.text[j];
\r
894 if (Bidi.IsBidiControlChar(uchar)) {
\r
898 return visualIndex - controlFound;
\r
902 return visualIndex;
\r
905 static int getLogicalIndex(Bidi bidi, int visualIndex)
\r
908 int i, runCount, start;
\r
911 runCount = bidi.runCount;
\r
912 if (bidi.insertPoints.size > 0) {
\r
913 /* handle inserted LRM/RLM */
\r
914 int markFound = 0, insertRemove;
\r
915 int visualStart = 0, length;
\r
916 /* subtract number of marks until visual index */
\r
917 for (i = 0; ; i++, visualStart += length) {
\r
918 length = runs[i].limit - visualStart;
\r
919 insertRemove = runs[i].insertRemove;
\r
920 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
921 if (visualIndex <= (visualStart+markFound)) {
\r
922 return Bidi.MAP_NOWHERE;
\r
926 /* is adjusted visual index within this run? */
\r
927 if (visualIndex < (runs[i].limit + markFound)) {
\r
928 visualIndex -= markFound;
\r
931 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
932 if (visualIndex == (visualStart + length + markFound)) {
\r
933 return Bidi.MAP_NOWHERE;
\r
939 else if (bidi.controlCount > 0) {
\r
940 /* handle removed BiDi control characters */
\r
941 int controlFound = 0, insertRemove, length;
\r
942 int logicalStart, logicalEnd, visualStart = 0, j, k;
\r
945 /* add number of controls until visual index */
\r
946 for (i = 0; ; i++, visualStart += length) {
\r
947 length = runs[i].limit - visualStart;
\r
948 insertRemove = runs[i].insertRemove;
\r
949 /* is adjusted visual index beyond current run? */
\r
950 if (visualIndex >= (runs[i].limit - controlFound + insertRemove)) {
\r
951 controlFound -= insertRemove;
\r
954 /* adjusted visual index is within current run */
\r
955 if (insertRemove == 0) {
\r
956 visualIndex += controlFound;
\r
959 /* count non-control chars until visualIndex */
\r
960 logicalStart = runs[i].start;
\r
961 evenRun = runs[i].isEvenRun();
\r
962 logicalEnd = logicalStart + length - 1;
\r
963 for (j = 0; j < length; j++) {
\r
964 k= evenRun ? logicalStart+j : logicalEnd-j;
\r
965 uchar = bidi.text[k];
\r
966 if (Bidi.IsBidiControlChar(uchar)) {
\r
969 if ((visualIndex + controlFound) == (visualStart + j)) {
\r
973 visualIndex += controlFound;
\r
977 /* handle all cases */
\r
978 if (runCount <= 10) {
\r
979 /* linear search for the run */
\r
980 for (i = 0; visualIndex >= runs[i].limit; ++i) {}
\r
982 /* binary search for the run */
\r
983 int begin = 0, limit = runCount;
\r
985 /* the middle if() is guaranteed to find the run, we don't need a loop limit */
\r
987 i = (begin + limit) / 2;
\r
988 if (visualIndex >= runs[i].limit) {
\r
990 } else if (i==0 || visualIndex >= runs[i-1].limit) {
\r
998 start= runs[i].start;
\r
999 if (runs[i].isEvenRun()) {
\r
1001 /* the offset in runs[i] is visualIndex-runs[i-1].visualLimit */
\r
1003 visualIndex -= runs[i - 1].limit;
\r
1005 return start + visualIndex;
\r
1008 return start + runs[i].limit - visualIndex - 1;
\r
1012 static int[] getLogicalMap(Bidi bidi)
\r
1014 /* fill a logical-to-visual index map using the runs[] */
\r
1015 BidiRun[] runs = bidi.runs;
\r
1016 int logicalStart, visualStart, logicalLimit, visualLimit;
\r
1017 int[] indexMap = new int[bidi.length];
\r
1018 if (bidi.length > bidi.resultLength) {
\r
1019 Arrays.fill(indexMap, Bidi.MAP_NOWHERE);
\r
1023 for (int j = 0; j < bidi.runCount; ++j) {
\r
1024 logicalStart = runs[j].start;
\r
1025 visualLimit = runs[j].limit;
\r
1026 if (runs[j].isEvenRun()) {
\r
1028 indexMap[logicalStart++] = visualStart++;
\r
1029 } while (visualStart < visualLimit);
\r
1031 logicalStart += visualLimit - visualStart; /* logicalLimit */
\r
1033 indexMap[--logicalStart] = visualStart++;
\r
1034 } while (visualStart < visualLimit);
\r
1036 /* visualStart==visualLimit; */
\r
1039 if (bidi.insertPoints.size > 0) {
\r
1040 int markFound = 0, runCount = bidi.runCount;
\r
1041 int length, insertRemove, i, j;
\r
1044 /* add number of marks found until each index */
\r
1045 for (i = 0; i < runCount; i++, visualStart += length) {
\r
1046 length = runs[i].limit - visualStart;
\r
1047 insertRemove = runs[i].insertRemove;
\r
1048 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
1051 if (markFound > 0) {
\r
1052 logicalStart = runs[i].start;
\r
1053 logicalLimit = logicalStart + length;
\r
1054 for (j = logicalStart; j < logicalLimit; j++) {
\r
1055 indexMap[j] += markFound;
\r
1058 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
1063 else if (bidi.controlCount > 0) {
\r
1064 int controlFound = 0, runCount = bidi.runCount;
\r
1065 int length, insertRemove, i, j, k;
\r
1070 /* subtract number of controls found until each index */
\r
1071 for (i = 0; i < runCount; i++, visualStart += length) {
\r
1072 length = runs[i].limit - visualStart;
\r
1073 insertRemove = runs[i].insertRemove;
\r
1074 /* no control found within previous runs nor within this run */
\r
1075 if ((controlFound - insertRemove) == 0) {
\r
1078 logicalStart = runs[i].start;
\r
1079 evenRun = runs[i].isEvenRun();
\r
1080 logicalLimit = logicalStart + length;
\r
1081 /* if no control within this run */
\r
1082 if (insertRemove == 0) {
\r
1083 for (j = logicalStart; j < logicalLimit; j++) {
\r
1084 indexMap[j] -= controlFound;
\r
1088 for (j = 0; j < length; j++) {
\r
1089 k = evenRun ? logicalStart + j : logicalLimit - j - 1;
\r
1090 uchar = bidi.text[k];
\r
1091 if (Bidi.IsBidiControlChar(uchar)) {
\r
1093 indexMap[k] = Bidi.MAP_NOWHERE;
\r
1096 indexMap[k] -= controlFound;
\r
1103 static int[] getVisualMap(Bidi bidi)
\r
1105 /* fill a visual-to-logical index map using the runs[] */
\r
1106 BidiRun[] runs = bidi.runs;
\r
1107 int logicalStart, visualStart, visualLimit;
\r
1108 int allocLength = bidi.length > bidi.resultLength ? bidi.length
\r
1109 : bidi.resultLength;
\r
1110 int[] indexMap = new int[allocLength];
\r
1114 for (int j = 0; j < bidi.runCount; ++j) {
\r
1115 logicalStart = runs[j].start;
\r
1116 visualLimit = runs[j].limit;
\r
1117 if (runs[j].isEvenRun()) {
\r
1119 indexMap[idx++] = logicalStart++;
\r
1120 } while (++visualStart < visualLimit);
\r
1122 logicalStart += visualLimit - visualStart; /* logicalLimit */
\r
1124 indexMap[idx++] = --logicalStart;
\r
1125 } while (++visualStart < visualLimit);
\r
1127 /* visualStart==visualLimit; */
\r
1130 if (bidi.insertPoints.size > 0) {
\r
1131 int markFound = 0, runCount = bidi.runCount;
\r
1132 int insertRemove, i, j, k;
\r
1134 /* count all inserted marks */
\r
1135 for (i = 0; i < runCount; i++) {
\r
1136 insertRemove = runs[i].insertRemove;
\r
1137 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
1140 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
1144 /* move back indexes by number of preceding marks */
\r
1145 k = bidi.resultLength;
\r
1146 for (i = runCount - 1; i >= 0 && markFound > 0; i--) {
\r
1147 insertRemove = runs[i].insertRemove;
\r
1148 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
1149 indexMap[--k] = Bidi.MAP_NOWHERE;
\r
1152 visualStart = i > 0 ? runs[i-1].limit : 0;
\r
1153 for (j = runs[i].limit - 1; j >= visualStart && markFound > 0; j--) {
\r
1154 indexMap[--k] = indexMap[j];
\r
1156 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
1157 indexMap[--k] = Bidi.MAP_NOWHERE;
\r
1162 else if (bidi.controlCount > 0) {
\r
1163 int runCount = bidi.runCount, logicalEnd;
\r
1164 int insertRemove, length, i, j, k, m;
\r
1169 /* move forward indexes by number of preceding controls */
\r
1171 for (i = 0; i < runCount; i++, visualStart += length) {
\r
1172 length = runs[i].limit - visualStart;
\r
1173 insertRemove = runs[i].insertRemove;
\r
1174 /* if no control found yet, nothing to do in this run */
\r
1175 if ((insertRemove == 0) && (k == visualStart)) {
\r
1179 /* if no control in this run */
\r
1180 if (insertRemove == 0) {
\r
1181 visualLimit = runs[i].limit;
\r
1182 for (j = visualStart; j < visualLimit; j++) {
\r
1183 indexMap[k++] = indexMap[j];
\r
1187 logicalStart = runs[i].start;
\r
1188 evenRun = runs[i].isEvenRun();
\r
1189 logicalEnd = logicalStart + length - 1;
\r
1190 for (j = 0; j < length; j++) {
\r
1191 m = evenRun ? logicalStart + j : logicalEnd - j;
\r
1192 uchar = bidi.text[m];
\r
1193 if (!Bidi.IsBidiControlChar(uchar)) {
\r
1194 indexMap[k++] = m;
\r
1199 if (allocLength == bidi.resultLength) {
\r
1202 int[] newMap = new int[bidi.resultLength];
\r
1203 System.arraycopy(indexMap, 0, newMap, 0, bidi.resultLength);
\r
1207 static int[] invertMap(int[] srcMap)
\r
1209 int srcLength = srcMap.length;
\r
1210 int destLength = -1, count = 0, i, srcEntry;
\r
1212 /* find highest value and count positive indexes in srcMap */
\r
1213 for (i = 0; i < srcLength; i++) {
\r
1214 srcEntry = srcMap[i];
\r
1215 if (srcEntry > destLength) {
\r
1216 destLength = srcEntry;
\r
1218 if (srcEntry >= 0) {
\r
1222 destLength++; /* add 1 for origin 0 */
\r
1223 int[] destMap = new int[destLength];
\r
1224 if (count < destLength) {
\r
1225 /* we must fill unmatched destMap entries with -1 */
\r
1226 Arrays.fill(destMap, Bidi.MAP_NOWHERE);
\r
1228 for (i = 0; i < srcLength; i++) {
\r
1229 srcEntry = srcMap[i];
\r
1230 if (srcEntry >= 0) {
\r
1231 destMap[srcEntry] = i;
\r