Java Code Examples for java.text.AttributedCharacterIterator#setIndex()

The following examples show how to use java.text.AttributedCharacterIterator#setIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwingUtilities2.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example 2
Source File: SwingUtilities2.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example 3
Source File: SwingUtilities2.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
    }
}
 
Example 4
Source File: MaxFontSizeFinder.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public float findMaxFontSize(AttributedCharacterIterator line, float defaultFontSize)
{
	line.setIndex(0);
	Float maxFontSize = ZERO;
	int runLimit = 0;
	
	while(runLimit < line.getEndIndex() && (runLimit = line.getRunLimit(TextAttribute.SIZE)) <= line.getEndIndex())
	{
		Float size = (Float)line.getAttribute(TextAttribute.SIZE);
		if (maxFontSize.compareTo(size) < 0)
		{
			maxFontSize = size;
		}
		line.setIndex(runLimit);
	}
	
	return maxFontSize;
}
 
Example 5
Source File: JRXlsMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont, Locale locale) {
	String text = styledText.getText();
	HSSFRichTextString richTextStr = new HSSFRichTextString(text);
	int runLimit = 0;
	AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

	while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
		Map<Attribute,Object> attributes = iterator.getAttributes();
		JRFont runFont = attributes.isEmpty()? defaultFont : new JRBaseFont(attributes);
		short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null  
			? getWorkbookColor((Color)attributes.get(TextAttribute.FOREGROUND)).getIndex() 
			: forecolor;
		HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
		richTextStr.applyFont(iterator.getIndex(), runLimit, font);
		iterator.setIndex(runLimit);
	}
	return richTextStr;
}
 
Example 6
Source File: TextLine.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * When this returns, the ACI's current position will be at the start of the
 * first run which does NOT contain a GraphicAttribute.  If no such run exists
 * the ACI's position will be at the end, and this method will return false.
 */
static boolean advanceToFirstFont(AttributedCharacterIterator aci) {

    for (char ch = aci.first();
         ch != CharacterIterator.DONE;
         ch = aci.setIndex(aci.getRunLimit()))
    {

        if (aci.getAttribute(TextAttribute.CHAR_REPLACEMENT) == null) {
            return true;
        }
    }

    return false;
}
 
Example 7
Source File: AttributedStringTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, Attribute key, int[] expectedLimits) throws Exception {
    int previous = 0;
    char c = iterator.first();
    for (int i = 0; i < expectedLimits.length; i++) {
         if (iterator.getRunStart(key) != previous || iterator.getRunLimit(key) != expectedLimits[i]) {
             throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart(key) + ", " + iterator.getRunLimit(key) + " for key " + key);
         }
         previous = expectedLimits[i];
         c = iterator.setIndex(previous);
    }
    if (c != CharacterIterator.DONE) {
        throwException(iterator, "iterator's run sequence doesn't end with DONE");
    }
}
 
Example 8
Source File: AttributedStringTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static final void checkIteratorAttribute(AttributedCharacterIterator iterator, int index, Attribute key, Object expectedValue) throws Exception {
    iterator.setIndex(index);
    Object value = iterator.getAttribute(key);
    if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) {
        throwException(iterator, "iterator returns wrong attribute value - " + value + " instead of " + expectedValue);
    }
    value = iterator.getAttributes().get(key);
    if (!((expectedValue == null && value == null) || (expectedValue != null && expectedValue.equals(value)))) {
        throwException(iterator, "iterator's map returns wrong attribute value - " + value + " instead of " + expectedValue);
    }
}
 
Example 9
Source File: LineLayout.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static boolean hasTextRunBreakingAttribute(AttributedCharacterIterator iterator, int index) {
    int s = iterator.getIndex();
    if (index != s)
        iterator.setIndex(index);
    int k = iterator.getRunStart(textRunBreakingAttributes);
    if (index != s)
        iterator.setIndex(s);
    return k == index;
}
 
Example 10
Source File: TextLine.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * When this returns, the ACI's current position will be at the start of the
 * first run which does NOT contain a GraphicAttribute.  If no such run exists
 * the ACI's position will be at the end, and this method will return false.
 */
