/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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)
*/
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.
*/
{
/* 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
*/
return;
}
/* go backwards across all WS, BN, explicit codes */
while (start > 0 &&
--start;
}
/* if the WS run can be merged with the previous run then do so here */
--start;
}
}
int length;
/* 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;
int j;
}
}
}
/* copy proper subset of DirProps */
length);
/* copy proper subset of Levels */
length);
/* the parent is already trivial */
/*
* The parent's levels are all either
* implicitly or explicitly ==paraLevel;
* do the same here.
*/
} else {
}
} else {
int i, trailingWSStart;
byte level;
/* recalculate lineBidi.direction */
if (trailingWSStart == 0) {
/* all levels are at paraLevel */
} else {
/* get the level of the first character */
/* if there is anything of a different level, then the line
is mixed */
if (trailingWSStart < length &&
/* the trailing WS is at paraLevel, which differs from
levels[0] */
} 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 */
break;
break;
}
}
}
}
case Bidi.DIRECTION_LEFT_TO_RIGHT:
/* make sure paraLevel is even */
/* all levels are implicitly at paraLevel (important for
getLevels()) */
break;
case Bidi.DIRECTION_RIGHT_TO_LEFT:
/* make sure paraLevel is odd */
/* all levels are implicitly at paraLevel (important for
getLevels()) */
break;
default:
break;
}
}
return newBidi;
}
{
/* return paraLevel if in the trailing WS run, otherwise the real level */
} else {
}
}
{
/* 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 */
/* this new levels array is set for the line and reflects the WS run */
}
return levels;
}
}
{
/* this is done based on runs rather than on levels since levels have
a special interpretation when REORDER_RUNS_ONLY
*/
for (int i = 0; i < runCount; i++) {
(logicalPosition < logicalLimit)) {
break;
}
}
return newRun;
}
/* in trivial cases there is only one trivial run; called by getRuns() */
/* simple, single-run case */
/* fill and reorder the single run */
}
/* 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.
*/
/* nothing to do? */
return;
}
byte[] levels;
/*
* 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;
/* do not include the WS run at paraLevel<=old minLevel except in the simple loop */
--runCount;
}
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 */
++firstRun;
}
break; /* no more such runs */
}
/* look for the limit run of such a sequence (the run behind it) */
/* Swap the entire sequence of runs from firstRun to limitRun-1. */
++firstRun;
--endRun;
}
break; /* no more such runs */
} else {
}
}
}
/* now do maxLevel==old minLevel (==odd!), see above */
firstRun = 0;
/* include the trailing WS run in this complete reordering */
--runCount;
}
/* Swap the entire sequence of all runs. (endRun==runCount) */
++firstRun;
--runCount;
}
}
}
/* compute the runs array --------------------------------------------------- */
for (i = 0; i < runCount; i++) {
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.
*/
/*
* This method returns immediately if the runs are already set. This
* includes the case of length==0 (handled in setPara)..
*/
return;
}
/* simple, single-run case - this covers length==0 */
/* bidiBase.paraLevel is ok even for contextual multiple paragraphs */
} else /* BidiBase.MIXED, length>0 */ {
/* mixed directionality */
int i, runCount;
/*
* If there are WS characters at the end of the line
* and the run preceding them has a level different from
* paraLevel, then they will form their own run at paraLevel (L1).
* Count them separately.
* We need some special treatment for this in order to not
* modify the levels array which a line Bidi object shares
* with its paragraph parent and its other line siblings.
* In other words, for the trailing WS, it may be
* levels[]!=paraLevel but we have to treat it like it were so.
*/
/* count the runs, there is at least one non-WS run, and limit>0 */
runCount = 0;
for (i = 0; i < limit; ++i) {
/* increment runCount at the start of each run */
++runCount;
}
}
/*
* We don't need to see if the last run can be merged with a trailing
* WS run because setTrailingWSStart() would have done that.
*/
/* There is only one non-WS run and no trailing WS-run. */
} else /* runCount>1 || limit<length */ {
/* allocate and set the runs */
byte maxLevel=0;
/* now, count a (non-mergeable) WS run */
++runCount;
}
/* runCount > 1 */
/* set the runs */
/* FOOD FOR THOUGHT: this could be optimized, e.g.:
* 464->444, 484->444, 575->555, 595->555
* However, that would take longer. Check also how it would
* interact with BiDi control removal and inserting Marks.
*/
runIndex = 0;
/* search for the run limits and initialize visualLimit values with the run lengths */
i = 0;
do {
/* prepare this run */
start = i;
}
}
/* look for the run limit */
/* i is another run limit */
++runIndex;
} while (i < limit);
/* there is a separate WS run */
/* For the trailing WS run, bidiBase.paraLevel is ok even
if contextual multiple paragraphs. */
}
}
/* set the object fields */
/* now add the direction flags and adjust the visualLimit's to be just that */
/* this loop will also handle the trailing WS run */
limit = 0;
for (i = 0; i < runCount; ++i) {
}
/* Set the embedding level for the trailing WS run. */
/* For a RTL paragraph, it will be the *first* run in visual order. */
/* For the trailing WS run, bidiBase.paraLevel is ok even if
contextual multiple paragraphs. */
}
}
}
}
}
/* handle remove BiDi control characters */
char c;
if (BidiBase.IsBidiControlChar(c)) {
}
}
}
}
{
int start;
return null;
}
/* determine minLevel and maxLevel */
maxLevel = 0;
return null;
}
}
}
}
/* initialize the index map */
--start;
}
return indexMap;
}
{
byte[] aMinLevel = new byte[1];
byte[] aMaxLevel = new byte[1];
return null;
}
/* nothing to do? */
return indexMap;
}
/* reorder only down to the lowest odd level */
minLevel |= 1;
/* loop maxLevel..minLevel */
do {
start = 0;
/* loop for all sequences of levels to reorder at the current maxLevel */
for ( ; ; ) {
/* look for a sequence of levels that are all at >=maxLevel */
/* look for the first index of such a sequence */
++start;
}
break; /* no more such runs */
}
/* look for the limit of such a sequence (the index behind it) */
/*
* Swap the entire interval of indexes from start to limit-1.
* We don't need to swap the levels for the purpose of this
* algorithm: the sequence of levels that we look at does not
* move anyway.
*/
++start;
--end;
}
break; /* no more such sequences */
} else {
}
}
return indexMap;
}
{
/* fill a visual-to-logical index map using the runs[] */
int[] indexMap = new int[allocLength];
visualStart = 0;
int idx = 0;
do { /* LTR */
} while (++visualStart < visualLimit);
} else {
do { /* RTL */
} while (++visualStart < visualLimit);
}
/* visualStart==visualLimit; */
}
int insertRemove, i, j, k;
/* count all inserted marks */
for (i = 0; i < runCount; i++) {
markFound++;
}
markFound++;
}
}
/* move back indexes by number of preceding marks */
k = bidiBase.resultLength;
markFound--;
}
}
markFound--;
}
}
}
int insertRemove, length, i, j, k, m;
char uchar;
boolean evenRun;
visualStart = 0;
/* move forward indexes by number of preceding controls */
k = 0;
/* if no control found yet, nothing to do in this run */
k += length;
continue;
}
/* if no control in this run */
if (insertRemove == 0) {
for (j = visualStart; j < visualLimit; j++) {
}
continue;
}
for (j = 0; j < length; j++) {
indexMap[k++] = m;
}
}
}
}
return indexMap;
}
return newMap;
}
}