Example usage for java.text Bidi isMixed

List of usage examples for java.text Bidi isMixed

Introduction

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

Prototype

public boolean isMixed() 

Source Link

Document

Return true if the line is not left-to-right or right-to-left.

Usage

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!/* ww w .  j  ava2  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!//from www .  ja v  a2  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
 */
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();
}