Example usage for java.awt Font layoutGlyphVector

List of usage examples for java.awt Font layoutGlyphVector

Introduction

In this page you can find the example usage for java.awt Font layoutGlyphVector.

Prototype

public GlyphVector layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags) 

Source Link

Document

Returns a new GlyphVector object, performing full layout of the text if possible.

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.ja v  a2 s  .c om
 * @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;
}