Examples of TextLayout


Examples of java.awt.font.TextLayout

              current_interval_pos - cpt * (pixels_per_interval / 10),
              dimension.height - axis_margin_bottom - 1);

      final String current_time_str = formatTime(current_time_to_display);
      final String current_date_str = formatDate(current_time_to_display);
      final TextLayout current_layout = new TextLayout(current_time_str, backing_g.getFont(), backing_g.getFontRenderContext());
      final TextLayout current_layout_date = new TextLayout(current_date_str, backing_g.getFont(), backing_g.getFontRenderContext());
      final Rectangle2D current_bounds = current_layout.getBounds();
      final Rectangle2D current_bounds_date = current_layout_date.getBounds();
      backing_g.setColor(Color.YELLOW.darker());
      backing_g.drawString(current_time_str,
          current_interval_pos - (int) (current_bounds.getWidth() / 2),
          dimension.height - axis_margin_bottom + (int) current_bounds.getHeight() + 2 * std_separator);
      backing_g.setColor(Color.YELLOW.darker().darker());
      backing_g.drawString(current_date_str,
          current_interval_pos - (int) (current_bounds_date.getWidth() / 2),
          3 + ((int) current_bounds.getHeight()) + dimension.height - axis_margin_bottom + (int) current_bounds.getHeight() + 2 * std_separator);
      if (current_interval_pos - current_bounds.getWidth() / 2 < axis_margin_left)
        stop = true;
      current_interval_pos -= pixels_per_interval;
      current_time_to_display -= _getDelayPerInterval();
    }
    backing_g.setClip(null);

    vinterval_pos = dimension.height - axis_margin_bottom - pixels_per_vinterval;
    int value = value_per_vinterval;
    while (vinterval_pos > axis_margin_top) {
      backing_g.setColor(Color.YELLOW.darker());
      backing_g.drawLine(axis_margin_left - std_separator, vinterval_pos,
          dimension.width - axis_margin_right, vinterval_pos);
      final String value_str;
      if (value >= 1000000) value_str = "" + value / 1000000 + "M";
      else if (value >= 1000) value_str = "" + value / 1000 + "k";
      else value_str = "" + value;
      final TextLayout current_layout = new TextLayout(value_str, backing_g.getFont(), backing_g.getFontRenderContext());
      final Rectangle2D current_bounds = current_layout.getBounds();
      backing_g.setColor(Color.YELLOW.darker());
      backing_g.drawString(value_str,
          axis_margin_left - (int) current_bounds.getWidth() - 2 * std_separator,
          vinterval_pos + (int) (current_bounds.getHeight() / 2));
      vinterval_pos -= pixels_per_vinterval;
View Full Code Here

Examples of java.awt.font.TextLayout

            descText);
        attributedDescription.addAttribute(TextAttribute.FONT, font);
        LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(
            attributedDescription.getIterator(), frc);
        while (true) {
          TextLayout tl = lineBreakMeasurer.nextLayout(descTextWidth);
          if (tl == null)
            break;
          descriptionTextHeight += fontHeight;
        }
        // add an empty line after the paragraph
        descriptionTextHeight += fontHeight;
      }
      // remove the empty line after the last paragraph
      descriptionTextHeight -= fontHeight;

      if (tooltipInfo.getMainImage() != null) {
        height += Math.max(descriptionTextHeight, new JLabel(
            new ImageIcon(tooltipInfo.getMainImage()))
            .getPreferredSize().height);
      } else {
        height += descriptionTextHeight;
      }

      if ((tooltipInfo.getFooterImage() != null)
          || (tooltipInfo.getFooterSections().size() > 0)) {
        height += gap;
        // The footer separator
        height += new JSeparator(JSeparator.HORIZONTAL)
            .getPreferredSize().height;

        height += gap;

        int footerTextHeight = 0;
        int availableWidth = descTextWidth;
        if (tooltipInfo.getFooterImage() != null) {
          availableWidth -= tooltipInfo.getFooterImage().getWidth(
              null);
        }
        if (tooltipInfo.getMainImage() != null) {
          availableWidth += tooltipInfo.getMainImage().getWidth(null);
        }
        for (String footerText : tooltipInfo.getFooterSections()) {
          AttributedString attributedDescription = new AttributedString(
              footerText);
          attributedDescription
              .addAttribute(TextAttribute.FONT, font);
          LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(
              attributedDescription.getIterator(), frc);
          while (true) {
            TextLayout tl = lineBreakMeasurer
                .nextLayout(availableWidth);
            if (tl == null)
              break;
            footerTextHeight += fontHeight;
          }
View Full Code Here

Examples of java.awt.font.TextLayout

        attributedDescription.addAttribute(TextAttribute.FONT, font);
        LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(
            attributedDescription.getIterator(), frc);
        int currOffset = 0;
        while (true) {
          TextLayout tl = lineBreakMeasurer
              .nextLayout(descLabelWidth);
          if (tl == null)
            break;
          int charCount = tl.getCharacterCount();
          String line = descText.substring(currOffset, currOffset
              + charCount);

          JLabel descLabel = new JLabel(line);
          descriptionLabels.add(descLabel);
          richTooltipPanel.add(descLabel);
          descLabel.setBounds(x, y,
              descLabel.getPreferredSize().width, fontHeight);
          y += descLabel.getHeight();

          currOffset += charCount;
        }
        // add an empty line after the paragraph
        y += titleLabel.getHeight();
      }
      // remove the empty line after the last paragraph
      y -= titleLabel.getHeight();

      if (mainImageLabel != null) {
        y = Math.max(y, mainImageLabel.getY()
            + mainImageLabel.getHeight());
      }

      if ((tooltipInfo.getFooterImage() != null)
          || (tooltipInfo.getFooterSections().size() > 0)) {
        y += gap;
        // The footer separator
        footerSeparator = new JSeparator(JSeparator.HORIZONTAL);
        richTooltipPanel.add(footerSeparator);
        footerSeparator.setBounds(ins.left, y, parent.getWidth()
            - ins.left - ins.right, footerSeparator
            .getPreferredSize().height);

        y += footerSeparator.getHeight() + gap;

        // The footer image
        x = ins.left;
        if (tooltipInfo.getFooterImage() != null) {
          footerImageLabel = new JLabel(new ImageIcon(tooltipInfo
              .getFooterImage()));
          richTooltipPanel.add(footerImageLabel);
          footerImageLabel.setBounds(x, y, footerImageLabel
              .getPreferredSize().width, footerImageLabel
              .getPreferredSize().height);
          x += footerImageLabel.getWidth() + 2 * gap;
        }

        // The footer text
        int footerLabelWidth = parent.getWidth() - x - ins.right;
        for (String footerText : tooltipInfo.getFooterSections()) {
          AttributedString attributedDescription = new AttributedString(
              footerText);
          attributedDescription
              .addAttribute(TextAttribute.FONT, font);
          LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(
              attributedDescription.getIterator(), frc);
          int currOffset = 0;
          while (true) {
            TextLayout tl = lineBreakMeasurer
                .nextLayout(footerLabelWidth);
            if (tl == null)
              break;
            int charCount = tl.getCharacterCount();
            String line = footerText.substring(currOffset,
                currOffset + charCount);

            JLabel footerLabel = new JLabel(line);
            footerLabels.add(footerLabel);
View Full Code Here

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

Examples of java.awt.font.TextLayout

        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

Examples of java.awt.font.TextLayout

        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

Examples of java.awt.font.TextLayout

    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

Examples of java.awt.font.TextLayout

      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

Examples of java.awt.font.TextLayout

            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

Examples of java.awt.font.TextLayout

        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
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.