Package java.awt.font

Examples of java.awt.font.TextLayout


     * @param useMergedCells    whether to use merged cells
     * @return  the width in pixels
     */
    public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells){
        AttributedString str;
        TextLayout layout;

        Workbook wb = sheet.getWorkbook();
        DataFormatter formatter = new DataFormatter();
        Font defaultFont = wb.getFontAt((short) 0);

        str = new AttributedString(String.valueOf(defaultChar));
        copyAttributes(defaultFont, str, 0, 1);
        layout = new TextLayout(str.getIterator(), fontRenderContext);
        int defaultCharWidth = (int)layout.getAdvance();

        double width = -1;
        rows:
        for (Row row : sheet) {
            Cell cell = row.getCell(column);

            if (cell == null) {
                continue;
            }

            int colspan = 1;
            for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) {
                CellRangeAddress region = sheet.getMergedRegion(i);
                if (containsCell(region, row.getRowNum(), column)) {
                    if (!useMergedCells) {
                        // If we're not using merged cells, skip this one and move on to the next.
                        continue rows;
                    }
                    cell = row.getCell(region.getFirstColumn());
                    colspan = 1 + region.getLastColumn() - region.getFirstColumn();
                }
            }

            CellStyle style = cell.getCellStyle();
            int cellType = cell.getCellType();

            // for formula cells we compute the cell width for the cached formula result
            if(cellType == Cell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();

            Font font = wb.getFontAt(style.getFontIndex());

            if (cellType == Cell.CELL_TYPE_STRING) {
                RichTextString rt = cell.getRichStringCellValue();
                String[] lines = rt.getString().split("\\n");
                for (int i = 0; i < lines.length; i++) {
                    String txt = lines[i] + defaultChar;

                    str = new AttributedString(txt);
                    copyAttributes(font, str, 0, txt.length());

                    if (rt.numFormattingRuns() > 0) {
                        // TODO: support rich text fragments
                    }

                    layout = new TextLayout(str.getIterator(), fontRenderContext);
                    if(style.getRotation() != 0){
                        /*
                         * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                         * and then rotate the text before computing the bounds. The scale results in some whitespace around
                         * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
                         * is added by the standard Excel autosize.
                         */
                        AffineTransform trans = new AffineTransform();
                        trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
                        trans.concatenate(
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
                        );
                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                    } else {
                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                    }
                }
            } else {
                String sval = null;
                if (cellType == Cell.CELL_TYPE_NUMERIC) {
                    // Try to get it formatted to look the same as excel
                    try {
                        sval = formatter.formatCellValue(cell, dummyEvaluator);
                    } catch (Exception e) {
                        sval = String.valueOf(cell.getNumericCellValue());
                    }
                } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
                    sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase();
                }
                if(sval != null) {
                    String txt = sval + defaultChar;
                    str = new AttributedString(txt);
                    copyAttributes(font, str, 0, txt.length());

                    layout = new TextLayout(str.getIterator(), fontRenderContext);
                    if(style.getRotation() != 0){
                        /*
                         * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                         * and then rotate the text before computing the bounds. The scale results in some whitespace around
                         * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
                         * is added by the standard Excel autosize.
                         */
                        AffineTransform trans = new AffineTransform();
                        trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
                        trans.concatenate(
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
                        );
                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                    } else {
                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                    }
                }
            }

        }
