jdk/src/share/classes/sun/text/bidi/BidiBase.java
author peytoia
Tue, 12 Jul 2011 08:46:49 +0900
changeset 10090 8571585915b6
parent 10089 297652d14d44
child 11136 f0f53bbe5bd1
permissions -rw-r--r--
7051769: java.text.Bidi.toString() output is wrong Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4235
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
     4
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4235
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4235
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    10
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    15
 * accompanied this code).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    16
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4235
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4235
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4235
diff changeset
    23
 * questions.
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    24
 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    25
/*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    26
 *******************************************************************************
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    27
 * (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved         *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    28
 *                                                                             *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    29
 * The original version of this source code and documentation is copyrighted   *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    30
 * and owned by IBM, These materials are provided under terms of a License     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    31
 * Agreement between IBM and Sun. This technology is protected by multiple     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    32
 * US and International patents. This notice and attribution to IBM may not    *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    33
 * to removed.                                                                 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    34
 *******************************************************************************
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    35
 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    36
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    37
/* FOOD FOR THOUGHT: currently the reordering modes are a mixture of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    38
 * algorithm for direct BiDi, algorithm for inverse Bidi and the bizarre
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    39
 * concept of RUNS_ONLY which is a double operation.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    40
 * It could be advantageous to divide this into 3 concepts:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    41
 * a) Operation: direct / inverse / RUNS_ONLY
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    42
 * b) Direct algorithm: default / NUMBERS_SPECIAL / GROUP_NUMBERS_WITH_L
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    43
 * c) Inverse algorithm: default / INVERSE_LIKE_DIRECT / NUMBERS_SPECIAL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    44
 * This would allow combinations not possible today like RUNS_ONLY with
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    45
 * NUMBERS_SPECIAL.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    46
 * Also allow to set INSERT_MARKS for the direct step of RUNS_ONLY and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    47
 * REMOVE_CONTROLS for the inverse step.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    48
 * Not all combinations would be supported, and probably not all do make sense.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    49
 * This would need to document which ones are supported and what are the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    50
 * fallbacks for unsupported combinations.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    51
 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    52
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    53
package sun.text.bidi;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    54
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    55
import java.io.IOException;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    56
import java.lang.reflect.Array;
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
    57
import java.lang.reflect.Field;
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
    58
import java.lang.reflect.Method;
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
    59
import java.lang.reflect.InvocationTargetException;
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    60
import java.text.AttributedCharacterIterator;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    61
import java.text.Bidi;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    62
import java.util.Arrays;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    63
import java.util.MissingResourceException;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    64
import sun.text.normalizer.UBiDiProps;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    65
import sun.text.normalizer.UCharacter;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    66
import sun.text.normalizer.UTF16;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    67
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    68
/**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    69
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    70
 * <h2>Bidi algorithm for ICU</h2>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    71
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    72
 * This is an implementation of the Unicode Bidirectional algorithm. The
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    73
 * algorithm is defined in the <a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    74
 * href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    75
 * version 13, also described in The Unicode Standard, Version 4.0 .
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    76
 * <p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    77
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    78
 * Note: Libraries that perform a bidirectional algorithm and reorder strings
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    79
 * accordingly are sometimes called "Storage Layout Engines". ICU's Bidi and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    80
 * shaping (ArabicShaping) classes can be used at the core of such "Storage
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    81
 * Layout Engines".
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    82
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    83
 * <h3>General remarks about the API:</h3>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    84
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    85
 * The &quot;limit&quot; of a sequence of characters is the position just after
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    86
 * their last character, i.e., one more than that position.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    87
 * <p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    88
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    89
 * Some of the API methods provide access to &quot;runs&quot;. Such a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    90
 * &quot;run&quot; is defined as a sequence of characters that are at the same
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    91
 * embedding level after performing the Bidi algorithm.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    92
 * <p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    93
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    94
 * <h3>Basic concept: paragraph</h3>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    95
 * A piece of text can be divided into several paragraphs by characters
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    96
 * with the Bidi class <code>Block Separator</code>. For handling of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    97
 * paragraphs, see:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    98
 * <ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
    99
 * <li>{@link #countParagraphs}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   100
 * <li>{@link #getParaLevel}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   101
 * <li>{@link #getParagraph}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   102
 * <li>{@link #getParagraphByIndex}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   103
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   104
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   105
 * <h3>Basic concept: text direction</h3>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   106
 * The direction of a piece of text may be:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   107
 * <ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   108
 * <li>{@link #LTR}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   109
 * <li>{@link #RTL}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   110
 * <li>{@link #MIXED}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   111
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   112
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   113
 * <h3>Basic concept: levels</h3>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   114
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   115
 * Levels in this API represent embedding levels according to the Unicode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   116
 * Bidirectional Algorithm.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   117
 * Their low-order bit (even/odd value) indicates the visual direction.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   118
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   119
 * Levels can be abstract values when used for the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   120
 * <code>paraLevel</code> and <code>embeddingLevels</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   121
 * arguments of <code>setPara()</code>; there:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   122
 * <ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   123
 * <li>the high-order bit of an <code>embeddingLevels[]</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   124
 * value indicates whether the using application is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   125
 * specifying the level of a character to <i>override</i> whatever the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   126
 * Bidi implementation would resolve it to.</li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   127
 * <li><code>paraLevel</code> can be set to the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   128
 * pseudo-level values <code>LEVEL_DEFAULT_LTR</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   129
 * and <code>LEVEL_DEFAULT_RTL</code>.</li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   130
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   131
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   132
 * <p>The related constants are not real, valid level values.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   133
 * <code>DEFAULT_XXX</code> can be used to specify
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   134
 * a default for the paragraph level for
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   135
 * when the <code>setPara()</code> method
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   136
 * shall determine it but there is no
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   137
 * strongly typed character in the input.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   138
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   139
 * Note that the value for <code>LEVEL_DEFAULT_LTR</code> is even
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   140
 * and the one for <code>LEVEL_DEFAULT_RTL</code> is odd,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   141
 * just like with normal LTR and RTL level values -
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   142
 * these special values are designed that way. Also, the implementation
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   143
 * assumes that MAX_EXPLICIT_LEVEL is odd.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   144
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   145
 * <ul><b>See Also:</b>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   146
 * <li>{@link #LEVEL_DEFAULT_LTR}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   147
 * <li>{@link #LEVEL_DEFAULT_RTL}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   148
 * <li>{@link #LEVEL_OVERRIDE}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   149
 * <li>{@link #MAX_EXPLICIT_LEVEL}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   150
 * <li>{@link #setPara}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   151
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   152
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   153
 * <h3>Basic concept: Reordering Mode</h3>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   154
 * Reordering mode values indicate which variant of the Bidi algorithm to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   155
 * use.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   156
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   157
 * <ul><b>See Also:</b>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   158
 * <li>{@link #setReorderingMode}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   159
 * <li>{@link #REORDER_DEFAULT}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   160
 * <li>{@link #REORDER_NUMBERS_SPECIAL}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   161
 * <li>{@link #REORDER_GROUP_NUMBERS_WITH_R}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   162
 * <li>{@link #REORDER_RUNS_ONLY}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   163
 * <li>{@link #REORDER_INVERSE_NUMBERS_AS_L}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   164
 * <li>{@link #REORDER_INVERSE_LIKE_DIRECT}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   165
 * <li>{@link #REORDER_INVERSE_FOR_NUMBERS_SPECIAL}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   166
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   167
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   168
 * <h3>Basic concept: Reordering Options</h3>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   169
 * Reordering options can be applied during Bidi text transformations.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   170
 * <ul><b>See Also:</b>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   171
 * <li>{@link #setReorderingOptions}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   172
 * <li>{@link #OPTION_DEFAULT}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   173
 * <li>{@link #OPTION_INSERT_MARKS}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   174
 * <li>{@link #OPTION_REMOVE_CONTROLS}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   175
 * <li>{@link #OPTION_STREAMING}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   176
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   177
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   178
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   179
 * @author Simon Montagu, Matitiahu Allouche (ported from C code written by Markus W. Scherer)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   180
 * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   181
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   182
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   183
 * <h4> Sample code for the ICU Bidi API </h4>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   184
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   185
 * <h5>Rendering a paragraph with the ICU Bidi API</h5>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   186
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   187
 * This is (hypothetical) sample code that illustrates how the ICU Bidi API
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   188
 * could be used to render a paragraph of text. Rendering code depends highly on
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   189
 * the graphics system, therefore this sample code must make a lot of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   190
 * assumptions, which may or may not match any existing graphics system's
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   191
 * properties.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   192
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   193
 * <p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   194
 * The basic assumptions are:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   195
 * </p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   196
 * <ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   197
 * <li>Rendering is done from left to right on a horizontal line.</li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   198
 * <li>A run of single-style, unidirectional text can be rendered at once.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   199
 * </li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   200
 * <li>Such a run of text is passed to the graphics system with characters
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   201
 * (code units) in logical order.</li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   202
 * <li>The line-breaking algorithm is very complicated and Locale-dependent -
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   203
 * and therefore its implementation omitted from this sample code.</li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   204
 * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   205
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   206
 * <pre>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   207
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   208
 *  package com.ibm.icu.dev.test.bidi;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   209
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   210
 *  import com.ibm.icu.text.Bidi;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   211
 *  import com.ibm.icu.text.BidiRun;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   212
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   213
 *  public class Sample {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   214
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   215
 *      static final int styleNormal = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   216
 *      static final int styleSelected = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   217
 *      static final int styleBold = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   218
 *      static final int styleItalics = 4;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   219
 *      static final int styleSuper=8;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   220
 *      static final int styleSub = 16;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   221
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   222
 *      static class StyleRun {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   223
 *          int limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   224
 *          int style;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   225
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   226
 *          public StyleRun(int limit, int style) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   227
 *              this.limit = limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   228
 *              this.style = style;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   229
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   230
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   231
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   232
 *      static class Bounds {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   233
 *          int start;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   234
 *          int limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   235
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   236
 *          public Bounds(int start, int limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   237
 *              this.start = start;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   238
 *              this.limit = limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   239
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   240
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   241
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   242
 *      static int getTextWidth(String text, int start, int limit,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   243
 *                              StyleRun[] styleRuns, int styleRunCount) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   244
 *          // simplistic way to compute the width
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   245
 *          return limit - start;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   246
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   247
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   248
 *      // set limit and StyleRun limit for a line
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   249
 *      // from text[start] and from styleRuns[styleRunStart]
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   250
 *      // using Bidi.getLogicalRun(...)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   251
 *      // returns line width
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   252
 *      static int getLineBreak(String text, Bounds line, Bidi para,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   253
 *                              StyleRun styleRuns[], Bounds styleRun) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   254
 *          // dummy return
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   255
 *          return 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   256
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   257
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   258
 *      // render runs on a line sequentially, always from left to right
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   259
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   260
 *      // prepare rendering a new line
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   261
 *      static void startLine(byte textDirection, int lineWidth) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   262
 *          System.out.println();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   263
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   264
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   265
 *      // render a run of text and advance to the right by the run width
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   266
 *      // the text[start..limit-1] is always in logical order
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   267
 *      static void renderRun(String text, int start, int limit,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   268
 *                            byte textDirection, int style) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   269
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   270
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   271
 *      // We could compute a cross-product
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   272
 *      // from the style runs with the directional runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   273
 *      // and then reorder it.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   274
 *      // Instead, here we iterate over each run type
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   275
 *      // and render the intersections -
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   276
 *      // with shortcuts in simple (and common) cases.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   277
 *      // renderParagraph() is the main function.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   278
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   279
 *      // render a directional run with
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   280
 *      // (possibly) multiple style runs intersecting with it
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   281
 *      static void renderDirectionalRun(String text, int start, int limit,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   282
 *                                       byte direction, StyleRun styleRuns[],
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   283
 *                                       int styleRunCount) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   284
 *          int i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   285
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   286
 *          // iterate over style runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   287
 *          if (direction == Bidi.LTR) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   288
 *              int styleLimit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   289
 *              for (i = 0; i < styleRunCount; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   290
 *                  styleLimit = styleRuns[i].limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   291
 *                  if (start < styleLimit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   292
 *                      if (styleLimit > limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   293
 *                          styleLimit = limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   294
 *                      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   295
 *                      renderRun(text, start, styleLimit,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   296
 *                                direction, styleRuns[i].style);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   297
 *                      if (styleLimit == limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   298
 *                          break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   299
 *                      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   300
 *                      start = styleLimit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   301
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   302
 *              }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   303
 *          } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   304
 *              int styleStart;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   305
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   306
 *              for (i = styleRunCount-1; i >= 0; --i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   307
 *                  if (i > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   308
 *                      styleStart = styleRuns[i-1].limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   309
 *                  } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   310
 *                      styleStart = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   311
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   312
 *                  if (limit >= styleStart) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   313
 *                      if (styleStart < start) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   314
 *                          styleStart = start;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   315
 *                      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   316
 *                      renderRun(text, styleStart, limit, direction,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   317
 *                                styleRuns[i].style);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   318
 *                      if (styleStart == start) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   319
 *                          break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   320
 *                      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   321
 *                      limit = styleStart;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   322
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   323
 *              }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   324
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   325
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   326
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   327
 *      // the line object represents text[start..limit-1]
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   328
 *      static void renderLine(Bidi line, String text, int start, int limit,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   329
 *                             StyleRun styleRuns[], int styleRunCount) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   330
 *          byte direction = line.getDirection();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   331
 *          if (direction != Bidi.MIXED) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   332
 *              // unidirectional
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   333
 *              if (styleRunCount <= 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   334
 *                  renderRun(text, start, limit, direction, styleRuns[0].style);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   335
 *              } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   336
 *                  renderDirectionalRun(text, start, limit, direction,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   337
 *                                       styleRuns, styleRunCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   338
 *              }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   339
 *          } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   340
 *              // mixed-directional
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   341
 *              int count, i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   342
 *              BidiRun run;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   343
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   344
 *              try {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   345
 *                  count = line.countRuns();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   346
 *              } catch (IllegalStateException e) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   347
 *                  e.printStackTrace();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   348
 *                  return;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   349
 *              }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   350
 *              if (styleRunCount <= 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   351
 *                  int style = styleRuns[0].style;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   352
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   353
 *                  // iterate over directional runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   354
 *                  for (i = 0; i < count; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   355
 *                      run = line.getVisualRun(i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   356
 *                      renderRun(text, run.getStart(), run.getLimit(),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   357
 *                                run.getDirection(), style);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   358
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   359
 *              } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   360
 *                  // iterate over both directional and style runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   361
 *                  for (i = 0; i < count; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   362
 *                      run = line.getVisualRun(i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   363
 *                      renderDirectionalRun(text, run.getStart(),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   364
 *                                           run.getLimit(), run.getDirection(),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   365
 *                                           styleRuns, styleRunCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   366
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   367
 *              }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   368
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   369
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   370
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   371
 *      static void renderParagraph(String text, byte textDirection,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   372
 *                                  StyleRun styleRuns[], int styleRunCount,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   373
 *                                  int lineWidth) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   374
 *          int length = text.length();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   375
 *          Bidi para = new Bidi();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   376
 *          try {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   377
 *              para.setPara(text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   378
 *                           textDirection != 0 ? Bidi.LEVEL_DEFAULT_RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   379
 *                                              : Bidi.LEVEL_DEFAULT_LTR,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   380
 *                           null);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   381
 *          } catch (Exception e) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   382
 *              e.printStackTrace();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   383
 *              return;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   384
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   385
 *          byte paraLevel = (byte)(1 & para.getParaLevel());
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   386
 *          StyleRun styleRun = new StyleRun(length, styleNormal);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   387
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   388
 *          if (styleRuns == null || styleRunCount <= 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   389
 *              styleRuns = new StyleRun[1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   390
 *              styleRunCount = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   391
 *              styleRuns[0] = styleRun;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   392
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   393
 *          // assume styleRuns[styleRunCount-1].limit>=length
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   394
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   395
 *          int width = getTextWidth(text, 0, length, styleRuns, styleRunCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   396
 *          if (width <= lineWidth) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   397
 *              // everything fits onto one line
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   398
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   399
 *              // prepare rendering a new line from either left or right
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   400
 *              startLine(paraLevel, width);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   401
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   402
 *              renderLine(para, text, 0, length, styleRuns, styleRunCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   403
 *          } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   404
 *              // we need to render several lines
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   405
 *              Bidi line = new Bidi(length, 0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   406
 *              int start = 0, limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   407
 *              int styleRunStart = 0, styleRunLimit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   408
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   409
 *              for (;;) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   410
 *                  limit = length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   411
 *                  styleRunLimit = styleRunCount;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   412
 *                  width = getLineBreak(text, new Bounds(start, limit),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   413
 *                                       para, styleRuns,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   414
 *                                       new Bounds(styleRunStart, styleRunLimit));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   415
 *                  try {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   416
 *                      line = para.setLine(start, limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   417
 *                  } catch (Exception e) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   418
 *                      e.printStackTrace();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   419
 *                      return;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   420
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   421
 *                  // prepare rendering a new line
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   422
 *                  // from either left or right
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   423
 *                  startLine(paraLevel, width);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   424
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   425
 *                  if (styleRunStart > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   426
 *                      int newRunCount = styleRuns.length - styleRunStart;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   427
 *                      StyleRun[] newRuns = new StyleRun[newRunCount];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   428
 *                      System.arraycopy(styleRuns, styleRunStart, newRuns, 0,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   429
 *                                       newRunCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   430
 *                      renderLine(line, text, start, limit, newRuns,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   431
 *                                 styleRunLimit - styleRunStart);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   432
 *                  } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   433
 *                      renderLine(line, text, start, limit, styleRuns,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   434
 *                                 styleRunLimit - styleRunStart);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   435
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   436
 *                  if (limit == length) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   437
 *                      break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   438
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   439
 *                  start = limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   440
 *                  styleRunStart = styleRunLimit - 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   441
 *                  if (start >= styleRuns[styleRunStart].limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   442
 *                      ++styleRunStart;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   443
 *                  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   444
 *              }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   445
 *          }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   446
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   447
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   448
 *      public static void main(String[] args)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   449
 *      {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   450
 *          renderParagraph("Some Latin text...", Bidi.LTR, null, 0, 80);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   451
 *          renderParagraph("Some Hebrew text...", Bidi.RTL, null, 0, 60);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   452
 *      }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   453
 *  }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   454
 *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   455
 * </pre>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   456
 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   457
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   458
public class BidiBase {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   459
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   460
    class Point {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   461
        int pos;    /* position in text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   462
        int flag;   /* flag for LRM/RLM, before/after */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   463
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   464
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   465
    class InsertPoints {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   466
        int size;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   467
        int confirmed;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   468
        Point[] points = new Point[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   469
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   470
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   471
    /** Paragraph level setting<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   472
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   473
     * Constant indicating that the base direction depends on the first strong
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   474
     * directional character in the text according to the Unicode Bidirectional
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   475
     * Algorithm. If no strong directional character is present,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   476
     * then set the paragraph level to 0 (left-to-right).<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   477
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   478
     * If this value is used in conjunction with reordering modes
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   479
     * <code>REORDER_INVERSE_LIKE_DIRECT</code> or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   480
     * <code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the text to reorder
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   481
     * is assumed to be visual LTR, and the text after reordering is required
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   482
     * to be the corresponding logical string with appropriate contextual
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   483
     * direction. The direction of the result string will be RTL if either
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   484
     * the righmost or leftmost strong character of the source text is RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   485
     * or Arabic Letter, the direction will be LTR otherwise.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   486
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   487
     * If reordering option <code>OPTION_INSERT_MARKS</code> is set, an RLM may
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   488
     * be added at the beginning of the result string to ensure round trip
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   489
     * (that the result string, when reordered back to visual, will produce
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   490
     * the original source text).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   491
     * @see #REORDER_INVERSE_LIKE_DIRECT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   492
     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   493
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   494
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   495
    public static final byte INTERNAL_LEVEL_DEFAULT_LTR = (byte)0x7e;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   496
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   497
    /** Paragraph level setting<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   498
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   499
     * Constant indicating that the base direction depends on the first strong
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   500
     * directional character in the text according to the Unicode Bidirectional
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   501
     * Algorithm. If no strong directional character is present,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   502
     * then set the paragraph level to 1 (right-to-left).<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   503
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   504
     * If this value is used in conjunction with reordering modes
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   505
     * <code>REORDER_INVERSE_LIKE_DIRECT</code> or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   506
     * <code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the text to reorder
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   507
     * is assumed to be visual LTR, and the text after reordering is required
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   508
     * to be the corresponding logical string with appropriate contextual
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   509
     * direction. The direction of the result string will be RTL if either
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   510
     * the righmost or leftmost strong character of the source text is RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   511
     * or Arabic Letter, or if the text contains no strong character;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   512
     * the direction will be LTR otherwise.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   513
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   514
     * If reordering option <code>OPTION_INSERT_MARKS</code> is set, an RLM may
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   515
     * be added at the beginning of the result string to ensure round trip
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   516
     * (that the result string, when reordered back to visual, will produce
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   517
     * the original source text).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   518
     * @see #REORDER_INVERSE_LIKE_DIRECT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   519
     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   520
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   521
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   522
    public static final byte INTERNAL_LEVEL_DEFAULT_RTL = (byte)0x7f;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   523
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   524
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   525
     * Maximum explicit embedding level.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   526
     * (The maximum resolved level can be up to <code>MAX_EXPLICIT_LEVEL+1</code>).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   527
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   528
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   529
    public static final byte MAX_EXPLICIT_LEVEL = 61;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   530
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   531
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   532
     * Bit flag for level input.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   533
     * Overrides directional properties.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   534
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   535
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   536
    public static final byte INTERNAL_LEVEL_OVERRIDE = (byte)0x80;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   537
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   538
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   539
     * Special value which can be returned by the mapping methods when a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   540
     * logical index has no corresponding visual index or vice-versa. This may
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   541
     * happen for the logical-to-visual mapping of a Bidi control when option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   542
     * <code>OPTION_REMOVE_CONTROLS</code> is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   543
     * specified. This can also happen for the visual-to-logical mapping of a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   544
     * Bidi mark (LRM or RLM) inserted by option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   545
     * <code>OPTION_INSERT_MARKS</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   546
     * @see #getVisualIndex
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   547
     * @see #getVisualMap
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   548
     * @see #getLogicalIndex
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   549
     * @see #getLogicalMap
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   550
     * @see #OPTION_INSERT_MARKS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   551
     * @see #OPTION_REMOVE_CONTROLS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   552
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   553
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   554
    public static final int MAP_NOWHERE = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   555
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   556
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   557
     * Mixed-directional text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   558
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   559
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   560
    public static final byte MIXED = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   561
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   562
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   563
     * option bit for writeReordered():
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   564
     * replace characters with the "mirrored" property in RTL runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   565
     * by their mirror-image mappings
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   566
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   567
     * @see #writeReordered
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   568
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   569
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   570
    public static final short DO_MIRRORING = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   571
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   572
    /** Reordering mode: Regular Logical to Visual Bidi algorithm according to Unicode.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   573
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   574
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   575
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   576
    private static final short REORDER_DEFAULT = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   577
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   578
    /** Reordering mode: Logical to Visual algorithm which handles numbers in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   579
     * a way which mimicks the behavior of Windows XP.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   580
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   581
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   582
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   583
    private static final short REORDER_NUMBERS_SPECIAL = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   584
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   585
    /** Reordering mode: Logical to Visual algorithm grouping numbers with
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   586
     * adjacent R characters (reversible algorithm).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   587
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   588
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   589
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   590
    private static final short REORDER_GROUP_NUMBERS_WITH_R = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   591
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   592
    /** Reordering mode: Reorder runs only to transform a Logical LTR string
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   593
     * to the logical RTL string with the same display, or vice-versa.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   594
     * If this mode is set together with option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   595
     * <code>OPTION_INSERT_MARKS</code>, some Bidi controls in the source
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   596
     * text may be removed and other controls may be added to produce the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   597
     * minimum combination which has the required display.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   598
     * @see #OPTION_INSERT_MARKS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   599
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   600
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   601
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   602
    private static final short REORDER_RUNS_ONLY = 3;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   603
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   604
    /** Reordering mode: Visual to Logical algorithm which handles numbers
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   605
     * like L (same algorithm as selected by <code>setInverse(true)</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   606
     * @see #setInverse
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   607
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   608
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   609
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   610
    private static final short REORDER_INVERSE_NUMBERS_AS_L = 4;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   611
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   612
    /** Reordering mode: Visual to Logical algorithm equivalent to the regular
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   613
     * Logical to Visual algorithm.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   614
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   615
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   616
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   617
    private static final short REORDER_INVERSE_LIKE_DIRECT = 5;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   618
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   619
    /** Reordering mode: Inverse Bidi (Visual to Logical) algorithm for the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   620
     * <code>REORDER_NUMBERS_SPECIAL</code> Bidi algorithm.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   621
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   622
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   623
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   624
    private static final short REORDER_INVERSE_FOR_NUMBERS_SPECIAL = 6;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   625
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   626
    /* Reordering mode values must be ordered so that all the regular logical to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   627
     * visual modes come first, and all inverse Bidi modes come last.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   628
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   629
    private static final short REORDER_LAST_LOGICAL_TO_VISUAL =
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   630
            REORDER_NUMBERS_SPECIAL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   631
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   632
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   633
     * Option bit for <code>setReorderingOptions</code>:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   634
     * insert Bidi marks (LRM or RLM) when needed to ensure correct result of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   635
     * a reordering to a Logical order
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   636
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   637
     * <p>This option must be set or reset before calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   638
     * <code>setPara</code>.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   639
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   640
     * <p>This option is significant only with reordering modes which generate
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   641
     * a result with Logical order, specifically.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   642
     * <ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   643
     *   <li><code>REORDER_RUNS_ONLY</code></li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   644
     *   <li><code>REORDER_INVERSE_NUMBERS_AS_L</code></li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   645
     *   <li><code>REORDER_INVERSE_LIKE_DIRECT</code></li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   646
     *   <li><code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code></li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   647
     * </ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   648
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   649
     * <p>If this option is set in conjunction with reordering mode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   650
     * <code>REORDER_INVERSE_NUMBERS_AS_L</code> or with calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   651
     * <code>setInverse(true)</code>, it implies option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   652
     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to method
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   653
     * <code>writeReordered()</code>.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   654
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   655
     * <p>For other reordering modes, a minimum number of LRM or RLM characters
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   656
     * will be added to the source text after reordering it so as to ensure
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   657
     * round trip, i.e. when applying the inverse reordering mode on the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   658
     * resulting logical text with removal of Bidi marks
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   659
     * (option <code>OPTION_REMOVE_CONTROLS</code> set before calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   660
     * <code>setPara()</code> or option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   661
     * <code>REMOVE_BIDI_CONTROLS</code> in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   662
     * <code>writeReordered</code>), the result will be identical to the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   663
     * source text in the first transformation.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   664
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   665
     * <p>This option will be ignored if specified together with option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   666
     * <code>OPTION_REMOVE_CONTROLS</code>. It inhibits option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   667
     * <code>REMOVE_BIDI_CONTROLS</code> in calls to method
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   668
     * <code>writeReordered()</code> and it implies option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   669
     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to method
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   670
     * <code>writeReordered()</code> if the reordering mode is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   671
     * <code>REORDER_INVERSE_NUMBERS_AS_L</code>.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   672
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   673
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   674
     * @see #setReorderingOptions
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   675
     * @see #INSERT_LRM_FOR_NUMERIC
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   676
     * @see #REMOVE_BIDI_CONTROLS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   677
     * @see #OPTION_REMOVE_CONTROLS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   678
     * @see #REORDER_RUNS_ONLY
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   679
     * @see #REORDER_INVERSE_NUMBERS_AS_L
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   680
     * @see #REORDER_INVERSE_LIKE_DIRECT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   681
     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   682
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   683
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   684
    private static final int OPTION_INSERT_MARKS = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   685
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   686
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   687
     * Option bit for <code>setReorderingOptions</code>:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   688
     * remove Bidi control characters
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   689
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   690
     * <p>This option must be set or reset before calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   691
     * <code>setPara</code>.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   692
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   693
     * <p>This option nullifies option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   694
     * <code>OPTION_INSERT_MARKS</code>. It inhibits option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   695
     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to method
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   696
     * <code>writeReordered()</code> and it implies option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   697
     * <code>REMOVE_BIDI_CONTROLS</code> in calls to that method.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   698
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   699
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   700
     * @see #setReorderingOptions
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   701
     * @see #OPTION_INSERT_MARKS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   702
     * @see #INSERT_LRM_FOR_NUMERIC
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   703
     * @see #REMOVE_BIDI_CONTROLS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   704
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   705
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   706
    private static final int OPTION_REMOVE_CONTROLS = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   707
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   708
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   709
     * Option bit for <code>setReorderingOptions</code>:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   710
     * process the output as part of a stream to be continued
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   711
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   712
     * <p>This option must be set or reset before calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   713
     * <code>setPara</code>.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   714
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   715
     * <p>This option specifies that the caller is interested in processing
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   716
     * large text object in parts. The results of the successive calls are
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   717
     * expected to be concatenated by the caller. Only the call for the last
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   718
     * part will have this option bit off.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   719
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   720
     * <p>When this option bit is on, <code>setPara()</code> may process
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   721
     * less than the full source text in order to truncate the text at a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   722
     * meaningful boundary. The caller should call
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   723
     * <code>getProcessedLength()</code> immediately after calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   724
     * <code>setPara()</code> in order to determine how much of the source
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   725
     * text has been processed. Source text beyond that length should be
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   726
     * resubmitted in following calls to <code>setPara</code>. The
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   727
     * processed length may be less than the length of the source text if a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   728
     * character preceding the last character of the source text constitutes a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   729
     * reasonable boundary (like a block separator) for text to be continued.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   730
     * If the last character of the source text constitutes a reasonable
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   731
     * boundary, the whole text will be processed at once.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   732
     * If nowhere in the source text there exists
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   733
     * such a reasonable boundary, the processed length will be zero.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   734
     * The caller should check for such an occurrence and do one of the following:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   735
     * <ul><li>submit a larger amount of text with a better chance to include
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   736
     *         a reasonable boundary.</li>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   737
     *     <li>resubmit the same text after turning off option
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   738
     *         <code>OPTION_STREAMING</code>.</li></ul>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   739
     * In all cases, this option should be turned off before processing the last
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   740
     * part of the text.</p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   741
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   742
     * <p>When the <code>OPTION_STREAMING</code> option is used, it is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   743
     * recommended to call <code>orderParagraphsLTR()</code> with argument
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   744
     * <code>orderParagraphsLTR</code> set to <code>true</code> before calling
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   745
     * <code>setPara()</code> so that later paragraphs may be concatenated to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   746
     * previous paragraphs on the right.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   747
     * </p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   748
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   749
     * @see #setReorderingMode
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   750
     * @see #setReorderingOptions
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   751
     * @see #getProcessedLength
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   752
     * @see #orderParagraphsLTR
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   753
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   754
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   755
    private static final int OPTION_STREAMING = 4;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   756
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   757
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   758
     *   Comparing the description of the Bidi algorithm with this implementation
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   759
     *   is easier with the same names for the Bidi types in the code as there.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   760
     *   See UCharacterDirection
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   761
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   762
    private static final byte L   = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   763
    private static final byte R   = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   764
    private static final byte EN  = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   765
    private static final byte ES  = 3;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   766
    private static final byte ET  = 4;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   767
    private static final byte AN  = 5;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   768
    private static final byte CS  = 6;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   769
    static final byte B   = 7;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   770
    private static final byte S   = 8;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   771
    private static final byte WS  = 9;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   772
    private static final byte ON  = 10;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   773
    private static final byte LRE = 11;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   774
    private static final byte LRO = 12;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   775
    private static final byte AL  = 13;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   776
    private static final byte RLE = 14;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   777
    private static final byte RLO = 15;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   778
    private static final byte PDF = 16;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   779
    private static final byte NSM = 17;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   780
    private static final byte BN  = 18;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   781
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   782
    private static final int MASK_R_AL = (1 << R | 1 << AL);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   783
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   784
    private static final char CR = '\r';
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   785
    private static final char LF = '\n';
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   786
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   787
    static final int LRM_BEFORE = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   788
    static final int LRM_AFTER = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   789
    static final int RLM_BEFORE = 4;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   790
    static final int RLM_AFTER = 8;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   791
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   792
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   793
     * reference to parent paragraph object (reference to self if this object is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   794
     * a paragraph object); set to null in a newly opened object; set to a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   795
     * real value after a successful execution of setPara or setLine
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   796
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   797
    BidiBase                paraBidi;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   798
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   799
    final UBiDiProps    bdp;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   800
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   801
    /* character array representing the current text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   802
    char[]              text;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   803
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   804
    /* length of the current text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   805
    int                 originalLength;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   806
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   807
    /* if the option OPTION_STREAMING is set, this is the length of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   808
     * text actually processed by <code>setPara</code>, which may be shorter
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   809
     * than the original length. Otherwise, it is identical to the original
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   810
     * length.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   811
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   812
    public int                 length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   813
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   814
    /* if option OPTION_REMOVE_CONTROLS is set, and/or Bidi
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   815
     * marks are allowed to be inserted in one of the reordering modes, the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   816
     * length of the result string may be different from the processed length.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   817
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   818
    int                 resultLength;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   819
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   820
    /* indicators for whether memory may be allocated after construction */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   821
    boolean             mayAllocateText;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   822
    boolean             mayAllocateRuns;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   823
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   824
    /* arrays with one value per text-character */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   825
    byte[]              dirPropsMemory = new byte[1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   826
    byte[]              levelsMemory = new byte[1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   827
    byte[]              dirProps;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   828
    byte[]              levels;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   829
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   830
    /* must block separators receive level 0? */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   831
    boolean             orderParagraphsLTR;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   832
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   833
    /* the paragraph level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   834
    byte                paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   835
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   836
    /* original paraLevel when contextual */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   837
    /* must be one of DEFAULT_xxx or 0 if not contextual */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   838
    byte                defaultParaLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   839
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   840
    /* the following is set in setPara, used in processPropertySeq */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   841
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   842
    ImpTabPair          impTabPair;  /* reference to levels state table pair */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   843
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   844
    /* the overall paragraph or line directionality*/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   845
    byte                direction;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   846
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   847
    /* flags is a bit set for which directional properties are in the text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   848
    int                 flags;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   849
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   850
    /* lastArabicPos is index to the last AL in the text, -1 if none */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   851
    int                 lastArabicPos;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   852
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   853
    /* characters after trailingWSStart are WS and are */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   854
    /* implicitly at the paraLevel (rule (L1)) - levels may not reflect that */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   855
    int                 trailingWSStart;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   856
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   857
    /* fields for paragraph handling */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   858
    int                 paraCount;       /* set in getDirProps() */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   859
    int[]               parasMemory = new int[1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   860
    int[]               paras;           /* limits of paragraphs, filled in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   861
                                          ResolveExplicitLevels() or CheckExplicitLevels() */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   862
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   863
    /* for single paragraph text, we only need a tiny array of paras (no allocation) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   864
    int[]               simpleParas = {0};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   865
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   866
    /* fields for line reordering */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   867
    int                 runCount;     /* ==-1: runs not set up yet */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   868
    BidiRun[]           runsMemory = new BidiRun[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   869
    BidiRun[]           runs;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   870
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   871
    /* for non-mixed text, we only need a tiny array of runs (no allocation) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   872
    BidiRun[]           simpleRuns = {new BidiRun()};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   873
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   874
    /* mapping of runs in logical order to visual order */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   875
    int[]               logicalToVisualRunsMap;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   876
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   877
    /* flag to indicate that the map has been updated */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   878
    boolean             isGoodLogicalToVisualRunsMap;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   879
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   880
    /* for inverse Bidi with insertion of directional marks */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   881
    InsertPoints        insertPoints = new InsertPoints();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   882
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   883
    /* for option OPTION_REMOVE_CONTROLS */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   884
    int                 controlCount;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   885
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   886
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   887
     * Sometimes, bit values are more appropriate
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   888
     * to deal with directionality properties.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   889
     * Abbreviations in these method names refer to names
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   890
     * used in the Bidi algorithm.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   891
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   892
    static int DirPropFlag(byte dir) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   893
        return (1 << dir);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   894
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   895
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   896
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   897
     * The following bit is ORed to the property of characters in paragraphs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   898
     * with contextual RTL direction when paraLevel is contextual.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   899
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   900
    static final byte CONTEXT_RTL_SHIFT = 6;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   901
    static final byte CONTEXT_RTL = (byte)(1<<CONTEXT_RTL_SHIFT);   // 0x40
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   902
    static byte NoContextRTL(byte dir)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   903
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   904
        return (byte)(dir & ~CONTEXT_RTL);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   905
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   906
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   907
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   908
     * The following is a variant of DirProp.DirPropFlag() which ignores the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   909
     * CONTEXT_RTL bit.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   910
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   911
    static int DirPropFlagNC(byte dir) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   912
        return (1<<(dir & ~CONTEXT_RTL));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   913
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   914
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   915
    static final int DirPropFlagMultiRuns = DirPropFlag((byte)31);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   916
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   917
    /* to avoid some conditional statements, use tiny constant arrays */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   918
    static final int DirPropFlagLR[] = { DirPropFlag(L), DirPropFlag(R) };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   919
    static final int DirPropFlagE[] = { DirPropFlag(LRE), DirPropFlag(RLE) };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   920
    static final int DirPropFlagO[] = { DirPropFlag(LRO), DirPropFlag(RLO) };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   921
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   922
    static final int DirPropFlagLR(byte level) { return DirPropFlagLR[level & 1]; }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   923
    static final int DirPropFlagE(byte level)  { return DirPropFlagE[level & 1]; }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   924
    static final int DirPropFlagO(byte level)  { return DirPropFlagO[level & 1]; }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   925
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   926
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   927
     *  are there any characters that are LTR?
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   928
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   929
    static final int MASK_LTR =
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   930
        DirPropFlag(L)|DirPropFlag(EN)|DirPropFlag(AN)|DirPropFlag(LRE)|DirPropFlag(LRO);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   931
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   932
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   933
     *  are there any characters that are RTL?
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   934
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   935
    static final int MASK_RTL = DirPropFlag(R)|DirPropFlag(AL)|DirPropFlag(RLE)|DirPropFlag(RLO);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   936
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   937
    /* explicit embedding codes */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   938
    private static final int MASK_LRX = DirPropFlag(LRE)|DirPropFlag(LRO);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   939
    private static final int MASK_RLX = DirPropFlag(RLE)|DirPropFlag(RLO);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   940
    private static final int MASK_EXPLICIT = MASK_LRX|MASK_RLX|DirPropFlag(PDF);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   941
    private static final int MASK_BN_EXPLICIT = DirPropFlag(BN)|MASK_EXPLICIT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   942
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   943
    /* paragraph and segment separators */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   944
    private static final int MASK_B_S = DirPropFlag(B)|DirPropFlag(S);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   945
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   946
    /* all types that are counted as White Space or Neutral in some steps */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   947
    static final int MASK_WS = MASK_B_S|DirPropFlag(WS)|MASK_BN_EXPLICIT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   948
    private static final int MASK_N = DirPropFlag(ON)|MASK_WS;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   949
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   950
    /* types that are neutrals or could becomes neutrals in (Wn) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   951
    private static final int MASK_POSSIBLE_N = DirPropFlag(CS)|DirPropFlag(ES)|DirPropFlag(ET)|MASK_N;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   952
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   953
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   954
     * These types may be changed to "e",
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   955
     * the embedding type (L or R) of the run,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   956
     * in the Bidi algorithm (N2)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   957
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   958
    static final int MASK_EMBEDDING = DirPropFlag(NSM)|MASK_POSSIBLE_N;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   959
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   960
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   961
     *  the dirProp's L and R are defined to 0 and 1 values in UCharacterDirection.java
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   962
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   963
    private static byte GetLRFromLevel(byte level)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   964
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   965
        return (byte)(level & 1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   966
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   967
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   968
    private static boolean IsDefaultLevel(byte level)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   969
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   970
        return ((level & INTERNAL_LEVEL_DEFAULT_LTR) == INTERNAL_LEVEL_DEFAULT_LTR);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   971
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   972
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   973
    byte GetParaLevelAt(int index)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   974
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   975
        return (defaultParaLevel != 0) ?
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   976
                (byte)(dirProps[index]>>CONTEXT_RTL_SHIFT) : paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   977
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   978
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   979
    static boolean IsBidiControlChar(int c)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   980
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   981
        /* check for range 0x200c to 0x200f (ZWNJ, ZWJ, LRM, RLM) or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   982
                           0x202a to 0x202e (LRE, RLE, PDF, LRO, RLO) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   983
        return (((c & 0xfffffffc) == 0x200c) || ((c >= 0x202a) && (c <= 0x202e)));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   984
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   985
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   986
    public void verifyValidPara()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   987
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   988
        if (this != this.paraBidi) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   989
            throw new IllegalStateException("");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   990
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   991
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   992
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   993
    public void verifyValidParaOrLine()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   994
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   995
        BidiBase para = this.paraBidi;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   996
        /* verify Para */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   997
        if (this == para) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   998
            return;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
   999
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1000
        /* verify Line */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1001
        if ((para == null) || (para != para.paraBidi)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1002
            throw new IllegalStateException();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1003
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1004
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1005
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1006
    public void verifyRange(int index, int start, int limit)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1007
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1008
        if (index < start || index >= limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1009
            throw new IllegalArgumentException("Value " + index +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1010
                      " is out of range " + start + " to " + limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1011
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1012
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1013
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1014
    public void verifyIndex(int index, int start, int limit)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1015
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1016
        if (index < start || index >= limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1017
            throw new ArrayIndexOutOfBoundsException("Index " + index +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1018
                      " is out of range " + start + " to " + limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1019
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1020
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1021
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1022
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1023
     * Allocate a <code>Bidi</code> object with preallocated memory
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1024
     * for internal structures.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1025
     * This method provides a <code>Bidi</code> object like the default constructor
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1026
     * but it also preallocates memory for internal structures
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1027
     * according to the sizings supplied by the caller.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1028
     * The preallocation can be limited to some of the internal memory
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1029
     * by setting some values to 0 here. That means that if, e.g.,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1030
     * <code>maxRunCount</code> cannot be reasonably predetermined and should not
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1031
     * be set to <code>maxLength</code> (the only failproof value) to avoid
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1032
     * wasting  memory, then <code>maxRunCount</code> could be set to 0 here
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1033
     * and the internal structures that are associated with it will be allocated
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1034
     * on demand, just like with the default constructor.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1035
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1036
     * @param maxLength is the maximum text or line length that internal memory
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1037
     *        will be preallocated for. An attempt to associate this object with a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1038
     *        longer text will fail, unless this value is 0, which leaves the allocation
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1039
     *        up to the implementation.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1040
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1041
     * @param maxRunCount is the maximum anticipated number of same-level runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1042
     *        that internal memory will be preallocated for. An attempt to access
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1043
     *        visual runs on an object that was not preallocated for as many runs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1044
     *        as the text was actually resolved to will fail,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1045
     *        unless this value is 0, which leaves the allocation up to the implementation.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1046
     *        The number of runs depends on the actual text and maybe anywhere between
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1047
     *        1 and <code>maxLength</code>. It is typically small.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1048
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1049
     * @throws IllegalArgumentException if maxLength or maxRunCount is less than 0
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1050
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1051
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1052
    public BidiBase(int maxLength, int maxRunCount)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1053
     {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1054
        /* check the argument values */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1055
        if (maxLength < 0 || maxRunCount < 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1056
            throw new IllegalArgumentException();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1057
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1058
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1059
        /* reset the object, all reference variables null, all flags false,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1060
           all sizes 0.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1061
           In fact, we don't need to do anything, since class members are
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1062
           initialized as zero when an instance is created.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1063
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1064
        /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1065
        mayAllocateText = false;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1066
        mayAllocateRuns = false;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1067
        orderParagraphsLTR = false;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1068
        paraCount = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1069
        runCount = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1070
        trailingWSStart = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1071
        flags = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1072
        paraLevel = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1073
        defaultParaLevel = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1074
        direction = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1075
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1076
        /* get Bidi properties */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1077
        try {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1078
            bdp = UBiDiProps.getSingleton();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1079
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1080
        catch (IOException e) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1081
            throw new MissingResourceException(e.getMessage(), "(BidiProps)", "");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1082
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1083
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1084
        /* allocate memory for arrays as requested */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1085
        if (maxLength > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1086
            getInitialDirPropsMemory(maxLength);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1087
            getInitialLevelsMemory(maxLength);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1088
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1089
            mayAllocateText = true;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1090
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1091
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1092
        if (maxRunCount > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1093
            // if maxRunCount == 1, use simpleRuns[]
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1094
            if (maxRunCount > 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1095
                getInitialRunsMemory(maxRunCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1096
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1097
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1098
            mayAllocateRuns = true;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1099
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1100
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1101
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1102
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1103
     * We are allowed to allocate memory if object==null or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1104
     * mayAllocate==true for each array that we need.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1105
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1106
     * Assume sizeNeeded>0.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1107
     * If object != null, then assume size > 0.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1108
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1109
    private Object getMemory(String label, Object array, Class arrayClass,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1110
            boolean mayAllocate, int sizeNeeded)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1111
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1112
        int len = Array.getLength(array);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1113
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1114
        /* we have at least enough memory and must not allocate */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1115
        if (sizeNeeded == len) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1116
            return array;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1117
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1118
        if (!mayAllocate) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1119
            /* we must not allocate */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1120
            if (sizeNeeded <= len) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1121
                return array;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1122
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1123
            throw new OutOfMemoryError("Failed to allocate memory for "
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1124
                                       + label);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1125
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1126
        /* we may try to grow or shrink */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1127
        /* FOOD FOR THOUGHT: when shrinking it should be possible to avoid
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1128
           the allocation altogether and rely on this.length */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1129
        try {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1130
            return Array.newInstance(arrayClass, sizeNeeded);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1131
        } catch (Exception e) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1132
            throw new OutOfMemoryError("Failed to allocate memory for "
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1133
                                       + label);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1134
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1135
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1136
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1137
    /* helper methods for each allocated array */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1138
    private void getDirPropsMemory(boolean mayAllocate, int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1139
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1140
        Object array = getMemory("DirProps", dirPropsMemory, Byte.TYPE, mayAllocate, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1141
        dirPropsMemory = (byte[]) array;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1142
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1143
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1144
    void getDirPropsMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1145
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1146
        getDirPropsMemory(mayAllocateText, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1147
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1148
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1149
    private void getLevelsMemory(boolean mayAllocate, int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1150
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1151
        Object array = getMemory("Levels", levelsMemory, Byte.TYPE, mayAllocate, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1152
        levelsMemory = (byte[]) array;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1153
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1154
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1155
    void getLevelsMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1156
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1157
        getLevelsMemory(mayAllocateText, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1158
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1159
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1160
    private void getRunsMemory(boolean mayAllocate, int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1161
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1162
        Object array = getMemory("Runs", runsMemory, BidiRun.class, mayAllocate, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1163
        runsMemory = (BidiRun[]) array;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1164
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1165
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1166
    void getRunsMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1167
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1168
        getRunsMemory(mayAllocateRuns, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1169
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1170
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1171
    /* additional methods used by constructor - always allow allocation */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1172
    private void getInitialDirPropsMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1173
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1174
        getDirPropsMemory(true, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1175
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1176
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1177
    private void getInitialLevelsMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1178
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1179
        getLevelsMemory(true, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1180
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1181
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1182
    private void getInitialParasMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1183
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1184
        Object array = getMemory("Paras", parasMemory, Integer.TYPE, true, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1185
        parasMemory = (int[]) array;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1186
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1187
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1188
    private void getInitialRunsMemory(int len)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1189
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1190
        getRunsMemory(true, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1191
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1192
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1193
/* perform (P2)..(P3) ------------------------------------------------------- */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1194
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1195
    private void getDirProps()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1196
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1197
        int i = 0, i0, i1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1198
        flags = 0;          /* collect all directionalities in the text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1199
        int uchar;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1200
        byte dirProp;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1201
        byte paraDirDefault = 0;   /* initialize to avoid compiler warnings */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1202
        boolean isDefaultLevel = IsDefaultLevel(paraLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1203
        /* for inverse Bidi, the default para level is set to RTL if there is a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1204
           strong R or AL character at either end of the text                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1205
        lastArabicPos = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1206
        controlCount = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1207
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1208
        final int NOT_CONTEXTUAL = 0;         /* 0: not contextual paraLevel */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1209
        final int LOOKING_FOR_STRONG = 1;     /* 1: looking for first strong char */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1210
        final int FOUND_STRONG_CHAR = 2;      /* 2: found first strong char       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1211
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1212
        int state;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1213
        int paraStart = 0;                    /* index of first char in paragraph */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1214
        byte paraDir;                         /* == CONTEXT_RTL within paragraphs
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1215
                                                 starting with strong R char      */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1216
        byte lastStrongDir=0;                 /* for default level & inverse Bidi */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1217
        int lastStrongLTR=0;                  /* for STREAMING option             */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1218
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1219
        if (isDefaultLevel) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1220
            paraDirDefault = ((paraLevel & 1) != 0) ? CONTEXT_RTL : 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1221
            paraDir = paraDirDefault;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1222
            lastStrongDir = paraDirDefault;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1223
            state = LOOKING_FOR_STRONG;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1224
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1225
            state = NOT_CONTEXTUAL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1226
            paraDir = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1227
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1228
        /* count paragraphs and determine the paragraph level (P2..P3) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1229
        /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1230
         * see comment on constant fields:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1231
         * the LEVEL_DEFAULT_XXX values are designed so that
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1232
         * their low-order bit alone yields the intended default
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1233
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1234
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1235
        for (i = 0; i < originalLength; /* i is incremented in the loop */) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1236
            i0 = i;                     /* index of first code unit */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1237
            uchar = UTF16.charAt(text, 0, originalLength, i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1238
            i += Character.charCount(uchar);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1239
            i1 = i - 1; /* index of last code unit, gets the directional property */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1240
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1241
            dirProp = (byte)bdp.getClass(uchar);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1242
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1243
            flags |= DirPropFlag(dirProp);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1244
            dirProps[i1] = (byte)(dirProp | paraDir);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1245
            if (i1 > i0) {     /* set previous code units' properties to BN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1246
                flags |= DirPropFlag(BN);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1247
                do {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1248
                    dirProps[--i1] = (byte)(BN | paraDir);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1249
                } while (i1 > i0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1250
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1251
            if (state == LOOKING_FOR_STRONG) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1252
                if (dirProp == L) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1253
                    state = FOUND_STRONG_CHAR;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1254
                    if (paraDir != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1255
                        paraDir = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1256
                        for (i1 = paraStart; i1 < i; i1++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1257
                            dirProps[i1] &= ~CONTEXT_RTL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1258
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1259
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1260
                    continue;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1261
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1262
                if (dirProp == R || dirProp == AL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1263
                    state = FOUND_STRONG_CHAR;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1264
                    if (paraDir == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1265
                        paraDir = CONTEXT_RTL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1266
                        for (i1 = paraStart; i1 < i; i1++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1267
                            dirProps[i1] |= CONTEXT_RTL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1268
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1269
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1270
                    continue;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1271
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1272
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1273
            if (dirProp == L) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1274
                lastStrongDir = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1275
                lastStrongLTR = i;      /* i is index to next character */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1276
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1277
            else if (dirProp == R) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1278
                lastStrongDir = CONTEXT_RTL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1279
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1280
            else if (dirProp == AL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1281
                lastStrongDir = CONTEXT_RTL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1282
                lastArabicPos = i-1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1283
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1284
            else if (dirProp == B) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1285
                if (i < originalLength) {   /* B not last char in text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1286
                    if (!((uchar == (int)CR) && (text[i] == (int)LF))) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1287
                        paraCount++;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1288
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1289
                    if (isDefaultLevel) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1290
                        state=LOOKING_FOR_STRONG;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1291
                        paraStart = i;        /* i is index to next character */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1292
                        paraDir = paraDirDefault;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1293
                        lastStrongDir = paraDirDefault;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1294
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1295
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1296
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1297
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1298
        if (isDefaultLevel) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1299
            paraLevel = GetParaLevelAt(0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1300
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1301
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1302
        /* The following line does nothing new for contextual paraLevel, but is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1303
           needed for absolute paraLevel.                               */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1304
        flags |= DirPropFlagLR(paraLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1305
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1306
        if (orderParagraphsLTR && (flags & DirPropFlag(B)) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1307
            flags |= DirPropFlag(L);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1308
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1309
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1310
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1311
    /* perform (X1)..(X9) ------------------------------------------------------- */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1312
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1313
    /* determine if the text is mixed-directional or single-directional */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1314
    private byte directionFromFlags() {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1315
        /* if the text contains AN and neutrals, then some neutrals may become RTL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1316
        if (!((flags & MASK_RTL) != 0 ||
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1317
              ((flags & DirPropFlag(AN)) != 0 &&
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1318
               (flags & MASK_POSSIBLE_N) != 0))) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1319
            return Bidi.DIRECTION_LEFT_TO_RIGHT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1320
        } else if ((flags & MASK_LTR) == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1321
            return Bidi.DIRECTION_RIGHT_TO_LEFT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1322
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1323
            return MIXED;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1324
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1325
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1326
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1327
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1328
     * Resolve the explicit levels as specified by explicit embedding codes.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1329
     * Recalculate the flags to have them reflect the real properties
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1330
     * after taking the explicit embeddings into account.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1331
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1332
     * The Bidi algorithm is designed to result in the same behavior whether embedding
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1333
     * levels are externally specified (from "styled text", supposedly the preferred
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1334
     * method) or set by explicit embedding codes (LRx, RLx, PDF) in the plain text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1335
     * That is why (X9) instructs to remove all explicit codes (and BN).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1336
     * However, in a real implementation, this removal of these codes and their index
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1337
     * positions in the plain text is undesirable since it would result in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1338
     * reallocated, reindexed text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1339
     * Instead, this implementation leaves the codes in there and just ignores them
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1340
     * in the subsequent processing.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1341
     * In order to get the same reordering behavior, positions with a BN or an
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1342
     * explicit embedding code just get the same level assigned as the last "real"
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1343
     * character.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1344
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1345
     * Some implementations, not this one, then overwrite some of these
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1346
     * directionality properties at "real" same-level-run boundaries by
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1347
     * L or R codes so that the resolution of weak types can be performed on the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1348
     * entire paragraph at once instead of having to parse it once more and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1349
     * perform that resolution on same-level-runs.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1350
     * This limits the scope of the implicit rules in effectively
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1351
     * the same way as the run limits.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1352
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1353
     * Instead, this implementation does not modify these codes.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1354
     * On one hand, the paragraph has to be scanned for same-level-runs, but
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1355
     * on the other hand, this saves another loop to reset these codes,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1356
     * or saves making and modifying a copy of dirProps[].
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1357
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1358
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1359
     * Note that (Pn) and (Xn) changed significantly from version 4 of the Bidi algorithm.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1360
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1361
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1362
     * Handling the stack of explicit levels (Xn):
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1363
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1364
     * With the Bidi stack of explicit levels,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1365
     * as pushed with each LRE, RLE, LRO, and RLO and popped with each PDF,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1366
     * the explicit level must never exceed MAX_EXPLICIT_LEVEL==61.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1367
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1368
     * In order to have a correct push-pop semantics even in the case of overflows,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1369
     * there are two overflow counters:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1370
     * - countOver60 is incremented with each LRx at level 60
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1371
     * - from level 60, one RLx increases the level to 61
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1372
     * - countOver61 is incremented with each LRx and RLx at level 61
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1373
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1374
     * Popping levels with PDF must work in the opposite order so that level 61
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1375
     * is correct at the correct point. Underflows (too many PDFs) must be checked.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1376
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1377
     * This implementation assumes that MAX_EXPLICIT_LEVEL is odd.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1378
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1379
    private byte resolveExplicitLevels() {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1380
        int i = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1381
        byte dirProp;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1382
        byte level = GetParaLevelAt(0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1383
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1384
        byte dirct;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1385
        int paraIndex = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1386
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1387
        /* determine if the text is mixed-directional or single-directional */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1388
        dirct = directionFromFlags();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1389
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1390
        /* we may not need to resolve any explicit levels, but for multiple
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1391
           paragraphs we want to loop on all chars to set the para boundaries */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1392
        if ((dirct != MIXED) && (paraCount == 1)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1393
            /* not mixed directionality: levels don't matter - trailingWSStart will be 0 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1394
        } else if ((paraCount == 1) &&
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1395
                   ((flags & MASK_EXPLICIT) == 0)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1396
            /* mixed, but all characters are at the same embedding level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1397
            /* or we are in "inverse Bidi" */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1398
            /* and we don't have contextual multiple paragraphs with some B char */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1399
            /* set all levels to the paragraph level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1400
            for (i = 0; i < length; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1401
                levels[i] = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1402
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1403
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1404
            /* continue to perform (Xn) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1405
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1406
            /* (X1) level is set for all codes, embeddingLevel keeps track of the push/pop operations */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1407
            /* both variables may carry the LEVEL_OVERRIDE flag to indicate the override status */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1408
            byte embeddingLevel = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1409
            byte newLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1410
            byte stackTop = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1411
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1412
            byte[] stack = new byte[MAX_EXPLICIT_LEVEL];    /* we never push anything >=MAX_EXPLICIT_LEVEL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1413
            int countOver60 = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1414
            int countOver61 = 0;  /* count overflows of explicit levels */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1415
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1416
            /* recalculate the flags */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1417
            flags = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1418
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1419
            for (i = 0; i < length; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1420
                dirProp = NoContextRTL(dirProps[i]);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1421
                switch(dirProp) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1422
                case LRE:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1423
                case LRO:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1424
                    /* (X3, X5) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1425
                    newLevel = (byte)((embeddingLevel+2) & ~(INTERNAL_LEVEL_OVERRIDE | 1)); /* least greater even level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1426
                    if (newLevel <= MAX_EXPLICIT_LEVEL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1427
                        stack[stackTop] = embeddingLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1428
                        ++stackTop;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1429
                        embeddingLevel = newLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1430
                        if (dirProp == LRO) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1431
                            embeddingLevel |= INTERNAL_LEVEL_OVERRIDE;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1432
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1433
                        /* we don't need to set LEVEL_OVERRIDE off for LRE
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1434
                           since this has already been done for newLevel which is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1435
                           the source for embeddingLevel.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1436
                         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1437
                    } else if ((embeddingLevel & ~INTERNAL_LEVEL_OVERRIDE) == MAX_EXPLICIT_LEVEL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1438
                        ++countOver61;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1439
                    } else /* (embeddingLevel & ~INTERNAL_LEVEL_OVERRIDE) == MAX_EXPLICIT_LEVEL-1 */ {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1440
                        ++countOver60;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1441
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1442
                    flags |= DirPropFlag(BN);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1443
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1444
                case RLE:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1445
                case RLO:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1446
                    /* (X2, X4) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1447
                    newLevel=(byte)(((embeddingLevel & ~INTERNAL_LEVEL_OVERRIDE) + 1) | 1); /* least greater odd level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1448
                    if (newLevel<=MAX_EXPLICIT_LEVEL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1449
                        stack[stackTop] = embeddingLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1450
                        ++stackTop;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1451
                        embeddingLevel = newLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1452
                        if (dirProp == RLO) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1453
                            embeddingLevel |= INTERNAL_LEVEL_OVERRIDE;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1454
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1455
                        /* we don't need to set LEVEL_OVERRIDE off for RLE
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1456
                           since this has already been done for newLevel which is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1457
                           the source for embeddingLevel.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1458
                         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1459
                    } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1460
                        ++countOver61;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1461
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1462
                    flags |= DirPropFlag(BN);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1463
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1464
                case PDF:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1465
                    /* (X7) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1466
                    /* handle all the overflow cases first */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1467
                    if (countOver61 > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1468
                        --countOver61;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1469
                    } else if (countOver60 > 0 && (embeddingLevel & ~INTERNAL_LEVEL_OVERRIDE) != MAX_EXPLICIT_LEVEL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1470
                        /* handle LRx overflows from level 60 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1471
                        --countOver60;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1472
                    } else if (stackTop > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1473
                        /* this is the pop operation; it also pops level 61 while countOver60>0 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1474
                        --stackTop;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1475
                        embeddingLevel = stack[stackTop];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1476
                    /* } else { (underflow) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1477
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1478
                    flags |= DirPropFlag(BN);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1479
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1480
                case B:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1481
                    stackTop = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1482
                    countOver60 = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1483
                    countOver61 = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1484
                    level = GetParaLevelAt(i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1485
                    if ((i + 1) < length) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1486
                        embeddingLevel = GetParaLevelAt(i+1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1487
                        if (!((text[i] == CR) && (text[i + 1] == LF))) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1488
                            paras[paraIndex++] = i+1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1489
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1490
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1491
                    flags |= DirPropFlag(B);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1492
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1493
                case BN:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1494
                    /* BN, LRE, RLE, and PDF are supposed to be removed (X9) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1495
                    /* they will get their levels set correctly in adjustWSLevels() */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1496
                    flags |= DirPropFlag(BN);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1497
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1498
                default:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1499
                    /* all other types get the "real" level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1500
                    if (level != embeddingLevel) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1501
                        level = embeddingLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1502
                        if ((level & INTERNAL_LEVEL_OVERRIDE) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1503
                            flags |= DirPropFlagO(level) | DirPropFlagMultiRuns;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1504
                        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1505
                            flags |= DirPropFlagE(level) | DirPropFlagMultiRuns;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1506
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1507
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1508
                    if ((level & INTERNAL_LEVEL_OVERRIDE) == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1509
                        flags |= DirPropFlag(dirProp);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1510
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1511
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1512
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1513
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1514
                /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1515
                 * We need to set reasonable levels even on BN codes and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1516
                 * explicit codes because we will later look at same-level runs (X10).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1517
                 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1518
                levels[i] = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1519
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1520
            if ((flags & MASK_EMBEDDING) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1521
                flags |= DirPropFlagLR(paraLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1522
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1523
            if (orderParagraphsLTR && (flags & DirPropFlag(B)) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1524
                flags |= DirPropFlag(L);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1525
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1526
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1527
            /* subsequently, ignore the explicit codes and BN (X9) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1528
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1529
            /* again, determine if the text is mixed-directional or single-directional */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1530
            dirct = directionFromFlags();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1531
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1532
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1533
        return dirct;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1534
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1535
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1536
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1537
     * Use a pre-specified embedding levels array:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1538
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1539
     * Adjust the directional properties for overrides (->LEVEL_OVERRIDE),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1540
     * ignore all explicit codes (X9),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1541
     * and check all the preset levels.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1542
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1543
     * Recalculate the flags to have them reflect the real properties
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1544
     * after taking the explicit embeddings into account.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1545
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1546
    private byte checkExplicitLevels() {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1547
        byte dirProp;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1548
        int i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1549
        this.flags = 0;     /* collect all directionalities in the text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1550
        byte level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1551
        int paraIndex = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1552
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1553
        for (i = 0; i < length; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1554
            if (levels[i] == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1555
                levels[i] = paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1556
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1557
            if (MAX_EXPLICIT_LEVEL < (levels[i]&0x7f)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1558
                if ((levels[i] & INTERNAL_LEVEL_OVERRIDE) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1559
                    levels[i] =  (byte)(paraLevel|INTERNAL_LEVEL_OVERRIDE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1560
                } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1561
                    levels[i] = paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1562
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1563
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1564
            level = levels[i];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1565
            dirProp = NoContextRTL(dirProps[i]);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1566
            if ((level & INTERNAL_LEVEL_OVERRIDE) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1567
                /* keep the override flag in levels[i] but adjust the flags */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1568
                level &= ~INTERNAL_LEVEL_OVERRIDE;     /* make the range check below simpler */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1569
                flags |= DirPropFlagO(level);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1570
            } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1571
                /* set the flags */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1572
                flags |= DirPropFlagE(level) | DirPropFlag(dirProp);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1573
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1574
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1575
            if ((level < GetParaLevelAt(i) &&
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1576
                    !((0 == level) && (dirProp == B))) ||
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1577
                    (MAX_EXPLICIT_LEVEL <level)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1578
                /* level out of bounds */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1579
                throw new IllegalArgumentException("level " + level +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1580
                                                   " out of bounds at index " + i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1581
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1582
            if ((dirProp == B) && ((i + 1) < length)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1583
                if (!((text[i] == CR) && (text[i + 1] == LF))) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1584
                    paras[paraIndex++] = i + 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1585
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1586
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1587
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1588
        if ((flags&MASK_EMBEDDING) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1589
            flags |= DirPropFlagLR(paraLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1590
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1591
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1592
        /* determine if the text is mixed-directional or single-directional */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1593
        return directionFromFlags();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1594
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1595
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1596
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1597
    /* The Properties state machine table                                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1598
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1599
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1600
    /* All table cells are 8 bits:                                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1601
    /*      bits 0..4:  next state                                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1602
    /*      bits 5..7:  action to perform (if > 0)                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1603
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1604
    /* Cells may be of format "n" where n represents the next state      */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1605
    /* (except for the rightmost column).                                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1606
    /* Cells may also be of format "_(x,y)" where x represents an action */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1607
    /* to perform and y represents the next state.                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1608
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1609
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1610
    /* Definitions and type for properties state tables                  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1611
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1612
    private static final int IMPTABPROPS_COLUMNS = 14;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1613
    private static final int IMPTABPROPS_RES = IMPTABPROPS_COLUMNS - 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1614
    private static short GetStateProps(short cell) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1615
        return (short)(cell & 0x1f);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1616
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1617
    private static short GetActionProps(short cell) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1618
        return (short)(cell >> 5);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1619
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1620
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1621
    private static final short groupProp[] =          /* dirProp regrouped */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1622
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1623
        /*  L   R   EN  ES  ET  AN  CS  B   S   WS  ON  LRE LRO AL  RLE RLO PDF NSM BN  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1624
        0,  1,  2,  7,  8,  3,  9,  6,  5,  4,  4,  10, 10, 12, 10, 10, 10, 11, 10
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1625
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1626
    private static final short _L  = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1627
    private static final short _R  = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1628
    private static final short _EN = 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1629
    private static final short _AN = 3;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1630
    private static final short _ON = 4;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1631
    private static final short _S  = 5;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1632
    private static final short _B  = 6; /* reduced dirProp */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1633
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1634
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1635
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1636
    /*      PROPERTIES  STATE  TABLE                                     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1637
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1638
    /* In table impTabProps,                                             */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1639
    /*      - the ON column regroups ON and WS                           */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1640
    /*      - the BN column regroups BN, LRE, RLE, LRO, RLO, PDF         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1641
    /*      - the Res column is the reduced property assigned to a run   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1642
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1643
    /* Action 1: process current run1, init new run1                     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1644
    /*        2: init new run2                                           */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1645
    /*        3: process run1, process run2, init new run1               */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1646
    /*        4: process run1, set run1=run2, init new run2              */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1647
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1648
    /* Notes:                                                            */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1649
    /*  1) This table is used in resolveImplicitLevels().                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1650
    /*  2) This table triggers actions when there is a change in the Bidi*/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1651
    /*     property of incoming characters (action 1).                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1652
    /*  3) Most such property sequences are processed immediately (in    */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1653
    /*     fact, passed to processPropertySeq().                         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1654
    /*  4) However, numbers are assembled as one sequence. This means    */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1655
    /*     that undefined situations (like CS following digits, until    */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1656
    /*     it is known if the next char will be a digit) are held until  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1657
    /*     following chars define them.                                  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1658
    /*     Example: digits followed by CS, then comes another CS or ON;  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1659
    /*              the digits will be processed, then the CS assigned   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1660
    /*              as the start of an ON sequence (action 3).           */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1661
    /*  5) There are cases where more than one sequence must be          */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1662
    /*     processed, for instance digits followed by CS followed by L:  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1663
    /*     the digits must be processed as one sequence, and the CS      */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1664
    /*     must be processed as an ON sequence, all this before starting */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1665
    /*     assembling chars for the opening L sequence.                  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1666
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1667
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1668
    private static final short impTabProps[][] =
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1669
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1670
/*                        L,     R,    EN,    AN,    ON,     S,     B,    ES,    ET,    CS,    BN,   NSM,    AL,  Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1671
/* 0 Init        */ {     1,     2,     4,     5,     7,    15,    17,     7,     9,     7,     0,     7,     3,  _ON },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1672
/* 1 L           */ {     1,  32+2,  32+4,  32+5,  32+7, 32+15, 32+17,  32+7,  32+9,  32+7,     1,     1,  32+3,   _L },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1673
/* 2 R           */ {  32+1,     2,  32+4,  32+5,  32+7, 32+15, 32+17,  32+7,  32+9,  32+7,     2,     2,  32+3,   _R },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1674
/* 3 AL          */ {  32+1,  32+2,  32+6,  32+6,  32+8, 32+16, 32+17,  32+8,  32+8,  32+8,     3,     3,     3,   _R },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1675
/* 4 EN          */ {  32+1,  32+2,     4,  32+5,  32+7, 32+15, 32+17, 64+10,    11, 64+10,     4,     4,  32+3,  _EN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1676
/* 5 AN          */ {  32+1,  32+2,  32+4,     5,  32+7, 32+15, 32+17,  32+7,  32+9, 64+12,     5,     5,  32+3,  _AN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1677
/* 6 AL:EN/AN    */ {  32+1,  32+2,     6,     6,  32+8, 32+16, 32+17,  32+8,  32+8, 64+13,     6,     6,  32+3,  _AN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1678
/* 7 ON          */ {  32+1,  32+2,  32+4,  32+5,     7, 32+15, 32+17,     7, 64+14,     7,     7,     7,  32+3,  _ON },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1679
/* 8 AL:ON       */ {  32+1,  32+2,  32+6,  32+6,     8, 32+16, 32+17,     8,     8,     8,     8,     8,  32+3,  _ON },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1680
/* 9 ET          */ {  32+1,  32+2,     4,  32+5,     7, 32+15, 32+17,     7,     9,     7,     9,     9,  32+3,  _ON },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1681
/*10 EN+ES/CS    */ {  96+1,  96+2,     4,  96+5, 128+7, 96+15, 96+17, 128+7,128+14, 128+7,    10, 128+7,  96+3,  _EN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1682
/*11 EN+ET       */ {  32+1,  32+2,     4,  32+5,  32+7, 32+15, 32+17,  32+7,    11,  32+7,    11,    11,  32+3,  _EN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1683
/*12 AN+CS       */ {  96+1,  96+2,  96+4,     5, 128+7, 96+15, 96+17, 128+7,128+14, 128+7,    12, 128+7,  96+3,  _AN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1684
/*13 AL:EN/AN+CS */ {  96+1,  96+2,     6,     6, 128+8, 96+16, 96+17, 128+8, 128+8, 128+8,    13, 128+8,  96+3,  _AN },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1685
/*14 ON+ET       */ {  32+1,  32+2, 128+4,  32+5,     7, 32+15, 32+17,     7,    14,     7,    14,    14,  32+3,  _ON },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1686
/*15 S           */ {  32+1,  32+2,  32+4,  32+5,  32+7,    15, 32+17,  32+7,  32+9,  32+7,    15,  32+7,  32+3,   _S },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1687
/*16 AL:S        */ {  32+1,  32+2,  32+6,  32+6,  32+8,    16, 32+17,  32+8,  32+8,  32+8,    16,  32+8,  32+3,   _S },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1688
/*17 B           */ {  32+1,  32+2,  32+4,  32+5,  32+7, 32+15,    17,  32+7,  32+9,  32+7,    17,  32+7,  32+3,   _B }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1689
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1690
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1691
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1692
    /* The levels state machine tables                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1693
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1694
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1695
    /* All table cells are 8 bits:                                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1696
    /*      bits 0..3:  next state                                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1697
    /*      bits 4..7:  action to perform (if > 0)                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1698
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1699
    /* Cells may be of format "n" where n represents the next state      */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1700
    /* (except for the rightmost column).                                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1701
    /* Cells may also be of format "_(x,y)" where x represents an action */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1702
    /* to perform and y represents the next state.                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1703
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1704
    /* This format limits each table to 16 states each and to 15 actions.*/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1705
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1706
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1707
    /* Definitions and type for levels state tables                      */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1708
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1709
    private static final int IMPTABLEVELS_COLUMNS = _B + 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1710
    private static final int IMPTABLEVELS_RES = IMPTABLEVELS_COLUMNS - 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1711
    private static short GetState(byte cell) { return (short)(cell & 0x0f); }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1712
    private static short GetAction(byte cell) { return (short)(cell >> 4); }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1713
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1714
    private static class ImpTabPair {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1715
        byte[][][] imptab;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1716
        short[][] impact;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1717
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1718
        ImpTabPair(byte[][] table1, byte[][] table2,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1719
                   short[] act1, short[] act2) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1720
            imptab = new byte[][][] {table1, table2};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1721
            impact = new short[][] {act1, act2};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1722
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1723
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1724
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1725
    /*********************************************************************/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1726
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1727
    /*      LEVELS  STATE  TABLES                                        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1728
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1729
    /* In all levels state tables,                                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1730
    /*      - state 0 is the initial state                               */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1731
    /*      - the Res column is the increment to add to the text level   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1732
    /*        for this property sequence.                                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1733
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1734
    /* The impact arrays for each table of a pair map the local action   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1735
    /* numbers of the table to the total list of actions. For instance,  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1736
    /* action 2 in a given table corresponds to the action number which  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1737
    /* appears in entry [2] of the impact array for that table.          */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1738
    /* The first entry of all impact arrays must be 0.                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1739
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1740
    /* Action 1: init conditional sequence                               */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1741
    /*        2: prepend conditional sequence to current sequence        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1742
    /*        3: set ON sequence to new level - 1                        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1743
    /*        4: init EN/AN/ON sequence                                  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1744
    /*        5: fix EN/AN/ON sequence followed by R                     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1745
    /*        6: set previous level sequence to level 2                  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1746
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1747
    /* Notes:                                                            */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1748
    /*  1) These tables are used in processPropertySeq(). The input      */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1749
    /*     is property sequences as determined by resolveImplicitLevels. */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1750
    /*  2) Most such property sequences are processed immediately        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1751
    /*     (levels are assigned).                                        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1752
    /*  3) However, some sequences cannot be assigned a final level till */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1753
    /*     one or more following sequences are received. For instance,   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1754
    /*     ON following an R sequence within an even-level paragraph.    */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1755
    /*     If the following sequence is R, the ON sequence will be       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1756
    /*     assigned basic run level+1, and so will the R sequence.       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1757
    /*  4) S is generally handled like ON, since its level will be fixed */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1758
    /*     to paragraph level in adjustWSLevels().                       */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1759
    /*                                                                   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1760
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1761
    private static final byte impTabL_DEFAULT[][] = /* Even paragraph level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1762
        /*  In this table, conditional sequences receive the higher possible level
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1763
            until proven otherwise.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1764
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1765
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1766
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1767
        /* 0 : init       */ {     0,     1,     0,     2,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1768
        /* 1 : R          */ {     0,     1,     3,     3,  0x14,  0x14,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1769
        /* 2 : AN         */ {     0,     1,     0,     2,  0x15,  0x15,     0,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1770
        /* 3 : R+EN/AN    */ {     0,     1,     3,     3,  0x14,  0x14,     0,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1771
        /* 4 : R+ON       */ {  0x20,     1,     3,     3,     4,     4,  0x20,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1772
        /* 5 : AN+ON      */ {  0x20,     1,  0x20,     2,     5,     5,  0x20,  1 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1773
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1774
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1775
    private static final byte impTabR_DEFAULT[][] = /* Odd  paragraph level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1776
        /*  In this table, conditional sequences receive the lower possible level
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1777
            until proven otherwise.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1778
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1779
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1780
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1781
        /* 0 : init       */ {     1,     0,     2,     2,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1782
        /* 1 : L          */ {     1,     0,     1,     3,  0x14,  0x14,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1783
        /* 2 : EN/AN      */ {     1,     0,     2,     2,     0,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1784
        /* 3 : L+AN       */ {     1,     0,     1,     3,     5,     5,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1785
        /* 4 : L+ON       */ {  0x21,     0,  0x21,     3,     4,     4,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1786
        /* 5 : L+AN+ON    */ {     1,     0,     1,     3,     5,     5,     0,  0 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1787
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1788
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1789
    private static final short[] impAct0 = {0,1,2,3,4,5,6};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1790
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1791
    private static final ImpTabPair impTab_DEFAULT = new ImpTabPair(
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1792
            impTabL_DEFAULT, impTabR_DEFAULT, impAct0, impAct0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1793
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1794
    private static final byte impTabL_NUMBERS_SPECIAL[][] = { /* Even paragraph level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1795
        /* In this table, conditional sequences receive the higher possible
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1796
           level until proven otherwise.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1797
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1798
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1799
        /* 0 : init       */ {     0,     2,     1,     1,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1800
        /* 1 : L+EN/AN    */ {     0,     2,     1,     1,     0,     0,     0,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1801
        /* 2 : R          */ {     0,     2,     4,     4,  0x13,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1802
        /* 3 : R+ON       */ {  0x20,     2,     4,     4,     3,     3,  0x20,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1803
        /* 4 : R+EN/AN    */ {     0,     2,     4,     4,  0x13,  0x13,     0,  2 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1804
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1805
    private static final ImpTabPair impTab_NUMBERS_SPECIAL = new ImpTabPair(
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1806
            impTabL_NUMBERS_SPECIAL, impTabR_DEFAULT, impAct0, impAct0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1807
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1808
    private static final byte impTabL_GROUP_NUMBERS_WITH_R[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1809
        /* In this table, EN/AN+ON sequences receive levels as if associated with R
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1810
           until proven that there is L or sor/eor on both sides. AN is handled like EN.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1811
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1812
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1813
        /* 0 init         */ {     0,     3,  0x11,  0x11,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1814
        /* 1 EN/AN        */ {  0x20,     3,     1,     1,     2,  0x20,  0x20,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1815
        /* 2 EN/AN+ON     */ {  0x20,     3,     1,     1,     2,  0x20,  0x20,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1816
        /* 3 R            */ {     0,     3,     5,     5,  0x14,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1817
        /* 4 R+ON         */ {  0x20,     3,     5,     5,     4,  0x20,  0x20,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1818
        /* 5 R+EN/AN      */ {     0,     3,     5,     5,  0x14,     0,     0,  2 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1819
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1820
    private static final byte impTabR_GROUP_NUMBERS_WITH_R[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1821
        /*  In this table, EN/AN+ON sequences receive levels as if associated with R
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1822
            until proven that there is L on both sides. AN is handled like EN.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1823
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1824
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1825
        /* 0 init         */ {     2,     0,     1,     1,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1826
        /* 1 EN/AN        */ {     2,     0,     1,     1,     0,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1827
        /* 2 L            */ {     2,     0,  0x14,  0x14,  0x13,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1828
        /* 3 L+ON         */ {  0x22,     0,     4,     4,     3,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1829
        /* 4 L+EN/AN      */ {  0x22,     0,     4,     4,     3,     0,     0,  1 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1830
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1831
    private static final ImpTabPair impTab_GROUP_NUMBERS_WITH_R = new
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1832
            ImpTabPair(impTabL_GROUP_NUMBERS_WITH_R,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1833
                       impTabR_GROUP_NUMBERS_WITH_R, impAct0, impAct0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1834
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1835
    private static final byte impTabL_INVERSE_NUMBERS_AS_L[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1836
        /* This table is identical to the Default LTR table except that EN and AN
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1837
           are handled like L.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1838
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1839
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1840
        /* 0 : init       */ {     0,     1,     0,     0,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1841
        /* 1 : R          */ {     0,     1,     0,     0,  0x14,  0x14,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1842
        /* 2 : AN         */ {     0,     1,     0,     0,  0x15,  0x15,     0,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1843
        /* 3 : R+EN/AN    */ {     0,     1,     0,     0,  0x14,  0x14,     0,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1844
        /* 4 : R+ON       */ {  0x20,     1,  0x20,  0x20,     4,     4,  0x20,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1845
        /* 5 : AN+ON      */ {  0x20,     1,  0x20,  0x20,     5,     5,  0x20,  1 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1846
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1847
    private static final byte impTabR_INVERSE_NUMBERS_AS_L[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1848
        /* This table is identical to the Default RTL table except that EN and AN
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1849
           are handled like L.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1850
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1851
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1852
        /* 0 : init       */ {     1,     0,     1,     1,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1853
        /* 1 : L          */ {     1,     0,     1,     1,  0x14,  0x14,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1854
        /* 2 : EN/AN      */ {     1,     0,     1,     1,     0,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1855
        /* 3 : L+AN       */ {     1,     0,     1,     1,     5,     5,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1856
        /* 4 : L+ON       */ {  0x21,     0,  0x21,  0x21,     4,     4,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1857
        /* 5 : L+AN+ON    */ {     1,     0,     1,     1,     5,     5,     0,  0 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1858
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1859
    private static final ImpTabPair impTab_INVERSE_NUMBERS_AS_L = new ImpTabPair
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1860
            (impTabL_INVERSE_NUMBERS_AS_L, impTabR_INVERSE_NUMBERS_AS_L,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1861
             impAct0, impAct0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1862
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1863
    private static final byte impTabR_INVERSE_LIKE_DIRECT[][] = {  /* Odd  paragraph level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1864
        /*  In this table, conditional sequences receive the lower possible level
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1865
            until proven otherwise.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1866
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1867
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1868
        /* 0 : init       */ {     1,     0,     2,     2,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1869
        /* 1 : L          */ {     1,     0,     1,     2,  0x13,  0x13,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1870
        /* 2 : EN/AN      */ {     1,     0,     2,     2,     0,     0,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1871
        /* 3 : L+ON       */ {  0x21,  0x30,     6,     4,     3,     3,  0x30,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1872
        /* 4 : L+ON+AN    */ {  0x21,  0x30,     6,     4,     5,     5,  0x30,  3 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1873
        /* 5 : L+AN+ON    */ {  0x21,  0x30,     6,     4,     5,     5,  0x30,  2 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1874
        /* 6 : L+ON+EN    */ {  0x21,  0x30,     6,     4,     3,     3,  0x30,  1 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1875
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1876
    private static final short[] impAct1 = {0,1,11,12};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1877
    private static final ImpTabPair impTab_INVERSE_LIKE_DIRECT = new ImpTabPair(
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1878
            impTabL_DEFAULT, impTabR_INVERSE_LIKE_DIRECT, impAct0, impAct1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1879
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1880
    private static final byte impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1881
        /* The case handled in this table is (visually):  R EN L
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1882
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1883
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1884
        /* 0 : init       */ {     0,  0x63,     0,     1,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1885
        /* 1 : L+AN       */ {     0,  0x63,     0,     1,  0x12,  0x30,     0,  4 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1886
        /* 2 : L+AN+ON    */ {  0x20,  0x63,  0x20,     1,     2,  0x30,  0x20,  3 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1887
        /* 3 : R          */ {     0,  0x63,  0x55,  0x56,  0x14,  0x30,     0,  3 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1888
        /* 4 : R+ON       */ {  0x30,  0x43,  0x55,  0x56,     4,  0x30,  0x30,  3 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1889
        /* 5 : R+EN       */ {  0x30,  0x43,     5,  0x56,  0x14,  0x30,  0x30,  4 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1890
        /* 6 : R+AN       */ {  0x30,  0x43,  0x55,     6,  0x14,  0x30,  0x30,  4 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1891
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1892
    private static final byte impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1893
        /* The cases handled in this table are (visually):  R EN L
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1894
                                                            R L AN L
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1895
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1896
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1897
        /* 0 : init       */ {  0x13,     0,     1,     1,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1898
        /* 1 : R+EN/AN    */ {  0x23,     0,     1,     1,     2,  0x40,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1899
        /* 2 : R+EN/AN+ON */ {  0x23,     0,     1,     1,     2,  0x40,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1900
        /* 3 : L          */ {    3 ,     0,     3,  0x36,  0x14,  0x40,     0,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1901
        /* 4 : L+ON       */ {  0x53,  0x40,     5,  0x36,     4,  0x40,  0x40,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1902
        /* 5 : L+ON+EN    */ {  0x53,  0x40,     5,  0x36,     4,  0x40,  0x40,  1 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1903
        /* 6 : L+AN       */ {  0x53,  0x40,     6,     6,     4,  0x40,  0x40,  3 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1904
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1905
    private static final short impAct2[] = {0,1,7,8,9,10};
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1906
    private static final ImpTabPair impTab_INVERSE_LIKE_DIRECT_WITH_MARKS =
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1907
            new ImpTabPair(impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1908
                           impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS, impAct0, impAct2);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1909
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1910
    private static final ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL = new ImpTabPair(
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1911
            impTabL_NUMBERS_SPECIAL, impTabR_INVERSE_LIKE_DIRECT, impAct0, impAct1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1912
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1913
    private static final byte impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS[][] = {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1914
        /*  The case handled in this table is (visually):  R EN L
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1915
        */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1916
        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1917
        /* 0 : init       */ {     0,  0x62,     1,     1,     0,     0,     0,  0 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1918
        /* 1 : L+EN/AN    */ {     0,  0x62,     1,     1,     0,  0x30,     0,  4 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1919
        /* 2 : R          */ {     0,  0x62,  0x54,  0x54,  0x13,  0x30,     0,  3 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1920
        /* 3 : R+ON       */ {  0x30,  0x42,  0x54,  0x54,     3,  0x30,  0x30,  3 },
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1921
        /* 4 : R+EN/AN    */ {  0x30,  0x42,     4,     4,  0x13,  0x30,  0x30,  4 }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1922
    };
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1923
    private static final ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS = new
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1924
            ImpTabPair(impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1925
                       impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS, impAct0, impAct2);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1926
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1927
    private class LevState {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1928
        byte[][] impTab;                /* level table pointer          */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1929
        short[] impAct;                 /* action map array             */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1930
        int startON;                    /* start of ON sequence         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1931
        int startL2EN;                  /* start of level 2 sequence    */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1932
        int lastStrongRTL;              /* index of last found R or AL  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1933
        short state;                    /* current state                */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1934
        byte runLevel;                  /* run level before implicit solving */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1935
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1936
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1937
    /*------------------------------------------------------------------------*/
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1938
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1939
    static final int FIRSTALLOC = 10;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1940
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1941
     *  param pos:     position where to insert
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1942
     *  param flag:    one of LRM_BEFORE, LRM_AFTER, RLM_BEFORE, RLM_AFTER
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1943
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1944
    private void addPoint(int pos, int flag)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1945
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1946
        Point point = new Point();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1947
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1948
        int len = insertPoints.points.length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1949
        if (len == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1950
            insertPoints.points = new Point[FIRSTALLOC];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1951
            len = FIRSTALLOC;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1952
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1953
        if (insertPoints.size >= len) { /* no room for new point */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1954
            Point[] savePoints = insertPoints.points;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1955
            insertPoints.points = new Point[len * 2];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1956
            System.arraycopy(savePoints, 0, insertPoints.points, 0, len);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1957
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1958
        point.pos = pos;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1959
        point.flag = flag;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1960
        insertPoints.points[insertPoints.size] = point;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1961
        insertPoints.size++;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1962
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1963
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1964
    /* perform rules (Wn), (Nn), and (In) on a run of the text ------------------ */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1965
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1966
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1967
     * This implementation of the (Wn) rules applies all rules in one pass.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1968
     * In order to do so, it needs a look-ahead of typically 1 character
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1969
     * (except for W5: sequences of ET) and keeps track of changes
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1970
     * in a rule Wp that affect a later Wq (p<q).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1971
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1972
     * The (Nn) and (In) rules are also performed in that same single loop,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1973
     * but effectively one iteration behind for white space.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1974
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1975
     * Since all implicit rules are performed in one step, it is not necessary
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1976
     * to actually store the intermediate directional properties in dirProps[].
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1977
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1978
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1979
    private void processPropertySeq(LevState levState, short _prop,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1980
            int start, int limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1981
        byte cell;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1982
        byte[][] impTab = levState.impTab;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1983
        short[] impAct = levState.impAct;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1984
        short oldStateSeq,actionSeq;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1985
        byte level, addLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1986
        int start0, k;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1987
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1988
        start0 = start;                 /* save original start position */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1989
        oldStateSeq = levState.state;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1990
        cell = impTab[oldStateSeq][_prop];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1991
        levState.state = GetState(cell);        /* isolate the new state */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1992
        actionSeq = impAct[GetAction(cell)];    /* isolate the action */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1993
        addLevel = (byte)impTab[levState.state][IMPTABLEVELS_RES];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1994
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1995
        if (actionSeq != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1996
            switch (actionSeq) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1997
            case 1:                     /* init ON seq */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1998
                levState.startON = start0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  1999
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2000
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2001
            case 2:                     /* prepend ON seq to current seq */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2002
                start = levState.startON;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2003
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2004
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2005
            case 3:                     /* L or S after possible relevant EN/AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2006
                /* check if we had EN after R/AL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2007
                if (levState.startL2EN >= 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2008
                    addPoint(levState.startL2EN, LRM_BEFORE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2009
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2010
                levState.startL2EN = -1;  /* not within previous if since could also be -2 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2011
                /* check if we had any relevant EN/AN after R/AL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2012
                if ((insertPoints.points.length == 0) ||
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2013
                        (insertPoints.size <= insertPoints.confirmed)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2014
                    /* nothing, just clean up */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2015
                    levState.lastStrongRTL = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2016
                    /* check if we have a pending conditional segment */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2017
                    level = (byte)impTab[oldStateSeq][IMPTABLEVELS_RES];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2018
                    if ((level & 1) != 0 && levState.startON > 0) { /* after ON */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2019
                        start = levState.startON;   /* reset to basic run level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2020
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2021
                    if (_prop == _S) {              /* add LRM before S */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2022
                        addPoint(start0, LRM_BEFORE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2023
                        insertPoints.confirmed = insertPoints.size;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2024
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2025
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2026
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2027
                /* reset previous RTL cont to level for LTR text */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2028
                for (k = levState.lastStrongRTL + 1; k < start0; k++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2029
                    /* reset odd level, leave runLevel+2 as is */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2030
                    levels[k] = (byte)((levels[k] - 2) & ~1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2031
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2032
                /* mark insert points as confirmed */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2033
                insertPoints.confirmed = insertPoints.size;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2034
                levState.lastStrongRTL = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2035
                if (_prop == _S) {           /* add LRM before S */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2036
                    addPoint(start0, LRM_BEFORE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2037
                    insertPoints.confirmed = insertPoints.size;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2038
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2039
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2040
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2041
            case 4:                     /* R/AL after possible relevant EN/AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2042
                /* just clean up */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2043
                if (insertPoints.points.length > 0)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2044
                    /* remove all non confirmed insert points */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2045
                    insertPoints.size = insertPoints.confirmed;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2046
                levState.startON = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2047
                levState.startL2EN = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2048
                levState.lastStrongRTL = limit - 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2049
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2050
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2051
            case 5:                     /* EN/AN after R/AL + possible cont */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2052
                /* check for real AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2053
                if ((_prop == _AN) && (NoContextRTL(dirProps[start0]) == AN)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2054
                    /* real AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2055
                    if (levState.startL2EN == -1) { /* if no relevant EN already found */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2056
                        /* just note the righmost digit as a strong RTL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2057
                        levState.lastStrongRTL = limit - 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2058
                        break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2059
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2060
                    if (levState.startL2EN >= 0)  { /* after EN, no AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2061
                        addPoint(levState.startL2EN, LRM_BEFORE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2062
                        levState.startL2EN = -2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2063
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2064
                    /* note AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2065
                    addPoint(start0, LRM_BEFORE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2066
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2067
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2068
                /* if first EN/AN after R/AL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2069
                if (levState.startL2EN == -1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2070
                    levState.startL2EN = start0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2071
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2072
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2073
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2074
            case 6:                     /* note location of latest R/AL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2075
                levState.lastStrongRTL = limit - 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2076
                levState.startON = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2077
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2078
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2079
            case 7:                     /* L after R+ON/EN/AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2080
                /* include possible adjacent number on the left */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2081
                for (k = start0-1; k >= 0 && ((levels[k] & 1) == 0); k--) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2082
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2083
                if (k >= 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2084
                    addPoint(k, RLM_BEFORE);    /* add RLM before */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2085
                    insertPoints.confirmed = insertPoints.size; /* confirm it */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2086
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2087
                levState.startON = start0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2088
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2089
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2090
            case 8:                     /* AN after L */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2091
                /* AN numbers between L text on both sides may be trouble. */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2092
                /* tentatively bracket with LRMs; will be confirmed if followed by L */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2093
                addPoint(start0, LRM_BEFORE);   /* add LRM before */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2094
                addPoint(start0, LRM_AFTER);    /* add LRM after  */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2095
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2096
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2097
            case 9:                     /* R after L+ON/EN/AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2098
                /* false alert, infirm LRMs around previous AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2099
                insertPoints.size=insertPoints.confirmed;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2100
                if (_prop == _S) {          /* add RLM before S */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2101
                    addPoint(start0, RLM_BEFORE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2102
                    insertPoints.confirmed = insertPoints.size;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2103
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2104
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2105
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2106
            case 10:                    /* L after L+ON/AN */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2107
                level = (byte)(levState.runLevel + addLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2108
                for (k=levState.startON; k < start0; k++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2109
                    if (levels[k] < level) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2110
                        levels[k] = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2111
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2112
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2113
                insertPoints.confirmed = insertPoints.size;   /* confirm inserts */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2114
                levState.startON = start0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2115
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2116
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2117
            case 11:                    /* L after L+ON+EN/AN/ON */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2118
                level = (byte)levState.runLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2119
                for (k = start0-1; k >= levState.startON; k--) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2120
                    if (levels[k] == level+3) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2121
                        while (levels[k] == level+3) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2122
                            levels[k--] -= 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2123
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2124
                        while (levels[k] == level) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2125
                            k--;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2126
                        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2127
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2128
                    if (levels[k] == level+2) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2129
                        levels[k] = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2130
                        continue;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2131
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2132
                    levels[k] = (byte)(level+1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2133
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2134
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2135
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2136
            case 12:                    /* R after L+ON+EN/AN/ON */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2137
                level = (byte)(levState.runLevel+1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2138
                for (k = start0-1; k >= levState.startON; k--) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2139
                    if (levels[k] > level) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2140
                        levels[k] -= 2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2141
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2142
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2143
                break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2144
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2145
            default:                        /* we should never get here */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2146
                throw new IllegalStateException("Internal ICU error in processPropertySeq");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2147
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2148
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2149
        if ((addLevel) != 0 || (start < start0)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2150
            level = (byte)(levState.runLevel + addLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2151
            for (k = start; k < limit; k++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2152
                levels[k] = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2153
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2154
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2155
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2156
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2157
    private void resolveImplicitLevels(int start, int limit, short sor, short eor)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2158
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2159
        LevState levState = new LevState();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2160
        int i, start1, start2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2161
        short oldStateImp, stateImp, actionImp;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2162
        short gprop, resProp, cell;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2163
        short nextStrongProp = R;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2164
        int nextStrongPos = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2165
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2166
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2167
        /* check for RTL inverse Bidi mode */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2168
        /* FOOD FOR THOUGHT: in case of RTL inverse Bidi, it would make sense to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2169
         * loop on the text characters from end to start.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2170
         * This would need a different properties state table (at least different
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2171
         * actions) and different levels state tables (maybe very similar to the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2172
         * LTR corresponding ones.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2173
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2174
        /* initialize for levels state table */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2175
        levState.startL2EN = -1;        /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2176
        levState.lastStrongRTL = -1;    /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2177
        levState.state = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2178
        levState.runLevel = levels[start];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2179
        levState.impTab = impTabPair.imptab[levState.runLevel & 1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2180
        levState.impAct = impTabPair.impact[levState.runLevel & 1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2181
        processPropertySeq(levState, (short)sor, start, start);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2182
        /* initialize for property state table */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2183
        if (dirProps[start] == NSM) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2184
            stateImp = (short)(1 + sor);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2185
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2186
            stateImp = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2187
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2188
        start1 = start;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2189
        start2 = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2190
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2191
        for (i = start; i <= limit; i++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2192
            if (i >= limit) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2193
                gprop = eor;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2194
            } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2195
                short prop, prop1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2196
                prop = NoContextRTL(dirProps[i]);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2197
                gprop = groupProp[prop];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2198
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2199
            oldStateImp = stateImp;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2200
            cell = impTabProps[oldStateImp][gprop];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2201
            stateImp = GetStateProps(cell);     /* isolate the new state */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2202
            actionImp = GetActionProps(cell);   /* isolate the action */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2203
            if ((i == limit) && (actionImp == 0)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2204
                /* there is an unprocessed sequence if its property == eor   */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2205
                actionImp = 1;                  /* process the last sequence */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2206
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2207
            if (actionImp != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2208
                resProp = impTabProps[oldStateImp][IMPTABPROPS_RES];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2209
                switch (actionImp) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2210
                case 1:             /* process current seq1, init new seq1 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2211
                    processPropertySeq(levState, resProp, start1, i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2212
                    start1 = i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2213
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2214
                case 2:             /* init new seq2 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2215
                    start2 = i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2216
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2217
                case 3:             /* process seq1, process seq2, init new seq1 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2218
                    processPropertySeq(levState, resProp, start1, start2);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2219
                    processPropertySeq(levState, _ON, start2, i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2220
                    start1 = i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2221
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2222
                case 4:             /* process seq1, set seq1=seq2, init new seq2 */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2223
                    processPropertySeq(levState, resProp, start1, start2);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2224
                    start1 = start2;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2225
                    start2 = i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2226
                    break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2227
                default:            /* we should never get here */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2228
                    throw new IllegalStateException("Internal ICU error in resolveImplicitLevels");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2229
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2230
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2231
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2232
        /* flush possible pending sequence, e.g. ON */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2233
        processPropertySeq(levState, (short)eor, limit, limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2234
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2235
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2236
    /* perform (L1) and (X9) ---------------------------------------------------- */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2237
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2238
    /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2239
     * Reset the embedding levels for some non-graphic characters (L1).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2240
     * This method also sets appropriate levels for BN, and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2241
     * explicit embedding types that are supposed to have been removed
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2242
     * from the paragraph in (X9).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2243
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2244
    private void adjustWSLevels() {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2245
        int i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2246
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2247
        if ((flags & MASK_WS) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2248
            int flag;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2249
            i = trailingWSStart;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2250
            while (i > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2251
                /* reset a sequence of WS/BN before eop and B/S to the paragraph paraLevel */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2252
                while (i > 0 && ((flag = DirPropFlagNC(dirProps[--i])) & MASK_WS) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2253
                    if (orderParagraphsLTR && (flag & DirPropFlag(B)) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2254
                        levels[i] = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2255
                    } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2256
                        levels[i] = GetParaLevelAt(i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2257
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2258
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2259
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2260
                /* reset BN to the next character's paraLevel until B/S, which restarts above loop */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2261
                /* here, i+1 is guaranteed to be <length */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2262
                while (i > 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2263
                    flag = DirPropFlagNC(dirProps[--i]);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2264
                    if ((flag & MASK_BN_EXPLICIT) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2265
                        levels[i] = levels[i + 1];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2266
                    } else if (orderParagraphsLTR && (flag & DirPropFlag(B)) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2267
                        levels[i] = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2268
                        break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2269
                    } else if ((flag & MASK_B_S) != 0){
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2270
                        levels[i] = GetParaLevelAt(i);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2271
                        break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2272
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2273
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2274
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2275
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2276
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2277
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2278
    private int Bidi_Min(int x, int y) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2279
        return x < y ? x : y;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2280
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2281
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2282
    private int Bidi_Abs(int x) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2283
        return x >= 0 ? x : -x;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2284
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2285
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2286
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2287
     * Perform the Unicode Bidi algorithm. It is defined in the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2288
     * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2289
     * version 13,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2290
     * also described in The Unicode Standard, Version 4.0 .<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2291
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2292
     * This method takes a piece of plain text containing one or more paragraphs,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2293
     * with or without externally specified embedding levels from <i>styled</i>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2294
     * text and computes the left-right-directionality of each character.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2295
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2296
     * If the entire text is all of the same directionality, then
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2297
     * the method may not perform all the steps described by the algorithm,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2298
     * i.e., some levels may not be the same as if all steps were performed.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2299
     * This is not relevant for unidirectional text.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2300
     * For example, in pure LTR text with numbers the numbers would get
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2301
     * a resolved level of 2 higher than the surrounding text according to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2302
     * the algorithm. This implementation may set all resolved levels to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2303
     * the same value in such a case.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2304
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2305
     * The text can be composed of multiple paragraphs. Occurrence of a block
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2306
     * separator in the text terminates a paragraph, and whatever comes next starts
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2307
     * a new paragraph. The exception to this rule is when a Carriage Return (CR)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2308
     * is followed by a Line Feed (LF). Both CR and LF are block separators, but
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2309
     * in that case, the pair of characters is considered as terminating the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2310
     * preceding paragraph, and a new paragraph will be started by a character
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2311
     * coming after the LF.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2312
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2313
     * Although the text is passed here as a <code>String</code>, it is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2314
     * stored internally as an array of characters. Therefore the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2315
     * documentation will refer to indexes of the characters in the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2316
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2317
     * @param text contains the text that the Bidi algorithm will be performed
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2318
     *        on. This text can be retrieved with <code>getText()</code> or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2319
     *        <code>getTextAsString</code>.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2320
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2321
     * @param paraLevel specifies the default level for the text;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2322
     *        it is typically 0 (LTR) or 1 (RTL).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2323
     *        If the method shall determine the paragraph level from the text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2324
     *        then <code>paraLevel</code> can be set to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2325
     *        either <code>LEVEL_DEFAULT_LTR</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2326
     *        or <code>LEVEL_DEFAULT_RTL</code>; if the text contains multiple
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2327
     *        paragraphs, the paragraph level shall be determined separately for
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2328
     *        each paragraph; if a paragraph does not include any strongly typed
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2329
     *        character, then the desired default is used (0 for LTR or 1 for RTL).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2330
     *        Any other value between 0 and <code>MAX_EXPLICIT_LEVEL</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2331
     *        is also valid, with odd levels indicating RTL.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2332
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2333
     * @param embeddingLevels (in) may be used to preset the embedding and override levels,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2334
     *        ignoring characters like LRE and PDF in the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2335
     *        A level overrides the directional property of its corresponding
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2336
     *        (same index) character if the level has the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2337
     *        <code>LEVEL_OVERRIDE</code> bit set.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2338
     *        Except for that bit, it must be
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2339
     *        <code>paraLevel<=embeddingLevels[]<=MAX_EXPLICIT_LEVEL</code>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2340
     *        with one exception: a level of zero may be specified for a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2341
     *        paragraph separator even if <code>paraLevel&gt;0</code> when multiple
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2342
     *        paragraphs are submitted in the same call to <code>setPara()</code>.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2343
     *        <strong>Caution: </strong>A reference to this array, not a copy
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2344
     *        of the levels, will be stored in the <code>Bidi</code> object;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2345
     *        the <code>embeddingLevels</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2346
     *        should not be modified to avoid unexpected results on subsequent
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2347
     *        Bidi operations. However, the <code>setPara()</code> and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2348
     *        <code>setLine()</code> methods may modify some or all of the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2349
     *        levels.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2350
     *        <strong>Note:</strong> the <code>embeddingLevels</code> array must
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2351
     *        have one entry for each character in <code>text</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2352
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2353
     * @throws IllegalArgumentException if the values in embeddingLevels are
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2354
     *         not within the allowed range
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2355
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2356
     * @see #LEVEL_DEFAULT_LTR
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2357
     * @see #LEVEL_DEFAULT_RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2358
     * @see #LEVEL_OVERRIDE
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2359
     * @see #MAX_EXPLICIT_LEVEL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2360
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2361
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2362
    void setPara(String text, byte paraLevel, byte[] embeddingLevels)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2363
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2364
        if (text == null) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2365
            setPara(new char[0], paraLevel, embeddingLevels);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2366
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2367
            setPara(text.toCharArray(), paraLevel, embeddingLevels);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2368
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2369
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2370
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2371
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2372
     * Perform the Unicode Bidi algorithm. It is defined in the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2373
     * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2374
     * version 13,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2375
     * also described in The Unicode Standard, Version 4.0 .<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2376
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2377
     * This method takes a piece of plain text containing one or more paragraphs,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2378
     * with or without externally specified embedding levels from <i>styled</i>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2379
     * text and computes the left-right-directionality of each character.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2380
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2381
     * If the entire text is all of the same directionality, then
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2382
     * the method may not perform all the steps described by the algorithm,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2383
     * i.e., some levels may not be the same as if all steps were performed.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2384
     * This is not relevant for unidirectional text.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2385
     * For example, in pure LTR text with numbers the numbers would get
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2386
     * a resolved level of 2 higher than the surrounding text according to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2387
     * the algorithm. This implementation may set all resolved levels to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2388
     * the same value in such a case.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2389
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2390
     * The text can be composed of multiple paragraphs. Occurrence of a block
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2391
     * separator in the text terminates a paragraph, and whatever comes next starts
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2392
     * a new paragraph. The exception to this rule is when a Carriage Return (CR)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2393
     * is followed by a Line Feed (LF). Both CR and LF are block separators, but
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2394
     * in that case, the pair of characters is considered as terminating the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2395
     * preceding paragraph, and a new paragraph will be started by a character
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2396
     * coming after the LF.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2397
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2398
     * The text is stored internally as an array of characters. Therefore the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2399
     * documentation will refer to indexes of the characters in the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2400
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2401
     * @param chars contains the text that the Bidi algorithm will be performed
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2402
     *        on. This text can be retrieved with <code>getText()</code> or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2403
     *        <code>getTextAsString</code>.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2404
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2405
     * @param paraLevel specifies the default level for the text;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2406
     *        it is typically 0 (LTR) or 1 (RTL).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2407
     *        If the method shall determine the paragraph level from the text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2408
     *        then <code>paraLevel</code> can be set to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2409
     *        either <code>LEVEL_DEFAULT_LTR</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2410
     *        or <code>LEVEL_DEFAULT_RTL</code>; if the text contains multiple
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2411
     *        paragraphs, the paragraph level shall be determined separately for
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2412
     *        each paragraph; if a paragraph does not include any strongly typed
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2413
     *        character, then the desired default is used (0 for LTR or 1 for RTL).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2414
     *        Any other value between 0 and <code>MAX_EXPLICIT_LEVEL</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2415
     *        is also valid, with odd levels indicating RTL.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2416
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2417
     * @param embeddingLevels (in) may be used to preset the embedding and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2418
     *        override levels, ignoring characters like LRE and PDF in the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2419
     *        A level overrides the directional property of its corresponding
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2420
     *        (same index) character if the level has the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2421
     *        <code>LEVEL_OVERRIDE</code> bit set.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2422
     *        Except for that bit, it must be
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2423
     *        <code>paraLevel<=embeddingLevels[]<=MAX_EXPLICIT_LEVEL</code>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2424
     *        with one exception: a level of zero may be specified for a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2425
     *        paragraph separator even if <code>paraLevel&gt;0</code> when multiple
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2426
     *        paragraphs are submitted in the same call to <code>setPara()</code>.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2427
     *        <strong>Caution: </strong>A reference to this array, not a copy
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2428
     *        of the levels, will be stored in the <code>Bidi</code> object;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2429
     *        the <code>embeddingLevels</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2430
     *        should not be modified to avoid unexpected results on subsequent
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2431
     *        Bidi operations. However, the <code>setPara()</code> and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2432
     *        <code>setLine()</code> methods may modify some or all of the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2433
     *        levels.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2434
     *        <strong>Note:</strong> the <code>embeddingLevels</code> array must
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2435
     *        have one entry for each character in <code>text</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2436
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2437
     * @throws IllegalArgumentException if the values in embeddingLevels are
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2438
     *         not within the allowed range
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2439
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2440
     * @see #LEVEL_DEFAULT_LTR
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2441
     * @see #LEVEL_DEFAULT_RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2442
     * @see #LEVEL_OVERRIDE
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2443
     * @see #MAX_EXPLICIT_LEVEL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2444
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2445
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2446
    public void setPara(char[] chars, byte paraLevel, byte[] embeddingLevels)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2447
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2448
        /* check the argument values */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2449
        if (paraLevel < INTERNAL_LEVEL_DEFAULT_LTR) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2450
            verifyRange(paraLevel, 0, MAX_EXPLICIT_LEVEL + 1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2451
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2452
        if (chars == null) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2453
            chars = new char[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2454
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2455
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2456
        /* initialize the Bidi object */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2457
        this.paraBidi = null;          /* mark unfinished setPara */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2458
        this.text = chars;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2459
        this.length = this.originalLength = this.resultLength = text.length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2460
        this.paraLevel = paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2461
        this.direction = Bidi.DIRECTION_LEFT_TO_RIGHT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2462
        this.paraCount = 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2463
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2464
        /* Allocate zero-length arrays instead of setting to null here; then
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2465
         * checks for null in various places can be eliminated.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2466
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2467
        dirProps = new byte[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2468
        levels = new byte[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2469
        runs = new BidiRun[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2470
        isGoodLogicalToVisualRunsMap = false;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2471
        insertPoints.size = 0;          /* clean up from last call */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2472
        insertPoints.confirmed = 0;     /* clean up from last call */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2473
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2474
        /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2475
         * Save the original paraLevel if contextual; otherwise, set to 0.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2476
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2477
        if (IsDefaultLevel(paraLevel)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2478
            defaultParaLevel = paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2479
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2480
            defaultParaLevel = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2481
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2482
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2483
        if (length == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2484
            /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2485
             * For an empty paragraph, create a Bidi object with the paraLevel and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2486
             * the flags and the direction set but without allocating zero-length arrays.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2487
             * There is nothing more to do.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2488
             */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2489
            if (IsDefaultLevel(paraLevel)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2490
                this.paraLevel &= 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2491
                defaultParaLevel = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2492
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2493
            if ((this.paraLevel & 1) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2494
                flags = DirPropFlag(R);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2495
                direction = Bidi.DIRECTION_RIGHT_TO_LEFT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2496
            } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2497
                flags = DirPropFlag(L);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2498
                direction = Bidi.DIRECTION_LEFT_TO_RIGHT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2499
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2500
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2501
            runCount = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2502
            paraCount = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2503
            paraBidi = this;         /* mark successful setPara */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2504
            return;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2505
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2506
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2507
        runCount = -1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2508
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2509
        /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2510
         * Get the directional properties,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2511
         * the flags bit-set, and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2512
         * determine the paragraph level if necessary.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2513
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2514
        getDirPropsMemory(length);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2515
        dirProps = dirPropsMemory;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2516
        getDirProps();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2517
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2518
        /* the processed length may have changed if OPTION_STREAMING is set */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2519
        trailingWSStart = length;  /* the levels[] will reflect the WS run */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2520
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2521
        /* allocate paras memory */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2522
        if (paraCount > 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2523
            getInitialParasMemory(paraCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2524
            paras = parasMemory;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2525
            paras[paraCount - 1] = length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2526
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2527
            /* initialize paras for single paragraph */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2528
            paras = simpleParas;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2529
            simpleParas[0] = length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2530
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2531
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2532
        /* are explicit levels specified? */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2533
        if (embeddingLevels == null) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2534
            /* no: determine explicit levels according to the (Xn) rules */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2535
            getLevelsMemory(length);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2536
            levels = levelsMemory;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2537
            direction = resolveExplicitLevels();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2538
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2539
            /* set BN for all explicit codes, check that all levels are 0 or paraLevel..MAX_EXPLICIT_LEVEL */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2540
            levels = embeddingLevels;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2541
            direction = checkExplicitLevels();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2542
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2543
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2544
        /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2545
         * The steps after (X9) in the Bidi algorithm are performed only if
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2546
         * the paragraph text has mixed directionality!
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2547
         */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2548
        switch (direction) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2549
        case Bidi.DIRECTION_LEFT_TO_RIGHT:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2550
            /* make sure paraLevel is even */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2551
            paraLevel = (byte)((paraLevel + 1) & ~1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2552
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2553
            /* all levels are implicitly at paraLevel (important for getLevels()) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2554
            trailingWSStart = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2555
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2556
        case Bidi.DIRECTION_RIGHT_TO_LEFT:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2557
            /* make sure paraLevel is odd */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2558
            paraLevel |= 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2559
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2560
            /* all levels are implicitly at paraLevel (important for getLevels()) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2561
            trailingWSStart = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2562
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2563
        default:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2564
            this.impTabPair = impTab_DEFAULT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2565
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2566
            /*
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2567
             * If there are no external levels specified and there
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2568
             * are no significant explicit level codes in the text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2569
             * then we can treat the entire paragraph as one run.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2570
             * Otherwise, we need to perform the following rules on runs of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2571
             * the text with the same embedding levels. (X10)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2572
             * "Significant" explicit level codes are ones that actually
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2573
             * affect non-BN characters.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2574
             * Examples for "insignificant" ones are empty embeddings
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2575
             * LRE-PDF, LRE-RLE-PDF-PDF, etc.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2576
             */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2577
            if (embeddingLevels == null && paraCount <= 1 &&
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2578
                (flags & DirPropFlagMultiRuns) == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2579
                resolveImplicitLevels(0, length,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2580
                        GetLRFromLevel(GetParaLevelAt(0)),
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2581
                        GetLRFromLevel(GetParaLevelAt(length - 1)));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2582
            } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2583
                /* sor, eor: start and end types of same-level-run */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2584
                int start, limit = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2585
                byte level, nextLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2586
                short sor, eor;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2587
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2588
                /* determine the first sor and set eor to it because of the loop body (sor=eor there) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2589
                level = GetParaLevelAt(0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2590
                nextLevel = levels[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2591
                if (level < nextLevel) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2592
                    eor = GetLRFromLevel(nextLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2593
                } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2594
                    eor = GetLRFromLevel(level);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2595
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2596
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2597
                do {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2598
                    /* determine start and limit of the run (end points just behind the run) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2599
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2600
                    /* the values for this run's start are the same as for the previous run's end */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2601
                    start = limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2602
                    level = nextLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2603
                    if ((start > 0) && (NoContextRTL(dirProps[start - 1]) == B)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2604
                        /* except if this is a new paragraph, then set sor = para level */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2605
                        sor = GetLRFromLevel(GetParaLevelAt(start));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2606
                    } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2607
                        sor = eor;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2608
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2609
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2610
                    /* search for the limit of this run */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2611
                    while (++limit < length && levels[limit] == level) {}
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2612
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2613
                    /* get the correct level of the next run */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2614
                    if (limit < length) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2615
                        nextLevel = levels[limit];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2616
                    } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2617
                        nextLevel = GetParaLevelAt(length - 1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2618
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2619
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2620
                    /* determine eor from max(level, nextLevel); sor is last run's eor */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2621
                    if ((level & ~INTERNAL_LEVEL_OVERRIDE) < (nextLevel & ~INTERNAL_LEVEL_OVERRIDE)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2622
                        eor = GetLRFromLevel(nextLevel);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2623
                    } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2624
                        eor = GetLRFromLevel(level);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2625
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2626
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2627
                    /* if the run consists of overridden directional types, then there
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2628
                       are no implicit types to be resolved */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2629
                    if ((level & INTERNAL_LEVEL_OVERRIDE) == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2630
                        resolveImplicitLevels(start, limit, sor, eor);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2631
                    } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2632
                        /* remove the LEVEL_OVERRIDE flags */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2633
                        do {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2634
                            levels[start++] &= ~INTERNAL_LEVEL_OVERRIDE;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2635
                        } while (start < limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2636
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2637
                } while (limit  < length);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2638
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2639
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2640
            /* reset the embedding levels for some non-graphic characters (L1), (X9) */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2641
            adjustWSLevels();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2642
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2643
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2644
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2645
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2646
        resultLength += insertPoints.size;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2647
        paraBidi = this;             /* mark successful setPara */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2648
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2649
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2650
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2651
     * Perform the Unicode Bidi algorithm on a given paragraph, as defined in the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2652
     * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2653
     * version 13,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2654
     * also described in The Unicode Standard, Version 4.0 .<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2655
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2656
     * This method takes a paragraph of text and computes the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2657
     * left-right-directionality of each character. The text should not
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2658
     * contain any Unicode block separators.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2659
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2660
     * The RUN_DIRECTION attribute in the text, if present, determines the base
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2661
     * direction (left-to-right or right-to-left). If not present, the base
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2662
     * direction is computed using the Unicode Bidirectional Algorithm,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2663
     * defaulting to left-to-right if there are no strong directional characters
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2664
     * in the text. This attribute, if present, must be applied to all the text
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2665
     * in the paragraph.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2666
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2667
     * The BIDI_EMBEDDING attribute in the text, if present, represents
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2668
     * embedding level information. Negative values from -1 to -62 indicate
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2669
     * overrides at the absolute value of the level. Positive values from 1 to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2670
     * 62 indicate embeddings. Where values are zero or not defined, the base
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2671
     * embedding level as determined by the base direction is assumed.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2672
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2673
     * The NUMERIC_SHAPING attribute in the text, if present, converts European
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2674
     * digits to other decimal digits before running the bidi algorithm. This
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2675
     * attribute, if present, must be applied to all the text in the paragraph.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2676
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2677
     * If the entire text is all of the same directionality, then
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2678
     * the method may not perform all the steps described by the algorithm,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2679
     * i.e., some levels may not be the same as if all steps were performed.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2680
     * This is not relevant for unidirectional text.<br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2681
     * For example, in pure LTR text with numbers the numbers would get
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2682
     * a resolved level of 2 higher than the surrounding text according to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2683
     * the algorithm. This implementation may set all resolved levels to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2684
     * the same value in such a case.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2685
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2686
     * @param paragraph a paragraph of text with optional character and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2687
     *        paragraph attribute information
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2688
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2689
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2690
    public void setPara(AttributedCharacterIterator paragraph)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2691
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2692
        byte paraLvl;
10089
297652d14d44 7042148: closed/java/awt/font/TextLayout/CheckLayoutLTR.java failed
peytoia
parents: 9668
diff changeset
  2693
        char ch = paragraph.first();
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2694
        Boolean runDirection =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2695
            (Boolean) paragraph.getAttribute(TextAttributeConstants.RUN_DIRECTION);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2696
        Object shaper = paragraph.getAttribute(TextAttributeConstants.NUMERIC_SHAPING);
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2697
        if (runDirection == null) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2698
            paraLvl = INTERNAL_LEVEL_DEFAULT_LTR;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2699
        } else {
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2700
            paraLvl = (runDirection.equals(TextAttributeConstants.RUN_DIRECTION_LTR)) ?
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2701
                        (byte)Bidi.DIRECTION_LEFT_TO_RIGHT : (byte)Bidi.DIRECTION_RIGHT_TO_LEFT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2702
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2703
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2704
        byte[] lvls = null;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2705
        int len = paragraph.getEndIndex() - paragraph.getBeginIndex();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2706
        byte[] embeddingLevels = new byte[len];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2707
        char[] txt = new char[len];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2708
        int i = 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2709
        while (ch != AttributedCharacterIterator.DONE) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2710
            txt[i] = ch;
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2711
            Integer embedding =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2712
                (Integer) paragraph.getAttribute(TextAttributeConstants.BIDI_EMBEDDING);
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2713
            if (embedding != null) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2714
                byte level = embedding.byteValue();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2715
                if (level == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2716
                    /* no-op */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2717
                } else if (level < 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2718
                    lvls = embeddingLevels;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2719
                    embeddingLevels[i] = (byte)((0 - level) | INTERNAL_LEVEL_OVERRIDE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2720
                } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2721
                    lvls = embeddingLevels;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2722
                    embeddingLevels[i] = level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2723
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2724
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2725
            ch = paragraph.next();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2726
            ++i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2727
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2728
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2729
        if (shaper != null) {
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  2730
            NumericShapings.shape(shaper, txt, 0, len);
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2731
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2732
        setPara(txt, paraLvl, lvls);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2733
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2734
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2735
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2736
     * Specify whether block separators must be allocated level zero,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2737
     * so that successive paragraphs will progress from left to right.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2738
     * This method must be called before <code>setPara()</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2739
     * Paragraph separators (B) may appear in the text.  Setting them to level zero
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2740
     * means that all paragraph separators (including one possibly appearing
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2741
     * in the last text position) are kept in the reordered text after the text
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2742
     * that they follow in the source text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2743
     * When this feature is not enabled, a paragraph separator at the last
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2744
     * position of the text before reordering will go to the first position
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2745
     * of the reordered text when the paragraph level is odd.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2746
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2747
     * @param ordarParaLTR specifies whether paragraph separators (B) must
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2748
     * receive level 0, so that successive paragraphs progress from left to right.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2749
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2750
     * @see #setPara
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2751
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2752
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2753
    private void orderParagraphsLTR(boolean ordarParaLTR) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2754
        orderParagraphsLTR = ordarParaLTR;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2755
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2756
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2757
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2758
     * Get the directionality of the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2759
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2760
     * @return a value of <code>LTR</code>, <code>RTL</code> or <code>MIXED</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2761
     *         that indicates if the entire text
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2762
     *         represented by this object is unidirectional,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2763
     *         and which direction, or if it is mixed-directional.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2764
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2765
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2766
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2767
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2768
     * @see #LTR
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2769
     * @see #RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2770
     * @see #MIXED
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2771
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2772
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2773
    private byte getDirection()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2774
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2775
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2776
        return direction;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2777
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2778
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2779
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2780
     * Get the length of the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2781
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2782
     * @return The length of the text that the <code>Bidi</code> object was
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2783
     *         created for.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2784
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2785
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2786
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2787
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2788
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2789
    public int getLength()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2790
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2791
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2792
        return originalLength;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2793
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2794
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2795
    /* paragraphs API methods ------------------------------------------------- */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2796
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2797
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2798
     * Get the paragraph level of the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2799
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2800
     * @return The paragraph level. If there are multiple paragraphs, their
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2801
     *         level may vary if the required paraLevel is LEVEL_DEFAULT_LTR or
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2802
     *         LEVEL_DEFAULT_RTL.  In that case, the level of the first paragraph
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2803
     *         is returned.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2804
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2805
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2806
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2807
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2808
     * @see #LEVEL_DEFAULT_LTR
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2809
     * @see #LEVEL_DEFAULT_RTL
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2810
     * @see #getParagraph
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2811
     * @see #getParagraphByIndex
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2812
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2813
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2814
    public byte getParaLevel()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2815
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2816
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2817
        return paraLevel;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2818
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2819
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2820
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2821
     * Get the index of a paragraph, given a position within the text.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2822
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2823
     * @param charIndex is the index of a character within the text, in the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2824
     *        range <code>[0..getProcessedLength()-1]</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2825
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2826
     * @return The index of the paragraph containing the specified position,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2827
     *         starting from 0.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2828
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2829
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2830
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2831
     * @throws IllegalArgumentException if charIndex is not within the legal range
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2832
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2833
     * @see com.ibm.icu.text.BidiRun
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2834
     * @see #getProcessedLength
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2835
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2836
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2837
    public int getParagraphIndex(int charIndex)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2838
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2839
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2840
        BidiBase bidi = paraBidi;             /* get Para object if Line object */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2841
        verifyRange(charIndex, 0, bidi.length);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2842
        int paraIndex;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2843
        for (paraIndex = 0; charIndex >= bidi.paras[paraIndex]; paraIndex++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2844
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2845
        return paraIndex;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2846
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2847
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2848
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2849
     * <code>setLine()</code> returns a <code>Bidi</code> object to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2850
     * contain the reordering information, especially the resolved levels,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2851
     * for all the characters in a line of text. This line of text is
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2852
     * specified by referring to a <code>Bidi</code> object representing
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2853
     * this information for a piece of text containing one or more paragraphs,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2854
     * and by specifying a range of indexes in this text.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2855
     * In the new line object, the indexes will range from 0 to <code>limit-start-1</code>.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2856
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2857
     * This is used after calling <code>setPara()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2858
     * for a piece of text, and after line-breaking on that text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2859
     * It is not necessary if each paragraph is treated as a single line.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2860
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2861
     * After line-breaking, rules (L1) and (L2) for the treatment of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2862
     * trailing WS and for reordering are performed on
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2863
     * a <code>Bidi</code> object that represents a line.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2864
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2865
     * <strong>Important: </strong>the line <code>Bidi</code> object may
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2866
     * reference data within the global text <code>Bidi</code> object.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2867
     * You should not alter the content of the global text object until
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2868
     * you are finished using the line object.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2869
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2870
     * @param start is the line's first index into the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2871
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2872
     * @param limit is just behind the line's last index into the text
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2873
     *        (its last index +1).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2874
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2875
     * @return a <code>Bidi</code> object that will now represent a line of the text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2876
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2877
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2878
     *         call to <code>setPara</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2879
     * @throws IllegalArgumentException if start and limit are not in the range
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2880
     *         <code>0&lt;=start&lt;limit&lt;=getProcessedLength()</code>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2881
     *         or if the specified line crosses a paragraph boundary
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2882
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2883
     * @see #setPara
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2884
     * @see #getProcessedLength
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2885
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2886
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2887
    public Bidi setLine(Bidi bidi, BidiBase bidiBase, Bidi newBidi, BidiBase newBidiBase, int start, int limit)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2888
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2889
        verifyValidPara();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2890
        verifyRange(start, 0, limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2891
        verifyRange(limit, 0, length+1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2892
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2893
        return BidiLine.setLine(bidi, this, newBidi, newBidiBase, start, limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2894
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2895
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2896
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2897
     * Get the level for one character.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2898
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2899
     * @param charIndex the index of a character.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2900
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2901
     * @return The level for the character at <code>charIndex</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2902
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2903
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2904
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2905
     * @throws IllegalArgumentException if charIndex is not in the range
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2906
     *         <code>0&lt;=charIndex&lt;getProcessedLength()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2907
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2908
     * @see #getProcessedLength
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2909
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2910
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2911
    public byte getLevelAt(int charIndex)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2912
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2913
        if (charIndex < 0 || charIndex >= length) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2914
            return (byte)getBaseLevel();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2915
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2916
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2917
        verifyRange(charIndex, 0, length);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2918
        return BidiLine.getLevelAt(this, charIndex);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2919
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2920
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2921
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2922
     * Get an array of levels for each character.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2923
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2924
     * Note that this method may allocate memory under some
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2925
     * circumstances, unlike <code>getLevelAt()</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2926
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2927
     * @return The levels array for the text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2928
     *         or <code>null</code> if an error occurs.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2929
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2930
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2931
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2932
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2933
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2934
    private byte[] getLevels()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2935
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2936
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2937
        if (length <= 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2938
            return new byte[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2939
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2940
        return BidiLine.getLevels(this);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2941
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2942
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2943
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2944
     * Get the number of runs.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2945
     * This method may invoke the actual reordering on the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2946
     * <code>Bidi</code> object, after <code>setPara()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2947
     * may have resolved only the levels of the text. Therefore,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2948
     * <code>countRuns()</code> may have to allocate memory,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2949
     * and may throw an exception if it fails to do so.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2950
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2951
     * @return The number of runs.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2952
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2953
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2954
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2955
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2956
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2957
    public int countRuns()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2958
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2959
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2960
        BidiLine.getRuns(this);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2961
        return runCount;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2962
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2963
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2964
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2965
     * Get a visual-to-logical index map (array) for the characters in the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2966
     * <code>Bidi</code> (paragraph or line) object.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2967
     * <p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2968
     * Some values in the map may be <code>MAP_NOWHERE</code> if the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2969
     * corresponding text characters are Bidi marks inserted in the visual
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2970
     * output by the option <code>OPTION_INSERT_MARKS</code>.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2971
     * <p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2972
     * When the visual output is altered by using options of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2973
     * <code>writeReordered()</code> such as <code>INSERT_LRM_FOR_NUMERIC</code>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2974
     * <code>KEEP_BASE_COMBINING</code>, <code>OUTPUT_REVERSE</code>,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2975
     * <code>REMOVE_BIDI_CONTROLS</code>, the logical positions returned may not
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2976
     * be correct. It is advised to use, when possible, reordering options
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2977
     * such as {@link #OPTION_INSERT_MARKS} and {@link #OPTION_REMOVE_CONTROLS}.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2978
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2979
     * @return an array of <code>getResultLength()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2980
     *        indexes which will reflect the reordering of the characters.<br><br>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2981
     *        The index map will result in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2982
     *        <code>indexMap[visualIndex]==logicalIndex</code>, where
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2983
     *        <code>indexMap</code> represents the returned array.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2984
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2985
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2986
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2987
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2988
     * @see #getLogicalMap
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2989
     * @see #getLogicalIndex
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2990
     * @see #getResultLength
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2991
     * @see #MAP_NOWHERE
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2992
     * @see #OPTION_INSERT_MARKS
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2993
     * @see #writeReordered
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2994
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2995
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2996
    private int[] getVisualMap()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2997
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2998
        /* countRuns() checks successful call to setPara/setLine */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  2999
        countRuns();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3000
        if (resultLength <= 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3001
            return new int[0];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3002
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3003
        return BidiLine.getVisualMap(this);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3004
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3005
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3006
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3007
     * This is a convenience method that does not use a <code>Bidi</code> object.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3008
     * It is intended to be used for when an application has determined the levels
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3009
     * of objects (character sequences) and just needs to have them reordered (L2).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3010
     * This is equivalent to using <code>getVisualMap()</code> on a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3011
     * <code>Bidi</code> object.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3012
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3013
     * @param levels is an array of levels that have been determined by
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3014
     *        the application.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3015
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3016
     * @return an array of <code>levels.length</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3017
     *        indexes which will reflect the reordering of the characters.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3018
     *        The index map will result in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3019
     *        <code>indexMap[visualIndex]==logicalIndex</code>, where
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3020
     *        <code>indexMap</code> represents the returned array.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3021
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3022
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3023
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3024
    private static int[] reorderVisual(byte[] levels)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3025
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3026
        return BidiLine.reorderVisual(levels);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3027
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3028
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3029
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3030
     * Constant indicating that the base direction depends on the first strong
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3031
     * directional character in the text according to the Unicode Bidirectional
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3032
     * Algorithm. If no strong directional character is present, the base
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3033
     * direction is left-to-right.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3034
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3035
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3036
    private static final int INTERNAL_DIRECTION_DEFAULT_LEFT_TO_RIGHT = 0x7e;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3037
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3038
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3039
     * Constant indicating that the base direction depends on the first strong
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3040
     * directional character in the text according to the Unicode Bidirectional
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3041
     * Algorithm. If no strong directional character is present, the base
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3042
     * direction is right-to-left.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3043
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3044
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3045
    private static final int INTERMAL_DIRECTION_DEFAULT_RIGHT_TO_LEFT = 0x7f;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3046
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3047
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3048
     * Create Bidi from the given text, embedding, and direction information.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3049
     * The embeddings array may be null. If present, the values represent
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3050
     * embedding level information. Negative values from -1 to -61 indicate
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3051
     * overrides at the absolute value of the level. Positive values from 1 to
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3052
     * 61 indicate embeddings. Where values are zero, the base embedding level
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3053
     * as determined by the base direction is assumed.<p>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3054
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3055
     * Note: this constructor calls setPara() internally.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3056
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3057
     * @param text an array containing the paragraph of text to process.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3058
     * @param textStart the index into the text array of the start of the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3059
     *        paragraph.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3060
     * @param embeddings an array containing embedding values for each character
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3061
     *        in the paragraph. This can be null, in which case it is assumed
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3062
     *        that there is no external embedding information.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3063
     * @param embStart the index into the embedding array of the start of the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3064
     *        paragraph.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3065
     * @param paragraphLength the length of the paragraph in the text and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3066
     *        embeddings arrays.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3067
     * @param flags a collection of flags that control the algorithm. The
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3068
     *        algorithm understands the flags DIRECTION_LEFT_TO_RIGHT,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3069
     *        DIRECTION_RIGHT_TO_LEFT, DIRECTION_DEFAULT_LEFT_TO_RIGHT, and
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3070
     *        DIRECTION_DEFAULT_RIGHT_TO_LEFT. Other values are reserved.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3071
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3072
     * @throws IllegalArgumentException if the values in embeddings are
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3073
     *         not within the allowed range
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3074
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3075
     * @see #DIRECTION_LEFT_TO_RIGHT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3076
     * @see #DIRECTION_RIGHT_TO_LEFT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3077
     * @see #DIRECTION_DEFAULT_LEFT_TO_RIGHT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3078
     * @see #DIRECTION_DEFAULT_RIGHT_TO_LEFT
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3079
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3080
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3081
    public BidiBase(char[] text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3082
             int textStart,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3083
             byte[] embeddings,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3084
             int embStart,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3085
             int paragraphLength,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3086
             int flags)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3087
     {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3088
        this(0, 0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3089
        byte paraLvl;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3090
        switch (flags) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3091
        case Bidi.DIRECTION_LEFT_TO_RIGHT:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3092
        default:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3093
            paraLvl = Bidi.DIRECTION_LEFT_TO_RIGHT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3094
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3095
        case Bidi.DIRECTION_RIGHT_TO_LEFT:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3096
            paraLvl = Bidi.DIRECTION_RIGHT_TO_LEFT;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3097
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3098
        case Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3099
            paraLvl = INTERNAL_LEVEL_DEFAULT_LTR;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3100
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3101
        case Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT:
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3102
            paraLvl = INTERNAL_LEVEL_DEFAULT_RTL;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3103
            break;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3104
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3105
        byte[] paraEmbeddings;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3106
        if (embeddings == null) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3107
            paraEmbeddings = null;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3108
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3109
            paraEmbeddings = new byte[paragraphLength];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3110
            byte lev;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3111
            for (int i = 0; i < paragraphLength; i++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3112
                lev = embeddings[i + embStart];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3113
                if (lev < 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3114
                    lev = (byte)((- lev) | INTERNAL_LEVEL_OVERRIDE);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3115
                } else if (lev == 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3116
                    lev = paraLvl;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3117
                    if (paraLvl > MAX_EXPLICIT_LEVEL) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3118
                        lev &= 1;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3119
                    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3120
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3121
                paraEmbeddings[i] = lev;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3122
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3123
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3124
        if (textStart == 0 && embStart == 0 && paragraphLength == text.length) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3125
            setPara(text, paraLvl, paraEmbeddings);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3126
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3127
            char[] paraText = new char[paragraphLength];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3128
            System.arraycopy(text, textStart, paraText, 0, paragraphLength);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3129
            setPara(paraText, paraLvl, paraEmbeddings);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3130
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3131
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3132
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3133
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3134
     * Return true if the line is not left-to-right or right-to-left. This means
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3135
     * it either has mixed runs of left-to-right and right-to-left text, or the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3136
     * base direction differs from the direction of the only run of text.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3137
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3138
     * @return true if the line is not left-to-right or right-to-left.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3139
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3140
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3141
     *         call to <code>setPara</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3142
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3143
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3144
    public boolean isMixed()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3145
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3146
        return (!isLeftToRight() && !isRightToLeft());
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3147
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3148
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3149
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3150
    * Return true if the line is all left-to-right text and the base direction
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3151
     * is left-to-right.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3152
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3153
     * @return true if the line is all left-to-right text and the base direction
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3154
     *         is left-to-right.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3155
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3156
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3157
     *         call to <code>setPara</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3158
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3159
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3160
    public boolean isLeftToRight()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3161
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3162
        return (getDirection() == Bidi.DIRECTION_LEFT_TO_RIGHT && (paraLevel & 1) == 0);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3163
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3164
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3165
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3166
     * Return true if the line is all right-to-left text, and the base direction
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3167
     * is right-to-left
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3168
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3169
     * @return true if the line is all right-to-left text, and the base
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3170
     *         direction is right-to-left
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3171
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3172
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3173
     *         call to <code>setPara</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3174
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3175
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3176
    public boolean isRightToLeft()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3177
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3178
        return (getDirection() == Bidi.DIRECTION_RIGHT_TO_LEFT && (paraLevel & 1) == 1);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3179
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3180
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3181
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3182
     * Return true if the base direction is left-to-right
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3183
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3184
     * @return true if the base direction is left-to-right
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3185
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3186
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3187
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3188
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3189
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3190
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3191
    public boolean baseIsLeftToRight()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3192
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3193
        return (getParaLevel() == Bidi.DIRECTION_LEFT_TO_RIGHT);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3194
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3195
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3196
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3197
     * Return the base level (0 if left-to-right, 1 if right-to-left).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3198
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3199
     * @return the base level
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3200
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3201
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3202
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3203
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3204
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3205
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3206
    public int getBaseLevel()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3207
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3208
        return getParaLevel();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3209
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3210
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3211
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3212
     * Compute the logical to visual run mapping
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3213
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3214
    private void getLogicalToVisualRunsMap()
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3215
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3216
        if (isGoodLogicalToVisualRunsMap) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3217
            return;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3218
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3219
        int count = countRuns();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3220
        if ((logicalToVisualRunsMap == null) ||
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3221
            (logicalToVisualRunsMap.length < count)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3222
            logicalToVisualRunsMap = new int[count];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3223
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3224
        int i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3225
        long[] keys = new long[count];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3226
        for (i = 0; i < count; i++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3227
            keys[i] = ((long)(runs[i].start)<<32) + i;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3228
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3229
        Arrays.sort(keys);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3230
        for (i = 0; i < count; i++) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3231
            logicalToVisualRunsMap[i] = (int)(keys[i] & 0x00000000FFFFFFFF);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3232
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3233
        keys = null;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3234
        isGoodLogicalToVisualRunsMap = true;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3235
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3236
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3237
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3238
     * Return the level of the nth logical run in this line.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3239
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3240
     * @param run the index of the run, between 0 and <code>countRuns()-1</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3241
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3242
     * @return the level of the run
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3243
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3244
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3245
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3246
     * @throws IllegalArgumentException if <code>run</code> is not in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3247
     *         the range <code>0&lt;=run&lt;countRuns()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3248
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3249
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3250
    public int getRunLevel(int run)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3251
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3252
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3253
        BidiLine.getRuns(this);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3254
        if (runCount == 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3255
            return getParaLevel();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3256
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3257
        verifyIndex(run, 0, runCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3258
        getLogicalToVisualRunsMap();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3259
        return runs[logicalToVisualRunsMap[run]].level;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3260
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3261
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3262
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3263
     * Return the index of the character at the start of the nth logical run in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3264
     * this line, as an offset from the start of the line.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3265
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3266
     * @param run the index of the run, between 0 and <code>countRuns()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3267
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3268
     * @return the start of the run
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3269
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3270
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3271
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3272
     * @throws IllegalArgumentException if <code>run</code> is not in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3273
     *         the range <code>0&lt;=run&lt;countRuns()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3274
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3275
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3276
    public int getRunStart(int run)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3277
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3278
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3279
        BidiLine.getRuns(this);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3280
        if (runCount == 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3281
            return 0;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3282
        } else if (run == runCount) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3283
            return length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3284
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3285
        verifyIndex(run, 0, runCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3286
        getLogicalToVisualRunsMap();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3287
        return runs[logicalToVisualRunsMap[run]].start;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3288
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3289
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3290
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3291
     * Return the index of the character past the end of the nth logical run in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3292
     * this line, as an offset from the start of the line. For example, this
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3293
     * will return the length of the line for the last run on the line.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3294
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3295
     * @param run the index of the run, between 0 and <code>countRuns()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3296
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3297
     * @return the limit of the run
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3298
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3299
     * @throws IllegalStateException if this call is not preceded by a successful
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3300
     *         call to <code>setPara</code> or <code>setLine</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3301
     * @throws IllegalArgumentException if <code>run</code> is not in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3302
     *         the range <code>0&lt;=run&lt;countRuns()</code>
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3303
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3304
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3305
    public int getRunLimit(int run)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3306
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3307
        verifyValidParaOrLine();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3308
        BidiLine.getRuns(this);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3309
        if (runCount == 1) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3310
            return length;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3311
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3312
        verifyIndex(run, 0, runCount);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3313
        getLogicalToVisualRunsMap();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3314
        int idx = logicalToVisualRunsMap[run];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3315
        int len = idx == 0 ? runs[idx].limit :
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3316
                                runs[idx].limit - runs[idx-1].limit;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3317
        return runs[idx].start + len;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3318
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3319
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3320
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3321
     * Return true if the specified text requires bidi analysis. If this returns
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3322
     * false, the text will display left-to-right. Clients can then avoid
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3323
     * constructing a Bidi object. Text in the Arabic Presentation Forms area of
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3324
     * Unicode is presumed to already be shaped and ordered for display, and so
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3325
     * will not cause this method to return true.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3326
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3327
     * @param text the text containing the characters to test
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3328
     * @param start the start of the range of characters to test
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3329
     * @param limit the limit of the range of characters to test
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3330
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3331
     * @return true if the range of characters requires bidi analysis
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3332
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3333
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3334
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3335
    public static boolean requiresBidi(char[] text,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3336
            int start,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3337
            int limit)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3338
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3339
        final int RTLMask = (1 << Bidi.DIRECTION_RIGHT_TO_LEFT |
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3340
                1 << AL |
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3341
                1 << RLE |
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3342
                1 << RLO |
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3343
                1 << AN);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3344
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3345
        if (0 > start || start > limit || limit > text.length) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3346
            throw new IllegalArgumentException("Value start " + start +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3347
                      " is out of range 0 to " + limit);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3348
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3349
        for (int i = start; i < limit; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3350
            if (Character.isHighSurrogate(text[i]) && i < (limit-1) &&
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3351
                Character.isLowSurrogate(text[i+1])) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3352
                if (((1 << UCharacter.getDirection(Character.codePointAt(text, i))) & RTLMask) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3353
                    return true;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3354
                }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3355
            } else if (((1 << UCharacter.getDirection(text[i])) & RTLMask) != 0) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3356
                return true;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3357
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3358
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3359
        return false;
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3360
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3361
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3362
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3363
     * Reorder the objects in the array into visual order based on their levels.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3364
     * This is a utility method to use when you have a collection of objects
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3365
     * representing runs of text in logical order, each run containing text at a
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3366
     * single level. The elements at <code>index</code> from
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3367
     * <code>objectStart</code> up to <code>objectStart + count</code> in the
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3368
     * objects array will be reordered into visual order assuming
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3369
     * each run of text has the level indicated by the corresponding element in
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3370
     * the levels array (at <code>index - objectStart + levelStart</code>).
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3371
     *
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3372
     * @param levels an array representing the bidi level of each object
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3373
     * @param levelStart the start position in the levels array
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3374
     * @param objects the array of objects to be reordered into visual order
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3375
     * @param objectStart the start position in the objects array
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3376
     * @param count the number of objects to reorder
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3377
     * @stable ICU 3.8
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3378
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3379
    public static void reorderVisually(byte[] levels,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3380
            int levelStart,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3381
            Object[] objects,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3382
            int objectStart,
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3383
            int count)
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3384
    {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3385
        if (0 > levelStart || levels.length <= levelStart) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3386
            throw new IllegalArgumentException("Value levelStart " +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3387
                      levelStart + " is out of range 0 to " +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3388
                      (levels.length-1));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3389
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3390
        if (0 > objectStart || objects.length <= objectStart) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3391
            throw new IllegalArgumentException("Value objectStart " +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3392
                      levelStart + " is out of range 0 to " +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3393
                      (objects.length-1));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3394
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3395
        if (0 > count || objects.length < (objectStart+count)) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3396
            throw new IllegalArgumentException("Value count " +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3397
                      levelStart + " is out of range 0 to " +
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3398
                      (objects.length - objectStart));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3399
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3400
        byte[] reorderLevels = new byte[count];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3401
        System.arraycopy(levels, levelStart, reorderLevels, 0, count);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3402
        int[] indexMap = reorderVisual(reorderLevels);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3403
        Object[] temp = new Object[count];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3404
        System.arraycopy(objects, objectStart, temp, 0, count);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3405
        for (int i = 0; i < count; ++i) {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3406
            objects[objectStart + i] = temp[indexMap[i]];
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3407
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3408
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3409
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3410
    /**
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3411
     * Display the bidi internal state, used in debugging.
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3412
     */
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3413
    public String toString() {
10090
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3414
        StringBuilder buf = new StringBuilder(getClass().getName());
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3415
10090
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3416
        buf.append("[dir: ");
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3417
        buf.append(direction);
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3418
        buf.append(" baselevel: ");
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3419
        buf.append(paraLevel);
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3420
        buf.append(" length: ");
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3421
        buf.append(length);
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3422
        buf.append(" runs: ");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3423
        if (levels == null) {
10090
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3424
            buf.append("none");
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3425
        } else {
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3426
            buf.append('[');
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3427
            buf.append(levels[0]);
10090
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3428
            for (int i = 1; i < levels.length; i++) {
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3429
                buf.append(' ');
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3430
                buf.append(levels[i]);
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3431
            }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3432
            buf.append(']');
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3433
        }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3434
        buf.append(" text: [0x");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3435
        buf.append(Integer.toHexString(text[0]));
10090
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3436
        for (int i = 1; i < text.length; i++) {
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3437
            buf.append(" 0x");
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3438
            buf.append(Integer.toHexString(text[i]));
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3439
        }
10090
8571585915b6 7051769: java.text.Bidi.toString() output is wrong
peytoia
parents: 10089
diff changeset
  3440
        buf.append("]]");
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3441
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3442
        return buf.toString();
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3443
    }
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3444
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3445
    /**
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3446
     * A class that provides access to constants defined by
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3447
     * java.awt.font.TextAttribute without creating a static dependency.
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3448
     */
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3449
    private static class TextAttributeConstants {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3450
        private static final Class<?> clazz = getClass("java.awt.font.TextAttribute");
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3451
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3452
        /**
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3453
         * TextAttribute instances (or a fake Attribute type if
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3454
         * java.awt.font.TextAttribute is not present)
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3455
         */
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3456
        static final AttributedCharacterIterator.Attribute RUN_DIRECTION =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3457
            getTextAttribute("RUN_DIRECTION");
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3458
        static final AttributedCharacterIterator.Attribute NUMERIC_SHAPING =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3459
            getTextAttribute("NUMERIC_SHAPING");
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3460
        static final AttributedCharacterIterator.Attribute BIDI_EMBEDDING =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3461
            getTextAttribute("BIDI_EMBEDDING");
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3462
9015
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3463
        /**
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3464
         * TextAttribute.RUN_DIRECTION_LTR
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3465
         */
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3466
        static final Boolean RUN_DIRECTION_LTR = (clazz == null) ?
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3467
            Boolean.FALSE : (Boolean)getStaticField(clazz, "RUN_DIRECTION_LTR");
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3468
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3469
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3470
        private static Class<?> getClass(String name) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3471
            try {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3472
                return Class.forName(name, true, null);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3473
            } catch (ClassNotFoundException e) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3474
                return null;
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3475
            }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3476
        }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3477
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3478
        private static Object getStaticField(Class<?> clazz, String name) {
9015
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3479
            try {
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3480
                Field f = clazz.getField(name);
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3481
                return f.get(null);
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3482
            } catch (NoSuchFieldException | IllegalAccessException x) {
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3483
                throw new AssertionError(x);
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3484
            }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3485
        }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3486
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3487
        private static AttributedCharacterIterator.Attribute
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3488
            getTextAttribute(String name)
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3489
        {
9015
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3490
            if (clazz == null) {
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3491
                // fake attribute
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3492
                return new AttributedCharacterIterator.Attribute(name) { };
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3493
            } else {
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3494
                return (AttributedCharacterIterator.Attribute)getStaticField(clazz, name);
6679e1339581 7026507: Bidi initialization fails if AWT not present
alanb
parents: 5506
diff changeset
  3495
            }
4235
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3496
        }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3497
    }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3498
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3499
    /**
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3500
     * A class that provides access to java.awt.font.NumericShaping without
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3501
     * creating a static dependency.
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3502
     */
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3503
    private static class NumericShapings {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3504
        private static final Class<?> clazz =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3505
            getClass("java.awt.font.NumericShaper");
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3506
        private static final Method shapeMethod =
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3507
            getMethod(clazz, "shape", char[].class, int.class, int.class);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3508
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3509
        private static Class<?> getClass(String name) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3510
            try {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3511
                return Class.forName(name, true, null);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3512
            } catch (ClassNotFoundException e) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3513
                return null;
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3514
            }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3515
        }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3516
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3517
        private static Method getMethod(Class<?> clazz,
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3518
                                        String name,
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3519
                                        Class<?>... paramTypes)
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3520
        {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3521
            if (clazz != null) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3522
                try {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3523
                    return clazz.getMethod(name, paramTypes);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3524
                } catch (NoSuchMethodException e) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3525
                    throw new AssertionError(e);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3526
                }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3527
            } else {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3528
                return null;
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3529
            }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3530
        }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3531
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3532
        /**
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3533
         * Invokes NumericShaping shape(text,start,count) method.
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3534
         */
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3535
        static void shape(Object shaper, char[] text, int start, int count) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3536
            if (shapeMethod == null)
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3537
                throw new AssertionError("Should not get here");
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3538
            try {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3539
                shapeMethod.invoke(shaper, text, start, count);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3540
            } catch (InvocationTargetException e) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3541
                Throwable cause = e.getCause();
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3542
                if (cause instanceof RuntimeException)
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3543
                    throw (RuntimeException)cause;
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3544
                throw new AssertionError(e);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3545
            } catch (IllegalAccessException iae) {
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3546
                throw new AssertionError(iae);
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3547
            }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3548
        }
c05c788c4a98 6898124: Bidi should not require AWT to be present
alanb
parents: 2956
diff changeset
  3549
    }
2956
931f209e57a9 6850113: Bidi class needs to be updated to support Unicode 5.1
peytoia
parents:
diff changeset
  3550
}