Package java.awt.font

Examples of java.awt.font.LineBreakMeasurer


                    lineBreakTLs = new Vector();
                    for ( int i = 0; i < fileText.length; i++ ) {
                        AttributedString as =
                          new AttributedString( fileText[i], g2.getFont().getAttributes() );

                        LineBreakMeasurer lbm =
                          new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );

                        while ( lbm.getPosition() < fileText[i].length() )
                          lineBreakTLs.add( lbm.nextLayout( (float) w ));

                    }
                }
                if ( !isPrinting )
                  resetScrollbar( verticalBar.getValue() );
View Full Code Here


        Rectangle2D anchor = _shape.getLogicalAnchor2D();

        float textHeight = 0;
        ArrayList lines = new ArrayList();
        LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext());
        measurer.setPosition(paragraphStart);
        while (measurer.getPosition() < paragraphEnd) {
            int startIndex = measurer.getPosition();
            int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);

            boolean prStart = text.charAt(startIndex) == '\n';
            if(prStart) measurer.setPosition(startIndex++);

            RichTextRun rt = run.getRichTextRunAt(startIndex == text.length() ? (startIndex-1) : startIndex);
            if(rt == null) {
                logger.log(POILogger.WARN,  "RichTextRun not found at pos" + startIndex + "; text.length: " + text.length());
                break;
            }

            float wrappingWidth = (float)anchor.getWidth() - _shape.getMarginLeft() - _shape.getMarginRight();
            int bulletOffset = rt.getBulletOffset();
            int textOffset = rt.getTextOffset();
            int indent = rt.getIndentLevel();

            TextRulerAtom ruler = run.getTextRuler();
            if(ruler != null) {
                int bullet_val = ruler.getBulletOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI;
                int text_val = ruler.getTextOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI;
                if(bullet_val > text_val){
                    int a = bullet_val;
                    bullet_val = text_val;
                    text_val = a;
                }
                if(bullet_val != 0 ) bulletOffset = bullet_val;
                if(text_val != 0) textOffset = text_val;
            }

            wrappingWidth -= textOffset;

            if (_shape.getWordWrap() == TextShape.WrapNone) {
                wrappingWidth = _shape.getSheet().getSlideShow().getPageSize().width;
            }

            TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1,
                    nextBreak == -1 ? paragraphEnd : nextBreak, true);
            if (textLayout == null) {
                textLayout = measurer.nextLayout((float)anchor.getWidth(),
                    nextBreak == -1 ? paragraphEnd : nextBreak, false);
            }
            if(textLayout == null){
                logger.log(POILogger.WARN, "Failed to break text into lines: wrappingWidth: "+wrappingWidth+
                        "; text: " + rt.getText());
                measurer.setPosition(rt.getEndIndex());
                continue;
            }
            int endIndex = measurer.getPosition();

            float lineHeight = (float)textLayout.getBounds().getHeight();
            int linespacing = rt.getLineSpacing();
            if(linespacing == 0) linespacing = 100;

