Search in sources :

Example 26 with TextLayout

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

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

Example 28 with TextLayout

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

the class WFContentPanel method paintComponent.

//	mouseMoved
/**************************************************************************
	 * 	Paint Component.
	 * 	Paint Lines directly as not added.
	 *	@param g graphics
	 */
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    //	Paint Lines
    for (int i = 0; i < m_lines.size(); i++) {
        WFLine line = (WFLine) m_lines.get(i);
        line.paint(g);
    }
    //	Paint Position = right next to the box
    if (m_dragged && m_draggedNode != null) {
        Point loc = m_draggedNode.getLocation();
        String text = "(" + loc.x + "," + loc.y + ")";
        Graphics2D g2D = (Graphics2D) g;
        Font font = new Font("Dialog", Font.PLAIN, 10);
        g2D.setColor(Color.magenta);
        TextLayout layout = new TextLayout(text, font, g2D.getFontRenderContext());
        loc.x += m_draggedNode.getWidth();
        loc.y += layout.getAscent();
        layout.draw(g2D, loc.x, loc.y);
    }
}
Also used : Point(java.awt.Point) Point(java.awt.Point) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 29 with TextLayout

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

use of java.awt.font.TextLayout in project poi by apache.

the class EscherGraphics2d method drawString.

public void drawString(AttributedCharacterIterator attributedcharacteriterator, float x, float y) {
    TextLayout textlayout = new TextLayout(attributedcharacteriterator, getFontRenderContext());
    Paint paint1 = getPaint();
    setColor(getColor());
    fill(textlayout.getOutline(AffineTransform.getTranslateInstance(x, y)));
    setPaint(paint1);
}
Also used : 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