Example usage for java.awt.font GlyphVector setGlyphPosition

List of usage examples for java.awt.font GlyphVector setGlyphPosition

Introduction

In this page you can find the example usage for java.awt.font GlyphVector setGlyphPosition.

Prototype

public abstract void setGlyphPosition(int glyphIndex, Point2D newPos);

Source Link

Document

Sets the position of the specified glyph within this GlyphVector .

Usage

From source file:org.apache.fop.render.java2d.Java2DPainter.java

/** {@inheritDoc} */
public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[] dx, String text)
        throws IFException {
    g2dState.updateColor(state.getTextColor());
    FontTriplet triplet = new FontTriplet(state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
    //TODO Ignored: state.getFontVariant()
    //TODO Opportunity for font caching if font state is more heavily used
    Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
    //String fontName = font.getFontName();
    //float fontSize = state.getFontSize() / 1000f;
    g2dState.updateFont(font.getFontName(), state.getFontSize() * 1000);

    Graphics2D g2d = this.g2dState.getGraph();
    GlyphVector gv = g2d.getFont().createGlyphVector(g2d.getFontRenderContext(), text);
    Point2D cursor = new Point2D.Float(0, 0);

    int l = text.length();
    int dxl = (dx != null ? dx.length : 0);

    if (dx != null && dxl > 0 && dx[0] != 0) {
        cursor.setLocation(cursor.getX() - (dx[0] / 10f), cursor.getY());
        gv.setGlyphPosition(0, cursor);
    }// w w w  .j  a  v a 2 s.  c o m
    for (int i = 0; i < l; i++) {
        char orgChar = text.charAt(i);
        float glyphAdjust = 0;
        int cw = font.getCharWidth(orgChar);

        if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
            glyphAdjust += wordSpacing;
        }
        glyphAdjust += letterSpacing;
        if (dx != null && i < dxl - 1) {
            glyphAdjust += dx[i + 1];
        }

        cursor.setLocation(cursor.getX() + cw + glyphAdjust, cursor.getY());
        gv.setGlyphPosition(i + 1, cursor);
    }
    g2d.drawGlyphVector(gv, x, y);
}