View Full Code Here

    AttributedString attributedString = new AttributedString(getText());
    attributedString.addAttribute(TextAttribute.FONT, getFont());
    AttributedCharacterIterator charIterator = attributedString.getIterator();
    FontRenderContext fontContext = g2d.getFontRenderContext();

    LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, BreakIterator.getWordInstance(Locale.ENGLISH), fontContext);
    lineCount = 0;
    while (measurer.getPosition() < charIterator.getEndIndex()) {
      //TextLayout textLayout = measurer.nextLayout(wrapWidth);
      lineCount++;
      if (lineCount > 2) break;
    }
    charIterator.first();
    // Use char wrap if word wrap would cause more than two lines of text.
    if (lineCount > 2)
      measurer = new LineBreakMeasurer(charIterator, BreakIterator.getCharacterInstance(Locale.ENGLISH), fontContext);
    else
      measurer.setPosition(0);
    while (measurer.getPosition() < charIterator.getEndIndex()) {
      TextLayout textLayout = measurer.nextLayout(wrapWidth);
      float ascent = textLayout.getAscent();
      textY += ascent; // Move down to baseline.

      g2d.setColor(glowColor);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
View Full Code Here

        String text = getRenderableText();
        if(text.length() == 0) return _lines;

        AttributedString at = getAttributedString(graphics);
        AttributedCharacterIterator it = at.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext())  ;
        for (;;) {
            int startIndex = measurer.getPosition();

            double wrappingWidth = getWrappingWidth(_lines.size() == 0, graphics) + 1; // add a pixel to compensate rounding errors
            // shape width can be smaller that the sum of insets (this was proved by a test file)
            if(wrappingWidth < 0) wrappingWidth = 1;

            int nextBreak = text.indexOf('\n', startIndex + 1);
            if(nextBreak == -1) nextBreak = it.getEndIndex();

            TextLayout layout = measurer.nextLayout((float)wrappingWidth, nextBreak, true);
            if (layout == null) {
                 // layout can be null if the entire word at the current position
                 // does not fit within the wrapping width. Try with requireNextWord=false.
                 layout = measurer.nextLayout((float)wrappingWidth, nextBreak, false);
            }

            if(layout == null) {
                // exit if can't break any more
                break;
            }

            int endIndex = measurer.getPosition();
            // skip over new line breaks (we paint 'clear' text runs not starting or ending with \n)
            if(endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n'){
                measurer.setPosition(endIndex + 1);
            }

            TextAlign hAlign = getTextAlign();
            if(hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) {
                layout = layout.getJustifiedLayout((float)wrappingWidth);
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 <= boxHeight) {
      
      int previousPosition = measurer.getPosition();
     
      // Request next layout
      layout = measurer.nextLayout(boxWidth);
     
      int height = ((Float)(layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue();
     
      if(currentHeight + height > boxHeight) {
        // 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
        if (layouts.size() > 0) {
          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 as = new AttributedString(text);
      as.addAttribute(TextAttribute.FOREGROUND, g.getPaint());
      as.addAttribute(TextAttribute.FONT, g.getFont());
      AttributedCharacterIterator aci = as.getIterator();
      FontRenderContext frc = new FontRenderContext(null, true, false);
      LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
     
      while (lbm.getPosition() < text.length()) {
        TextLayout tl = lbm.nextLayout(wrapWidth);
        textY += tl.getAscent();
        Rectangle2D bb = tl.getBounds();
        int tX = (int) graphicInfo.getX();
        if (centered)
            tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2);
View Full Code Here

            AttributedString attrStr = new AttributedString(text);
            attrStr.addAttribute(TextAttribute.FONT, currFont);
   
            // Use LineBreakMeasurer to count number of lines needed for the text
            FontRenderContext frc = new FontRenderContext(null, true, true);
            LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(),
            frc);
            int nextPos = 0;
            int lineCnt = 0;
            while (measurer.getPosition() < text.length())  {
                nextPos = measurer.nextOffset(width/43);            // mergedCellWidth is the max width of each line
                lineCnt++;
                measurer.setPosition(nextPos);
            }
            if(255*lineCnt > r.getHeight()){
                r.setHeight((short)(255 * lineCnt));
            }
        }
View Full Code Here

            AttributedString attrStr = new AttributedString(text);
            attrStr.addAttribute(TextAttribute.FONT, currFont);
   
            // Use LineBreakMeasurer to count number of lines needed for the text
            FontRenderContext frc = new FontRenderContext(null, true, true);
            LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(),
            frc);
            int nextPos = 0;
            int lineCnt = 0;
            while (measurer.getPosition() < text.length())  {
                nextPos = measurer.nextOffset(width/43);            // mergedCellWidth is the max width of each line
                lineCnt++;
                measurer.setPosition(nextPos);
            }
            if(255*lineCnt > r.getHeight()){
                r.setHeight((short)(255 * lineCnt));
            }
        }
View Full Code Here

            AttributedString attrStr = new AttributedString(text);
            attrStr.addAttribute(TextAttribute.FONT, currFont);
   
            // Use LineBreakMeasurer to count number of lines needed for the text
            FontRenderContext frc = new FontRenderContext(null, true, true);
            LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(),
            frc);
            int nextPos = 0;
            int lineCnt = 0;
            while (measurer.getPosition() < text.length())  {
                nextPos = measurer.nextOffset(width/43);            // mergedCellWidth is the max width of each line
                lineCnt++;
                measurer.setPosition(nextPos);
            }
            if(255*lineCnt > r.getHeight()){
                r.setHeight((short)(255 * lineCnt));
            }
        }
View Full Code Here

             return;
         }
       
         AttributedCharacterIterator aci = this.atribStr.getIterator();
         FontRenderContext frc = g.getFontRenderContext();
         LineBreakMeasurer lbm;
         try{
            lbm = new LineBreakMeasurer(aci, frc);
         }catch(Exception e){
             e.printStackTrace();
             return;
         }

         // Compute text font size
         float drawPosY;
         float fontReduction = 0f;
         int limit;
         do {

getFullWord: {
   
             this.atribStr.addAttribute(TextAttribute.SIZE, getFontSize() - fontReduction);
             lbm = new LineBreakMeasurer(aci, frc);
             fontReduction += 1f;
             drawPosY = 0;
             while (lbm.getPosition() < formatedString.length() ) {
                 //TextLayout layout = lbm.nextLayout(this.getWidth());
                
                 limit = lbm.getPosition() + formatedString.substring( lbm.getPosition() ).indexOf('\n')+1;
                 if (limit <= lbm.getPosition()){
                     limit = formatedString.length();
                 }
                 TextLayout layout = lbm.nextLayout(this.getWidth(), limit, true);
                
                 if (layout == null){
                     break getFullWord;
                 }
                
                 drawPosY += layout.getAscent();
                 drawPosY += layout.getDescent();
                 drawPosY += layout.getLeading();
             }
}

         }while ( (lbm.getPosition() < formatedString.length()) || ( drawPosY > this.getHeight() && fontReduction < getFontSize()));
       
         // do the paiting
         aci = this.atribStr.getIterator();
         lbm.setPosition(0);
         if ( vertAlign == VALIGN_MIDDLE ){
             drawPosY = (this.getHeight() - drawPosY) / 2;
         } else if (vertAlign == VALIGN_BOTTOM){
             drawPosY = this.getHeight() - drawPosY;
         } else {
             drawPosY = 0;
         }
        
         Color oldColor = g.getColor();
         Composite oldComposite = g.getComposite();
         try{
             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
             g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
             g.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.getAlpha() * time ) );
             while (lbm.getPosition() < formatedString.length() ) {

                 limit = lbm.getPosition() + formatedString.substring( lbm.getPosition() ).indexOf('\n')+1;
                 if (limit <= lbm.getPosition()){
                     limit = formatedString.length();
                 }
                 TextLayout layout = lbm.nextLayout(this.getWidth(), limit, true);

                 drawPosY += layout.getAscent();

                 // default alignment = Left
                 float drawPosX = this.getLeft();
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.