Search in sources :

Example 6 with LineBreakMeasurer

use of java.awt.font.LineBreakMeasurer 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 7 with LineBreakMeasurer

use of java.awt.font.LineBreakMeasurer 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 8 with LineBreakMeasurer

use of java.awt.font.LineBreakMeasurer 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)

Example 9 with LineBreakMeasurer

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

the class StringElement method paint.

//	getDrillAcross
/**************************************************************************
	 * 	Paint/Print.
	 *  Calculate actual Size.
	 *  The text is printed in the topmost left position - i.e. the leading is below the line
	 * 	@param g2D Graphics
	 *  @param pageStart top left Location of page
	 *  @param pageNo page number for multi page support (0 = header/footer) - ignored
	 *  @param ctx print context
	 *  @param isView true if online view (IDs are links)
	 */
public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView) {
    //	log.finest( "StringElement.paint", "<" + m_originalString + "> " + p_pageLocation.x + "/" + p_pageLocation.y
    //		+ ", Clip=" + g2D.getClip()
    //		+ ", Translate=" + g2D.getTransform().getTranslateX() + "/" + g2D.getTransform().getTranslateY()
    //		+ ", Scale=" + g2D.getTransform().getScaleX() + "/" + g2D.getTransform().getScaleY()
    //		+ ", Shear=" + g2D.getTransform().getShearX() + "/" + g2D.getTransform().getShearY());
    Point2D.Double location = getAbsoluteLocation(pageStart);
    //
    if (m_originalString != null)
        translate(ctx);
    AttributedString aString = null;
    AttributedCharacterIterator iter = null;
    AttributedCharacterIterator iter2 = null;
    float xPos = (float) location.x;
    float yPos = (float) location.y;
    float yPen = 0f;
    float height = 0f;
    float width = 0f;
    //	for all lines
    for (int i = 0; i < m_string_paper.length; i++) {
        //	Get Text
        if (isView) {
            if (m_string_view[i] == null)
                continue;
            aString = m_string_view[i];
        } else {
            if (m_string_paper[i] == null)
                continue;
            aString = m_string_paper[i];
        }
        iter = aString.getIterator();
        //	Zero Length
        if (iter.getBeginIndex() == iter.getEndIndex())
            continue;
        //	Check for Tab (just first) and 16 bit characters
        int tabPos = -1;
        boolean is8Bit = true;
        for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            if (c == '\t' && tabPos == -1)
                tabPos = iter.getIndex();
            if (c > 255)
                is8Bit = false;
        }
        TextLayout layout = null;
        float xPen = xPos;
        //	No Limit
        if (p_maxWidth == 0f) {
            if (tabPos == -1) {
                layout = new TextLayout(iter, g2D.getFontRenderContext());
                yPen = yPos + layout.getAscent();
                //	layout.draw(g2D, xPen, yPen);
                g2D.setFont(m_font);
                g2D.setPaint(m_paint);
                g2D.drawString(iter, xPen, yPen);
                //
                yPos += layout.getAscent() + layout.getDescent() + layout.getLeading();
                if (width < layout.getAdvance())
                    width = layout.getAdvance();
            } else //	we have a tab
            {
                LineBreakMeasurer measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext());
                layout = measurer.nextLayout(9999, tabPos, false);
                float lineHeight_1 = layout.getAscent() + layout.getDescent() + layout.getLeading();
                yPen = yPos + layout.getAscent();
                //	first part before tab
                layout.draw(g2D, xPen, yPen);
                xPen = getTabPos(xPos, layout.getAdvance());
                float lineWidth = xPen - xPos;
                //, iter.getEndIndex(), true);
                layout = measurer.nextLayout(9999);
                float lineHeight_2 = layout.getAscent() + layout.getDescent() + layout.getLeading();
                //	second part after tab
                layout.draw(g2D, xPen, yPen);
                //
                yPos += Math.max(lineHeight_1, lineHeight_2);
                lineWidth += layout.getAdvance();
                if (width < lineWidth)
                    width = lineWidth;
            }
        //	log.finest( "StringElement.paint - No Limit - " + location.x + "/" + yPos
        //		+ " w=" + layout.getAdvance() + ",h=" + lineHeight + ", Bounds=" + layout.getBounds());
        } else //	Size Limits
        {
            boolean fastDraw = LayoutEngine.s_FASTDRAW;
            if (fastDraw && !isView && !is8Bit)
                fastDraw = false;
            LineBreakMeasurer measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext());
            while (measurer.getPosition() < iter.getEndIndex()) {
                if (tabPos == -1) {
                    layout = measurer.nextLayout(p_maxWidth);
                    if (measurer.getPosition() < iter.getEndIndex())
                        fastDraw = false;
                } else //	tab
                {
                    fastDraw = false;
                    layout = measurer.nextLayout(p_maxWidth, tabPos, false);
                }
                //	Line Height
                float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
                if (//	one line only
                p_maxHeight == -1f && i == 0)
                    p_maxHeight = lineHeight;
                //	If we have hight left over
                if (p_maxHeight == 0f || (height + lineHeight) <= p_maxHeight) {
                    yPen = (float) location.y + height + layout.getAscent();
                    //	Tab in Text
                    if (tabPos != -1) {
                        //	first part before tab
                        layout.draw(g2D, xPen, yPen);
                        xPen = getTabPos(xPos, layout.getAdvance());
                        layout = measurer.nextLayout(p_width, iter.getEndIndex(), true);
                        //	reset (just one tab)
                        tabPos = -1;
                    } else if ((MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight.equals(p_FieldAlignmentType) && layout.isLeftToRight()) || (MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft.equals(p_FieldAlignmentType) && !layout.isLeftToRight()))
                        xPen += p_maxWidth - layout.getAdvance();
                    else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Center.equals(p_FieldAlignmentType))
                        xPen += (p_maxWidth - layout.getAdvance()) / 2;
                    else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Block.equals(p_FieldAlignmentType) && measurer.getPosition() < iter.getEndIndex()) {
                        layout = layout.getJustifiedLayout(p_maxWidth);
                        fastDraw = false;
                    }
                    if (fastDraw) {
                        g2D.setFont(m_font);
                        g2D.setPaint(m_paint);
                        g2D.drawString(iter, xPen, yPen);
                        height += lineHeight;
                        break;
                    } else {
                        layout.draw(g2D, xPen, yPen);
                    }
                    height += lineHeight;
                //	log.finest( "StringElement.paint - Limit - " + xPen + "/" + yPen
                //		+ " w=" + layout.getAdvance() + ",h=" + lineHeight + ", Align=" + p_FieldAlignmentType + ", Max w=" + p_maxWidth + ",h=" + p_maxHeight + ", Bounds=" + layout.getBounds());
                }
            }
            width = p_maxWidth;
        }
    //	size limits
    }
    //	for all strings
    if (m_check != null) {
        int x = (int) (location.x + width + 1);
        int y = (int) (location.y);
        g2D.drawImage(m_check.booleanValue() ? LayoutEngine.IMAGE_TRUE : LayoutEngine.IMAGE_FALSE, x, y, this);
    }
}
Also used : AttributedString(java.text.AttributedString) Point2D(java.awt.geom.Point2D) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Point(java.awt.Point) Paint(java.awt.Paint) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 10 with LineBreakMeasurer

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

