public static byte getBaseDirection ( CharSequence paragraph ) { if ( paragraph == null || paragraph . length ( ) == 0 ) { return NEUTRAL ; } int length = paragraph . length ( ) ; int c ; // codepoint byte direction ; for ( int i = 0 ; i < length ; ) { // U16_NEXT(paragraph, i, length, c) for C++ c = UCharacter . codePointAt ( paragraph , i ) ; direction = UCharacter . getDirectionality ( c ) ; if ( direction == UCharacterDirection . LEFT_TO_RIGHT ) { return LTR ; } else if ( direction == UCharacterDirection . RIGHT_TO_LEFT || direction == UCharacterDirection . RIGHT_TO_LEFT_ARABIC ) { return RTL ; } i = UCharacter . offsetByCodePoints ( paragraph , i , 1 ) ; // set i to the head index of next codepoint } return NEUTRAL ; }
Get the base direction of the text provided according to the Unicode Bidirectional Algorithm . The base direction is derived from the first character in the string with bidirectional character type L R or AL . If the first such character has type L LTR is returned . If the first such character has type R or AL RTL is returned . If the string does not contain any character of these types then NEUTRAL is returned . This is a lightweight function for use when only the base direction is needed and no further bidi processing of the text is needed .