View Full Code Here


        return shadowedTile;
    }

    public BufferedImage textButton(String text, int width, int buttonType) {
        Font font = new Font("Lucida Grande", Font.PLAIN, 12);
        TextLayout layout = new TextLayout(text, font, new FontRenderContext(null, true, true));

        Rectangle2D bounds = layout.getBounds();
        if (width == 0)
            width = (int) bounds.getWidth() + 16;
        int height = 20; // (int) bounds.getHeight() + 8;

        BufferedImage buttonTile = useResource
                                   ? basicButtonFromResouce(width, height, buttonType)
                                   : basicButton(width, height, buttonType);

        Graphics2D g = (Graphics2D) buttonTile.getGraphics();
        if (state == DISABLED)
            g.setColor(Color.gray);
        else
            g.setColor(Color.BLACK);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        float x = (float) (buttonTile.getWidth() / 2.0 - bounds.getWidth() / 2.0) + 1;
        float y = (float) (buttonTile.getHeight() / 2.0 - bounds.getHeight() / 2.0) - 1;

        if (!isOSXPanther) {
            // Workaround for broken TextLayout on Panther
            y += layout.getAscent() - layout.getDescent();
            layout.draw(g, x, y);
        } else {
            g.setFont(font);
            FontMetrics fm = g.getFontMetrics();
            bounds = fm.getStringBounds(text, g);
            y += fm.getAscent() - layout.getDescent();
            g.drawString(text, x, y);
        }

        boolean debug = false;
        if (debug)
View Full Code Here

        g.setColor(Color.black);

        Font font = new Font("Monospaced", Font.PLAIN, 12);
        g.setFont(font);

        TextLayout layout = new TextLayout("ABC", font, g.getFontRenderContext());

        float textHeight = (float) layout.getBounds().getHeight() + 5;

        if (loc != null && engine != null) {
            g.drawString("    x: " + loc.x, minx + 10, miny + 2 + textHeight);
            g.drawString("    y: " + loc.y, minx + 10, miny + 2 + 2 * textHeight);

 
View Full Code Here

    private static Shape stringToShape(Graphics2D g, String s, float size) {
        FontRenderContext context = g.getFontRenderContext();
        Font font = g.getFont();
        font = font.deriveFont(size);
        TextLayout text = new TextLayout(s, font, context);
        Shape shape = text.getOutline(null);
        shape = justify(shape);
        return shape;
    }
View Full Code Here

      g2.setPaint( pntLine );
      g2.draw( pathTrns );
      if( label != null ) {
        g2.setPaint( pntLabel );
        if( txtLay == null ) {
          txtLay    = new TextLayout( label, getFont(), g2.getFontRenderContext() );
          txtBounds   = txtLay.getBounds();
        }
        txtLay.draw( g2, recentSize.width - (float) txtBounds.getWidth() - 4,
                 recentSize.height - (float) txtBounds.getHeight() );
      }
View Full Code Here

            this.width = config.getTextMarginLeft() * 2;
            this.height = config.getTextMarginBottom() * 6;
        }
        char[] chars = challengeId.toCharArray();
        charAttsList = new ArrayList();
        TextLayout text = null;
        AffineTransform textAt = null;
        String []fontNames = config.getFontNames();
        for (int i = 0; i < chars.length; i++)
        {
            // font name
            String fontName = (fontNames.length == 1) ? fontNames[0] : fontNames[randomInt(0, fontNames.length)];
                       
            // rise
            int rise = config.getTextRiseRange();
            if (rise > 0)
            {
                rise = randomInt(config.getTextMarginBottom(), config.getTextMarginBottom() + config.getTextRiseRange());
            }

            if (config.getTextShear() > 0.0 || config.getTextRotation() > 0)
            {
                // rotation
                double dRotation = 0.0;
                if (config.getTextRotation() > 0)
                {
                    dRotation = Math.toRadians(randomInt(-(config.getTextRotation()), config.getTextRotation()));
                }
               
                // shear
                double shearX = 0.0;
                double shearY = 0.0;
                if (config.getTextShear() > 0.0)
                {
                    Random ran = new Random();
                    shearX = ran.nextDouble() * config.getTextShear();
                    shearY = ran.nextDouble() * config.getTextShear();
                }
                CharAttributes cf = new CharAttributes(chars[i], fontName, dRotation, rise, shearX, shearY);
                charAttsList.add(cf);
                text = new TextLayout(chars[i] + "", getFont(fontName),
                        new FontRenderContext(null, config.isFontAntialiasing(), false));
                textAt = new AffineTransform();
                if (config.getTextRotation() > 0)
                    textAt.rotate(dRotation);
                if (config.getTextShear() > 0.0)
                    textAt.shear(shearX, shearY);               
            }
            else
            {
                CharAttributes cf = new CharAttributes(chars[i], fontName, 0, rise, 0.0, 0.0);
                charAttsList.add(cf);               
            }
            if (emptyBackground)
            {
                Shape shape = text.getOutline(textAt);
//                this.width += text.getBounds().getWidth();
                this.width += (int) shape.getBounds2D().getWidth();
                this.width += config.getTextSpacing() + 1;
                if (this.height < (int) shape.getBounds2D().getHeight() + rise)
                {
View Full Code Here

        int curWidth = config.getTextMarginLeft();
        FontRenderContext ctx = new FontRenderContext(null, config.isFontAntialiasing(), false);       
        for (int i = 0; i < charAttsList.size(); i++)
        {
            CharAttributes cf = (CharAttributes) charAttsList.get(i);
            TextLayout text = new TextLayout(cf.getChar() + "", getFont(cf.getName()), ctx); //gfx.getFontRenderContext());
            AffineTransform textAt = new AffineTransform();
            textAt.translate(curWidth, this.height - cf.getRise());
            if (cf.getRotation() != 0)
            {
                textAt.rotate(cf.getRotation());
            }
            if (cf.getShearX() > 0.0)
                textAt.shear(cf.getShearX(), cf.getShearY());
            Shape shape = text.getOutline(textAt);
            curWidth += shape.getBounds().getWidth() + config.getTextSpacing();
            if (config.isUseImageBackground())
                gfx.setColor(Color.BLACK);
            else
                gfx.setXORMode(Color.BLACK);
View Full Code Here

    return null;
  }


  private IPoint getTextPositionText() {
    final TextLayout tl = new TextLayout(shape.getText(), FONT, FONT_METRICS.getFontRenderContext());
    final Rectangle2D bounds = tl.getBounds();

    switch(shape.getTextPosition()) {
      case BOT : return ShapeFactory.createPoint(shape.getX()-bounds.getWidth()/2., shape.getY());
      case TOP : return ShapeFactory.createPoint(shape.getX()-bounds.getWidth()/2., shape.getY()+bounds.getHeight());
      case BOT_LEFT : return ShapeFactory.createPoint(shape.getX(), shape.getY());
View Full Code Here

    final double tly;
    final double widthBorder;
    final double heightBorder;

    if(image==null) {
      final TextLayout tl = new TextLayout(shape.getText(), FONT, FONT_METRICS.getFontRenderContext());
      final Rectangle2D bounds = tl.getBounds();
      tlx = position.getX();
      tly = position.getY()-bounds.getHeight()+tl.getDescent();
      widthBorder = tl.getAdvance();
      heightBorder = bounds.getHeight();
    }
    else {
      final double scale = FlyweightThumbnail.scaleImage();
      tlx = position.getX();
      tly = position.getY();
      widthBorder = image.getWidth(null)*(1/scale);
      heightBorder = image.getHeight(null)*(1/scale);
    }

    if(LNumber.equalsDouble(angle, 0.))
      border.setFrame(tlx, tly, widthBorder, heightBorder);
    else {
      final IPoint tl = ShapeFactory.createPoint();
      final IPoint br = ShapeFactory.createPoint();
      getRotatedRectangle(tlx, tly, widthBorder, heightBorder, angle, shape.getGravityCentre(), tl, br);
      // The border of the rotated rectangle is now the border of the rectangular view.
      border.setFrameFromDiagonal(tl.getX(), tl.getY(), br.getX(), br.getY());
    }
  }
View Full Code Here

                angle, rotateX, rotateY);
        g2.transform(rotate);

        if (useDrawRotatedStringWorkaround) {
            // workaround for JDC bug ID 4312117 and others...
            final TextLayout tl = new TextLayout(text, g2.getFont(),
                    g2.getFontRenderContext());
            tl.draw(g2, textX, textY);
        }
        else {
            AttributedString as = new AttributedString(text,
                    g2.getFont().getAttributes());
          g2.drawString(as.getIterator(), textX, textY);
View Full Code Here

TOP

Related Classes of java.awt.font.TextLayout

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.