Class Bidi
java.lang.Object
com.codename1.util.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
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intLeft to right: a paragraph direction, and the answer ofdirection(String).static final intThe answer ofdirection(String)for text without a strong character.static final intRight to left: a paragraph direction, and the answer ofdirection(String). -
Method Summary
Modifier and TypeMethodDescriptionstatic intstatic booleanisLeftToRight(String text, boolean rtl) True whentext, in a paragraph laid out right to left whenrtlis true, displays in its logical order with no reordering: the fast path for the common case.static booleanisMark(char c) True whencis 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[]The resolved embedding level of every character of the paragraphtext, laid out right to left whenrtlis true: even levels are left to right, odd ones right to left.static charmirror(char c) The mirror image ofcfor right-to-left display (L4), such as)for(, orcitself.static Stringtextin display order, left to right, for a paragraph laid out right to left whenrtlis true, with the characters of right-to-left runs mirrored.static intstrongDirection(char c) static int[]visualOrder(byte[] levels) Applies reordering rule L2 to produce the visual order of the characters.
-
Field Details
-
LTR
public static final int LTRLeft to right: a paragraph direction, and the answer ofdirection(String).- See Also:
-
RTL
public static final int RTLRight to left: a paragraph direction, and the answer ofdirection(String).- See Also:
-
NEUTRAL
public static final int NEUTRALThe answer ofdirection(String)for text without a strong character.- See Also:
-
-
Method Details
-
levels
The resolved embedding level of every character of the paragraphtext, laid out right to left whenrtlis 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
True whentext, in a paragraph laid out right to left whenrtlis true, displays in its logical order with no reordering: the fast path for the common case. -
direction
-
strongDirection
-
isMark
public static boolean isMark(char c) True whencis 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 ofcfor right-to-left display (L4), such as)for(, orcitself. -
reorder
textin display order, left to right, for a paragraph laid out right to left whenrtlis 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 arrayvisualToLogicalwhere elementvis the logical index of the character drawn at visual positionv(left to right on screen).
-