Class Bidi

java.lang.Object
com.codename1.util.Bidi

public final class Bidi extends Object

The Unicode Bidirectional Algorithm (UAX #9): the embedding level of every character of a paragraph of mixed left-to-right and right-to-left text, and the order the characters are displayed in.

Every resolution phase is implemented: character types, explicit embeddings, overrides and isolates (X1-X10), the weak types (W1-W7), paired brackets (N0), the neutrals (N1-N2), the implicit levels (I1-I2), the separator and trailing white space reset (L1), reordering (L2) and mirroring (L4). Character types come from compact range tables rather than the full Unicode database. Shaping (Arabic joining, ligatures) is not part of the algorithm and is left to whoever draws the text.

String line = "abc אבג 123";
byte[] levels = Bidi.levels(line, Bidi.direction(line) == Bidi.RTL);
int[] order = Bidi.visualOrder(levels);
// order[v] is the index in line of the character drawn v-th from the left
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Left to right: a paragraph direction, and the answer of direction(String).
    static final int
    The answer of direction(String) for text without a strong character.
    static final int
    Right to left: a paragraph direction, and the answer of direction(String).
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    The direction of the first strong character of text outside isolates (P2, P3): LTR, RTL, or NEUTRAL when it has none.
    static boolean
    isLeftToRight(String text, boolean rtl)
    True when text, in a paragraph laid out right to left when rtl is true, displays in its logical order with no reordering: the fast path for the common case.
    static boolean
    isMark(char c)
    True when c is a nonspacing mark (NSM), such as a Hebrew point or an Arabic vowel sign, which takes the direction of the character it follows.
    static byte[]
    levels(String text, boolean rtl)
    The resolved embedding level of every character of the paragraph text, laid out right to left when rtl is true: even levels are left to right, odd ones right to left.
    static char
    mirror(char c)
    The mirror image of c for right-to-left display (L4), such as ) for (, or c itself.
    static String
    reorder(String text, boolean rtl)
    text in display order, left to right, for a paragraph laid out right to left when rtl is true, with the characters of right-to-left runs mirrored.
    static int
    The direction of c when it is a strong character: LTR, RTL, or NEUTRAL for anything else (digits, punctuation, white space, marks).
    static int[]
    visualOrder(byte[] levels)
    Applies reordering rule L2 to produce the visual order of the characters.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • levels

      public static byte[] levels(String text, boolean rtl)
      The resolved embedding level of every character of the paragraph text, laid out right to left when rtl is true: even levels are left to right, odd ones right to left. Characters removed by X9 (the explicit formatting characters) take the level of the preceding character, so a caller can still draw them.
    • isLeftToRight

      public static boolean isLeftToRight(String text, boolean rtl)
      True when text, in a paragraph laid out right to left when rtl is true, displays in its logical order with no reordering: the fast path for the common case.
    • direction

      public static int direction(String text)
      The direction of the first strong character of text outside isolates (P2, P3): LTR, RTL, or NEUTRAL when it has none.
    • strongDirection

      public static int strongDirection(char c)
      The direction of c when it is a strong character: LTR, RTL, or NEUTRAL for anything else (digits, punctuation, white space, marks).
    • isMark

      public static boolean isMark(char c)
      True when c is a nonspacing mark (NSM), such as a Hebrew point or an Arabic vowel sign, which takes the direction of the character it follows.
    • mirror

      public static char mirror(char c)
      The mirror image of c for right-to-left display (L4), such as ) for (, or c itself.
    • reorder

      public static String reorder(String text, boolean rtl)
      text in display order, left to right, for a paragraph laid out right to left when rtl is true, with the characters of right-to-left runs mirrored. A line without explicit formatting characters is its own inverse, so this also restores the logical order of text that was captured in display order.
    • visualOrder

      public static int[] visualOrder(byte[] levels)
      Applies reordering rule L2 to produce the visual order of the characters. Returns an array visualToLogical where element v is the logical index of the character drawn at visual position v (left to right on screen).