Search in sources :

Example 36 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 37 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 38 with AttributedString

use of java.text.AttributedString in project jdk8u_jdk by JetBrains.

the class SwingUtilities2 method clipString.

/**
     * Clips the passed in String to the space provided.  NOTE: this assumes
     * the string does not fit in the available space.
     *
     * @param c JComponent that will display the string, may be null
     * @param fm FontMetrics used to measure the String width
     * @param string String to display
     * @param availTextWidth Amount of space that the string can be drawn in
     * @return Clipped string that can fit in the provided space.
     */
public static String clipString(JComponent c, FontMetrics fm, String string, int availTextWidth) {
    // c may be null here.
    String clipString = "...";
    availTextWidth -= SwingUtilities2.stringWidth(c, fm, clipString);
    if (availTextWidth <= 0) {
        //can not fit any characters
        return clipString;
    }
    boolean needsTextLayout;
    synchronized (charsBufferLock) {
        int stringLength = syncCharsBuffer(string);
        needsTextLayout = isComplexLayout(charsBuffer, 0, stringLength);
        if (!needsTextLayout) {
            int width = 0;
            for (int nChars = 0; nChars < stringLength; nChars++) {
                width += fm.charWidth(charsBuffer[nChars]);
                if (width > availTextWidth) {
                    string = string.substring(0, nChars);
                    break;
                }
            }
        }
    }
    if (needsTextLayout) {
        AttributedString aString = new AttributedString(string);
        if (c != null) {
            aString.addAttribute(TextAttribute.NUMERIC_SHAPING, c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
        }
        LineBreakMeasurer measurer = new LineBreakMeasurer(aString.getIterator(), BreakIterator.getCharacterInstance(), getFontRenderContext(c, fm));
        string = string.substring(0, measurer.nextOffset(availTextWidth));
    }
    return string + clipString;
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString)

Example 39 with AttributedString

use of java.text.AttributedString in project jdk8u_jdk by JetBrains.

the class SwingUtilities2 method getTrimmedTrailingSpacesIterator.

private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator(AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();
    char c = iterator.last();
    while (c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }
    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();
        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator, iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
Also used : AttributedString(java.text.AttributedString)

Example 40 with AttributedString

use of java.text.AttributedString in project jdk8u_jdk by JetBrains.

the class CodePointInputMethod method sendComposedText.

/**
     * Send the composed text to the client.
     */
private void sendComposedText() {
    AttributedString as = new AttributedString(buffer.toString());
    as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT, InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);
    context.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as.getIterator(), 0, TextHitInfo.leading(insertionPoint), null);
}
Also used : AttributedString(java.text.AttributedString)

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