Example usage for java.text AttributedCharacterIterator getEndIndex

List of usage examples for java.text AttributedCharacterIterator getEndIndex

Introduction

In this page you can find the example usage for java.text AttributedCharacterIterator getEndIndex.

Prototype

public int getEndIndex();

Source Link

Document

Returns the end index of the text.

Usage

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Create an image that contains text/*from www .  j a  v a2s.  c  om*/
 * 
 * @param height
 * @param width
 * @param text
 * @param textColor
 * @param center
 * @param fontMap
 * @param type
 * @return
 */
private BufferedImage createText(int height, int width, String text, String textColor, boolean center,
        Map<TextAttribute, Object> fontMap, int type) {
    BufferedImage img = new BufferedImage(width, height, type);
    Graphics2D g2d = null;
    try {
        g2d = (Graphics2D) img.getGraphics();

        // Create attributed text
        AttributedString txt = new AttributedString(text, fontMap);

        // Set graphics color
        g2d.setColor(Color.decode(textColor));

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

        // Set break width to width of Component.
        float breakWidth = (float) width;
        float drawPosY = 0;
        // Set position to the index of the first character in the
        // paragraph.
        lineMeasurer.setPosition(paragraphStart);

        // Get lines until the entire paragraph has been displayed.
        while (lineMeasurer.getPosition() < paragraphEnd) {
            // Retrieve next layout. A cleverer program would also cache
            // these layouts until the component is re-sized.
            TextLayout layout = lineMeasurer.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: drawPosX is always where the LEFT of the text is
            // placed.
            float drawPosX = layout.isLeftToRight() ? 0 : breakWidth - layout.getAdvance();

            if (center == true) {
                double xOffSet = (width - layout.getBounds().getWidth()) / 2;
                drawPosX = drawPosX + new Float(xOffSet);
            }

            // Move y-coordinate by the ascent of the layout.
            drawPosY += layout.getAscent();

            // Draw the TextLayout at (drawPosX, drawPosY).
            layout.draw(g2d, drawPosX, drawPosY);

            // Move y-coordinate in preparation for next layout.
            drawPosY += layout.getDescent() + layout.getLeading();
        }
    } finally {
        if (g2d != null) {
            g2d.dispose();
        }
    }
    return img;
}

From source file:com.github.lucapino.sheetmaker.renderer.JavaTemplateRenderer.java

public void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle,
        boolean multiline) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();/*  w  w w  .  j a va 2s  .co  m*/
    if (angle != 0) {
        g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle))));
    }

    Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2);
    Point2D pos = getPoint(bounds, align);
    double x = pos.getX();
    double y = pos.getY() + sSize.getHeight();

    switch (align) {
    case TopCenter:
    case BottomCenter:
    case Center:
        x -= (sSize.getWidth() / 2);
        break;
    case TopRight:
    case MiddleRight:
    case BottomRight:
        x -= (sSize.getWidth());
        break;
    case BottomLeft:
    case MiddleLeft:
    case TopLeft:
        break;
    }
    if (multiline) {
        // Create a new LineBreakMeasurer from the paragraph.
        // It will be cached and re-used.
        //if (lineMeasurer == null) {
        AttributedCharacterIterator paragraph = new AttributedString(text).getIterator();
        paragraphStart = paragraph.getBeginIndex();
        paragraphEnd = paragraph.getEndIndex();
        FontRenderContext frc = g2.getFontRenderContext();
        lineMeasurer = new LineBreakMeasurer(paragraph, frc);
        //}

        // Set break width to width of Component.
        float breakWidth = (float) bounds.getWidth();
        float drawPosY = (float) y;
        // Set position to the index of the first character in the paragraph.
        lineMeasurer.setPosition(paragraphStart);

        // Get lines until the entire paragraph has been displayed.
        while (lineMeasurer.getPosition() < paragraphEnd) {

            // Retrieve next layout. A cleverer program would also cache
            // these layouts until the component is re-sized.
            TextLayout layout = lineMeasurer.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.
            float drawPosX = layout.isLeftToRight() ? (float) x : (float) x + breakWidth - layout.getAdvance();

            // Move y-coordinate by the ascent of the layout.
            drawPosY += layout.getAscent();

            // Draw the TextLayout at (drawPosX, drawPosY).
            layout.draw(g2, drawPosX, drawPosY);

            // Move y-coordinate in preparation for next layout.
            drawPosY += layout.getDescent() + layout.getLeading();
        }
    } else {
        g2.drawString(text, (float) x, (float) y);
    }
    g2.setFont(font);
}

From source file:net.sf.jasperreports.engine.util.JRStyledTextParser.java

/**
 * Outputs a styled text String given a set of element-level styled text
 * attributes and a styled text in the form of a String text and an iterator
 * of style attributes.// ww w.j  av  a 2  s . co m
 * 
 * @param parentAttrs the element-level styled text attributes
 * @param iterator iterator of styled text attributes
 * @param text the text
 * @return the String styled text representation
 */
