Package java.text
Class Bidi
java.lang.Object
java.text.Bidi
public final class Bidi extends Object
Implements the Unicode Bidirectional Algorithm.
Use a Bidi object to get the information on the position reordering of a
bidirectional text, such as Arabic or Hebrew. The natural display ordering of
horizontal text in these languages is from right to left, while they order
numbers from left to right.
If the text contains multiple runs, the information of each run can be obtained from the run index. The level of any particular run indicates the direction of the text as well as the nesting level. Left-to-right runs have even levels while right-to-left runs have odd levels.
-
Field Summary
Fields Modifier and Type Field Description static intDIRECTION_DEFAULT_LEFT_TO_RIGHTConstant that indicates the default base level.static intDIRECTION_DEFAULT_RIGHT_TO_LEFTConstant that indicates the default base level.static intDIRECTION_LEFT_TO_RIGHTConstant that specifies the default base level as 0 (left-to-right).static intDIRECTION_RIGHT_TO_LEFTConstant that specifies the default base level as 1 (right-to-left). -
Constructor Summary
Constructors Constructor Description Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags)Creates aBidiobject.Bidi(String paragraph, int flags)Creates aBidiobject.Bidi(AttributedCharacterIterator paragraph)Creates aBidiobject from theAttributedCharacterIteratorof a paragraph text. -
Method Summary
Modifier and Type Method Description booleanbaseIsLeftToRight()Returns whether the base level is from left to right.BidicreateLineBidi(int lineStart, int lineLimit)Creates a newBidiobject containing the information of one line from this object.intgetBaseLevel()Returns the base level.intgetLength()Returns the length of the text in theBidiobject.intgetLevelAt(int offset)Returns the level of a specified character.intgetRunCount()Returns the number of runs in the bidirectional text.intgetRunLevel(int run)Returns the level of the specified run.intgetRunLimit(int run)Returns the limit offset of the specified run.intgetRunStart(int run)Returns the start offset of the specified run.booleanisLeftToRight()Indicates whether the text is from left to right, that is, both the base direction and the text direction is from left to right.booleanisMixed()Indicates whether the text direction is mixed.booleanisRightToLeft()Indicates whether the text is from right to left, that is, both the base direction and the text direction is from right to left.static voidreorderVisually(byte[] levels, int levelStart, Object[] objects, int objectStart, int count)Reorders a range of objects according to their specified levels.static booleanrequiresBidi(char[] text, int start, int limit)Indicates whether a range of characters of a text requires aBidiobject to display properly.StringtoString()Returns a string containing a concise, human-readable description of this object.
-
Field Details
-
DIRECTION_DEFAULT_LEFT_TO_RIGHT
public static final int DIRECTION_DEFAULT_LEFT_TO_RIGHTConstant that indicates the default base level. If there is no strong character, then set the paragraph level to 0 (left-to-right).- See Also:
- Constant Field Values
-
DIRECTION_DEFAULT_RIGHT_TO_LEFT
public static final int DIRECTION_DEFAULT_RIGHT_TO_LEFTConstant that indicates the default base level. If there is no strong character, then set the paragraph level to 1 (right-to-left).- See Also:
- Constant Field Values
-
DIRECTION_LEFT_TO_RIGHT
public static final int DIRECTION_LEFT_TO_RIGHTConstant that specifies the default base level as 0 (left-to-right).- See Also:
- Constant Field Values
-
DIRECTION_RIGHT_TO_LEFT
public static final int DIRECTION_RIGHT_TO_LEFTConstant that specifies the default base level as 1 (right-to-left).- See Also:
- Constant Field Values
-
-
Constructor Details
-
Bidi
Creates aBidiobject from theAttributedCharacterIteratorof a paragraph text. The RUN_DIRECTION attribute determines the base direction of the bidirectional text. If it is not specified explicitly, the algorithm uses DIRECTION_DEFAULT_LEFT_TO_RIGHT by default. The BIDI_EMBEDDING attribute specifies the level of embedding for each character. Values between -1 and -62 denote overrides at the level's absolute value, values from 1 to 62 indicate embeddings, and the 0 value indicates the level is calculated by the algorithm automatically. For the character with no BIDI_EMBEDDING attribute or with a improper attribute value, such as anullvalue, the algorithm treats its embedding level as 0. The NUMERIC_SHAPING attribute specifies the instance of NumericShaper used to convert European digits to other decimal digits before performing the bidi algorithm.- Parameters:
paragraph- the String containing the paragraph text to perform the algorithm.- Throws:
IllegalArgumentException- ifparagraph == null- See Also:
TextAttribute.BIDI_EMBEDDING,TextAttribute.NUMERIC_SHAPING,TextAttribute.RUN_DIRECTION
-
Bidi
public Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags)Creates aBidiobject.- Parameters:
text- the char array of the paragraph text that is processed.textStart- the index intextof the start of the paragraph.embeddings- the embedding level array of the paragraph text, specifying the embedding level information for each character. Values between -1 and -61 denote overrides at the level's absolute value, values from 1 to 61 indicate embeddings, and the 0 value indicates the level is calculated by the algorithm automatically.embStart- the index inembeddingsof the start of the paragraph.paragraphLength- the length of the text to perform the algorithm.flags- indicates the base direction of the bidirectional text. It is expected that this will be one of the direction constant values defined in this class. An unknown value is treated as DIRECTION_DEFAULT_LEFT_TO_RIGHT.- Throws:
IllegalArgumentException- iftextStart,embStart, orparagraphLengthis negative; iftext.length < textStart + paragraphLengthorembeddings.length < embStart + paragraphLength.- See Also:
DIRECTION_LEFT_TO_RIGHT,DIRECTION_RIGHT_TO_LEFT,DIRECTION_DEFAULT_RIGHT_TO_LEFT,DIRECTION_DEFAULT_LEFT_TO_RIGHT
-
Bidi
Creates aBidiobject.- Parameters:
paragraph- the string containing the paragraph text to perform the algorithm on.flags- indicates the base direction of the bidirectional text. It is expected that this will be one of the direction constant values defined in this class. An unknown value is treated as DIRECTION_DEFAULT_LEFT_TO_RIGHT.- See Also:
DIRECTION_LEFT_TO_RIGHT,DIRECTION_RIGHT_TO_LEFT,DIRECTION_DEFAULT_RIGHT_TO_LEFT,DIRECTION_DEFAULT_LEFT_TO_RIGHT
-
-
Method Details
-
baseIsLeftToRight
public boolean baseIsLeftToRight()Returns whether the base level is from left to right.- Returns:
- true if the base level is from left to right.
-
createLineBidi
Creates a newBidiobject containing the information of one line from this object.- Parameters:
lineStart- the start offset of the line.lineLimit- the limit of the line.- Returns:
- the new line Bidi object. In this new object, the indices will range from 0 to (limit - start - 1).
- Throws:
IllegalArgumentException- iflineStart < 0,lineLimit < 0,lineStart > lineLimitor iflineStartis greater than the length of this object's paragraph text.
-
getBaseLevel
public int getBaseLevel()Returns the base level.- Returns:
- the base level.
-
getLength
public int getLength()Returns the length of the text in theBidiobject.- Returns:
- the length.
-
getLevelAt
public int getLevelAt(int offset)Returns the level of a specified character.- Parameters:
offset- the offset of the character.- Returns:
- the level.
-
getRunCount
public int getRunCount()Returns the number of runs in the bidirectional text.- Returns:
- the number of runs, at least 1.
-
getRunLevel
public int getRunLevel(int run)Returns the level of the specified run.- Parameters:
run- the index of the run.- Returns:
- the level of the run.
-
getRunLimit
public int getRunLimit(int run)Returns the limit offset of the specified run.- Parameters:
run- the index of the run.- Returns:
- the limit offset of the run.
-
getRunStart
public int getRunStart(int run)Returns the start offset of the specified run.- Parameters:
run- the index of the run.- Returns:
- the start offset of the run.
-
isLeftToRight
public boolean isLeftToRight()Indicates whether the text is from left to right, that is, both the base direction and the text direction is from left to right.- Returns:
trueif the text is from left to right;falseotherwise.
-
isMixed
public boolean isMixed()Indicates whether the text direction is mixed.- Returns:
trueif the text direction is mixed;falseotherwise.
-
isRightToLeft
public boolean isRightToLeft()Indicates whether the text is from right to left, that is, both the base direction and the text direction is from right to left.- Returns:
trueif the text is from right to left;falseotherwise.
-
reorderVisually
public static void reorderVisually(byte[] levels, int levelStart, Object[] objects, int objectStart, int count)Reorders a range of objects according to their specified levels. This is a convenience function that does not use aBidiobject. The range of objects atindexfromobjectStarttoobjectStart + countwill be reordered according to the range of levels atindexfromlevelStarttolevelStart + count.- Parameters:
levels- the level array, which is already determined.levelStart- the start offset of the range of the levels.objects- the object array to reorder.objectStart- the start offset of the range of objects.count- the count of the range of objects to reorder.- Throws:
IllegalArgumentException- ifcount,levelStartorobjectStartis negative; ifcount > levels.length - levelStartor ifcount > objects.length - objectStart.
-
requiresBidi
public static boolean requiresBidi(char[] text, int start, int limit)Indicates whether a range of characters of a text requires aBidiobject to display properly.- Parameters:
text- the char array of the text.start- the start offset of the range of characters.limit- the limit offset of the range of characters.- Returns:
trueif the range of characters requires aBidiobject;falseotherwise.- Throws:
IllegalArgumentException- ifstartorlimitis negative;start > limitorlimitis greater than the length of this object's paragraph text.
-
toString
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-