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