Search in sources :

Example 81 with TextLayout

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

the class StringElement method calculateSize.

//	translate
/**************************************************************************
	 * 	Layout and Calculate Size.
	 * 	Set p_width & p_height
	 * 	@return Size
	 */
protected boolean calculateSize() {
    if (p_sizeCalculated)
        return true;
    //
    FontRenderContext frc = new FontRenderContext(null, true, true);
    TextLayout layout = null;
    p_height = 0f;
    p_width = 0f;
    //	No Limit
    if (p_maxWidth == 0f && p_maxHeight == 0f) {
        for (int i = 0; i < m_string_paper.length; i++) {
            AttributedCharacterIterator iter = m_string_paper[i].getIterator();
            if (iter.getBeginIndex() == iter.getEndIndex())
                continue;
            //	Check for Tab (just first)
            int tabPos = -1;
            for (char c = iter.first(); c != CharacterIterator.DONE && tabPos == -1; c = iter.next()) {
                if (c == '\t')
                    tabPos = iter.getIndex();
            }
            if (tabPos == -1) {
                layout = new TextLayout(iter, frc);
                p_height += layout.getAscent() + layout.getDescent() + layout.getLeading();
                if (p_width < layout.getAdvance())
                    p_width = layout.getAdvance();
            } else //	with tab
            {
                LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc);
                layout = measurer.nextLayout(9999, tabPos, false);
                p_height += layout.getAscent() + layout.getDescent() + layout.getLeading();
                float width = getTabPos(0, layout.getAdvance());
                layout = measurer.nextLayout(9999, iter.getEndIndex(), true);
                width += layout.getAdvance();
                if (p_width < width)
                    p_width = width;
            }
        }
        //	Add CheckBox Size
        if (m_check != null) {
            p_width += LayoutEngine.IMAGE_SIZE.width;
            if (p_height < LayoutEngine.IMAGE_SIZE.height)
                p_height = LayoutEngine.IMAGE_SIZE.height;
        }
    } else //	Size Limits
    {
        p_width = p_maxWidth;
        for (int i = 0; i < m_string_paper.length; i++) {
            AttributedCharacterIterator iter = m_string_paper[i].getIterator();
            if (iter.getBeginIndex() == iter.getEndIndex())
                continue;
            LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc);
            //	System.out.println("StringLength=" + m_originalString.length() + " MaxWidth=" + p_maxWidth + " MaxHeight=" + p_maxHeight);
            while (measurer.getPosition() < iter.getEndIndex()) {
                //	no need to expand tab space for limited space
                layout = measurer.nextLayout(p_maxWidth);
                float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
                //	System.out.println("  LineWidth=" + layout.getAdvance() + "  LineHeight=" + lineHeight);
                if (//	one line only
                p_maxHeight == -1f && i == 0)
                    p_maxHeight = lineHeight;
                if (p_maxHeight == 0f || (p_height + lineHeight) <= p_maxHeight)
                    p_height += lineHeight;
            }
        }
        //	Add CheckBox Size
        if (m_check != null) {
            //	p_width += LayoutEngine.IMAGE_SIZE.width;
            if (p_height < LayoutEngine.IMAGE_SIZE.height)
                p_height = LayoutEngine.IMAGE_SIZE.height;
        }
    //	System.out.println("  Width=" + p_width + "  Height=" + p_height);
    }
    //	Enlarge Size when aligned and max size is given
    if (p_FieldAlignmentType != null) {
        boolean changed = false;
        if (p_height < p_maxHeight) {
            p_height = p_maxHeight;
            changed = true;
        }
        if (p_width < p_maxWidth) {
            p_width = p_maxWidth;
            changed = true;
        }
    //	if (changed)
    //		System.out.println("StringElement.calculate size - Width="
    //			+ p_width + "(" + p_maxWidth + ") - Height=" + p_height + "(" + p_maxHeight + ")");
    }
    return true;
}
Also used : LineBreakMeasurer(java.awt.font.LineBreakMeasurer) FontRenderContext(java.awt.font.FontRenderContext) Point(java.awt.Point) Paint(java.awt.Paint) TextLayout(java.awt.font.TextLayout) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 82 with TextLayout

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

