Example usage for java.text Bidi DIRECTION_RIGHT_TO_LEFT

List of usage examples for java.text Bidi DIRECTION_RIGHT_TO_LEFT

Introduction

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

Prototype

int DIRECTION_RIGHT_TO_LEFT

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

Click Source Link

Document

Constant indicating base direction is right-to-left.

Usage

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 .  j a  va2  s .co  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;
}