the class VSchedulePanel method paint.

//	setAssignmentSlots
/*************************************************************************/
/**
	 *	Paint it
	 *  @param g the <code>Graphics</code> object
	 */
public void paint(Graphics g) {
    //	log.fine( "VSchedulePanel.paint", g.getClip());
    Graphics2D g2D = (Graphics2D) g;
    Dimension size = getPreferredSize();
    Rectangle clipBounds = g2D.getClipBounds();
    int w = size.width;
    int h = size.height;
    //	Paint Background
    g2D.setPaint(Color.white);
    g2D.fill3DRect(1, 1, w - 2, h - 2, true);
    if (//	required
    m_timePanel == null)
        return;
    int headerHeight = m_timePanel.getHeaderHeight();
    //	horizontal lines -
    g2D.setStroke(VScheduleTimePanel.getStroke(true));
    for (int i = 1; i < m_timePanel.getSlotCount(); i++) {
        g2D.setPaint(Color.gray);
        int yy = m_timePanel.getSlotYStart(i);
        //	top horiz line
        g2D.drawLine(1, yy, w - 2, yy);
    }
    //	heading and right vertical lines |
    g2D.setStroke(VScheduleTimePanel.getStroke(false));
    for (int i = 0; i < m_noDays; i++) {
        Rectangle where = new Rectangle(i * m_dayWidth, 0, m_dayWidth, headerHeight);
        if (!where.intersects(clipBounds))
            continue;
        //	Header Background
        CompiereUtils.paint3Deffect(g2D, where, false, true);
        g2D.setPaint(Color.blue);
        TextLayout layout = new TextLayout(getHeading(i), g2D.getFont(), g2D.getFontRenderContext());
        float hh = layout.getAscent() + layout.getDescent();
        //	center
        layout.draw(//	center
        g2D, //	center
        where.x + (m_dayWidth - layout.getAdvance()) / 2, //	center
        ((where.height - hh) / 2) + layout.getAscent());
        //	line
        g2D.setPaint(Color.black);
        int xEnd = (i + 1) * m_dayWidth;
        //	right part
        g2D.drawLine(xEnd, 1, xEnd, h - 1);
    }
    //	Paint Assignments
    for (int i = 0; m_slots != null && i < m_slots.length; i++) {
        if (!m_where[i].intersects(clipBounds))
            continue;
        //	Background
        g2D.setColor(m_slots[i].getColor(true));
        g2D.fill(m_where[i]);
        //	Text
        String string = m_slots[i].getInfoNameDescription();
        AttributedString as = new AttributedString(string);
        as.addAttribute(TextAttribute.FONT, g2D.getFont());
        as.addAttribute(TextAttribute.FOREGROUND, m_slots[i].getColor(false));
        //	Italics for Description
        int startIt = string.indexOf('(');
        int endIt = string.lastIndexOf(')');
        if (startIt != -1 && endIt != -1)
            as.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIt, endIt);
        //	Paint multiple lines if required
        AttributedCharacterIterator aci = as.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(aci, g2D.getFontRenderContext());
        float wrappingWidth = m_where[i].width - (2 * MARGIN);
        float curY = m_where[i].y + MARGIN;
        TextLayout layout = null;
        int yEnd = m_where[i].y + m_where[i].height;
        while (measurer.getPosition() < aci.getEndIndex() && curY < yEnd) {
            layout = measurer.nextLayout(wrappingWidth);
            curY += layout.getAscent();
            if (curY < yEnd)
                layout.draw(g2D, m_where[i].x + MARGIN, curY);
        }
    }
    //	assignments
    //	Paint Borders
    g2D.setPaint(Color.black);
    g2D.setStroke(VScheduleTimePanel.getStroke(false));
    //	left
    g2D.drawLine(1, 1, 1, h - 1);
    //	top
    g2D.drawLine(1, 1, w - 1, 1);
    //	heading line
    g2D.drawLine(1, headerHeight, w - 1, headerHeight);
    //	Final lines
    g2D.setStroke(VScheduleTimePanel.getStroke(false));
    //	right
    g2D.drawLine(w - 1, 1, w - 1, h - 1);
    //	bottom line
    g2D.drawLine(1, h - 1, w - 1, h - 1);
}
Also used : AttributedString(java.text.AttributedString) Rectangle(java.awt.Rectangle) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Dimension(java.awt.Dimension) AttributedString(java.text.AttributedString) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

LineBreakMeasurer (java.awt.font.LineBreakMeasurer)17 TextLayout (java.awt.font.TextLayout)16 AttributedString (java.text.AttributedString)16 AttributedCharacterIterator (java.text.AttributedCharacterIterator)15 FontRenderContext (java.awt.font.FontRenderContext)7 Font (java.awt.Font)6 Point (java.awt.Point)6 Graphics2D (java.awt.Graphics2D)5 Paint (java.awt.Paint)5 Color (java.awt.Color)3 Rectangle (java.awt.Rectangle)3 ArrayList (java.util.ArrayList)3 Dimension (java.awt.Dimension)2 Insets (java.awt.Insets)2 KeyNamePair (org.compiere.util.KeyNamePair)2 NamePair (org.compiere.util.NamePair)2 ValueNamePair (org.compiere.util.ValueNamePair)2 BooleanAttribute (doc.attributes.BooleanAttribute)1 MathObjectAttribute (doc.attributes.MathObjectAttribute)1 Image (java.awt.Image)1