static boolean advanceToFirstFont(AttributedCharacterIterator aci) {

    for (char ch = aci.first();
         ch != CharacterIterator.DONE;
         ch = aci.setIndex(aci.getRunLimit()))
    {

        if (aci.getAttribute(TextAttribute.CHAR_REPLACEMENT) == null) {
            return true;
        }
    }

    return false;
}
 
Example 11
Source File: AttributedStringTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, int[] expectedLimits) throws Exception {
    int previous = 0;
    char c = iterator.first();
    for (int i = 0; i < expectedLimits.length; i++) {
         if (iterator.getRunStart() != previous || iterator.getRunLimit() != expectedLimits[i]) {
             throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart() + ", " + iterator.getRunLimit());
         }
         previous = expectedLimits[i];
         c = iterator.setIndex(previous);
    }
    if (c != CharacterIterator.DONE) {
        throwException(iterator, "iterator's run sequence doesn't end with DONE");
    }
}
 
Example 12
Source File: Phrase.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isWhitespace() {
    boolean rv = true;
    AttributedCharacterIterator aci = getIterator();
    int savedIndex = aci.getIndex();
    for (int i = aci.getBeginIndex(), n = aci.getEndIndex(); rv && (i < n); ++i) {
        char c = aci.setIndex(i);
        if (!Characters.isWhitespace(c))
            rv = false;
    }
    aci.setIndex(savedIndex);
    return rv;
}
 
Example 13
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected boolean canUseGlyphRendering(JRPrintText text, JRStyledText styledText)
{
	Locale locale = getTextLocale(text);
	AttributedCharacterIterator attributesIterator = styledText.getAttributedString().getIterator();
	int index = 0;
	while (index < styledText.length())
	{
		FontKey fontKey = extractFontKey(attributesIterator.getAttributes(), locale);
		if (!fontKey.fontAttribute.hasAttribute())
		{
			return false;
		}
		
		Boolean canUse = glyphRendererFonts.get(fontKey);
		if (canUse == null)
		{
			canUse = canUseGlyphRendering(fontKey);
			glyphRendererFonts.put(fontKey, canUse);
		}
		
		if (!canUse)
		{
			return false;
		}
		
		index = attributesIterator.getRunLimit();
		attributesIterator.setIndex(index);
	}
	return true;
}
 
Example 14
Source File: TTML2ResourceConverterState.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void populateText(Paragraph p, AttributedString as, boolean insertBreakBefore, Direction blockDirection) {
    if (as != null) {
        List<Serializable> content = p.getContent();
        if (insertBreakBefore)
            content.add(ttmlFactory.createBr(ttmlFactory.createBreak()));
        AttributedCharacterIterator aci = as.getIterator();
        aci.first();
        StringBuffer sb = new StringBuffer();
        while (aci.current() != CharacterIterator.DONE) {
            int i = aci.getRunStart();
            int e = aci.getRunLimit();
            Map<AttributedCharacterIterator.Attribute,Object> attributes = aci.getAttributes();
            while (i < e) {
                sb.append(aci.setIndex(i++));
            }
            String text = sb.toString();
            if (!text.isEmpty()) {
                if (!attributes.isEmpty()) {
                    populateAttributedText(content, text, attributes, blockDirection);
                } else {
                    content.add(text);
                }
            }
            sb.setLength(0);
            aci.setIndex(e);
        }
    }
}
 
Example 15
Source File: Phrase.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isEmbedding() {
    boolean rv = true;
    AttributedCharacterIterator aci = getIterator();
    int savedIndex = aci.getIndex();
    int b = aci.getBeginIndex();
    int e = aci.getEndIndex();
    if ((e - b) != 1)
        rv = false;
    else if (aci.setIndex(b) != Characters.UC_OBJECT)
        rv = false;
    aci.setIndex(savedIndex);
    return rv;
}
 
Example 16
Source File: TextLine.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * When this returns, the ACI's current position will be at the start of the
 * first run which does NOT contain a GraphicAttribute.  If no such run exists
 * the ACI's position will be at the end, and this method will return false.
 */
