Search in sources :

Example 6 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project robovm by robovm.

the class OldAttributedStringTest method test_getIterator$Ljava_text_AttributedCharacterIterator$Attribute.

/**
     * java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[])
     *        Test of method
     *        java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[]).
     */
public void test_getIterator$Ljava_text_AttributedCharacterIterator$Attribute() {
    String test = "Test string";
    try {
        Map<AttributedCharacterIterator.Attribute, String> hm = new HashMap<AttributedCharacterIterator.Attribute, String>();
        AttributedCharacterIterator.Attribute[] aci = new AttributedCharacterIterator.Attribute[3];
        aci[0] = new TestAttributedCharacterIteratorAttribute("att1");
        aci[1] = new TestAttributedCharacterIteratorAttribute("att2");
        aci[2] = new TestAttributedCharacterIteratorAttribute("att3");
        hm.put(aci[0], "value1");
        hm.put(aci[1], "value2");
        AttributedString attrString = new AttributedString(test, hm);
        AttributedCharacterIterator it = attrString.getIterator(aci);
        assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[0]).equals("value1"));
        assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[1]).equals("value2"));
        assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[2]) == null);
    } catch (Exception e) {
        fail("Unexpected exceptiption " + e.toString());
    }
}
Also used : AttributedString(java.text.AttributedString) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 7 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project robovm by robovm.

the class OldDecimalFormatTest method test_formatToCharacterIterator.

public void test_formatToCharacterIterator() throws Exception {
    AttributedCharacterIterator iterator;
    int[] runStarts;
    int[] runLimits;
    String result;
    char current;
    // For BigDecimal with multiplier test.
    DecimalFormat df = new DecimalFormat();
    df.setMultiplier(10);
    iterator = df.formatToCharacterIterator(new BigDecimal("12345678901234567890"));
    result = "123,456,789,012,345,678,900";
    current = iterator.current();
    for (int i = 0; i < result.length(); i++) {
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    // For BigDecimal with multiplier test.
    df = new DecimalFormat();
    df.setMultiplier(-1);
    df.setMaximumFractionDigits(20);
    iterator = df.formatToCharacterIterator(new BigDecimal("1.23456789012345678901"));
    result = "-1.23456789012345678901";
    current = iterator.current();
    for (int i = 0; i < result.length(); i++) {
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.23456789E1234"));
    runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 };
    runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 };
    // 000,000,000,000....
    result = "12,345,678,900,";
    current = iterator.current();
    for (int i = 0; i < runStarts.length; i++) {
        assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
        assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    assertEquals(0, iterator.getBeginIndex());
    assertEquals(1646, iterator.getEndIndex());
    iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.23456789E301"));
    runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 };
    runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 };
    // 000,000,000,000....
    result = "12,345,678,900,";
    current = iterator.current();
    for (int i = 0; i < runStarts.length; i++) {
        assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
        assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    assertEquals(0, iterator.getBeginIndex());
    assertEquals(402, iterator.getEndIndex());
    iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.2345678E4"));
    runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7 };
    runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10 };
    result = "12,345.678";
    current = iterator.current();
    for (int i = 0; i < runStarts.length; i++) {
        assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
        assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    assertEquals(0, iterator.getBeginIndex());
    assertEquals(10, iterator.getEndIndex());
    iterator = new DecimalFormat().formatToCharacterIterator(new BigInteger("123456789"));
    runStarts = new int[] { 0, 0, 0, 3, 4, 4, 4, 7, 8, 8, 8 };
    runLimits = new int[] { 3, 3, 3, 4, 7, 7, 7, 8, 11, 11, 11 };
    result = "123,456,789";
    current = iterator.current();
    for (int i = 0; i < runStarts.length; i++) {
        assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
        assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    assertEquals(0, iterator.getBeginIndex());
    assertEquals(11, iterator.getEndIndex());
}
Also used : Support_DecimalFormat(tests.support.Support_DecimalFormat) DecimalFormat(java.text.DecimalFormat) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 8 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project robovm by robovm.

the class OldFormatTest method test_formatToCharacterIteratorLjava_lang_Object.

public void test_formatToCharacterIteratorLjava_lang_Object() {
    MockFormat mf = new MockFormat();
    AttributedCharacterIterator aci = mf.formatToCharacterIterator("Test 123 Test");
    assertEquals(0, aci.getBeginIndex());
    try {
        mf.formatToCharacterIterator(null);
        fail("NullPointerException was not thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    try {
        mf.formatToCharacterIterator("");
    } catch (IllegalArgumentException iae) {
    //expected
    }
}
Also used : AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 9 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project robovm by robovm.

the class Support_Format method findFields.

/**
     * finds attributes with regards to char index in this
     * AttributedCharacterIterator, and puts them in a vector
     *
     * @param iterator
     * @return a vector, each entry in this vector are of type FieldContainer ,
     *         which stores start and end indexes and an attribute this range
     *         has
     */
protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
    Vector<FieldContainer> result = new Vector<FieldContainer>();
    while (iterator.getIndex() != iterator.getEndIndex()) {
        int start = iterator.getRunStart();
        int end = iterator.getRunLimit();
        Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
        while (it.hasNext()) {
            AttributedCharacterIterator.Attribute attribute = it.next();
            Object value = iterator.getAttribute(attribute);
            result.add(new FieldContainer(start, end, attribute, value));
        // System.out.println(start + " " + end + ": " + attribute + ",
        // " + value );
        // System.out.println("v.add(new FieldContainer(" + start +"," +
        // end +"," + attribute+ "," + value+ "));");
        }
        iterator.setIndex(end);
    }
    return result;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) Attribute(java.text.AttributedCharacterIterator.Attribute) Vector(java.util.Vector) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 10 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project robovm by robovm.

the class OldAttributedCharacterIteratorTest method test_getRunLimit.

public void test_getRunLimit() {
    int limit = it.getRunLimit();
    assertEquals(string.length(), limit);
    AttributedString as = new AttributedString("");
    assertEquals(0, as.getIterator().getRunLimit());
    as = new AttributedString(new AttributedString("test text").getIterator(), 2, 7);
    AttributedCharacterIterator it = as.getIterator();
    assertEquals(5, it.getRunLimit());
}
Also used : AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

AttributedCharacterIterator (java.text.AttributedCharacterIterator)52 AttributedString (java.text.AttributedString)35 TextLayout (java.awt.font.TextLayout)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)15 Point (java.awt.Point)8 Font (java.awt.Font)7 Paint (java.awt.Paint)7 FontRenderContext (java.awt.font.FontRenderContext)7 Graphics2D (java.awt.Graphics2D)6 Color (java.awt.Color)5 ArrayList (java.util.ArrayList)5 WeakHashMap (java.util.WeakHashMap)5 Rectangle (java.awt.Rectangle)4 Attribute (java.text.AttributedCharacterIterator.Attribute)4 DecimalFormat (java.text.DecimalFormat)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Image (java.awt.Image)2