the class TableElement method calculateSize.

/**************************************************************************
	 * 	Layout and Calculate Size.
	 * 	Set p_width & p_height
	 * 	@return true if calculated
	 */
protected boolean calculateSize() {
    if (p_sizeCalculated)
        return true;
    p_width = 0;
    //	reset
    m_printRows = new ArrayList<ArrayList<ArrayList<Object>>>(m_data.length);
    //	Max Column Width = 50% of available width (used if maxWidth not set)
    float dynMxColumnWidth = m_firstPage.width / 2;
    //	Width caolculation
    int rows = m_data.length;
    int cols = m_columnHeader.length;
    //	Data Sizes and Header Sizes
    Dimension2DImpl[][] dataSizes = new Dimension2DImpl[rows][cols];
    Dimension2DImpl[] headerSizes = new Dimension2DImpl[cols];
    FontRenderContext frc = new FontRenderContext(null, true, true);
    //	data columns - rows
    for (int dataCol = 0; dataCol < cols; dataCol++) {
        int col = dataCol;
        //	Print below existing column
        if (m_additionalLines.containsKey(new Integer(dataCol))) {
            col = ((Integer) m_additionalLines.get(new Integer(dataCol))).intValue();
            log.finest("DataColumn=" + dataCol + ", BelowColumn=" + col);
        }
        float colWidth = 0;
        for (int row = 0; row < rows; row++) {
            Object dataItem = m_data[row][dataCol];
            if (dataItem == null) {
                dataSizes[row][dataCol] = new Dimension2DImpl();
                continue;
            }
            String string = dataItem.toString();
            if (string.length() == 0) {
                dataSizes[row][dataCol] = new Dimension2DImpl();
                continue;
            }
            Font font = getFont(row, dataCol);
            //	Print below existing column = (col != dataCol)
            addPrintLines(row, col, dataItem);
            //	don't print
            dataSizes[row][dataCol] = new Dimension2DImpl();
            if (dataItem instanceof Boolean) {
                dataSizes[row][col].addBelow(LayoutEngine.IMAGE_SIZE);
                continue;
            } else if (dataItem instanceof ImageElement) {
                dataSizes[row][col].addBelow(new Dimension((int) ((ImageElement) dataItem).getWidth(), (int) ((ImageElement) dataItem).getHeight()));
                // Adjust the column width - teo_sarca, [ 1673620 ]
                float width = (float) Math.ceil(dataSizes[row][col].getWidth());
                if (colWidth < width)
                    colWidth = width;
                continue;
            } else if (dataItem instanceof BarcodeElement) {
                dataSizes[row][col].addBelow(new Dimension((int) ((BarcodeElement) dataItem).getWidth(), (int) ((BarcodeElement) dataItem).getHeight()));
                // Check if the overflow is allowed - teo_sarca, [ 1673590 ]
                if (!((BarcodeElement) dataItem).isAllowOverflow()) {
                    float width = (float) Math.ceil(dataSizes[row][col].getWidth());
                    if (colWidth < width)
                        colWidth = width;
                }
                continue;
            }
            //	No Width Limitations
            if (m_columnMaxWidth[col] == 0 || m_columnMaxWidth[col] == -1) {
                //	if (HTMLElement.isHTML(string))
                //		log.finest( "HTML (no) r=" + row + ",c=" + dataCol); 
                TextLayout layout = new TextLayout(string, font, frc);
                //	buffer
                float width = layout.getAdvance() + 2;
                float height = layout.getAscent() + layout.getDescent() + layout.getLeading();
                if (width > dynMxColumnWidth)
                    m_columnMaxWidth[col] = (int) Math.ceil(dynMxColumnWidth);
                else if (colWidth < width)
                    colWidth = width;
                if (dataSizes[row][col] == null) {
                    dataSizes[row][col] = new Dimension2DImpl();
                    log.log(Level.WARNING, "No Size for r=" + row + ",c=" + col);
                }
                dataSizes[row][col].addBelow(width, height);
            }
            //	Width limitations
            if (m_columnMaxWidth[col] != 0 && m_columnMaxWidth[col] != -1) {
                float height = 0;
                //
                if (HTMLElement.isHTML(string)) {
                    //	log.finest( "HTML (limit) r=" + row + ",c=" + dataCol);
                    HTMLRenderer renderer = HTMLRenderer.get(string);
                    colWidth = renderer.getWidth();
                    if (//	one line only
                    m_columnMaxHeight[col] == -1)
                        height = renderer.getHeightOneLine();
                    else
                        height = renderer.getHeight();
                    renderer.setAllocation((int) colWidth, (int) height);
                    //	log.finest( "calculateSize HTML - " + renderer.getAllocation());
                    //	replace for printing
                    m_data[row][dataCol] = renderer;
                } else {
                    String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(string);
                    for (int lineNo = 0; lineNo < lines.length; lineNo++) {
                        AttributedString aString = new AttributedString(lines[lineNo]);
                        aString.addAttribute(TextAttribute.FONT, font);
                        AttributedCharacterIterator iter = aString.getIterator();
                        LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc);
                        while (measurer.getPosition() < iter.getEndIndex()) {
                            TextLayout layout = measurer.nextLayout(Math.abs(m_columnMaxWidth[col]));
                            float width = layout.getAdvance();
                            if (colWidth < width)
                                colWidth = width;
                            float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
                            if (//	one line only
                            m_columnMaxHeight[col] == -1) {
                                height = lineHeight;
                                break;
                            } else if (m_columnMaxHeight[col] == 0 || (height + lineHeight) <= m_columnMaxHeight[col])
                                height += lineHeight;
                        }
                    }
                //	for all lines
                }
                if (m_fixedWidth[col])
                    colWidth = Math.abs(m_columnMaxWidth[col]);
                dataSizes[row][col].addBelow(colWidth, height);
            }
            dataSizes[row][col].roundUp();
            if (dataItem instanceof NamePair)
                m_rowColDrillDown.put(new Point(row, col), (NamePair) dataItem);
            //	
            log.finest("Col=" + col + ", row=" + row + " => " + dataSizes[row][col] + " - ColWidth=" + colWidth);
        }
        //	for all data rows
        //	Column Width  for Header
        String string = "";
        if (m_columnHeader[dataCol] != null)
            string = m_columnHeader[dataCol].toString();
        //	Print below existing column
        if (col != dataCol)
            headerSizes[dataCol] = new Dimension2DImpl();
        else if (//	suppress Null
        colWidth == 0 && m_columnMaxWidth[dataCol] < 0 || string.length() == 0)
            headerSizes[dataCol] = new Dimension2DImpl();
        else {
            Font font = getFont(HEADER_ROW, dataCol);
            if (!font.isBold())
                font = new Font(font.getName(), Font.BOLD, font.getSize());
            //	No Width Limitations
            if (m_columnMaxWidth[dataCol] == 0 || m_columnMaxWidth[dataCol] == -1 || !m_multiLineHeader) {
                TextLayout layout = new TextLayout(string, font, frc);
                //	buffer
                float width = layout.getAdvance() + 3;
                float height = layout.getAscent() + layout.getDescent() + layout.getLeading();
                if (width > dynMxColumnWidth)
                    m_columnMaxWidth[dataCol] = (int) Math.ceil(dynMxColumnWidth);
                else if (colWidth < width)
                    colWidth = width;
                headerSizes[dataCol] = new Dimension2DImpl(width, height);
            }
            //	Width limitations
            if (m_columnMaxWidth[dataCol] != 0 && m_columnMaxWidth[dataCol] != -1) {
                float height = 0;
                //
                String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(string);
                for (int lineNo = 0; lineNo < lines.length; lineNo++) {
                    AttributedString aString = new AttributedString(lines[lineNo]);
                    aString.addAttribute(TextAttribute.FONT, font);
                    AttributedCharacterIterator iter = aString.getIterator();
                    LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc);
                    colWidth = Math.abs(m_columnMaxWidth[dataCol]);
                    while (measurer.getPosition() < iter.getEndIndex()) {
                        TextLayout layout = measurer.nextLayout(colWidth);
                        float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
                        if (//	one line only
                        !m_multiLineHeader) {
                            height = lineHeight;
                            break;
                        } else
                            /*if (m_columnMaxHeight[dataCol] == 0
								|| (height + lineHeight) <= m_columnMaxHeight[dataCol])*/
                            height += lineHeight;
                    }
                }
                //	for all header lines
                headerSizes[dataCol] = new Dimension2DImpl(colWidth, height);
            }
        }
        //	headerSize
        headerSizes[dataCol].roundUp();
        colWidth = (float) Math.ceil(colWidth);
        //	Round Column Width
        if (dataCol == 0)
            colWidth += m_tFormat.getVLineStroke().floatValue();
        if (colWidth != 0)
            colWidth += (2 * H_GAP) + m_tFormat.getVLineStroke().floatValue();
        //	Print below existing column
        if (col != dataCol) {
            //	for the data column
            m_columnWidths.add(new Float(0.0));
            Float origWidth = (Float) m_columnWidths.get(col);
            if (origWidth == null)
                log.log(Level.SEVERE, "Column " + dataCol + " below " + col + " - no value for orig width");
            else {
                if (origWidth.compareTo(new Float(colWidth)) >= 0) {
                    log.finest("Same Width - Col=" + col + " - OrigWidth=" + origWidth + " - Width=" + colWidth + " - Total=" + p_width);
                } else {
                    m_columnWidths.set(col, new Float(colWidth));
                    p_width += (colWidth - origWidth.floatValue());
                    log.finest("New Width - Col=" + col + " - OrigWidth=" + origWidth + " - Width=" + colWidth + " - Total=" + p_width);
                }
            }
        } else //	Add new Column
        {
            m_columnWidths.add(new Float(colWidth));
            p_width += colWidth;
            log.finest("Width - Col=" + dataCol + " - Width=" + colWidth + " - Total=" + p_width);
        }
    }
    //	for all columns
    //	Height	**********
    p_height = 0;
    for (int row = 0; row < rows; row++) {
        float rowHeight = 0f;
        for (int col = 0; col < cols; col++) {
            if (//	max
            dataSizes[row][col].height > rowHeight)
                rowHeight = (float) dataSizes[row][col].height;
        }
        //	for all columns
        rowHeight += m_tFormat.getLineStroke().floatValue() + (2 * V_GAP);
        m_rowHeights.add(new Float(rowHeight));
        p_height += rowHeight;
    }
    //	for all rows
    //	HeaderRow
    m_headerHeight = 0;
    for (int col = 0; col < cols; col++) {
        if (headerSizes[col].height > m_headerHeight)
            m_headerHeight = (int) headerSizes[col].height;
    }
    //	for all columns
    //	Thick lines
    m_headerHeight += (4 * m_tFormat.getLineStroke().floatValue()) + (2 * V_GAP);
    p_height += m_headerHeight;
    //	Last row Lines
    //	last fat line
    p_height += m_tFormat.getLineStroke().floatValue();
    //	Page Layout	*******************************************************
    log.fine("FirstPage=" + m_firstPage + ", NextPages=" + m_nextPages);
    //	One Page on Y | Axis
    if (m_firstPage.height >= p_height && m_pageBreak.size() == 0) {
        log.finest("Page Y=1 - PageHeight=" + m_firstPage.height + " - TableHeight=" + p_height);
        //	Y
        m_firstRowOnPage.add(new Integer(0));
        //	Y index only
        m_pageHeight.add(new Float(p_height));
    } else //	multiple pages on Y | Axis
    {
        float availableHeight = 0f;
        float usedHeight = 0f;
        boolean firstPage = true;
        int addlRows = 0;
        //	for all rows
        for (int dataRow = 0; dataRow < m_rowHeights.size(); dataRow++) {
            float rowHeight = ((Float) m_rowHeights.get(dataRow)).floatValue();
            //	Y page break before
            boolean pageBreak = isPageBreak(dataRow);
            //adjust for lastrow
            if (dataRow + 1 == m_rowHeights.size()) {
                availableHeight -= m_tFormat.getLineStroke().floatValue();
            }
            if (!pageBreak && availableHeight < rowHeight) {
                if (availableHeight > 40 && rowHeight > 40) {
                    log.finest("- Split (leave on current) Row=" + dataRow + " - Available=" + availableHeight + ", RowHeight=" + rowHeight);
                //	if (splitRow (dataRow))
                //		addlRows += 1;
                }
                //	else
                pageBreak = true;
            }
            if (pageBreak) {
                availableHeight = firstPage ? m_firstPage.height : m_nextPages.height;
                //	Y
                m_firstRowOnPage.add(new Integer(dataRow + addlRows));
                if (!firstPage) {
                    //	Y index only
                    m_pageHeight.add(new Float(usedHeight));
                    log.finest("Page Y=" + m_pageHeight.size() + " - PageHeight=" + usedHeight);
                }
                log.finest("Page Y=" + m_firstRowOnPage.size() + " - Row=" + dataRow + " - force=" + isPageBreak(dataRow));
                firstPage = false;
                //
                availableHeight -= m_headerHeight;
                usedHeight += m_headerHeight;
            }
            availableHeight -= rowHeight;
            usedHeight += rowHeight;
            if (availableHeight < 0) {
                log.finest("- Split (move to next) Row=" + dataRow + " - Available=" + availableHeight + ", RowHeight=" + rowHeight);
            }
            log.finest("Page Y=" + m_pageHeight.size() + ", Row=" + dataRow + ",AddlRows=" + addlRows + ", Height=" + rowHeight + " - Available=" + availableHeight + ", Used=" + usedHeight);
        }
        //	for all rows
        //	Y index only
        m_pageHeight.add(new Float(usedHeight));
        log.finest("Page Y=" + m_pageHeight.size() + " - PageHeight=" + usedHeight);
    }
    //	One page on - X Axis
    if (m_firstPage.width >= p_width) {
        log.finest("Page X=1 - PageWidth=" + m_firstPage.width + " - TableWidth=" + p_width);
        //	X
        m_firstColumnOnPage.add(new Integer(0));
        //
        distributeColumns(m_firstPage.width - (int) p_width, 0, m_columnWidths.size());
    } else //	multiple pages on - X Axis
    {
        int availableWidth = 0;
        int lastStart = 0;
        for (int col = 0; col < m_columnWidths.size(); col++) {
            int columnWidth = ((Float) m_columnWidths.get(col)).intValue();
            //	X page preak
            if (availableWidth < columnWidth) {
                if (col != 0)
                    distributeColumns(availableWidth, lastStart, col);
                //
                //	X
                m_firstColumnOnPage.add(new Integer(col));
                log.finest("Page X=" + m_firstColumnOnPage.size() + " - Col=" + col);
                lastStart = col;
                //	Width is the same on all pages
                availableWidth = m_firstPage.width;
                //
                for (int repCol = 0; repCol < m_repeatedColumns && col > repCol; repCol++) {
                    float repColumnWidth = ((Float) m_columnWidths.get(repCol)).floatValue();
                    //	leave 50% of space available for non repeated columns
                    if (availableWidth < m_firstPage.width * 0.5)
                        break;
                    availableWidth -= repColumnWidth;
                }
            }
            //	pageBreak
            availableWidth -= columnWidth;
        }
    //	for acc columns
    }
    //	multiple - X pages
    log.fine("Pages=" + getPageCount() + " X=" + m_firstColumnOnPage.size() + "/Y=" + m_firstRowOnPage.size() + " - Width=" + p_width + ", Height=" + p_height);
    return true;
}
Also used : ArrayList(java.util.ArrayList) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) Font(java.awt.Font) AttributedString(java.text.AttributedString) NamePair(org.compiere.util.NamePair) ValueNamePair(org.compiere.util.ValueNamePair) KeyNamePair(org.compiere.util.KeyNamePair) Dimension(java.awt.Dimension) Point(java.awt.Point) Point(java.awt.Point) TextLayout(java.awt.font.TextLayout) AttributedCharacterIterator(java.text.AttributedCharacterIterator) FontRenderContext(java.awt.font.FontRenderContext)

