Search in sources :

Example 21 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class TestLineBreakWithFontSub method test.

public void test() {
    // construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
    StringBuffer text = new StringBuffer(MIXED);
    for (int i = 0; i < NUM_WORDS; i++) {
        text.append(SPACING);
        text.append(WORD);
    }
    AttributedString attrString = new AttributedString(text.toString());
    attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(), DEFAULT_FRC);
    // get width of a space-word sequence, in context
    int sequenceLength = WORD.length() + SPACING.length();
    measurer.setPosition(text.length() - sequenceLength);
    TextLayout layout = measurer.nextLayout(10000.0f);
    if (layout.getCharacterCount() != sequenceLength) {
        throw new Error("layout length is incorrect!");
    }
    final float sequenceAdvance = layout.getVisibleAdvance();
    float wrappingWidth = sequenceAdvance * 2;
    // now run test with a variety of widths
    while (wrappingWidth < (sequenceAdvance * NUM_WORDS)) {
        measurer.setPosition(0);
        checkMeasurer(measurer, wrappingWidth, sequenceAdvance, text.length());
        wrappingWidth += sequenceAdvance / 5;
    }
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) TextLayout(java.awt.font.TextLayout)

Example 22 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class MissingCodePointLayoutTest method main.

public static void main(String[] args) {
    Font font = new Font("Tahoma", Font.PLAIN, 12);
    String text = "�";
    FontRenderContext frc = new FontRenderContext(null, false, false);
    TextLayout layout = new TextLayout(text, font, frc);
    layout.getCaretShapes(0);
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout)

Example 23 with TextLayout

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

the class DefaultProcessDiagramCanvas method drawMultilineText.

protected void drawMultilineText(String text, int x, int y, int boxWidth, int boxHeight, boolean centered) {
    // Create an attributed string based in input text
    AttributedString attributedString = new AttributedString(text);
    attributedString.addAttribute(TextAttribute.FONT, g.getFont());
    attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black);
    AttributedCharacterIterator characterIterator = attributedString.getIterator();
    int currentHeight = 0;
    // Prepare a list of lines of text we'll be drawing
    List<TextLayout> layouts = new ArrayList<TextLayout>();
    String lastLine = null;
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext());
    TextLayout layout = null;
    while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= boxHeight) {
        int previousPosition = measurer.getPosition();
        // Request next layout
        layout = measurer.nextLayout(boxWidth);
        int height = ((Float) (layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue();
        if (currentHeight + height > boxHeight) {
            // to indicate more text is truncated
            if (!layouts.isEmpty()) {
                layouts.remove(layouts.size() - 1);
                if (lastLine.length() >= 4) {
                    lastLine = lastLine.substring(0, lastLine.length() - 4) + "...";
                }
                layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext()));
            }
            break;
        } else {
            layouts.add(layout);
            lastLine = text.substring(previousPosition, measurer.getPosition());
            currentHeight += height;
        }
    }
    int currentY = y + (centered ? ((boxHeight - currentHeight) / 2) : 0);
    int currentX = 0;
    // Actually draw the lines
    for (TextLayout textLayout : layouts) {
        currentY += textLayout.getAscent();
        currentX = x + (centered ? ((boxWidth - ((Double) textLayout.getBounds().getWidth()).intValue()) / 2) : 0);
        textLayout.draw(g, currentX, currentY);
        currentY += textLayout.getDescent() + textLayout.getLeading();
    }
}
Also used : AttributedString(java.text.AttributedString) ArrayList(java.util.ArrayList) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) Point(java.awt.Point) Paint(java.awt.Paint) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 24 with TextLayout

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

the class DefaultProcessDiagramCanvas method drawLabel.

public void drawLabel(String text, GraphicInfo graphicInfo, boolean centered) {
    float interline = 1.0f;
    // text
    if (text != null && text.length() > 0) {
        Paint originalPaint = g.getPaint();
        Font originalFont = g.getFont();
        g.setPaint(LABEL_COLOR);
        g.setFont(LABEL_FONT);
        int wrapWidth = 100;
        int textY = (int) graphicInfo.getY();
        // TODO: use drawMultilineText()
        AttributedString as = new AttributedString(text);
        as.addAttribute(TextAttribute.FOREGROUND, g.getPaint());
        as.addAttribute(TextAttribute.FONT, g.getFont());
        AttributedCharacterIterator aci = as.getIterator();
        FontRenderContext frc = new FontRenderContext(null, true, false);
        LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
        while (lbm.getPosition() < text.length()) {
            TextLayout tl = lbm.nextLayout(wrapWidth);
            textY += tl.getAscent();
            Rectangle2D bb = tl.getBounds();
            double tX = graphicInfo.getX();
            if (centered) {
                tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2);
            }
            tl.draw(g, (float) tX, textY);
            textY += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.getAscent();
        }
        // restore originals
        g.setFont(originalFont);
        g.setPaint(originalPaint);
    }
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Paint(java.awt.Paint) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Point(java.awt.Point) Paint(java.awt.Paint) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 25 with TextLayout

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

the class WFNode method paint.

public void paint(Graphics2D g2D) {
    m_icon.paintIcon(null, g2D, 0, 0);
    //	Paint Text
    g2D.setPaint(Color.BLACK);
    Font base = new Font(null);
    Font font = new Font(base.getName(), Font.ITALIC | Font.BOLD, base.getSize());
    //
    AttributedString aString = new AttributedString(m_node.getName(true));
    aString.addAttribute(TextAttribute.FONT, font);
    aString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
    AttributedCharacterIterator iter = aString.getIterator();
    //
    LineBreakMeasurer measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext());
    float width = s_size.width - m_icon.getIconWidth() - 2;
    TextLayout layout = measurer.nextLayout(width);
    float xPos = m_icon.getIconWidth();
    float yPos = layout.getAscent() + 2;
    //
    layout.draw(g2D, xPos, yPos);
    //	2 pt 
    width = s_size.width - 4;
    while (measurer.getPosition() < iter.getEndIndex()) {
        layout = measurer.nextLayout(width);
        yPos += layout.getAscent() + layout.getDescent() + layout.getLeading();
        layout.draw(g2D, 2, yPos);
    }
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Font(java.awt.Font) AttributedCharacterIterator(java.text.AttributedCharacterIterator) 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