Search in sources :

Example 51 with AttributedString

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

the class CompositionTextManager method getCommittedText.

public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex) {
    int length = endIndex - beginIndex;
    String textAreaString = textArea.getText(beginIndex, length);
    return new AttributedString(textAreaString).getIterator();
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) Point(java.awt.Point)

Example 52 with AttributedString

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

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

the class InputMethodSupport method getTextLayout.

private TextLayout getTextLayout(AttributedCharacterIterator text, int committedCount) {
    boolean antialias = Preferences.getBoolean("editor.smooth");
    TextAreaPainter painter = textArea.getPainter();
    // create attributed string with font info.
    if (text.getEndIndex() - (text.getBeginIndex() + committedCount) > 0) {
        composedTextString = new AttributedString(text, committedCount, text.getEndIndex(), CUSTOM_IM_ATTRIBUTES);
        Font font = painter.getFontMetrics().getFont();
        TextAreaDefaults defaults = textArea.getDefaults();
        Color bgColor = defaults.lineHighlight ? defaults.lineHighlightColor : defaults.bgcolor;
        composedTextString.addAttribute(TextAttribute.FONT, font);
        composedTextString.addAttribute(TextAttribute.FOREGROUND, defaults.fgcolor);
        composedTextString.addAttribute(TextAttribute.BACKGROUND, bgColor);
    } else {
        composedTextString = new AttributedString("");
        return null;
    }
    // set hint of antialiasing to render target.
    Graphics2D g2d = (Graphics2D) painter.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    FontRenderContext frc = g2d.getFontRenderContext();
    if (Base.DEBUG) {
        Messages.log("debug: FontRenderContext is Antialiased = " + frc.getAntiAliasingHint());
    }
    return new TextLayout(composedTextString.getIterator(), frc);
}
Also used : TextAreaDefaults(processing.app.syntax.TextAreaDefaults) AttributedString(java.text.AttributedString) TextAreaPainter(processing.app.syntax.TextAreaPainter) Color(java.awt.Color) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 54 with AttributedString

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

the class InputMethodSupport method getCommittedText.

@Override
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex, AttributedCharacterIterator.Attribute[] attributes) {
    int length = endIndex - beginIndex;
    String textAreaString = textArea.getText(beginIndex, length);
    return new AttributedString(textAreaString).getIterator();
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) Point(java.awt.Point)

Example 55 with AttributedString

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

Aggregations

AttributedString (java.text.AttributedString)86 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)11 Point (java.awt.Point)11 Paint (java.awt.Paint)10 Graphics2D (java.awt.Graphics2D)9 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