static boolean advanceToFirstFont(AttributedCharacterIterator aci) {

    for (char ch = aci.first();
         ch != CharacterIterator.DONE;
         ch = aci.setIndex(aci.getRunLimit()))
    {

        if (aci.getAttribute(TextAttribute.CHAR_REPLACEMENT) == null) {
            return true;
        }
    }

    return false;
}
 
Example 17
Source File: StyledParagraph.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return a StyledParagraph reflecting the insertion of a single character
 * into the text.  This method will attempt to reuse the given paragraph,
 * but may create a new paragraph.
 * @param aci an iterator over the text.  The text should be the same as the
 *     text used to create (or most recently update) oldParagraph, with
 *     the exception of inserting a single character at insertPos.
 * @param chars the characters in aci
 * @param insertPos the index of the new character in aci
 * @param oldParagraph a StyledParagraph for the text in aci before the
 *     insertion
 */
public static StyledParagraph insertChar(AttributedCharacterIterator aci,
                                         char[] chars,
                                         int insertPos,
                                         StyledParagraph oldParagraph) {

    // If the styles at insertPos match those at insertPos-1,
    // oldParagraph will be reused.  Otherwise we create a new
    // paragraph.

    char ch = aci.setIndex(insertPos);
    int relativePos = Math.max(insertPos - aci.getBeginIndex() - 1, 0);

    Map<? extends Attribute, ?> attributes =
        addInputMethodAttrs(aci.getAttributes());
    Decoration d = Decoration.getDecoration(attributes);
    if (!oldParagraph.getDecorationAt(relativePos).equals(d)) {
        return new StyledParagraph(aci, chars);
    }
    Object f = getGraphicOrFont(attributes);
    if (f == null) {
        FontResolver resolver = FontResolver.getInstance();
        int fontIndex = resolver.getFontIndex(ch);
        f = resolver.getFont(fontIndex, attributes);
    }
    if (!oldParagraph.getFontOrGraphicAt(relativePos).equals(f)) {
        return new StyledParagraph(aci, chars);
    }

    // insert into existing paragraph
    oldParagraph.length += 1;
    if (oldParagraph.decorations != null) {
        insertInto(relativePos,
                   oldParagraph.decorationStarts,
                   oldParagraph.decorations.size());
    }
    if (oldParagraph.fonts != null) {
        insertInto(relativePos,
                   oldParagraph.fontStarts,
                   oldParagraph.fonts.size());
    }
    return oldParagraph;
}
 
Example 18
Source File: StyledParagraph.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new StyledParagraph over the given styled text.
 * @param aci an iterator over the text
 * @param chars the characters extracted from aci
 */
public StyledParagraph(AttributedCharacterIterator aci,
                       char[] chars) {

    int start = aci.getBeginIndex();
    int end = aci.getEndIndex();
    length = end - start;

    int index = start;
    aci.first();

    do {
        final int nextRunStart = aci.getRunLimit();
        final int localIndex = index-start;

        Map<? extends Attribute, ?> attributes = aci.getAttributes();
        attributes = addInputMethodAttrs(attributes);
        Decoration d = Decoration.getDecoration(attributes);
        addDecoration(d, localIndex);

        Object f = getGraphicOrFont(attributes);
        if (f == null) {
            addFonts(chars, attributes, localIndex, nextRunStart-start);
        }
        else {
            addFont(f, localIndex);
        }

        aci.setIndex(nextRunStart);
        index = nextRunStart;

    } while (index < end);

    // Add extra entries to starts arrays with the length
    // of the paragraph.  'this' is used as a dummy value
    // in the Vector.
    if (decorations != null) {
        decorationStarts = addToVector(this, length, decorations, decorationStarts);
    }
    if (fonts != null) {
        fontStarts = addToVector(this, length, fonts, fontStarts);
    }
}
 
