Search in sources :

Example 11 with TextLayout

use of java.awt.font.TextLayout in project limelight by slagyr.

the class TextPanel method calculateDimensions.

private void calculateDimensions() {
    consumedHeight = 0;
    consumedWidth = 0;
    synchronized (this) {
        for (TextLayout layout : lines) {
            consumedHeight += (layout.getAscent() + layout.getDescent() + layout.getLeading());
            double lineWidth = widthOf(layout);
            if (lineWidth > consumedWidth)
                consumedWidth = lineWidth;
        }
    }
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 12 with TextLayout

use of java.awt.font.TextLayout in project antlrworks by antlr.

the class GEngineGraphics method drawString.

public void drawString(Font font, String s, float x, float y, int align) {
    getG2D().setFont(font);
    TextLayout layout = new TextLayout(s, font, getG2D().getFontRenderContext());
    float tx = Float.MIN_VALUE;
    float ty = Float.MIN_VALUE;
    switch(align) {
        case GContext.ALIGN_CENTER:
            tx = (float) (x - layout.getBounds().getWidth() * 0.5);
            ty = (float) (y + layout.getBounds().getHeight() * 0.5);
            break;
        case GContext.ALIGN_CENTER_UP:
            tx = (float) (x - layout.getBounds().getWidth() * 0.5);
            ty = y;
            break;
        case GContext.ALIGN_RIGHT:
            tx = (float) (x - layout.getBounds().getWidth());
            ty = (float) (y + layout.getBounds().getHeight() * 0.5);
            break;
        case GContext.ALIGN_LEFT:
            tx = x;
            ty = (float) (y + layout.getBounds().getHeight() * 0.5);
            break;
    }
    layout.draw(getG2D(), tx, ty - 1);
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 13 with TextLayout

use of java.awt.font.TextLayout in project antlrworks by antlr.

the class GEngineGraphics method getStringPixelWidth.

public float getStringPixelWidth(Font font, String s) {
    getG2D().setFont(font);
    TextLayout layout = new TextLayout(s, getG2D().getFont(), getG2D().getFontRenderContext());
    return (float) layout.getBounds().getWidth();
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 14 with TextLayout

use of java.awt.font.TextLayout in project playn by threerings.

the class JavaTextLayout method layoutText.

public static JavaTextLayout[] layoutText(JavaGraphics gfx, String text, TextFormat format, TextWrap wrap) {
    // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF)
    text = normalizeEOL(text);
    // we do some fiddling to work around the fact that TextLayout chokes on the empty string
    String ltext = text.length() == 0 ? " " : text;
    // set up an attributed character iterator so that we can measure the text
    AttributedString astring = new AttributedString(ltext);
    if (format.font != null) {
        astring.addAttribute(TextAttribute.FONT, ((JavaFont) format.font).jfont);
    }
    List<JavaTextLayout> layouts = new ArrayList<JavaTextLayout>();
    FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
    LineBreakMeasurer measurer = new LineBreakMeasurer(astring.getIterator(), frc);
    int lastPos = ltext.length(), curPos = 0;
    char eol = '\n';
    while (curPos < lastPos) {
        int nextRet = ltext.indexOf(eol, measurer.getPosition() + 1);
        if (nextRet == -1) {
            nextRet = lastPos;
        }
        TextLayout layout = measurer.nextLayout(wrap.width, nextRet, false);
        int endPos = measurer.getPosition();
        while (curPos < endPos && ltext.charAt(curPos) == eol) // skip over EOLs
        curPos += 1;
        layouts.add(new JavaTextLayout(ltext.substring(curPos, endPos), format, layout));
        curPos = endPos;
    }
    return layouts.toArray(new JavaTextLayout[layouts.size()]);
}
Also used : AttributedString(java.text.AttributedString) ArrayList(java.util.ArrayList) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) AbstractTextLayout(playn.core.AbstractTextLayout) TextLayout(java.awt.font.TextLayout)

Example 15 with TextLayout

use of java.awt.font.TextLayout in project binnavi by google.

the class ZyCaret method calcHitPosition.

private int calcHitPosition(final int caretPosition, final double x, final double y, final double zoomFactor) {
    boolean switched = false;
    int lp = m_mouse_pressed_y;
    int lr = m_mouse_released_y;
    if (lp > lr) {
        lp = m_mouse_released_y;
        lr = m_mouse_pressed_y;
        switched = true;
    }
    final int linecount = m_content.getLineCount();
    final double height = (float) m_content.getLineHeight();
    int maxIndex = caretPosition;
    for (int line = lp; line <= lr; line++) {
        double deltaY = 0;
        if (switched) {
            deltaY = height * zoomFactor * line;
        } else {
            deltaY = -(height * zoomFactor * (linecount - line));
        }
        final TextLayout textLayout = m_content.getLineContent(line).getTextLayout();
        final TextHitInfo hitInfo = textLayout.hitTestChar((float) x, (float) (y + deltaY), textLayout.getBounds());
        final int insertionIndex = hitInfo.getInsertionIndex();
        if ((caretPosition < insertionIndex) && (insertionIndex > maxIndex)) {
            maxIndex = insertionIndex;
        }
    }
    return maxIndex;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayout (java.awt.font.TextLayout)104 AttributedString (java.text.AttributedString)32 Graphics2D (java.awt.Graphics2D)24 FontRenderContext (java.awt.font.FontRenderContext)21 Font (java.awt.Font)18 AttributedCharacterIterator (java.text.AttributedCharacterIterator)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 Point (java.awt.Point)11 Rectangle (java.awt.Rectangle)9 Paint (java.awt.Paint)8 AffineTransform (java.awt.geom.AffineTransform)8 Rectangle2D (java.awt.geom.Rectangle2D)8 Color (java.awt.Color)7 Shape (java.awt.Shape)6 TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)5 Dimension (java.awt.Dimension)5 TextHitInfo (java.awt.font.TextHitInfo)4 ArrayList (java.util.ArrayList)4 Graphics (java.awt.Graphics)3 Insets (java.awt.Insets)3