Example usage for java.text Bidi DIRECTION_DEFAULT_LEFT_TO_RIGHT

List of usage examples for java.text Bidi DIRECTION_DEFAULT_LEFT_TO_RIGHT

Introduction

In this page you can find the example usage for java.text Bidi DIRECTION_DEFAULT_LEFT_TO_RIGHT.

Prototype

int DIRECTION_DEFAULT_LEFT_TO_RIGHT

To view the source code for java.text Bidi DIRECTION_DEFAULT_LEFT_TO_RIGHT.

Click Source Link

Document

Constant indicating that the base direction depends on the first strong directional character in the text according to the Unicode Bidirectional Algorithm.

Usage

From source file:Main.java

/**
 * This method determines if the direction of a substring is right-to-left.
 * If the string is empty that determination is based on the default system language
 * Locale.getDefault()./* ww  w.ja  v  a2 s  . com*/
 * The method can handle invalid substring definitions (start > end etc.), in which case the
 * method returns False.
 *
 * @return True if the text direction is right-to-left, false otherwise.
 */
public static boolean isRTL(CharSequence s, int start, int end) {
    if (s == null || s.length() == 0) {
        // empty string --> determine the direction from the default language
        return isRTL(Locale.getDefault());
    }

    if (start == end) {
        // if no character is selected we need to expand the selection
        start = Math.max(0, --start);
        if (start == end) {
            end = Math.min(s.length(), ++end);
        }
    }

    try {
        Bidi bidi = new Bidi(s.subSequence(start, end).toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        return !bidi.baseIsLeftToRight();
    } catch (IndexOutOfBoundsException e) {
        return false;
    }
}

From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java

protected boolean isLeftToRight(char[] chars) {
    boolean leftToRight = true;
    if (Bidi.requiresBidi(chars, 0, chars.length)) {
        // determining the text direction
        // using default LTR as there's no way to have other default in the text
        Bidi bidi = new Bidi(chars, 0, null, 0, chars.length, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        leftToRight = bidi.baseIsLeftToRight();
    }/*w  ww. java  2 s.c  o m*/
    return leftToRight;
}

From source file:com.repeatability.pdf.PDFTextStripper.java

/**
 * Handles the LTR and RTL direction of the given words. The whole implementation stands and falls with the given
 * word. If the word is a full line, the results will be the best. If the word contains of single words or
 * characters, the order of the characters in a word or words in a line may wrong, due to RTL and LTR marks and
 * characters!// w  ww . j av a 2  s .c o  m
 * 
 * Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx
 * 
 * @param word The word that shall be processed
 * @return new word with the correct direction of the containing characters
 */
// kwa
//private String handleDirection(String word)
protected String handleDirection(String word) {
    Bidi bidi = new Bidi(word, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);

    // if there is pure LTR text no need to process further
    if (!bidi.isMixed() && bidi.getBaseLevel() == Bidi.DIRECTION_LEFT_TO_RIGHT) {
        return word;
    }

    // collect individual bidi information
    int runCount = bidi.getRunCount();
    byte[] levels = new byte[runCount];
    Integer[] runs = new Integer[runCount];

    for (int i = 0; i < runCount; i++) {
        levels[i] = (byte) bidi.getRunLevel(i);
        runs[i] = i;
    }

    // reorder individual parts based on their levels
    Bidi.reorderVisually(levels, 0, runs, 0, runCount);

    // collect the parts based on the direction within the run
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < runCount; i++) {
        int index = runs[i];
        int start = bidi.getRunStart(index);
        int end = bidi.getRunLimit(index);

        int level = levels[index];

        if ((level & 1) != 0) {
            for (; --end >= start;) {
                char character = word.charAt(end);
                if (Character.isMirrored(word.codePointAt(end))) {
                    if (MIRRORING_CHAR_MAP.containsKey(character)) {
                        result.append(MIRRORING_CHAR_MAP.get(character));
                    } else {
                        result.append(character);
                    }
                } else {
                    result.append(character);
                }
            }
        } else {
            result.append(word, start, end);
        }
    }

    return result.toString();
}

From source file:org.apache.pdfbox.text.PDFTextStripper.java

/**
 * Handles the LTR and RTL direction of the given words. The whole implementation stands and falls with the given
 * word. If the word is a full line, the results will be the best. If the word contains of single words or
 * characters, the order of the characters in a word or words in a line may wrong, due to RTL and LTR marks and
 * characters!/*w  w w . jav  a2s .c om*/
 * 
 * Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx
 * 
 * @param word The word that shall be processed
 * @return new word with the correct direction of the containing characters
 */
private String handleDirection(String word) {
    Bidi bidi = new Bidi(word, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);

    // if there is pure LTR text no need to process further
    if (!bidi.isMixed() && bidi.getBaseLevel() == Bidi.DIRECTION_LEFT_TO_RIGHT) {
        return word;
    }

    // collect individual bidi information
    int runCount = bidi.getRunCount();
    byte[] levels = new byte[runCount];
    Integer[] runs = new Integer[runCount];

    for (int i = 0; i < runCount; i++) {
        levels[i] = (byte) bidi.getRunLevel(i);
        runs[i] = i;
    }

    // reorder individual parts based on their levels
    Bidi.reorderVisually(levels, 0, runs, 0, runCount);

    // collect the parts based on the direction within the run
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < runCount; i++) {
        int index = runs[i];
        int start = bidi.getRunStart(index);
        int end = bidi.getRunLimit(index);

        int level = levels[index];

        if ((level & 1) != 0) {
            while (--end >= start) {
                char character = word.charAt(end);
                if (Character.isMirrored(word.codePointAt(end))) {
                    if (MIRRORING_CHAR_MAP.containsKey(character)) {
                        result.append(MIRRORING_CHAR_MAP.get(character));
                    } else {
                        result.append(character);
                    }
                } else {
                    result.append(character);
                }
            }
        } else {
            result.append(word, start, end);
        }
    }

    return result.toString();
}

From source file:org.openstreetmap.josm.tools.Utils.java

/**
 * Convert a string to a list of {@link GlyphVector}s. The string may contain
 * bi-directional text. The result will be in correct visual order.
 * Each element of the resulting list corresponds to one section of the
 * string with consistent writing direction (left-to-right or right-to-left).
 *
 * @param string the string to render//from  ww w  .ja va  2  s  .c  o m
 * @param font the font
 * @param frc a FontRenderContext object
 * @return a list of GlyphVectors
 */
public static List<GlyphVector> getGlyphVectorsBidi(String string, Font font, FontRenderContext frc) {
    List<GlyphVector> gvs = new ArrayList<>();
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    byte[] levels = new byte[bidi.getRunCount()];
    DirectionString[] dirStrings = new DirectionString[levels.length];
    for (int i = 0; i < levels.length; ++i) {
        levels[i] = (byte) bidi.getRunLevel(i);
        String substr = string.substring(bidi.getRunStart(i), bidi.getRunLimit(i));
        int dir = levels[i] % 2 == 0 ? Bidi.DIRECTION_LEFT_TO_RIGHT : Bidi.DIRECTION_RIGHT_TO_LEFT;
        dirStrings[i] = new DirectionString(dir, substr);
    }
    Bidi.reorderVisually(levels, 0, dirStrings, 0, levels.length);
    for (int i = 0; i < dirStrings.length; ++i) {
        char[] chars = dirStrings[i].str.toCharArray();
        gvs.add(font.layoutGlyphVector(frc, chars, 0, chars.length, dirStrings[i].direction));
    }
    return gvs;
}