Search in sources :

Example 76 with TextLayout

use of java.awt.font.TextLayout in project intellij-community by JetBrains.

the class SimpleColoredComponent method doDrawString.

private void doDrawString(Graphics2D g, int fragmentIndex, float x, float y) {
    String text = myFragments.get(fragmentIndex);
    if (StringUtil.isEmpty(text))
        return;
    TextLayout layout = getTextLayout(fragmentIndex, g.getFont(), g.getFontRenderContext());
    if (layout != null) {
        layout.draw(g, x, y);
    } else {
        g.drawString(text, x, y);
    }
}
Also used : AttributedString(java.text.AttributedString) TextLayout(java.awt.font.TextLayout)

Example 77 with TextLayout

use of java.awt.font.TextLayout in project antlrworks by antlr.

the class XJGraphics2DPS method drawString.

public void drawString(AttributedCharacterIterator iterator, float x, float y) {
    TextLayout layout = new TextLayout(iterator, getFontRenderContext());
    Shape shape = layout.getOutline(AffineTransform.getTranslateInstance(x, y));
    fill(shape);
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 78 with TextLayout

use of java.awt.font.TextLayout in project antlrworks by antlr.

the class XJRotableToggleButton method computeSize.

private void computeSize(Graphics2D g2d) {
    TextLayout tl = new TextLayout(title, g2d.getFont(), g2d.getFontRenderContext());
    Rectangle2D tb = tl.getBounds();
    computedWidth = 0;
    if (icon != null) {
        computedWidth += icon.getIconWidth();
        computedWidth += SEPARATOR;
    }
    computedWidth += tb.getWidth();
    computedHeight = BUTTON_HEIGHT;
    computedWidth += MARGIN_WIDTH;
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) TextLayout(java.awt.font.TextLayout)

Example 79 with TextLayout

use of java.awt.font.TextLayout in project antlrworks by antlr.

the class IdeaOverlay method resizeIdeas.

public void resizeIdeas() {
    Rectangle r = content.getBounds();
    int height = ideasList.getFixedCellHeight();
    int size = ideasModel.size();
    if (size > 0) {
        int width = 0;
        for (int i = 0; i < ideasModel.size(); i++) {
            IdeaAction action = (IdeaAction) ideasModel.getElementAt(i);
            TextLayout layout = new TextLayout(action.name, ideasList.getFont(), ((Graphics2D) ideasList.getGraphics()).getFontRenderContext());
            width = Math.max(width, (int) layout.getBounds().getWidth());
        }
        height = height * Math.min(VISIBLE_IDEAS, size) + 5;
        ideasScrollPane.setBounds(r.x, r.y + r.height, width + 10, height);
    }
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 80 with TextLayout

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

the class TableElement method printColumn.

//	paint
/**
	 * 	Print non zero width Column
	 * 	@param g2D graphics
	 * 	@param col column index
	 * 	@param origX start X
	 * 	@param origY start Y
	 *  @param leftVline if true print left vertical line (for first column)
	 * 	@param firstRow first row index
	 * 	@param nextPageRow row index of next page
	 *  @param isView true if online view (IDs are links)
	 */
private void printColumn(Graphics2D g2D, int col, final int origX, final int origY, boolean leftVline, final int firstRow, final int nextPageRow, boolean isView) {
    int curX = origX;
    //	start from top
    int curY = origY;
    //
    //	includes 2*Gaps+Line
    float colWidth = ((Float) m_columnWidths.get(col)).floatValue();
    float netWidth = colWidth - (2 * H_GAP) - m_tFormat.getVLineStroke().floatValue();
    if (leftVline)
        netWidth -= m_tFormat.getVLineStroke().floatValue();
    int rowHeight = m_headerHeight;
    float netHeight = rowHeight - (4 * m_tFormat.getLineStroke().floatValue()) + (2 * V_GAP);
    if (DEBUG_PRINT)
        log.finer("#" + col + " - x=" + curX + ", y=" + curY + ", width=" + colWidth + "/" + netWidth + ", HeaderHeight=" + rowHeight + "/" + netHeight);
    String alignment = m_columnJustification[col];
    //	paint header	***************************************************
    if (//	draw left | line
    leftVline) {
        g2D.setPaint(m_tFormat.getVLine_Color());
        g2D.setStroke(m_tFormat.getVLine_Stroke());
        if (//	 -> | (left)
        m_tFormat.isPaintBoundaryLines())
            g2D.drawLine(origX, (int) (origY + m_tFormat.getLineStroke().floatValue()), origX, (int) (origY + rowHeight - (4 * m_tFormat.getLineStroke().floatValue())));
        curX += m_tFormat.getVLineStroke().floatValue();
    }
    //	X - start line
    if (m_tFormat.isPaintHeaderLines()) {
        g2D.setPaint(m_tFormat.getHeaderLine_Color());
        g2D.setStroke(m_tFormat.getHeader_Stroke());
        //	 -> - (top)
        g2D.drawLine(//	 -> - (top)
        origX, //	 -> - (top)
        origY, (int) (origX + colWidth - m_tFormat.getVLineStroke().floatValue()), origY);
    }
    //	thick
    curY += (2 * m_tFormat.getLineStroke().floatValue());
    //	Background
    Color bg = getBackground(HEADER_ROW, col);
    if (!bg.equals(Color.white)) {
        g2D.setPaint(bg);
        g2D.fillRect(curX, (int) (curY - m_tFormat.getLineStroke().floatValue()), (int) (colWidth - m_tFormat.getVLineStroke().floatValue()), (int) (rowHeight - (4 * m_tFormat.getLineStroke().floatValue())));
    }
    int tempCurY = curY;
    //	upper left gap
    curX += H_GAP;
    curY += V_GAP;
    //	Header
    AttributedString aString = null;
    AttributedCharacterIterator iter = null;
    LineBreakMeasurer measurer = null;
    float usedHeight = 0;
    // Calculate column header height - teo_sarca [ 1673429 ]  
    String headerString = m_columnHeader[col].toString();
    if (headerString.length() == 0)
        headerString = " ";
    //if (m_columnHeader[col].toString().length() > 0)
    {
        aString = new AttributedString(headerString);
        aString.addAttribute(TextAttribute.FONT, getFont(HEADER_ROW, col));
        aString.addAttribute(TextAttribute.FOREGROUND, getColor(HEADER_ROW, col));
        //
        boolean fastDraw = LayoutEngine.s_FASTDRAW;
        if (fastDraw && !isView && !Util.is8Bit(headerString))
            fastDraw = false;
        iter = aString.getIterator();
        measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext());
        while (//	print header
        measurer.getPosition() < iter.getEndIndex()) {
            TextLayout layout = measurer.nextLayout(netWidth + 2);
            if (iter.getEndIndex() != measurer.getPosition())
                fastDraw = false;
            float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
            if (alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_Block)) {
                layout = layout.getJustifiedLayout(netWidth + 2);
                fastDraw = false;
            }
            curY += layout.getAscent();
            float penX = curX;
            if (alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_Center))
                penX += (netWidth - layout.getAdvance()) / 2;
            else if ((alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight) && layout.isLeftToRight()) || (alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft) && !layout.isLeftToRight()))
                penX += netWidth - layout.getAdvance();
            //
            if (fastDraw) {
                //	Bug - set Font/Color explicitly 
                g2D.setFont(getFont(HEADER_ROW, col));
                g2D.setColor(getColor(HEADER_ROW, col));
                g2D.drawString(iter, penX, curY);
            } else
                //	-> text
                layout.draw(g2D, penX, curY);
            curY += layout.getDescent() + layout.getLeading();
            usedHeight += layout.getAscent() + layout.getDescent();
            if (//	one line only
            !m_multiLineHeader)
                break;
        }
    }
    curX += netWidth + H_GAP;
    curY = tempCurY + (int) (rowHeight - (4 * m_tFormat.getLineStroke().floatValue()));
    //	Y end line
    g2D.setPaint(m_tFormat.getVLine_Color());
    g2D.setStroke(m_tFormat.getVLine_Stroke());
    if (//	 -> | (right)
    m_tFormat.isPaintVLines())
        g2D.drawLine(curX, (int) (origY + m_tFormat.getLineStroke().floatValue()), curX, (int) (origY + rowHeight - (4 * m_tFormat.getLineStroke().floatValue())));
    curX += m_tFormat.getVLineStroke().floatValue();
    //	X end line
    if (m_tFormat.isPaintHeaderLines()) {
        g2D.setPaint(m_tFormat.getHeaderLine_Color());
        g2D.setStroke(m_tFormat.getHeader_Stroke());
        //	 -> - (button)
        g2D.drawLine(//	 -> - (button)
        origX, //	 -> - (button)
        curY, (int) (origX + colWidth - m_tFormat.getVLineStroke().floatValue()), curY);
    }
    //	thick
    curY += (2 * m_tFormat.getLineStroke().floatValue());
    //	paint Data		***************************************************
    for (int row = firstRow; row < nextPageRow; row++) {
        //	includes 2*Gaps+Line
        rowHeight = ((Float) m_rowHeights.get(row)).intValue();
        netHeight = rowHeight - (2 * V_GAP) - m_tFormat.getLineStroke().floatValue();
        int rowYstart = curY;
        curX = origX;
        if (//	draw left | line
        leftVline) {
            g2D.setPaint(m_tFormat.getVLine_Color());
            g2D.setStroke(m_tFormat.getVLine_Stroke());
            if (m_tFormat.isPaintBoundaryLines())
                //	 -> | (left)
                g2D.drawLine(//	 -> | (left)
                curX, //	 -> | (left)
                rowYstart, curX, (int) (rowYstart + rowHeight - m_tFormat.getLineStroke().floatValue()));
            curX += m_tFormat.getVLineStroke().floatValue();
        }
        //	Background
        bg = getBackground(row, col);
        if (!bg.equals(Color.white)) {
            g2D.setPaint(bg);
            g2D.fillRect(curX, curY, (int) (colWidth - m_tFormat.getVLineStroke().floatValue()), (int) (rowHeight - m_tFormat.getLineStroke().floatValue()));
        }
        //	upper left gap
        curX += H_GAP;
        curY += V_GAP;
        //	actual data
        Object[] printItems = getPrintItems(row, col);
        float penY = curY;
        // suppress repeated values
        boolean suppress = false;
        if (m_colSuppressRepeats[col] && row > 0 && row != firstRow) {
            Object[] lastItems = {};
            lastItems = getPrintItems(row - 1, col);
            if (Arrays.equals(lastItems, printItems))
                suppress = true;
        }
        if (!suppress) {
            for (int index = 0; index < printItems.length; index++) {
                if (printItems[index] == null)
                    ;
                else if (printItems[index] instanceof ImageElement) {
                    Image imageToDraw = ((ImageElement) printItems[index]).getImage();
                    if (// teo_sarca [ 1674706 ] 
                    imageToDraw != null) {
                        // Draw image using the scale factor - teo_sarca, [ 1673548 ] Image is not scaled in a report table cell 
                        double scale = ((ImageElement) printItems[index]).getScaleFactor();
                        if (scale != 1.0) {
                            AffineTransform transform = new AffineTransform();
                            transform.translate(curX, penY);
                            transform.scale(scale, scale);
                            g2D.drawImage(imageToDraw, transform, this);
                        } else {
                            g2D.drawImage(imageToDraw, curX, (int) penY, this);
                        }
                    }
                } else if (printItems[index] instanceof BarcodeElement) {
                    try {
                        Barcode barcode = ((BarcodeElement) printItems[index]).getBarcode();
                        if (barcode != null) {
                            double scale = ((BarcodeElement) printItems[index]).getScaleFactor();
                            if (scale != 1.0) {
                                int w = barcode.getWidth();
                                int h = barcode.getHeight();
                                // draw barcode to buffer
                                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                                Graphics2D temp = (Graphics2D) image.getGraphics();
                                barcode.draw(temp, 0, 0);
                                // scale barcode and paint
                                AffineTransform transform = new AffineTransform();
                                transform.translate(curX, penY);
                                transform.scale(scale, scale);
                                g2D.drawImage(image, transform, this);
                            } else {
                                barcode.draw(g2D, curX, (int) penY);
                            }
                        }
                    } catch (OutputException e) {
                    }
                } else if (printItems[index] instanceof Boolean) {
                    //	center
                    int penX = curX + (int) ((netWidth - LayoutEngine.IMAGE_SIZE.width) / 2);
                    if (((Boolean) printItems[index]).booleanValue())
                        g2D.drawImage(LayoutEngine.IMAGE_TRUE, penX, (int) penY, this);
                    else
                        g2D.drawImage(LayoutEngine.IMAGE_FALSE, penX, (int) penY, this);
                    penY += LayoutEngine.IMAGE_SIZE.height;
                } else if (printItems[index] instanceof HTMLRenderer) {
                    HTMLRenderer renderer = (HTMLRenderer) printItems[index];
                    Rectangle allocation = new Rectangle((int) colWidth, (int) netHeight);
                    //	log.finest( "printColumn HTML - " + allocation);
                    g2D.translate(curX, penY);
                    renderer.paint(g2D, allocation);
                    g2D.translate(-curX, -penY);
                    penY += allocation.getHeight();
                } else {
                    String str = printItems[index].toString();
                    if (DEBUG_PRINT)
                        log.fine("row=" + row + ",col=" + col + " - " + str + " 8Bit=" + Util.is8Bit(str));
                    if (str.length() > 0) {
                        usedHeight = 0;
                        String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(str);
                        for (int lineNo = 0; lineNo < lines.length; lineNo++) {
                            aString = new AttributedString(lines[lineNo]);
                            aString.addAttribute(TextAttribute.FONT, getFont(row, col));
                            if (//	ID
                            isView && printItems[index] instanceof NamePair) {
                                aString.addAttribute(TextAttribute.FOREGROUND, LINK_COLOR);
                                aString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, 0, str.length());
                            } else
                                aString.addAttribute(TextAttribute.FOREGROUND, getColor(row, col));
                            //
                            iter = aString.getIterator();
                            boolean fastDraw = LayoutEngine.s_FASTDRAW;
                            if (fastDraw && !isView && !Util.is8Bit(lines[lineNo]))
                                fastDraw = false;
                            measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext());
                            while (//	print element
                            measurer.getPosition() < iter.getEndIndex()) {
                                TextLayout layout = measurer.nextLayout(netWidth + 2);
                                if (iter.getEndIndex() != measurer.getPosition())
                                    fastDraw = false;
                                float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
                                if ((m_columnMaxHeight[col] <= 0 || (usedHeight + lineHeight) <= m_columnMaxHeight[col]) && (usedHeight + lineHeight) <= netHeight) {
                                    if (alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_Block) && measurer.getPosition() < iter.getEndIndex()) {
                                        layout = layout.getJustifiedLayout(netWidth + 2);
                                        fastDraw = false;
                                    }
                                    penY += layout.getAscent();
                                    float penX = curX;
                                    if (alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_Center))
                                        penX += (netWidth - layout.getAdvance()) / 2;
                                    else if ((alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight) && layout.isLeftToRight()) || (alignment.equals(MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft) && !layout.isLeftToRight()))
                                        penX += netWidth - layout.getAdvance();
                                    //
                                    if (fastDraw) {
                                        //	Bug - set Font/Color explicitly
                                        g2D.setFont(getFont(row, col));
                                        if (//	ID
                                        isView && printItems[index] instanceof NamePair) {
                                            g2D.setColor(LINK_COLOR);
                                        //	TextAttribute.UNDERLINE
                                        } else
                                            g2D.setColor(getColor(row, col));
                                        g2D.drawString(iter, penX, penY);
                                    } else
                                        //	-> text
                                        layout.draw(g2D, penX, penY);
                                    if (DEBUG_PRINT)
                                        log.fine("row=" + row + ",col=" + col + " - " + str + " - x=" + penX + ",y=" + penY);
                                    penY += layout.getDescent() + layout.getLeading();
                                    usedHeight += lineHeight;
                                    //
                                    if (//	FirstLineOny
                                    m_columnMaxHeight[col] == -1)
                                        break;
                                }
                            }
                        //	print element
                        }
                    //	for all lines
                    }
                //	length > 0
                }
            //	non boolean
            }
        //	for all print items
        }
        // not suppressed
        curY += netHeight + V_GAP;
        curX += netWidth + H_GAP;
        //	Y end line
        g2D.setPaint(m_tFormat.getVLine_Color());
        g2D.setStroke(m_tFormat.getVLine_Stroke());
        if (m_tFormat.isPaintVLines())
            //	 -> | (right)
            g2D.drawLine(//	 -> | (right)
            curX, //	 -> | (right)
            rowYstart, curX, (int) (rowYstart + rowHeight - m_tFormat.getLineStroke().floatValue()));
        curX += m_tFormat.getVLineStroke().floatValue();
        //  X end line
        if (//  last Line
        row == m_data.length - 1) {
            /**
                 * Bug fix - Bottom line was always displayed whether or not header lines was set to be visible
                 * @author ashley
                 */
            if (m_tFormat.isPaintHeaderLines()) {
                g2D.setPaint(m_tFormat.getHeaderLine_Color());
                g2D.setStroke(m_tFormat.getHeader_Stroke());
                //   -> - (last line) 
                g2D.drawLine(//   -> - (last line) 
                origX, //   -> - (last line) 
                curY, (int) (origX + colWidth - m_tFormat.getVLineStroke().floatValue()), curY);
                //  thick
                curY += (2 * m_tFormat.getLineStroke().floatValue());
            } else {
                curY += m_tFormat.getLineStroke().floatValue();
            }
        } else {
            //  next line is a funcion column -> underline this
            boolean nextIsFunction = m_functionRows.contains(new Integer(row + 1));
            if (nextIsFunction && m_functionRows.contains(new Integer(row)))
                //  this is a function line too
                nextIsFunction = false;
            if (nextIsFunction) {
                g2D.setPaint(m_tFormat.getFunctFG_Color());
                g2D.setStroke(m_tFormat.getHLine_Stroke());
                //   -> - (bottom)
                g2D.drawLine(//   -> - (bottom)
                origX, //   -> - (bottom)
                curY, (int) (origX + colWidth - m_tFormat.getVLineStroke().floatValue()), curY);
            } else if (m_tFormat.isPaintHLines()) {
                g2D.setPaint(m_tFormat.getHLine_Color());
                g2D.setStroke(m_tFormat.getHLine_Stroke());
                //   -> - (bottom)
                g2D.drawLine(//   -> - (bottom)
                origX, //   -> - (bottom)
                curY, (int) (origX + colWidth - m_tFormat.getVLineStroke().floatValue()), curY);
            }
            curY += m_tFormat.getLineStroke().floatValue();
        }
    }
// for all rows
}
Also used : LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Rectangle(java.awt.Rectangle) AttributedString(java.text.AttributedString) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) AttributedString(java.text.AttributedString) OutputException(net.sourceforge.barbecue.output.OutputException) Color(java.awt.Color) NamePair(org.compiere.util.NamePair) ValueNamePair(org.compiere.util.ValueNamePair) KeyNamePair(org.compiere.util.KeyNamePair) Point(java.awt.Point) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout) Graphics2D(java.awt.Graphics2D) Barcode(net.sourceforge.barbecue.Barcode) AffineTransform(java.awt.geom.AffineTransform)

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