Path: blob/master/src/java.base/share/classes/java/text/Bidi.java
41152 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright IBM Corp. 1999-2003 - All Rights Reserved27*28* The original version of this source code and documentation is29* copyrighted and owned by IBM. These materials are provided30* under terms of a License Agreement between IBM and Sun.31* This technology is protected by multiple US and International32* patents. This notice and attribution to IBM may not be removed.33*/3435package java.text;3637import jdk.internal.icu.text.BidiBase;3839/**40* This class implements the Unicode Bidirectional Algorithm.41* <p>42* A Bidi object provides information on the bidirectional reordering of the text43* used to create it. This is required, for example, to properly display Arabic44* or Hebrew text. These languages are inherently mixed directional, as they order45* numbers from left-to-right while ordering most other text from right-to-left.46* <p>47* Once created, a Bidi object can be queried to see if the text it represents is48* all left-to-right or all right-to-left. Such objects are very lightweight and49* this text is relatively easy to process.50* <p>51* If there are multiple runs of text, information about the runs can be accessed52* by indexing to get the start, limit, and level of a run. The level represents53* both the direction and the 'nesting level' of a directional run. Odd levels54* are right-to-left, while even levels are left-to-right. So for example level55* 0 represents left-to-right text, while level 1 represents right-to-left text, and56* level 2 represents left-to-right text embedded in a right-to-left run.57*58* @since 1.459*/60public final class Bidi {6162/** Constant indicating base direction is left-to-right. */63public static final int DIRECTION_LEFT_TO_RIGHT = 0;6465/** Constant indicating base direction is right-to-left. */66public static final int DIRECTION_RIGHT_TO_LEFT = 1;6768/**69* Constant indicating that the base direction depends on the first strong70* directional character in the text according to the Unicode71* Bidirectional Algorithm. If no strong directional character is present,72* the base direction is left-to-right.73*/74public static final int DIRECTION_DEFAULT_LEFT_TO_RIGHT = -2;7576/**77* Constant indicating that the base direction depends on the first strong78* directional character in the text according to the Unicode79* Bidirectional Algorithm. If no strong directional character is present,80* the base direction is right-to-left.81*/82public static final int DIRECTION_DEFAULT_RIGHT_TO_LEFT = -1;8384private BidiBase bidiBase;8586/**87* Create Bidi from the given paragraph of text and base direction.88* @param paragraph a paragraph of text89* @param flags a collection of flags that control the algorithm. The90* algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT,91* DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT.92* Other values are reserved.93*/94public Bidi(String paragraph, int flags) {95if (paragraph == null) {96throw new IllegalArgumentException("paragraph is null");97}9899bidiBase = new BidiBase(paragraph.toCharArray(), 0, null, 0, paragraph.length(), flags);100}101102/**103* Create Bidi from the given paragraph of text.104* <p>105* The RUN_DIRECTION attribute in the text, if present, determines the base106* direction (left-to-right or right-to-left). If not present, the base107* direction is computes using the Unicode Bidirectional Algorithm, defaulting to left-to-right108* if there are no strong directional characters in the text. This attribute, if109* present, must be applied to all the text in the paragraph.110* <p>111* The BIDI_EMBEDDING attribute in the text, if present, represents embedding level112* information. Negative values from -1 to -62 indicate overrides at the absolute value113* of the level. Positive values from 1 to 62 indicate embeddings. Where values are114* zero or not defined, the base embedding level as determined by the base direction115* is assumed.116* <p>117* The NUMERIC_SHAPING attribute in the text, if present, converts European digits to118* other decimal digits before running the bidi algorithm. This attribute, if present,119* must be applied to all the text in the paragraph.120*121* @param paragraph a paragraph of text with optional character and paragraph attribute information122*123* @see java.awt.font.TextAttribute#BIDI_EMBEDDING124* @see java.awt.font.TextAttribute#NUMERIC_SHAPING125* @see java.awt.font.TextAttribute#RUN_DIRECTION126*/127public Bidi(AttributedCharacterIterator paragraph) {128if (paragraph == null) {129throw new IllegalArgumentException("paragraph is null");130}131132bidiBase = new BidiBase(0, 0);133bidiBase.setPara(paragraph);134}135136/**137* Create Bidi from the given text, embedding, and direction information.138* The embeddings array may be null. If present, the values represent embedding level139* information. Negative values from -1 to -61 indicate overrides at the absolute value140* of the level. Positive values from 1 to 61 indicate embeddings. Where values are141* zero, the base embedding level as determined by the base direction is assumed.142* @param text an array containing the paragraph of text to process.143* @param textStart the index into the text array of the start of the paragraph.144* @param embeddings an array containing embedding values for each character in the paragraph.145* This can be null, in which case it is assumed that there is no external embedding information.146* @param embStart the index into the embedding array of the start of the paragraph.147* @param paragraphLength the length of the paragraph in the text and embeddings arrays.148* @param flags a collection of flags that control the algorithm. The149* algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT,150* DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT.151* Other values are reserved.152*/153public Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags) {154if (text == null) {155throw new IllegalArgumentException("text is null");156}157if (paragraphLength < 0) {158throw new IllegalArgumentException("bad length: " + paragraphLength);159}160if (textStart < 0 || paragraphLength > text.length - textStart) {161throw new IllegalArgumentException("bad range: " + textStart +162" length: " + paragraphLength +163" for text of length: " + text.length);164}165if (embeddings != null && (embStart < 0 || paragraphLength > embeddings.length - embStart)) {166throw new IllegalArgumentException("bad range: " + embStart +167" length: " + paragraphLength +168" for embeddings of length: " + text.length);169}170171bidiBase = new BidiBase(text, textStart, embeddings, embStart, paragraphLength, flags);172}173174/**175* Create a Bidi object representing the bidi information on a line of text within176* the paragraph represented by the current Bidi. This call is not required if the177* entire paragraph fits on one line.178*179* @param lineStart the offset from the start of the paragraph to the start of the line.180* @param lineLimit the offset from the start of the paragraph to the limit of the line.181* @return a {@code Bidi} object182*/183public Bidi createLineBidi(int lineStart, int lineLimit) {184AttributedString astr = new AttributedString("");185Bidi newBidi = new Bidi(astr.getIterator());186187return bidiBase.setLine(this, bidiBase, newBidi, newBidi.bidiBase, lineStart, lineLimit);188}189190/**191* Return true if the line is not left-to-right or right-to-left. This means it either has mixed runs of left-to-right192* and right-to-left text, or the base direction differs from the direction of the only run of text.193*194* @return true if the line is not left-to-right or right-to-left.195*/196public boolean isMixed() {197return bidiBase.isMixed();198}199200/**201* Return true if the line is all left-to-right text and the base direction is left-to-right.202*203* @return true if the line is all left-to-right text and the base direction is left-to-right204*/205public boolean isLeftToRight() {206return bidiBase.isLeftToRight();207}208209/**210* Return true if the line is all right-to-left text, and the base direction is right-to-left.211* @return true if the line is all right-to-left text, and the base direction is right-to-left212*/213public boolean isRightToLeft() {214return bidiBase.isRightToLeft();215}216217/**218* Return the length of text in the line.219* @return the length of text in the line220*/221public int getLength() {222return bidiBase.getLength();223}224225/**226* Return true if the base direction is left-to-right.227* @return true if the base direction is left-to-right228*/229public boolean baseIsLeftToRight() {230return bidiBase.baseIsLeftToRight();231}232233/**234* Return the base level (0 if left-to-right, 1 if right-to-left).235* @return the base level236*/237public int getBaseLevel() {238return bidiBase.getParaLevel();239}240241/**242* Return the resolved level of the character at offset. If offset is243* {@literal <} 0 or ≥ the length of the line, return the base direction244* level.245*246* @param offset the index of the character for which to return the level247* @return the resolved level of the character at offset248*/249public int getLevelAt(int offset) {250return bidiBase.getLevelAt(offset);251}252253/**254* Return the number of level runs.255* @return the number of level runs256*/257public int getRunCount() {258return bidiBase.countRuns();259}260261/**262* Return the level of the nth logical run in this line.263* @param run the index of the run, between 0 and {@code getRunCount()}264* @return the level of the run265*/266public int getRunLevel(int run) {267return bidiBase.getRunLevel(run);268}269270/**271* Return the index of the character at the start of the nth logical run in this line, as272* an offset from the start of the line.273* @param run the index of the run, between 0 and {@code getRunCount()}274* @return the start of the run275*/276public int getRunStart(int run) {277return bidiBase.getRunStart(run);278}279280/**281* Return the index of the character past the end of the nth logical run in this line, as282* an offset from the start of the line. For example, this will return the length283* of the line for the last run on the line.284* @param run the index of the run, between 0 and {@code getRunCount()}285* @return limit the limit of the run286*/287public int getRunLimit(int run) {288return bidiBase.getRunLimit(run);289}290291/**292* Return true if the specified text requires bidi analysis. If this returns false,293* the text will display left-to-right. Clients can then avoid constructing a Bidi object.294* Text in the Arabic Presentation Forms area of Unicode is presumed to already be shaped295* and ordered for display, and so will not cause this function to return true.296*297* @param text the text containing the characters to test298* @param start the start of the range of characters to test299* @param limit the limit of the range of characters to test300* @return true if the range of characters requires bidi analysis301*/302public static boolean requiresBidi(char[] text, int start, int limit) {303return BidiBase.requiresBidi(text, start, limit);304}305306/**307* Reorder the objects in the array into visual order based on their levels.308* This is a utility function to use when you have a collection of objects309* representing runs of text in logical order, each run containing text310* at a single level. The elements at {@code index} from311* {@code objectStart} up to {@code objectStart + count}312* in the objects array will be reordered into visual order assuming313* each run of text has the level indicated by the corresponding element314* in the levels array (at {@code index - objectStart + levelStart}).315*316* @param levels an array representing the bidi level of each object317* @param levelStart the start position in the levels array318* @param objects the array of objects to be reordered into visual order319* @param objectStart the start position in the objects array320* @param count the number of objects to reorder321*/322public static void reorderVisually(byte[] levels, int levelStart, Object[] objects, int objectStart, int count) {323BidiBase.reorderVisually(levels, levelStart, objects, objectStart, count);324}325326/**327* Display the bidi internal state, used in debugging.328*/329public String toString() {330return bidiBase.toString();331}332333}334335336