⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bidi.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)Bidi.java	1.13 03/03/19 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * (C) Copyright IBM Corp. 1999-2000 - All Rights Reserved * * The original version of this source code and documentation is * copyrighted and owned by IBM. These materials are provided * under terms of a License Agreement between IBM and Sun. * This technology is protected by multiple US and International * patents. This notice and attribution to IBM may not be removed. */package java.text;import java.awt.Toolkit;import java.awt.font.TextAttribute;import java.awt.font.NumericShaper;/** * This class implements the Unicode Version 3.0 Bidirectional Algorithm. * <p> * A Bidi object provides information on the bidirectional reordering of the text * used to create it.  This is required, for example, to properly display Arabic  * or Hebrew text.  These languages are inherently mixed directional, as they order * numbers from left-to-right while ordering most other text from right-to-left. * <p> * Once created, a Bidi object can be queried to see if the text it represents is * all left-to-right or all right-to-left.  Such objects are very lightweight and * this text is relatively easy to process. * <p> * If there are multiple runs of text, information about the runs can be accessed * by indexing to get the start, limit, and level of a run.  The level represents * both the direction and the 'nesting level' of a directional run.  Odd levels * are right-to-left, while even levels are left-to-right.  So for example level * 0 represents left-to-right text, while level 1 represents right-to-left text, and * level 2 represents left-to-right text embedded in a right-to-left run. * * @since 1.4 */public final class Bidi {    byte dir;    byte baselevel;    int length;    int[] runs;    int[] cws;    static {	java.security.AccessController.doPrivileged(	    new sun.security.action.LoadLibraryAction("awt"));	java.security.AccessController.doPrivileged(	    new sun.security.action.LoadLibraryAction("fontmanager"));    }    /** Constant indicating base direction is left-to-right. */    public static final int DIRECTION_LEFT_TO_RIGHT = 0;    /** Constant indicating base direction is right-to-left. */    public static final int DIRECTION_RIGHT_TO_LEFT = 1;    /**      * Constant indicating that the base direction depends on the first strong     * directional character in the text according to the Unicode     * Bidirectional Algorithm.  If no strong directional character is present,     * the base direction is left-to-right.     */    public static final int DIRECTION_DEFAULT_LEFT_TO_RIGHT = -2;    /**      * Constant indicating that the base direction depends on the first strong     * directional character in the text according to the Unicode     * Bidirectional Algorithm.  If no strong directional character is present,     * the base direction is right-to-left.     */    public static final int DIRECTION_DEFAULT_RIGHT_TO_LEFT = -1;    private static final int DIR_MIXED = 2;    /**     * Create Bidi from the given paragraph of text and base direction.     * @param paragraph a paragraph of text     * @param flags a collection of flags that control the algorithm.  The     * algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT,     * DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT.     * Other values are reserved.     */    public Bidi(String paragraph, int flags) {        if (paragraph == null) {            throw new IllegalArgumentException("paragraph is null");        }        nativeBidiChars(this, paragraph.toCharArray(), 0, null, 0, paragraph.length(), flags);    }    /**     * Create Bidi from the given paragraph of text.     * <p>     * The RUN_DIRECTION attribute in the text, if present, determines the base     * direction (left-to-right or right-to-left).  If not present, the base     * direction is computes using the Unicode Bidirectional Algorithm, defaulting to left-to-right     * if there are no strong directional characters in the text.  This attribute, if     * present, must be applied to all the text in the paragraph.     * <p>     * The BIDI_EMBEDDING attribute in the text, if present, represents embedding level     * information.  Negative values from -1 to -62 indicate overrides at the absolute value     * of the level.  Positive values from 1 to 62 indicate embeddings.  Where values are     * zero or not defined, the base embedding level as determined by the base direction     * is assumed.     * <p>     * The NUMERIC_SHAPING attribute in the text, if present, converts European digits to     * other decimal digits before running the bidi algorithm.  This attribute, if present,     * must be applied to all the text in the paragraph.     *     * @param paragraph a paragraph of text with optional character and paragraph attribute information     *     * @see TextAttribute#BIDI_EMBEDDING     * @see TextAttribute#NUMERIC_SHAPING     * @see TextAttribute#RUN_DIRECTION     */    public Bidi(AttributedCharacterIterator paragraph) {        if (paragraph == null) {            throw new IllegalArgumentException("paragraph is null");        }        int flags = DIRECTION_DEFAULT_LEFT_TO_RIGHT;        byte[] embeddings = null;        int start = paragraph.getBeginIndex();        int limit = paragraph.getEndIndex();        int length = limit - start;        int n = 0;        char[] text = new char[length];        for (char c = paragraph.first(); c != paragraph.DONE; c = paragraph.next()) {            text[n++] = c;        }        paragraph.first();        try {            Boolean runDirection = (Boolean)paragraph.getAttribute(TextAttribute.RUN_DIRECTION);            if (runDirection != null) {                if (TextAttribute.RUN_DIRECTION_LTR.equals(runDirection)) {                    flags = DIRECTION_LEFT_TO_RIGHT; // clears default setting                } else {                    flags = DIRECTION_RIGHT_TO_LEFT;                }            }        }        catch (ClassCastException e) {        }	try {	    NumericShaper shaper = (NumericShaper)paragraph.getAttribute(TextAttribute.NUMERIC_SHAPING);	    if (shaper != null) {		shaper.shape(text, 0, text.length);	    }	}	catch (ClassCastException e) {	}        int pos = start;        do {            paragraph.setIndex(pos);            Object embeddingLevel = paragraph.getAttribute(TextAttribute.BIDI_EMBEDDING);            int newpos = paragraph.getRunLimit(TextAttribute.BIDI_EMBEDDING);            if (embeddingLevel != null) {                try {                    int intLevel = ((Integer)embeddingLevel).intValue();                    if (intLevel >= -61 && intLevel < 61) {                        byte level = (byte)(intLevel < 0 ? (-intLevel | 0x80) : intLevel);                        if (embeddings == null) {                            embeddings = new byte[length];                        }                        for (int i = pos - start; i < newpos - start; ++i) {                            embeddings[i] = level;                        }                    }                }		catch (ClassCastException e) {		}            }            pos = newpos;        } while (pos < limit);        nativeBidiChars(this, text, 0, embeddings, 0, text.length, flags);    }            /**     * Create Bidi from the given text, embedding, and direction information.     * The embeddings array may be null.  If present, the values represent embedding level     * information.  Negative values from -1 to -61 indicate overrides at the absolute value     * of the level.  Positive values from 1 to 61 indicate embeddings.  Where values are     * zero, the base embedding level as determined by the base direction is assumed.     * @param text an array containing the paragraph of text to process.     * @param textStart the index into the text array of the start of the paragraph.     * @param embeddings an array containing embedding values for each character in the paragraph.     * This can be null, in which case it is assumed that there is no external embedding information.     * @param embStart the index into the embedding array of the start of the paragraph.     * @param paragraphLength the length of the paragraph in the text and embeddings arrays.     * @param flags a collection of flags that control the algorithm.  The     * algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT,     * DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT.     * Other values are reserved.     */    public Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags) {        if (text == null) {            throw new IllegalArgumentException("text is null");        }        if (paragraphLength < 0) {            throw new IllegalArgumentException("bad length: " + paragraphLength);        }        if (textStart < 0 || paragraphLength > text.length - textStart) {            throw new IllegalArgumentException("bad range: " + textStart +                                                " length: " + paragraphLength +                                                " for text of length: " + text.length);        }        if (embeddings != null && (embStart < 0 || paragraphLength > embeddings.length - embStart)) {            throw new IllegalArgumentException("bad range: " + embStart +                                                " length: " + paragraphLength +                                                " for embeddings of length: " + text.length);        }	if (embeddings != null) {	    // native uses high bit to indicate override, not negative value, sigh	    for (int i = embStart, embLimit = embStart + paragraphLength; i < embLimit; ++i) {		if (embeddings[i] < 0) {		    byte[] temp = new byte[paragraphLength];		    System.arraycopy(embeddings, embStart, temp, 0, paragraphLength);		    for (i -= embStart; i < paragraphLength; ++i) {			if (temp[i] < 0) {			    temp[i] = (byte)(-temp[i] | 0x80);			}		    }		    embeddings = temp;		    embStart = 0;		    break;		}	    }	}        nativeBidiChars(this, text, textStart, embeddings, embStart, paragraphLength, flags);    }    /**     * Private constructor used by line bidi.     */    private Bidi(int dir, int baseLevel, int length, int[] data, int[] cws) {        reset(dir, baseLevel, length, data, cws);    }    /**     * Private mutator used by native code.     */    private void reset(int dir, int baselevel, int length, int[] data, int[] cws) {        this.dir = (byte)dir;        this.baselevel = (byte)baselevel;        this.length = length;        this.runs = data;        this.cws = cws;    }    /**     * Create a Bidi object representing the bidi information on a line of text within     * the paragraph represented by the current Bidi.  This call is not required if the     * entire paragraph fits on one line.     * @param lineStart the offset from the start of the paragraph to the start of the line.     * @param lineLimit the offset from the start of the paragraph to the limit of the line.     */    public Bidi createLineBidi(int lineStart, int lineLimit) {	if (lineStart == 0 && lineLimit == length) {	    return this;	}	int lineLength = lineLimit - lineStart;        if (lineStart < 0 ||            lineLimit < lineStart ||            lineLimit > length) {            throw new IllegalArgumentException("range " + lineStart +                                                " to " + lineLimit +                                                " is invalid for paragraph of length " + length);        }        if (runs == null) {            return new Bidi(dir, baselevel, lineLength, null, null);        } else {            int cwspos = -1;            int[] ncws = null;            if (cws != null) {                int cwss = 0;                int cwsl = cws.length;                while (cwss < cwsl) {                    if (cws[cwss] >= lineStart) {                        cwsl = cwss;                        while (cwsl < cws.length && cws[cwsl] < lineLimit) {                            cwsl++;                        }                        int ll = lineLimit-1;                        while (cwsl > cwss && cws[cwsl-1] == ll) {                            cwspos = ll; // record start of counter-directional whitespace                            --cwsl;                            --ll;                        }                        			if (cwspos == lineStart) { // entire line is cws, so ignore			    return new Bidi(dir, baselevel, lineLength, null, null);			}                        int ncwslen = cwsl - cwss;                        if (ncwslen > 0) {                            ncws = new int[ncwslen];                            for (int i = 0; i < ncwslen; ++i) {                                ncws[i] = cws[cwss+i] - lineStart;                            }                        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -