Search in sources :

Example 11 with AttributedString

use of java.text.AttributedString 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 12 with AttributedString

use of java.text.AttributedString 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 13 with AttributedString

use of java.text.AttributedString 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 14 with AttributedString

use of java.text.AttributedString in project adempiere by adempiere.

the class GridElement method setData.

/**
	 * 	Create TextLayout from Data and calculate size.
	 * 	Called from ParameterElement and Location
	 *  @param row row
	 *  @param col column
	 *  @param stringData info element
	 *  @param font font
	 *  @param foreground color for foreground
	 */
public void setData(int row, int col, String stringData, Font font, Paint foreground) {
    if (stringData == null || stringData.length() == 0)
        return;
    //
    //	log.fine("setData - " + row + "/" + col + " - " + stringData);
    AttributedString aString = new AttributedString(stringData);
    aString.addAttribute(TextAttribute.FONT, font);
    aString.addAttribute(TextAttribute.FOREGROUND, foreground);
    AttributedCharacterIterator iter = aString.getIterator();
    TextLayout layout = new TextLayout(iter, m_frc);
    setData(row, col, layout, iter);
}
Also used : AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 15 with AttributedString

use of java.text.AttributedString in project adempiere by adempiere.

the class Util method main.

//	trimSize
/**************************************************************************
	 * Test
	 * @param args args
	 */
public static void main(String[] args) {
    String str = "a�b�c?d?e?f?g?";
    System.out.println(str + " = " + str.length() + " - " + size(str));
    String str1 = trimLength(str, 10);
    System.out.println(str1 + " = " + str1.length() + " - " + size(str1));
    String str2 = trimSize(str, 10);
    System.out.println(str2 + " = " + str2.length() + " - " + size(str2));
    //
    AttributedString aString = new AttributedString("test test");
    aString.addAttribute(TextAttribute.FOREGROUND, Color.blue);
    aString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 2, 4);
    getIterator(aString, new AttributedCharacterIterator.Attribute[] { TextAttribute.UNDERLINE });
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

AttributedString (java.text.AttributedString)86 AttributedCharacterIterator (java.text.AttributedCharacterIterator)37 TextLayout (java.awt.font.TextLayout)29 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 FontRenderContext (java.awt.font.FontRenderContext)13 Font (java.awt.Font)11 Point (java.awt.Point)11 Paint (java.awt.Paint)10 Graphics2D (java.awt.Graphics2D)9 WeakHashMap (java.util.WeakHashMap)8 Color (java.awt.Color)4 Bidi (java.text.Bidi)4 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)4 Rectangle (java.awt.Rectangle)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Insets (java.awt.Insets)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2