Examples of TextLayout


Examples of java.awt.font.TextLayout

  {
    int width = margin * 2;
    int height = margin * 2;
    char[] chars = challengeId.getObject().toCharArray();
    List<CharAttributes> charAttsList = new ArrayList<>();
    TextLayout text;
    AffineTransform textAt;
    Shape shape;

    for (char ch : chars)
    {
      String fontName = fontNames.get(randomInt(rng, 0, fontNames.size()));
      double rotation = Math.toRadians(randomInt(rng, -35, 35));
      int rise = randomInt(rng, margin / 2, margin);

      double shearX = rng.nextDouble() * 0.2;
      double shearY = rng.nextDouble() * 0.2;
      CharAttributes cf = new CharAttributes(ch, fontName, rotation, rise, shearX, shearY);
      charAttsList.add(cf);
      text = new TextLayout(ch + "", getFont(fontName), new FontRenderContext(null, false,
        false));
      textAt = new AffineTransform();
      textAt.rotate(rotation);
      textAt.shear(shearX, shearY);
      shape = text.getOutline(textAt);
      width += (int) shape.getBounds2D().getWidth();
      if (height < (int) shape.getBounds2D().getHeight() + rise)
      {
        height = (int) shape.getBounds2D().getHeight() + rise;
      }
    }

    final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D gfx = (Graphics2D) image.getGraphics();
    gfx.setBackground(Color.WHITE);
    int curWidth = margin;
    for (CharAttributes cf : charAttsList)
    {
      text = new TextLayout(cf.getChar() + "", getFont(cf.getName()),
        gfx.getFontRenderContext());
      textAt = new AffineTransform();
      textAt.translate(curWidth, height - cf.getRise());
      textAt.rotate(cf.getRotation());
      textAt.shear(cf.getShearX(), cf.getShearY());
      shape = text.getOutline(textAt);
      curWidth += shape.getBounds().getWidth();
      gfx.setXORMode(Color.BLACK);
      gfx.fill(shape);
    }

View Full Code Here

Examples of java.awt.font.TextLayout

     *
     * @param column the column index
     */
    public void autoSizeColumn(short column) {
        AttributedString str;
        TextLayout layout;
        /**
         * Excel measures columns in units of 1/256th of a character width
         * but the docs say nothing about what particular character is used.
         * '0' looks a good choice.
         */
        char defaultChar = '0';
      
        FontRenderContext frc = new FontRenderContext(null, true, true);

        HSSFWorkbook wb = new HSSFWorkbook(book);
        HSSFFont defaultFont = wb.getFontAt((short) 0);

        str = new AttributedString("" + defaultChar);
        str.addAttribute(TextAttribute.FAMILY, defaultFont.getFontName());
        str.addAttribute(TextAttribute.SIZE, new Float(defaultFont.getFontHeightInPoints()));
        layout = new TextLayout(str.getIterator(), frc);
        int defaultCharWidth = (int)layout.getAdvance();

        double width = -1;
        for (Iterator it = rowIterator(); it.hasNext();) {
            HSSFRow row = (HSSFRow) it.next();
            HSSFCell cell = row.getCell(column);
            if (cell == null) continue;

            HSSFCellStyle style = cell.getCellStyle();
            HSSFFont font = wb.getFontAt(style.getFontIndex());
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                HSSFRichTextString rt = cell.getRichStringCellValue();
                String[] lines = rt.getString().split("\\n");
                for (int i = 0; i < lines.length; i++) {
                    str = new AttributedString(lines[i] + defaultChar);
                    str.addAttribute(TextAttribute.FAMILY, font.getFontName());
                    str.addAttribute(TextAttribute.SIZE, new Float(font.getFontHeightInPoints()));
                    if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
                    if (rt.numFormattingRuns() > 0) {
                        for (int j = 0; j < lines[i].length(); j++) {
                            int idx = rt.getFontAtIndex(j);
                            if (idx != 0) {
                                HSSFFont fnt = wb.getFontAt((short) idx);
                                str.addAttribute(TextAttribute.FAMILY, fnt.getFontName(), j, j + 1);
                                str.addAttribute(TextAttribute.SIZE, new Float(fnt.getFontHeightInPoints()), j, j + 1);
                            }
                        }
                    }
                    layout = new TextLayout(str.getIterator(), frc);
                    width = Math.max(width, layout.getAdvance() / defaultCharWidth);
                }
            } else {
                String sval = null;
                if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                    HSSFDataFormat dataformat = wb.createDataFormat();
                    short idx = style.getDataFormat();
                    String format = dataformat.getFormat(idx).replaceAll("\"", "");
                    double value = cell.getNumericCellValue();
                    try {
                        NumberFormat fmt;
                        if ("General".equals(format))
                            fmt = new DecimalFormat();
                        else
                            fmt = new DecimalFormat(format);
                        sval = fmt.format(value);
                    } catch (Exception e) {
                        sval = "" + value;
                    }
                } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                    sval = String.valueOf(cell.getBooleanCellValue());
                }

                str = new AttributedString(sval + defaultChar);
                str.addAttribute(TextAttribute.FAMILY, font.getFontName());
                str.addAttribute(TextAttribute.SIZE, new Float(font.getFontHeightInPoints()));
                layout = new TextLayout(str.getIterator(), frc);
                width = Math.max(width, layout.getAdvance() / defaultCharWidth);
            }

            if (width != -1) {
                sheet.setColumnWidth(column, (short) (width * 256));
            }
