2 *******************************************************************************
\r
3 * Copyright (C) 2001-2007, 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
459 /* we should never get here */
\r
460 throw new IllegalStateException("Internal ICU error in getRunFromLogicalIndex");
\r
464 * Compute the runs array from the levels array.
\r
465 * After getRuns() returns true, runCount is guaranteed to be >0
\r
466 * and the runs are reordered.
\r
467 * Odd-level runs have visualStart on their visual right edge and
\r
468 * they progress visually to the left.
\r
469 * If option OPTION_INSERT_MARKS is set, insertRemove will contain the
\r
470 * sum of appropriate LRM/RLM_BEFORE/AFTER flags.
\r
471 * If option OPTION_REMOVE_CONTROLS is set, insertRemove will contain the
\r
472 * negative number of BiDi control characters within this run.
\r
474 static void getRuns(Bidi bidi) {
\r
476 * This method returns immediately if the runs are already set. This
\r
477 * includes the case of length==0 (handled in setPara)..
\r
479 if (bidi.runCount >= 0) {
\r
482 if (bidi.direction != Bidi.MIXED) {
\r
483 /* simple, single-run case - this covers length==0 */
\r
484 /* bidi.paraLevel is ok even for contextual multiple paragraphs */
\r
485 getSingleRun(bidi, bidi.paraLevel);
\r
486 } else /* Bidi.MIXED, length>0 */ {
\r
487 /* mixed directionality */
\r
488 int length = bidi.length, limit;
\r
489 byte[] levels = bidi.levels;
\r
491 byte level = Bidi.LEVEL_DEFAULT_LTR; /* initialize with no valid level */
\r
493 * If there are WS characters at the end of the line
\r
494 * and the run preceding them has a level different from
\r
495 * paraLevel, then they will form their own run at paraLevel (L1).
\r
496 * Count them separately.
\r
497 * We need some special treatment for this in order to not
\r
498 * modify the levels array which a line Bidi object shares
\r
499 * with its paragraph parent and its other line siblings.
\r
500 * In other words, for the trailing WS, it may be
\r
501 * levels[]!=paraLevel but we have to treat it like it were so.
\r
503 limit = bidi.trailingWSStart;
\r
504 /* count the runs, there is at least one non-WS run, and limit>0 */
\r
506 for (i = 0; i < limit; ++i) {
\r
507 /* increment runCount at the start of each run */
\r
508 if (levels[i] != level) {
\r
515 * We don't need to see if the last run can be merged with a trailing
\r
516 * WS run because setTrailingWSStart() would have done that.
\r
518 if (runCount == 1 && limit == length) {
\r
519 /* There is only one non-WS run and no trailing WS-run. */
\r
520 getSingleRun(bidi, levels[0]);
\r
521 } else /* runCount>1 || limit<length */ {
\r
522 /* allocate and set the runs */
\r
524 int runIndex, start;
\r
525 byte minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;
\r
528 /* now, count a (non-mergeable) WS run */
\r
529 if (limit < length) {
\r
534 bidi.getRunsMemory(runCount);
\r
535 runs = bidi.runsMemory;
\r
538 /* FOOD FOR THOUGHT: this could be optimized, e.g.:
\r
539 * 464->444, 484->444, 575->555, 595->555
\r
540 * However, that would take longer. Check also how it would
\r
541 * interact with BiDi control removal and inserting Marks.
\r
545 /* search for the run limits and initialize visualLimit values with the run lengths */
\r
548 /* prepare this run */
\r
551 if (level < minLevel) {
\r
554 if (level > maxLevel) {
\r
558 /* look for the run limit */
\r
559 while (++i < limit && levels[i] == level) {}
\r
561 /* i is another run limit */
\r
562 runs[runIndex] = new BidiRun(start, i - start, level);
\r
564 } while (i < limit);
\r
566 if (limit < length) {
\r
567 /* there is a separate WS run */
\r
568 runs[runIndex] = new BidiRun(limit, length - limit, bidi.paraLevel);
\r
569 /* For the trailing WS run, bidi.paraLevel is ok even
\r
570 if contextual multiple paragraphs. */
\r
571 if (bidi.paraLevel < minLevel) {
\r
572 minLevel = bidi.paraLevel;
\r
576 /* set the object fields */
\r
578 bidi.runCount = runCount;
\r
580 reorderLine(bidi, minLevel, maxLevel);
\r
582 /* now add the direction flags and adjust the visualLimit's to be just that */
\r
583 /* this loop will also handle the trailing WS run */
\r
585 for (i = 0; i < runCount; ++i) {
\r
586 runs[i].level = levels[runs[i].start];
\r
587 limit = (runs[i].limit += limit);
\r
590 /* Set the embedding level for the trailing WS run. */
\r
591 /* For a RTL paragraph, it will be the *first* run in visual order. */
\r
592 /* For the trailing WS run, bidi.paraLevel is ok even if
\r
593 contextual multiple paragraphs. */
\r
594 if (runIndex < runCount) {
\r
595 int trailingRun = ((bidi.paraLevel & 1) != 0)? 0 : runIndex;
\r
596 runs[trailingRun].level = bidi.paraLevel;
\r
601 /* handle insert LRM/RLM BEFORE/AFTER run */
\r
602 if (bidi.insertPoints.size > 0) {
\r
605 for (ip = 0; ip < bidi.insertPoints.size; ip++) {
\r
606 point = bidi.insertPoints.points[ip];
\r
607 runIndex = getRunFromLogicalIndex(bidi, point.pos);
\r
608 bidi.runs[runIndex].insertRemove |= point.flag;
\r
612 /* handle remove BiDi control characters */
\r
613 if (bidi.controlCount > 0) {
\r
616 for (ic = 0; ic < bidi.length; ic++) {
\r
618 if (Bidi.IsBidiControlChar(c)) {
\r
619 runIndex = getRunFromLogicalIndex(bidi, ic);
\r
620 bidi.runs[runIndex].insertRemove--;
\r
626 static int[] prepareReorder(byte[] levels, byte[] pMinLevel, byte[] pMaxLevel)
\r
629 byte level, minLevel, maxLevel;
\r
631 if (levels == null || levels.length <= 0) {
\r
635 /* determine minLevel and maxLevel */
\r
636 minLevel = Bidi.MAX_EXPLICIT_LEVEL + 1;
\r
638 for (start = levels.length; start>0; ) {
\r
639 level = levels[--start];
\r
640 if (level > Bidi.MAX_EXPLICIT_LEVEL + 1) {
\r
643 if (level < minLevel) {
\r
646 if (level > maxLevel) {
\r
650 pMinLevel[0] = minLevel;
\r
651 pMaxLevel[0] = maxLevel;
\r
653 /* initialize the index map */
\r
654 int[] indexMap = new int[levels.length];
\r
655 for (start = levels.length; start > 0; ) {
\r
657 indexMap[start] = start;
\r
663 static int[] reorderLogical(byte[] levels)
\r
665 byte[] aMinLevel = new byte[1];
\r
666 byte[] aMaxLevel = new byte[1];
\r
667 int start, limit, sumOfSosEos;
\r
668 byte minLevel, maxLevel;
\r
669 int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);
\r
670 if (indexMap == null) {
\r
674 minLevel = aMinLevel[0];
\r
675 maxLevel = aMaxLevel[0];
\r
677 /* nothing to do? */
\r
678 if (minLevel == maxLevel && (minLevel & 1) == 0) {
\r
682 /* reorder only down to the lowest odd level */
\r
685 /* loop maxLevel..minLevel */
\r
689 /* loop for all sequences of levels to reorder at the current maxLevel */
\r
691 /* look for a sequence of levels that are all at >=maxLevel */
\r
692 /* look for the first index of such a sequence */
\r
693 while (start < levels.length && levels[start] < maxLevel) {
\r
696 if (start >= levels.length) {
\r
697 break; /* no more such sequences */
\r
700 /* look for the limit of such a sequence (the index behind it) */
\r
701 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}
\r
704 * sos=start of sequence, eos=end of sequence
\r
706 * The closed (inclusive) interval from sos to eos includes all the logical
\r
707 * and visual indexes within this sequence. They are logically and
\r
708 * visually contiguous and in the same range.
\r
710 * For each run, the new visual index=sos+eos-old visual index;
\r
711 * we pre-add sos+eos into sumOfSosEos ->
\r
712 * new visual index=sumOfSosEos-old visual index;
\r
714 sumOfSosEos = start + limit - 1;
\r
716 /* reorder each index in the sequence */
\r
718 indexMap[start] = sumOfSosEos - indexMap[start];
\r
719 } while (++start < limit);
\r
722 if (limit == levels.length) {
\r
723 break; /* no more such sequences */
\r
728 } while (--maxLevel >= minLevel);
\r
732 static int[] reorderVisual(byte[] levels)
\r
734 byte[] aMinLevel = new byte[1];
\r
735 byte[] aMaxLevel = new byte[1];
\r
736 int start, end, limit, temp;
\r
737 byte minLevel, maxLevel;
\r
739 int[] indexMap = prepareReorder(levels, aMinLevel, aMaxLevel);
\r
740 if (indexMap == null) {
\r
744 minLevel = aMinLevel[0];
\r
745 maxLevel = aMaxLevel[0];
\r
747 /* nothing to do? */
\r
748 if (minLevel == maxLevel && (minLevel & 1) == 0) {
\r
752 /* reorder only down to the lowest odd level */
\r
755 /* loop maxLevel..minLevel */
\r
759 /* loop for all sequences of levels to reorder at the current maxLevel */
\r
761 /* look for a sequence of levels that are all at >=maxLevel */
\r
762 /* look for the first index of such a sequence */
\r
763 while (start < levels.length && levels[start] < maxLevel) {
\r
766 if (start >= levels.length) {
\r
767 break; /* no more such runs */
\r
770 /* look for the limit of such a sequence (the index behind it) */
\r
771 for (limit = start; ++limit < levels.length && levels[limit] >= maxLevel; ) {}
\r
774 * Swap the entire interval of indexes from start to limit-1.
\r
775 * We don't need to swap the levels for the purpose of this
\r
776 * algorithm: the sequence of levels that we look at does not
\r
780 while (start < end) {
\r
781 temp = indexMap[start];
\r
782 indexMap[start] = indexMap[end];
\r
783 indexMap[end] = temp;
\r
789 if (limit == levels.length) {
\r
790 break; /* no more such sequences */
\r
795 } while (--maxLevel >= minLevel);
\r
800 static int getVisualIndex(Bidi bidi, int logicalIndex)
\r
802 int visualIndex = Bidi.MAP_NOWHERE;
\r
804 /* we can do the trivial cases without the runs array */
\r
805 switch(bidi.direction) {
\r
807 visualIndex = logicalIndex;
\r
810 visualIndex = bidi.length - logicalIndex - 1;
\r
814 BidiRun[] runs = bidi.runs;
\r
815 int i, visualStart = 0, offset, length;
\r
817 /* linear search for the run, search on the visual runs */
\r
818 for (i = 0; i < bidi.runCount; ++i) {
\r
819 length = runs[i].limit - visualStart;
\r
820 offset = logicalIndex - runs[i].start;
\r
821 if (offset >= 0 && offset < length) {
\r
822 if (runs[i].isEvenRun()) {
\r
824 visualIndex = visualStart + offset;
\r
827 visualIndex = visualStart + length - offset - 1;
\r
829 break; /* exit for loop */
\r
831 visualStart += length;
\r
833 if (i >= bidi.runCount) {
\r
834 return Bidi.MAP_NOWHERE;
\r
838 if (bidi.insertPoints.size > 0) {
\r
839 /* add the number of added marks until the calculated visual index */
\r
840 BidiRun runs[] = bidi.runs;
\r
841 int i, length, insertRemove;
\r
842 int visualStart = 0, markFound = 0;
\r
843 for (i = 0; ; i++, visualStart += length) {
\r
844 length = runs[i].limit - visualStart;
\r
845 insertRemove = runs[i].insertRemove;
\r
846 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
849 /* is it the run containing the visual index? */
\r
850 if (visualIndex < runs[i].limit) {
\r
851 return visualIndex + markFound;
\r
853 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
858 else if (bidi.controlCount > 0) {
\r
859 /* subtract the number of controls until the calculated visual index */
\r
860 BidiRun[] runs = bidi.runs;
\r
861 int i, j, start, limit, length, insertRemove;
\r
862 int visualStart = 0, controlFound = 0;
\r
863 char uchar = bidi.text[logicalIndex];
\r
864 /* is the logical index pointing to a control ? */
\r
865 if (Bidi.IsBidiControlChar(uchar)) {
\r
866 return Bidi.MAP_NOWHERE;
\r
869 for (i = 0; ; i++, visualStart += length) {
\r
870 length = runs[i].limit - visualStart;
\r
871 insertRemove = runs[i].insertRemove;
\r
872 /* calculated visual index is beyond this run? */
\r
873 if (visualIndex >= runs[i].limit) {
\r
874 controlFound -= insertRemove;
\r
877 /* calculated visual index must be within current run */
\r
878 if (insertRemove == 0) {
\r
879 return visualIndex - controlFound;
\r
881 if (runs[i].isEvenRun()) {
\r
882 /* LTR: check from run start to logical index */
\r
883 start = runs[i].start;
\r
884 limit = logicalIndex;
\r
886 /* RTL: check from logical index to run end */
\r
887 start = logicalIndex + 1;
\r
888 limit = runs[i].start + length;
\r
890 for (j = start; j < limit; j++) {
\r
891 uchar = bidi.text[j];
\r
892 if (Bidi.IsBidiControlChar(uchar)) {
\r
896 return visualIndex - controlFound;
\r
900 return visualIndex;
\r
903 static int getLogicalIndex(Bidi bidi, int visualIndex)
\r
906 int i, runCount, start;
\r
909 runCount = bidi.runCount;
\r
910 if (bidi.insertPoints.size > 0) {
\r
911 /* handle inserted LRM/RLM */
\r
912 int markFound = 0, insertRemove;
\r
913 int visualStart = 0, length;
\r
914 /* subtract number of marks until visual index */
\r
915 for (i = 0; ; i++, visualStart += length) {
\r
916 length = runs[i].limit - visualStart;
\r
917 insertRemove = runs[i].insertRemove;
\r
918 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
919 if (visualIndex <= (visualStart+markFound)) {
\r
920 return Bidi.MAP_NOWHERE;
\r
924 /* is adjusted visual index within this run? */
\r
925 if (visualIndex < (runs[i].limit + markFound)) {
\r
926 visualIndex -= markFound;
\r
929 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
930 if (visualIndex == (visualStart + length + markFound)) {
\r
931 return Bidi.MAP_NOWHERE;
\r
937 else if (bidi.controlCount > 0) {
\r
938 /* handle removed BiDi control characters */
\r
939 int controlFound = 0, insertRemove, length;
\r
940 int logicalStart, logicalEnd, visualStart = 0, j, k;
\r
943 /* add number of controls until visual index */
\r
944 for (i = 0; ; i++, visualStart += length) {
\r
945 length = runs[i].limit - visualStart;
\r
946 insertRemove = runs[i].insertRemove;
\r
947 /* is adjusted visual index beyond current run? */
\r
948 if (visualIndex >= (runs[i].limit - controlFound + insertRemove)) {
\r
949 controlFound -= insertRemove;
\r
952 /* adjusted visual index is within current run */
\r
953 if (insertRemove == 0) {
\r
954 visualIndex += controlFound;
\r
957 /* count non-control chars until visualIndex */
\r
958 logicalStart = runs[i].start;
\r
959 evenRun = runs[i].isEvenRun();
\r
960 logicalEnd = logicalStart + length - 1;
\r
961 for (j = 0; j < length; j++) {
\r
962 k= evenRun ? logicalStart+j : logicalEnd-j;
\r
963 uchar = bidi.text[k];
\r
964 if (Bidi.IsBidiControlChar(uchar)) {
\r
967 if ((visualIndex + controlFound) == (visualStart + j)) {
\r
971 visualIndex += controlFound;
\r
975 /* handle all cases */
\r
976 if (runCount <= 10) {
\r
977 /* linear search for the run */
\r
978 for (i = 0; visualIndex >= runs[i].limit; ++i) {}
\r
980 /* binary search for the run */
\r
981 int begin = 0, limit = runCount;
\r
983 /* the middle if() is guaranteed to find the run, we don't need a loop limit */
\r
985 i = (begin + limit) / 2;
\r
986 if (visualIndex >= runs[i].limit) {
\r
988 } else if (i==0 || visualIndex >= runs[i-1].limit) {
\r
996 start= runs[i].start;
\r
997 if (runs[i].isEvenRun()) {
\r
999 /* the offset in runs[i] is visualIndex-runs[i-1].visualLimit */
\r
1001 visualIndex -= runs[i - 1].limit;
\r
1003 return start + visualIndex;
\r
1006 return start + runs[i].limit - visualIndex - 1;
\r
1010 static int[] getLogicalMap(Bidi bidi)
\r
1012 /* fill a logical-to-visual index map using the runs[] */
\r
1013 BidiRun[] runs = bidi.runs;
\r
1014 int logicalStart, visualStart, logicalLimit, visualLimit;
\r
1015 int[] indexMap = new int[bidi.length];
\r
1016 if (bidi.length > bidi.resultLength) {
\r
1017 Arrays.fill(indexMap, Bidi.MAP_NOWHERE);
\r
1021 for (int j = 0; j < bidi.runCount; ++j) {
\r
1022 logicalStart = runs[j].start;
\r
1023 visualLimit = runs[j].limit;
\r
1024 if (runs[j].isEvenRun()) {
\r
1026 indexMap[logicalStart++] = visualStart++;
\r
1027 } while (visualStart < visualLimit);
\r
1029 logicalStart += visualLimit - visualStart; /* logicalLimit */
\r
1031 indexMap[--logicalStart] = visualStart++;
\r
1032 } while (visualStart < visualLimit);
\r
1034 /* visualStart==visualLimit; */
\r
1037 if (bidi.insertPoints.size > 0) {
\r
1038 int markFound = 0, runCount = bidi.runCount;
\r
1039 int length, insertRemove, i, j;
\r
1042 /* add number of marks found until each index */
\r
1043 for (i = 0; i < runCount; i++, visualStart += length) {
\r
1044 length = runs[i].limit - visualStart;
\r
1045 insertRemove = runs[i].insertRemove;
\r
1046 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
1049 if (markFound > 0) {
\r
1050 logicalStart = runs[i].start;
\r
1051 logicalLimit = logicalStart + length;
\r
1052 for (j = logicalStart; j < logicalLimit; j++) {
\r
1053 indexMap[j] += markFound;
\r
1056 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
1061 else if (bidi.controlCount > 0) {
\r
1062 int controlFound = 0, runCount = bidi.runCount;
\r
1063 int length, insertRemove, i, j, k;
\r
1068 /* subtract number of controls found until each index */
\r
1069 for (i = 0; i < runCount; i++, visualStart += length) {
\r
1070 length = runs[i].limit - visualStart;
\r
1071 insertRemove = runs[i].insertRemove;
\r
1072 /* no control found within previous runs nor within this run */
\r
1073 if ((controlFound - insertRemove) == 0) {
\r
1076 logicalStart = runs[i].start;
\r
1077 evenRun = runs[i].isEvenRun();
\r
1078 logicalLimit = logicalStart + length;
\r
1079 /* if no control within this run */
\r
1080 if (insertRemove == 0) {
\r
1081 for (j = logicalStart; j < logicalLimit; j++) {
\r
1082 indexMap[j] -= controlFound;
\r
1086 for (j = 0; j < length; j++) {
\r
1087 k = evenRun ? logicalStart + j : logicalLimit - j - 1;
\r
1088 uchar = bidi.text[k];
\r
1089 if (Bidi.IsBidiControlChar(uchar)) {
\r
1091 indexMap[k] = Bidi.MAP_NOWHERE;
\r
1094 indexMap[k] -= controlFound;
\r
1101 static int[] getVisualMap(Bidi bidi)
\r
1103 /* fill a visual-to-logical index map using the runs[] */
\r
1104 BidiRun[] runs = bidi.runs;
\r
1105 int logicalStart, visualStart, visualLimit;
\r
1106 int allocLength = bidi.length > bidi.resultLength ? bidi.length
\r
1107 : bidi.resultLength;
\r
1108 int[] indexMap = new int[allocLength];
\r
1112 for (int j = 0; j < bidi.runCount; ++j) {
\r
1113 logicalStart = runs[j].start;
\r
1114 visualLimit = runs[j].limit;
\r
1115 if (runs[j].isEvenRun()) {
\r
1117 indexMap[idx++] = logicalStart++;
\r
1118 } while (++visualStart < visualLimit);
\r
1120 logicalStart += visualLimit - visualStart; /* logicalLimit */
\r
1122 indexMap[idx++] = --logicalStart;
\r
1123 } while (++visualStart < visualLimit);
\r
1125 /* visualStart==visualLimit; */
\r
1128 if (bidi.insertPoints.size > 0) {
\r
1129 int markFound = 0, runCount = bidi.runCount;
\r
1130 int insertRemove, i, j, k;
\r
1132 /* count all inserted marks */
\r
1133 for (i = 0; i < runCount; i++) {
\r
1134 insertRemove = runs[i].insertRemove;
\r
1135 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
1138 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
1142 /* move back indexes by number of preceding marks */
\r
1143 k = bidi.resultLength;
\r
1144 for (i = runCount - 1; i >= 0 && markFound > 0; i--) {
\r
1145 insertRemove = runs[i].insertRemove;
\r
1146 if ((insertRemove & (Bidi.LRM_AFTER|Bidi.RLM_AFTER)) > 0) {
\r
1147 indexMap[--k] = Bidi.MAP_NOWHERE;
\r
1150 visualStart = i > 0 ? runs[i-1].limit : 0;
\r
1151 for (j = runs[i].limit - 1; j >= visualStart && markFound > 0; j--) {
\r
1152 indexMap[--k] = indexMap[j];
\r
1154 if ((insertRemove & (Bidi.LRM_BEFORE|Bidi.RLM_BEFORE)) > 0) {
\r
1155 indexMap[--k] = Bidi.MAP_NOWHERE;
\r
1160 else if (bidi.controlCount > 0) {
\r
1161 int runCount = bidi.runCount, logicalEnd;
\r
1162 int insertRemove, length, i, j, k, m;
\r
1167 /* move forward indexes by number of preceding controls */
\r
1169 for (i = 0; i < runCount; i++, visualStart += length) {
\r
1170 length = runs[i].limit - visualStart;
\r
1171 insertRemove = runs[i].insertRemove;
\r
1172 /* if no control found yet, nothing to do in this run */
\r
1173 if ((insertRemove == 0) && (k == visualStart)) {
\r
1177 /* if no control in this run */
\r
1178 if (insertRemove == 0) {
\r
1179 visualLimit = runs[i].limit;
\r
1180 for (j = visualStart; j < visualLimit; j++) {
\r
1181 indexMap[k++] = indexMap[j];
\r
1185 logicalStart = runs[i].start;
\r
1186 evenRun = runs[i].isEvenRun();
\r
1187 logicalEnd = logicalStart + length - 1;
\r
1188 for (j = 0; j < length; j++) {
\r
1189 m = evenRun ? logicalStart + j : logicalEnd - j;
\r
1190 uchar = bidi.text[m];
\r
1191 if (!Bidi.IsBidiControlChar(uchar)) {
\r
1192 indexMap[k++] = m;
\r
1197 if (allocLength == bidi.resultLength) {
\r
1200 int[] newMap = new int[bidi.resultLength];
\r
1201 System.arraycopy(indexMap, 0, newMap, 0, bidi.resultLength);
\r
1205 static int[] invertMap(int[] srcMap)
\r
1207 int srcLength = srcMap.length;
\r
1208 int destLength = -1, count = 0, i, srcEntry;
\r
1210 /* find highest value and count positive indexes in srcMap */
\r
1211 for (i = 0; i < srcLength; i++) {
\r
1212 srcEntry = srcMap[i];
\r
1213 if (srcEntry > destLength) {
\r
1214 destLength = srcEntry;
\r
1216 if (srcEntry >= 0) {
\r
1220 destLength++; /* add 1 for origin 0 */
\r
1221 int[] destMap = new int[destLength];
\r
1222 if (count < destLength) {
\r
1223 /* we must fill unmatched destMap entries with -1 */
\r
1224 Arrays.fill(destMap, Bidi.MAP_NOWHERE);
\r
1226 for (i = 0; i < srcLength; i++) {
\r
1227 srcEntry = srcMap[i];
\r
1228 if (srcEntry >= 0) {
\r
1229 destMap[srcEntry] = i;
\r