Package java.awt.font

Examples of java.awt.font.LineBreakMeasurer


    }

    public void format(FontRenderContext frc, double width) {
        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


        return height;
    }

    public void draw(Graphics2D g, double x, double y, double width, int align) {
        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

            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                    RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            FontRenderContext frc = g2d.getFontRenderContext();
            LineBreakMeasurer lbm = new LineBreakMeasurer(
                    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

  {
    this.wrappingWidth = (float) StrictGeomUtility.toExternalValue(box.getCachedWidth());
    this.justifiedLayout =
        ElementAlignment.JUSTIFY.equals(box.getStyleSheet().getStyleProperty(ElementStyleKeys.ALIGNMENT));
    this.ci = ci;
    this.lineBreakMeasurer = new LineBreakMeasurer(ci, fontRenderContext);
    this.lineBreakMeasurer.setPosition(ci.getBeginIndex());
  }
View Full Code Here

        AttributedCharacterIterator it = at.getIterator();
        int paragraphStart = it.getBeginIndex();
        int paragraphEnd = it.getEndIndex();

        ArrayList lines = new ArrayList();
        LineBreakMeasurer measurer = new LineBreakMeasurer(it, frc);
        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 = textWidth - _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;
            }

            if(bulletOffset > 0 || prStart || startIndex == 0) 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(textWidth,
                    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

            if (lineMeasurer == null) {
                AttributedCharacterIterator paragraph = attStr.getIterator();
                paragraphStart = paragraph.getBeginIndex();
                paragraphEnd = paragraph.getEndIndex();
                FontRenderContext frc = g2d.getFontRenderContext();
                lineMeasurer = new LineBreakMeasurer(paragraph, frc);
            }

            // Set break width to width of Component.
            breakWidth = itModel.getTextBoxWidth();
            float drawPosY = desty;
View Full Code Here

                if (rectMeasurer == null) {
                    AttributedCharacterIterator paragraph = attStr.getIterator();
                    paragraphStart = paragraph.getBeginIndex();
                    paragraphEnd = paragraph.getEndIndex();
                    FontRenderContext frc = g2d.getFontRenderContext();
                    rectMeasurer = new LineBreakMeasurer(paragraph, frc);
                }

                // Set break width to width of Component.
                breakWidth = itModel.getTextBoxWidth();
               
                // Set position to the index of the first character in the paragraph.
                rectMeasurer.setPosition(paragraphStart);

                // make a copy of measurer and loop through to get dimensions
                // calculate the rectangle size ant set variables
                LineBreakMeasurer lmCopy = rectMeasurer;
                // Get lines until the entire paragraph has been displayed.
                while (lmCopy.getPosition() < paragraphEnd) {

                    // Retrieve next layout. A cleverer program would also cache
                    // these layouts until the component is re-sized.
                    TextLayout layout = lmCopy.nextLayout(breakWidth);

                    // Compute pen x position. If the paragraph is right-to-left we
                    // will align the TextLayouts to the right edge of the panel.
                    // Note: this won't occur for the English text in this sample.
                    // Note: drawPosX is always where the LEFT of the text is placed.
                    // TODO adjust the RightToLeft case with destx also
                    // LeftToRight has been fixed
                    rectPosX = layout.isLeftToRight()
                            ? (0 + destx) : breakWidth - layout.getAdvance();
                    // Move y-coordinate by the ascent of the layout.
                    rectPosY += layout.getAscent();
                    // Draw the TextLayout at (drawPosX, drawPosY).
                    // layout.draw(g2d, drawPosX, drawPosY);
                    // Move y-coordinate in preparation for next layout.
                    rectPosY += layout.getDescent() + layout.getLeading();
                    // add the y move to rectangle height
                    System.out.println("rectHeight was " + rectHeight);
                    rectHeight += layout.getAscent() + layout.getDescent() +
                            layout.getLeading();
                    System.out.println("rectHeight is now " + rectHeight);
                    // if text line width is greater than rectWidth
                    // set rectWidth to line width
                    if (layout.getAdvance() > rectWidth) {
                        rectWidth = layout.getAdvance();
                    }
                }

                System.out.println("rectangle position and size= " +
                        rectPosX + ", " + desty + ", " +
                        rectWidth + ", " + rectHeight);
                // draw a rectangle around text with padding
                g2d.fill(new RoundRectangle2D.Double((destx - 4) , (desty - 4),
                        (rectWidth + 8),
                        (rectHeight + 8), 10, 10));

                // finish
                lmCopy = null;
                rectMeasurer = null;

                // end text rectangle

                // color for text
                g2d.setColor(itModel.getColor());

                // Create a new LineBreakMeasurer from the paragraph.
                // It will be cached and re-used.
                if (lineMeasurer == null) {
                    AttributedCharacterIterator paragraph = attStr.getIterator();
                    paragraphStart = paragraph.getBeginIndex();
                    paragraphEnd = paragraph.getEndIndex();
                    FontRenderContext frc = g2d.getFontRenderContext();
                    lineMeasurer = new LineBreakMeasurer(paragraph, frc);
                }

                // Set break width to width of Component.
                breakWidth = itModel.getTextBoxWidth();
                float drawPosY = desty;
View Full Code Here

            if ("".equals(line)) {
                cursor.y += lineHeight;
            } else {
                AttributedString styledText = new AttributedString(line);
                LineBreakMeasurer measurer = new LineBreakMeasurer(styledText.getIterator(), frc);

                while (measurer.getPosition() < line.length()) {

                    TextLayout layout = measurer.nextLayout(lineWidth);

                    cursor.y += (layout.getAscent());
                    float dx = layout.isLeftToRight() ? 0 : (lineWidth - layout.getAdvance());

                    layout.draw(g, cursor.x + dx, cursor.y);
View Full Code Here

        AttributedCharacterIterator it = at.getIterator();
        int paragraphStart = it.getBeginIndex();
        int paragraphEnd = it.getEndIndex();

        List<TextElement> lines = new ArrayList<TextElement>();
        LineBreakMeasurer measurer = new LineBreakMeasurer(it, frc);
        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 = textWidth - _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;
            }

            if(bulletOffset > 0 || prStart || startIndex == 0) 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(textWidth,
                    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

                    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

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.