Search in sources :

Example 1 with AttributedString

use of java.text.AttributedString 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 AttributedString

use of java.text.AttributedString in project hid-serial by rayshobby.

the class StyledString method readObject.

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();
    // Recreate transient elements
    spacer = getParagraghSpacer(wrapWidth);
    styledText = new AttributedString(plainText);
    styledText = insertParagraphMarkers(plainText, styledText);
    linesInfo = new LinkedList<TextLayoutInfo>();
    applyAttributes();
}
Also used : AttributedString(java.text.AttributedString)

Example 3 with AttributedString

use of java.text.AttributedString in project hid-serial by rayshobby.

the class StyledString method convertToSingleLineText.

/**
	 * Converts this StyledString from multi-line to single-line by replacing all EOL
	 * characters with the space character
	 * for paragraphs
	 * @param ptext
	 * @param as
	 * @return the converted string
	 */
StyledString convertToSingleLineText() {
    // Make sure we have something to work with.
    if (styledText == null || plainText == null) {
        plainText = "";
        styledText = new AttributedString(plainText);
    } else {
        // Scan through plain text and for each EOL replace the paragraph spacer from
        // the attributed string (styledText).
        int fromIndex = plainText.indexOf('\n', 0);
        if (fromIndex >= 0) {
            while (fromIndex >= 0) {
                try {
                    // if text == "\n" then an exception is thrown
                    styledText.addAttribute(TextAttribute.CHAR_REPLACEMENT, ' ', fromIndex, fromIndex + 1);
                    fromIndex = plainText.indexOf('\n', fromIndex + 1);
                } catch (Exception excp) {
                    break;
                }
            }
            // Finally replace all EOL in the plainText
            plainText = plainText.replace('\n', ' ');
        }
    }
    wrapWidth = Integer.MAX_VALUE;
    return this;
}
Also used : AttributedString(java.text.AttributedString) IOException(java.io.IOException)

Example 4 with AttributedString

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

the class OldAttributedCharacterIteratorTest method test_getRunStartLAttribute.

public void test_getRunStartLAttribute() {
    assertEquals(0, it.getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE));
    AttributedString as = new AttributedString("test text");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "GERMAN", 2, 5);
    as.addAttribute(AttributedCharacterIterator.Attribute.READING, "READ", 2, 7);
    assertEquals(0, as.getIterator().getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE));
    assertEquals(0, as.getIterator().getRunStart(AttributedCharacterIterator.Attribute.READING));
}
Also used : AttributedString(java.text.AttributedString)

Example 5 with AttributedString

use of java.text.AttributedString 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)

Aggregations

AttributedString (java.text.AttributedString)85 AttributedCharacterIterator (java.text.AttributedCharacterIterator)37 TextLayout (java.awt.font.TextLayout)29 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 FontRenderContext (java.awt.font.FontRenderContext)13 Font (java.awt.Font)10 Paint (java.awt.Paint)10 Point (java.awt.Point)10 Graphics2D (java.awt.Graphics2D)8 WeakHashMap (java.util.WeakHashMap)8 Color (java.awt.Color)4 Bidi (java.text.Bidi)4 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)4 Rectangle (java.awt.Rectangle)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Insets (java.awt.Insets)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2