Class BidiOrder
- java.lang.Object
-
- com.aowagie.text.pdf.BidiOrder
-
final class BidiOrder extends java.lang.Object
Reference implementation of the Unicode 3.0 Bidi algorithm.This implementation is not optimized for performance. It is intended as a reference implementation that closely follows the specification of the Bidirectional Algorithm in The Unicode Standard version 3.0.
Input:
There are two levels of input to the algorithm, since clients may prefer to supply some information from out-of-band sources rather than relying on the default behavior.- unicode type array
- unicode type array, with externally supplied base line direction
Output:
Output is separated into several stages as well, to better enable clients to evaluate various aspects of implementation conformance.- levels array over entire paragraph
- reordering array over entire paragraph
- levels array over line
- reordering array over line
As the algorithm is defined to operate on a single paragraph at a time, this implementation is written to handle single paragraphs. Thus rule P1 is presumed by this implementation-- the data provided to the implementation is assumed to be a single paragraph, and either contains no 'B' codes, or a single 'B' code at the end of the input. 'B' is allowed as input to illustrate how the algorithm assigns it a level.
Also note that rules L3 and L4 depend on the rendering engine that uses the result of the bidi algorithm. This implementation assumes that the rendering engine expects combining marks in visual order (e.g. to the left of their base character in RTL runs) and that it adjust the glyphs used to render mirrored characters that are in RTL runs so that they render appropriately.
-
-
Field Summary
Fields Modifier and Type Field Description static byte
AL
Right-to-Left Arabicprivate static byte
AN
Arabic Numberprivate static byte
B
Paragraph Separatorprivate static char[]
baseTypes
private static byte
BN
Boundary Neutralprivate static byte
CS
Common Number Separatorprivate byte[]
embeddings
static byte
EN
European Numberprivate static byte
ES
European Number Separatorprivate static byte
ET
European Number Terminatorprivate byte[]
initialTypes
static byte
L
Left-to-rightprivate static byte
LRE
Left-to-Right Embeddingprivate static byte
LRO
Left-to-Right Overrideprivate static byte
NSM
Non-Spacing Markprivate static byte
ON
Other Neutralsprivate byte
paragraphEmbeddingLevel
private static byte
PDF
Pop Directional Formatstatic byte
R
Right-to-Leftprivate byte[]
resultLevels
private byte[]
resultTypes
private static byte
RLE
Right-to-Left Embeddingprivate static byte
RLO
Right-to-Left Overrideprivate static byte[]
rtypes
private static byte
S
Segment Separatorprivate int
textLength
private static byte
TYPE_MAX
Maximum bidi type value.private static byte
TYPE_MIN
Minimum bidi type value.private static byte
WS
Whitespace
-
Constructor Summary
Constructors Constructor Description BidiOrder(char[] text, int offset, int length, byte paragraphEmbeddingLevel)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int[]
computeMultilineReordering(byte[] levels, int[] linebreaks)
Return multiline reordering array for a given level array.private static int[]
computeReordering(byte[] levels)
Return reordering array for a given level array.private void
determineExplicitEmbeddingLevels()
Process embedding format codes.private void
determineParagraphEmbeddingLevel()
1) determining the paragraph level.private int
findRunLimit(int index, int limit, byte[] validSet)
Return the limit of the run starting at index that includes only resultTypes in validSet.private int
findRunStart(int index, byte[] validSet)
Return the start of the run including index that includes only resultTypes in validSet.byte
getBaseLevel()
Return the base level of the paragraph.(package private) static byte
getDirection(char c)
byte[]
getLevels()
private byte[]
getLevels(int[] linebreaks)
Return levels array breaking lines at offsets in linebreaks.private static boolean
isWhitespace(byte biditype)
Return true if the type is considered a whitespace type for the line break rules.private static byte[]
processEmbeddings(byte[] resultTypes, byte paragraphEmbeddingLevel)
2) determining explicit levels Rules X1 - X8 The interaction of these rules makes handling them a bit complex.private int
reinsertExplicitCodes(int textLength)
Reinsert levels information for explicit codes.private int
removeExplicitCodes()
Rules X9.private void
resolveImplicitLevels(int start, int limit, byte level, byte sor, byte eor)
7) resolving implicit embedding levels Rules I1, I2.private void
resolveNeutralTypes(int start, int limit, byte level, byte sor, byte eor)
6) resolving neutral types Rules N1-N2.private void
resolveWeakTypes(int start, int limit, byte level, byte sor, byte eor)
3) resolving weak types Rules W1-W7.private void
runAlgorithm()
The algorithm.private void
setLevels(int start, int limit, byte newLevel)
Set resultLevels from start up to (but not including) limit to newLevel.private void
setTypes(int start, int limit, byte newType)
Set resultTypes from start up to (but not including) limit to newType.private static byte
typeForLevel(int level)
Return the strong type (L or R) corresponding to the level.private static void
validateLineBreaks(int[] linebreaks, int textLength)
Throw exception if line breaks array is invalid.private static void
validateParagraphEmbeddingLevel(byte paragraphEmbeddingLevel)
Throw exception if paragraph embedding level is invalid.private static void
validateTypes(byte[] types)
Throw exception if type array is invalid.
-
-
-
Field Detail
-
initialTypes
private final byte[] initialTypes
-
embeddings
private byte[] embeddings
-
paragraphEmbeddingLevel
private byte paragraphEmbeddingLevel
-
textLength
private int textLength
-
resultTypes
private byte[] resultTypes
-
resultLevels
private byte[] resultLevels
-
L
public static final byte L
Left-to-right- See Also:
- Constant Field Values
-
LRE
private static final byte LRE
Left-to-Right Embedding- See Also:
- Constant Field Values
-
LRO
private static final byte LRO
Left-to-Right Override- See Also:
- Constant Field Values
-
R
public static final byte R
Right-to-Left- See Also:
- Constant Field Values
-
AL
public static final byte AL
Right-to-Left Arabic- See Also:
- Constant Field Values
-
RLE
private static final byte RLE
Right-to-Left Embedding- See Also:
- Constant Field Values
-
RLO
private static final byte RLO
Right-to-Left Override- See Also:
- Constant Field Values
-
PDF
private static final byte PDF
Pop Directional Format- See Also:
- Constant Field Values
-
EN
public static final byte EN
European Number- See Also:
- Constant Field Values
-
ES
private static final byte ES
European Number Separator- See Also:
- Constant Field Values
-
ET
private static final byte ET
European Number Terminator- See Also:
- Constant Field Values
-
AN
private static final byte AN
Arabic Number- See Also:
- Constant Field Values
-
CS
private static final byte CS
Common Number Separator- See Also:
- Constant Field Values
-
NSM
private static final byte NSM
Non-Spacing Mark- See Also:
- Constant Field Values
-
BN
private static final byte BN
Boundary Neutral- See Also:
- Constant Field Values
-
B
private static final byte B
Paragraph Separator- See Also:
- Constant Field Values
-
S
private static final byte S
Segment Separator- See Also:
- Constant Field Values
-
WS
private static final byte WS
Whitespace- See Also:
- Constant Field Values
-
ON
private static final byte ON
Other Neutrals- See Also:
- Constant Field Values
-
TYPE_MIN
private static final byte TYPE_MIN
Minimum bidi type value.- See Also:
- Constant Field Values
-
TYPE_MAX
private static final byte TYPE_MAX
Maximum bidi type value.- See Also:
- Constant Field Values
-
rtypes
private static final byte[] rtypes
-
baseTypes
private static char[] baseTypes
-
-
Method Detail
-
getDirection
static final byte getDirection(char c)
-
runAlgorithm
private void runAlgorithm()
The algorithm. Does not include line-based processing (Rules L1, L2). These are applied later in the line-based phase of the algorithm.
-
determineParagraphEmbeddingLevel
private void determineParagraphEmbeddingLevel()
1) determining the paragraph level.Rules P2, P3.
At the end of this function, the member variable paragraphEmbeddingLevel is set to either 0 or 1.
-
determineExplicitEmbeddingLevels
private void determineExplicitEmbeddingLevels()
Process embedding format codes.Calls processEmbeddings to generate an embedding array from the explicit format codes. The embedding overrides in the array are then applied to the result types, and the result levels are initialized.
- See Also:
processEmbeddings(byte[], byte)
-
removeExplicitCodes
private int removeExplicitCodes()
Rules X9. Remove explicit codes so that they may be ignored during the remainder of the main portion of the algorithm. The length of the resulting text is returned.- Returns:
- the length of the data excluding explicit codes and BN.
-
reinsertExplicitCodes
private int reinsertExplicitCodes(int textLength)
Reinsert levels information for explicit codes. This is for ease of relating the level information to the original input data. Note that the levels assigned to these codes are arbitrary, they're chosen so as to avoid breaking level runs.- Parameters:
textLength
- the length of the data after compression- Returns:
- the length of the data (original length of types array supplied to constructor)
-
processEmbeddings
private static byte[] processEmbeddings(byte[] resultTypes, byte paragraphEmbeddingLevel)
2) determining explicit levels Rules X1 - X8 The interaction of these rules makes handling them a bit complex. This examines resultTypes but does not modify it. It returns embedding and override information in the result array. The low 7 bits are the level, the high bit is set if the level is an override, and clear if it is an embedding.
-
resolveWeakTypes
private void resolveWeakTypes(int start, int limit, byte level, byte sor, byte eor)
3) resolving weak types Rules W1-W7. Note that some weak types (EN, AN) remain after this processing is complete.
-
resolveNeutralTypes
private void resolveNeutralTypes(int start, int limit, byte level, byte sor, byte eor)
6) resolving neutral types Rules N1-N2.
-
resolveImplicitLevels
private void resolveImplicitLevels(int start, int limit, byte level, byte sor, byte eor)
7) resolving implicit embedding levels Rules I1, I2.
-
getLevels
public byte[] getLevels()
-
getLevels
private byte[] getLevels(int[] linebreaks)
Return levels array breaking lines at offsets in linebreaks.
Rule L1.The returned levels array contains the resolved level for each bidi code passed to the constructor.
The linebreaks array must include at least one value. The values must be in strictly increasing order (no duplicates) between 1 and the length of the text, inclusive. The last value must be the length of the text.
- Parameters:
linebreaks
- the offsets at which to break the paragraph- Returns:
- the resolved levels of the text
-
computeMultilineReordering
private static int[] computeMultilineReordering(byte[] levels, int[] linebreaks)
Return multiline reordering array for a given level array. Reordering does not occur across a line break.
-
computeReordering
private static int[] computeReordering(byte[] levels)
Return reordering array for a given level array. This reorders a single line. The reordering is a visual to logical map. For example, the leftmost char is string.charAt(order[0]). Rule L2.
-
getBaseLevel
public byte getBaseLevel()
Return the base level of the paragraph.
-
isWhitespace
private static boolean isWhitespace(byte biditype)
Return true if the type is considered a whitespace type for the line break rules.
-
typeForLevel
private static byte typeForLevel(int level)
Return the strong type (L or R) corresponding to the level.
-
findRunLimit
private int findRunLimit(int index, int limit, byte[] validSet)
Return the limit of the run starting at index that includes only resultTypes in validSet. This checks the value at index, and will return index if that value is not in validSet.
-
findRunStart
private int findRunStart(int index, byte[] validSet)
Return the start of the run including index that includes only resultTypes in validSet. This assumes the value at index is valid, and does not check it.
-
setTypes
private void setTypes(int start, int limit, byte newType)
Set resultTypes from start up to (but not including) limit to newType.
-
setLevels
private void setLevels(int start, int limit, byte newLevel)
Set resultLevels from start up to (but not including) limit to newLevel.
-
validateTypes
private static void validateTypes(byte[] types)
Throw exception if type array is invalid.
-
validateParagraphEmbeddingLevel
private static void validateParagraphEmbeddingLevel(byte paragraphEmbeddingLevel)
Throw exception if paragraph embedding level is invalid. Special allowance for -1 so that default processing can still be performed when using this API.
-
validateLineBreaks
private static void validateLineBreaks(int[] linebreaks, int textLength)
Throw exception if line breaks array is invalid.
-
-