1/*
2*******************************************************************************
3*   Copyright (C) 2001-2014, International Business Machines
4*   Corporation and others.  All Rights Reserved.
5*******************************************************************************
6*/
7
8/* FOOD FOR THOUGHT: currently the reordering modes are a mixture of
9 * algorithm for direct BiDi, algorithm for inverse Bidi and the bizarre
10 * concept of RUNS_ONLY which is a double operation.
11 * It could be advantageous to divide this into 3 concepts:
12 * a) Operation: direct / inverse / RUNS_ONLY
13 * b) Direct algorithm: default / NUMBERS_SPECIAL / GROUP_NUMBERS_WITH_L
14 * c) Inverse algorithm: default / INVERSE_LIKE_DIRECT / NUMBERS_SPECIAL
15 * This would allow combinations not possible today like RUNS_ONLY with
16 * NUMBERS_SPECIAL.
17 * Also allow to set INSERT_MARKS for the direct step of RUNS_ONLY and
18 * REMOVE_CONTROLS for the inverse step.
19 * Not all combinations would be supported, and probably not all do make sense.
20 * This would need to document which ones are supported and what are the
21 * fallbacks for unsupported combinations.
22 */
23
24//TODO: make sample program do something simple but real and complete
25
26package com.ibm.icu.text;
27
28import java.awt.font.NumericShaper;
29import java.awt.font.TextAttribute;
30import java.lang.reflect.Array;
31import java.text.AttributedCharacterIterator;
32import java.util.Arrays;
33
34import com.ibm.icu.impl.UBiDiProps;
35import com.ibm.icu.lang.UCharacter;
36import com.ibm.icu.lang.UCharacterDirection;
37import com.ibm.icu.lang.UProperty;
38
39/**
40 *
41 * <h2>Bidi algorithm for ICU</h2>
42 *
43 * This is an implementation of the Unicode Bidirectional Algorithm. The
44 * algorithm is defined in the <a
45 * href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>.
46 * <p>
47 *
48 * Note: Libraries that perform a bidirectional algorithm and reorder strings
49 * accordingly are sometimes called "Storage Layout Engines". ICU's Bidi and
50 * shaping (ArabicShaping) classes can be used at the core of such "Storage
51 * Layout Engines".
52 *
53 * <h3>General remarks about the API:</h3>
54 *
55 * The &quot;limit&quot; of a sequence of characters is the position just after
56 * their last character, i.e., one more than that position.
57 * <p>
58 *
59 * Some of the API methods provide access to &quot;runs&quot;. Such a
60 * &quot;run&quot; is defined as a sequence of characters that are at the same
61 * embedding level after performing the Bidi algorithm.
62 * <p>
63 *
64 * <h3>Basic concept: paragraph</h3>
65 * A piece of text can be divided into several paragraphs by characters
66 * with the Bidi class <code>Block Separator</code>. For handling of
67 * paragraphs, see:
68 * <ul>
69 * <li>{@link #countParagraphs}
70 * <li>{@link #getParaLevel}
71 * <li>{@link #getParagraph}
72 * <li>{@link #getParagraphByIndex}
73 * </ul>
74 *
75 * <h3>Basic concept: text direction</h3>
76 * The direction of a piece of text may be:
77 * <ul>
78 * <li>{@link #LTR}
79 * <li>{@link #RTL}
80 * <li>{@link #MIXED}
81 * <li>{@link #NEUTRAL}
82 * </ul>
83 *
84 * <h3>Basic concept: levels</h3>
85 *
86 * Levels in this API represent embedding levels according to the Unicode
87 * Bidirectional Algorithm.
88 * Their low-order bit (even/odd value) indicates the visual direction.<p>
89 *
90 * Levels can be abstract values when used for the
91 * <code>paraLevel</code> and <code>embeddingLevels</code>
92 * arguments of <code>setPara()</code>; there:
93 * <ul>
94 * <li>the high-order bit of an <code>embeddingLevels[]</code>
95 * value indicates whether the using application is
96 * specifying the level of a character to <i>override</i> whatever the
97 * Bidi implementation would resolve it to.</li>
98 * <li><code>paraLevel</code> can be set to the
99 * pseudo-level values <code>LEVEL_DEFAULT_LTR</code>
100 * and <code>LEVEL_DEFAULT_RTL</code>.</li>
101 * </ul>
102 *
103 * <p>The related constants are not real, valid level values.
104 * <code>DEFAULT_XXX</code> can be used to specify
105 * a default for the paragraph level for
106 * when the <code>setPara()</code> method
107 * shall determine it but there is no
108 * strongly typed character in the input.<p>
109 *
110 * Note that the value for <code>LEVEL_DEFAULT_LTR</code> is even
111 * and the one for <code>LEVEL_DEFAULT_RTL</code> is odd,
112 * just like with normal LTR and RTL level values -
113 * these special values are designed that way. Also, the implementation
114 * assumes that MAX_EXPLICIT_LEVEL is odd.
115 *
116 * <b>See Also:</b>
117 * <ul>
118 * <li>{@link #LEVEL_DEFAULT_LTR}
119 * <li>{@link #LEVEL_DEFAULT_RTL}
120 * <li>{@link #LEVEL_OVERRIDE}
121 * <li>{@link #MAX_EXPLICIT_LEVEL}
122 * <li>{@link #setPara}
123 * </ul>
124 *
125 * <h3>Basic concept: Reordering Mode</h3>
126 * Reordering mode values indicate which variant of the Bidi algorithm to
127 * use.
128 *
129 * <b>See Also:</b>
130 * <ul>
131 * <li>{@link #setReorderingMode}
132 * <li>{@link #REORDER_DEFAULT}
133 * <li>{@link #REORDER_NUMBERS_SPECIAL}
134 * <li>{@link #REORDER_GROUP_NUMBERS_WITH_R}
135 * <li>{@link #REORDER_RUNS_ONLY}
136 * <li>{@link #REORDER_INVERSE_NUMBERS_AS_L}
137 * <li>{@link #REORDER_INVERSE_LIKE_DIRECT}
138 * <li>{@link #REORDER_INVERSE_FOR_NUMBERS_SPECIAL}
139 * </ul>
140 *
141 * <h3>Basic concept: Reordering Options</h3>
142 * Reordering options can be applied during Bidi text transformations.
143 *
144 * <b>See Also:</b>
145 * <ul>
146 * <li>{@link #setReorderingOptions}
147 * <li>{@link #OPTION_DEFAULT}
148 * <li>{@link #OPTION_INSERT_MARKS}
149 * <li>{@link #OPTION_REMOVE_CONTROLS}
150 * <li>{@link #OPTION_STREAMING}
151 * </ul>
152 *
153 *
154 * @author Simon Montagu, Matitiahu Allouche (ported from C code written by Markus W. Scherer)
155 * @stable ICU 3.8
156 *
157 *
158 * <h4> Sample code for the ICU Bidi API </h4>
159 *
160 * <h5>Rendering a paragraph with the ICU Bidi API</h5>
161 *
162 * This is (hypothetical) sample code that illustrates how the ICU Bidi API
163 * could be used to render a paragraph of text. Rendering code depends highly on
164 * the graphics system, therefore this sample code must make a lot of
165 * assumptions, which may or may not match any existing graphics system's
166 * properties.
167 *
168 * <p>
169 * The basic assumptions are:
170 * </p>
171 * <ul>
172 * <li>Rendering is done from left to right on a horizontal line.</li>
173 * <li>A run of single-style, unidirectional text can be rendered at once.
174 * </li>
175 * <li>Such a run of text is passed to the graphics system with characters
176 * (code units) in logical order.</li>
177 * <li>The line-breaking algorithm is very complicated and Locale-dependent -
178 * and therefore its implementation omitted from this sample code.</li>
179 * </ul>
180 *
181 * <pre>
182 *
183 *  package com.ibm.icu.dev.test.bidi;
184 *
185 *  import com.ibm.icu.text.Bidi;
186 *  import com.ibm.icu.text.BidiRun;
187 *
188 *  public class Sample {
189 *
190 *      static final int styleNormal = 0;
191 *      static final int styleSelected = 1;
192 *      static final int styleBold = 2;
193 *      static final int styleItalics = 4;
194 *      static final int styleSuper=8;
195 *      static final int styleSub = 16;
196 *
197 *      static class StyleRun {
198 *          int limit;
199 *          int style;
200 *
201 *          public StyleRun(int limit, int style) {
202 *              this.limit = limit;
203 *              this.style = style;
204 *          }
205 *      }
206 *
207 *      static class Bounds {
208 *          int start;
209 *          int limit;
210 *
211 *          public Bounds(int start, int limit) {
212 *              this.start = start;
213 *              this.limit = limit;
214 *          }
215 *      }
216 *
217 *      static int getTextWidth(String text, int start, int limit,
218 *                              StyleRun[] styleRuns, int styleRunCount) {
219 *          // simplistic way to compute the width
220 *          return limit - start;
221 *      }
222 *
223 *      // set limit and StyleRun limit for a line
224 *      // from text[start] and from styleRuns[styleRunStart]
225 *      // using Bidi.getLogicalRun(...)
226 *      // returns line width
227 *      static int getLineBreak(String text, Bounds line, Bidi para,
228 *                              StyleRun styleRuns[], Bounds styleRun) {
229 *          // dummy return
230 *          return 0;
231 *      }
232 *
233 *      // render runs on a line sequentially, always from left to right
234 *
235 *      // prepare rendering a new line
236 *      static void startLine(byte textDirection, int lineWidth) {
237 *          System.out.println();
238 *      }
239 *
240 *      // render a run of text and advance to the right by the run width
241 *      // the text[start..limit-1] is always in logical order
242 *      static void renderRun(String text, int start, int limit,
243 *                            byte textDirection, int style) {
244 *      }
245 *
246 *      // We could compute a cross-product
247 *      // from the style runs with the directional runs
248 *      // and then reorder it.
249 *      // Instead, here we iterate over each run type
250 *      // and render the intersections -
251 *      // with shortcuts in simple (and common) cases.
252 *      // renderParagraph() is the main function.
253 *
254 *      // render a directional run with
255 *      // (possibly) multiple style runs intersecting with it
256 *      static void renderDirectionalRun(String text, int start, int limit,
257 *                                       byte direction, StyleRun styleRuns[],
258 *                                       int styleRunCount) {
259 *          int i;
260 *
261 *          // iterate over style runs
262 *          if (direction == Bidi.LTR) {
263 *              int styleLimit;
264 *              for (i = 0; i < styleRunCount; ++i) {
265 *                  styleLimit = styleRuns[i].limit;
266 *                  if (start < styleLimit) {
267 *                      if (styleLimit > limit) {
268 *                          styleLimit = limit;
269 *                      }
270 *                      renderRun(text, start, styleLimit,
271 *                                direction, styleRuns[i].style);
272 *                      if (styleLimit == limit) {
273 *                          break;
274 *                      }
275 *                      start = styleLimit;
276 *                  }
277 *              }
278 *          } else {
279 *              int styleStart;
280 *
281 *              for (i = styleRunCount-1; i >= 0; --i) {
282 *                  if (i > 0) {
283 *                      styleStart = styleRuns[i-1].limit;
284 *                  } else {
285 *                      styleStart = 0;
286 *                  }
287 *                  if (limit >= styleStart) {
288 *                      if (styleStart < start) {
289 *                          styleStart = start;
290 *                      }
291 *                      renderRun(text, styleStart, limit, direction,
292 *                                styleRuns[i].style);
293 *                      if (styleStart == start) {
294 *                          break;
295 *                      }
296 *                      limit = styleStart;
297 *                  }
298 *              }
299 *          }
300 *      }
301 *
302 *      // the line object represents text[start..limit-1]
303 *      static void renderLine(Bidi line, String text, int start, int limit,
304 *                             StyleRun styleRuns[], int styleRunCount) {
305 *          byte direction = line.getDirection();
306 *          if (direction != Bidi.MIXED) {
307 *              // unidirectional
308 *              if (styleRunCount <= 1) {
309 *                  renderRun(text, start, limit, direction, styleRuns[0].style);
310 *              } else {
311 *                  renderDirectionalRun(text, start, limit, direction,
312 *                                       styleRuns, styleRunCount);
313 *              }
314 *          } else {
315 *              // mixed-directional
316 *              int count, i;
317 *              BidiRun run;
318 *
319 *              try {
320 *                  count = line.countRuns();
321 *              } catch (IllegalStateException e) {
322 *                  e.printStackTrace();
323 *                  return;
324 *              }
325 *              if (styleRunCount <= 1) {
326 *                  int style = styleRuns[0].style;
327 *
328 *                  // iterate over directional runs
329 *                  for (i = 0; i < count; ++i) {
330 *                      run = line.getVisualRun(i);
331 *                      renderRun(text, run.getStart(), run.getLimit(),
332 *                                run.getDirection(), style);
333 *                  }
334 *              } else {
335 *                  // iterate over both directional and style runs
336 *                  for (i = 0; i < count; ++i) {
337 *                      run = line.getVisualRun(i);
338 *                      renderDirectionalRun(text, run.getStart(),
339 *                                           run.getLimit(), run.getDirection(),
340 *                                           styleRuns, styleRunCount);
341 *                  }
342 *              }
343 *          }
344 *      }
345 *
346 *      static void renderParagraph(String text, byte textDirection,
347 *                                  StyleRun styleRuns[], int styleRunCount,
348 *                                  int lineWidth) {
349 *          int length = text.length();
350 *          Bidi para = new Bidi();
351 *          try {
352 *              para.setPara(text,
353 *                           textDirection != 0 ? Bidi.LEVEL_DEFAULT_RTL
354 *                                              : Bidi.LEVEL_DEFAULT_LTR,
355 *                           null);
356 *          } catch (Exception e) {
357 *              e.printStackTrace();
358 *              return;
359 *          }
360 *          byte paraLevel = (byte)(1 & para.getParaLevel());
361 *          StyleRun styleRun = new StyleRun(length, styleNormal);
362 *
363 *          if (styleRuns == null || styleRunCount <= 0) {
364 *              styleRuns = new StyleRun[1];
365 *              styleRunCount = 1;
366 *              styleRuns[0] = styleRun;
367 *          }
368 *          // assume styleRuns[styleRunCount-1].limit>=length
369 *
370 *          int width = getTextWidth(text, 0, length, styleRuns, styleRunCount);
371 *          if (width <= lineWidth) {
372 *              // everything fits onto one line
373 *
374 *              // prepare rendering a new line from either left or right
375 *              startLine(paraLevel, width);
376 *
377 *              renderLine(para, text, 0, length, styleRuns, styleRunCount);
378 *          } else {
379 *              // we need to render several lines
380 *              Bidi line = new Bidi(length, 0);
381 *              int start = 0, limit;
382 *              int styleRunStart = 0, styleRunLimit;
383 *
384 *              for (;;) {
385 *                  limit = length;
386 *                  styleRunLimit = styleRunCount;
387 *                  width = getLineBreak(text, new Bounds(start, limit),
388 *                                       para, styleRuns,
389 *                                       new Bounds(styleRunStart, styleRunLimit));
390 *                  try {
391 *                      line = para.setLine(start, limit);
392 *                  } catch (Exception e) {
393 *                      e.printStackTrace();
394 *                      return;
395 *                  }
396 *                  // prepare rendering a new line
397 *                  // from either left or right
398 *                  startLine(paraLevel, width);
399 *
400 *                  if (styleRunStart > 0) {
401 *                      int newRunCount = styleRuns.length - styleRunStart;
402 *                      StyleRun[] newRuns = new StyleRun[newRunCount];
403 *                      System.arraycopy(styleRuns, styleRunStart, newRuns, 0,
404 *                                       newRunCount);
405 *                      renderLine(line, text, start, limit, newRuns,
406 *                                 styleRunLimit - styleRunStart);
407 *                  } else {
408 *                      renderLine(line, text, start, limit, styleRuns,
409 *                                 styleRunLimit - styleRunStart);
410 *                  }
411 *                  if (limit == length) {
412 *                      break;
413 *                  }
414 *                  start = limit;
415 *                  styleRunStart = styleRunLimit - 1;
416 *                  if (start >= styleRuns[styleRunStart].limit) {
417 *                      ++styleRunStart;
418 *                  }
419 *              }
420 *          }
421 *      }
422 *
423 *      public static void main(String[] args)
424 *      {
425 *          renderParagraph("Some Latin text...", Bidi.LTR, null, 0, 80);
426 *          renderParagraph("Some Hebrew text...", Bidi.RTL, null, 0, 60);
427 *      }
428 *  }
429 *
430 * </pre>
431 */
432
433/*
434 * General implementation notes:
435 *
436 * Throughout the implementation, there are comments like (W2) that refer to
437 * rules of the BiDi algorithm, in this example to the second rule of the
438 * resolution of weak types.
439 *
440 * For handling surrogate pairs, where two UChar's form one "abstract" (or UTF-32)
441 * character according to UTF-16, the second UChar gets the directional property of
442 * the entire character assigned, while the first one gets a BN, a boundary
443 * neutral, type, which is ignored by most of the algorithm according to
444 * rule (X9) and the implementation suggestions of the BiDi algorithm.
445 *
446 * Later, adjustWSLevels() will set the level for each BN to that of the
447 * following character (UChar), which results in surrogate pairs getting the
448 * same level on each of their surrogates.
449 *
450 * In a UTF-8 implementation, the same thing could be done: the last byte of
451 * a multi-byte sequence would get the "real" property, while all previous
452 * bytes of that sequence would get BN.
453 *
454 * It is not possible to assign all those parts of a character the same real
455 * property because this would fail in the resolution of weak types with rules
456 * that look at immediately surrounding types.
457 *
458 * As a related topic, this implementation does not remove Boundary Neutral
459 * types from the input, but ignores them wherever this is relevant.
460 * For example, the loop for the resolution of the weak types reads
461 * types until it finds a non-BN.
462 * Also, explicit embedding codes are neither changed into BN nor removed.
463 * They are only treated the same way real BNs are.
464 * As stated before, adjustWSLevels() takes care of them at the end.
465 * For the purpose of conformance, the levels of all these codes
466 * do not matter.
467 *
468 * Note that this implementation modifies the dirProps
469 * after the initial setup, when applying X5c (replace FSI by LRI or RLI),
470 * X6, N0 (replace paired brackets by L or R).
471 *
472 * In this implementation, the resolution of weak types (W1 to W6),
473 * neutrals (N1 and N2), and the assignment of the resolved level (In)
474 * are all done in one single loop, in resolveImplicitLevels().
475 * Changes of dirProp values are done on the fly, without writing
476 * them back to the dirProps array.
477 *
478 *
479 * This implementation contains code that allows to bypass steps of the
480 * algorithm that are not needed on the specific paragraph
481 * in order to speed up the most common cases considerably,
482 * like text that is entirely LTR, or RTL text without numbers.
483 *
484 * Most of this is done by setting a bit for each directional property
485 * in a flags variable and later checking for whether there are
486 * any LTR characters or any RTL characters, or both, whether
487 * there are any explicit embedding codes, etc.
488 *
489 * If the (Xn) steps are performed, then the flags are re-evaluated,
490 * because they will then not contain the embedding codes any more
491 * and will be adjusted for override codes, so that subsequently
492 * more bypassing may be possible than what the initial flags suggested.
493 *
494 * If the text is not mixed-directional, then the
495 * algorithm steps for the weak type resolution are not performed,
496 * and all levels are set to the paragraph level.
497 *
498 * If there are no explicit embedding codes, then the (Xn) steps
499 * are not performed.
500 *
501 * If embedding levels are supplied as a parameter, then all
502 * explicit embedding codes are ignored, and the (Xn) steps
503 * are not performed.
504 *
505 * White Space types could get the level of the run they belong to,
506 * and are checked with a test of (flags&MASK_EMBEDDING) to
507 * consider if the paragraph direction should be considered in
508 * the flags variable.
509 *
510 * If there are no White Space types in the paragraph, then
511 * (L1) is not necessary in adjustWSLevels().
512 */
513
514public class Bidi {
515
516    static class Point {
517        int pos;    /* position in text */
518        int flag;   /* flag for LRM/RLM, before/after */
519    }
520
521    static class InsertPoints {
522        int size;
523        int confirmed;
524        Point[] points = new Point[0];
525    }
526
527    static class Opening {
528        int   position;                 /* position of opening bracket */
529        int   match;                    /* matching char or -position of closing bracket */
530        int   contextPos;               /* position of last strong char found before opening */
531        short flags;                    /* bits for L or R/AL found within the pair */
532        byte  contextDir;               /* L or R according to last strong char before opening */
533        byte  filler;                   /* to complete a nice multiple of 4 bytes */
534    }
535
536    static class IsoRun {
537        int   contextPos;               /* position of char determining context */
538        short start;                    /* index of first opening entry for this run */
539        short limit;                    /* index after last opening entry for this run */
540        byte  level;                    /* level of this run */
541        byte  lastStrong;               /* bidi class of last strong char found in this run */
542        byte  lastBase;                 /* bidi class of last base char found in this run */
543        byte  contextDir;               /* L or R to use as context for following openings */
544    }
545
546    static class BracketData {
547        Opening[] openings = new Opening[SIMPLE_OPENINGS_COUNT];
548        int   isoRunLast;               /* index of last used entry */
549        /* array of nested isolated sequence entries; can never excess UBIDI_MAX_EXPLICIT_LEVEL
550           + 1 for index 0, + 1 for before the first isolated sequence */
551        IsoRun[]  isoRuns = new IsoRun[MAX_EXPLICIT_LEVEL+2];
552        boolean   isNumbersSpecial;     /*reordering mode for NUMBERS_SPECIAL */
553    }
554
555    static class Isolate {
556        int   startON;
557        int   start1;
558        short stateImp;
559        short state;
560    }
561
562    /** Paragraph level setting<p>
563     *
564     * Constant indicating that the base direction depends on the first strong
565     * directional character in the text according to the Unicode Bidirectional
566     * Algorithm. If no strong directional character is present,
567     * then set the paragraph level to 0 (left-to-right).<p>
568     *
569     * If this value is used in conjunction with reordering modes
570     * <code>REORDER_INVERSE_LIKE_DIRECT</code> or
571     * <code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the text to reorder
572     * is assumed to be visual LTR, and the text after reordering is required
573     * to be the corresponding logical string with appropriate contextual
574     * direction. The direction of the result string will be RTL if either
575     * the rightmost or leftmost strong character of the source text is RTL
576     * or Arabic Letter, the direction will be LTR otherwise.<p>
577     *
578     * If reordering option <code>OPTION_INSERT_MARKS</code> is set, an RLM may
579     * be added at the beginning of the result string to ensure round trip
580     * (that the result string, when reordered back to visual, will produce
581     * the original source text).
582     * @see #REORDER_INVERSE_LIKE_DIRECT
583     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
584     * @stable ICU 3.8
585     */
586    public static final byte LEVEL_DEFAULT_LTR = (byte)0x7e;
587
588    /** Paragraph level setting<p>
589     *
590     * Constant indicating that the base direction depends on the first strong
591     * directional character in the text according to the Unicode Bidirectional
592     * Algorithm. If no strong directional character is present,
593     * then set the paragraph level to 1 (right-to-left).<p>
594     *
595     * If this value is used in conjunction with reordering modes
596     * <code>REORDER_INVERSE_LIKE_DIRECT</code> or
597     * <code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the text to reorder
598     * is assumed to be visual LTR, and the text after reordering is required
599     * to be the corresponding logical string with appropriate contextual
600     * direction. The direction of the result string will be RTL if either
601     * the rightmost or leftmost strong character of the source text is RTL
602     * or Arabic Letter, or if the text contains no strong character;
603     * the direction will be LTR otherwise.<p>
604     *
605     * If reordering option <code>OPTION_INSERT_MARKS</code> is set, an RLM may
606     * be added at the beginning of the result string to ensure round trip
607     * (that the result string, when reordered back to visual, will produce
608     * the original source text).
609     * @see #REORDER_INVERSE_LIKE_DIRECT
610     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
611     * @stable ICU 3.8
612     */
613    public static final byte LEVEL_DEFAULT_RTL = (byte)0x7f;
614
615    /**
616     * Maximum explicit embedding level.
617     * (The maximum resolved level can be up to <code>MAX_EXPLICIT_LEVEL+1</code>).
618     * @stable ICU 3.8
619     */
620    public static final byte MAX_EXPLICIT_LEVEL = 125;
621
622    /**
623     * Bit flag for level input.
624     * Overrides directional properties.
625     * @stable ICU 3.8
626     */
627    public static final byte LEVEL_OVERRIDE = (byte)0x80;
628
629    /**
630     * Special value which can be returned by the mapping methods when a
631     * logical index has no corresponding visual index or vice-versa. This may
632     * happen for the logical-to-visual mapping of a Bidi control when option
633     * <code>OPTION_REMOVE_CONTROLS</code> is
634     * specified. This can also happen for the visual-to-logical mapping of a
635     * Bidi mark (LRM or RLM) inserted by option
636     * <code>OPTION_INSERT_MARKS</code>.
637     * @see #getVisualIndex
638     * @see #getVisualMap
639     * @see #getLogicalIndex
640     * @see #getLogicalMap
641     * @see #OPTION_INSERT_MARKS
642     * @see #OPTION_REMOVE_CONTROLS
643     * @stable ICU 3.8
644     */
645    public static final int MAP_NOWHERE = -1;
646
647    /**
648     * Left-to-right text.
649     * <ul>
650     * <li>As return value for <code>getDirection()</code>, it means
651     *     that the source string contains no right-to-left characters, or
652     *     that the source string is empty and the paragraph level is even.
653     * <li>As return value for <code>getBaseDirection()</code>, it
654     *     means that the first strong character of the source string has
655     *     a left-to-right direction.
656     * </ul>
657     * @stable ICU 3.8
658     */
659    public static final byte LTR = 0;
660
661    /**
662     * Right-to-left text.
663     * <ul>
664     * <li>As return value for <code>getDirection()</code>, it means
665     *     that the source string contains no left-to-right characters, or
666     *     that the source string is empty and the paragraph level is odd.
667     * <li>As return value for <code>getBaseDirection()</code>, it
668     *     means that the first strong character of the source string has
669     *     a right-to-left direction.
670     * </ul>
671     * @stable ICU 3.8
672     */
673    public static final byte RTL = 1;
674
675    /**
676     * Mixed-directional text.
677     * <p>As return value for <code>getDirection()</code>, it means
678     *    that the source string contains both left-to-right and
679     *    right-to-left characters.
680     * @stable ICU 3.8
681     */
682    public static final byte MIXED = 2;
683
684    /**
685     * No strongly directional text.
686     * <p>As return value for <code>getBaseDirection()</code>, it means
687     *    that the source string is missing or empty, or contains neither
688     *    left-to-right nor right-to-left characters.
689     * @stable ICU 4.6
690     */
691    public static final byte NEUTRAL = 3;
692
693    /**
694     * option bit for writeReordered():
695     * keep combining characters after their base characters in RTL runs
696     *
697     * @see #writeReordered
698     * @stable ICU 3.8
699     */
700    public static final short KEEP_BASE_COMBINING = 1;
701
702    /**
703     * option bit for writeReordered():
704     * replace characters with the "mirrored" property in RTL runs
705     * by their mirror-image mappings
706     *
707     * @see #writeReordered
708     * @stable ICU 3.8
709     */
710    public static final short DO_MIRRORING = 2;
711
712    /**
713     * option bit for writeReordered():
714     * surround the run with LRMs if necessary;
715     * this is part of the approximate "inverse Bidi" algorithm
716     *
717     * <p>This option does not imply corresponding adjustment of the index
718     * mappings.</p>
719     *
720     * @see #setInverse
721     * @see #writeReordered
722     * @stable ICU 3.8
723     */
724    public static final short INSERT_LRM_FOR_NUMERIC = 4;
725
726    /**
727     * option bit for writeReordered():
728     * remove Bidi control characters
729     * (this does not affect INSERT_LRM_FOR_NUMERIC)
730     *
731     * <p>This option does not imply corresponding adjustment of the index
732     * mappings.</p>
733     *
734     * @see #writeReordered
735     * @see #INSERT_LRM_FOR_NUMERIC
736     * @stable ICU 3.8
737     */
738    public static final short REMOVE_BIDI_CONTROLS = 8;
739
740    /**
741     * option bit for writeReordered():
742     * write the output in reverse order
743     *
744     * <p>This has the same effect as calling <code>writeReordered()</code>
745     * first without this option, and then calling
746     * <code>writeReverse()</code> without mirroring.
747     * Doing this in the same step is faster and avoids a temporary buffer.
748     * An example for using this option is output to a character terminal that
749     * is designed for RTL scripts and stores text in reverse order.</p>
750     *
751     * @see #writeReordered
752     * @stable ICU 3.8
753     */
754    public static final short OUTPUT_REVERSE = 16;
755
756    /** Reordering mode: Regular Logical to Visual Bidi algorithm according to Unicode.
757     * @see #setReorderingMode
758     * @stable ICU 3.8
759     */
760    public static final short REORDER_DEFAULT = 0;
761
762    /** Reordering mode: Logical to Visual algorithm which handles numbers in
763     * a way which mimicks the behavior of Windows XP.
764     * @see #setReorderingMode
765     * @stable ICU 3.8
766     */
767    public static final short REORDER_NUMBERS_SPECIAL = 1;
768
769    /** Reordering mode: Logical to Visual algorithm grouping numbers with
770     * adjacent R characters (reversible algorithm).
771     * @see #setReorderingMode
772     * @stable ICU 3.8
773     */
774    public static final short REORDER_GROUP_NUMBERS_WITH_R = 2;
775
776    /** Reordering mode: Reorder runs only to transform a Logical LTR string
777     * to the logical RTL string with the same display, or vice-versa.<br>
778     * If this mode is set together with option
779     * <code>OPTION_INSERT_MARKS</code>, some Bidi controls in the source
780     * text may be removed and other controls may be added to produce the
781     * minimum combination which has the required display.
782     * @see #OPTION_INSERT_MARKS
783     * @see #setReorderingMode
784     * @stable ICU 3.8
785     */
786    public static final short REORDER_RUNS_ONLY = 3;
787
788    /** Reordering mode: Visual to Logical algorithm which handles numbers
789     * like L (same algorithm as selected by <code>setInverse(true)</code>.
790     * @see #setInverse
791     * @see #setReorderingMode
792     * @stable ICU 3.8
793     */
794    public static final short REORDER_INVERSE_NUMBERS_AS_L = 4;
795
796    /** Reordering mode: Visual to Logical algorithm equivalent to the regular
797     * Logical to Visual algorithm.
798     * @see #setReorderingMode
799     * @stable ICU 3.8
800     */
801    public static final short REORDER_INVERSE_LIKE_DIRECT = 5;
802
803    /** Reordering mode: Inverse Bidi (Visual to Logical) algorithm for the
804     * <code>REORDER_NUMBERS_SPECIAL</code> Bidi algorithm.
805     * @see #setReorderingMode
806     * @stable ICU 3.8
807     */
808    public static final short REORDER_INVERSE_FOR_NUMBERS_SPECIAL = 6;
809
810    /*  Number of values for reordering mode. */
811    static final short REORDER_COUNT = 7;
812
813    /* Reordering mode values must be ordered so that all the regular logical to
814     * visual modes come first, and all inverse Bidi modes come last.
815     */
816    static final short REORDER_LAST_LOGICAL_TO_VISUAL =
817            REORDER_NUMBERS_SPECIAL;
818
819    /**
820     * Option value for <code>setReorderingOptions</code>:
821     * disable all the options which can be set with this method
822     * @see #setReorderingOptions
823     * @stable ICU 3.8
824     */
825    public static final int OPTION_DEFAULT = 0;
826
827    /**
828     * Option bit for <code>setReorderingOptions</code>:
829     * insert Bidi marks (LRM or RLM) when needed to ensure correct result of
830     * a reordering to a Logical order
831     *
832     * <p>This option must be set or reset before calling
833     * <code>setPara</code>.</p>
834     *
835     * <p>This option is significant only with reordering modes which generate
836     * a result with Logical order, specifically.</p>
837     * <ul>
838     *   <li><code>REORDER_RUNS_ONLY</code></li>
839     *   <li><code>REORDER_INVERSE_NUMBERS_AS_L</code></li>
840     *   <li><code>REORDER_INVERSE_LIKE_DIRECT</code></li>
841     *   <li><code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code></li>
842     * </ul>
843     *
844     * <p>If this option is set in conjunction with reordering mode
845     * <code>REORDER_INVERSE_NUMBERS_AS_L</code> or with calling
846     * <code>setInverse(true)</code>, it implies option
847     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to method
848     * <code>writeReordered()</code>.</p>
849     *
850     * <p>For other reordering modes, a minimum number of LRM or RLM characters
851     * will be added to the source text after reordering it so as to ensure
852     * round trip, i.e. when applying the inverse reordering mode on the
853     * resulting logical text with removal of Bidi marks
854     * (option <code>OPTION_REMOVE_CONTROLS</code> set before calling
855     * <code>setPara()</code> or option
856     * <code>REMOVE_BIDI_CONTROLS</code> in
857     * <code>writeReordered</code>), the result will be identical to the
858     * source text in the first transformation.
859     *
860     * <p>This option will be ignored if specified together with option
861     * <code>OPTION_REMOVE_CONTROLS</code>. It inhibits option
862     * <code>REMOVE_BIDI_CONTROLS</code> in calls to method
863     * <code>writeReordered()</code> and it implies option
864     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to method
865     * <code>writeReordered()</code> if the reordering mode is
866     * <code>REORDER_INVERSE_NUMBERS_AS_L</code>.</p>
867     *
868     * @see #setReorderingMode
869     * @see #setReorderingOptions
870     * @see #INSERT_LRM_FOR_NUMERIC
871     * @see #REMOVE_BIDI_CONTROLS
872     * @see #OPTION_REMOVE_CONTROLS
873     * @see #REORDER_RUNS_ONLY
874     * @see #REORDER_INVERSE_NUMBERS_AS_L
875     * @see #REORDER_INVERSE_LIKE_DIRECT
876     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
877     * @stable ICU 3.8
878     */
879    public static final int OPTION_INSERT_MARKS = 1;
880
881    /**
882     * Option bit for <code>setReorderingOptions</code>:
883     * remove Bidi control characters
884     *
885     * <p>This option must be set or reset before calling
886     * <code>setPara</code>.</p>
887     *
888     * <p>This option nullifies option
889     * <code>OPTION_INSERT_MARKS</code>. It inhibits option
890     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to method
891     * <code>writeReordered()</code> and it implies option
892     * <code>REMOVE_BIDI_CONTROLS</code> in calls to that method.</p>
893     *
894     * @see #setReorderingMode
895     * @see #setReorderingOptions
896     * @see #OPTION_INSERT_MARKS
897     * @see #INSERT_LRM_FOR_NUMERIC
898     * @see #REMOVE_BIDI_CONTROLS
899     * @stable ICU 3.8
900     */
901    public static final int OPTION_REMOVE_CONTROLS = 2;
902
903    /**
904     * Option bit for <code>setReorderingOptions</code>:
905     * process the output as part of a stream to be continued
906     *
907     * <p>This option must be set or reset before calling
908     * <code>setPara</code>.</p>
909     *
910     * <p>This option specifies that the caller is interested in processing
911     * large text object in parts. The results of the successive calls are
912     * expected to be concatenated by the caller. Only the call for the last
913     * part will have this option bit off.</p>
914     *
915     * <p>When this option bit is on, <code>setPara()</code> may process
916     * less than the full source text in order to truncate the text at a
917     * meaningful boundary. The caller should call
918     * <code>getProcessedLength()</code> immediately after calling
919     * <code>setPara()</code> in order to determine how much of the source
920     * text has been processed. Source text beyond that length should be
921     * resubmitted in following calls to <code>setPara</code>. The
922     * processed length may be less than the length of the source text if a
923     * character preceding the last character of the source text constitutes a
924     * reasonable boundary (like a block separator) for text to be continued.<br>
925     * If the last character of the source text constitutes a reasonable
926     * boundary, the whole text will be processed at once.<br>
927     * If nowhere in the source text there exists
928     * such a reasonable boundary, the processed length will be zero.<br>
929     * The caller should check for such an occurrence and do one of the following:
930     * <ul><li>submit a larger amount of text with a better chance to include
931     *         a reasonable boundary.</li>
932     *     <li>resubmit the same text after turning off option
933     *         <code>OPTION_STREAMING</code>.</li></ul>
934     * In all cases, this option should be turned off before processing the last
935     * part of the text.</p>
936     *
937     * <p>When the <code>OPTION_STREAMING</code> option is used, it is
938     * recommended to call <code>orderParagraphsLTR(true)</code> before calling
939     * <code>setPara()</code> so that later paragraphs may be concatenated to
940     * previous paragraphs on the right.
941     * </p>
942     *
943     * @see #setReorderingMode
944     * @see #setReorderingOptions
945     * @see #getProcessedLength
946     * @stable ICU 3.8
947     */
948    public static final int OPTION_STREAMING = 4;
949
950    /*
951     *   Comparing the description of the Bidi algorithm with this implementation
952     *   is easier with the same names for the Bidi types in the code as there.
953     *   See UCharacterDirection
954     */
955    static final byte L   = UCharacterDirection.LEFT_TO_RIGHT;                  /*  0 */
956    static final byte R   = UCharacterDirection.RIGHT_TO_LEFT;                  /*  1 */
957    static final byte EN  = UCharacterDirection.EUROPEAN_NUMBER;                /*  2 */
958    static final byte ES  = UCharacterDirection.EUROPEAN_NUMBER_SEPARATOR;      /*  3 */
959    static final byte ET  = UCharacterDirection.EUROPEAN_NUMBER_TERMINATOR;     /*  4 */
960    static final byte AN  = UCharacterDirection.ARABIC_NUMBER;                  /*  5 */
961    static final byte CS  = UCharacterDirection.COMMON_NUMBER_SEPARATOR;        /*  6 */
962    static final byte B   = UCharacterDirection.BLOCK_SEPARATOR;                /*  7 */
963    static final byte S   = UCharacterDirection.SEGMENT_SEPARATOR;              /*  8 */
964    static final byte WS  = UCharacterDirection.WHITE_SPACE_NEUTRAL;            /*  9 */
965    static final byte ON  = UCharacterDirection.OTHER_NEUTRAL;                  /* 10 */
966    static final byte LRE = UCharacterDirection.LEFT_TO_RIGHT_EMBEDDING;        /* 11 */
967    static final byte LRO = UCharacterDirection.LEFT_TO_RIGHT_OVERRIDE;         /* 12 */
968    static final byte AL  = UCharacterDirection.RIGHT_TO_LEFT_ARABIC;           /* 13 */
969    static final byte RLE = UCharacterDirection.RIGHT_TO_LEFT_EMBEDDING;        /* 14 */
970    static final byte RLO = UCharacterDirection.RIGHT_TO_LEFT_OVERRIDE;         /* 15 */
971    static final byte PDF = UCharacterDirection.POP_DIRECTIONAL_FORMAT;         /* 16 */
972    static final byte NSM = UCharacterDirection.DIR_NON_SPACING_MARK;           /* 17 */
973    static final byte BN  = UCharacterDirection.BOUNDARY_NEUTRAL;               /* 18 */
974    static final byte FSI = UCharacterDirection.FIRST_STRONG_ISOLATE;           /* 19 */
975    static final byte LRI = UCharacterDirection.LEFT_TO_RIGHT_ISOLATE;          /* 20 */
976    static final byte RLI = UCharacterDirection.RIGHT_TO_LEFT_ISOLATE;          /* 21 */
977    static final byte PDI = UCharacterDirection.POP_DIRECTIONAL_ISOLATE;        /* 22 */
978    static final byte ENL = PDI + 1;    /* EN after W7 */                       /* 23 */
979    static final byte ENR = ENL + 1;    /* EN not subject to W7 */              /* 24 */
980
981    /**
982     * Value returned by <code>BidiClassifier</code> when there is no need to
983     * override the standard Bidi class for a given code point.
984     * @see BidiClassifier
985     * @stable ICU 3.8
986     */
987    public static final int CLASS_DEFAULT = UCharacterDirection
988                                            .CHAR_DIRECTION_COUNT;
989
990    /* number of paras entries allocated initially */
991    static final int SIMPLE_PARAS_COUNT = 10;
992    /* number of isolate run entries for paired brackets allocated initially */
993    static final int SIMPLE_OPENINGS_COUNT = 20;
994
995    private static final char CR = '\r';
996    private static final char LF = '\n';
997
998    static final int LRM_BEFORE = 1;
999    static final int LRM_AFTER = 2;
1000    static final int RLM_BEFORE = 4;
1001    static final int RLM_AFTER = 8;
1002
1003    /* flags for Opening.flags */
1004    static final byte FOUND_L = (byte)DirPropFlag(L);
1005    static final byte FOUND_R = (byte)DirPropFlag(R);
1006
1007    /*
1008     * The following bit is used for the directional isolate status.
1009     * Stack entries corresponding to isolate sequences are greater than ISOLATE.
1010     */
1011    static final int ISOLATE = 0x0100;
1012
1013
1014    /*
1015     * reference to parent paragraph object (reference to self if this object is
1016     * a paragraph object); set to null in a newly opened object; set to a
1017     * real value after a successful execution of setPara or setLine
1018     */
1019    Bidi                paraBidi;
1020
1021    final UBiDiProps    bdp;
1022
1023    /* character array representing the current text */
1024    char[]              text;
1025
1026    /* length of the current text */
1027    int                 originalLength;
1028
1029    /* if the option OPTION_STREAMING is set, this is the length of
1030     * text actually processed by <code>setPara</code>, which may be shorter
1031     * than the original length. Otherwise, it is identical to the original
1032     * length.
1033     */
1034    int                 length;
1035
1036    /* if option OPTION_REMOVE_CONTROLS is set, and/or Bidi
1037     * marks are allowed to be inserted in one of the reordering modes, the
1038     * length of the result string may be different from the processed length.
1039     */
1040    int                 resultLength;
1041
1042    /* indicators for whether memory may be allocated after construction */
1043    boolean             mayAllocateText;
1044    boolean             mayAllocateRuns;
1045
1046    /* arrays with one value per text-character */
1047    byte[]              dirPropsMemory = new byte[1];
1048    byte[]              levelsMemory = new byte[1];
1049    byte[]              dirProps;
1050    byte[]              levels;
1051
1052    /* are we performing an approximation of the "inverse Bidi" algorithm? */
1053    boolean             isInverse;
1054
1055    /* are we using the basic algorithm or its variation? */
1056    int                 reorderingMode;
1057
1058    /* bitmask for reordering options */
1059    int                 reorderingOptions;
1060
1061    /* must block separators receive level 0? */
1062    boolean             orderParagraphsLTR;
1063
1064    /* the paragraph level */
1065    byte                paraLevel;
1066    /* original paraLevel when contextual */
1067    /* must be one of DEFAULT_xxx or 0 if not contextual */
1068    byte                defaultParaLevel;
1069
1070    /* context data */
1071    String              prologue;
1072    String              epilogue;
1073
1074    /* the following is set in setPara, used in processPropertySeq */
1075
1076    ImpTabPair          impTabPair;  /* reference to levels state table pair */
1077    /* the overall paragraph or line directionality*/
1078    byte                direction;
1079
1080    /* flags is a bit set for which directional properties are in the text */
1081    int                 flags;
1082
1083    /* lastArabicPos is index to the last AL in the text, -1 if none */
1084    int                 lastArabicPos;
1085
1086    /* characters after trailingWSStart are WS and are */
1087    /* implicitly at the paraLevel (rule (L1)) - levels may not reflect that */
1088    int                 trailingWSStart;
1089
1090    /* fields for paragraph handling, set in getDirProps() */
1091    int                 paraCount;
1092    int[]               paras_limit = new int[SIMPLE_PARAS_COUNT];
1093    byte[]              paras_level = new byte[SIMPLE_PARAS_COUNT];
1094
1095    /* fields for line reordering */
1096    int                 runCount;     /* ==-1: runs not set up yet */
1097    BidiRun[]           runsMemory = new BidiRun[0];
1098    BidiRun[]           runs;
1099
1100    /* for non-mixed text, we only need a tiny array of runs (no allocation) */
1101    BidiRun[]           simpleRuns = {new BidiRun()};
1102
1103    /* fields for managing isolate sequences */
1104    Isolate[]           isolates;
1105    /* maximum or current nesting depth of isolate sequences */
1106    /* Within resolveExplicitLevels() and checkExplicitLevels(), this is the maximal
1107       nesting encountered.
1108       Within resolveImplicitLevels(), this is the index of the current isolates
1109       stack entry. */
1110    int                 isolateCount;
1111
1112    /* mapping of runs in logical order to visual order */
1113    int[]               logicalToVisualRunsMap;
1114    /* flag to indicate that the map has been updated */
1115    boolean             isGoodLogicalToVisualRunsMap;
1116
1117    /* customized class provider */
1118    BidiClassifier      customClassifier = null;
1119
1120    /* for inverse Bidi with insertion of directional marks */
1121    InsertPoints        insertPoints = new InsertPoints();
1122
1123    /* for option OPTION_REMOVE_CONTROLS */
1124    int                 controlCount;
1125
1126    /*
1127     * Sometimes, bit values are more appropriate
1128     * to deal with directionality properties.
1129     * Abbreviations in these method names refer to names
1130     * used in the Bidi algorithm.
1131     */
1132    static int DirPropFlag(byte dir) {
1133        return (1 << dir);
1134    }
1135
1136    boolean testDirPropFlagAt(int flag, int index) {
1137        return ((DirPropFlag(dirProps[index]) & flag) != 0);
1138    }
1139
1140    static final int DirPropFlagMultiRuns = DirPropFlag((byte)31);
1141
1142    /* to avoid some conditional statements, use tiny constant arrays */
1143    static final int DirPropFlagLR[] = { DirPropFlag(L), DirPropFlag(R) };
1144    static final int DirPropFlagE[] = { DirPropFlag(LRE), DirPropFlag(RLE) };
1145    static final int DirPropFlagO[] = { DirPropFlag(LRO), DirPropFlag(RLO) };
1146
1147    static final int DirPropFlagLR(byte level) { return DirPropFlagLR[level & 1]; }
1148    static final int DirPropFlagE(byte level)  { return DirPropFlagE[level & 1]; }
1149    static final int DirPropFlagO(byte level)  { return DirPropFlagO[level & 1]; }
1150    static final byte DirFromStrong(byte strong) { return strong == L ? L : R; }
1151    static final byte NoOverride(byte level) { return (byte)(level & ~LEVEL_OVERRIDE); }
1152
1153    /*  are there any characters that are LTR or RTL? */
1154    static final int MASK_LTR =
1155        DirPropFlag(L)|DirPropFlag(EN)|DirPropFlag(ENL)|DirPropFlag(ENR)|DirPropFlag(AN)|DirPropFlag(LRE)|DirPropFlag(LRO)|DirPropFlag(LRI);
1156    static final int MASK_RTL = DirPropFlag(R)|DirPropFlag(AL)|DirPropFlag(RLE)|DirPropFlag(RLO)|DirPropFlag(RLI);
1157
1158    static final int MASK_R_AL = DirPropFlag(R)|DirPropFlag(AL);
1159    static final int MASK_STRONG_EN_AN = DirPropFlag(L)|DirPropFlag(R)|DirPropFlag(AL)|DirPropFlag(EN)|DirPropFlag(AN);
1160    /* explicit embedding codes */
1161    static final int MASK_EXPLICIT = DirPropFlag(LRE)|DirPropFlag(LRO)|DirPropFlag(RLE)|DirPropFlag(RLO)|DirPropFlag(PDF);
1162    static final int MASK_BN_EXPLICIT = DirPropFlag(BN)|MASK_EXPLICIT;
1163
1164    /* explicit isolate codes */
1165    static final int MASK_ISO = DirPropFlag(LRI)|DirPropFlag(RLI)|DirPropFlag(FSI)|DirPropFlag(PDI);
1166
1167    /* paragraph and segment separators */
1168    static final int MASK_B_S = DirPropFlag(B)|DirPropFlag(S);
1169
1170    /* all types that are counted as White Space or Neutral in some steps */
1171    static final int MASK_WS = MASK_B_S|DirPropFlag(WS)|MASK_BN_EXPLICIT|MASK_ISO;
1172
1173    /* types that are neutrals or could becomes neutrals in (Wn) */
1174    static final int MASK_POSSIBLE_N = DirPropFlag(ON)|DirPropFlag(CS)|DirPropFlag(ES)|DirPropFlag(ET)|MASK_WS;
1175
1176    /*
1177     * These types may be changed to "e",
1178     * the embedding type (L or R) of the run,
1179     * in the Bidi algorithm (N2)
1180     */
1181    static final int MASK_EMBEDDING = DirPropFlag(NSM)|MASK_POSSIBLE_N;
1182
1183    /*
1184     *  the dirProp's L and R are defined to 0 and 1 values in UCharacterDirection.java
1185     */
1186    static byte GetLRFromLevel(byte level)
1187    {
1188        return (byte)(level & 1);
1189    }
1190
1191    static boolean IsDefaultLevel(byte level)
1192    {
1193        return ((level & LEVEL_DEFAULT_LTR) == LEVEL_DEFAULT_LTR);
1194    }
1195
1196    static boolean IsBidiControlChar(int c)
1197    {
1198        /* check for range 0x200c to 0x200f (ZWNJ, ZWJ, LRM, RLM) or
1199                           0x202a to 0x202e (LRE, RLE, PDF, LRO, RLO) */
1200        return (((c & 0xfffffffc) == 0x200c) || ((c >= 0x202a) && (c <= 0x202e))
1201                                             || ((c >= 0x2066) && (c <= 0x2069)));
1202    }
1203
1204    void verifyValidPara()
1205    {
1206        if (!(this == this.paraBidi)) {
1207            throw new IllegalStateException();
1208        }
1209    }
1210
1211    void verifyValidParaOrLine()
1212    {
1213        Bidi para = this.paraBidi;
1214        /* verify Para */
1215        if (this == para) {
1216            return;
1217        }
1218        /* verify Line */
1219        if ((para == null) || (para != para.paraBidi)) {
1220            throw new IllegalStateException();
1221        }
1222    }
1223
1224    void verifyRange(int index, int start, int limit)
1225    {
1226        if (index < start || index >= limit) {
1227            throw new IllegalArgumentException("Value " + index +
1228                      " is out of range " + start + " to " + limit);
1229        }
1230    }
1231
1232    /**
1233     * Allocate a <code>Bidi</code> object.
1234     * Such an object is initially empty. It is assigned
1235     * the Bidi properties of a piece of text containing one or more paragraphs
1236     * by <code>setPara()</code>
1237     * or the Bidi properties of a line within a paragraph by
1238     * <code>setLine()</code>.<p>
1239     * This object can be reused.<p>
1240     * <code>setPara()</code> and <code>setLine()</code> will allocate
1241     * additional memory for internal structures as necessary.
1242     *
1243     * @stable ICU 3.8
1244     */
1245    public Bidi()
1246    {
1247        this(0, 0);
1248    }
1249
1250    /**
1251     * Allocate a <code>Bidi</code> object with preallocated memory
1252     * for internal structures.
1253     * This method provides a <code>Bidi</code> object like the default constructor
1254     * but it also preallocates memory for internal structures
1255     * according to the sizings supplied by the caller.<p>
1256     * The preallocation can be limited to some of the internal memory
1257     * by setting some values to 0 here. That means that if, e.g.,
1258     * <code>maxRunCount</code> cannot be reasonably predetermined and should not
1259     * be set to <code>maxLength</code> (the only failproof value) to avoid
1260     * wasting  memory, then <code>maxRunCount</code> could be set to 0 here
1261     * and the internal structures that are associated with it will be allocated
1262     * on demand, just like with the default constructor.
1263     *
1264     * @param maxLength is the maximum text or line length that internal memory
1265     *        will be preallocated for. An attempt to associate this object with a
1266     *        longer text will fail, unless this value is 0, which leaves the allocation
1267     *        up to the implementation.
1268     *
1269     * @param maxRunCount is the maximum anticipated number of same-level runs
1270     *        that internal memory will be preallocated for. An attempt to access
1271     *        visual runs on an object that was not preallocated for as many runs
1272     *        as the text was actually resolved to will fail,
1273     *        unless this value is 0, which leaves the allocation up to the implementation.<br><br>
1274     *        The number of runs depends on the actual text and maybe anywhere between
1275     *        1 and <code>maxLength</code>. It is typically small.
1276     *
1277     * @throws IllegalArgumentException if maxLength or maxRunCount is less than 0
1278     * @stable ICU 3.8
1279     */
1280    public Bidi(int maxLength, int maxRunCount)
1281    {
1282        /* check the argument values */
1283        if (maxLength < 0 || maxRunCount < 0) {
1284            throw new IllegalArgumentException();
1285        }
1286
1287        /* reset the object, all reference variables null, all flags false,
1288           all sizes 0.
1289           In fact, we don't need to do anything, since class members are
1290           initialized as zero when an instance is created.
1291         */
1292        /*
1293        mayAllocateText = false;
1294        mayAllocateRuns = false;
1295        orderParagraphsLTR = false;
1296        paraCount = 0;
1297        runCount = 0;
1298        trailingWSStart = 0;
1299        flags = 0;
1300        paraLevel = 0;
1301        defaultParaLevel = 0;
1302        direction = 0;
1303        */
1304        /* get Bidi properties */
1305        bdp = UBiDiProps.INSTANCE;
1306
1307        /* allocate memory for arrays as requested */
1308        if (maxLength > 0) {
1309            getInitialDirPropsMemory(maxLength);
1310            getInitialLevelsMemory(maxLength);
1311        } else {
1312            mayAllocateText = true;
1313        }
1314
1315        if (maxRunCount > 0) {
1316            // if maxRunCount == 1, use simpleRuns[]
1317            if (maxRunCount > 1) {
1318                getInitialRunsMemory(maxRunCount);
1319            }
1320        } else {
1321            mayAllocateRuns = true;
1322        }
1323    }
1324
1325    /*
1326     * We are allowed to allocate memory if object==null or
1327     * mayAllocate==true for each array that we need.
1328     *
1329     * Assume sizeNeeded>0.
1330     * If object != null, then assume size > 0.
1331     */
1332    private Object getMemory(String label, Object array, Class<?> arrayClass,
1333            boolean mayAllocate, int sizeNeeded)
1334    {
1335        int len = Array.getLength(array);
1336
1337        /* we have at least enough memory and must not allocate */
1338        if (sizeNeeded == len) {
1339            return array;
1340        }
1341        if (!mayAllocate) {
1342            /* we must not allocate */
1343            if (sizeNeeded <= len) {
1344                return array;
1345            }
1346            throw new OutOfMemoryError("Failed to allocate memory for "
1347                                       + label);
1348        }
1349        /* we may try to grow or shrink */
1350        /* FOOD FOR THOUGHT: when shrinking it should be possible to avoid
1351           the allocation altogether and rely on this.length */
1352        try {
1353            return Array.newInstance(arrayClass, sizeNeeded);
1354        } catch (Exception e) {
1355            throw new OutOfMemoryError("Failed to allocate memory for "
1356                                       + label);
1357        }
1358    }
1359
1360    /* helper methods for each allocated array */
1361    private void getDirPropsMemory(boolean mayAllocate, int len)
1362    {
1363        Object array = getMemory("DirProps", dirPropsMemory, Byte.TYPE, mayAllocate, len);
1364        dirPropsMemory = (byte[]) array;
1365    }
1366
1367    void getDirPropsMemory(int len)
1368    {
1369        getDirPropsMemory(mayAllocateText, len);
1370    }
1371
1372    private void getLevelsMemory(boolean mayAllocate, int len)
1373    {
1374        Object array = getMemory("Levels", levelsMemory, Byte.TYPE, mayAllocate, len);
1375        levelsMemory = (byte[]) array;
1376    }
1377
1378    void getLevelsMemory(int len)
1379    {
1380        getLevelsMemory(mayAllocateText, len);
1381    }
1382
1383    private void getRunsMemory(boolean mayAllocate, int len)
1384    {
1385        Object array = getMemory("Runs", runsMemory, BidiRun.class, mayAllocate, len);
1386        runsMemory = (BidiRun[]) array;
1387    }
1388
1389    void getRunsMemory(int len)
1390    {
1391        getRunsMemory(mayAllocateRuns, len);
1392    }
1393
1394    /* additional methods used by constructor - always allow allocation */
1395    private void getInitialDirPropsMemory(int len)
1396    {
1397        getDirPropsMemory(true, len);
1398    }
1399
1400    private void getInitialLevelsMemory(int len)
1401    {
1402        getLevelsMemory(true, len);
1403    }
1404
1405    private void getInitialRunsMemory(int len)
1406    {
1407        getRunsMemory(true, len);
1408    }
1409
1410    /**
1411     * Modify the operation of the Bidi algorithm such that it
1412     * approximates an "inverse Bidi" algorithm. This method
1413     * must be called before <code>setPara()</code>.
1414     *
1415     * <p>The normal operation of the Bidi algorithm as described
1416     * in the Unicode Technical Report is to take text stored in logical
1417     * (keyboard, typing) order and to determine the reordering of it for visual
1418     * rendering.
1419     * Some legacy systems store text in visual order, and for operations
1420     * with standard, Unicode-based algorithms, the text needs to be transformed
1421     * to logical order. This is effectively the inverse algorithm of the
1422     * described Bidi algorithm. Note that there is no standard algorithm for
1423     * this "inverse Bidi" and that the current implementation provides only an
1424     * approximation of "inverse Bidi".</p>
1425     *
1426     * <p>With <code>isInversed</code> set to <code>true</code>,
1427     * this method changes the behavior of some of the subsequent methods
1428     * in a way that they can be used for the inverse Bidi algorithm.
1429     * Specifically, runs of text with numeric characters will be treated in a
1430     * special way and may need to be surrounded with LRM characters when they are
1431     * written in reordered sequence.</p>
1432     *
1433     * <p>Output runs should be retrieved using <code>getVisualRun()</code>.
1434     * Since the actual input for "inverse Bidi" is visually ordered text and
1435     * <code>getVisualRun()</code> gets the reordered runs, these are actually
1436     * the runs of the logically ordered output.</p>
1437     *
1438     * <p>Calling this method with argument <code>isInverse</code> set to
1439     * <code>true</code> is equivalent to calling <code>setReorderingMode</code>
1440     * with argument <code>reorderingMode</code>
1441     * set to <code>REORDER_INVERSE_NUMBERS_AS_L</code>.<br>
1442     * Calling this method with argument <code>isInverse</code> set to
1443     * <code>false</code> is equivalent to calling <code>setReorderingMode</code>
1444     * with argument <code>reorderingMode</code>
1445     * set to <code>REORDER_DEFAULT</code>.
1446     *
1447     * @param isInverse specifies "forward" or "inverse" Bidi operation.
1448     *
1449     * @see #setPara
1450     * @see #writeReordered
1451     * @see #setReorderingMode
1452     * @see #REORDER_INVERSE_NUMBERS_AS_L
1453     * @see #REORDER_DEFAULT
1454     * @stable ICU 3.8
1455     */
1456    public void setInverse(boolean isInverse) {
1457        this.isInverse = (isInverse);
1458        this.reorderingMode = isInverse ? REORDER_INVERSE_NUMBERS_AS_L
1459                : REORDER_DEFAULT;
1460    }
1461
1462    /**
1463     * Is this <code>Bidi</code> object set to perform the inverse Bidi
1464     * algorithm?
1465     * <p>Note: calling this method after setting the reordering mode with
1466     * <code>setReorderingMode</code> will return <code>true</code> if the
1467     * reordering mode was set to
1468     * <code>REORDER_INVERSE_NUMBERS_AS_L</code>, <code>false</code>
1469     * for all other values.</p>
1470     *
1471     * @return <code>true</code> if the <code>Bidi</code> object is set to
1472     * perform the inverse Bidi algorithm by handling numbers as L.
1473     *
1474     * @see #setInverse
1475     * @see #setReorderingMode
1476     * @see #REORDER_INVERSE_NUMBERS_AS_L
1477     * @stable ICU 3.8
1478     */
1479    public boolean isInverse() {
1480        return isInverse;
1481    }
1482
1483    /**
1484     * Modify the operation of the Bidi algorithm such that it implements some
1485     * variant to the basic Bidi algorithm or approximates an "inverse Bidi"
1486     * algorithm, depending on different values of the "reordering mode".
1487     * This method must be called before <code>setPara()</code>, and stays in
1488     * effect until called again with a different argument.
1489     *
1490     * <p>The normal operation of the Bidi algorithm as described in the Unicode
1491     * Standard Annex #9 is to take text stored in logical (keyboard, typing)
1492     * order and to determine how to reorder it for visual rendering.</p>
1493     *
1494     * <p>With the reordering mode set to a value other than
1495     * <code>REORDER_DEFAULT</code>, this method changes the behavior of some of
1496     * the subsequent methods in a way such that they implement an inverse Bidi
1497     * algorithm or some other algorithm variants.</p>
1498     *
1499     * <p>Some legacy systems store text in visual order, and for operations
1500     * with standard, Unicode-based algorithms, the text needs to be transformed
1501     * into logical order. This is effectively the inverse algorithm of the
1502     * described Bidi algorithm. Note that there is no standard algorithm for
1503     * this "inverse Bidi", so a number of variants are implemented here.</p>
1504     *
1505     * <p>In other cases, it may be desirable to emulate some variant of the
1506     * Logical to Visual algorithm (e.g. one used in MS Windows), or perform a
1507     * Logical to Logical transformation.</p>
1508     *
1509     * <ul>
1510     * <li>When the Reordering Mode is set to
1511     * <code>REORDER_DEFAULT</code>,
1512     * the standard Bidi Logical to Visual algorithm is applied.</li>
1513     *
1514     * <li>When the reordering mode is set to
1515     * <code>REORDER_NUMBERS_SPECIAL</code>,
1516     * the algorithm used to perform Bidi transformations when calling
1517     * <code>setPara</code> should approximate the algorithm used in Microsoft
1518     * Windows XP rather than strictly conform to the Unicode Bidi algorithm.
1519     * <br>
1520     * The differences between the basic algorithm and the algorithm addressed
1521     * by this option are as follows:
1522     * <ul>
1523     *   <li>Within text at an even embedding level, the sequence "123AB"
1524     *   (where AB represent R or AL letters) is transformed to "123BA" by the
1525     *   Unicode algorithm and to "BA123" by the Windows algorithm.</li>
1526     *
1527     *   <li>Arabic-Indic numbers (AN) are handled by the Windows algorithm just
1528     *   like regular numbers (EN).</li>
1529     * </ul></li>
1530     *
1531     * <li>When the reordering mode is set to
1532     * <code>REORDER_GROUP_NUMBERS_WITH_R</code>,
1533     * numbers located between LTR text and RTL text are associated with the RTL
1534     * text. For instance, an LTR paragraph with content "abc 123 DEF" (where
1535     * upper case letters represent RTL characters) will be transformed to
1536     * "abc FED 123" (and not "abc 123 FED"), "DEF 123 abc" will be transformed
1537     * to "123 FED abc" and "123 FED abc" will be transformed to "DEF 123 abc".
1538     * This makes the algorithm reversible and makes it useful when round trip
1539     * (from visual to logical and back to visual) must be achieved without
1540     * adding LRM characters. However, this is a variation from the standard
1541     * Unicode Bidi algorithm.<br>
1542     * The source text should not contain Bidi control characters other than LRM
1543     * or RLM.</li>
1544     *
1545     * <li>When the reordering mode is set to
1546     * <code>REORDER_RUNS_ONLY</code>,
1547     * a "Logical to Logical" transformation must be performed:
1548     * <ul>
1549     * <li>If the default text level of the source text (argument
1550     * <code>paraLevel</code> in <code>setPara</code>) is even, the source text
1551     * will be handled as LTR logical text and will be transformed to the RTL
1552     * logical text which has the same LTR visual display.</li>
1553     * <li>If the default level of the source text is odd, the source text
1554     * will be handled as RTL logical text and will be transformed to the
1555     * LTR logical text which has the same LTR visual display.</li>
1556     * </ul>
1557     * This mode may be needed when logical text which is basically Arabic or
1558     * Hebrew, with possible included numbers or phrases in English, has to be
1559     * displayed as if it had an even embedding level (this can happen if the
1560     * displaying application treats all text as if it was basically LTR).
1561     * <br>
1562     * This mode may also be needed in the reverse case, when logical text which
1563     * is basically English, with possible included phrases in Arabic or Hebrew,
1564     * has to be displayed as if it had an odd embedding level.
1565     * <br>
1566     * Both cases could be handled by adding LRE or RLE at the head of the
1567     * text, if the display subsystem supports these formatting controls. If it
1568     * does not, the problem may be handled by transforming the source text in
1569     * this mode before displaying it, so that it will be displayed properly.
1570     * <br>
1571     * The source text should not contain Bidi control characters other than LRM
1572     * or RLM.</li>
1573     *
1574     * <li>When the reordering mode is set to
1575     * <code>REORDER_INVERSE_NUMBERS_AS_L</code>, an "inverse Bidi"
1576     * algorithm is applied.
1577     * Runs of text with numeric characters will be treated like LTR letters and
1578     * may need to be surrounded with LRM characters when they are written in
1579     * reordered sequence (the option <code>INSERT_LRM_FOR_NUMERIC</code> can
1580     * be used with method <code>writeReordered</code> to this end. This mode
1581     * is equivalent to calling <code>setInverse()</code> with
1582     * argument <code>isInverse</code> set to <code>true</code>.</li>
1583     *
1584     * <li>When the reordering mode is set to
1585     * <code>REORDER_INVERSE_LIKE_DIRECT</code>, the "direct" Logical to
1586     * Visual Bidi algorithm is used as an approximation of an "inverse Bidi"
1587     * algorithm. This mode is similar to mode
1588     * <code>REORDER_INVERSE_NUMBERS_AS_L</code> but is closer to the
1589     * regular Bidi algorithm.
1590     * <br>
1591     * For example, an LTR paragraph with the content "FED 123 456 CBA" (where
1592     * upper case represents RTL characters) will be transformed to
1593     * "ABC 456 123 DEF", as opposed to "DEF 123 456 ABC"
1594     * with mode <code>REORDER_INVERSE_NUMBERS_AS_L</code>.<br>
1595     * When used in conjunction with option
1596     * <code>OPTION_INSERT_MARKS</code>, this mode generally
1597     * adds Bidi marks to the output significantly more sparingly than mode
1598     * <code>REORDER_INVERSE_NUMBERS_AS_L</code>.<br> with option
1599     * <code>INSERT_LRM_FOR_NUMERIC</code> in calls to
1600     * <code>writeReordered</code>.</li>
1601     *
1602     * <li>When the reordering mode is set to
1603     * <code>REORDER_INVERSE_FOR_NUMBERS_SPECIAL</code>, the Logical to Visual
1604     * Bidi algorithm used in Windows XP is used as an approximation of an "inverse
1605     * Bidi" algorithm.
1606     * <br>
1607     * For example, an LTR paragraph with the content "abc FED123" (where
1608     * upper case represents RTL characters) will be transformed to
1609     * "abc 123DEF.</li>
1610     * </ul>
1611     *
1612     * <p>In all the reordering modes specifying an "inverse Bidi" algorithm
1613     * (i.e. those with a name starting with <code>REORDER_INVERSE</code>),
1614     * output runs should be retrieved using <code>getVisualRun()</code>, and
1615     * the output text with <code>writeReordered()</code>. The caller should
1616     * keep in mind that in "inverse Bidi" modes the input is actually visually
1617     * ordered text and reordered output returned by <code>getVisualRun()</code>
1618     * or <code>writeReordered()</code> are actually runs or character string
1619     * of logically ordered output.<br>
1620     * For all the "inverse Bidi" modes, the source text should not contain
1621     * Bidi control characters other than LRM or RLM.</p>
1622     *
1623     * <p>Note that option <code>OUTPUT_REVERSE</code> of
1624     * <code>writeReordered</code> has no useful meaning and should not be used
1625     * in conjunction with any value of the reordering mode specifying "inverse
1626     * Bidi" or with value <code>REORDER_RUNS_ONLY</code>.
1627     *
1628     * @param reorderingMode specifies the required variant of the Bidi
1629     *                       algorithm.
1630     *
1631     * @see #setInverse
1632     * @see #setPara
1633     * @see #writeReordered
1634     * @see #INSERT_LRM_FOR_NUMERIC
1635     * @see #OUTPUT_REVERSE
1636     * @see #REORDER_DEFAULT
1637     * @see #REORDER_NUMBERS_SPECIAL
1638     * @see #REORDER_GROUP_NUMBERS_WITH_R
1639     * @see #REORDER_RUNS_ONLY
1640     * @see #REORDER_INVERSE_NUMBERS_AS_L
1641     * @see #REORDER_INVERSE_LIKE_DIRECT
1642     * @see #REORDER_INVERSE_FOR_NUMBERS_SPECIAL
1643     * @stable ICU 3.8
1644     */
1645    public void setReorderingMode(int reorderingMode) {
1646        if ((reorderingMode < REORDER_DEFAULT) ||
1647            (reorderingMode >= REORDER_COUNT))
1648            return;                     /* don't accept a wrong value */
1649        this.reorderingMode = reorderingMode;
1650        this.isInverse =
1651            reorderingMode == REORDER_INVERSE_NUMBERS_AS_L;
1652    }
1653
1654    /**
1655     * What is the requested reordering mode for a given Bidi object?
1656     *
1657     * @return the current reordering mode of the Bidi object
1658     *
1659     * @see #setReorderingMode
1660     * @stable ICU 3.8
1661     */
1662    public int getReorderingMode() {
1663        return this.reorderingMode;
1664    }
1665
1666    /**
1667     * Specify which of the reordering options should be applied during Bidi
1668     * transformations.
1669     *
1670     * @param options A combination of zero or more of the following
1671     * reordering options:
1672     * <code>OPTION_DEFAULT</code>, <code>OPTION_INSERT_MARKS</code>,
1673     * <code>OPTION_REMOVE_CONTROLS</code>, <code>OPTION_STREAMING</code>.
1674     *
1675     * @see #getReorderingOptions
1676     * @see #OPTION_DEFAULT
1677     * @see #OPTION_INSERT_MARKS
1678     * @see #OPTION_REMOVE_CONTROLS
1679     * @see #OPTION_STREAMING
1680     * @stable ICU 3.8
1681     */
1682    public void setReorderingOptions(int options) {
1683        if ((options & OPTION_REMOVE_CONTROLS) != 0) {
1684            this.reorderingOptions = options & ~OPTION_INSERT_MARKS;
1685        } else {
1686            this.reorderingOptions = options;
1687        }
1688    }
1689
1690    /**
1691     * What are the reordering options applied to a given Bidi object?
1692     *
1693     * @return the current reordering options of the Bidi object
1694     *
1695     * @see #setReorderingOptions
1696     * @stable ICU 3.8
1697     */
1698    public int getReorderingOptions() {
1699        return this.reorderingOptions;
1700    }
1701
1702    /**
1703     * Get the base direction of the text provided according to the Unicode
1704     * Bidirectional Algorithm. The base direction is derived from the first
1705     * character in the string with bidirectional character type L, R, or AL.
1706     * If the first such character has type L, LTR is returned. If the first
1707     * such character has type R or AL, RTL is returned. If the string does
1708     * not contain any character of these types, then NEUTRAL is returned.
1709     * This is a lightweight function for use when only the base direction is
1710     * needed and no further bidi processing of the text is needed.
1711     * @param paragraph the text whose paragraph level direction is needed.
1712     * @return LTR, RTL, NEUTRAL
1713     * @see #LTR
1714     * @see #RTL
1715     * @see #NEUTRAL
1716     * @stable ICU 4.6
1717     */
1718    public static byte getBaseDirection(CharSequence paragraph) {
1719        if (paragraph == null || paragraph.length() == 0) {
1720            return NEUTRAL;
1721        }
1722
1723        int length = paragraph.length();
1724        int c;// codepoint
1725        byte direction;
1726
1727        for (int i = 0; i < length; ) {
1728            // U16_NEXT(paragraph, i, length, c) for C++
1729            c = UCharacter.codePointAt(paragraph, i);
1730            direction = UCharacter.getDirectionality(c);
1731            if (direction == UCharacterDirection.LEFT_TO_RIGHT) {
1732                return LTR;
1733            } else if (direction == UCharacterDirection.RIGHT_TO_LEFT
1734                || direction == UCharacterDirection.RIGHT_TO_LEFT_ARABIC) {
1735                return RTL;
1736            }
1737
1738            i = UCharacter.offsetByCodePoints(paragraph, i, 1);// set i to the head index of next codepoint
1739        }
1740        return NEUTRAL;
1741    }
1742
1743/* perform (P2)..(P3) ------------------------------------------------------- */
1744
1745    /**
1746     * Returns the directionality of the first strong character
1747     * after the last B in prologue, if any.
1748     * Requires prologue!=null.
1749     */
1750    private byte firstL_R_AL() {
1751        byte result = ON;
1752        for (int i = 0; i < prologue.length(); ) {
1753            int uchar = prologue.codePointAt(i);
1754            i += Character.charCount(uchar);
1755            byte dirProp = (byte)getCustomizedClass(uchar);
1756            if (result == ON) {
1757                if (dirProp == L || dirProp == R || dirProp == AL) {
1758                    result = dirProp;
1759                }
1760            } else {
1761                if (dirProp == B) {
1762                    result = ON;
1763                }
1764            }
1765        }
1766        return result;
1767    }
1768
1769    /*
1770     * Check that there are enough entries in the arrays paras_limit and paras_level
1771     */
1772    private void checkParaCount() {
1773        int[] saveLimits;
1774        byte[] saveLevels;
1775        int count = paraCount;
1776        if (count <= paras_level.length)
1777            return;
1778        int oldLength = paras_level.length;
1779        saveLimits = paras_limit;
1780        saveLevels = paras_level;
1781        try {
1782            paras_limit = new int[count * 2];
1783            paras_level = new byte[count * 2];
1784        } catch (Exception e) {
1785            throw new OutOfMemoryError("Failed to allocate memory for paras");
1786        }
1787        System.arraycopy(saveLimits, 0, paras_limit, 0, oldLength);
1788        System.arraycopy(saveLevels, 0, paras_level, 0, oldLength);
1789    }
1790
1791    /*
1792     * Get the directional properties for the text, calculate the flags bit-set, and
1793     * determine the paragraph level if necessary (in paras_level[i]).
1794     * FSI initiators are also resolved and their dirProp replaced with LRI or RLI.
1795     * When encountering an FSI, it is initially replaced with an LRI, which is the
1796     * default. Only if a strong R or AL is found within its scope will the LRI be
1797     * replaced by an RLI.
1798     */
1799    static final int NOT_SEEKING_STRONG = 0;        /* 0: not contextual paraLevel, not after FSI */
1800    static final int SEEKING_STRONG_FOR_PARA = 1;   /* 1: looking for first strong char in para */
1801    static final int SEEKING_STRONG_FOR_FSI = 2;    /* 2: looking for first strong after FSI */
1802    static final int LOOKING_FOR_PDI = 3;           /* 3: found strong after FSI, looking for PDI */
1803
1804    private void getDirProps()
1805    {
1806        int i = 0, i0, i1;
1807        flags = 0;          /* collect all directionalities in the text */
1808        int uchar;
1809        byte dirProp;
1810        byte defaultParaLevel = 0;   /* initialize to avoid compiler warnings */
1811        boolean isDefaultLevel = IsDefaultLevel(paraLevel);
1812        /* for inverse Bidi, the default para level is set to RTL if there is a
1813           strong R or AL character at either end of the text                */
1814        boolean isDefaultLevelInverse=isDefaultLevel &&
1815                (reorderingMode == REORDER_INVERSE_LIKE_DIRECT ||
1816                 reorderingMode == REORDER_INVERSE_FOR_NUMBERS_SPECIAL);
1817        lastArabicPos = -1;
1818        int controlCount = 0;
1819        boolean removeBidiControls = (reorderingOptions & OPTION_REMOVE_CONTROLS) != 0;
1820
1821        byte state;
1822        byte lastStrong = ON;           /* for default level & inverse Bidi */
1823    /* The following stacks are used to manage isolate sequences. Those
1824       sequences may be nested, but obviously never more deeply than the
1825       maximum explicit embedding level.
1826       lastStack is the index of the last used entry in the stack. A value of -1
1827       means that there is no open isolate sequence.
1828       lastStack is reset to -1 on paragraph boundaries. */
1829    /* The following stack contains the position of the initiator of
1830       each open isolate sequence */
1831        int[] isolateStartStack= new int[MAX_EXPLICIT_LEVEL+1];
1832    /* The following stack contains the last known state before
1833       encountering the initiator of an isolate sequence */
1834        byte[] previousStateStack = new byte[MAX_EXPLICIT_LEVEL+1];
1835        int  stackLast=-1;
1836
1837        if ((reorderingOptions & OPTION_STREAMING) != 0)
1838            length = 0;
1839        defaultParaLevel = (byte)(paraLevel & 1);
1840
1841        if (isDefaultLevel) {
1842            paras_level[0] = defaultParaLevel;
1843            lastStrong = defaultParaLevel;
1844            if (prologue != null &&                        /* there is a prologue */
1845                (dirProp = firstL_R_AL()) != ON) {     /* with a strong character */
1846                if (dirProp == L)
1847                    paras_level[0] = 0;             /* set the default para level */
1848                else
1849                    paras_level[0] = 1;             /* set the default para level */
1850                state = NOT_SEEKING_STRONG;
1851            } else {
1852                state = SEEKING_STRONG_FOR_PARA;
1853            }
1854        } else {
1855            paras_level[0] = paraLevel;
1856            state = NOT_SEEKING_STRONG;
1857        }
1858        /* count paragraphs and determine the paragraph level (P2..P3) */
1859        /*
1860         * see comment on constant fields:
1861         * the LEVEL_DEFAULT_XXX values are designed so that
1862         * their low-order bit alone yields the intended default
1863         */
1864
1865        for (i = 0; i < originalLength; /* i is incremented in the loop */) {
1866            i0 = i;                     /* index of first code unit */
1867            uchar = UTF16.charAt(text, 0, originalLength, i);
1868            i += UTF16.getCharCount(uchar);
1869            i1 = i - 1; /* index of last code unit, gets the directional property */
1870
1871            dirProp = (byte)getCustomizedClass(uchar);
1872            flags |= DirPropFlag(dirProp);
1873            dirProps[i1] = dirProp;
1874            if (i1 > i0) {     /* set previous code units' properties to BN */
1875                flags |= DirPropFlag(BN);
1876                do {
1877                    dirProps[--i1] = BN;
1878                } while (i1 > i0);
1879            }
1880            if (removeBidiControls && IsBidiControlChar(uchar)) {
1881                controlCount++;
1882            }
1883            if (dirProp == L) {
1884                if (state == SEEKING_STRONG_FOR_PARA) {
1885                    paras_level[paraCount - 1] = 0;
1886                    state = NOT_SEEKING_STRONG;
1887                }
1888                else if (state == SEEKING_STRONG_FOR_FSI) {
1889                    if (stackLast <= MAX_EXPLICIT_LEVEL) {
1890                        /* no need for next statement, already set by default */
1891                        /* dirProps[isolateStartStack[stackLast]] = LRI; */
1892                        flags |= DirPropFlag(LRI);
1893                    }
1894                    state = LOOKING_FOR_PDI;
1895                }
1896                lastStrong = L;
1897                continue;
1898            }
1899            if (dirProp == R || dirProp == AL) {
1900                if (state == SEEKING_STRONG_FOR_PARA) {
1901                    paras_level[paraCount - 1] = 1;
1902                    state = NOT_SEEKING_STRONG;
1903                }
1904                else if (state == SEEKING_STRONG_FOR_FSI) {
1905                    if (stackLast <= MAX_EXPLICIT_LEVEL) {
1906                        dirProps[isolateStartStack[stackLast]] = RLI;
1907                        flags |= DirPropFlag(RLI);
1908                    }
1909                    state = LOOKING_FOR_PDI;
1910                }
1911                lastStrong = R;
1912                if (dirProp == AL)
1913                    lastArabicPos = i - 1;
1914                continue;
1915            }
1916            if (dirProp >= FSI && dirProp <= RLI) { /* FSI, LRI or RLI */
1917                stackLast++;
1918                if (stackLast <= MAX_EXPLICIT_LEVEL) {
1919                    isolateStartStack[stackLast] = i - 1;
1920                    previousStateStack[stackLast] = state;
1921                }
1922                if (dirProp == FSI) {
1923                    dirProps[i-1] = LRI;    /* default if no strong char */
1924                    state = SEEKING_STRONG_FOR_FSI;
1925                }
1926                else
1927                    state = LOOKING_FOR_PDI;
1928                continue;
1929            }
1930            if (dirProp == PDI) {
1931                if (state == SEEKING_STRONG_FOR_FSI) {
1932                    if (stackLast <= MAX_EXPLICIT_LEVEL) {
1933                        /* no need for next statement, already set by default */
1934                        /* dirProps[isolateStartStack[stackLast]] = LRI; */
1935                        flags |= DirPropFlag(LRI);
1936                    }
1937                }
1938                if (stackLast >= 0) {
1939                    if (stackLast <= MAX_EXPLICIT_LEVEL)
1940                        state = previousStateStack[stackLast];
1941                    stackLast--;
1942                }
1943                continue;
1944            }
1945            if (dirProp == B) {
1946                if (i < originalLength && uchar == CR && text[i] == LF) /* do nothing on the CR */
1947                    continue;
1948                paras_limit[paraCount - 1] = i;
1949                if (isDefaultLevelInverse && lastStrong == R)
1950                    paras_level[paraCount - 1] = 1;
1951                if ((reorderingOptions & OPTION_STREAMING) != 0) {
1952                /* When streaming, we only process whole paragraphs
1953                   thus some updates are only done on paragraph boundaries */
1954                   length = i;          /* i is index to next character */
1955                   this.controlCount = controlCount;
1956                }
1957                if (i < originalLength) {       /* B not last char in text */
1958                    paraCount++;
1959                    checkParaCount();   /* check that there is enough memory for a new para entry */
1960                    if (isDefaultLevel) {
1961                        paras_level[paraCount - 1] = defaultParaLevel;
1962                        state = SEEKING_STRONG_FOR_PARA;
1963                        lastStrong = defaultParaLevel;
1964                    } else {
1965                        paras_level[paraCount - 1] = paraLevel;
1966                        state = NOT_SEEKING_STRONG;
1967                    }
1968                    stackLast = -1;
1969                }
1970                continue;
1971            }
1972        }
1973        /* +Ignore still open isolate sequences with overflow */
1974        if (stackLast > MAX_EXPLICIT_LEVEL) {
1975            stackLast = MAX_EXPLICIT_LEVEL;
1976            state=SEEKING_STRONG_FOR_FSI;   /* to be on the safe side */
1977        }
1978        /* Resolve direction of still unresolved open FSI sequences */
1979        while (stackLast >= 0) {
1980            if (state == SEEKING_STRONG_FOR_FSI) {
1981                /* no need for next statement, already set by default */
1982                /* dirProps[isolateStartStack[stackLast]] = LRI; */
1983                flags |= DirPropFlag(LRI);
1984                break;
1985            }
1986            state = previousStateStack[stackLast];
1987            stackLast--;
1988        }
1989        /* When streaming, ignore text after the last paragraph separator */
1990        if ((reorderingOptions & OPTION_STREAMING) != 0) {
1991            if (length < originalLength)
1992                paraCount--;
1993        } else {
1994            paras_limit[paraCount - 1] = originalLength;
1995            this.controlCount = controlCount;
1996        }
1997        /* For inverse bidi, default para direction is RTL if there is
1998           a strong R or AL at either end of the paragraph */
1999        if (isDefaultLevelInverse && lastStrong == R) {
2000            paras_level[paraCount - 1] = 1;
2001        }
2002        if (isDefaultLevel) {
2003            paraLevel = paras_level[0];
2004        }
2005        /* The following is needed to resolve the text direction for default level
2006           paragraphs containing no strong character */
2007        for (i = 0; i < paraCount; i++)
2008            flags |= DirPropFlagLR(paras_level[i]);
2009
2010        if (orderParagraphsLTR && (flags & DirPropFlag(B)) != 0) {
2011            flags |= DirPropFlag(L);
2012        }
2013    }
2014
2015    /* determine the paragraph level at position index */
2016    byte GetParaLevelAt(int pindex)
2017    {
2018        if (defaultParaLevel == 0 || pindex < paras_limit[0])
2019            return paraLevel;
2020        int i;
2021        for (i = 1; i < paraCount; i++)
2022            if (pindex < paras_limit[i])
2023                break;
2024        if (i >= paraCount)
2025            i = paraCount - 1;
2026        return paras_level[i];
2027    }
2028
2029    /* Functions for handling paired brackets ----------------------------------- */
2030
2031    /* In the isoRuns array, the first entry is used for text outside of any
2032       isolate sequence.  Higher entries are used for each more deeply nested
2033       isolate sequence. isoRunLast is the index of the last used entry.  The
2034       openings array is used to note the data of opening brackets not yet
2035       matched by a closing bracket, or matched but still susceptible to change
2036       level.
2037       Each isoRun entry contains the index of the first and
2038       one-after-last openings entries for pending opening brackets it
2039       contains.  The next openings entry to use is the one-after-last of the
2040       most deeply nested isoRun entry.
2041       isoRun entries also contain their current embedding level and the last
2042       encountered strong character, since these will be needed to resolve
2043       the level of paired brackets.  */
2044
2045    private void bracketInit(BracketData bd) {
2046        bd.isoRunLast = 0;
2047        bd.isoRuns[0] = new IsoRun();
2048        bd.isoRuns[0].start = 0;
2049        bd.isoRuns[0].limit = 0;
2050        bd.isoRuns[0].level = GetParaLevelAt(0);
2051        bd.isoRuns[0].lastStrong = bd.isoRuns[0].lastBase = bd.isoRuns[0].contextDir = (byte)(GetParaLevelAt(0) & 1);
2052        bd.isoRuns[0].contextPos = 0;
2053        bd.openings = new Opening[SIMPLE_OPENINGS_COUNT];
2054        bd.isNumbersSpecial = reorderingMode == REORDER_NUMBERS_SPECIAL ||
2055                              reorderingMode == REORDER_INVERSE_FOR_NUMBERS_SPECIAL;
2056    }
2057
2058    /* paragraph boundary */
2059    private void bracketProcessB(BracketData bd, byte level) {
2060        bd.isoRunLast = 0;
2061        bd.isoRuns[0].limit = 0;
2062        bd.isoRuns[0].level = level;
2063        bd.isoRuns[0].lastStrong = bd.isoRuns[0].lastBase = bd.isoRuns[0].contextDir = (byte)(level & 1);
2064        bd.isoRuns[0].contextPos = 0;
2065    }
2066
2067    /* LRE, LRO, RLE, RLO, PDF */
2068    private void bracketProcessBoundary(BracketData bd, int lastCcPos,
2069                                        byte contextLevel, byte embeddingLevel) {
2070        IsoRun pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2071        if ((DirPropFlag(dirProps[lastCcPos]) & MASK_ISO) != 0) /* after an isolate */
2072            return;
2073        if (NoOverride(embeddingLevel) > NoOverride(contextLevel))  /* not a PDF */
2074            contextLevel = embeddingLevel;
2075        pLastIsoRun.limit = pLastIsoRun.start;
2076        pLastIsoRun.level = embeddingLevel;
2077        pLastIsoRun.lastStrong = pLastIsoRun.lastBase = pLastIsoRun.contextDir = (byte)(contextLevel & 1);
2078        pLastIsoRun.contextPos = lastCcPos;
2079    }
2080
2081    /* LRI or RLI */
2082    private void bracketProcessLRI_RLI(BracketData bd, byte level) {
2083        IsoRun pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2084        short lastLimit;
2085        pLastIsoRun.lastBase = ON;
2086        lastLimit = pLastIsoRun.limit;
2087        bd.isoRunLast++;
2088        pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2089        if (pLastIsoRun == null)
2090            pLastIsoRun = bd.isoRuns[bd.isoRunLast] = new IsoRun();
2091        pLastIsoRun.start = pLastIsoRun.limit = lastLimit;
2092        pLastIsoRun.level = level;
2093        pLastIsoRun.lastStrong = pLastIsoRun.lastBase = pLastIsoRun.contextDir = (byte)(level & 1);
2094        pLastIsoRun.contextPos = 0;
2095    }
2096
2097    /* PDI */
2098    private void bracketProcessPDI(BracketData bd) {
2099        IsoRun pLastIsoRun;
2100        bd.isoRunLast--;
2101        pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2102        pLastIsoRun.lastBase = ON;
2103    }
2104
2105    /* newly found opening bracket: create an openings entry */
2106    private void bracketAddOpening(BracketData bd, char match, int position) {
2107        IsoRun pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2108        Opening pOpening;
2109        if (pLastIsoRun.limit >= bd.openings.length) {  /* no available new entry */
2110            Opening[] saveOpenings = bd.openings;
2111            int count;
2112            try {
2113                count = bd.openings.length;
2114                bd.openings = new Opening[count * 2];
2115            } catch (Exception e) {
2116                throw new OutOfMemoryError("Failed to allocate memory for openings");
2117            }
2118            System.arraycopy(saveOpenings, 0, bd.openings, 0, count);
2119        }
2120        pOpening = bd.openings[pLastIsoRun.limit];
2121        if (pOpening == null)
2122            pOpening = bd.openings[pLastIsoRun.limit]= new Opening();
2123        pOpening.position = position;
2124        pOpening.match = match;
2125        pOpening.contextDir = pLastIsoRun.contextDir;
2126        pOpening.contextPos = pLastIsoRun.contextPos;
2127        pOpening.flags = 0;
2128        pLastIsoRun.limit++;
2129    }
2130
2131    /* change N0c1 to N0c2 when a preceding bracket is assigned the embedding level */
2132    private void fixN0c(BracketData bd, int openingIndex, int newPropPosition, byte newProp) {
2133        /* This function calls itself recursively */
2134        IsoRun pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2135        Opening qOpening;
2136        int k, openingPosition, closingPosition;
2137        for (k = openingIndex+1; k < pLastIsoRun.limit; k++) {
2138            qOpening = bd.openings[k];
2139            if (qOpening.match >= 0)    /* not an N0c match */
2140                continue;
2141            if (newPropPosition < qOpening.contextPos)
2142                break;
2143            if (newPropPosition >= qOpening.position)
2144                continue;
2145            if (newProp == qOpening.contextDir)
2146                break;
2147            openingPosition = qOpening.position;
2148            dirProps[openingPosition] = newProp;
2149            closingPosition = -(qOpening.match);
2150            dirProps[closingPosition] = newProp;
2151            qOpening.match = 0;                                 /* prevent further changes */
2152            fixN0c(bd, k, openingPosition, newProp);
2153            fixN0c(bd, k, closingPosition, newProp);
2154        }
2155    }
2156
2157    /* process closing bracket; return L or R if N0b or N0c, ON if N0d */
2158    private byte bracketProcessClosing(BracketData bd, int openIdx, int position) {
2159        IsoRun pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2160        Opening pOpening, qOpening;
2161        byte direction;
2162        boolean stable;
2163        byte newProp;
2164        pOpening = bd.openings[openIdx];
2165        direction = (byte)(pLastIsoRun.level & 1);
2166        stable = true;          /* assume stable until proved otherwise */
2167
2168        /* The stable flag is set when brackets are paired and their
2169           level is resolved and cannot be changed by what will be
2170           found later in the source string.
2171           An unstable match can occur only when applying N0c, where
2172           the resolved level depends on the preceding context, and
2173           this context may be affected by text occurring later.
2174           Example: RTL paragraph containing:  abc[(latin) HEBREW]
2175           When the closing parenthesis is encountered, it appears
2176           that N0c1 must be applied since 'abc' sets an opposite
2177           direction context and both parentheses receive level 2.
2178           However, when the closing square bracket is processed,
2179           N0b applies because of 'HEBREW' being included within the
2180           brackets, thus the square brackets are treated like R and
2181           receive level 1. However, this changes the preceding
2182           context of the opening parenthesis, and it now appears
2183           that N0c2 must be applied to the parentheses rather than
2184           N0c1. */
2185
2186            if ((direction == 0 && (pOpening.flags & FOUND_L) > 0) ||
2187                (direction == 1 && (pOpening.flags & FOUND_R) > 0)) {   /* N0b */
2188                newProp = direction;
2189            }
2190            else if ((pOpening.flags & (FOUND_L | FOUND_R)) != 0) {     /* N0c */
2191                    /* it is stable if there is no preceding text or in
2192                       conditions too complicated and not worth checking */
2193                    stable = (openIdx == pLastIsoRun.start);
2194                if (direction != pOpening.contextDir)
2195                    newProp = pOpening.contextDir;                      /* N0c1 */
2196                else
2197                    newProp = direction;                                /* N0c2 */
2198            } else {
2199            /* forget this and any brackets nested within this pair */
2200            pLastIsoRun.limit = (short)openIdx;
2201            return ON;                                                  /* N0d */
2202        }
2203        dirProps[pOpening.position] = newProp;
2204        dirProps[position] = newProp;
2205        /* Update nested N0c pairs that may be affected */
2206        fixN0c(bd, openIdx, pOpening.position, newProp);
2207        if (stable) {
2208            pLastIsoRun.limit = (short)openIdx; /* forget any brackets nested within this pair */
2209            /* remove lower located synonyms if any */
2210            while (pLastIsoRun.limit > pLastIsoRun.start &&
2211                   bd.openings[pLastIsoRun.limit - 1].position == pOpening.position)
2212                pLastIsoRun.limit--;
2213        } else {
2214            int k;
2215            pOpening.match = -position;
2216            /* neutralize lower located synonyms if any */
2217            k = openIdx - 1;
2218            while (k >= pLastIsoRun.start &&
2219                   bd.openings[k].position == pOpening.position)
2220                bd.openings[k--].match = 0;
2221            /* neutralize any unmatched opening between the current pair;
2222               this will also neutralize higher located synonyms if any */
2223            for (k = openIdx + 1; k < pLastIsoRun.limit; k++) {
2224                qOpening =bd.openings[k];
2225                if (qOpening.position >= position)
2226                    break;
2227                if (qOpening.match > 0)
2228                    qOpening.match = 0;
2229            }
2230        }
2231        return newProp;
2232    }
2233
2234    /* handle strong characters, digits and candidates for closing brackets */
2235    private void bracketProcessChar(BracketData bd, int position) {
2236        IsoRun pLastIsoRun = bd.isoRuns[bd.isoRunLast];
2237        byte dirProp, newProp;
2238        byte level;
2239        dirProp = dirProps[position];
2240        if (dirProp == ON) {
2241            char c, match;
2242            int idx;
2243            /* First see if it is a matching closing bracket. Hopefully, this is
2244               more efficient than checking if it is a closing bracket at all */
2245            c = text[position];
2246            for (idx = pLastIsoRun.limit - 1; idx >= pLastIsoRun.start; idx--) {
2247                if (bd.openings[idx].match != c)
2248                    continue;
2249                /* We have a match */
2250                newProp = bracketProcessClosing(bd, idx, position);
2251                if(newProp == ON) {         /* N0d */
2252                    c = 0;          /* prevent handling as an opening */
2253                    break;
2254                }
2255                pLastIsoRun.lastBase = ON;
2256                pLastIsoRun.contextDir = newProp;
2257                pLastIsoRun.contextPos = position;
2258                level = levels[position];
2259                if ((level & LEVEL_OVERRIDE) != 0) {    /* X4, X5 */
2260                    short flag;
2261                    int i;
2262                    newProp = (byte)(level & 1);
2263                    pLastIsoRun.lastStrong = newProp;
2264                    flag = (short)DirPropFlag(newProp);
2265                    for (i = pLastIsoRun.start; i < idx; i++)
2266                        bd.openings[i].flags |= flag;
2267                    /* matching brackets are not overridden by LRO/RLO */
2268                    levels[position] &= ~LEVEL_OVERRIDE;
2269                }
2270                /* matching brackets are not overridden by LRO/RLO */
2271                levels[bd.openings[idx].position] &= ~LEVEL_OVERRIDE;
2272                return;
2273            }
2274            /* We get here only if the ON character is not a matching closing
2275               bracket or it is a case of N0d */
2276            /* Now see if it is an opening bracket */
2277            if (c != 0)
2278                match = (char)UCharacter.getBidiPairedBracket(c); /* get the matching char */
2279            else
2280                match = 0;
2281            if (match != c &&               /* has a matching char */
2282                UCharacter.getIntPropertyValue(c, UProperty.BIDI_PAIRED_BRACKET_TYPE) ==
2283                    /* opening bracket */         UCharacter.BidiPairedBracketType.OPEN) {
2284                /* special case: process synonyms
2285                   create an opening entry for each synonym */
2286                if (match == 0x232A) {      /* RIGHT-POINTING ANGLE BRACKET */
2287                    bracketAddOpening(bd, (char)0x3009, position);
2288                }
2289                else if (match == 0x3009) { /* RIGHT ANGLE BRACKET */
2290                    bracketAddOpening(bd, (char)0x232A, position);
2291                }
2292                bracketAddOpening(bd, match, position);
2293            }
2294        }
2295        level = levels[position];
2296        if ((level & LEVEL_OVERRIDE) != 0) {    /* X4, X5 */
2297            newProp = (byte)(level & 1);
2298            if (dirProp != S && dirProp != WS && dirProp != ON)
2299                dirProps[position] = newProp;
2300            pLastIsoRun.lastBase = newProp;
2301            pLastIsoRun.lastStrong = newProp;
2302            pLastIsoRun.contextDir = newProp;
2303            pLastIsoRun.contextPos = position;
2304        }
2305        else if (dirProp <= R || dirProp == AL) {
2306            newProp = DirFromStrong(dirProp);
2307            pLastIsoRun.lastBase = dirProp;
2308            pLastIsoRun.lastStrong = dirProp;
2309            pLastIsoRun.contextDir = newProp;
2310            pLastIsoRun.contextPos = position;
2311        }
2312        else if(dirProp == EN) {
2313            pLastIsoRun.lastBase = EN;
2314            if (pLastIsoRun.lastStrong == L) {
2315                newProp = L;                    /* W7 */
2316                if (!bd.isNumbersSpecial)
2317                    dirProps[position] = ENL;
2318                pLastIsoRun.contextDir = L;
2319                pLastIsoRun.contextPos = position;
2320            }
2321            else {
2322                newProp = R;                    /* N0 */
2323                if (pLastIsoRun.lastStrong == AL)
2324                    dirProps[position] = AN;    /* W2 */
2325                else
2326                    dirProps[position] = ENR;
2327                pLastIsoRun.contextDir = R;
2328                pLastIsoRun.contextPos = position;
2329            }
2330        }
2331        else if (dirProp == AN) {
2332            newProp = R;                        /* N0 */
2333            pLastIsoRun.lastBase = AN;
2334            pLastIsoRun.contextDir = R;
2335            pLastIsoRun.contextPos = position;
2336        }
2337        else if (dirProp == NSM) {
2338            /* if the last real char was ON, change NSM to ON so that it
2339               will stay ON even if the last real char is a bracket which
2340               may be changed to L or R */
2341            newProp = pLastIsoRun.lastBase;
2342            if (newProp == ON)
2343                dirProps[position] = newProp;
2344        }
2345        else {
2346            newProp = dirProp;
2347            pLastIsoRun.lastBase = dirProp;
2348        }
2349        if (newProp <= R || newProp == AL) {
2350            int i;
2351            short flag = (short)DirPropFlag(DirFromStrong(newProp));
2352            for (i = pLastIsoRun.start; i < pLastIsoRun.limit; i++)
2353                if (position > bd.openings[i].position)
2354                    bd.openings[i].flags |= flag;
2355        }
2356    }
2357
2358    /* perform (X1)..(X9) ------------------------------------------------------- */
2359
2360    /* determine if the text is mixed-directional or single-directional */
2361    private byte directionFromFlags() {
2362        /* if the text contains AN and neutrals, then some neutrals may become RTL */
2363        if (!((flags & MASK_RTL) != 0 ||
2364              ((flags & DirPropFlag(AN)) != 0 &&
2365               (flags & MASK_POSSIBLE_N) != 0))) {
2366            return LTR;
2367        } else if ((flags & MASK_LTR) == 0) {
2368            return RTL;
2369        } else {
2370            return MIXED;
2371        }
2372    }
2373
2374    /*
2375 * Resolve the explicit levels as specified by explicit embedding codes.
2376 * Recalculate the flags to have them reflect the real properties
2377 * after taking the explicit embeddings into account.
2378 *
2379 * The BiDi algorithm is designed to result in the same behavior whether embedding
2380 * levels are externally specified (from "styled text", supposedly the preferred
2381 * method) or set by explicit embedding codes (LRx, RLx, PDF, FSI, PDI) in the plain text.
2382 * That is why (X9) instructs to remove all not-isolate explicit codes (and BN).
2383 * However, in a real implementation, the removal of these codes and their index
2384 * positions in the plain text is undesirable since it would result in
2385 * reallocated, reindexed text.
2386 * Instead, this implementation leaves the codes in there and just ignores them
2387 * in the subsequent processing.
2388 * In order to get the same reordering behavior, positions with a BN or a not-isolate
2389 * explicit embedding code just get the same level assigned as the last "real"
2390 * character.
2391 *
2392 * Some implementations, not this one, then overwrite some of these
2393 * directionality properties at "real" same-level-run boundaries by
2394 * L or R codes so that the resolution of weak types can be performed on the
2395 * entire paragraph at once instead of having to parse it once more and
2396 * perform that resolution on same-level-runs.
2397 * This limits the scope of the implicit rules in effectively
2398 * the same way as the run limits.
2399 *
2400 * Instead, this implementation does not modify these codes, except for
2401 * paired brackets whose properties (ON) may be replaced by L or R.
2402 * On one hand, the paragraph has to be scanned for same-level-runs, but
2403 * on the other hand, this saves another loop to reset these codes,
2404 * or saves making and modifying a copy of dirProps[].
2405 *
2406 *
2407 * Note that (Pn) and (Xn) changed significantly from version 4 of the BiDi algorithm.
2408 *
2409 *
2410 * Handling the stack of explicit levels (Xn):
2411 *
2412 * With the BiDi stack of explicit levels, as pushed with each
2413 * LRE, RLE, LRO, RLO, LRI, RLI and FSI and popped with each PDF and PDI,
2414 * the explicit level must never exceed MAX_EXPLICIT_LEVEL.
2415 *
2416 * In order to have a correct push-pop semantics even in the case of overflows,
2417 * overflow counters and a valid isolate counter are used as described in UAX#9
2418 * section 3.3.2 "Explicit Levels and Directions".
2419 *
2420 * This implementation assumes that MAX_EXPLICIT_LEVEL is odd.
2421 *
2422 * Returns the direction
2423 *
2424 */
2425    private byte resolveExplicitLevels() {
2426        int i = 0;
2427        byte dirProp;
2428        byte level = GetParaLevelAt(0);
2429        byte dirct;
2430        isolateCount = 0;
2431
2432        /* determine if the text is mixed-directional or single-directional */
2433        dirct = directionFromFlags();
2434
2435        /* we may not need to resolve any explicit levels */
2436        if (dirct != MIXED) {
2437            /* not mixed directionality: levels don't matter - trailingWSStart will be 0 */
2438            return dirct;
2439        }
2440        if (reorderingMode > REORDER_LAST_LOGICAL_TO_VISUAL) {
2441            /* inverse BiDi: mixed, but all characters are at the same embedding level */
2442            /* set all levels to the paragraph level */
2443            int paraIndex, start, limit;
2444            for (paraIndex = 0; paraIndex < paraCount; paraIndex++) {
2445                if (paraIndex == 0)
2446                    start = 0;
2447                else
2448                    start = paras_limit[paraIndex - 1];
2449                limit = paras_limit[paraIndex];
2450                level = paras_level[paraIndex];
2451                for (i = start; i < limit; i++)
2452                    levels[i] =level;
2453            }
2454            return dirct;               /* no bracket matching for inverse BiDi */
2455        }
2456        if ((flags & (MASK_EXPLICIT | MASK_ISO)) == 0) {
2457            /* no embeddings, set all levels to the paragraph level */
2458            /* we still have to perform bracket matching */
2459            int paraIndex, start, limit;
2460            BracketData bracketData = new BracketData();
2461            bracketInit(bracketData);
2462            for (paraIndex = 0; paraIndex < paraCount; paraIndex++) {
2463                if (paraIndex == 0)
2464                    start = 0;
2465                else
2466                    start = paras_limit[paraIndex-1];
2467                limit = paras_limit[paraIndex];
2468                level = paras_level[paraIndex];
2469                for (i = start; i < limit; i++) {
2470                    levels[i] = level;
2471                    dirProp = dirProps[i];
2472                    if (dirProp == BN)
2473                        continue;
2474                    if (dirProp == B) {
2475                        if ((i + 1) < length) {
2476                            if (text[i] == CR && text[i + 1] == LF)
2477                                continue;   /* skip CR when followed by LF */
2478                            bracketProcessB(bracketData, level);
2479                        }
2480                        continue;
2481                    }
2482                    bracketProcessChar(bracketData, i);
2483                }
2484            }
2485            return dirct;
2486        }
2487        /* continue to perform (Xn) */
2488
2489        /* (X1) level is set for all codes, embeddingLevel keeps track of the push/pop operations */
2490        /* both variables may carry the LEVEL_OVERRIDE flag to indicate the override status */
2491        byte embeddingLevel = level, newLevel;
2492        byte previousLevel = level; /* previous level for regular (not CC) characters */
2493        int lastCcPos = 0;          /* index of last effective LRx,RLx, PDx */
2494
2495        /* The following stack remembers the embedding level and the ISOLATE flag of level runs.
2496           stackLast points to its current entry. */
2497        short[] stack = new short[MAX_EXPLICIT_LEVEL + 2];  /* we never push anything >= MAX_EXPLICIT_LEVEL
2498                                                               but we need one more entry as base */
2499        int stackLast = 0;
2500        int overflowIsolateCount = 0;
2501        int overflowEmbeddingCount = 0;
2502        int validIsolateCount = 0;
2503        BracketData bracketData = new BracketData();
2504        bracketInit(bracketData);
2505        stack[0] = level;       /* initialize base entry to para level, no override, no isolate */
2506
2507        /* recalculate the flags */
2508        flags = 0;
2509
2510        for (i = 0; i < length; i++) {
2511            dirProp = dirProps[i];
2512            switch (dirProp) {
2513            case LRE:
2514            case RLE:
2515            case LRO:
2516            case RLO:
2517                /* (X2, X3, X4, X5) */
2518                flags |= DirPropFlag(BN);
2519                levels[i] = previousLevel;
2520                if (dirProp == LRE || dirProp == LRO)
2521                    /* least greater even level */
2522                    newLevel = (byte)((embeddingLevel+2) & ~(LEVEL_OVERRIDE | 1));
2523                else
2524                    /* least greater odd level */
2525                    newLevel = (byte)((NoOverride(embeddingLevel) + 1) | 1);
2526                if (newLevel <= MAX_EXPLICIT_LEVEL && overflowIsolateCount == 0 &&
2527                                                      overflowEmbeddingCount == 0) {
2528                    lastCcPos = i;
2529                    embeddingLevel = newLevel;
2530                    if (dirProp == LRO || dirProp == RLO)
2531                        embeddingLevel |= LEVEL_OVERRIDE;
2532                    stackLast++;
2533                    stack[stackLast] = embeddingLevel;
2534                    /* we don't need to set LEVEL_OVERRIDE off for LRE and RLE
2535                       since this has already been done for newLevel which is
2536                       the source for embeddingLevel.
2537                     */
2538                } else {
2539                    if (overflowIsolateCount == 0)
2540                        overflowEmbeddingCount++;
2541                }
2542                break;
2543            case PDF:
2544                /* (X7) */
2545                flags |= DirPropFlag(BN);
2546                levels[i] = previousLevel;
2547                /* handle all the overflow cases first */
2548                if (overflowIsolateCount > 0) {
2549                    break;
2550                }
2551                if (overflowEmbeddingCount > 0) {
2552                    overflowEmbeddingCount--;
2553                    break;
2554                }
2555                if (stackLast > 0 && stack[stackLast] < ISOLATE) {   /* not an isolate entry */
2556                    lastCcPos = i;
2557                    stackLast--;
2558                    embeddingLevel = (byte)stack[stackLast];
2559                }
2560                break;
2561            case LRI:
2562            case RLI:
2563                flags |= DirPropFlag(ON) | DirPropFlagLR(embeddingLevel);
2564                levels[i] = NoOverride(embeddingLevel);
2565                if (NoOverride(embeddingLevel) != NoOverride(previousLevel)) {
2566                    bracketProcessBoundary(bracketData, lastCcPos,
2567                                           previousLevel, embeddingLevel);
2568                    flags |= DirPropFlagMultiRuns;
2569                }
2570                previousLevel = embeddingLevel;
2571                /* (X5a, X5b) */
2572                if (dirProp == LRI)
2573                    /* least greater even level */
2574                    newLevel=(byte)((embeddingLevel+2)&~(LEVEL_OVERRIDE|1));
2575                else
2576                    /* least greater odd level */
2577                    newLevel=(byte)((NoOverride(embeddingLevel)+1)|1);
2578                if (newLevel <= MAX_EXPLICIT_LEVEL && overflowIsolateCount == 0
2579                                                   && overflowEmbeddingCount == 0) {
2580                    flags |= DirPropFlag(dirProp);
2581                    lastCcPos = i;
2582                    validIsolateCount++;
2583                    if (validIsolateCount > isolateCount)
2584                        isolateCount = validIsolateCount;
2585                    embeddingLevel = newLevel;
2586                    /* we can increment stackLast without checking because newLevel
2587                       will exceed UBIDI_MAX_EXPLICIT_LEVEL before stackLast overflows */
2588                    stackLast++;
2589                    stack[stackLast] = (short)(embeddingLevel + ISOLATE);
2590                    bracketProcessLRI_RLI(bracketData, embeddingLevel);
2591                } else {
2592                    /* make it WS so that it is handled by adjustWSLevels() */
2593                    dirProps[i] = WS;
2594                    overflowIsolateCount++;
2595                }
2596                break;
2597            case PDI:
2598                if (NoOverride(embeddingLevel) != NoOverride(previousLevel)) {
2599                    bracketProcessBoundary(bracketData, lastCcPos,
2600                                           previousLevel, embeddingLevel);
2601                    flags |= DirPropFlagMultiRuns;
2602                }
2603                /* (X6a) */
2604                if (overflowIsolateCount > 0) {
2605                    overflowIsolateCount--;
2606                    /* make it WS so that it is handled by adjustWSLevels() */
2607                    dirProps[i] = WS;
2608                }
2609                else if (validIsolateCount > 0) {
2610                    flags |= DirPropFlag(PDI);
2611                    lastCcPos = i;
2612                    overflowEmbeddingCount = 0;
2613                    while (stack[stackLast] < ISOLATE)  /* pop embedding entries */
2614                        stackLast--;                    /* until the last isolate entry */
2615                    stackLast--;                        /* pop also the last isolate entry */
2616                    validIsolateCount--;
2617                    bracketProcessPDI(bracketData);
2618                } else
2619                    /* make it WS so that it is handled by adjustWSLevels() */
2620                    dirProps[i] = WS;
2621                embeddingLevel = (byte)(stack[stackLast] & ~ISOLATE);
2622                flags |= DirPropFlag(ON) | DirPropFlagLR(embeddingLevel);
2623                previousLevel = embeddingLevel;
2624                levels[i] = NoOverride(embeddingLevel);
2625                break;
2626            case B:
2627                flags |= DirPropFlag(B);
2628                levels[i] = GetParaLevelAt(i);
2629                if ((i + 1) < length) {
2630                    if (text[i] == CR && text[i + 1] == LF)
2631                        break;          /* skip CR when followed by LF */
2632                    overflowEmbeddingCount = overflowIsolateCount = 0;
2633                    validIsolateCount = 0;
2634                    stackLast = 0;
2635                    previousLevel = embeddingLevel = GetParaLevelAt(i + 1);
2636                    stack[0] = embeddingLevel;   /* initialize base entry to para level, no override, no isolate */
2637                    bracketProcessB(bracketData, embeddingLevel);
2638                }
2639                break;
2640            case BN:
2641                /* BN, LRE, RLE, and PDF are supposed to be removed (X9) */
2642                /* they will get their levels set correctly in adjustWSLevels() */
2643                levels[i] = previousLevel;
2644                flags |= DirPropFlag(BN);
2645                break;
2646            default:
2647                /* all other types are normal characters and get the "real" level */
2648                if (NoOverride(embeddingLevel) != NoOverride(previousLevel)) {
2649                    bracketProcessBoundary(bracketData, lastCcPos,
2650                                           previousLevel, embeddingLevel);
2651                    flags |= DirPropFlagMultiRuns;
2652                    if ((embeddingLevel & LEVEL_OVERRIDE) != 0)
2653                        flags |= DirPropFlagO(embeddingLevel);
2654                    else
2655                        flags |= DirPropFlagE(embeddingLevel);
2656                }
2657                previousLevel = embeddingLevel;
2658                levels[i] = embeddingLevel;
2659                bracketProcessChar(bracketData, i);
2660                /* the dirProp may have been changed in bracketProcessChar() */
2661                flags |= DirPropFlag(dirProps[i]);
2662                break;
2663            }
2664        }
2665        if ((flags & MASK_EMBEDDING) != 0) {
2666            flags |= DirPropFlagLR(paraLevel);
2667        }
2668        if (orderParagraphsLTR && (flags & DirPropFlag(B)) != 0) {
2669            flags |= DirPropFlag(L);
2670        }
2671        /* again, determine if the text is mixed-directional or single-directional */
2672        dirct = directionFromFlags();
2673
2674        return dirct;
2675    }
2676
2677    /*
2678     * Use a pre-specified embedding levels array:
2679     *
2680     * Adjust the directional properties for overrides (->LEVEL_OVERRIDE),
2681     * ignore all explicit codes (X9),
2682     * and check all the preset levels.
2683     *
2684     * Recalculate the flags to have them reflect the real properties
2685     * after taking the explicit embeddings into account.
2686     */
2687    private byte checkExplicitLevels() {
2688        byte dirProp;
2689        int i;
2690        int isolateCount = 0;
2691
2692        this.flags = 0;     /* collect all directionalities in the text */
2693        byte level;
2694        this.isolateCount = 0;
2695
2696        for (i = 0; i < length; ++i) {
2697            level = levels[i];
2698            dirProp = dirProps[i];
2699            if (dirProp == LRI || dirProp == RLI) {
2700                isolateCount++;
2701                if (isolateCount > this.isolateCount)
2702                    this.isolateCount = isolateCount;
2703            }
2704            else if (dirProp == PDI)
2705                isolateCount--;
2706            else if (dirProp == B)
2707                isolateCount = 0;
2708            if ((level & LEVEL_OVERRIDE) != 0) {
2709                /* keep the override flag in levels[i] but adjust the flags */
2710                level &= ~LEVEL_OVERRIDE;     /* make the range check below simpler */
2711                flags |= DirPropFlagO(level);
2712            } else {
2713                /* set the flags */
2714                flags |= DirPropFlagE(level) | DirPropFlag(dirProp);
2715            }
2716            if ((level < GetParaLevelAt(i) &&
2717                    !((0 == level) && (dirProp == B))) ||
2718                    (MAX_EXPLICIT_LEVEL < level)) {
2719                /* level out of bounds */
2720                throw new IllegalArgumentException("level " + level +
2721                                                   " out of bounds at " + i);
2722            }
2723        }
2724        if ((flags & MASK_EMBEDDING) != 0)
2725            flags |= DirPropFlagLR(paraLevel);
2726        /* determine if the text is mixed-directional or single-directional */
2727        return directionFromFlags();
2728    }
2729
2730    /*********************************************************************/
2731    /* The Properties state machine table                                */
2732    /*********************************************************************/
2733    /*                                                                   */
2734    /* All table cells are 8 bits:                                       */
2735    /*      bits 0..4:  next state                                       */
2736    /*      bits 5..7:  action to perform (if > 0)                       */
2737    /*                                                                   */
2738    /* Cells may be of format "n" where n represents the next state      */
2739    /* (except for the rightmost column).                                */
2740    /* Cells may also be of format "_(x,y)" where x represents an action */
2741    /* to perform and y represents the next state.                       */
2742    /*                                                                   */
2743    /*********************************************************************/
2744    /* Definitions and type for properties state tables                  */
2745    /*********************************************************************/
2746    private static final int IMPTABPROPS_COLUMNS = 16;
2747    private static final int IMPTABPROPS_RES = IMPTABPROPS_COLUMNS - 1;
2748    private static short GetStateProps(short cell) {
2749        return (short)(cell & 0x1f);
2750    }
2751    private static short GetActionProps(short cell) {
2752        return (short)(cell >> 5);
2753    }
2754
2755    private static final short groupProp[] =          /* dirProp regrouped */
2756    {
2757        /*  L   R   EN  ES  ET  AN  CS  B   S   WS  ON  LRE LRO AL  RLE RLO PDF NSM BN  FSI LRI RLI PDI ENL ENR */
2758            0,  1,  2,  7,  8,  3,  9,  6,  5,  4,  4,  10, 10, 12, 10, 10, 10, 11, 10, 4,  4,  4,  4,  13, 14
2759    };
2760    private static final short _L  = 0;
2761    private static final short _R  = 1;
2762    private static final short _EN = 2;
2763    private static final short _AN = 3;
2764    private static final short _ON = 4;
2765    private static final short _S  = 5;
2766    private static final short _B  = 6; /* reduced dirProp */
2767
2768    /*********************************************************************/
2769    /*                                                                   */
2770    /*      PROPERTIES  STATE  TABLE                                     */
2771    /*                                                                   */
2772    /* In table impTabProps,                                             */
2773    /*      - the ON column regroups ON and WS, FSI, RLI, LRI and PDI    */
2774    /*      - the BN column regroups BN, LRE, RLE, LRO, RLO, PDF         */
2775    /*      - the Res column is the reduced property assigned to a run   */
2776    /*                                                                   */
2777    /* Action 1: process current run1, init new run1                     */
2778    /*        2: init new run2                                           */
2779    /*        3: process run1, process run2, init new run1               */
2780    /*        4: process run1, set run1=run2, init new run2              */
2781    /*                                                                   */
2782    /* Notes:                                                            */
2783    /*  1) This table is used in resolveImplicitLevels().                */
2784    /*  2) This table triggers actions when there is a change in the Bidi*/
2785    /*     property of incoming characters (action 1).                   */
2786    /*  3) Most such property sequences are processed immediately (in    */
2787    /*     fact, passed to processPropertySeq().                         */
2788    /*  4) However, numbers are assembled as one sequence. This means    */
2789    /*     that undefined situations (like CS following digits, until    */
2790    /*     it is known if the next char will be a digit) are held until  */
2791    /*     following chars define them.                                  */
2792    /*     Example: digits followed by CS, then comes another CS or ON;  */
2793    /*              the digits will be processed, then the CS assigned   */
2794    /*              as the start of an ON sequence (action 3).           */
2795    /*  5) There are cases where more than one sequence must be          */
2796    /*     processed, for instance digits followed by CS followed by L:  */
2797    /*     the digits must be processed as one sequence, and the CS      */
2798    /*     must be processed as an ON sequence, all this before starting */
2799    /*     assembling chars for the opening L sequence.                  */
2800    /*                                                                   */
2801    /*                                                                   */
2802    private static final short impTabProps[][] =
2803    {
2804/*                        L,     R,    EN,    AN,    ON,     S,     B,    ES,    ET,    CS,    BN,   NSM,    AL,   ENL,   ENR,   Res */
2805/* 0 Init        */ {     1,     2,     4,     5,     7,    15,    17,     7,     9,     7,     0,     7,     3,    18,    21,   _ON },
2806/* 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, 32+18, 32+21,    _L },
2807/* 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, 32+18, 32+21,    _R },
2808/* 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, 32+18, 32+21,    _R },
2809/* 4 EN          */ {  32+1,  32+2,     4,  32+5,  32+7, 32+15, 32+17, 64+10,    11, 64+10,     4,     4,  32+3,    18,    21,   _EN },
2810/* 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, 32+18, 32+21,   _AN },
2811/* 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,    18,    21,   _AN },
2812/* 7 ON          */ {  32+1,  32+2,  32+4,  32+5,     7, 32+15, 32+17,     7, 64+14,     7,     7,     7,  32+3, 32+18, 32+21,   _ON },
2813/* 8 AL:ON       */ {  32+1,  32+2,  32+6,  32+6,     8, 32+16, 32+17,     8,     8,     8,     8,     8,  32+3, 32+18, 32+21,   _ON },
2814/* 9 ET          */ {  32+1,  32+2,     4,  32+5,     7, 32+15, 32+17,     7,     9,     7,     9,     9,  32+3,    18,    21,   _ON },
2815/*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,    18,    21,   _EN },
2816/*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,    18,    21,   _EN },
2817/*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, 96+18, 96+21,   _AN },
2818/*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,    18,    21,   _AN },
2819/*14 ON+ET       */ {  32+1,  32+2, 128+4,  32+5,     7, 32+15, 32+17,     7,    14,     7,    14,    14,  32+3,128+18,128+21,   _ON },
2820/*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, 32+18, 32+21,    _S },
2821/*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, 32+18, 32+21,    _S },
2822/*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, 32+18, 32+21,    _B },
2823/*18 ENL         */ {  32+1,  32+2,    18,  32+5,  32+7, 32+15, 32+17, 64+19,    20, 64+19,    18,    18,  32+3,    18,    21,    _L },
2824/*19 ENL+ES/CS   */ {  96+1,  96+2,    18,  96+5, 128+7, 96+15, 96+17, 128+7,128+14, 128+7,    19, 128+7,  96+3,    18,    21,    _L },
2825/*20 ENL+ET      */ {  32+1,  32+2,    18,  32+5,  32+7, 32+15, 32+17,  32+7,    20,  32+7,    20,    20,  32+3,    18,    21,    _L },
2826/*21 ENR         */ {  32+1,  32+2,    21,  32+5,  32+7, 32+15, 32+17, 64+22,    23, 64+22,    21,    21,  32+3,    18,    21,   _AN },
2827/*22 ENR+ES/CS   */ {  96+1,  96+2,    21,  96+5, 128+7, 96+15, 96+17, 128+7,128+14, 128+7,    22, 128+7,  96+3,    18,    21,   _AN },
2828/*23 ENR+ET      */ {  32+1,  32+2,    21,  32+5,  32+7, 32+15, 32+17,  32+7,    23,  32+7,    23,    23,  32+3,    18,    21,   _AN }
2829    };
2830
2831    /*********************************************************************/
2832    /* The levels state machine tables                                   */
2833    /*********************************************************************/
2834    /*                                                                   */
2835    /* All table cells are 8 bits:                                       */
2836    /*      bits 0..3:  next state                                       */
2837    /*      bits 4..7:  action to perform (if > 0)                       */
2838    /*                                                                   */
2839    /* Cells may be of format "n" where n represents the next state      */
2840    /* (except for the rightmost column).                                */
2841    /* Cells may also be of format "_(x,y)" where x represents an action */
2842    /* to perform and y represents the next state.                       */
2843    /*                                                                   */
2844    /* This format limits each table to 16 states each and to 15 actions.*/
2845    /*                                                                   */
2846    /*********************************************************************/
2847    /* Definitions and type for levels state tables                      */
2848    /*********************************************************************/
2849    private static final int IMPTABLEVELS_COLUMNS = _B + 2;
2850    private static final int IMPTABLEVELS_RES = IMPTABLEVELS_COLUMNS - 1;
2851    private static short GetState(byte cell) { return (short)(cell & 0x0f); }
2852    private static short GetAction(byte cell) { return (short)(cell >> 4); }
2853
2854    private static class ImpTabPair {
2855        byte[][][] imptab;
2856        short[][] impact;
2857
2858        ImpTabPair(byte[][] table1, byte[][] table2,
2859                   short[] act1, short[] act2) {
2860            imptab = new byte[][][] {table1, table2};
2861            impact = new short[][] {act1, act2};
2862        }
2863    }
2864
2865    /*********************************************************************/
2866    /*                                                                   */
2867    /*      LEVELS  STATE  TABLES                                        */
2868    /*                                                                   */
2869    /* In all levels state tables,                                       */
2870    /*      - state 0 is the initial state                               */
2871    /*      - the Res column is the increment to add to the text level   */
2872    /*        for this property sequence.                                */
2873    /*                                                                   */
2874    /* The impact arrays for each table of a pair map the local action   */
2875    /* numbers of the table to the total list of actions. For instance,  */
2876    /* action 2 in a given table corresponds to the action number which  */
2877    /* appears in entry [2] of the impact array for that table.          */
2878    /* The first entry of all impact arrays must be 0.                   */
2879    /*                                                                   */
2880    /* Action 1: init conditional sequence                               */
2881    /*        2: prepend conditional sequence to current sequence        */
2882    /*        3: set ON sequence to new level - 1                        */
2883    /*        4: init EN/AN/ON sequence                                  */
2884    /*        5: fix EN/AN/ON sequence followed by R                     */
2885    /*        6: set previous level sequence to level 2                  */
2886    /*                                                                   */
2887    /* Notes:                                                            */
2888    /*  1) These tables are used in processPropertySeq(). The input      */
2889    /*     is property sequences as determined by resolveImplicitLevels. */
2890    /*  2) Most such property sequences are processed immediately        */
2891    /*     (levels are assigned).                                        */
2892    /*  3) However, some sequences cannot be assigned a final level till */
2893    /*     one or more following sequences are received. For instance,   */
2894    /*     ON following an R sequence within an even-level paragraph.    */
2895    /*     If the following sequence is R, the ON sequence will be       */
2896    /*     assigned basic run level+1, and so will the R sequence.       */
2897    /*  4) S is generally handled like ON, since its level will be fixed */
2898    /*     to paragraph level in adjustWSLevels().                       */
2899    /*                                                                   */
2900
2901    private static final byte impTabL_DEFAULT[][] = /* Even paragraph level */
2902        /*  In this table, conditional sequences receive the lower possible level
2903            until proven otherwise.
2904        */
2905    {
2906        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2907        /* 0 : init       */ {     0,     1,     0,     2,     0,     0,     0,  0 },
2908        /* 1 : R          */ {     0,     1,     3,     3,  0x14,  0x14,     0,  1 },
2909        /* 2 : AN         */ {     0,     1,     0,     2,  0x15,  0x15,     0,  2 },
2910        /* 3 : R+EN/AN    */ {     0,     1,     3,     3,  0x14,  0x14,     0,  2 },
2911        /* 4 : R+ON       */ {     0,  0x21,  0x33,  0x33,     4,     4,     0,  0 },
2912        /* 5 : AN+ON      */ {     0,  0x21,     0,  0x32,     5,     5,     0,  0 }
2913    };
2914
2915    private static final byte impTabR_DEFAULT[][] = /* Odd  paragraph level */
2916        /*  In this table, conditional sequences receive the lower possible level
2917            until proven otherwise.
2918        */
2919    {
2920        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2921        /* 0 : init       */ {     1,     0,     2,     2,     0,     0,     0,  0 },
2922        /* 1 : L          */ {     1,     0,     1,     3,  0x14,  0x14,     0,  1 },
2923        /* 2 : EN/AN      */ {     1,     0,     2,     2,     0,     0,     0,  1 },
2924        /* 3 : L+AN       */ {     1,     0,     1,     3,     5,     5,     0,  1 },
2925        /* 4 : L+ON       */ {  0x21,     0,  0x21,     3,     4,     4,     0,  0 },
2926        /* 5 : L+AN+ON    */ {     1,     0,     1,     3,     5,     5,     0,  0 }
2927    };
2928
2929    private static final short[] impAct0 = {0,1,2,3,4};
2930
2931    private static final ImpTabPair impTab_DEFAULT = new ImpTabPair(
2932            impTabL_DEFAULT, impTabR_DEFAULT, impAct0, impAct0);
2933
2934    private static final byte impTabL_NUMBERS_SPECIAL[][] = { /* Even paragraph level */
2935        /* In this table, conditional sequences receive the lower possible
2936           level until proven otherwise.
2937        */
2938        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2939        /* 0 : init       */ {     0,     2,  0x11,  0x11,     0,     0,     0,  0 },
2940        /* 1 : L+EN/AN    */ {     0,  0x42,     1,     1,     0,     0,     0,  0 },
2941        /* 2 : R          */ {     0,     2,     4,     4,  0x13,  0x13,     0,  1 },
2942        /* 3 : R+ON       */ {     0,  0x22,  0x34,  0x34,     3,     3,     0,  0 },
2943        /* 4 : R+EN/AN    */ {     0,     2,     4,     4,  0x13,  0x13,     0,  2 }
2944    };
2945    private static final ImpTabPair impTab_NUMBERS_SPECIAL = new ImpTabPair(
2946            impTabL_NUMBERS_SPECIAL, impTabR_DEFAULT, impAct0, impAct0);
2947
2948    private static final byte impTabL_GROUP_NUMBERS_WITH_R[][] = {
2949        /* In this table, EN/AN+ON sequences receive levels as if associated with R
2950           until proven that there is L or sor/eor on both sides. AN is handled like EN.
2951        */
2952        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2953        /* 0 init         */ {     0,     3,  0x11,  0x11,     0,     0,     0,  0 },
2954        /* 1 EN/AN        */ {  0x20,     3,     1,     1,     2,  0x20,  0x20,  2 },
2955        /* 2 EN/AN+ON     */ {  0x20,     3,     1,     1,     2,  0x20,  0x20,  1 },
2956        /* 3 R            */ {     0,     3,     5,     5,  0x14,     0,     0,  1 },
2957        /* 4 R+ON         */ {  0x20,     3,     5,     5,     4,  0x20,  0x20,  1 },
2958        /* 5 R+EN/AN      */ {     0,     3,     5,     5,  0x14,     0,     0,  2 }
2959    };
2960    private static final byte impTabR_GROUP_NUMBERS_WITH_R[][] = {
2961        /*  In this table, EN/AN+ON sequences receive levels as if associated with R
2962            until proven that there is L on both sides. AN is handled like EN.
2963        */
2964        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2965        /* 0 init         */ {     2,     0,     1,     1,     0,     0,     0,  0 },
2966        /* 1 EN/AN        */ {     2,     0,     1,     1,     0,     0,     0,  1 },
2967        /* 2 L            */ {     2,     0,  0x14,  0x14,  0x13,     0,     0,  1 },
2968        /* 3 L+ON         */ {  0x22,     0,     4,     4,     3,     0,     0,  0 },
2969        /* 4 L+EN/AN      */ {  0x22,     0,     4,     4,     3,     0,     0,  1 }
2970    };
2971    private static final ImpTabPair impTab_GROUP_NUMBERS_WITH_R = new
2972            ImpTabPair(impTabL_GROUP_NUMBERS_WITH_R,
2973                       impTabR_GROUP_NUMBERS_WITH_R, impAct0, impAct0);
2974
2975    private static final byte impTabL_INVERSE_NUMBERS_AS_L[][] = {
2976        /* This table is identical to the Default LTR table except that EN and AN
2977           are handled like L.
2978        */
2979        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2980        /* 0 : init       */ {     0,     1,     0,     0,     0,     0,     0,  0 },
2981        /* 1 : R          */ {     0,     1,     0,     0,  0x14,  0x14,     0,  1 },
2982        /* 2 : AN         */ {     0,     1,     0,     0,  0x15,  0x15,     0,  2 },
2983        /* 3 : R+EN/AN    */ {     0,     1,     0,     0,  0x14,  0x14,     0,  2 },
2984        /* 4 : R+ON       */ {  0x20,     1,  0x20,  0x20,     4,     4,  0x20,  1 },
2985        /* 5 : AN+ON      */ {  0x20,     1,  0x20,  0x20,     5,     5,  0x20,  1 }
2986    };
2987    private static final byte impTabR_INVERSE_NUMBERS_AS_L[][] = {
2988        /* This table is identical to the Default RTL table except that EN and AN
2989           are handled like L.
2990        */
2991        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
2992        /* 0 : init       */ {     1,     0,     1,     1,     0,     0,     0,  0 },
2993        /* 1 : L          */ {     1,     0,     1,     1,  0x14,  0x14,     0,  1 },
2994        /* 2 : EN/AN      */ {     1,     0,     1,     1,     0,     0,     0,  1 },
2995        /* 3 : L+AN       */ {     1,     0,     1,     1,     5,     5,     0,  1 },
2996        /* 4 : L+ON       */ {  0x21,     0,  0x21,  0x21,     4,     4,     0,  0 },
2997        /* 5 : L+AN+ON    */ {     1,     0,     1,     1,     5,     5,     0,  0 }
2998    };
2999    private static final ImpTabPair impTab_INVERSE_NUMBERS_AS_L = new ImpTabPair
3000            (impTabL_INVERSE_NUMBERS_AS_L, impTabR_INVERSE_NUMBERS_AS_L,
3001             impAct0, impAct0);
3002
3003    private static final byte impTabR_INVERSE_LIKE_DIRECT[][] = {  /* Odd  paragraph level */
3004        /*  In this table, conditional sequences receive the lower possible level
3005            until proven otherwise.
3006        */
3007        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
3008        /* 0 : init       */ {     1,     0,     2,     2,     0,     0,     0,  0 },
3009        /* 1 : L          */ {     1,     0,     1,     2,  0x13,  0x13,     0,  1 },
3010        /* 2 : EN/AN      */ {     1,     0,     2,     2,     0,     0,     0,  1 },
3011        /* 3 : L+ON       */ {  0x21,  0x30,     6,     4,     3,     3,  0x30,  0 },
3012        /* 4 : L+ON+AN    */ {  0x21,  0x30,     6,     4,     5,     5,  0x30,  3 },
3013        /* 5 : L+AN+ON    */ {  0x21,  0x30,     6,     4,     5,     5,  0x30,  2 },
3014        /* 6 : L+ON+EN    */ {  0x21,  0x30,     6,     4,     3,     3,  0x30,  1 }
3015    };
3016    private static final short[] impAct1 = {0,1,13,14};
3017    private static final ImpTabPair impTab_INVERSE_LIKE_DIRECT = new ImpTabPair(
3018            impTabL_DEFAULT, impTabR_INVERSE_LIKE_DIRECT, impAct0, impAct1);
3019
3020    private static final byte impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS[][] = {
3021        /* The case handled in this table is (visually):  R EN L
3022         */
3023        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
3024        /* 0 : init       */ {     0,  0x63,     0,     1,     0,     0,     0,  0 },
3025        /* 1 : L+AN       */ {     0,  0x63,     0,     1,  0x12,  0x30,     0,  4 },
3026        /* 2 : L+AN+ON    */ {  0x20,  0x63,  0x20,     1,     2,  0x30,  0x20,  3 },
3027        /* 3 : R          */ {     0,  0x63,  0x55,  0x56,  0x14,  0x30,     0,  3 },
3028        /* 4 : R+ON       */ {  0x30,  0x43,  0x55,  0x56,     4,  0x30,  0x30,  3 },
3029        /* 5 : R+EN       */ {  0x30,  0x43,     5,  0x56,  0x14,  0x30,  0x30,  4 },
3030        /* 6 : R+AN       */ {  0x30,  0x43,  0x55,     6,  0x14,  0x30,  0x30,  4 }
3031    };
3032    private static final byte impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS[][] = {
3033        /* The cases handled in this table are (visually):  R EN L
3034                                                            R L AN L
3035        */
3036        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
3037        /* 0 : init       */ {  0x13,     0,     1,     1,     0,     0,     0,  0 },
3038        /* 1 : R+EN/AN    */ {  0x23,     0,     1,     1,     2,  0x40,     0,  1 },
3039        /* 2 : R+EN/AN+ON */ {  0x23,     0,     1,     1,     2,  0x40,     0,  0 },
3040        /* 3 : L          */ {     3,     0,     3,  0x36,  0x14,  0x40,     0,  1 },
3041        /* 4 : L+ON       */ {  0x53,  0x40,     5,  0x36,     4,  0x40,  0x40,  0 },
3042        /* 5 : L+ON+EN    */ {  0x53,  0x40,     5,  0x36,     4,  0x40,  0x40,  1 },
3043        /* 6 : L+AN       */ {  0x53,  0x40,     6,     6,     4,  0x40,  0x40,  3 }
3044    };
3045    private static final short[] impAct2 = {0,1,2,5,6,7,8};
3046    private static final short[] impAct3 = {0,1,9,10,11,12};
3047    private static final ImpTabPair impTab_INVERSE_LIKE_DIRECT_WITH_MARKS =
3048            new ImpTabPair(impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS,
3049                           impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS, impAct2, impAct3);
3050
3051    private static final ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL = new ImpTabPair(
3052            impTabL_NUMBERS_SPECIAL, impTabR_INVERSE_LIKE_DIRECT, impAct0, impAct1);
3053
3054    private static final byte impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS[][] = {
3055        /*  The case handled in this table is (visually):  R EN L
3056        */
3057        /*                         L,     R,    EN,    AN,    ON,     S,     B, Res */
3058        /* 0 : init       */ {     0,  0x62,     1,     1,     0,     0,     0,  0 },
3059        /* 1 : L+EN/AN    */ {     0,  0x62,     1,     1,     0,  0x30,     0,  4 },
3060        /* 2 : R          */ {     0,  0x62,  0x54,  0x54,  0x13,  0x30,     0,  3 },
3061        /* 3 : R+ON       */ {  0x30,  0x42,  0x54,  0x54,     3,  0x30,  0x30,  3 },
3062        /* 4 : R+EN/AN    */ {  0x30,  0x42,     4,     4,  0x13,  0x30,  0x30,  4 }
3063    };
3064    private static final ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS = new
3065            ImpTabPair(impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS,
3066                       impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS, impAct2, impAct3);
3067
3068    private static class LevState {
3069        byte[][] impTab;                /* level table pointer          */
3070        short[] impAct;                 /* action map array             */
3071        int startON;                    /* start of ON sequence         */
3072        int startL2EN;                  /* start of level 2 sequence    */
3073        int lastStrongRTL;              /* index of last found R or AL  */
3074        int runStart;                   /* start position of the run    */
3075        short state;                    /* current state                */
3076        byte runLevel;                  /* run level before implicit solving */
3077    }
3078
3079    /*------------------------------------------------------------------------*/
3080
3081    static final int FIRSTALLOC = 10;
3082    /*
3083     *  param pos:     position where to insert
3084     *  param flag:    one of LRM_BEFORE, LRM_AFTER, RLM_BEFORE, RLM_AFTER
3085     */
3086    private void addPoint(int pos, int flag)
3087    {
3088        Point point = new Point();
3089
3090        int len = insertPoints.points.length;
3091        if (len == 0) {
3092            insertPoints.points = new Point[FIRSTALLOC];
3093            len = FIRSTALLOC;
3094        }
3095        if (insertPoints.size >= len) { /* no room for new point */
3096            Point[] savePoints = insertPoints.points;
3097            insertPoints.points = new Point[len * 2];
3098            System.arraycopy(savePoints, 0, insertPoints.points, 0, len);
3099        }
3100        point.pos = pos;
3101        point.flag = flag;
3102        insertPoints.points[insertPoints.size] = point;
3103        insertPoints.size++;
3104    }
3105
3106    private void setLevelsOutsideIsolates(int start, int limit, byte level)
3107    {
3108        byte dirProp;
3109        int  isolateCount = 0, k;
3110        for (k = start; k < limit; k++) {
3111            dirProp = dirProps[k];
3112            if (dirProp == PDI)
3113                isolateCount--;
3114            if (isolateCount == 0)
3115                levels[k] = level;
3116            if (dirProp == LRI || dirProp == RLI)
3117                isolateCount++;
3118        }
3119    }
3120
3121    /* perform rules (Wn), (Nn), and (In) on a run of the text ------------------ */
3122
3123    /*
3124     * This implementation of the (Wn) rules applies all rules in one pass.
3125     * In order to do so, it needs a look-ahead of typically 1 character
3126     * (except for W5: sequences of ET) and keeps track of changes
3127     * in a rule Wp that affect a later Wq (p<q).
3128     *
3129     * The (Nn) and (In) rules are also performed in that same single loop,
3130     * but effectively one iteration behind for white space.
3131     *
3132     * Since all implicit rules are performed in one step, it is not necessary
3133     * to actually store the intermediate directional properties in dirProps[].
3134     */
3135
3136    private void processPropertySeq(LevState levState, short _prop,
3137            int start, int limit) {
3138        byte cell;
3139        byte[][] impTab = levState.impTab;
3140        short[] impAct = levState.impAct;
3141        short oldStateSeq,actionSeq;
3142        byte level, addLevel;
3143        int start0, k;
3144
3145        start0 = start;                 /* save original start position */
3146        oldStateSeq = levState.state;
3147        cell = impTab[oldStateSeq][_prop];
3148        levState.state = GetState(cell);        /* isolate the new state */
3149        actionSeq = impAct[GetAction(cell)];    /* isolate the action */
3150        addLevel = impTab[levState.state][IMPTABLEVELS_RES];
3151
3152        if (actionSeq != 0) {
3153            switch (actionSeq) {
3154            case 1:                     /* init ON seq */
3155                levState.startON = start0;
3156                break;
3157
3158            case 2:                     /* prepend ON seq to current seq */
3159                start = levState.startON;
3160                break;
3161
3162            case 3:                     /* EN/AN after R+ON */
3163                level = (byte)(levState.runLevel + 1);
3164                setLevelsOutsideIsolates(levState.startON, start0, level);
3165                break;
3166
3167            case 4:                     /* EN/AN before R for NUMBERS_SPECIAL */
3168                level = (byte)(levState.runLevel + 2);
3169                setLevelsOutsideIsolates(levState.startON, start0, level);
3170                break;
3171
3172            case 5:                     /* L or S after possible relevant EN/AN */
3173                /* check if we had EN after R/AL */
3174                if (levState.startL2EN >= 0) {
3175                    addPoint(levState.startL2EN, LRM_BEFORE);
3176                }
3177                levState.startL2EN = -1;  /* not within previous if since could also be -2 */
3178                /* check if we had any relevant EN/AN after R/AL */
3179                if ((insertPoints.points.length == 0) ||
3180                        (insertPoints.size <= insertPoints.confirmed)) {
3181                    /* nothing, just clean up */
3182                    levState.lastStrongRTL = -1;
3183                    /* check if we have a pending conditional segment */
3184                    level = impTab[oldStateSeq][IMPTABLEVELS_RES];
3185                    if ((level & 1) != 0 && levState.startON > 0) { /* after ON */
3186                        start = levState.startON;   /* reset to basic run level */
3187                    }
3188                    if (_prop == _S) {              /* add LRM before S */
3189                        addPoint(start0, LRM_BEFORE);
3190                        insertPoints.confirmed = insertPoints.size;
3191                    }
3192                    break;
3193                }
3194                /* reset previous RTL cont to level for LTR text */
3195                for (k = levState.lastStrongRTL + 1; k < start0; k++) {
3196                    /* reset odd level, leave runLevel+2 as is */
3197                    levels[k] = (byte)((levels[k] - 2) & ~1);
3198                }
3199                /* mark insert points as confirmed */
3200                insertPoints.confirmed = insertPoints.size;
3201                levState.lastStrongRTL = -1;
3202                if (_prop == _S) {           /* add LRM before S */
3203                    addPoint(start0, LRM_BEFORE);
3204                    insertPoints.confirmed = insertPoints.size;
3205                }
3206                break;
3207
3208            case 6:                     /* R/AL after possible relevant EN/AN */
3209                /* just clean up */
3210                if (insertPoints.points.length > 0)
3211                    /* remove all non confirmed insert points */
3212                    insertPoints.size = insertPoints.confirmed;
3213                levState.startON = -1;
3214                levState.startL2EN = -1;
3215                levState.lastStrongRTL = limit - 1;
3216                break;
3217
3218            case 7:                     /* EN/AN after R/AL + possible cont */
3219                /* check for real AN */
3220                if ((_prop == _AN) && (dirProps[start0] == AN) &&
3221                (reorderingMode != REORDER_INVERSE_FOR_NUMBERS_SPECIAL))
3222                {
3223                    /* real AN */
3224                    if (levState.startL2EN == -1) { /* if no relevant EN already found */
3225                        /* just note the rightmost digit as a strong RTL */
3226                        levState.lastStrongRTL = limit - 1;
3227                        break;
3228                    }
3229                    if (levState.startL2EN >= 0)  { /* after EN, no AN */
3230                        addPoint(levState.startL2EN, LRM_BEFORE);
3231                        levState.startL2EN = -2;
3232                    }
3233                    /* note AN */
3234                    addPoint(start0, LRM_BEFORE);
3235                    break;
3236                }
3237                /* if first EN/AN after R/AL */
3238                if (levState.startL2EN == -1) {
3239                    levState.startL2EN = start0;
3240                }
3241                break;
3242
3243            case 8:                     /* note location of latest R/AL */
3244                levState.lastStrongRTL = limit - 1;
3245                levState.startON = -1;
3246                break;
3247
3248            case 9:                     /* L after R+ON/EN/AN */
3249                /* include possible adjacent number on the left */
3250                for (k = start0-1; k >= 0 && ((levels[k] & 1) == 0); k--) {
3251                }
3252                if (k >= 0) {
3253                    addPoint(k, RLM_BEFORE);    /* add RLM before */
3254                    insertPoints.confirmed = insertPoints.size; /* confirm it */
3255                }
3256                levState.startON = start0;
3257                break;
3258
3259            case 10:                    /* AN after L */
3260                /* AN numbers between L text on both sides may be trouble. */
3261                /* tentatively bracket with LRMs; will be confirmed if followed by L */
3262                addPoint(start0, LRM_BEFORE);   /* add LRM before */
3263                addPoint(start0, LRM_AFTER);    /* add LRM after  */
3264                break;
3265
3266            case 11:                    /* R after L+ON/EN/AN */
3267                /* false alert, infirm LRMs around previous AN */
3268                insertPoints.size=insertPoints.confirmed;
3269                if (_prop == _S) {          /* add RLM before S */
3270                    addPoint(start0, RLM_BEFORE);
3271                    insertPoints.confirmed = insertPoints.size;
3272                }
3273                break;
3274
3275            case 12:                    /* L after L+ON/AN */
3276                level = (byte)(levState.runLevel + addLevel);
3277                for (k=levState.startON; k < start0; k++) {
3278                    if (levels[k] < level) {
3279                        levels[k] = level;
3280                    }
3281                }
3282                insertPoints.confirmed = insertPoints.size;   /* confirm inserts */
3283                levState.startON = start0;
3284                break;
3285
3286            case 13:                    /* L after L+ON+EN/AN/ON */
3287                level = levState.runLevel;
3288                for (k = start0-1; k >= levState.startON; k--) {
3289                    if (levels[k] == level+3) {
3290                        while (levels[k] == level+3) {
3291                            levels[k--] -= 2;
3292                        }
3293                        while (levels[k] == level) {
3294                            k--;
3295                        }
3296                    }
3297                    if (levels[k] == level+2) {
3298                        levels[k] = level;
3299                        continue;
3300                    }
3301                    levels[k] = (byte)(level+1);
3302                }
3303                break;
3304
3305            case 14:                    /* R after L+ON+EN/AN/ON */
3306                level = (byte)(levState.runLevel+1);
3307                for (k = start0-1; k >= levState.startON; k--) {
3308                    if (levels[k] > level) {
3309                        levels[k] -= 2;
3310                    }
3311                }
3312                break;
3313
3314            default:                        /* we should never get here */
3315                throw new IllegalStateException("Internal ICU error in processPropertySeq");
3316            }
3317        }
3318        if ((addLevel) != 0 || (start < start0)) {
3319            level = (byte)(levState.runLevel + addLevel);
3320            if (start >= levState.runStart) {
3321                for (k = start; k < limit; k++) {
3322                    levels[k] = level;
3323                }
3324            } else {
3325                setLevelsOutsideIsolates(start, limit, level);
3326            }
3327        }
3328    }
3329
3330    /**
3331     * Returns the directionality of the last strong character at the end of the prologue, if any.
3332     * Requires prologue!=null.
3333     */
3334    private byte lastL_R_AL() {
3335        for (int i = prologue.length(); i > 0; ) {
3336            int uchar = prologue.codePointBefore(i);
3337            i -= Character.charCount(uchar);
3338            byte dirProp = (byte)getCustomizedClass(uchar);
3339            if (dirProp == L) {
3340                return _L;
3341            }
3342            if (dirProp == R || dirProp == AL) {
3343                return _R;
3344            }
3345            if(dirProp == B) {
3346                return _ON;
3347            }
3348        }
3349        return _ON;
3350    }
3351
3352    /**
3353     * Returns the directionality of the first strong character, or digit, in the epilogue, if any.
3354     * Requires epilogue!=null.
3355     */
3356    private byte firstL_R_AL_EN_AN() {
3357        for (int i = 0; i < epilogue.length(); ) {
3358            int uchar = epilogue.codePointAt(i);
3359            i += Character.charCount(uchar);
3360            byte dirProp = (byte)getCustomizedClass(uchar);
3361            if (dirProp == L) {
3362                return _L;
3363            }
3364            if (dirProp == R || dirProp == AL) {
3365                return _R;
3366            }
3367            if (dirProp == EN) {
3368                return _EN;
3369            }
3370            if (dirProp == AN) {
3371                return _AN;
3372            }
3373        }
3374        return _ON;
3375    }
3376
3377    private void resolveImplicitLevels(int start, int limit, short sor, short eor)
3378    {
3379        byte dirProp;
3380        LevState levState = new LevState();
3381        int i, start1, start2;
3382        short oldStateImp, stateImp, actionImp;
3383        short gprop, resProp, cell;
3384        boolean inverseRTL;
3385        short nextStrongProp = R;
3386        int nextStrongPos = -1;
3387
3388        /* check for RTL inverse Bidi mode */
3389        /* FOOD FOR THOUGHT: in case of RTL inverse Bidi, it would make sense to
3390         * loop on the text characters from end to start.
3391         * This would need a different properties state table (at least different
3392         * actions) and different levels state tables (maybe very similar to the
3393         * LTR corresponding ones.
3394         */
3395        inverseRTL=((start<lastArabicPos) && ((GetParaLevelAt(start) & 1)>0) &&
3396                    (reorderingMode == REORDER_INVERSE_LIKE_DIRECT  ||
3397                     reorderingMode == REORDER_INVERSE_FOR_NUMBERS_SPECIAL));
3398        /* initialize for property and levels state table */
3399        levState.startL2EN = -1;        /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
3400        levState.lastStrongRTL = -1;    /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
3401        levState.runStart = start;
3402        levState.runLevel = levels[start];
3403        levState.impTab = impTabPair.imptab[levState.runLevel & 1];
3404        levState.impAct = impTabPair.impact[levState.runLevel & 1];
3405        if (start == 0 && prologue != null) {
3406            byte lastStrong = lastL_R_AL();
3407            if (lastStrong != _ON) {
3408                sor = lastStrong;
3409            }
3410        }
3411        /* The isolates[] entries contain enough information to
3412           resume the bidi algorithm in the same state as it was
3413           when it was interrupted by an isolate sequence. */
3414        if (dirProps[start] == PDI) {
3415            levState.startON = isolates[isolateCount].startON;
3416            start1 = isolates[isolateCount].start1;
3417            stateImp = isolates[isolateCount].stateImp;
3418            levState.state = isolates[isolateCount].state;
3419            isolateCount--;
3420        } else {
3421            levState.startON = -1;
3422            start1 = start;
3423            if (dirProps[start] == NSM)
3424                stateImp = (short)(1 + sor);
3425            else
3426                stateImp = 0;
3427            levState.state = 0;
3428            processPropertySeq(levState, sor, start, start);
3429        }
3430        start2 = start;                 /* to make the Java compiler happy */
3431
3432        for (i = start; i <= limit; i++) {
3433            if (i >= limit) {
3434                int k;
3435                for (k = limit - 1;
3436                     k > start &&
3437                         (DirPropFlag(dirProps[k]) & MASK_BN_EXPLICIT) != 0;
3438                     k--);
3439                dirProp = dirProps[k];
3440                if (dirProp == LRI || dirProp == RLI)
3441                    break;  /* no forced closing for sequence ending with LRI/RLI */
3442                gprop = eor;
3443            } else {
3444                byte prop, prop1;
3445                prop = dirProps[i];
3446                if (prop == B)
3447                    isolateCount = -1;  /* current isolates stack entry == none */
3448                if (inverseRTL) {
3449                    if (prop == AL) {
3450                        /* AL before EN does not make it AN */
3451                        prop = R;
3452                    } else if (prop == EN) {
3453                        if (nextStrongPos <= i) {
3454                            /* look for next strong char (L/R/AL) */
3455                            int j;
3456                            nextStrongProp = R;     /* set default */
3457                            nextStrongPos = limit;
3458                            for (j = i+1; j < limit; j++) {
3459                                prop1 = dirProps[j];
3460                                if (prop1 == L || prop1 == R || prop1 == AL) {
3461                                    nextStrongProp = prop1;
3462                                    nextStrongPos = j;
3463                                    break;
3464                                }
3465                            }
3466                        }
3467                        if (nextStrongProp == AL) {
3468                            prop = AN;
3469                        }
3470                    }
3471                }
3472                gprop = groupProp[prop];
3473            }
3474            oldStateImp = stateImp;
3475            cell = impTabProps[oldStateImp][gprop];
3476            stateImp = GetStateProps(cell);     /* isolate the new state */
3477            actionImp = GetActionProps(cell);   /* isolate the action */
3478            if ((i == limit) && (actionImp == 0)) {
3479                /* there is an unprocessed sequence if its property == eor   */
3480                actionImp = 1;                  /* process the last sequence */
3481            }
3482            if (actionImp != 0) {
3483                resProp = impTabProps[oldStateImp][IMPTABPROPS_RES];
3484                switch (actionImp) {
3485                case 1:             /* process current seq1, init new seq1 */
3486                    processPropertySeq(levState, resProp, start1, i);
3487                    start1 = i;
3488                    break;
3489                case 2:             /* init new seq2 */
3490                    start2 = i;
3491                    break;
3492                case 3:             /* process seq1, process seq2, init new seq1 */
3493                    processPropertySeq(levState, resProp, start1, start2);
3494                    processPropertySeq(levState, _ON, start2, i);
3495                    start1 = i;
3496                    break;
3497                case 4:             /* process seq1, set seq1=seq2, init new seq2 */
3498                    processPropertySeq(levState, resProp, start1, start2);
3499                    start1 = start2;
3500                    start2 = i;
3501                    break;
3502                default:            /* we should never get here */
3503                    throw new IllegalStateException("Internal ICU error in resolveImplicitLevels");
3504                }
3505            }
3506        }
3507
3508        /* flush possible pending sequence, e.g. ON */
3509        if (limit == length && epilogue != null) {
3510            byte firstStrong = firstL_R_AL_EN_AN();
3511            if (firstStrong != _ON) {
3512                eor = firstStrong;
3513            }
3514        }
3515
3516        /* look for the last char not a BN or LRE/RLE/LRO/RLO/PDF */
3517        for (i = limit - 1;
3518             i > start &&
3519                 (DirPropFlag(dirProps[i]) & MASK_BN_EXPLICIT) != 0;
3520             i--);
3521        dirProp = dirProps[i];
3522        if ((dirProp == LRI || dirProp == RLI) && limit < length) {
3523            isolateCount++;
3524            if (isolates[isolateCount] == null)
3525                isolates[isolateCount] = new Isolate();
3526            isolates[isolateCount].stateImp = stateImp;
3527            isolates[isolateCount].state = levState.state;
3528            isolates[isolateCount].start1 = start1;
3529            isolates[isolateCount].startON = levState.startON;
3530        }
3531        else
3532            processPropertySeq(levState, eor, limit, limit);
3533    }
3534
3535    /* perform (L1) and (X9) ---------------------------------------------------- */
3536
3537    /*
3538     * Reset the embedding levels for some non-graphic characters (L1).
3539     * This method also sets appropriate levels for BN, and
3540     * explicit embedding types that are supposed to have been removed
3541     * from the paragraph in (X9).
3542     */
3543    private void adjustWSLevels() {
3544        int i;
3545
3546        if ((flags & MASK_WS) != 0) {
3547            int flag;
3548            i = trailingWSStart;
3549            while (i > 0) {
3550                /* reset a sequence of WS/BN before eop and B/S to the paragraph paraLevel */
3551                while (i > 0 && ((flag = DirPropFlag(dirProps[--i])) & MASK_WS) != 0) {
3552                    if (orderParagraphsLTR && (flag & DirPropFlag(B)) != 0) {
3553                        levels[i] = 0;
3554                    } else {
3555                        levels[i] = GetParaLevelAt(i);
3556                    }
3557                }
3558
3559                /* reset BN to the next character's paraLevel until B/S, which restarts above loop */
3560                /* here, i+1 is guaranteed to be <length */
3561                while (i > 0) {
3562                    flag = DirPropFlag(dirProps[--i]);
3563                    if ((flag & MASK_BN_EXPLICIT) != 0) {
3564                        levels[i] = levels[i + 1];
3565                    } else if (orderParagraphsLTR && (flag & DirPropFlag(B)) != 0) {
3566                        levels[i] = 0;
3567                        break;
3568                    } else if ((flag & MASK_B_S) != 0){
3569                        levels[i] = GetParaLevelAt(i);
3570                        break;
3571                    }
3572                }
3573            }
3574        }
3575    }
3576
3577    /**
3578     * Set the context before a call to setPara().<p>
3579     *
3580     * setPara() computes the left-right directionality for a given piece
3581     * of text which is supplied as one of its arguments. Sometimes this piece
3582     * of text (the "main text") should be considered in context, because text
3583     * appearing before ("prologue") and/or after ("epilogue") the main text
3584     * may affect the result of this computation.<p>
3585     *
3586     * This function specifies the prologue and/or the epilogue for the next
3587     * call to setPara(). If successive calls to setPara()
3588     * all need specification of a context, setContext() must be called
3589     * before each call to setPara(). In other words, a context is not
3590     * "remembered" after the following successful call to setPara().<p>
3591     *
3592     * If a call to setPara() specifies DEFAULT_LTR or
3593     * DEFAULT_RTL as paraLevel and is preceded by a call to
3594     * setContext() which specifies a prologue, the paragraph level will
3595     * be computed taking in consideration the text in the prologue.<p>
3596     *
3597     * When setPara() is called without a previous call to
3598     * setContext, the main text is handled as if preceded and followed
3599     * by strong directional characters at the current paragraph level.
3600     * Calling setContext() with specification of a prologue will change
3601     * this behavior by handling the main text as if preceded by the last
3602     * strong character appearing in the prologue, if any.
3603     * Calling setContext() with specification of an epilogue will change
3604     * the behavior of setPara() by handling the main text as if followed
3605     * by the first strong character or digit appearing in the epilogue, if any.<p>
3606     *
3607     * Note 1: if <code>setContext</code> is called repeatedly without
3608     *         calling <code>setPara</code>, the earlier calls have no effect,
3609     *         only the last call will be remembered for the next call to
3610     *         <code>setPara</code>.<p>
3611     *
3612     * Note 2: calling <code>setContext(null, null)</code>
3613     *         cancels any previous setting of non-empty prologue or epilogue.
3614     *         The next call to <code>setPara()</code> will process no
3615     *         prologue or epilogue.<p>
3616     *
3617     * Note 3: users must be aware that even after setting the context
3618     *         before a call to setPara() to perform e.g. a logical to visual
3619     *         transformation, the resulting string may not be identical to what it
3620     *         would have been if all the text, including prologue and epilogue, had
3621     *         been processed together.<br>
3622     * Example (upper case letters represent RTL characters):<br>
3623     * &nbsp;&nbsp;prologue = "<code>abc DE</code>"<br>
3624     * &nbsp;&nbsp;epilogue = none<br>
3625     * &nbsp;&nbsp;main text = "<code>FGH xyz</code>"<br>
3626     * &nbsp;&nbsp;paraLevel = LTR<br>
3627     * &nbsp;&nbsp;display without prologue = "<code>HGF xyz</code>"
3628     *             ("HGF" is adjacent to "xyz")<br>
3629     * &nbsp;&nbsp;display with prologue = "<code>abc HGFED xyz</code>"
3630     *             ("HGF" is not adjacent to "xyz")<br>
3631     *
3632     * @param prologue is the text which precedes the text that
3633     *        will be specified in a coming call to setPara().
3634     *        If there is no prologue to consider,
3635     *        this parameter can be <code>null</code>.
3636     *
3637     * @param epilogue is the text which follows the text that
3638     *        will be specified in a coming call to setPara().
3639     *        If there is no epilogue to consider,
3640     *        this parameter can be <code>null</code>.
3641     *
3642     * @see #setPara
3643     * @stable ICU 4.8
3644     */
3645    public void setContext(String prologue, String epilogue) {
3646        this.prologue = prologue != null && prologue.length() > 0 ? prologue : null;
3647        this.epilogue = epilogue != null && epilogue.length() > 0 ? epilogue : null;
3648    }
3649
3650    private void setParaSuccess() {
3651        prologue = null;                /* forget the last context */
3652        epilogue = null;
3653        paraBidi = this;                /* mark successful setPara */
3654    }
3655
3656    int Bidi_Min(int x, int y) {
3657        return x < y ? x : y;
3658    }
3659
3660    int Bidi_Abs(int x) {
3661        return x >= 0 ? x : -x;
3662    }
3663
3664    void setParaRunsOnly(char[] parmText, byte parmParaLevel) {
3665        int[] visualMap;
3666        String visualText;
3667        int saveLength, saveTrailingWSStart;
3668        byte[] saveLevels;
3669        byte saveDirection;
3670        int i, j, visualStart, logicalStart,
3671            oldRunCount, runLength, addedRuns, insertRemove,
3672            start, limit, step, indexOddBit, logicalPos,
3673            index, index1;
3674        int saveOptions;
3675
3676        reorderingMode = REORDER_DEFAULT;
3677        int parmLength = parmText.length;
3678        if (parmLength == 0) {
3679            setPara(parmText, parmParaLevel, null);
3680            reorderingMode = REORDER_RUNS_ONLY;
3681            return;
3682        }
3683        /* obtain memory for mapping table and visual text */
3684        saveOptions = reorderingOptions;
3685        if ((saveOptions & OPTION_INSERT_MARKS) > 0) {
3686            reorderingOptions &= ~OPTION_INSERT_MARKS;
3687            reorderingOptions |= OPTION_REMOVE_CONTROLS;
3688        }
3689        parmParaLevel &= 1;             /* accept only 0 or 1 */
3690        setPara(parmText, parmParaLevel, null);
3691        /* we cannot access directly levels since it is not yet set if
3692         * direction is not MIXED
3693         */
3694        saveLevels = new byte[this.length];
3695        System.arraycopy(getLevels(), 0, saveLevels, 0, this.length);
3696        saveTrailingWSStart = trailingWSStart;
3697
3698        /* FOOD FOR THOUGHT: instead of writing the visual text, we could use
3699         * the visual map and the dirProps array to drive the second call
3700         * to setPara (but must make provision for possible removal of
3701         * Bidi controls.  Alternatively, only use the dirProps array via
3702         * customized classifier callback.
3703         */
3704        visualText = writeReordered(DO_MIRRORING);
3705        visualMap = getVisualMap();
3706        this.reorderingOptions = saveOptions;
3707        saveLength = this.length;
3708        saveDirection=this.direction;
3709
3710        this.reorderingMode = REORDER_INVERSE_LIKE_DIRECT;
3711        parmParaLevel ^= 1;
3712        setPara(visualText, parmParaLevel, null);
3713        BidiLine.getRuns(this);
3714        /* check if some runs must be split, count how many splits */
3715        addedRuns = 0;
3716        oldRunCount = this.runCount;
3717        visualStart = 0;
3718        for (i = 0; i < oldRunCount; i++, visualStart += runLength) {
3719            runLength = runs[i].limit - visualStart;
3720            if (runLength < 2) {
3721                continue;
3722            }
3723            logicalStart = runs[i].start;
3724            for (j = logicalStart+1; j < logicalStart+runLength; j++) {
3725                index = visualMap[j];
3726                index1 = visualMap[j-1];
3727                if ((Bidi_Abs(index-index1)!=1) || (saveLevels[index]!=saveLevels[index1])) {
3728                    addedRuns++;
3729                }
3730            }
3731        }
3732        if (addedRuns > 0) {
3733            getRunsMemory(oldRunCount + addedRuns);
3734            if (runCount == 1) {
3735                /* because we switch from UBiDi.simpleRuns to UBiDi.runs */
3736                runsMemory[0] = runs[0];
3737            } else {
3738                System.arraycopy(runs, 0, runsMemory, 0, runCount);
3739            }
3740            runs = runsMemory;
3741            runCount += addedRuns;
3742            for (i = oldRunCount; i < runCount; i++) {
3743                if (runs[i] == null) {
3744                    runs[i] = new BidiRun(0, 0, (byte)0);
3745                }
3746            }
3747        }
3748        /* split runs which are not consecutive in source text */
3749        int newI;
3750        for (i = oldRunCount-1; i >= 0; i--) {
3751            newI = i + addedRuns;
3752            runLength = i==0 ? runs[0].limit :
3753                               runs[i].limit - runs[i-1].limit;
3754            logicalStart = runs[i].start;
3755            indexOddBit = runs[i].level & 1;
3756            if (runLength < 2) {
3757                if (addedRuns > 0) {
3758                    runs[newI].copyFrom(runs[i]);
3759                }
3760                logicalPos = visualMap[logicalStart];
3761                runs[newI].start = logicalPos;
3762                runs[newI].level = (byte)(saveLevels[logicalPos] ^ indexOddBit);
3763                continue;
3764            }
3765            if (indexOddBit > 0) {
3766                start = logicalStart;
3767                limit = logicalStart + runLength - 1;
3768                step = 1;
3769            } else {
3770                start = logicalStart + runLength - 1;
3771                limit = logicalStart;
3772                step = -1;
3773            }
3774            for (j = start; j != limit; j += step) {
3775                index = visualMap[j];
3776                index1 = visualMap[j+step];
3777                if ((Bidi_Abs(index-index1)!=1) || (saveLevels[index]!=saveLevels[index1])) {
3778                    logicalPos = Bidi_Min(visualMap[start], index);
3779                    runs[newI].start = logicalPos;
3780                    runs[newI].level = (byte)(saveLevels[logicalPos] ^ indexOddBit);
3781                    runs[newI].limit = runs[i].limit;
3782                    runs[i].limit -= Bidi_Abs(j - start) + 1;
3783                    insertRemove = runs[i].insertRemove & (LRM_AFTER|RLM_AFTER);
3784                    runs[newI].insertRemove = insertRemove;
3785                    runs[i].insertRemove &= ~insertRemove;
3786                    start = j + step;
3787                    addedRuns--;
3788                    newI--;
3789                }
3790            }
3791            if (addedRuns > 0) {
3792                runs[newI].copyFrom(runs[i]);
3793            }
3794            logicalPos = Bidi_Min(visualMap[start], visualMap[limit]);
3795            runs[newI].start = logicalPos;
3796            runs[newI].level = (byte)(saveLevels[logicalPos] ^ indexOddBit);
3797        }
3798
3799//    cleanup1:
3800        /* restore initial paraLevel */
3801        this.paraLevel ^= 1;
3802//    cleanup2:
3803        /* restore real text */
3804        this.text = parmText;
3805        this.length = saveLength;
3806        this.originalLength = parmLength;
3807        this.direction=saveDirection;
3808        this.levels = saveLevels;
3809        this.trailingWSStart = saveTrailingWSStart;
3810        if (runCount > 1) {
3811            this.direction = MIXED;
3812        }
3813//    cleanup3:
3814        this.reorderingMode = REORDER_RUNS_ONLY;
3815    }
3816
3817    /**
3818     * Perform the Unicode Bidi algorithm. It is defined in the
3819     * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
3820     * version 13,
3821     * also described in The Unicode Standard, Version 4.0 .<p>
3822     *
3823     * This method takes a piece of plain text containing one or more paragraphs,
3824     * with or without externally specified embedding levels from <i>styled</i>
3825     * text and computes the left-right-directionality of each character.<p>
3826     *
3827     * If the entire text is all of the same directionality, then
3828     * the method may not perform all the steps described by the algorithm,
3829     * i.e., some levels may not be the same as if all steps were performed.
3830     * This is not relevant for unidirectional text.<br>
3831     * For example, in pure LTR text with numbers the numbers would get
3832     * a resolved level of 2 higher than the surrounding text according to
3833     * the algorithm. This implementation may set all resolved levels to
3834     * the same value in such a case.<p>
3835     *
3836     * The text can be composed of multiple paragraphs. Occurrence of a block
3837     * separator in the text terminates a paragraph, and whatever comes next starts
3838     * a new paragraph. The exception to this rule is when a Carriage Return (CR)
3839     * is followed by a Line Feed (LF). Both CR and LF are block separators, but
3840     * in that case, the pair of characters is considered as terminating the
3841     * preceding paragraph, and a new paragraph will be started by a character
3842     * coming after the LF.
3843     *
3844     * Although the text is passed here as a <code>String</code>, it is
3845     * stored internally as an array of characters. Therefore the
3846     * documentation will refer to indexes of the characters in the text.
3847     *
3848     * @param text contains the text that the Bidi algorithm will be performed
3849     *        on. This text can be retrieved with <code>getText()</code> or
3850     *        <code>getTextAsString</code>.<br>
3851     *
3852     * @param paraLevel specifies the default level for the text;
3853     *        it is typically 0 (LTR) or 1 (RTL).
3854     *        If the method shall determine the paragraph level from the text,
3855     *        then <code>paraLevel</code> can be set to
3856     *        either <code>LEVEL_DEFAULT_LTR</code>
3857     *        or <code>LEVEL_DEFAULT_RTL</code>; if the text contains multiple
3858     *        paragraphs, the paragraph level shall be determined separately for
3859     *        each paragraph; if a paragraph does not include any strongly typed
3860     *        character, then the desired default is used (0 for LTR or 1 for RTL).
3861     *        Any other value between 0 and <code>MAX_EXPLICIT_LEVEL</code>
3862     *        is also valid, with odd levels indicating RTL.
3863     *
3864     * @param embeddingLevels (in) may be used to preset the embedding and override levels,
3865     *        ignoring characters like LRE and PDF in the text.
3866     *        A level overrides the directional property of its corresponding
3867     *        (same index) character if the level has the
3868     *        <code>LEVEL_OVERRIDE</code> bit set.<br><br>
3869     *        Except for that bit, it must be
3870     *        <code>paraLevel<=embeddingLevels[]<=MAX_EXPLICIT_LEVEL</code>,
3871     *        with one exception: a level of zero may be specified for a
3872     *        paragraph separator even if <code>paraLevel&gt;0</code> when multiple
3873     *        paragraphs are submitted in the same call to <code>setPara()</code>.<br><br>
3874     *        <strong>Caution: </strong>A reference to this array, not a copy
3875     *        of the levels, will be stored in the <code>Bidi</code> object;
3876     *        the <code>embeddingLevels</code>
3877     *        should not be modified to avoid unexpected results on subsequent
3878     *        Bidi operations. However, the <code>setPara()</code> and
3879     *        <code>setLine()</code> methods may modify some or all of the
3880     *        levels.<br><br>
3881     *        <strong>Note:</strong> the <code>embeddingLevels</code> array must
3882     *        have one entry for each character in <code>text</code>.
3883     *
3884     * @throws IllegalArgumentException if the values in embeddingLevels are
3885     *         not within the allowed range
3886     *
3887     * @see #LEVEL_DEFAULT_LTR
3888     * @see #LEVEL_DEFAULT_RTL
3889     * @see #LEVEL_OVERRIDE
3890     * @see #MAX_EXPLICIT_LEVEL
3891     * @stable ICU 3.8
3892     */
3893    public void setPara(String text, byte paraLevel, byte[] embeddingLevels)
3894    {
3895        if (text == null) {
3896            setPara(new char[0], paraLevel, embeddingLevels);
3897        } else {
3898            setPara(text.toCharArray(), paraLevel, embeddingLevels);
3899        }
3900    }
3901
3902    /**
3903     * Perform the Unicode Bidi algorithm. It is defined in the
3904     * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
3905     * version 13,
3906     * also described in The Unicode Standard, Version 4.0 .<p>
3907     *
3908     * This method takes a piece of plain text containing one or more paragraphs,
3909     * with or without externally specified embedding levels from <i>styled</i>
3910     * text and computes the left-right-directionality of each character.<p>
3911     *
3912     * If the entire text is all of the same directionality, then
3913     * the method may not perform all the steps described by the algorithm,
3914     * i.e., some levels may not be the same as if all steps were performed.
3915     * This is not relevant for unidirectional text.<br>
3916     * For example, in pure LTR text with numbers the numbers would get
3917     * a resolved level of 2 higher than the surrounding text according to
3918     * the algorithm. This implementation may set all resolved levels to
3919     * the same value in such a case.<p>
3920     *
3921     * The text can be composed of multiple paragraphs. Occurrence of a block
3922     * separator in the text terminates a paragraph, and whatever comes next starts
3923     * a new paragraph. The exception to this rule is when a Carriage Return (CR)
3924     * is followed by a Line Feed (LF). Both CR and LF are block separators, but
3925     * in that case, the pair of characters is considered as terminating the
3926     * preceding paragraph, and a new paragraph will be started by a character
3927     * coming after the LF.
3928     *
3929     * The text is stored internally as an array of characters. Therefore the
3930     * documentation will refer to indexes of the characters in the text.
3931     *
3932     * @param chars contains the text that the Bidi algorithm will be performed
3933     *        on. This text can be retrieved with <code>getText()</code> or
3934     *        <code>getTextAsString</code>.<br>
3935     *
3936     * @param paraLevel specifies the default level for the text;
3937     *        it is typically 0 (LTR) or 1 (RTL).
3938     *        If the method shall determine the paragraph level from the text,
3939     *        then <code>paraLevel</code> can be set to
3940     *        either <code>LEVEL_DEFAULT_LTR</code>
3941     *        or <code>LEVEL_DEFAULT_RTL</code>; if the text contains multiple
3942     *        paragraphs, the paragraph level shall be determined separately for
3943     *        each paragraph; if a paragraph does not include any strongly typed
3944     *        character, then the desired default is used (0 for LTR or 1 for RTL).
3945     *        Any other value between 0 and <code>MAX_EXPLICIT_LEVEL</code>
3946     *        is also valid, with odd levels indicating RTL.
3947     *
3948     * @param embeddingLevels (in) may be used to preset the embedding and
3949     *        override levels, ignoring characters like LRE and PDF in the text.
3950     *        A level overrides the directional property of its corresponding
3951     *        (same index) character if the level has the
3952     *        <code>LEVEL_OVERRIDE</code> bit set.<br><br>
3953     *        Except for that bit, it must be
3954     *        <code>paraLevel<=embeddingLevels[]<=MAX_EXPLICIT_LEVEL</code>,
3955     *        with one exception: a level of zero may be specified for a
3956     *        paragraph separator even if <code>paraLevel&gt;0</code> when multiple
3957     *        paragraphs are submitted in the same call to <code>setPara()</code>.<br><br>
3958     *        <strong>Caution: </strong>A reference to this array, not a copy
3959     *        of the levels, will be stored in the <code>Bidi</code> object;
3960     *        the <code>embeddingLevels</code>
3961     *        should not be modified to avoid unexpected results on subsequent
3962     *        Bidi operations. However, the <code>setPara()</code> and
3963     *        <code>setLine()</code> methods may modify some or all of the
3964     *        levels.<br><br>
3965     *        <strong>Note:</strong> the <code>embeddingLevels</code> array must
3966     *        have one entry for each character in <code>text</code>.
3967     *
3968     * @throws IllegalArgumentException if the values in embeddingLevels are
3969     *         not within the allowed range
3970     *
3971     * @see #LEVEL_DEFAULT_LTR
3972     * @see #LEVEL_DEFAULT_RTL
3973     * @see #LEVEL_OVERRIDE
3974     * @see #MAX_EXPLICIT_LEVEL
3975     * @stable ICU 3.8
3976     */
3977    public void setPara(char[] chars, byte paraLevel, byte[] embeddingLevels)
3978    {
3979        /* check the argument values */
3980        if (paraLevel < LEVEL_DEFAULT_LTR) {
3981            verifyRange(paraLevel, 0, MAX_EXPLICIT_LEVEL + 1);
3982        }
3983        if (chars == null) {
3984            chars = new char[0];
3985        }
3986
3987        /* special treatment for RUNS_ONLY mode */
3988        if (reorderingMode == REORDER_RUNS_ONLY) {
3989            setParaRunsOnly(chars, paraLevel);
3990            return;
3991        }
3992
3993        /* initialize the Bidi object */
3994        this.paraBidi = null;          /* mark unfinished setPara */
3995        this.text = chars;
3996        this.length = this.originalLength = this.resultLength = text.length;
3997        this.paraLevel = paraLevel;
3998        this.direction = (byte)(paraLevel & 1);
3999        this.paraCount = 1;
4000
4001        /* Allocate zero-length arrays instead of setting to null here; then
4002         * checks for null in various places can be eliminated.
4003         */
4004        dirProps = new byte[0];
4005        levels = new byte[0];
4006        runs = new BidiRun[0];
4007        isGoodLogicalToVisualRunsMap = false;
4008        insertPoints.size = 0;          /* clean up from last call */
4009        insertPoints.confirmed = 0;     /* clean up from last call */
4010
4011        /*
4012         * Save the original paraLevel if contextual; otherwise, set to 0.
4013         */
4014        defaultParaLevel = IsDefaultLevel(paraLevel) ? paraLevel : 0;
4015
4016        if (length == 0) {
4017            /*
4018             * For an empty paragraph, create a Bidi object with the paraLevel and
4019             * the flags and the direction set but without allocating zero-length arrays.
4020             * There is nothing more to do.
4021             */
4022            if (IsDefaultLevel(paraLevel)) {
4023                this.paraLevel &= 1;
4024                defaultParaLevel = 0;
4025            }
4026            flags = DirPropFlagLR(paraLevel);
4027            runCount = 0;
4028            paraCount = 0;
4029            setParaSuccess();
4030            return;
4031        }
4032
4033        runCount = -1;
4034
4035        /*
4036         * Get the directional properties,
4037         * the flags bit-set, and
4038         * determine the paragraph level if necessary.
4039         */
4040        getDirPropsMemory(length);
4041        dirProps = dirPropsMemory;
4042        getDirProps();
4043        /* the processed length may have changed if OPTION_STREAMING is set */
4044        trailingWSStart = length;  /* the levels[] will reflect the WS run */
4045
4046        /* are explicit levels specified? */
4047        if (embeddingLevels == null) {
4048            /* no: determine explicit levels according to the (Xn) rules */
4049            getLevelsMemory(length);
4050            levels = levelsMemory;
4051            direction = resolveExplicitLevels();
4052        } else {
4053            /* set BN for all explicit codes, check that all levels are 0 or paraLevel..MAX_EXPLICIT_LEVEL */
4054            levels = embeddingLevels;
4055            direction = checkExplicitLevels();
4056        }
4057
4058        /* allocate isolate memory */
4059        if (isolateCount > 0) {
4060            if (isolates == null || isolates.length < isolateCount)
4061                isolates = new Isolate[isolateCount + 3];   /* keep some reserve */
4062        }
4063        isolateCount = -1;              /* current isolates stack entry == none */
4064
4065        /*
4066         * The steps after (X9) in the Bidi algorithm are performed only if
4067         * the paragraph text has mixed directionality!
4068         */
4069        switch (direction) {
4070        case LTR:
4071            /* all levels are implicitly at paraLevel (important for getLevels()) */
4072            trailingWSStart = 0;
4073            break;
4074        case RTL:
4075            /* all levels are implicitly at paraLevel (important for getLevels()) */
4076            trailingWSStart = 0;
4077            break;
4078        default:
4079            /*
4080             *  Choose the right implicit state table
4081             */
4082            switch(reorderingMode) {
4083            case REORDER_DEFAULT:
4084                this.impTabPair = impTab_DEFAULT;
4085                break;
4086            case REORDER_NUMBERS_SPECIAL:
4087                this.impTabPair = impTab_NUMBERS_SPECIAL;
4088                break;
4089            case REORDER_GROUP_NUMBERS_WITH_R:
4090                this.impTabPair = impTab_GROUP_NUMBERS_WITH_R;
4091                break;
4092            case REORDER_RUNS_ONLY:
4093                /* we should never get here */
4094                throw new InternalError("Internal ICU error in setPara");
4095                /* break; */
4096            case REORDER_INVERSE_NUMBERS_AS_L:
4097                this.impTabPair = impTab_INVERSE_NUMBERS_AS_L;
4098                break;
4099            case REORDER_INVERSE_LIKE_DIRECT:
4100                if ((reorderingOptions & OPTION_INSERT_MARKS) != 0) {
4101                    this.impTabPair = impTab_INVERSE_LIKE_DIRECT_WITH_MARKS;
4102                } else {
4103                    this.impTabPair = impTab_INVERSE_LIKE_DIRECT;
4104                }
4105                break;
4106            case REORDER_INVERSE_FOR_NUMBERS_SPECIAL:
4107                if ((reorderingOptions & OPTION_INSERT_MARKS) != 0) {
4108                    this.impTabPair = impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS;
4109                } else {
4110                    this.impTabPair = impTab_INVERSE_FOR_NUMBERS_SPECIAL;
4111                }
4112                break;
4113            }
4114            /*
4115             * If there are no external levels specified and there
4116             * are no significant explicit level codes in the text,
4117             * then we can treat the entire paragraph as one run.
4118             * Otherwise, we need to perform the following rules on runs of
4119             * the text with the same embedding levels. (X10)
4120             * "Significant" explicit level codes are ones that actually
4121             * affect non-BN characters.
4122             * Examples for "insignificant" ones are empty embeddings
4123             * LRE-PDF, LRE-RLE-PDF-PDF, etc.
4124             */
4125            if (embeddingLevels == null && paraCount <= 1 &&
4126                (flags & DirPropFlagMultiRuns) == 0) {
4127                resolveImplicitLevels(0, length,
4128                        GetLRFromLevel(GetParaLevelAt(0)),
4129                        GetLRFromLevel(GetParaLevelAt(length - 1)));
4130            } else {
4131                /* sor, eor: start and end types of same-level-run */
4132                int start, limit = 0;
4133                byte level, nextLevel;
4134                short sor, eor;
4135
4136                /* determine the first sor and set eor to it because of the loop body (sor=eor there) */
4137                level = GetParaLevelAt(0);
4138                nextLevel = levels[0];
4139                if (level < nextLevel) {
4140                    eor = GetLRFromLevel(nextLevel);
4141                } else {
4142                    eor = GetLRFromLevel(level);
4143                }
4144
4145                do {
4146                    /* determine start and limit of the run (end points just behind the run) */
4147
4148                    /* the values for this run's start are the same as for the previous run's end */
4149                    start = limit;
4150                    level = nextLevel;
4151                    if ((start > 0) && (dirProps[start - 1] == B)) {
4152                        /* except if this is a new paragraph, then set sor = para level */
4153                        sor = GetLRFromLevel(GetParaLevelAt(start));
4154                    } else {
4155                        sor = eor;
4156                    }
4157
4158                    /* search for the limit of this run */
4159                    while ((++limit < length) &&
4160                           ((levels[limit] == level) ||
4161                            ((DirPropFlag(dirProps[limit]) & MASK_BN_EXPLICIT) != 0))) {}
4162
4163                    /* get the correct level of the next run */
4164                    if (limit < length) {
4165                        nextLevel = levels[limit];
4166                    } else {
4167                        nextLevel = GetParaLevelAt(length - 1);
4168                    }
4169
4170                    /* determine eor from max(level, nextLevel); sor is last run's eor */
4171                    if (NoOverride(level) < NoOverride(nextLevel)) {
4172                        eor = GetLRFromLevel(nextLevel);
4173                    } else {
4174                        eor = GetLRFromLevel(level);
4175                    }
4176
4177                    /* if the run consists of overridden directional types, then there
4178                       are no implicit types to be resolved */
4179                    if ((level & LEVEL_OVERRIDE) == 0) {
4180                        resolveImplicitLevels(start, limit, sor, eor);
4181                    } else {
4182                        /* remove the LEVEL_OVERRIDE flags */
4183                        do {
4184                            levels[start++] &= ~LEVEL_OVERRIDE;
4185                        } while (start < limit);
4186                    }
4187                } while (limit  < length);
4188            }
4189
4190            /* reset the embedding levels for some non-graphic characters (L1), (X9) */
4191            adjustWSLevels();
4192
4193            break;
4194        }
4195        /* add RLM for inverse Bidi with contextual orientation resolving
4196         * to RTL which would not round-trip otherwise
4197         */
4198        if ((defaultParaLevel > 0) &&
4199            ((reorderingOptions & OPTION_INSERT_MARKS) != 0) &&
4200            ((reorderingMode == REORDER_INVERSE_LIKE_DIRECT) ||
4201             (reorderingMode == REORDER_INVERSE_FOR_NUMBERS_SPECIAL))) {
4202            int start, last;
4203            byte level;
4204            byte dirProp;
4205            for (int i = 0; i < paraCount; i++) {
4206                last = paras_limit[i] - 1;
4207                level = paras_level[i];
4208                if (level == 0)
4209                    continue;           /* LTR paragraph */
4210                start = i == 0 ? 0 : paras_limit[i - 1];
4211                for (int j = last; j >= start; j--) {
4212                    dirProp = dirProps[j];
4213                    if (dirProp == L) {
4214                        if (j < last) {
4215                            while (dirProps[last] == B) {
4216                                last--;
4217                            }
4218                        }
4219                        addPoint(last, RLM_BEFORE);
4220                        break;
4221                    }
4222                    if ((DirPropFlag(dirProp) & MASK_R_AL) != 0) {
4223                        break;
4224                    }
4225                }
4226            }
4227        }
4228
4229        if ((reorderingOptions & OPTION_REMOVE_CONTROLS) != 0) {
4230            resultLength -= controlCount;
4231        } else {
4232            resultLength += insertPoints.size;
4233        }
4234        setParaSuccess();
4235    }
4236
4237    /**
4238     * Perform the Unicode Bidi algorithm on a given paragraph, as defined in the
4239     * <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Standard Annex #9</a>,
4240     * version 13,
4241     * also described in The Unicode Standard, Version 4.0 .<p>
4242     *
4243     * This method takes a paragraph of text and computes the
4244     * left-right-directionality of each character. The text should not
4245     * contain any Unicode block separators.<p>
4246     *
4247     * The RUN_DIRECTION attribute in the text, if present, determines the base
4248     * direction (left-to-right or right-to-left). If not present, the base
4249     * direction is computed using the Unicode Bidirectional Algorithm,
4250     * defaulting to left-to-right if there are no strong directional characters
4251     * in the text. This attribute, if present, must be applied to all the text
4252     * in the paragraph.<p>
4253     *
4254     * The BIDI_EMBEDDING attribute in the text, if present, represents
4255     * embedding level information. Negative values from -1 to -62 indicate
4256     * overrides at the absolute value of the level. Positive values from 1 to
4257     * 62 indicate embeddings. Where values are zero or not defined, the base
4258     * embedding level as determined by the base direction is assumed.<p>
4259     *
4260     * The NUMERIC_SHAPING attribute in the text, if present, converts European
4261     * digits to other decimal digits before running the bidi algorithm. This
4262     * attribute, if present, must be applied to all the text in the paragraph.
4263     *
4264     * If the entire text is all of the same directionality, then
4265     * the method may not perform all the steps described by the algorithm,
4266     * i.e., some levels may not be the same as if all steps were performed.
4267     * This is not relevant for unidirectional text.<br>
4268     * For example, in pure LTR text with numbers the numbers would get
4269     * a resolved level of 2 higher than the surrounding text according to
4270     * the algorithm. This implementation may set all resolved levels to
4271     * the same value in such a case.<p>
4272     *
4273     * @param paragraph a paragraph of text with optional character and
4274     *        paragraph attribute information
4275     * @stable ICU 3.8
4276     */
4277    public void setPara(AttributedCharacterIterator paragraph)
4278    {
4279        byte paraLvl;
4280        Boolean runDirection = (Boolean) paragraph.getAttribute(TextAttribute.RUN_DIRECTION);
4281        if (runDirection == null) {
4282            paraLvl = LEVEL_DEFAULT_LTR;
4283        } else {
4284            paraLvl = (runDirection.equals(TextAttribute.RUN_DIRECTION_LTR)) ?
4285                        LTR : RTL;
4286        }
4287
4288        byte[] lvls = null;
4289        int len = paragraph.getEndIndex() - paragraph.getBeginIndex();
4290        byte[] embeddingLevels = new byte[len];
4291        char[] txt = new char[len];
4292        int i = 0;
4293        char ch = paragraph.first();
4294        while (ch != AttributedCharacterIterator.DONE) {
4295            txt[i] = ch;
4296            Integer embedding = (Integer) paragraph.getAttribute(TextAttribute.BIDI_EMBEDDING);
4297            if (embedding != null) {
4298                byte level = embedding.byteValue();
4299                if (level == 0) {
4300                    /* no-op */
4301                } else if (level < 0) {
4302                    lvls = embeddingLevels;
4303                    embeddingLevels[i] = (byte)((0 - level) | LEVEL_OVERRIDE);
4304                } else {
4305                    lvls = embeddingLevels;
4306                    embeddingLevels[i] = level;
4307                }
4308            }
4309            ch = paragraph.next();
4310            ++i;
4311        }
4312
4313        NumericShaper shaper = (NumericShaper) paragraph.getAttribute(TextAttribute.NUMERIC_SHAPING);
4314        if (shaper != null) {
4315            shaper.shape(txt, 0, len);
4316        }
4317        setPara(txt, paraLvl, lvls);
4318    }
4319
4320    /**
4321     * Specify whether block separators must be allocated level zero,
4322     * so that successive paragraphs will progress from left to right.
4323     * This method must be called before <code>setPara()</code>.
4324     * Paragraph separators (B) may appear in the text.  Setting them to level zero
4325     * means that all paragraph separators (including one possibly appearing
4326     * in the last text position) are kept in the reordered text after the text
4327     * that they follow in the source text.
4328     * When this feature is not enabled, a paragraph separator at the last
4329     * position of the text before reordering will go to the first position
4330     * of the reordered text when the paragraph level is odd.
4331     *
4332     * @param ordarParaLTR specifies whether paragraph separators (B) must
4333     * receive level 0, so that successive paragraphs progress from left to right.
4334     *
4335     * @see #setPara
4336     * @stable ICU 3.8
4337     */
4338    public void orderParagraphsLTR(boolean ordarParaLTR) {
4339        orderParagraphsLTR = ordarParaLTR;
4340    }
4341
4342    /**
4343     * Is this <code>Bidi</code> object set to allocate level 0 to block
4344     * separators so that successive paragraphs progress from left to right?
4345     *
4346     * @return <code>true</code> if the <code>Bidi</code> object is set to
4347     *         allocate level 0 to block separators.
4348     *
4349     * @stable ICU 3.8
4350     */
4351    public boolean isOrderParagraphsLTR() {
4352        return orderParagraphsLTR;
4353    }
4354
4355    /**
4356     * Get the directionality of the text.
4357     *
4358     * @return a value of <code>LTR</code>, <code>RTL</code> or <code>MIXED</code>
4359     *         that indicates if the entire text
4360     *         represented by this object is unidirectional,
4361     *         and which direction, or if it is mixed-directional.
4362     *
4363     * @throws IllegalStateException if this call is not preceded by a successful
4364     *         call to <code>setPara</code> or <code>setLine</code>
4365     *
4366     * @see #LTR
4367     * @see #RTL
4368     * @see #MIXED
4369     * @stable ICU 3.8
4370     */
4371    public byte getDirection()
4372    {
4373        verifyValidParaOrLine();
4374        return direction;
4375    }
4376
4377    /**
4378     * Get the text.
4379     *
4380     * @return A <code>String</code> containing the text that the
4381     *         <code>Bidi</code> object was created for.
4382     *
4383     * @throws IllegalStateException if this call is not preceded by a successful
4384     *         call to <code>setPara</code> or <code>setLine</code>
4385     *
4386     * @see #setPara
4387     * @see #setLine
4388     * @stable ICU 3.8
4389     */
4390    public String getTextAsString()
4391    {
4392        verifyValidParaOrLine();
4393        return new String(text);
4394    }
4395
4396    /**
4397     * Get the text.
4398     *
4399     * @return A <code>char</code> array containing the text that the
4400     *         <code>Bidi</code> object was created for.
4401     *
4402     * @throws IllegalStateException if this call is not preceded by a successful
4403     *         call to <code>setPara</code> or <code>setLine</code>
4404     *
4405     * @see #setPara
4406     * @see #setLine
4407     * @stable ICU 3.8
4408     */
4409    public char[] getText()
4410    {
4411        verifyValidParaOrLine();
4412        return text;
4413    }
4414
4415    /**
4416     * Get the length of the text.
4417     *
4418     * @return The length of the text that the <code>Bidi</code> object was
4419     *         created for.
4420     *
4421     * @throws IllegalStateException if this call is not preceded by a successful
4422     *         call to <code>setPara</code> or <code>setLine</code>
4423     * @stable ICU 3.8
4424     */
4425    public int getLength()
4426    {
4427        verifyValidParaOrLine();
4428        return originalLength;
4429    }
4430
4431    /**
4432     * Get the length of the source text processed by the last call to
4433     * <code>setPara()</code>. This length may be different from the length of
4434     * the source text if option <code>OPTION_STREAMING</code> has been
4435     * set.
4436     * <br>
4437     * Note that whenever the length of the text affects the execution or the
4438     * result of a method, it is the processed length which must be considered,
4439     * except for <code>setPara</code> (which receives unprocessed source text)
4440     * and <code>getLength</code> (which returns the original length of the
4441     * source text).<br>
4442     * In particular, the processed length is the one to consider in the
4443     * following cases:
4444     * <ul>
4445     * <li>maximum value of the <code>limit</code> argument of
4446     * <code>setLine</code></li>
4447     * <li>maximum value of the <code>charIndex</code> argument of
4448     * <code>getParagraph</code></li>
4449     * <li>maximum value of the <code>charIndex</code> argument of
4450     * <code>getLevelAt</code></li>
4451     * <li>number of elements in the array returned by <code>getLevels</code>
4452     * </li>
4453     * <li>maximum value of the <code>logicalStart</code> argument of
4454     * <code>getLogicalRun</code></li>
4455     * <li>maximum value of the <code>logicalIndex</code> argument of
4456     * <code>getVisualIndex</code></li>
4457     * <li>number of elements returned by <code>getLogicalMap</code></li>
4458     * <li>length of text processed by <code>writeReordered</code></li>
4459     * </ul>
4460     *
4461     * @return The length of the part of the source text processed by
4462     *         the last call to <code>setPara</code>.
4463     *
4464     * @throws IllegalStateException if this call is not preceded by a successful
4465     *         call to <code>setPara</code> or <code>setLine</code>
4466     *
4467     * @see #setPara
4468     * @see #OPTION_STREAMING
4469     * @stable ICU 3.8
4470     */
4471    public int getProcessedLength() {
4472        verifyValidParaOrLine();
4473        return length;
4474    }
4475
4476    /**
4477     * Get the length of the reordered text resulting from the last call to
4478     * <code>setPara()</code>. This length may be different from the length
4479     * of the source text if option <code>OPTION_INSERT_MARKS</code>
4480     * or option <code>OPTION_REMOVE_CONTROLS</code> has been set.
4481     * <br>
4482     * This resulting length is the one to consider in the following cases:
4483     * <ul>
4484     * <li>maximum value of the <code>visualIndex</code> argument of
4485     * <code>getLogicalIndex</code></li>
4486     * <li>number of elements returned by <code>getVisualMap</code></li>
4487     * </ul>
4488     * Note that this length stays identical to the source text length if
4489     * Bidi marks are inserted or removed using option bits of
4490     * <code>writeReordered</code>, or if option
4491     * <code>REORDER_INVERSE_NUMBERS_AS_L</code> has been set.
4492     *
4493     * @return The length of the reordered text resulting from
4494     *         the last call to <code>setPara</code>.
4495     *
4496     * @throws IllegalStateException if this call is not preceded by a successful
4497     *         call to <code>setPara</code> or <code>setLine</code>
4498     *
4499     * @see #setPara
4500     * @see #OPTION_INSERT_MARKS
4501     * @see #OPTION_REMOVE_CONTROLS
4502     * @see #REORDER_INVERSE_NUMBERS_AS_L
4503     * @stable ICU 3.8
4504     */
4505    public int getResultLength() {
4506        verifyValidParaOrLine();
4507        return resultLength;
4508    }
4509
4510    /* paragraphs API methods ------------------------------------------------- */
4511
4512    /**
4513     * Get the paragraph level of the text.
4514     *
4515     * @return The paragraph level. If there are multiple paragraphs, their
4516     *         level may vary if the required paraLevel is LEVEL_DEFAULT_LTR or
4517     *         LEVEL_DEFAULT_RTL.  In that case, the level of the first paragraph
4518     *         is returned.
4519     *
4520     * @throws IllegalStateException if this call is not preceded by a successful
4521     *         call to <code>setPara</code> or <code>setLine</code>
4522     *
4523     * @see #LEVEL_DEFAULT_LTR
4524     * @see #LEVEL_DEFAULT_RTL
4525     * @see #getParagraph
4526     * @see #getParagraphByIndex
4527     * @stable ICU 3.8
4528     */
4529    public byte getParaLevel()
4530    {
4531        verifyValidParaOrLine();
4532        return paraLevel;
4533    }
4534
4535    /**
4536     * Get the number of paragraphs.
4537     *
4538     * @return The number of paragraphs.
4539     *
4540     * @throws IllegalStateException if this call is not preceded by a successful
4541     *         call to <code>setPara</code> or <code>setLine</code>
4542     * @stable ICU 3.8
4543     */
4544    public int countParagraphs()
4545    {
4546        verifyValidParaOrLine();
4547        return paraCount;
4548    }
4549
4550    /**
4551     * Get a paragraph, given the index of this paragraph.
4552     *
4553     * This method returns information about a paragraph.<p>
4554     *
4555     * @param paraIndex is the number of the paragraph, in the
4556     *        range <code>[0..countParagraphs()-1]</code>.
4557     *
4558     * @return a BidiRun object with the details of the paragraph:<br>
4559     *        <code>start</code> will receive the index of the first character
4560     *        of the paragraph in the text.<br>
4561     *        <code>limit</code> will receive the limit of the paragraph.<br>
4562     *        <code>embeddingLevel</code> will receive the level of the paragraph.
4563     *
4564     * @throws IllegalStateException if this call is not preceded by a successful
4565     *         call to <code>setPara</code> or <code>setLine</code>
4566     * @throws IllegalArgumentException if paraIndex is not in the range
4567     *        <code>[0..countParagraphs()-1]</code>
4568     *
4569     * @see com.ibm.icu.text.BidiRun
4570     * @stable ICU 3.8
4571     */
4572    public BidiRun getParagraphByIndex(int paraIndex)
4573    {
4574        verifyValidParaOrLine();
4575        verifyRange(paraIndex, 0, paraCount);
4576
4577        Bidi bidi = paraBidi;             /* get Para object if Line object */
4578        int paraStart;
4579        if (paraIndex == 0) {
4580            paraStart = 0;
4581        } else {
4582            paraStart = bidi.paras_limit[paraIndex - 1];
4583        }
4584        BidiRun bidiRun = new BidiRun();
4585        bidiRun.start = paraStart;
4586        bidiRun.limit = bidi.paras_limit[paraIndex];
4587        bidiRun.level = GetParaLevelAt(paraStart);
4588        return bidiRun;
4589    }
4590
4591    /**
4592     * Get a paragraph, given a position within the text.
4593     * This method returns information about a paragraph.<br>
4594     * Note: if the paragraph index is known, it is more efficient to
4595     * retrieve the paragraph information using getParagraphByIndex().<p>
4596     *
4597     * @param charIndex is the index of a character within the text, in the
4598     *        range <code>[0..getProcessedLength()-1]</code>.
4599     *
4600     * @return a BidiRun object with the details of the paragraph:<br>
4601     *        <code>start</code> will receive the index of the first character
4602     *        of the paragraph in the text.<br>
4603     *        <code>limit</code> will receive the limit of the paragraph.<br>
4604     *        <code>embeddingLevel</code> will receive the level of the paragraph.
4605     *
4606     * @throws IllegalStateException if this call is not preceded by a successful
4607     *         call to <code>setPara</code> or <code>setLine</code>
4608     * @throws IllegalArgumentException if charIndex is not within the legal range
4609     *
4610     * @see com.ibm.icu.text.BidiRun
4611     * @see #getParagraphByIndex
4612     * @see #getProcessedLength
4613     * @stable ICU 3.8
4614     */
4615    public BidiRun getParagraph(int charIndex)
4616    {
4617        verifyValidParaOrLine();
4618        Bidi bidi = paraBidi;             /* get Para object if Line object */
4619        verifyRange(charIndex, 0, bidi.length);
4620        int paraIndex;
4621        for (paraIndex = 0; charIndex >= bidi.paras_limit[paraIndex]; paraIndex++) {
4622        }
4623        return getParagraphByIndex(paraIndex);
4624    }
4625
4626    /**
4627     * Get the index of a paragraph, given a position within the text.<p>
4628     *
4629     * @param charIndex is the index of a character within the text, in the
4630     *        range <code>[0..getProcessedLength()-1]</code>.
4631     *
4632     * @return The index of the paragraph containing the specified position,
4633     *         starting from 0.
4634     *
4635     * @throws IllegalStateException if this call is not preceded by a successful
4636     *         call to <code>setPara</code> or <code>setLine</code>
4637     * @throws IllegalArgumentException if charIndex is not within the legal range
4638     *
4639     * @see com.ibm.icu.text.BidiRun
4640     * @see #getProcessedLength
4641     * @stable ICU 3.8
4642     */
4643    public int getParagraphIndex(int charIndex)
4644    {
4645        verifyValidParaOrLine();
4646        Bidi bidi = paraBidi;             /* get Para object if Line object */
4647        verifyRange(charIndex, 0, bidi.length);
4648        int paraIndex;
4649        for (paraIndex = 0; charIndex >= bidi.paras_limit[paraIndex]; paraIndex++) {
4650        }
4651        return paraIndex;
4652    }
4653
4654    /**
4655     * Set a custom Bidi classifier used by the UBA implementation for Bidi
4656     * class determination.
4657     *
4658     * @param classifier A new custom classifier. This can be null.
4659     *
4660     * @see #getCustomClassifier
4661     * @stable ICU 3.8
4662     */
4663    public void setCustomClassifier(BidiClassifier classifier) {
4664        this.customClassifier = classifier;
4665    }
4666
4667    /**
4668     * Gets the current custom class classifier used for Bidi class
4669     * determination.
4670     *
4671     * @return An instance of class <code>BidiClassifier</code>
4672     *
4673     * @see #setCustomClassifier
4674     * @stable ICU 3.8
4675     */
4676    public BidiClassifier getCustomClassifier() {
4677        return this.customClassifier;
4678    }
4679
4680    /**
4681     * Retrieves the Bidi class for a given code point.
4682     * <p>If a <code>BidiClassifier</code> is defined and returns a value
4683     * other than <code>CLASS_DEFAULT</code>, that value is used; otherwise
4684     * the default class determination mechanism is invoked.</p>
4685     *
4686     * @param c The code point to get a Bidi class for.
4687     *
4688     * @return The Bidi class for the character <code>c</code> that is in effect
4689     *         for this <code>Bidi</code> instance.
4690     *
4691     * @see BidiClassifier
4692     * @stable ICU 3.8
4693     */
4694    public int getCustomizedClass(int c) {
4695        int dir;
4696
4697        if (customClassifier == null ||
4698                (dir = customClassifier.classify(c)) == Bidi.CLASS_DEFAULT) {
4699            dir = bdp.getClass(c);
4700        }
4701        if (dir >= UCharacterDirection.CHAR_DIRECTION_COUNT)
4702            dir = ON;
4703        return dir;
4704    }
4705
4706    /**
4707     * <code>setLine()</code> returns a <code>Bidi</code> object to
4708     * contain the reordering information, especially the resolved levels,
4709     * for all the characters in a line of text. This line of text is
4710     * specified by referring to a <code>Bidi</code> object representing
4711     * this information for a piece of text containing one or more paragraphs,
4712     * and by specifying a range of indexes in this text.<p>
4713     * In the new line object, the indexes will range from 0 to <code>limit-start-1</code>.<p>
4714     *
4715     * This is used after calling <code>setPara()</code>
4716     * for a piece of text, and after line-breaking on that text.
4717     * It is not necessary if each paragraph is treated as a single line.<p>
4718     *
4719     * After line-breaking, rules (L1) and (L2) for the treatment of
4720     * trailing WS and for reordering are performed on
4721     * a <code>Bidi</code> object that represents a line.<p>
4722     *
4723     * <strong>Important: </strong>the line <code>Bidi</code> object may
4724     * reference data within the global text <code>Bidi</code> object.
4725     * You should not alter the content of the global text object until
4726     * you are finished using the line object.
4727     *
4728     * @param start is the line's first index into the text.
4729     *
4730     * @param limit is just behind the line's last index into the text
4731     *        (its last index +1).
4732     *
4733     * @return a <code>Bidi</code> object that will now represent a line of the text.
4734     *
4735     * @throws IllegalStateException if this call is not preceded by a successful
4736     *         call to <code>setPara</code>
4737     * @throws IllegalArgumentException if start and limit are not in the range
4738     *         <code>0&lt;=start&lt;limit&lt;=getProcessedLength()</code>,
4739     *         or if the specified line crosses a paragraph boundary
4740     *
4741     * @see #setPara
4742     * @see #getProcessedLength
4743     * @stable ICU 3.8
4744     */
4745    public Bidi setLine(int start, int limit)
4746    {
4747        verifyValidPara();
4748        verifyRange(start, 0, limit);
4749        verifyRange(limit, 0, length+1);
4750        if (getParagraphIndex(start) != getParagraphIndex(limit - 1)) {
4751            /* the line crosses a paragraph boundary */
4752            throw new IllegalArgumentException();
4753        }
4754        return BidiLine.setLine(this, start, limit);
4755    }
4756
4757    /**
4758     * Get the level for one character.
4759     *
4760     * @param charIndex the index of a character.
4761     *
4762     * @return The level for the character at <code>charIndex</code>.
4763     *
4764     * @throws IllegalStateException if this call is not preceded by a successful
4765     *         call to <code>setPara</code> or <code>setLine</code>
4766     * @throws IllegalArgumentException if charIndex is not in the range
4767     *         <code>0&lt;=charIndex&lt;getProcessedLength()</code>
4768     *
4769     * @see #getProcessedLength
4770     * @stable ICU 3.8
4771     */
4772    public byte getLevelAt(int charIndex)
4773    {
4774        verifyValidParaOrLine();
4775        verifyRange(charIndex, 0, length);
4776        return BidiLine.getLevelAt(this, charIndex);
4777    }
4778
4779    /**
4780     * Get an array of levels for each character.<p>
4781     *
4782     * Note that this method may allocate memory under some
4783     * circumstances, unlike <code>getLevelAt()</code>.
4784     *
4785     * @return The levels array for the text,
4786     *         or <code>null</code> if an error occurs.
4787     *
4788     * @throws IllegalStateException if this call is not preceded by a successful
4789     *         call to <code>setPara</code> or <code>setLine</code>
4790     * @stable ICU 3.8
4791     */
4792    public byte[] getLevels()
4793    {
4794        verifyValidParaOrLine();
4795        if (length <= 0) {
4796            return new byte[0];
4797        }
4798        return BidiLine.getLevels(this);
4799    }
4800
4801    /**
4802     * Get a logical run.
4803     * This method returns information about a run and is used
4804     * to retrieve runs in logical order.<p>
4805     * This is especially useful for line-breaking on a paragraph.
4806     *
4807     * @param logicalPosition is a logical position within the source text.
4808     *
4809     * @return a BidiRun object filled with <code>start</code> containing
4810     *        the first character of the run, <code>limit</code> containing
4811     *        the limit of the run, and <code>embeddingLevel</code> containing
4812     *        the level of the run.
4813     *
4814     * @throws IllegalStateException if this call is not preceded by a successful
4815     *         call to <code>setPara</code> or <code>setLine</code>
4816     * @throws IllegalArgumentException if logicalPosition is not in the range
4817     *         <code>0&lt;=logicalPosition&lt;getProcessedLength()</code>
4818     *
4819     * @see com.ibm.icu.text.BidiRun
4820     * @see com.ibm.icu.text.BidiRun#getStart()
4821     * @see com.ibm.icu.text.BidiRun#getLimit()
4822     * @see com.ibm.icu.text.BidiRun#getEmbeddingLevel()
4823     *
4824     * @stable ICU 3.8
4825     */
4826    public BidiRun getLogicalRun(int logicalPosition)
4827    {
4828        verifyValidParaOrLine();
4829        verifyRange(logicalPosition, 0, length);
4830        return BidiLine.getLogicalRun(this, logicalPosition);
4831    }
4832
4833    /**
4834     * Get the number of runs.
4835     * This method may invoke the actual reordering on the
4836     * <code>Bidi</code> object, after <code>setPara()</code>
4837     * may have resolved only the levels of the text. Therefore,
4838     * <code>countRuns()</code> may have to allocate memory,
4839     * and may throw an exception if it fails to do so.
4840     *
4841     * @return The number of runs.
4842     *
4843     * @throws IllegalStateException if this call is not preceded by a successful
4844     *         call to <code>setPara</code> or <code>setLine</code>
4845     * @stable ICU 3.8
4846     */
4847    public int countRuns()
4848    {
4849        verifyValidParaOrLine();
4850        BidiLine.getRuns(this);
4851        return runCount;
4852    }
4853
4854    /**
4855     *
4856     * Get a <code>BidiRun</code> object according to its index. BidiRun methods
4857     * may be used to retrieve the run's logical start, length and level,
4858     * which can be even for an LTR run or odd for an RTL run.
4859     * In an RTL run, the character at the logical start is
4860     * visually on the right of the displayed run.
4861     * The length is the number of characters in the run.<p>
4862     * <code>countRuns()</code> is normally called
4863     * before the runs are retrieved.
4864     *
4865     * <p>
4866     *  Example:
4867     * <pre>
4868     *  Bidi bidi = new Bidi();
4869     *  String text = "abc 123 DEFG xyz";
4870     *  bidi.setPara(text, Bidi.RTL, null);
4871     *  int i, count=bidi.countRuns(), logicalStart, visualIndex=0, length;
4872     *  BidiRun run;
4873     *  for (i = 0; i &lt; count; ++i) {
4874     *      run = bidi.getVisualRun(i);
4875     *      logicalStart = run.getStart();
4876     *      length = run.getLength();
4877     *      if (Bidi.LTR == run.getEmbeddingLevel()) {
4878     *          do { // LTR
4879     *              show_char(text.charAt(logicalStart++), visualIndex++);
4880     *          } while (--length &gt; 0);
4881     *      } else {
4882     *          logicalStart += length;  // logicalLimit
4883     *          do { // RTL
4884     *              show_char(text.charAt(--logicalStart), visualIndex++);
4885     *          } while (--length &gt; 0);
4886     *      }
4887     *  }
4888     * </pre>
4889     * <p>
4890     * Note that in right-to-left runs, code like this places
4891     * second surrogates before first ones (which is generally a bad idea)
4892     * and combining characters before base characters.
4893     * <p>
4894     * Use of <code>{@link #writeReordered}</code>, optionally with the
4895     * <code>{@link #KEEP_BASE_COMBINING}</code> option, can be considered in
4896     * order to avoid these issues.
4897     *
4898     * @param runIndex is the number of the run in visual order, in the
4899     *        range <code>[0..countRuns()-1]</code>.
4900     *
4901     * @return a BidiRun object containing the details of the run. The
4902     *         directionality of the run is
4903     *         <code>LTR==0</code> or <code>RTL==1</code>,
4904     *         never <code>MIXED</code>.
4905     *
4906     * @throws IllegalStateException if this call is not preceded by a successful
4907     *         call to <code>setPara</code> or <code>setLine</code>
4908     * @throws IllegalArgumentException if <code>runIndex</code> is not in
4909     *         the range <code>0&lt;=runIndex&lt;countRuns()</code>
4910     *
4911     * @see #countRuns()
4912     * @see com.ibm.icu.text.BidiRun
4913     * @see com.ibm.icu.text.BidiRun#getStart()
4914     * @see com.ibm.icu.text.BidiRun#getLength()
4915     * @see com.ibm.icu.text.BidiRun#getEmbeddingLevel()
4916     * @stable ICU 3.8
4917     */
4918    public BidiRun getVisualRun(int runIndex)
4919    {
4920        verifyValidParaOrLine();
4921        BidiLine.getRuns(this);
4922        verifyRange(runIndex, 0, runCount);
4923        return BidiLine.getVisualRun(this, runIndex);
4924    }
4925
4926    /**
4927     * Get the visual position from a logical text position.
4928     * If such a mapping is used many times on the same
4929     * <code>Bidi</code> object, then calling
4930     * <code>getLogicalMap()</code> is more efficient.
4931     * <p>
4932     * The value returned may be <code>MAP_NOWHERE</code> if there is no
4933     * visual position because the corresponding text character is a Bidi
4934     * control removed from output by the option
4935     * <code>OPTION_REMOVE_CONTROLS</code>.
4936     * <p>
4937     * When the visual output is altered by using options of
4938     * <code>writeReordered()</code> such as <code>INSERT_LRM_FOR_NUMERIC</code>,
4939     * <code>KEEP_BASE_COMBINING</code>, <code>OUTPUT_REVERSE</code>,
4940     * <code>REMOVE_BIDI_CONTROLS</code>, the visual position returned may not
4941     * be correct. It is advised to use, when possible, reordering options
4942     * such as {@link #OPTION_INSERT_MARKS} and {@link #OPTION_REMOVE_CONTROLS}.
4943     * <p>
4944     * Note that in right-to-left runs, this mapping places
4945     * second surrogates before first ones (which is generally a bad idea)
4946     * and combining characters before base characters.
4947     * Use of <code>{@link #writeReordered}</code>, optionally with the
4948     * <code>{@link #KEEP_BASE_COMBINING}</code> option can be considered instead
4949     * of using the mapping, in order to avoid these issues.
4950     *
4951     * @param logicalIndex is the index of a character in the text.
4952     *
4953     * @return The visual position of this character.
4954     *
4955     * @throws IllegalStateException if this call is not preceded by a successful
4956     *         call to <code>setPara</code> or <code>setLine</code>
4957     * @throws IllegalArgumentException if <code>logicalIndex</code> is not in
4958     *         the range <code>0&lt;=logicalIndex&lt;getProcessedLength()</code>
4959     *
4960     * @see #getLogicalMap
4961     * @see #getLogicalIndex
4962     * @see #getProcessedLength
4963     * @see #MAP_NOWHERE
4964     * @see #OPTION_REMOVE_CONTROLS
4965     * @see #writeReordered
4966     * @stable ICU 3.8
4967     */
4968    public int getVisualIndex(int logicalIndex)
4969    {
4970        verifyValidParaOrLine();
4971        verifyRange(logicalIndex, 0, length);
4972        return BidiLine.getVisualIndex(this, logicalIndex);
4973    }
4974
4975
4976    /**
4977     * Get the logical text position from a visual position.
4978     * If such a mapping is used many times on the same
4979     * <code>Bidi</code> object, then calling
4980     * <code>getVisualMap()</code> is more efficient.
4981     * <p>
4982     * The value returned may be <code>MAP_NOWHERE</code> if there is no
4983     * logical position because the corresponding text character is a Bidi
4984     * mark inserted in the output by option
4985     * <code>OPTION_INSERT_MARKS</code>.
4986     * <p>
4987     * This is the inverse method to <code>getVisualIndex()</code>.
4988     * <p>
4989     * When the visual output is altered by using options of
4990     * <code>writeReordered()</code> such as <code>INSERT_LRM_FOR_NUMERIC</code>,
4991     * <code>KEEP_BASE_COMBINING</code>, <code>OUTPUT_REVERSE</code>,
4992     * <code>REMOVE_BIDI_CONTROLS</code>, the logical position returned may not
4993     * be correct. It is advised to use, when possible, reordering options
4994     * such as {@link #OPTION_INSERT_MARKS} and {@link #OPTION_REMOVE_CONTROLS}.
4995     *
4996     * @param visualIndex is the visual position of a character.
4997     *
4998     * @return The index of this character in the text.
4999     *
5000     * @throws IllegalStateException if this call is not preceded by a successful
5001     *         call to <code>setPara</code> or <code>setLine</code>
5002     * @throws IllegalArgumentException if <code>visualIndex</code> is not in
5003     *         the range <code>0&lt;=visualIndex&lt;getResultLength()</code>
5004     *
5005     * @see #getVisualMap
5006     * @see #getVisualIndex
5007     * @see #getResultLength
5008     * @see #MAP_NOWHERE
5009     * @see #OPTION_INSERT_MARKS
5010     * @see #writeReordered
5011     * @stable ICU 3.8
5012     */
5013    public int getLogicalIndex(int visualIndex)
5014    {
5015        verifyValidParaOrLine();
5016        verifyRange(visualIndex, 0, resultLength);
5017        /* we can do the trivial cases without the runs array */
5018        if (insertPoints.size == 0 && controlCount == 0) {
5019            if (direction == LTR) {
5020                return visualIndex;
5021            }
5022            else if (direction == RTL) {
5023                return length - visualIndex - 1;
5024            }
5025        }
5026        BidiLine.getRuns(this);
5027        return BidiLine.getLogicalIndex(this, visualIndex);
5028    }
5029
5030    /**
5031     * Get a logical-to-visual index map (array) for the characters in the
5032     * <code>Bidi</code> (paragraph or line) object.
5033     * <p>
5034     * Some values in the map may be <code>MAP_NOWHERE</code> if the
5035     * corresponding text characters are Bidi controls removed from the visual
5036     * output by the option <code>OPTION_REMOVE_CONTROLS</code>.
5037     * <p>
5038     * When the visual output is altered by using options of
5039     * <code>writeReordered()</code> such as <code>INSERT_LRM_FOR_NUMERIC</code>,
5040     * <code>KEEP_BASE_COMBINING</code>, <code>OUTPUT_REVERSE</code>,
5041     * <code>REMOVE_BIDI_CONTROLS</code>, the visual positions returned may not
5042     * be correct. It is advised to use, when possible, reordering options
5043     * such as {@link #OPTION_INSERT_MARKS} and {@link #OPTION_REMOVE_CONTROLS}.
5044     * <p>
5045     * Note that in right-to-left runs, this mapping places
5046     * second surrogates before first ones (which is generally a bad idea)
5047     * and combining characters before base characters.
5048     * Use of <code>{@link #writeReordered}</code>, optionally with the
5049     * <code>{@link #KEEP_BASE_COMBINING}</code> option can be considered instead
5050     * of using the mapping, in order to avoid these issues.
5051     *
5052     * @return an array of <code>getProcessedLength()</code>
5053     *        indexes which will reflect the reordering of the characters.<br><br>
5054     *        The index map will result in
5055     *        <code>indexMap[logicalIndex]==visualIndex</code>, where
5056     *        <code>indexMap</code> represents the returned array.
5057     *
5058     * @throws IllegalStateException if this call is not preceded by a successful
5059     *         call to <code>setPara</code> or <code>setLine</code>
5060     *
5061     * @see #getVisualMap
5062     * @see #getVisualIndex
5063     * @see #getProcessedLength
5064     * @see #MAP_NOWHERE
5065     * @see #OPTION_REMOVE_CONTROLS
5066     * @see #writeReordered
5067     * @stable ICU 3.8
5068     */
5069    public int[] getLogicalMap()
5070    {
5071        /* countRuns() checks successful call to setPara/setLine */
5072        countRuns();
5073        if (length <= 0) {
5074            return new int[0];
5075        }
5076        return BidiLine.getLogicalMap(this);
5077    }
5078
5079    /**
5080     * Get a visual-to-logical index map (array) for the characters in the
5081     * <code>Bidi</code> (paragraph or line) object.
5082     * <p>
5083     * Some values in the map may be <code>MAP_NOWHERE</code> if the
5084     * corresponding text characters are Bidi marks inserted in the visual
5085     * output by the option <code>OPTION_INSERT_MARKS</code>.
5086     * <p>
5087     * When the visual output is altered by using options of
5088     * <code>writeReordered()</code> such as <code>INSERT_LRM_FOR_NUMERIC</code>,
5089     * <code>KEEP_BASE_COMBINING</code>, <code>OUTPUT_REVERSE</code>,
5090     * <code>REMOVE_BIDI_CONTROLS</code>, the logical positions returned may not
5091     * be correct. It is advised to use, when possible, reordering options
5092     * such as {@link #OPTION_INSERT_MARKS} and {@link #OPTION_REMOVE_CONTROLS}.
5093     *
5094     * @return an array of <code>getResultLength()</code>
5095     *        indexes which will reflect the reordering of the characters.<br><br>
5096     *        The index map will result in
5097     *        <code>indexMap[visualIndex]==logicalIndex</code>, where
5098     *        <code>indexMap</code> represents the returned array.
5099     *
5100     * @throws IllegalStateException if this call is not preceded by a successful
5101     *         call to <code>setPara</code> or <code>setLine</code>
5102     *
5103     * @see #getLogicalMap
5104     * @see #getLogicalIndex
5105     * @see #getResultLength
5106     * @see #MAP_NOWHERE
5107     * @see #OPTION_INSERT_MARKS
5108     * @see #writeReordered
5109     * @stable ICU 3.8
5110     */
5111    public int[] getVisualMap()
5112    {
5113        /* countRuns() checks successful call to setPara/setLine */
5114        countRuns();
5115        if (resultLength <= 0) {
5116            return new int[0];
5117        }
5118        return BidiLine.getVisualMap(this);
5119    }
5120
5121    /**
5122     * This is a convenience method that does not use a <code>Bidi</code> object.
5123     * It is intended to be used for when an application has determined the levels
5124     * of objects (character sequences) and just needs to have them reordered (L2).
5125     * This is equivalent to using <code>getLogicalMap()</code> on a
5126     * <code>Bidi</code> object.
5127     *
5128     * @param levels is an array of levels that have been determined by
5129     *        the application.
5130     *
5131     * @return an array of <code>levels.length</code>
5132     *        indexes which will reflect the reordering of the characters.<p>
5133     *        The index map will result in
5134     *        <code>indexMap[logicalIndex]==visualIndex</code>, where
5135     *        <code>indexMap</code> represents the returned array.
5136     *
5137     * @stable ICU 3.8
5138     */
5139    public static int[] reorderLogical(byte[] levels)
5140    {
5141        return BidiLine.reorderLogical(levels);
5142    }
5143
5144    /**
5145     * This is a convenience method that does not use a <code>Bidi</code> object.
5146     * It is intended to be used for when an application has determined the levels
5147     * of objects (character sequences) and just needs to have them reordered (L2).
5148     * This is equivalent to using <code>getVisualMap()</code> on a
5149     * <code>Bidi</code> object.
5150     *
5151     * @param levels is an array of levels that have been determined by
5152     *        the application.
5153     *
5154     * @return an array of <code>levels.length</code>
5155     *        indexes which will reflect the reordering of the characters.<p>
5156     *        The index map will result in
5157     *        <code>indexMap[visualIndex]==logicalIndex</code>, where
5158     *        <code>indexMap</code> represents the returned array.
5159     *
5160     * @stable ICU 3.8
5161     */
5162    public static int[] reorderVisual(byte[] levels)
5163    {
5164        return BidiLine.reorderVisual(levels);
5165    }
5166
5167    /**
5168     * Invert an index map.
5169     * The index mapping of the argument map is inverted and returned as
5170     * an array of indexes that we will call the inverse map.
5171     *
5172     * @param srcMap is an array whose elements define the original mapping
5173     * from a source array to a destination array.
5174     * Some elements of the source array may have no mapping in the
5175     * destination array. In that case, their value will be
5176     * the special value <code>MAP_NOWHERE</code>.
5177     * All elements must be >=0 or equal to <code>MAP_NOWHERE</code>.
5178     * Some elements in the source map may have a value greater than the
5179     * srcMap.length if the destination array has more elements than the
5180     * source array.
5181     * There must be no duplicate indexes (two or more elements with the
5182     * same value except <code>MAP_NOWHERE</code>).
5183     *
5184     * @return an array representing the inverse map.
5185     *         This array has a number of elements equal to 1 + the highest
5186     *         value in <code>srcMap</code>.
5187     *         For elements of the result array which have no matching elements
5188     *         in the source array, the corresponding elements in the inverse
5189     *         map will receive a value equal to <code>MAP_NOWHERE</code>.
5190     *         If element with index i in <code>srcMap</code> has a value k different
5191     *         from <code>MAP_NOWHERE</code>, this means that element i of
5192     *         the source array maps to element k in the destination array.
5193     *         The inverse map will have value i in its k-th element.
5194     *         For all elements of the destination array which do not map to
5195     *         an element in the source array, the corresponding element in the
5196     *         inverse map will have a value equal to <code>MAP_NOWHERE</code>.
5197     *
5198     * @see #MAP_NOWHERE
5199     * @stable ICU 3.8
5200     */
5201    public static int[] invertMap(int[] srcMap)
5202    {
5203        if (srcMap == null) {
5204            return null;
5205        } else {
5206            return BidiLine.invertMap(srcMap);
5207        }
5208    }
5209
5210    /*
5211     * Fields and methods for compatibility with java.text.bidi (Sun implementation)
5212     */
5213
5214    /**
5215     * Constant indicating base direction is left-to-right.
5216     * @stable ICU 3.8
5217     */
5218    public static final int DIRECTION_LEFT_TO_RIGHT = LTR;
5219
5220    /**
5221     * Constant indicating base direction is right-to-left.
5222     * @stable ICU 3.8
5223     */
5224    public static final int DIRECTION_RIGHT_TO_LEFT = RTL;
5225
5226    /**
5227     * Constant indicating that the base direction depends on the first strong
5228     * directional character in the text according to the Unicode Bidirectional
5229     * Algorithm. If no strong directional character is present, the base
5230     * direction is left-to-right.
5231     * @stable ICU 3.8
5232     */
5233    public static final int DIRECTION_DEFAULT_LEFT_TO_RIGHT = LEVEL_DEFAULT_LTR;
5234
5235    /**
5236     * Constant indicating that the base direction depends on the first strong
5237     * directional character in the text according to the Unicode Bidirectional
5238     * Algorithm. If no strong directional character is present, the base
5239     * direction is right-to-left.
5240     * @stable ICU 3.8
5241     */
5242    public static final int DIRECTION_DEFAULT_RIGHT_TO_LEFT = LEVEL_DEFAULT_RTL;
5243
5244    /**
5245     * Create Bidi from the given paragraph of text and base direction.
5246     *
5247     * @param paragraph a paragraph of text
5248     * @param flags a collection of flags that control the algorithm. The
5249     *        algorithm understands the flags DIRECTION_LEFT_TO_RIGHT,
5250     *        DIRECTION_RIGHT_TO_LEFT, DIRECTION_DEFAULT_LEFT_TO_RIGHT, and
5251     *        DIRECTION_DEFAULT_RIGHT_TO_LEFT. Other values are reserved.
5252     * @see #DIRECTION_LEFT_TO_RIGHT
5253     * @see #DIRECTION_RIGHT_TO_LEFT
5254     * @see #DIRECTION_DEFAULT_LEFT_TO_RIGHT
5255     * @see #DIRECTION_DEFAULT_RIGHT_TO_LEFT
5256     * @stable ICU 3.8
5257     */
5258    public Bidi(String paragraph, int flags)
5259    {
5260        this(paragraph.toCharArray(), 0, null, 0, paragraph.length(), flags);
5261    }
5262
5263    /**
5264     * Create Bidi from the given paragraph of text.<p>
5265     *
5266     * The RUN_DIRECTION attribute in the text, if present, determines the base
5267     * direction (left-to-right or right-to-left). If not present, the base
5268     * direction is computed using the Unicode Bidirectional Algorithm,
5269     * defaulting to left-to-right if there are no strong directional characters
5270     * in the text. This attribute, if present, must be applied to all the text
5271     * in the paragraph.<p>
5272     *
5273     * The BIDI_EMBEDDING attribute in the text, if present, represents
5274     * embedding level information. Negative values from -1 to -62 indicate
5275     * overrides at the absolute value of the level. Positive values from 1 to
5276     * 62 indicate embeddings. Where values are zero or not defined, the base
5277     * embedding level as determined by the base direction is assumed.<p>
5278     *
5279     * The NUMERIC_SHAPING attribute in the text, if present, converts European
5280     * digits to other decimal digits before running the bidi algorithm. This
5281     * attribute, if present, must be applied to all the text in the paragraph.<p>
5282     *
5283     * Note: this constructor calls setPara() internally.
5284     *
5285     * @param paragraph a paragraph of text with optional character and
5286     *        paragraph attribute information
5287     * @stable ICU 3.8
5288     */
5289    public Bidi(AttributedCharacterIterator paragraph)
5290    {
5291        this();
5292        setPara(paragraph);
5293    }
5294
5295    /**
5296     * Create Bidi from the given text, embedding, and direction information.
5297     * The embeddings array may be null. If present, the values represent
5298     * embedding level information. Negative values from -1 to -61 indicate
5299     * overrides at the absolute value of the level. Positive values from 1 to
5300     * 61 indicate embeddings. Where values are zero, the base embedding level
5301     * as determined by the base direction is assumed.<p>
5302     *
5303     * Note: this constructor calls setPara() internally.
5304     *
5305     * @param text an array containing the paragraph of text to process.
5306     * @param textStart the index into the text array of the start of the
5307     *        paragraph.
5308     * @param embeddings an array containing embedding values for each character
5309     *        in the paragraph. This can be null, in which case it is assumed
5310     *        that there is no external embedding information.
5311     * @param embStart the index into the embedding array of the start of the
5312     *        paragraph.
5313     * @param paragraphLength the length of the paragraph in the text and
5314     *        embeddings arrays.
5315     * @param flags a collection of flags that control the algorithm. The
5316     *        algorithm understands the flags DIRECTION_LEFT_TO_RIGHT,
5317     *        DIRECTION_RIGHT_TO_LEFT, DIRECTION_DEFAULT_LEFT_TO_RIGHT, and
5318     *        DIRECTION_DEFAULT_RIGHT_TO_LEFT. Other values are reserved.
5319     *
5320     * @throws IllegalArgumentException if the values in embeddings are
5321     *         not within the allowed range
5322     *
5323     * @see #DIRECTION_LEFT_TO_RIGHT
5324     * @see #DIRECTION_RIGHT_TO_LEFT
5325     * @see #DIRECTION_DEFAULT_LEFT_TO_RIGHT
5326     * @see #DIRECTION_DEFAULT_RIGHT_TO_LEFT
5327     * @stable ICU 3.8
5328     */
5329    public Bidi(char[] text,
5330            int textStart,
5331            byte[] embeddings,
5332            int embStart,
5333            int paragraphLength,
5334            int flags)
5335    {
5336        this();
5337        byte paraLvl;
5338        switch (flags) {
5339        case DIRECTION_LEFT_TO_RIGHT:
5340        default:
5341            paraLvl = LTR;
5342            break;
5343        case DIRECTION_RIGHT_TO_LEFT:
5344            paraLvl = RTL;
5345            break;
5346        case DIRECTION_DEFAULT_LEFT_TO_RIGHT:
5347            paraLvl = LEVEL_DEFAULT_LTR;
5348            break;
5349        case DIRECTION_DEFAULT_RIGHT_TO_LEFT:
5350            paraLvl = LEVEL_DEFAULT_RTL;
5351            break;
5352        }
5353        byte[] paraEmbeddings;
5354        if (embeddings == null) {
5355            paraEmbeddings = null;
5356        } else {
5357            paraEmbeddings = new byte[paragraphLength];
5358            byte lev;
5359            for (int i = 0; i < paragraphLength; i++) {
5360                lev = embeddings[i + embStart];
5361                if (lev < 0) {
5362                    lev = (byte)((- lev) | LEVEL_OVERRIDE);
5363                } else if (lev == 0) {
5364                    lev = paraLvl;
5365                    if (paraLvl > MAX_EXPLICIT_LEVEL) {
5366                        lev &= 1;
5367                    }
5368                }
5369                paraEmbeddings[i] = lev;
5370            }
5371        }
5372        if (textStart == 0 && embStart == 0 && paragraphLength == text.length) {
5373            setPara(text, paraLvl, paraEmbeddings);
5374        } else {
5375            char[] paraText = new char[paragraphLength];
5376            System.arraycopy(text, textStart, paraText, 0, paragraphLength);
5377            setPara(paraText, paraLvl, paraEmbeddings);
5378        }
5379    }
5380
5381    /**
5382     * Create a Bidi object representing the bidi information on a line of text
5383     * within the paragraph represented by the current Bidi. This call is not
5384     * required if the entire paragraph fits on one line.
5385     *
5386     * @param lineStart the offset from the start of the paragraph to the start
5387     *        of the line.
5388     * @param lineLimit the offset from the start of the paragraph to the limit
5389     *        of the line.
5390     *
5391     * @throws IllegalStateException if this call is not preceded by a successful
5392     *         call to <code>setPara</code>
5393     * @throws IllegalArgumentException if lineStart and lineLimit are not in the range
5394     *         <code>0&lt;=lineStart&lt;lineLimit&lt;=getProcessedLength()</code>,
5395     *         or if the specified line crosses a paragraph boundary
5396     * @stable ICU 3.8
5397     */
5398    public Bidi createLineBidi(int lineStart, int lineLimit)
5399    {
5400        return setLine(lineStart, lineLimit);
5401    }
5402
5403    /**
5404     * Return true if the line is not left-to-right or right-to-left. This means
5405     * it either has mixed runs of left-to-right and right-to-left text, or the
5406     * base direction differs from the direction of the only run of text.
5407     *
5408     * @return true if the line is not left-to-right or right-to-left.
5409     *
5410     * @throws IllegalStateException if this call is not preceded by a successful
5411     *         call to <code>setPara</code>
5412     * @stable ICU 3.8
5413     */
5414    public boolean isMixed()
5415    {
5416        return (!isLeftToRight() && !isRightToLeft());
5417    }
5418
5419    /**
5420     * Return true if the line is all left-to-right text and the base direction
5421     * is left-to-right.
5422     *
5423     * @return true if the line is all left-to-right text and the base direction
5424     *         is left-to-right.
5425     *
5426     * @throws IllegalStateException if this call is not preceded by a successful
5427     *         call to <code>setPara</code>
5428     * @stable ICU 3.8
5429     */
5430    public boolean isLeftToRight()
5431    {
5432        return (getDirection() == LTR && (paraLevel & 1) == 0);
5433    }
5434
5435    /**
5436     * Return true if the line is all right-to-left text, and the base direction
5437     * is right-to-left
5438     *
5439     * @return true if the line is all right-to-left text, and the base
5440     *         direction is right-to-left
5441     *
5442     * @throws IllegalStateException if this call is not preceded by a successful
5443     *         call to <code>setPara</code>
5444     * @stable ICU 3.8
5445     */
5446    public boolean isRightToLeft()
5447    {
5448        return (getDirection() == RTL && (paraLevel & 1) == 1);
5449    }
5450
5451    /**
5452     * Return true if the base direction is left-to-right
5453     *
5454     * @return true if the base direction is left-to-right
5455     *
5456     * @throws IllegalStateException if this call is not preceded by a successful
5457     *         call to <code>setPara</code> or <code>setLine</code>
5458     *
5459     * @stable ICU 3.8
5460     */
5461    public boolean baseIsLeftToRight()
5462    {
5463        return (getParaLevel() == LTR);
5464    }
5465
5466    /**
5467     * Return the base level (0 if left-to-right, 1 if right-to-left).
5468     *
5469     * @return the base level
5470     *
5471     * @throws IllegalStateException if this call is not preceded by a successful
5472     *         call to <code>setPara</code> or <code>setLine</code>
5473     *
5474     * @stable ICU 3.8
5475     */
5476    public int getBaseLevel()
5477    {
5478        return getParaLevel();
5479    }
5480
5481    /**
5482     * Return the number of level runs.
5483     *
5484     * @return the number of level runs
5485     *
5486     * @throws IllegalStateException if this call is not preceded by a successful
5487     *         call to <code>setPara</code> or <code>setLine</code>
5488     *
5489     * @stable ICU 3.8
5490     */
5491    public int getRunCount()
5492    {
5493        return countRuns();
5494    }
5495
5496    /**
5497     * Compute the logical to visual run mapping
5498     */
5499     void getLogicalToVisualRunsMap()
5500     {
5501        if (isGoodLogicalToVisualRunsMap) {
5502            return;
5503        }
5504        int count = countRuns();
5505        if ((logicalToVisualRunsMap == null) ||
5506            (logicalToVisualRunsMap.length < count)) {
5507            logicalToVisualRunsMap = new int[count];
5508        }
5509        int i;
5510        long[] keys = new long[count];
5511        for (i = 0; i < count; i++) {
5512            keys[i] = ((long)(runs[i].start)<<32) + i;
5513        }
5514        Arrays.sort(keys);
5515        for (i = 0; i < count; i++) {
5516            logicalToVisualRunsMap[i] = (int)(keys[i] & 0x00000000FFFFFFFF);
5517        }
5518        isGoodLogicalToVisualRunsMap = true;
5519     }
5520
5521    /**
5522     * Return the level of the nth logical run in this line.
5523     *
5524     * @param run the index of the run, between 0 and <code>countRuns()-1</code>
5525     *
5526     * @return the level of the run
5527     *
5528     * @throws IllegalStateException if this call is not preceded by a successful
5529     *         call to <code>setPara</code> or <code>setLine</code>
5530     * @throws IllegalArgumentException if <code>run</code> is not in
5531     *         the range <code>0&lt;=run&lt;countRuns()</code>
5532     * @stable ICU 3.8
5533     */
5534    public int getRunLevel(int run)
5535    {
5536        verifyValidParaOrLine();
5537        BidiLine.getRuns(this);
5538        verifyRange(run, 0, runCount);
5539        getLogicalToVisualRunsMap();
5540        return runs[logicalToVisualRunsMap[run]].level;
5541    }
5542
5543    /**
5544     * Return the index of the character at the start of the nth logical run in
5545     * this line, as an offset from the start of the line.
5546     *
5547     * @param run the index of the run, between 0 and <code>countRuns()</code>
5548     *
5549     * @return the start of the run
5550     *
5551     * @throws IllegalStateException if this call is not preceded by a successful
5552     *         call to <code>setPara</code> or <code>setLine</code>
5553     * @throws IllegalArgumentException if <code>run</code> is not in
5554     *         the range <code>0&lt;=run&lt;countRuns()</code>
5555     * @stable ICU 3.8
5556     */
5557    public int getRunStart(int run)
5558    {
5559        verifyValidParaOrLine();
5560        BidiLine.getRuns(this);
5561        verifyRange(run, 0, runCount);
5562        getLogicalToVisualRunsMap();
5563        return runs[logicalToVisualRunsMap[run]].start;
5564    }
5565
5566    /**
5567     * Return the index of the character past the end of the nth logical run in
5568     * this line, as an offset from the start of the line. For example, this
5569     * will return the length of the line for the last run on the line.
5570     *
5571     * @param run the index of the run, between 0 and <code>countRuns()</code>
5572     *
5573     * @return the limit of the run
5574     *
5575     * @throws IllegalStateException if this call is not preceded by a successful
5576     *         call to <code>setPara</code> or <code>setLine</code>
5577     * @throws IllegalArgumentException if <code>run</code> is not in
5578     *         the range <code>0&lt;=run&lt;countRuns()</code>
5579     * @stable ICU 3.8
5580     */
5581    public int getRunLimit(int run)
5582    {
5583        verifyValidParaOrLine();
5584        BidiLine.getRuns(this);
5585        verifyRange(run, 0, runCount);
5586        getLogicalToVisualRunsMap();
5587        int idx = logicalToVisualRunsMap[run];
5588        int len = idx == 0 ? runs[idx].limit :
5589                                runs[idx].limit - runs[idx-1].limit;
5590        return runs[idx].start + len;
5591    }
5592
5593    /**
5594     * Return true if the specified text requires bidi analysis. If this returns
5595     * false, the text will display left-to-right. Clients can then avoid
5596     * constructing a Bidi object. Text in the Arabic Presentation Forms area of
5597     * Unicode is presumed to already be shaped and ordered for display, and so
5598     * will not cause this method to return true.
5599     *
5600     * @param text the text containing the characters to test
5601     * @param start the start of the range of characters to test
5602     * @param limit the limit of the range of characters to test
5603     *
5604     * @return true if the range of characters requires bidi analysis
5605     *
5606     * @stable ICU 3.8
5607     */
5608    public static boolean requiresBidi(char[] text,
5609            int start,
5610            int limit)
5611    {
5612        final int RTLMask = (1 << UCharacter.DIRECTIONALITY_RIGHT_TO_LEFT |
5613                1 << UCharacter.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC |
5614                1 << UCharacter.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING |
5615                1 << UCharacter.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE |
5616                1 << UCharacter.DIRECTIONALITY_ARABIC_NUMBER);
5617
5618        for (int i = start; i < limit; ++i) {
5619            if (((1 << UCharacter.getDirection(text[i])) & RTLMask) != 0) {
5620                return true;
5621            }
5622        }
5623        return false;
5624    }
5625
5626    /**
5627     * Reorder the objects in the array into visual order based on their levels.
5628     * This is a utility method to use when you have a collection of objects
5629     * representing runs of text in logical order, each run containing text at a
5630     * single level. The elements at <code>index</code> from
5631     * <code>objectStart</code> up to <code>objectStart + count</code> in the
5632     * objects array will be reordered into visual order assuming
5633     * each run of text has the level indicated by the corresponding element in
5634     * the levels array (at <code>index - objectStart + levelStart</code>).
5635     *
5636     * @param levels an array representing the bidi level of each object
5637     * @param levelStart the start position in the levels array
5638     * @param objects the array of objects to be reordered into visual order
5639     * @param objectStart the start position in the objects array
5640     * @param count the number of objects to reorder
5641     * @stable ICU 3.8
5642     */
5643    public static void reorderVisually(byte[] levels,
5644            int levelStart,
5645            Object[] objects,
5646            int objectStart,
5647            int count)
5648    {
5649        byte[] reorderLevels = new byte[count];
5650        System.arraycopy(levels, levelStart, reorderLevels, 0, count);
5651        int[] indexMap = reorderVisual(reorderLevels);
5652        Object[] temp = new Object[count];
5653        System.arraycopy(objects, objectStart, temp, 0, count);
5654        for (int i = 0; i < count; ++i) {
5655            objects[objectStart + i] = temp[indexMap[i]];
5656        }
5657    }
5658
5659    /**
5660     * Take a <code>Bidi</code> object containing the reordering
5661     * information for a piece of text (one or more paragraphs) set by
5662     * <code>setPara()</code> or for a line of text set by <code>setLine()</code>
5663     * and return a string containing the reordered text.
5664     *
5665     * <p>The text may have been aliased (only a reference was stored
5666     * without copying the contents), thus it must not have been modified
5667     * since the <code>setPara()</code> call.</p>
5668     *
5669     * This method preserves the integrity of characters with multiple
5670     * code units and (optionally) combining characters.
5671     * Characters in RTL runs can be replaced by mirror-image characters
5672     * in the returned string. Note that "real" mirroring has to be done in a
5673     * rendering engine by glyph selection and that for many "mirrored"
5674     * characters there are no Unicode characters as mirror-image equivalents.
5675     * There are also options to insert or remove Bidi control
5676     * characters; see the descriptions of the return value and the
5677     * <code>options</code> parameter, and of the option bit flags.
5678     *
5679     * @param options A bit set of options for the reordering that control
5680     *                how the reordered text is written.
5681     *                The options include mirroring the characters on a code
5682     *                point basis and inserting LRM characters, which is used
5683     *                especially for transforming visually stored text
5684     *                to logically stored text (although this is still an
5685     *                imperfect implementation of an "inverse Bidi" algorithm
5686     *                because it uses the "forward Bidi" algorithm at its core).
5687     *                The available options are:
5688     *                <code>DO_MIRRORING</code>,
5689     *                <code>INSERT_LRM_FOR_NUMERIC</code>,
5690     *                <code>KEEP_BASE_COMBINING</code>,
5691     *                <code>OUTPUT_REVERSE</code>,
5692     *                <code>REMOVE_BIDI_CONTROLS</code>,
5693     *                <code>STREAMING</code>
5694     *
5695     * @return The reordered text.
5696     *         If the <code>INSERT_LRM_FOR_NUMERIC</code> option is set, then
5697     *         the length of the returned string could be as large as
5698     *         <code>getLength()+2*countRuns()</code>.<br>
5699     *         If the <code>REMOVE_BIDI_CONTROLS</code> option is set, then the
5700     *         length of the returned string may be less than
5701     *         <code>getLength()</code>.<br>
5702     *         If none of these options is set, then the length of the returned
5703     *         string will be exactly <code>getProcessedLength()</code>.
5704     *
5705     * @throws IllegalStateException if this call is not preceded by a successful
5706     *         call to <code>setPara</code> or <code>setLine</code>
5707     *
5708     * @see #DO_MIRRORING
5709     * @see #INSERT_LRM_FOR_NUMERIC
5710     * @see #KEEP_BASE_COMBINING
5711     * @see #OUTPUT_REVERSE
5712     * @see #REMOVE_BIDI_CONTROLS
5713     * @see #OPTION_STREAMING
5714     * @see #getProcessedLength
5715     * @stable ICU 3.8
5716     */
5717    public String writeReordered(int options)
5718    {
5719        verifyValidParaOrLine();
5720        if (length == 0) {
5721            /* nothing to do */
5722            return "";
5723        }
5724        return BidiWriter.writeReordered(this, options);
5725    }
5726
5727    /**
5728     * Reverse a Right-To-Left run of Unicode text.
5729     *
5730     * This method preserves the integrity of characters with multiple
5731     * code units and (optionally) combining characters.
5732     * Characters can be replaced by mirror-image characters
5733     * in the destination buffer. Note that "real" mirroring has
5734     * to be done in a rendering engine by glyph selection
5735     * and that for many "mirrored" characters there are no
5736     * Unicode characters as mirror-image equivalents.
5737     * There are also options to insert or remove Bidi control
5738     * characters.
5739     *
5740     * This method is the implementation for reversing RTL runs as part
5741     * of <code>writeReordered()</code>. For detailed descriptions
5742     * of the parameters, see there.
5743     * Since no Bidi controls are inserted here, the output string length
5744     * will never exceed <code>src.length()</code>.
5745     *
5746     * @see #writeReordered
5747     *
5748     * @param src The RTL run text.
5749     *
5750     * @param options A bit set of options for the reordering that control
5751     *                how the reordered text is written.
5752     *                See the <code>options</code> parameter in <code>writeReordered()</code>.
5753     *
5754     * @return The reordered text.
5755     *         If the <code>REMOVE_BIDI_CONTROLS</code> option
5756     *         is set, then the length of the returned string may be less than
5757     *         <code>src.length()</code>. If this option is not set,
5758     *         then the length of the returned string will be exactly
5759     *         <code>src.length()</code>.
5760     *
5761     * @throws IllegalArgumentException if <code>src</code> is null.
5762     * @stable ICU 3.8
5763     */
5764    public static String writeReverse(String src, int options)
5765    {
5766        /* error checking */
5767        if (src == null) {
5768            throw new IllegalArgumentException();
5769        }
5770
5771        if (src.length() > 0) {
5772            return BidiWriter.writeReverse(src, options);
5773        } else {
5774            /* nothing to do */
5775            return "";
5776        }
5777    }
5778
5779}
5780