Package java.awt.font

Examples of java.awt.font.LineBreakMeasurer


    float bottomBoundary = 0;
    float topBoundary = 0;

    for (int i = 0; i < iterators.size(); i++) {
      AttributedCharacterIterator iterator = (AttributedCharacterIterator) iterators.elementAt(i);
      LineBreakMeasurer measurer = (LineBreakMeasurer) measurers.elementAt(i);

      int paragraphStart = iterator.getBeginIndex();
      int paragraphEnd = iterator.getEndIndex();

      if (measurer != null) measurer.setPosition(paragraphStart);
      //float bottomBoundary = 0;
      //float topBoundary = 0;

      cursorIndex = 0;
      currentText = null;

      // Get lines from lineMeasurer until the entire
      // paragraph has been displayed.
      if (measurer != null) {
        while (measurer.getPosition() < paragraphEnd) {

          // Retrieve next layout.
          TextLayout layout = measurer.nextLayout(textWidth);
          bottomBoundary = topBoundary + lineHeight;

          if ((clickY > topBoundary) && (clickY < bottomBoundary)) {
            // Get the character position of the mouse click.
            TextHitInfo currentHit = layout.hitTestChar((float)clickX, (float)clickY);
            if (currentHit != null) {
              this.currentText = (StringBuffer)buffers.elementAt(i);
              cursorIndex += currentHit.getInsertionIndex();
              return;
            }
          } else {
            cursorIndex = measurer.getPosition();
          }
          topBoundary = bottomBoundary;
        }
        topBoundary = topBoundary + lineHeight/2;
      }
View Full Code Here


   
      // define a fonte do texto como arial 12 it�lico
      str.addAttribute(TextAttribute.FONT, new Font("Arial", Font.ITALIC, 12));

      // instancia um line breaker para o texto formatado
      LineBreakMeasurer quebrador = new LineBreakMeasurer(str.getIterator(),
          gr.getFontRenderContext());   
   
      // cria um TextLayout para armazenar cada linha 'quebrada'
      TextLayout linha = quebrador.nextLayout(larguraTexto);
      while (linha != null) {

        // posiciona o texto
        posY += linha.getAscent();
       
        linha.draw(gr, posX, posY);
       
        // soma espa�o para a proxima linha
        posY += linha.getDescent() + linha.getLeading();
       
        linha = quebrador.nextLayout(larguraTexto);
      }
     
    } // fim dos paragrafos
   
    return PAGE_EXISTS;
