/* * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *//* ******************************************************************************* * (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved * * * * The original version of this source code and documentation is copyrighted * * and owned by IBM, These materials are provided under terms of a License * * Agreement between IBM and Sun. This technology is protected by multiple * * US and International patents. This notice and attribution to IBM may not * * to removed. * ******************************************************************************* *//* Written by Simon Montagu, Matitiahu Allouche * (ported from C code written by Markus W. Scherer) */ package sun.text.bidi; import java.text.Bidi;import java.util.Arrays; public final class BidiLine { /* * General remarks about the functions in this file: * * These functions deal with the aspects of potentially mixed-directional * text in a single paragraph or in a line of a single paragraph * which has already been processed according to * the Unicode 3.0 Bidi algorithm as defined in * http://www.unicode.org/unicode/reports/tr9/ , version 13, * also described in The Unicode Standard, Version 4.0.1 . * * This means that there is a Bidi object with a levels * and a dirProps array. * paraLevel and direction are also set. * Only if the length of the text is zero, then levels==dirProps==NULL. * * The overall directionality of the paragraph * or line is used to bypass the reordering steps if possible. * Even purely RTL text does not need reordering there because * the getLogical/VisualIndex() methods can compute the * index on the fly in such a case. * * The implementation of the access to same-level-runs and of the reordering * do attempt to provide better performance and less memory usage compared to * a direct implementation of especially rule (L2) with an array of * one (32-bit) integer per text character. * * Here, the levels array is scanned as soon as necessary, and a vector of * same-level-runs is created. Reordering then is done on this vector. * For each run of text positions that were resolved to the same level, * only 8 bytes are stored: the first text position of the run and the visual * position behind the run after reordering. * One sign bit is used to hold the directionality of the run. * This is inefficient if there are many very short runs. If the average run * length is <2, then this uses more memory. * * In a further attempt to save memory, the levels array is never changed * after all the resolution rules (Xn, Wn, Nn, In). * Many methods have to consider the field trailingWSStart: * if it is less than length, then there is an implicit trailing run * at the paraLevel, * which is not reflected in the levels array. * This allows a line Bidi object to use the same levels array as * its paragraph parent object. * * When a Bidi object is created for a line of a paragraph, then the * paragraph's levels and dirProps arrays are reused by way of setting * a pointer into them, not by copying. This again saves memory and forbids to * change the now shared levels for (L1). */ /* handle trailing WS (L1) -------------------------------------------------- */ /* * setTrailingWSStart() sets the start index for a trailing * run of WS in the line. This is necessary because we do not modify * the paragraph's levels array that we just point into. * Using trailingWSStart is another form of performing (L1). * * To make subsequent operations easier, we also include the run * before the WS if it is at the paraLevel - we merge the two here. * * This method is called only from setLine(), so paraLevel is * set correctly for the line even when contextual multiple paragraphs. */ static void setTrailingWSStart(BidiBase bidiBase) { byte[] dirProps = bidiBase.dirProps; byte[] levels = bidiBase.levels; int start = bidiBase.length; byte paraLevel = bidiBase.paraLevel; /* If the line is terminated by a block separator, all preceding WS etc... are already set to paragraph level. Setting trailingWSStart to pBidi->length will avoid changing the level of B chars from 0 to paraLevel in getLevels when orderParagraphsLTR==TRUE */ if (BidiBase.NoContextRTL(dirProps[start - 1]) == BidiBase.B) { bidiBase.trailingWSStart = start; /* currently == bidiBase.length */ return; } /* go backwards across all WS, BN, explicit codes */ while (start > 0 && (BidiBase.DirPropFlagNC(dirProps[start - 1]) & BidiBase.MASK_WS) != 0) { --start; } /* if the WS run can be merged with the previous run then do so here */ while (start > 0 && levels[start - 1] == paraLevel) { --start; } bidiBase.trailingWSStart=start; } public static Bidi setLine(Bidi bidi, BidiBase paraBidi, Bidi newBidi, BidiBase newBidiBase, int start, int limit) { int length; BidiBase lineBidi = newBidiBase; /* set the values in lineBidi from its paraBidi parent */ /* class members are already initialized to 0 */ // lineBidi.paraBidi = null; /* mark unfinished setLine */ // lineBidi.flags = 0; // lineBidi.controlCount = 0; length = lineBidi.length = lineBidi.originalLength = lineBidi.resultLength = limit - start; lineBidi.text = new char[length]; System.arraycopy(paraBidi.text, start, lineBidi.text, 0, length); lineBidi.paraLevel = paraBidi.GetParaLevelAt(start); lineBidi.paraCount = paraBidi.paraCount; lineBidi.runs = new BidiRun[0]; if (paraBidi.controlCount > 0) { int j; for (j = start; j < limit; j++) { if (BidiBase.IsBidiControlChar(paraBidi.text[j])) { lineBidi.controlCount++; } } lineBidi.resultLength -= lineBidi.controlCount; } /* copy proper subset of DirProps */ lineBidi.getDirPropsMemory(length); lineBidi.dirProps = lineBidi.dirPropsMemory; System.arraycopy(paraBidi.dirProps, start, lineBidi.dirProps, 0, length); /* copy proper subset of Levels */ lineBidi.getLevelsMemory(length); lineBidi.levels = lineBidi.levelsMemory; System.arraycopy(paraBidi.levels, start, lineBidi.levels, 0, length); lineBidi.runCount = -1; if (paraBidi.direction != BidiBase.MIXED) { /* the parent is already trivial */ lineBidi.direction = paraBidi.direction; /* * The parent's levels are all either * implicitly or explicitly ==paraLevel; * do the same here. */ if (paraBidi.trailingWSStart <= start) { lineBidi.trailingWSStart = 0; } else if (paraBidi.trailingWSStart < limit) { lineBidi.trailingWSStart = paraBidi.trailingWSStart - start; } else { lineBidi.trailingWSStart = length; } } else { byte[] levels = lineBidi.levels; int i, trailingWSStart; byte level; setTrailingWSStart(lineBidi); trailingWSStart = lineBidi.trailingWSStart; /* recalculate lineBidi.direction */ if (trailingWSStart == 0) { /* all levels are at paraLevel */ lineBidi.direction = (byte)(lineBidi.paraLevel & 1); } else { /* get the level of the first character */ level = (byte)(levels[0] & 1); /* if there is anything of a different level, then the line is mixed */ if (trailingWSStart < length && (lineBidi.paraLevel & 1) != level) { /* the trailing WS is at paraLevel, which differs from levels[0] */ lineBidi.direction = BidiBase.MIXED; } else { /* see if levels[1..trailingWSStart-1] have the same direction as levels[0] and paraLevel */ for (i = 1; ; i++) { if (i == trailingWSStart) { /* the direction values match those in level */ lineBidi.direction = level; break; } else if ((levels[i] & 1) != level) { lineBidi.direction = BidiBase.MIXED; break; } } } } switch(lineBidi.direction) { case Bidi.DIRECTION_LEFT_TO_RIGHT: /* make sure paraLevel is even */ lineBidi.paraLevel = (byte) ((lineBidi.paraLevel + 1) & ~1); /* all levels are implicitly at paraLevel (important for getLevels()) */ lineBidi.trailingWSStart = 0; break; case Bidi.DIRECTION_RIGHT_TO_LEFT: /* make sure paraLevel is odd */ lineBidi.paraLevel |= 1; /* all levels are implicitly at paraLevel (important for getLevels()) */ lineBidi.trailingWSStart = 0; break; default: break; } } newBidiBase.paraBidi = paraBidi; /* mark successful setLine */ return newBidi; } static byte getLevelAt(BidiBase bidiBase, int charIndex) { /* return paraLevel if in the trailing WS run, otherwise the real level */ if (bidiBase.direction != BidiBase.MIXED || charIndex >= bidiBase.trailingWSStart) { return bidiBase.GetParaLevelAt(charIndex); } else { return bidiBase.levels[charIndex]; } } static byte[] getLevels(BidiBase bidiBase) { int start = bidiBase.trailingWSStart; int length = bidiBase.length; if (start != length) { /* the current levels array does not reflect the WS run */ /* * After the previous if(), we know that the levels array * has an implicit trailing WS run and therefore does not fully * reflect itself all the levels. * This must be a Bidi object for a line, and * we need to create a new levels array. */ /* bidiBase.paraLevel is ok even if contextual multiple paragraphs, since bidiBase is a line object */ Arrays.fill(bidiBase.levels, start, length, bidiBase.paraLevel); /* this new levels array is set for the line and reflects the WS run */ bidiBase.trailingWSStart = length; } if (length < bidiBase.levels.length) { byte[] levels = new byte[length]; System.arraycopy(bidiBase.levels, 0, levels, 0, length); return levels; } return bidiBase.levels; } static BidiRun getLogicalRun(BidiBase bidiBase, int logicalPosition) { /* this is done based on runs rather than on levels since levels have a special interpretation when REORDER_RUNS_ONLY */ BidiRun newRun = new BidiRun(), iRun; getRuns(bidiBase); int runCount = bidiBase.runCount; int visualStart = 0, logicalLimit = 0; iRun = bidiBase.runs[0]; for (int i = 0; i < runCount; i++) { iRun = bidiBase.runs[i]; logicalLimit = iRun.start + iRun.limit - visualStart; if ((logicalPosition >= iRun.start) && (logicalPosition < logicalLimit)) { break; } visualStart = iRun.limit; } newRun.start = iRun.start; newRun.limit = logicalLimit; newRun.level = iRun.level; return newRun; } /* in trivial cases there is only one trivial run; called by getRuns() */ private static void getSingleRun(BidiBase bidiBase, byte level) { /* simple, single-run case */ bidiBase.runs = bidiBase.simpleRuns; bidiBase.runCount = 1; /* fill and reorder the single run */ bidiBase.runs[0] = new BidiRun(0, bidiBase.length, level); } /* reorder the runs array (L2) ---------------------------------------------- */ /* * Reorder the same-level runs in the runs array. * Here, runCount>1 and maxLevel>=minLevel>=paraLevel. * All the visualStart fields=logical start before reordering. * The "odd" bits are not set yet. * * Reordering with this data structure lends itself to some handy shortcuts: * * Since each run is moved but not modified, and since at the initial maxLevel * each sequence of same-level runs consists of only one run each, we * don't need to do anything there and can predecrement maxLevel. * In many simple cases, the reordering is thus done entirely in the * index mapping. * Also, reordering occurs only down to the lowest odd level that occurs, * which is minLevel|1. However, if the lowest level itself is odd, then * in the last reordering the sequence of the runs at this level or higher * will be all runs, and we don't need the elaborate loop to search for them. * This is covered by ++minLevel instead of minLevel|=1 followed * by an extra reorder-all after the reorder-some loop. * About a trailing WS run: * Such a run would need special treatment because its level is not * reflected in levels[] if this is not a paragraph object. * Instead, all characters from trailingWSStart on are implicitly at * paraLevel. * However, for all maxLevel>paraLevel, this run will never be reordered * and does not need to be taken into account. maxLevel==paraLevel is only reordered * if minLevel==paraLevel is odd, which is done in the extra segment. * This means that for the main reordering loop we don't need to consider * this run and can --runCount. If it is later part of the all-runs * reordering, then runCount is adjusted accordingly. */ private static void reorderLine(BidiBase bidiBase, byte minLevel, byte maxLevel) { /* nothing to do? */ if (maxLevel<=(minLevel|1)) { return; } BidiRun[] runs; BidiRun tempRun; byte[] levels; int firstRun, endRun, limitRun, runCount; /* * Reorder only down to the lowest odd level * and reorder at an odd minLevel in a separate, simpler loop. * See comments above for why minLevel is always incremented. */ ++minLevel; runs = bidiBase.runs; levels = bidiBase.levels; runCount = bidiBase.runCount; /* do not include the WS run at paraLevel<=old minLevel except in the simple loop */ if (bidiBase.trailingWSStart < bidiBase.length) { --runCount; } while (--maxLevel >= minLevel) { firstRun = 0; /* loop for all sequences of runs */ for ( ; ; ) { /* look for a sequence of runs that are all at >=maxLevel */ /* look for the first run of such a sequence */ while (firstRun < runCount && levels[runs[firstRun].start] < maxLevel) { ++firstRun; } if (firstRun >= runCount) { break; /* no more such runs */ } /* look for the limit run of such a sequence (the run behind it) */ for (limitRun = firstRun; ++limitRun < runCount && levels[runs[limitRun].start]>=maxLevel; ) {} /* Swap the entire sequence of runs from firstRun to limitRun-1. */ endRun = limitRun - 1; while (firstRun < endRun) { tempRun = runs[firstRun]; runs[firstRun] = runs[endRun]; runs[endRun] = tempRun; ++firstRun; --endRun; } if (limitRun == runCount) { break; /* no more such runs */ } else { firstRun = limitRun + 1; } } } /* now do maxLevel==old minLevel (==odd!), see above */ if ((minLevel & 1) == 0) { firstRun = 0; /* include the trailing WS run in this complete reordering */ if (bidiBase.trailingWSStart == bidiBase.length) { --runCount; } /* Swap the entire sequence of all runs. (endRun==runCount) */ while (firstRun < runCount) { tempRun = runs[firstRun]; runs[firstRun] = runs[runCount]; runs[runCount] = tempRun; ++firstRun; --runCount; } } } /* compute the runs array --------------------------------------------------- */ static int getRunFromLogicalIndex(BidiBase bidiBase, int logicalIndex) { BidiRun[] runs = bidiBase.runs; int runCount = bidiBase.runCount, visualStart = 0, i, length, logicalStart; for (i = 0; i < runCount; i++) { length = runs[i].limit - visualStart; logicalStart = runs[i].start; if ((logicalIndex >= logicalStart) && (logicalIndex < (logicalStart+length))) { return i; } visualStart += length; } /* we should never get here */ throw new IllegalStateException("Internal ICU error in getRunFromLogicalIndex"); } /* * Compute the runs array from the levels array. * After getRuns() returns true, runCount is guaranteed to be >0 * and the runs are reordered. * Odd-level runs have visualStart on their visual right edge and * they progress visually to the left. * If option OPTION_INSERT_MARKS is set, insertRemove will contain the * sum of appropriate LRM/RLM_BEFORE/AFTER flags. * If option OPTION_REMOVE_CONTROLS is set, insertRemove will contain the * negative number of BiDi control characters within this run. */ static void getRuns(BidiBase bidiBase) { /* * This method returns immediately if the runs are already set. This * includes the case of length==0 (handled in setPara).. */ if (bidiBase.runCount >= 0) { return; } if (bidiBase.direction != BidiBase.MIXED) { /* simple, single-run case - this covers length==0 */ /* bidiBase.paraLevel is ok even for contextual multiple paragraphs */ getSingleRun(bidiBase, bidiBase.paraLevel); } else /* BidiBase.MIXED, length>0 */ {Lines 1–500 of 849. Load more
/* * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* ******************************************************************************* * (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved * * * * The original version of this source code and documentation is copyrighted * * and owned by IBM, These materials are provided under terms of a License * * Agreement between IBM and Sun. This technology is protected by multiple * * US and International patents. This notice and attribution to IBM may not * * to removed. * ******************************************************************************* */ /* Written by Simon Montagu, Matitiahu Allouche * (ported from C code written by Markus W. Scherer) */ package sun.text.bidi; import java.text.Bidi; import java.util.Arrays; public final class BidiLine { /* * General remarks about the functions in this file: * * These functions deal with the aspects of potentially mixed-directional * text in a single paragraph or in a line of a single paragraph * which has already been processed according to * the Unicode 3.0 Bidi algorithm as defined in * http://www.unicode.org/unicode/reports/tr9/ , version 13, * also described in The Unicode Standard, Version 4.0.1 . * * This means that there is a Bidi object with a levels * and a dirProps array. * paraLevel and direction are also set. * Only if the length of the text is zero, then levels==dirProps==NULL. * * The overall directionality of the paragraph * or line is used to bypass the reordering steps if possible. * Even purely RTL text does not need reordering there because * the getLogical/VisualIndex() methods can compute the * index on the fly in such a case. * * The implementation of the access to same-level-runs and of the reordering * do attempt to provide better performance and less memory usage compared to * a direct implementation of especially rule (L2) with an array of * one (32-bit) integer per text character. * * Here, the levels array is scanned as soon as necessary, and a vector of * same-level-runs is created. Reordering then is done on this vector. * For each run of text positions that were resolved to the same level, * only 8 bytes are stored: the first text position of the run and the visual * position behind the run after reordering. * One sign bit is used to hold the directionality of the run. * This is inefficient if there are many very short runs. If the average run * length is <2, then this uses more memory. * * In a further attempt to save memory, the levels array is never changed * after all the resolution rules (Xn, Wn, Nn, In). * Many methods have to consider the field trailingWSStart: * if it is less than length, then there is an implicit trailing run * at the paraLevel, * which is not reflected in the levels array. * This allows a line Bidi object to use the same levels array as * its paragraph parent object. * * When a Bidi object is created for a line of a paragraph, then the * paragraph's levels and dirProps arrays are reused by way of setting * a pointer into them, not by copying. This again saves memory and forbids to * change the now shared levels for (L1). */ /* handle trailing WS (L1) -------------------------------------------------- */ /* * setTrailingWSStart() sets the start index for a trailing * run of WS in the line. This is necessary because we do not modify * the paragraph's levels array that we just point into. * Using trailingWSStart is another form of performing (L1). * * To make subsequent operations easier, we also include the run * before the WS if it is at the paraLevel - we merge the two here. * * This method is called only from setLine(), so paraLevel is * set correctly for the line even when contextual multiple paragraphs. */ static void setTrailingWSStart(BidiBase bidiBase) { byte[] dirProps = bidiBase.dirProps; byte[] levels = bidiBase.levels; int start = bidiBase.length; byte paraLevel = bidiBase.paraLevel; /* If the line is terminated by a block separator, all preceding WS etc... are already set to paragraph level. Setting trailingWSStart to pBidi->length will avoid changing the level of B chars from 0 to paraLevel in getLevels when orderParagraphsLTR==TRUE */ if (BidiBase.NoContextRTL(dirProps[start - 1]) == BidiBase.B) { bidiBase.trailingWSStart = start; /* currently == bidiBase.length */ return; } /* go backwards across all WS, BN, explicit codes */ while (start > 0 && (BidiBase.DirPropFlagNC(dirProps[start - 1]) & BidiBase.MASK_WS) != 0) { --start; } /* if the WS run can be merged with the previous run then do so here */ while (start > 0 && levels[start - 1] == paraLevel) { --start; } bidiBase.trailingWSStart=start; } public static Bidi setLine(Bidi bidi, BidiBase paraBidi, Bidi newBidi, BidiBase newBidiBase, int start, int limit) { int length; BidiBase lineBidi = newBidiBase; /* set the values in lineBidi from its paraBidi parent */ /* class members are already initialized to 0 */ // lineBidi.paraBidi = null; /* mark unfinished setLine */ // lineBidi.flags = 0; // lineBidi.controlCount = 0; length = lineBidi.length = lineBidi.originalLength = lineBidi.resultLength = limit - start; lineBidi.text = new char[length]; System.arraycopy(paraBidi.text, start, lineBidi.text, 0, length); lineBidi.paraLevel = paraBidi.GetParaLevelAt(start); lineBidi.paraCount = paraBidi.paraCount; lineBidi.runs = new BidiRun[0]; if (paraBidi.controlCount > 0) { int j; for (j = start; j < limit; j++) { if (BidiBase.IsBidiControlChar(paraBidi.text[j])) { lineBidi.controlCount++; } } lineBidi.resultLength -= lineBidi.controlCount; } /* copy proper subset of DirProps */ lineBidi.getDirPropsMemory(length); lineBidi.dirProps = lineBidi.dirPropsMemory; System.arraycopy(paraBidi.dirProps, start, lineBidi.dirProps, 0, length); /* copy proper subset of Levels */ lineBidi.getLevelsMemory(length); lineBidi.levels = lineBidi.levelsMemory; System.arraycopy(paraBidi.levels, start, lineBidi.levels, 0, length); lineBidi.runCount = -1; if (paraBidi.direction != BidiBase.MIXED) { /* the parent is already trivial */ lineBidi.direction = paraBidi.direction; /* * The parent's levels are all either * implicitly or explicitly ==paraLevel; * do the same here. */ if (paraBidi.trailingWSStart <= start) { lineBidi.trailingWSStart = 0; } else if (paraBidi.trailingWSStart < limit) { lineBidi.trailingWSStart = paraBidi.trailingWSStart - start; } else { lineBidi.trailingWSStart = length; } } else { byte[] levels = lineBidi.levels; int i, trailingWSStart; byte level; setTrailingWSStart(lineBidi); trailingWSStart = lineBidi.trailingWSStart; /* recalculate lineBidi.direction */ if (trailingWSStart == 0) { /* all levels are at paraLevel */ lineBidi.direction = (byte)(lineBidi.paraLevel & 1); } else { /* get the level of the first character */ level = (byte)(levels[0] & 1); /* if there is anything of a different level, then the line is mixed */ if (trailingWSStart < length && (lineBidi.paraLevel & 1) != level) { /* the trailing WS is at paraLevel, which differs from levels[0] */ lineBidi.direction = BidiBase.MIXED; } else { /* see if levels[1..trailingWSStart-1] have the same direction as levels[0] and paraLevel */ for (i = 1; ; i++) { if (i == trailingWSStart) { /* the direction values match those in level */ lineBidi.direction = level; break; } else if ((levels[i] & 1) != level) { lineBidi.direction = BidiBase.MIXED; break; } } } } switch(lineBidi.direction) { case Bidi.DIRECTION_LEFT_TO_RIGHT: /* make sure paraLevel is even */ lineBidi.paraLevel = (byte) ((lineBidi.paraLevel + 1) & ~1); /* all levels are implicitly at paraLevel (important for getLevels()) */ lineBidi.trailingWSStart = 0; break; case Bidi.DIRECTION_RIGHT_TO_LEFT: /* make sure paraLevel is odd */ lineBidi.paraLevel |= 1; /* all levels are implicitly at paraLevel (important for getLevels()) */ lineBidi.trailingWSStart = 0; break; default: break; } } newBidiBase.paraBidi = paraBidi; /* mark successful setLine */ return newBidi; } static byte getLevelAt(BidiBase bidiBase, int charIndex) { /* return paraLevel if in the trailing WS run, otherwise the real level */ if (bidiBase.direction != BidiBase.MIXED || charIndex >= bidiBase.trailingWSStart) { return bidiBase.GetParaLevelAt(charIndex); } else { return bidiBase.levels[charIndex]; } } static byte[] getLevels(BidiBase bidiBase) { int start = bidiBase.trailingWSStart; int length = bidiBase.length; if (start != length) { /* the current levels array does not reflect the WS run */ /* * After the previous if(), we know that the levels array * has an implicit trailing WS run and therefore does not fully * reflect itself all the levels. * This must be a Bidi object for a line, and * we need to create a new levels array. */ /* bidiBase.paraLevel is ok even if contextual multiple paragraphs, since bidiBase is a line object */ Arrays.fill(bidiBase.levels, start, length, bidiBase.paraLevel); /* this new levels array is set for the line and reflects the WS run */ bidiBase.trailingWSStart = length; } if (length < bidiBase.levels.length) { byte[] levels = new byte[length]; System.arraycopy(bidiBase.levels, 0, levels, 0, length); return levels; } return bidiBase.levels; } static BidiRun getLogicalRun(BidiBase bidiBase, int logicalPosition) { /* this is done based on runs rather than on levels since levels have a special interpretation when REORDER_RUNS_ONLY */ BidiRun newRun = new BidiRun(), iRun; getRuns(bidiBase); int runCount = bidiBase.runCount; int visualStart = 0, logicalLimit = 0; iRun = bidiBase.runs[0]; for (int i = 0; i < runCount; i++) { iRun = bidiBase.runs[i]; logicalLimit = iRun.start + iRun.limit - visualStart; if ((logicalPosition >= iRun.start) && (logicalPosition < logicalLimit)) { break; } visualStart = iRun.limit; } newRun.start = iRun.start; newRun.limit = logicalLimit; newRun.level = iRun.level; return newRun; } /* in trivial cases there is only one trivial run; called by getRuns() */ private static void getSingleRun(BidiBase bidiBase, byte level) { /* simple, single-run case */ bidiBase.runs = bidiBase.simpleRuns; bidiBase.runCount = 1; /* fill and reorder the single run */ bidiBase.runs[0] = new BidiRun(0, bidiBase.length, level); } /* reorder the runs array (L2) ---------------------------------------------- */ /* * Reorder the same-level runs in the runs array. * Here, runCount>1 and maxLevel>=minLevel>=paraLevel. * All the visualStart fields=logical start before reordering. * The "odd" bits are not set yet. * * Reordering with this data structure lends itself to some handy shortcuts: * * Since each run is moved but not modified, and since at the initial maxLevel * each sequence of same-level runs consists of only one run each, we * don't need to do anything there and can predecrement maxLevel. * In many simple cases, the reordering is thus done entirely in the * index mapping. * Also, reordering occurs only down to the lowest odd level that occurs, * which is minLevel|1. However, if the lowest level itself is odd, then * in the last reordering the sequence of the runs at this level or higher * will be all runs, and we don't need the elaborate loop to search for them. * This is covered by ++minLevel instead of minLevel|=1 followed * by an extra reorder-all after the reorder-some loop. * About a trailing WS run: * Such a run would need special treatment because its level is not * reflected in levels[] if this is not a paragraph object. * Instead, all characters from trailingWSStart on are implicitly at * paraLevel. * However, for all maxLevel>paraLevel, this run will never be reordered * and does not need to be taken into account. maxLevel==paraLevel is only reordered * if minLevel==paraLevel is odd, which is done in the extra segment. * This means that for the main reordering loop we don't need to consider * this run and can --runCount. If it is later part of the all-runs * reordering, then runCount is adjusted accordingly. */ private static void reorderLine(BidiBase bidiBase, byte minLevel, byte maxLevel) { /* nothing to do? */ if (maxLevel<=(minLevel|1)) { return; } BidiRun[] runs; BidiRun tempRun; byte[] levels; int firstRun, endRun, limitRun, runCount; /* * Reorder only down to the lowest odd level * and reorder at an odd minLevel in a separate, simpler loop. * See comments above for why minLevel is always incremented. */ ++minLevel; runs = bidiBase.runs; levels = bidiBase.levels; runCount = bidiBase.runCount; /* do not include the WS run at paraLevel<=old minLevel except in the simple loop */ if (bidiBase.trailingWSStart < bidiBase.length) { --runCount; } while (--maxLevel >= minLevel) { firstRun = 0; /* loop for all sequences of runs */ for ( ; ; ) { /* look for a sequence of runs that are all at >=maxLevel */ /* look for the first run of such a sequence */ while (firstRun < runCount && levels[runs[firstRun].start] < maxLevel) { ++firstRun; } if (firstRun >= runCount) { break; /* no more such runs */ } /* look for the limit run of such a sequence (the run behind it) */ for (limitRun = firstRun; ++limitRun < runCount && levels[runs[limitRun].start]>=maxLevel; ) {} /* Swap the entire sequence of runs from firstRun to limitRun-1. */ endRun = limitRun - 1; while (firstRun < endRun) { tempRun = runs[firstRun]; runs[firstRun] = runs[endRun]; runs[endRun] = tempRun; ++firstRun; --endRun; } if (limitRun == runCount) { break; /* no more such runs */ } else { firstRun = limitRun + 1; } } } /* now do maxLevel==old minLevel (==odd!), see above */ if ((minLevel & 1) == 0) { firstRun = 0; /* include the trailing WS run in this complete reordering */ if (bidiBase.trailingWSStart == bidiBase.length) { --runCount; } /* Swap the entire sequence of all runs. (endRun==runCount) */ while (firstRun < runCount) { tempRun = runs[firstRun]; runs[firstRun] = runs[runCount]; runs[runCount] = tempRun; ++firstRun; --runCount; } } } /* compute the runs array --------------------------------------------------- */ static int getRunFromLogicalIndex(BidiBase bidiBase, int logicalIndex) { BidiRun[] runs = bidiBase.runs; int runCount = bidiBase.runCount, visualStart = 0, i, length, logicalStart; for (i = 0; i < runCount; i++) { length = runs[i].limit - visualStart; logicalStart = runs[i].start; if ((logicalIndex >= logicalStart) && (logicalIndex < (logicalStart+length))) { return i; } visualStart += length; } /* we should never get here */ throw new IllegalStateException("Internal ICU error in getRunFromLogicalIndex"); } /* * Compute the runs array from the levels array. * After getRuns() returns true, runCount is guaranteed to be >0 * and the runs are reordered. * Odd-level runs have visualStart on their visual right edge and * they progress visually to the left. * If option OPTION_INSERT_MARKS is set, insertRemove will contain the * sum of appropriate LRM/RLM_BEFORE/AFTER flags. * If option OPTION_REMOVE_CONTROLS is set, insertRemove will contain the * negative number of BiDi control characters within this run. */ static void getRuns(BidiBase bidiBase) { /* * This method returns immediately if the runs are already set. This * includes the case of length==0 (handled in setPara).. */ if (bidiBase.runCount >= 0) { return; } if (bidiBase.direction != BidiBase.MIXED) { /* simple, single-run case - this covers length==0 */ /* bidiBase.paraLevel is ok even for contextual multiple paragraphs */ getSingleRun(bidiBase, bidiBase.paraLevel); } else /* BidiBase.MIXED, length>0 */ {