Example 83 with TextLayout

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

the class WFLine method paint.

/**************************************************************************
	 * 	Paint it.
	 *	Coordinates based on WFContentPanel.
	 * 	@param g Graph
	 */
public void paint(Graphics g) {
    if (m_from == null || m_to == null)
        return;
    Polygon arrow = new Polygon();
    Point from = null;
    Point to = null;
    //	
    if (isRightTop()) {
        from = addPoint(arrow, m_from, SwingConstants.RIGHT, true);
        to = addPoint(arrow, m_to, SwingConstants.TOP, false);
    } else if (isBottomTop()) {
        from = addPoint(arrow, m_from, SwingConstants.BOTTOM, true);
        to = addPoint(arrow, m_to, SwingConstants.TOP, false);
    } else //	
    if (isTopBottom()) {
        from = addPoint(arrow, m_from, SwingConstants.TOP, true);
        to = addPoint(arrow, m_to, SwingConstants.BOTTOM, false);
    } else if (isLeftRight()) {
        from = addPoint(arrow, m_from, SwingConstants.LEFT, true);
        to = addPoint(arrow, m_to, SwingConstants.RIGHT, false);
    } else //	if (isRightLeft())
    {
        from = addPoint(arrow, m_from, SwingConstants.RIGHT, true);
        to = addPoint(arrow, m_to, SwingConstants.LEFT, false);
    }
    /**
		 *	Paint Arrow:
		 * 	Unconditional: no fill - black text
		 *	Conditional: red fill - red text
		 * 	Visited: green line
		 *	NotVisited: black line
		 *	Split/Join: AND: Magenta Dot -- XOR: -
		 */
    if (!m_next.isUnconditional()) {
        g.setColor(Color.red);
        //	fill
        g.fillPolygon(arrow);
    }
    if (m_visited)
        g.setColor(Color.green);
    else
        g.setColor(Color.black);
    //	line
    g.drawPolygon(arrow);
    //	Paint Dot for AND From
    if (m_next.isFromSplitAnd()) {
        g.setColor(Color.magenta);
        g.fillOval(from.x - 3, from.y - 3, 6, 6);
    }
    //	Paint Dot for AND To
    if (m_next.isToJoinAnd()) {
        g.setColor(Color.magenta);
        g.fillOval(to.x - 3, to.y - 3, 6, 6);
    }
    //	Paint Description in red
    if (m_description != null) {
        Graphics2D g2D = (Graphics2D) g;
        Font font = new Font("Dialog", Font.PLAIN, 9);
        if (m_next.isUnconditional())
            g2D.setColor(Color.black);
        else
            g2D.setColor(Color.red);
        TextLayout layout = new TextLayout(m_description, font, g2D.getFontRenderContext());
        //	Mid Point
        int x = 0;
        if (from.x < to.x)
            x = from.x + ((to.x - from.x) / 2);
        else
            x = to.x + ((from.x - to.x) / 2);
        int y = 0;
        if (from.y < to.y)
            y = from.y + ((to.y - from.y) / 2);
        else
            y = to.y + ((from.y - to.y) / 2);
        //	Adjust |
        //	above center
        y -= (layout.getAscent() - 3);
        //	Adjust -
        //	center
        x -= (layout.getAdvance() / 2);
        if (x < 2)
            x = 2;
        layout.draw(g2D, x, y);
    }
}
Also used : Point(java.awt.Point) Polygon(java.awt.Polygon) Font(java.awt.Font) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 84 with TextLayout

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

