Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Bidi

  1. Object
  2. Bidi

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

Fields

public static final int LTR = 0Left to right: a paragraph direction, and the answer of direction(String).
public static final int RTL = 1Right to left: a paragraph direction, and the answer of direction(String).
public static final int NEUTRAL = -1The answer of direction(String) for text without a strong character.

Methods

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.
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.
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.
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).
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.
public static char mirror(char c)The mirror image of c for right-to-left display (L4), such as ) for (, or c itself.
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.
public static int[] visualOrder(byte[] levels)Applies reordering rule L2 to produce the visual order of the characters.

Inherited methods

Field details

LTR

public static final int LTR = 0
Left to right: a paragraph direction, and the answer of direction(String).

RTL

public static final int RTL = 1
Right to left: a paragraph direction, and the answer of direction(String).

NEUTRAL

public static final int NEUTRAL = -1
The answer of direction(String) for text without a strong character.

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).