Search in sources :

Example 1 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project OpenNotebook by jaltekruse.

the class TextObjectGUI method drawMathObject.

public void drawMathObject(TextObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
    if (!object.getText().equals("")) {
        Font f = g.getFont();
        String message = object.getText();
        g.setFont(f.deriveFont(sap.getFontSize()));
        g.setColor(Color.BLACK);
        Graphics2D graphics2D = (Graphics2D) g;
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        AttributedString messageAS = new AttributedString(message);
        messageAS.addAttribute(TextAttribute.FONT, g.getFont());
        AttributedCharacterIterator messageIterator = messageAS.getIterator();
        FontRenderContext messageFRC = graphics2D.getFontRenderContext();
        LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
        Insets insets = new Insets(2, 2, 2, 2);
        float wrappingWidth = sap.getWidth() - insets.left - insets.right;
        float x = sap.getxOrigin() + insets.left;
        float y = sap.getyOrigin() + insets.top;
        try {
            TextLayout textLayout;
            while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
                textLayout = messageLBM.nextLayout(wrappingWidth);
                y += textLayout.getAscent();
                if (object.getAlignment().equals(TextObject.LEFT)) {
                    textLayout.draw(graphics2D, x, y);
                } else if (object.getAlignment().equals(TextObject.RIGHT)) {
                    textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()), y);
                } else {
                    //centered
                    textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()) / 2, y);
                }
                y += textLayout.getDescent() + textLayout.getLeading();
                x = sap.getxOrigin() + insets.left;
            }
        } catch (Exception e) {
        // TODO - logging and error reporting to user
        //				System.out.println("error with text rendering");
        }
        object.setHeight((int) ((y - sap.getyOrigin()) / zoomLevel));
        g.setFont(f);
    } else {
        // draw the black box around the text box if nothing is in it
        g.setColor(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), (int) (object.getWidth() * zoomLevel), (int) (object.getHeight() * zoomLevel));
    }
    if (((BooleanAttribute) object.getAttributeWithName(TextObject.SHOW_BOX)).getValue()) {
        g.setColor(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
    }
}
Also used : BooleanAttribute(doc.attributes.BooleanAttribute) Insets(java.awt.Insets) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext)

Example 2 with AttributedCharacterIterator

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

the class OldAttributedStringTest method test_ConstructorLAttributedCharacterIterator_3.

public void test_ConstructorLAttributedCharacterIterator_3() {
    String testString = "Test string";
    AttributedString attrString = new AttributedString(testString);
    AttributedCharacterIterator iter = attrString.getIterator();
    AttributedString attrString2;
    attrString2 = new AttributedString(iter, 2, 7, new AttributedCharacterIterator.Attribute[] {});
    assertEqualString("String must match!", "st st", attrString2);
    attrString2 = new AttributedString(iter, 2, 7, null);
    assertEqualString("String must match!", "st st", attrString2);
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 3 with AttributedCharacterIterator

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

the class OldAttributedStringTest method test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII.

public void test_addAttributeLjava_text_AttributedCharacterIterator$AttributeLjava_lang_ObjectII() {
    AttributedString as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 3);
    AttributedCharacterIterator it = as.getIterator();
    assertEquals("non-null value limit", 2, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
    as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, 2, 3);
    it = as.getIterator();
    assertEquals("null value limit", 4, it.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE));
    try {
        as = new AttributedString("test");
        as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, -1, 3);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // Expected
    }
    // regression for Harmony-1244
    as = new AttributedString("123", new WeakHashMap());
    try {
        as.addAttribute(null, new TreeSet(), 0, 1);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        as.addAttribute(null, new TreeSet(), -1, 1);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : AttributedString(java.text.AttributedString) TreeSet(java.util.TreeSet) AttributedCharacterIterator(java.text.AttributedCharacterIterator) WeakHashMap(java.util.WeakHashMap)

Example 4 with AttributedCharacterIterator

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

the class OldAttributedStringTest method test_getIterator$Ljava_text_AttributedCharacterIterator$AttributeII.

/**
     * java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[],
     *        int, int) Test of method
     *        java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[],
     *        int, int).
     */
public void test_getIterator$Ljava_text_AttributedCharacterIterator$AttributeII() {
    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);
        attrString.addAttributes(hm, 2, 4);
        AttributedCharacterIterator it = attrString.getIterator(aci, 1, 5);
        assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[0]) == null);
        assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[1]) == null);
        assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[2]) == null);
        it.next();
        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);
        try {
            attrString.getIterator(aci, -1, 5);
            fail("IllegalArgumentException is not thrown.");
        } catch (IllegalArgumentException iae) {
        //expected
        }
        try {
            attrString.getIterator(aci, 6, 5);
            fail("IllegalArgumentException is not thrown.");
        } catch (IllegalArgumentException iae) {
        //expected
        }
        try {
            attrString.getIterator(aci, 3, 2);
            fail("IllegalArgumentException is not thrown.");
        } catch (IllegalArgumentException iae) {
        //expected
        }
    } 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 5 with AttributedCharacterIterator

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

the class OldAttributedStringTest method assertEqualString.

static void assertEqualString(String msg, String expected, AttributedString attrString) {
    AttributedCharacterIterator it = attrString.getIterator();
    StringBuffer buf = new StringBuffer();
    buf.append(it.first());
    char ch;
    while ((ch = it.next()) != CharacterIterator.DONE) buf.append(ch);
    assertEquals(msg, expected, buf.toString());
}
Also used : 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