Search in sources :

Example 71 with TextLayout

use of java.awt.font.TextLayout in project hid-serial by rayshobby.

the class StyledString method getLines.

/**
	 * Get the text layouts for display if the string has changed since last call
	 * to this method regenerate them.
	 * 
	 * @param g2d Graphics2D display context
	 * @return a list of text layouts for rendering
	 */
public LinkedList<TextLayoutInfo> getLines(Graphics2D g2d) {
    if (font != g2d.getFont()) {
        setFont(g2d.getFont());
        invalidText = true;
    }
    if (invalidText) {
        styledText = new AttributedString(plainText);
        styledText = insertParagraphMarkers(plainText, styledText);
        applyAttributes();
        invalidText = false;
        invalidLayout = true;
    }
    if (invalidLayout) {
        linesInfo.clear();
        if (plainText.length() > 0) {
            textHeight = 0;
            maxLineLength = 0;
            maxLineHeight = 0;
            nbrLines = 0;
            AttributedCharacterIterator paragraph = styledText.getIterator(null, 0, plainText.length());
            FontRenderContext frc = g2d.getFontRenderContext();
            lineMeasurer = new LineBreakMeasurer(paragraph, frc);
            float yposinpara = 0;
            int charssofar = 0;
            while (lineMeasurer.getPosition() < plainText.length()) {
                TextLayout layout = lineMeasurer.nextLayout(wrapWidth);
                float advance = layout.getVisibleAdvance();
                if (justify) {
                    if (justify && advance > justifyRatio * wrapWidth) {
                        //System.out.println(layout.getVisibleAdvance() + "  " + breakWidth + "  "+ layout.get);
                        // If advance > breakWidth then we have a line break
                        float jw = (advance > wrapWidth) ? advance - wrapWidth : wrapWidth;
                        layout = layout.getJustifiedLayout(jw);
                    }
                }
                // Remember the longest and tallest value for a layout so far.
                float lh = getHeight(layout);
                if (lh > maxLineHeight)
                    maxLineHeight = lh;
                textHeight += lh;
                if (advance <= wrapWidth && advance > maxLineLength)
                    maxLineLength = advance;
                // Store layout and line info
                linesInfo.add(new TextLayoutInfo(nbrLines, layout, charssofar, layout.getCharacterCount(), yposinpara));
                charssofar += layout.getCharacterCount();
                yposinpara += lh;
                nbrLines++;
            }
        }
        invalidLayout = false;
    }
    return linesInfo;
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) FontRenderContext(java.awt.font.FontRenderContext) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 72 with TextLayout

use of java.awt.font.TextLayout in project processing by processing.

the class Toolkit method getAscent.

/**
   * Synthesized replacement for FontMetrics.getAscent(), which is dreadfully
   * inaccurate and inconsistent across platforms.
   */
public static double getAscent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    FontRenderContext frc = g2.getFontRenderContext();
    //return new TextLayout("H", font, frc).getBounds().getHeight();
    return new TextLayout("H", g.getFont(), frc).getBounds().getHeight();
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 73 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) {
    // we do some fiddling to work around the fact that TextLayout chokes on the empty string
    AttributedString astring = new AttributedString(text.length() == 0 ? " " : text);
    if (format.font != null) {
        astring.addAttribute(TextAttribute.FONT, ((JavaFont) format.font).jfont);
    }
    FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
    return new JavaTextLayout(text, format, new TextLayout(astring.getIterator(), frc));
}
Also used : AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) AbstractTextLayout(playn.core.AbstractTextLayout) TextLayout(java.awt.font.TextLayout)

Example 74 with TextLayout

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

the class TextPanel method paintOn.

public void paintOn(Graphics2D graphics) {
    graphics.setColor(getStyle().getCompiledTextColor().getColor());
    float y = 0;
    if (lines == null)
        return;
    synchronized (this) {
        for (TextLayout textLayout : lines) {
            y += textLayout.getAscent();
            int x = getStyle().getCompiledHorizontalAlignment().getX((int) widthOf(textLayout), new Box(0, 0, getWidth(), getHeight()));
            textLayout.draw(graphics, x, y);
            y += textLayout.getDescent() + textLayout.getLeading();
        }
    }
}
Also used : Box(limelight.util.Box) TextLayout(java.awt.font.TextLayout)

Example 75 with TextLayout

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

the class TextPanelTest method styledInheritsFromDefault.

@Test
public void styledInheritsFromDefault() {
    createStyles();
    parent.setSize(200, 100);
    panel.setText("<size_only_style>This some text</size_only_style>", parent);
    panel.buildLines();
    List<TextLayout> lines = panel.getLines();
    assertEquals(1, lines.size());
    String onlyLine = lines.get(0).toString();
    assertSubString("name=" + defaultFontFace, onlyLine);
    assertSubString("size=" + "25", onlyLine);
    assertSubString("style=" + defaultFontStyle, onlyLine);
    StyledText first = panel.getTextChunks().get(0);
    assertEquals(defaultTextColor, first.getColor());
}
Also used : StyledText(limelight.ui.text.StyledText) TextLayout(java.awt.font.TextLayout) Test(org.junit.Test)

Aggregations

TextLayout (java.awt.font.TextLayout)108 AttributedString (java.text.AttributedString)32 Graphics2D (java.awt.Graphics2D)25 FontRenderContext (java.awt.font.FontRenderContext)25 Font (java.awt.Font)20 AttributedCharacterIterator (java.text.AttributedCharacterIterator)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 Point (java.awt.Point)11 Rectangle (java.awt.Rectangle)10 Rectangle2D (java.awt.geom.Rectangle2D)10 Color (java.awt.Color)9 Paint (java.awt.Paint)8 AffineTransform (java.awt.geom.AffineTransform)8 Shape (java.awt.Shape)7 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