Get a
BidiRun
object according to its index. BidiRun methods
may be used to retrieve the run's logical start, length and level,
which can be even for an LTR run or odd for an RTL run.
In an RTL run, the character at the logical start is
visually on the right of the displayed run.
The length is the number of characters in the run.
countRuns()
is normally called
before the runs are retrieved.
Example:
Bidi bidi = new Bidi();
String text = "abc 123 DEFG xyz";
bidi.setPara(text, Bidi.RTL, null);
int i, count=bidi.countRuns(), logicalStart, visualIndex=0, length;
BidiRun run;
for (i = 0; i < count; ++i) {
run = bidi.getVisualRun(i);
logicalStart = run.getStart();
length = run.getLength();
if (Bidi.LTR == run.getEmbeddingLevel()) {
do { // LTR
show_char(text.charAt(logicalStart++), visualIndex++);
} while (--length > 0);
} else {
logicalStart += length; // logicalLimit
do { // RTL
show_char(text.charAt(--logicalStart), visualIndex++);
} while (--length > 0);
}
}
Note that in right-to-left runs, code like this places
second surrogates before first ones (which is generally a bad idea)
and combining characters before base characters.
Use of
#writeReordered
, optionally with the
#KEEP_BASE_COMBINING
option, can be considered in
order to avoid these issues.