Example 19
Source File: StyledParagraph.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Return a StyledParagraph reflecting the insertion of a single character
 * into the text.  This method will attempt to reuse the given paragraph,
 * but may create a new paragraph.
 * @param aci an iterator over the text.  The text should be the same as the
 *     text used to create (or most recently update) oldParagraph, with
 *     the exception of inserting a single character at insertPos.
 * @param chars the characters in aci
 * @param insertPos the index of the new character in aci
 * @param oldParagraph a StyledParagraph for the text in aci before the
 *     insertion
 */
public static StyledParagraph insertChar(AttributedCharacterIterator aci,
                                         char[] chars,
                                         int insertPos,
                                         StyledParagraph oldParagraph) {

    // If the styles at insertPos match those at insertPos-1,
    // oldParagraph will be reused.  Otherwise we create a new
    // paragraph.

    char ch = aci.setIndex(insertPos);
    int relativePos = Math.max(insertPos - aci.getBeginIndex() - 1, 0);

    Map<? extends Attribute, ?> attributes =
        addInputMethodAttrs(aci.getAttributes());
    Decoration d = Decoration.getDecoration(attributes);
    if (!oldParagraph.getDecorationAt(relativePos).equals(d)) {
        return new StyledParagraph(aci, chars);
    }
    Object f = getGraphicOrFont(attributes);
    if (f == null) {
        FontResolver resolver = FontResolver.getInstance();
        int fontIndex = resolver.getFontIndex(ch);
        f = resolver.getFont(fontIndex, attributes);
    }
    if (!oldParagraph.getFontOrGraphicAt(relativePos).equals(f)) {
        return new StyledParagraph(aci, chars);
    }

    // insert into existing paragraph
    oldParagraph.length += 1;
    if (oldParagraph.decorations != null) {
        insertInto(relativePos,
                   oldParagraph.decorationStarts,
                   oldParagraph.decorations.size());
    }
    if (oldParagraph.fonts != null) {
        insertInto(relativePos,
                   oldParagraph.fontStarts,
                   oldParagraph.fonts.size());
    }
    return oldParagraph;
}
 
Example 20
Source File: StyledParagraph.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return a StyledParagraph reflecting the insertion of a single character
 * into the text.  This method will attempt to reuse the given paragraph,
 * but may create a new paragraph.
 * @param aci an iterator over the text.  The text should be the same as the
 *     text used to create (or most recently update) oldParagraph, with
 *     the exception of inserting a single character at insertPos.
 * @param chars the characters in aci
 * @param insertPos the index of the new character in aci
 * @param oldParagraph a StyledParagraph for the text in aci before the
 *     insertion
 */
public static StyledParagraph insertChar(AttributedCharacterIterator aci,
                                         char[] chars,
                                         int insertPos,
                                         StyledParagraph oldParagraph) {

    // If the styles at insertPos match those at insertPos-1,
    // oldParagraph will be reused.  Otherwise we create a new
    // paragraph.

    char ch = aci.setIndex(insertPos);
    int relativePos = Math.max(insertPos - aci.getBeginIndex() - 1, 0);

    Map<? extends Attribute, ?> attributes =
        addInputMethodAttrs(aci.getAttributes());
    Decoration d = Decoration.getDecoration(attributes);
    if (!oldParagraph.getDecorationAt(relativePos).equals(d)) {
        return new StyledParagraph(aci, chars);
    }
    Object f = getGraphicOrFont(attributes);
    if (f == null) {
        FontResolver resolver = FontResolver.getInstance();
        int fontIndex = resolver.getFontIndex(ch);
        f = resolver.getFont(fontIndex, attributes);
    }
    if (!oldParagraph.getFontOrGraphicAt(relativePos).equals(f)) {
        return new StyledParagraph(aci, chars);
    }

    // insert into existing paragraph
    oldParagraph.length += 1;
    if (oldParagraph.decorations != null) {
        insertInto(relativePos,
                   oldParagraph.decorationStarts,
                   oldParagraph.decorations.size());
    }
    if (oldParagraph.fonts != null) {
        insertInto(relativePos,
                   oldParagraph.fontStarts,
                   oldParagraph.fonts.size());
    }
    return oldParagraph;
}