View Full Code Here

            AttributedString attributedText = new AttributedString(text);
            attributedText.addAttribute(TextAttribute.FONT, font);

            AttributedCharacterIterator aci = attributedText.getIterator();
            LineBreakMeasurer lbm = new LineBreakMeasurer(aci, fontRenderContext);

            float lineHeights = 0;
            while (lbm.getPosition() < aci.getEndIndex()) {
                int offset = lbm.nextOffset(contentWidth);

                LineMetrics lm = font.getLineMetrics(aci,
                    lbm.getPosition(), offset, fontRenderContext);

                float lineHeight = lm.getAscent() + lm.getDescent()
                    + lm.getLeading();
                lineHeights += lineHeight;

                lbm.setPosition(offset);
            }

            preferredHeight = (int)Math.ceil(lineHeights);
        } else {
            LineMetrics lm = font.getLineMetrics(text, fontRenderContext);
View Full Code Here

            if (wrapText) {
                AttributedString attributedText = new AttributedString(text);
                attributedText.addAttribute(TextAttribute.FONT, font);

                AttributedCharacterIterator aci = attributedText.getIterator();
                LineBreakMeasurer lbm = new LineBreakMeasurer(aci, fontRenderContext);

                int contentWidth = width - (padding.left + padding.right);

                while (lbm.getPosition() < aci.getEndIndex()) {
                    TextLayout textLayout = lbm.nextLayout(contentWidth);
                    y += textLayout.getAscent();
                    drawText(graphics, textLayout, y);
                    y += textLayout.getDescent() + textLayout.getLeading();
                }
            } else {
View Full Code Here

        attributedString.addAttribute( TextAttribute.FOREGROUND, fgColor );

        FontRenderContext context = g2d.getFontRenderContext();

        AttributedCharacterIterator iterator = attributedString.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer( iterator, context );

        TextLayout layout = null;
        // precalculating the render pictureheight
        double renderHeight = padTop + padBottom;
        int numRows = 0;
        while ( measurer.getPosition() < iterator.getEndIndex() )
        {
            if ( ( layout = measurer.nextLayout( wrappingWidth ) ) == null || ( numRows >= maxRows ) )
            {
                break;
            }
            numRows++;
            renderHeight += layout.getAscent() + layout.getDescent() + layout.getLeading();
        }

        renderedImage = new BufferedImage( renderWidth, (int)( renderHeight + 0.5 ), templateImage.getType() );
        Graphics2D img2d = renderedImage.createGraphics();
        img2d.setRenderingHints( renderHints );

        img2d.setColor( fgColor );

        checkAndSetBackground();

        Point2D.Float pen = new Point2D.Float( padLeft, padTop );

        context = img2d.getFontRenderContext();
        iterator = attributedString.getIterator();
        measurer = new LineBreakMeasurer( iterator, context );
        numRows = 0;
        while ( measurer.getPosition() < iterator.getEndIndex() )
        {
            if ( ( layout = measurer.nextLayout( wrappingWidth ) ) == null || ( numRows >= maxRows ) )
            {
                break;
            }
            numRows++;
            pen.y += layout.getAscent();
View Full Code Here

        maxLineLength = 0;
        maxLineHeight = 0;
        nbrLines = 0;
        AttributedCharacterIterator paragraph = styledText.getIterator(null, 0, plainText.length());
        FontRenderContext frc = g2d.getFontRenderContext();
        lineMeasurer = new LineBreakMeasurer(paragraph, frc);
        float yposinpara = 0;
        int charssofar = 0;
        while (lineMeasurer.getPosition() < plainText.length()) {
          TextLayout layout = lineMeasurer.nextLayout(wrapWidth);
          float advance = layout.getVisibleAdvance();
View Full Code Here

    int currentHeight = 0;
    // Prepare a list of lines of text we'll be drawing
    List<TextLayout> layouts = new ArrayList<TextLayout>();
    String lastLine = null;
   
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext());
   
    TextLayout layout = null;
    while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= availableHeight) {
      
      int previousPosition = measurer.getPosition();
     
      // Request next layout
      layout = measurer.nextLayout(width);
     
      int height = ((Float)(layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue();
     
      if(currentHeight + height > availableHeight) {
        // The line we're about to add should NOT be added anymore, append three dots to previous one instead
        // to indicate more text is truncated
        layouts.remove(layouts.size() - 1);
       
        if(lastLine.length() >= 4) {
          lastLine = lastLine.substring(0, lastLine.length() - 4) + "...";
        }
        layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext()));
      } else {
        layouts.add(layout);
        lastLine = text.substring(previousPosition, measurer.getPosition());
        currentHeight += height;
      }
    }
   
   
View Full Code Here

                AttributedString attributedText = new AttributedString(text);
                attributedText.addAttribute(TextAttribute.FONT, font);

                AttributedCharacterIterator aci = attributedText.getIterator();
                LineBreakMeasurer lbm = new LineBreakMeasurer(aci, fontRenderContext);

                float lineHeights = 0;
                while (lbm.getPosition() < aci.getEndIndex()) {
                    TextLayout line = lbm.nextLayout(width);
                    lines.add(line);
                    lineHeights += line.getAscent() + line.getDescent()
                        + line.getLeading();
                }
View Full Code Here

      int descriptionTextHeight = 0;
      for (String descText : tooltipInfo.getDescriptionSections()) {
        AttributedString attributedDescription = new AttributedString(
            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

      int descLabelWidth = parent.getWidth() - x - ins.right;
      for (String descText : tooltipInfo.getDescriptionSections()) {
        AttributedString attributedDescription = new AttributedString(
            descText);
        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,
View Full Code Here

TOP

Related Classes of java.awt.font.LineBreakMeasurer

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.