View Full Code Here

Examples of java.awt.font.TextLayout

        getEscherGraphics().drawString(string, x, y);
    }

    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);
    }
View Full Code Here

Examples of java.awt.font.TextLayout

        if (rt.isBold()) style |= Font.BOLD;
        if (rt.isItalic()) style |= Font.ITALIC;
        String fntname = rt.getFontName();
        Font font = new Font(fntname, style, size);

        TextLayout layout = new TextLayout(getText(), font, frc);
        int width = Math.round(layout.getAdvance());
        int height = Math.round(layout.getAscent());

        Dimension txsize = new Dimension(width, height);
        java.awt.Rectangle anchor = getAnchor();
        anchor.setSize(txsize);
        setAnchor(anchor);
View Full Code Here

Examples of java.awt.font.TextLayout

     * to the next power-of-two-sized resulting image
     */
    private BufferedImage getImage(Vector2f scaleFactors) {

        // calculate the size of the label text rendered with the specified font
        TextLayout layout = new TextLayout(text, font, fontRenderContext);
        Rectangle2D b = layout.getBounds();

        // calculate the width of the label with shadow and blur
        int actualWidth = (int) (b.getWidth() + kernelSize + 1 + shadowOffsetX);

        // calculate the maximum height of the text including the ascents and
        // descents of the characters
        int actualHeight = (int) (layout.getAscent() + layout.getDescent() + kernelSize + 1 + shadowOffsetY);

        // determine the closest power of two bounding box
        //
        // NOTE: we scale the text height to fit the nearest power or two, and
        // then scale the text width equally to maintain the correct aspect
View Full Code Here

Examples of java.awt.font.TextLayout

        height = 0;
        if (length() > 0) {
            LineBreakMeasurer lbm = new LineBreakMeasurer(getIterator(), frc);
            if (lbm.getPosition() < length()) {
                while (true) {
                    TextLayout layout = lbm.nextLayout((float)width);
                    height += layout.getAscent() + layout.getDescent();
                    if (lbm.getPosition() >= length()) {
                        break;
                    }
                    height += layout.getLeading();
                }
            }
        }
    }
View Full Code Here

Examples of java.awt.font.TextLayout

        if (length() > 0) {
            LineBreakMeasurer lbm = new LineBreakMeasurer(getIterator(),
                    g.getFontRenderContext());
            if (lbm.getPosition() < length()) {
                while (true) {
                    TextLayout layout = lbm.nextLayout((float)width);
                    double xx = x;
                    if (align == ALIGN_RIGHT) {
                        xx += width-layout.getAdvance();
                    } else if (align == ALIGN_CENTER) {
                        xx += (width-layout.getAdvance())/2;
                    } else if (align == ALIGN_JUSTIFY && lbm.getPosition() < length()) {
                        layout = layout.getJustifiedLayout((float)width);
                    }
                    y += layout.getAscent();
                    layout.draw(g, (float)xx, (float)y);
                    y += layout.getDescent();
                    if (lbm.getPosition() >= length()) {
                        break;
                    }
                    y += layout.getLeading();
                }
            }
        }
    }
View Full Code Here

Examples of java.awt.font.TextLayout

                    textBuilder.getIterator(), frc);
            float width = getWidth()-15;
            float x = 8;
            float y = 0;
            while (lbm.getPosition() < textBuilder.length()) {
                TextLayout layout = lbm.nextLayout(width);
                if (lbm.getPosition() < textBuilder.length()) {
                    layout = layout.getJustifiedLayout(width);
                }
                y += layout.getAscent();
                layout.draw(g2d, x, y);
                y += layout.getDescent() + layout.getLeading();
            }
        }
    }
View Full Code Here

Examples of java.awt.font.TextLayout

            final Graphics2D g2, final int width, final double aY,
            final Color foreGround) {
        double y = aY;
        if (text != null && text.length() > 0) {
            FontRenderContext context = g2.getFontRenderContext();
            TextLayout layout = new TextLayout(text, font, context);
            Rectangle2D bounds = layout.getBounds();
            g2.setColor(foreGround);
            float textX = (float) (width - bounds.getWidth()) / 2;
            y = (float) (y + layout.getLeading() + 2 * layout.getAscent());
            layout.draw(g2, textX, (float) y);
        }
        return y;
    }
View Full Code Here

Examples of java.awt.font.TextLayout

        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);
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.