the class WFNode method paintComponent.

//	getPreferredSize
/**
	 * 	Paint Component
	 *	@param g Graphics
	 */
protected void paintComponent(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Rectangle bounds = getBounds();
    m_icon.paintIcon(this, g2D, 0, 0);
    //	Paint Text
    Color color = getForeground();
    g2D.setPaint(color);
    Font font = getFont();
    //
    AttributedString aString = new AttributedString(m_name);
    aString.addAttribute(TextAttribute.FONT, font);
    aString.addAttribute(TextAttribute.FOREGROUND, color);
    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) Color(java.awt.Color) Rectangle(java.awt.Rectangle) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 85 with TextLayout

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

the class VScheduleTimePanel method paint.

//	getTimeSlotIndex
/*************************************************************************/
/**
	 *	Paint it
	 * 	@param g the <code>Graphics</code> object
	 */
public void paint(Graphics g) {
    //	log.fine( "VScheduleTimePanel.paint", g.getClip());
    Graphics2D g2D = (Graphics2D) g;
    g2D.setFont(m_font);
    Dimension size = getPreferredSize();
    int w = size.width;
    int h = size.height;
    //	Paint Background
    g2D.setPaint(Color.white);
    g2D.fill3DRect(1, 1, w - 2, h - 2, true);
    //	Header Background
    Rectangle where = new Rectangle(0, 0, w, getHeaderHeight());
    CompiereUtils.paint3Deffect(g2D, where, false, true);
    //	heading
    TextLayout layout = null;
    //	layout = new TextLayout ("Heading", m_font, g2D.getFontRenderContext());
    //	float hh = layout.getAscent() + layout.getDescent();
    //	layout.draw (g2D, (w - layout.getAdvance())/2,		//	center
    //		((HEADING - hh)/2) + layout.getAscent());		//	center
    //	horizontal lines & text
    g2D.setStroke(getStroke(true));
    for (int i = 0; i < m_lines.length; i++) {
        int yy = getSlotYStart(i);
        if (m_lines[i] != null && m_lines[i].length() > 0) {
            layout = new TextLayout(m_lines[i], m_font, g2D.getFontRenderContext());
            g2D.setPaint(Color.blue);
            //	right aligned with 2 pt space
            layout.draw(//	right aligned with 2 pt space
            g2D, //	right aligned with 2 pt space
            w - layout.getAdvance() - 3, //	top aligned with leading space
            yy + layout.getAscent() + layout.getLeading());
        }
        //
        g2D.setPaint(Color.gray);
        //	top horiz line
        g2D.drawLine(2, yy, w - 2, yy);
    }
    //	Paint Borders
    g2D.setPaint(Color.black);
    g2D.setStroke(getStroke(false));
    //	left
    g2D.drawLine(1, 1, 1, h - 1);
    //	right
    g2D.drawLine(w - 1, 1, w - 1, h - 1);
    //	top
    g2D.drawLine(1, 1, w - 1, 1);
    //	header
    g2D.drawLine(1, getHeaderHeight(), w - 1, getHeaderHeight());
    //	bottom line
    g2D.drawLine(1, h - 1, w - 1, h - 1);
}
Also used : Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayout (java.awt.font.TextLayout)108 AttributedString (java.text.AttributedString)32 Graphics2D (java.awt.Graphics2D)25 FontRenderContext (java.awt.font.FontRenderContext)25 Font (java.awt.Font)20 AttributedCharacterIterator (java.text.AttributedCharacterIterator)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 Point (java.awt.Point)11 Rectangle (java.awt.Rectangle)10 Rectangle2D (java.awt.geom.Rectangle2D)10 Color (java.awt.Color)9 Paint (java.awt.Paint)8 AffineTransform (java.awt.geom.AffineTransform)8 Shape (java.awt.Shape)7 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