Example usage for java.text AttributedCharacterIterator next

List of usage examples for java.text AttributedCharacterIterator next

Introduction

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

Prototype

public char next();

Source Link

Document

Increments the iterator's index by one and returns the character at the new index.

Usage

From source file:Main.java

/**
 * Serialises an <code>AttributedString</code> object.
 *
 * @param as  the attributed string object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*  ww  w.j ava2 s. com*/
public static void writeAttributedString(AttributedString as, ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (as != null) {
        stream.writeBoolean(false);
        AttributedCharacterIterator aci = as.getIterator();
        // build a plain string from aci
        // then write the string
        StringBuffer plainStr = new StringBuffer();
        char current = aci.first();
        while (current != CharacterIterator.DONE) {
            plainStr = plainStr.append(current);
            current = aci.next();
        }
        stream.writeObject(plainStr.toString());

        // then write the attributes and limits for each run
        current = aci.first();
        int begin = aci.getBeginIndex();
        while (current != CharacterIterator.DONE) {
            // write the current character - when the reader sees that this
            // is not CharacterIterator.DONE, it will know to read the
            // run limits and attributes
            stream.writeChar(current);

            // now write the limit, adjusted as if beginIndex is zero
            int limit = aci.getRunLimit();
            stream.writeInt(limit - begin);

            // now write the attribute set
            Map atts = new HashMap(aci.getAttributes());
            stream.writeObject(atts);
            current = aci.setIndex(limit);
        }
        // write a character that signals to the reader that all runs
        // are done...
        stream.writeChar(CharacterIterator.DONE);
    } else {
        // write a flag that indicates a null
        stream.writeBoolean(true);
    }

}

From source file:AttributedStringUtilities.java

/**
 * Tests two attributed strings for equality.
 * /*from  w ww  .  jav  a2 s.  co  m*/
 * @param s1
 *          string 1 (<code>null</code> permitted).
 * @param s2
 *          string 2 (<code>null</code> permitted).
 * 
 * @return <code>true</code> if <code>s1</code> and <code>s2</code> are
 *         equal or both <code>null</code>, and <code>false</code>
 *         otherwise.
 */
public static boolean equal(AttributedString s1, AttributedString s2) {
    if (s1 == null) {
        return (s2 == null);
    }
    if (s2 == null) {
        return false;
    }
    AttributedCharacterIterator it1 = s1.getIterator();
    AttributedCharacterIterator it2 = s2.getIterator();
    char c1 = it1.first();
    char c2 = it2.first();
    int start = 0;
    while (c1 != CharacterIterator.DONE) {
        int limit1 = it1.getRunLimit();
        int limit2 = it2.getRunLimit();
        if (limit1 != limit2) {
            return false;
        }
        // if maps aren't equivalent, return false
        Map m1 = it1.getAttributes();
        Map m2 = it2.getAttributes();
        if (!m1.equals(m2)) {
            return false;
        }
        // now check characters in the run are the same
        for (int i = start; i < limit1; i++) {
            if (c1 != c2) {
                return false;
            }
            c1 = it1.next();
            c2 = it2.next();
        }
        start = limit1;
    }
    return c2 == CharacterIterator.DONE;
}

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 ava 2  s.  c o 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./*ww w.  j a v  a  2 s  .  com*/
 * @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/*from  w  w  w. j av  a  2  s .co  m*/
 * @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.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)
 *//*from  ww  w .  j  av  a 2  s .  c  om*/
@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;
}