Search in sources :

Example 1 with LineBreakMeasurer

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

Example 4 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 5 with LineBreakMeasurer

use of java.awt.font.LineBreakMeasurer in project playn by threerings.

the class JavaTextLayout method layoutText.

public static JavaTextLayout[] layoutText(JavaGraphics gfx, String text, TextFormat format, TextWrap wrap) {
    // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF)
    text = normalizeEOL(text);
    // we do some fiddling to work around the fact that TextLayout chokes on the empty string
    String ltext = text.length() == 0 ? " " : text;
    // set up an attributed character iterator so that we can measure the text
    AttributedString astring = new AttributedString(ltext);
    if (format.font != null) {
        astring.addAttribute(TextAttribute.FONT, ((JavaFont) format.font).jfont);
    }
    List<JavaTextLayout> layouts = new ArrayList<JavaTextLayout>();
    FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
    LineBreakMeasurer measurer = new LineBreakMeasurer(astring.getIterator(), frc);
    int lastPos = ltext.length(), curPos = 0;
    char eol = '\n';
    while (curPos < lastPos) {
        int nextRet = ltext.indexOf(eol, measurer.getPosition() + 1);
        if (nextRet == -1) {
            nextRet = lastPos;
        }
        TextLayout layout = measurer.nextLayout(wrap.width, nextRet, false);
        int endPos = measurer.getPosition();
        while (curPos < endPos && ltext.charAt(curPos) == eol) // skip over EOLs
        curPos += 1;
        layouts.add(new JavaTextLayout(ltext.substring(curPos, endPos), format, layout));
        curPos = endPos;
    }
    return layouts.toArray(new JavaTextLayout[layouts.size()]);
}
Also used : AttributedString(java.text.AttributedString) ArrayList(java.util.ArrayList) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) AbstractTextLayout(playn.core.AbstractTextLayout) TextLayout(java.awt.font.TextLayout)

Aggregations

LineBreakMeasurer (java.awt.font.LineBreakMeasurer)29 AttributedCharacterIterator (java.text.AttributedCharacterIterator)27 TextLayout (java.awt.font.TextLayout)26 AttributedString (java.text.AttributedString)26 FontRenderContext (java.awt.font.FontRenderContext)17 Font (java.awt.Font)8 Point (java.awt.Point)7 Graphics2D (java.awt.Graphics2D)6 Paint (java.awt.Paint)5 Dimension (java.awt.Dimension)4 ArrayList (java.util.ArrayList)4 Color (java.awt.Color)3 Insets (java.awt.Insets)3 Rectangle (java.awt.Rectangle)3 Rectangle2D (java.awt.geom.Rectangle2D)3 FontMetrics (java.awt.FontMetrics)2 AffineTransform (java.awt.geom.AffineTransform)2 Point2D (java.awt.geom.Point2D)2 BufferedImage (java.awt.image.BufferedImage)2 AttributedStringCharacterIterator (org.apache.pivot.text.AttributedStringCharacterIterator)2