public String write(Map<Attribute, Object> parentAttrs, AttributedCharacterIterator iterator, String text) {
    StringBuilder sb = new StringBuilder();

    int runLimit = 0;

    while (runLimit < iterator.getEndIndex() && (runLimit = iterator.getRunLimit()) <= iterator.getEndIndex()) {
        String chunk = text.substring(iterator.getIndex(), runLimit);
        Map<Attribute, Object> attrs = iterator.getAttributes();

        StringBuilder styleBuilder = writeStyleAttributes(parentAttrs, attrs);
        if (styleBuilder.length() > 0) {
            sb.append(LESS);
            sb.append(NODE_style);
            sb.append(styleBuilder.toString());
            sb.append(GREATER);
            writeChunk(sb, parentAttrs, attrs, chunk);
            sb.append(LESS_SLASH);
            sb.append(NODE_style);
            sb.append(GREATER);
        } else {
            writeChunk(sb, parentAttrs, attrs, chunk);
        }

        iterator.setIndex(runLimit);
    }

    return sb.toString();
}

From source file:org.jfree.experimental.swt.SWTGraphics2D.java

/**
 * Draws a string at the specified position.
 *
 * @param iterator  the string./*from  w w  w . j a  va2  s  . co m*/
 * @param x  the x-coordinate.
 * @param y  the y-coordinate.
 */
public void drawString(AttributedCharacterIterator iterator, int x, int y) {
    // for now we simply want to extract the chars from the iterator
    // and call an unstyled text renderer
    StringBuffer sb = new StringBuffer();
    int numChars = iterator.getEndIndex() - iterator.getBeginIndex();
    char c = iterator.first();
    for (int i = 0; i < numChars; i++) {
        sb.append(c);
        c = iterator.next();
    }
    drawString(new String(sb), x, y);
}

From source file:org.apache.fop.svg.AbstractFOPTextPainter.java

/**
 * Extract the raw text from an ACI./*from  w  w w.j ava 2  s.c o  m*/
 * @param aci ACI to inspect
 * @return the extracted text
 */
protected String getText(AttributedCharacterIterator aci) {
    StringBuffer sb = new StringBuffer(aci.getEndIndex() - aci.getBeginIndex());
    for (char c = aci.first(); c != CharacterIterator.DONE; c = aci.next()) {
        sb.append(c);
    }
    return sb.toString();
}

From source file:org.apache.fop.svg.NativeTextPainter.java

/**
 * Collects all characters from an {@link AttributedCharacterIterator}.
 * @param runaci the character iterator/* ww w.ja  v  a2  s  .  c  om*/
 * @return the characters
 */
protected CharSequence collectCharacters(AttributedCharacterIterator runaci) {
    StringBuffer chars = new StringBuffer();
    for (runaci.first(); runaci.getIndex() < runaci.getEndIndex();) {
        chars.append(runaci.current());
        runaci.next();
    }
    return chars;
}

From source file:org.apache.fop.svg.NativeTextPainter.java

/**
 * @param runaci an attributed character iterator
 * @param layout a text span layout/*  w  w w .  j  a v a2 s .c  o  m*/
 */
protected final void logTextRun(AttributedCharacterIterator runaci, TextSpanLayout layout) {
    if (log.isTraceEnabled()) {
        int charCount = runaci.getEndIndex() - runaci.getBeginIndex();
        log.trace("================================================");
        log.trace("New text run:");
        log.trace("char count: " + charCount);
        log.trace("range: " + runaci.getBeginIndex() + " - " + runaci.getEndIndex());
        log.trace("glyph count: " + layout.getGlyphCount()); //=getNumGlyphs()
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)
 *///from  www  .j av  a 2s . c  o m
@Override
public void drawString(final AttributedCharacterIterator iter, float x, final float y) {
    /*
     * StringBuffer sb = new StringBuffer(); for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c =
     * iter.next()) { sb.append(c); } drawString(sb.toString(),x,y);
     */
    final StringBuilder stringbuffer = new StringBuilder(iter.getEndIndex());
    for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
        if (iter.getIndex() == iter.getRunStart()) {
            if (stringbuffer.length() > 0) {
                drawString(stringbuffer.toString(), x, y);
                final FontMetrics fontmetrics = getFontMetrics();
                x = (float) (x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
                stringbuffer.delete(0, stringbuffer.length());
            }
            doAttributes(iter);
        }
        stringbuffer.append(c);
    }

    drawString(stringbuffer.toString(), x, y);
    underline = false;
}

From source file:ru.runa.wfe.graph.image.figure.AbstractFigure.java

private int drawText(Graphics2D graphics, String text, int hOffset) {
    Rectangle r = getTextBoundsRectangle();
    Rectangle2D textBounds = graphics.getFontMetrics().getStringBounds(text, graphics);
    if (textBounds.getWidth() > r.getWidth() - 4) {
        int y = coords[1] + hOffset;
        AttributedString attributedString = new AttributedString(text);
        attributedString.addAttribute(TextAttribute.FONT, graphics.getFont());
        AttributedCharacterIterator characterIterator = attributedString.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, graphics.getFontRenderContext());
        while (measurer.getPosition() < characterIterator.getEndIndex()) {
            TextLayout textLayout = measurer.nextLayout((float) r.getWidth() - 4);
            y += textLayout.getAscent();
            float x = (float) (r.getCenterX() + 2 - textLayout.getBounds().getCenterX());
            textLayout.draw(graphics, x, y);
            y += textLayout.getDescent() + textLayout.getLeading();
        }//from w w  w . j  a  v  a2 s  . com
        return y - coords[1];
    } else {
        graphics.drawString(text, (float) (r.getCenterX() + 2 - textBounds.getCenterX()),
                (float) (coords[1] + textBounds.getHeight() + hOffset));
        return (int) (textBounds.getHeight() + hOffset + 3);
    }
}