Package java.awt.font

Examples of java.awt.font.TextLayout


      AttributedCharacterIterator textIterator = attributedText.getIterator();
      FontRenderContext fontRendering = g.getFontRenderContext();
      LineBreakMeasurer measurer = new LineBreakMeasurer(textIterator, fontRendering);
      WrappedLines lines = new WrappedLines();
      while(measurer.getPosition() < textIterator.getEndIndex()) {
        TextLayout layout = measurer.nextLayout(maxWidth);
        lines.add(layout);
      }
      return lines;
    }
View Full Code Here


    try {
      graphics.setFont(font);
      graphics.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON);
     
      FontRenderContext context = graphics.getFontRenderContext();
      TextLayout layout = new TextLayout(text, font, context);
      int x = width / 2 - (int) (layout.getAdvance() / 2f);
      int y = height / 2 + (int) (layout.getAscent() / 2f) - 1;
     
      graphics.setColor(Color.DARK_GRAY.darker());
      graphics.drawString(text, x-1, y-1);
      graphics.setColor(Color.WHITE);
      graphics.drawString(text, x, y);
View Full Code Here

    ctx.drawString(label, (int) x,
        (int) y + ctx.getFontMetrics().getMaxAscent());
    if (DEBUG && label != null && !"".equals(label.trim())) {
      ctx.setPaint(Color.RED);

      TextLayout tl = new TextLayout(label, font, ctx.getFontRenderContext());
      Rectangle2D b = tl.getBounds();
      int h = (int) (tl.getAscent() + tl.getDescent() + tl.getLeading());
      ctx.drawRect((int) x, (int) y, (int) b.getWidth(), h);
    }
//    System.out.println("Drawing text " + label + " at " + x + ", "
//        + (y + ctx.getFontMetrics().getMaxAscent()) + " y=" + y + ", maxAscent="
//        + ctx.getFontMetrics().getMaxAscent() + " leading is " + ctx
View Full Code Here

    ctx.setFont(new Font(font, Font.PLAIN,
        Integer.parseInt(size.substring(0, size.length() - 2))));
    FontMetrics fm = ctx.getFontMetrics();
    Font f = ctx.getFont();
    TextLayout tl = new TextLayout(string, f, ctx.getFontRenderContext());
    Rectangle2D b = tl.getBounds();
    int h = (int) (tl.getAscent() + tl.getDescent() + tl.getLeading());
    // b.getHeight();//(int) fm.getMaxAscent() + fm.getMaxDescent() + 2;
//    System.out.println("height of " + string + " is " + h + "  vs " + fm
//        .getStringBounds(string, ctx));
    return h;
//        .getHeight();
View Full Code Here

        getG2D().setStroke(new BasicStroke(width));
    }

    public float getStringPixelWidth(Font font, String s) {
        getG2D().setFont(font);
        TextLayout layout = new TextLayout(s, getG2D().getFont(), getG2D().getFontRenderContext());
        return (float)layout.getBounds().getWidth();
    }
View Full Code Here

        getG2D().fillPolygon(x, y, 3);
    }

    public void drawString(Font font, String s, float x, float y, int align) {
        getG2D().setFont(font);
        TextLayout layout = new TextLayout(s, font, getG2D().getFontRenderContext());
        float tx = Float.MIN_VALUE;
        float ty = Float.MIN_VALUE;
        switch(align) {
            case GContext.ALIGN_CENTER:
                tx = (float)(x-layout.getBounds().getWidth()*0.5);
                ty = (float)(y+layout.getBounds().getHeight()*0.5);
                break;
            case GContext.ALIGN_CENTER_UP:
                tx = (float)(x-layout.getBounds().getWidth()*0.5);
                ty = y;
                break;
            case GContext.ALIGN_RIGHT:
                tx = (float)(x-layout.getBounds().getWidth());
                ty = (float)(y+layout.getBounds().getHeight()*0.5);
                break;
            case GContext.ALIGN_LEFT:
                tx = x;
                ty = (float)(y+layout.getBounds().getHeight()*0.5);
                break;
        }
        layout.draw(getG2D(), tx, ty-1);
    }
View Full Code Here

        int size = tipsModel.size();
        if(size > 0) {
            int width = 0;
            for(int i=0; i<tipsModel.size(); i++) {
                String e = (String)tipsModel.getElementAt(i);
                TextLayout layout = new TextLayout(e, tipsList.getFont(), ((Graphics2D)tipsList.getGraphics()).getFontRenderContext());
                width = Math.max(width, (int)layout.getBounds().getWidth());
            }
            height = height*Math.min(VISIBLE_TIPS, size)+5;
            Dimension d = new Dimension(width+10, height);
            setSize(d);
            tipsList.setSize(d);
View Full Code Here

  {
    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

     *
     * @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

        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

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.