Search in sources :

Example 36 with AttributedCharacterIterator

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

the class InputMethodSupport method inputMethodTextChanged.

/////////////////////////////////////////////////////////////////////////////
// InputMethodListener
/////////////////////////////////////////////////////////////////////////////
/**
   * Handles events from InputMethod.
   *
   * @param event event from Input Method.
   */
@Override
public void inputMethodTextChanged(InputMethodEvent event) {
    if (Base.DEBUG) {
        StringBuilder sb = new StringBuilder();
        sb.append("#Called inputMethodTextChanged");
        sb.append("\t ID: " + event.getID());
        sb.append("\t timestamp: " + new java.util.Date(event.getWhen()));
        sb.append("\t parmString: " + event.paramString());
        Messages.log(sb.toString());
    }
    // text = composedText + commitedText
    AttributedCharacterIterator text = event.getText();
    committedCount = event.getCommittedCharacterCount();
    // The caret for Input Method. If you type a character by a input method,
    // original caret position will be incorrect. JEditTextArea is not
    // implemented using AttributedString and TextLayout.
    textArea.setCaretVisible(false);
    // Korean Input Method
    if (text != null && text.getEndIndex() - (text.getBeginIndex() + committedCount) <= 0) {
        textArea.setCaretVisible(true);
    }
    // Japanese Input Method
    if (text == null) {
        textArea.setCaretVisible(true);
    }
    if (text != null) {
        if (committedCount > 0) {
            char[] insertion = new char[committedCount];
            char c = text.first();
            for (int i = 0; i < committedCount; i++) {
                insertion[i] = c;
                c = text.next();
            }
            // Insert this as a compound edit
            textArea.setSelectedText(new String(insertion), true);
            textArea.getInputHandler().handleInputMethodCommit();
        }
        CompositionTextPainter compositionPainter = textArea.getPainter().getCompositionTextpainter();
        Messages.log("textArea.getCaretPosition() + committed_count: " + (textArea.getCaretPosition() + committedCount));
        compositionPainter.setComposedTextLayout(getTextLayout(text, committedCount), textArea.getCaretPosition() + committedCount);
        compositionPainter.setCaret(event.getCaret());
    } else {
        // otherwise hide the input method
        CompositionTextPainter compositionPainter = textArea.getPainter().getCompositionTextpainter();
        compositionPainter.setComposedTextLayout(null, 0);
        compositionPainter.setCaret(null);
    }
    event.consume();
    textArea.repaint();
}
Also used : AttributedString(java.text.AttributedString) Point(java.awt.Point) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 37 with AttributedCharacterIterator

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

the class StyledString method getLines.

/**
	 * Get the text layouts for display if the string has changed since last call
	 * to this method regenerate them.
	 * 
	 * @param g2d Graphics2D display context
	 * @return a list of text layouts for rendering
	 */
public LinkedList<TextLayoutInfo> getLines(Graphics2D g2d) {
    if (font != g2d.getFont()) {
        setFont(g2d.getFont());
        invalidText = true;
    }
    if (invalidText) {
        styledText = new AttributedString(plainText);
        styledText = insertParagraphMarkers(plainText, styledText);
        applyAttributes();
        invalidText = false;
        invalidLayout = true;
    }
    if (invalidLayout) {
        linesInfo.clear();
        if (plainText.length() > 0) {
            textHeight = 0;
            maxLineLength = 0;
            maxLineHeight = 0;
            nbrLines = 0;
            AttributedCharacterIterator paragraph = styledText.getIterator(null, 0, plainText.length());
            FontRenderContext frc = g2d.getFontRenderContext();
            lineMeasurer = new LineBreakMeasurer(paragraph, frc);
            float yposinpara = 0;
            int charssofar = 0;
            while (lineMeasurer.getPosition() < plainText.length()) {
                TextLayout layout = lineMeasurer.nextLayout(wrapWidth);
                float advance = layout.getVisibleAdvance();
                if (justify) {
                    if (justify && advance > justifyRatio * wrapWidth) {
                        //System.out.println(layout.getVisibleAdvance() + "  " + breakWidth + "  "+ layout.get);
                        // If advance > breakWidth then we have a line break
                        float jw = (advance > wrapWidth) ? advance - wrapWidth : wrapWidth;
                        layout = layout.getJustifiedLayout(jw);
                    }
                }
                // Remember the longest and tallest value for a layout so far.
                float lh = getHeight(layout);
                if (lh > maxLineHeight)
                    maxLineHeight = lh;
                textHeight += lh;
                if (advance <= wrapWidth && advance > maxLineLength)
                    maxLineLength = advance;
                // Store layout and line info
                linesInfo.add(new TextLayoutInfo(nbrLines, layout, charssofar, layout.getCharacterCount(), yposinpara));
                charssofar += layout.getCharacterCount();
                yposinpara += lh;
                nbrLines++;
            }
        }
        invalidLayout = false;
    }
    return linesInfo;
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) FontRenderContext(java.awt.font.FontRenderContext) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 38 with AttributedCharacterIterator

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

the class OldAttributedCharacterIteratorTest method test_getRunLimitLSet.

public void test_getRunLimitLSet() {
    AttributedString as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 3);
    AttributedCharacterIterator it = as.getIterator();
    HashSet<AttributedCharacterIterator.Attribute> attr = new HashSet<AttributedCharacterIterator.Attribute>();
    attr.add(AttributedCharacterIterator.Attribute.LANGUAGE);
    assertEquals("non-null value limit", 2, it.getRunLimit(attr));
    as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null, 2, 3);
    it = as.getIterator();
    assertEquals("null value limit", 4, it.getRunLimit(attr));
    attr.add(AttributedCharacterIterator.Attribute.READING);
    assertEquals("null value limit", 4, it.getRunLimit(attr));
}
Also used : AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator) HashSet(java.util.HashSet)

Example 39 with AttributedCharacterIterator

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

the class OldAttributedCharacterIteratorTest method test_getRunStartLjava_util_Set.

public void test_getRunStartLjava_util_Set() {
    AttributedString as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "a", 2, 3);
    AttributedCharacterIterator it = as.getIterator();
    HashSet<AttributedCharacterIterator.Attribute> attr = new HashSet<AttributedCharacterIterator.Attribute>();
    attr.add(AttributedCharacterIterator.Attribute.LANGUAGE);
    assertEquals(0, it.getRunStart(attr));
    as = new AttributedString("test");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "ENGLISH", 1, 3);
    it = as.getIterator();
    assertEquals(0, it.getRunStart(attr));
    attr.add(AttributedCharacterIterator.Attribute.READING);
    assertEquals(0, it.getRunStart(attr));
}
Also used : AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator) HashSet(java.util.HashSet)

Example 40 with AttributedCharacterIterator

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

the class OldAttributedStringTest method test_ConstructorLAttributedCharacterIterator_1.

public void test_ConstructorLAttributedCharacterIterator_1() {
    String testString = "Test string";
    AttributedString attrString = new AttributedString(testString);
    AttributedCharacterIterator iter = attrString.getIterator();
    AttributedString attrString2 = new AttributedString(iter);
    assertEqualString("String must match!", testString, attrString2);
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

AttributedCharacterIterator (java.text.AttributedCharacterIterator)57 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