java.text.AttributedCharacterIterator#setIndex ( )源码实例Demo

下面列出了java.text.AttributedCharacterIterator#setIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dragonwell8_jdk   文件: SwingUtilities2.java
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;
    }
}
 
源代码2 项目: jasperreports   文件: MaxFontSizeFinder.java
@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;
}
 
源代码3 项目: jasperreports   文件: JRXlsMetadataExporter.java
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;
}
 
源代码4 项目: openjdk-8   文件: SwingUtilities2.java
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;
    }
}
 
源代码5 项目: TencentKona-8   文件: SwingUtilities2.java
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;
    }
}
 
源代码6 项目: jdk1.8-source-analysis   文件: TextLine.java
/**
 * 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;
}
 
源代码7 项目: ttt   文件: Phrase.java
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;
}
 
源代码8 项目: ttt   文件: TTML2ResourceConverterState.java
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);
        }
    }
}
 
源代码9 项目: jasperreports   文件: JRPdfExporter.java
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;
}
 
源代码10 项目: ttt   文件: Phrase.java
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;
}
 
源代码11 项目: jdk8u_jdk   文件: AttributedStringTest.java
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");
    }
}
 
源代码12 项目: openjdk-8-source   文件: TextLine.java
/**
 * 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;
}
 
源代码13 项目: ttt   文件: LineLayout.java
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;
}
 
源代码14 项目: openjdk-jdk9   文件: AttributedStringTest.java
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);
    }
}
 
源代码15 项目: TencentKona-8   文件: AttributedStringTest.java
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");
    }
}
 
源代码16 项目: openjdk-jdk8u   文件: TextLine.java
/**
 * 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;
}
 
源代码17 项目: openjdk-8   文件: StyledParagraph.java
/**
 * 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;
}
 
源代码18 项目: jdk1.8-source-analysis   文件: StyledParagraph.java
/**
 * 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;
}
 
源代码19 项目: openjdk-jdk8u   文件: StyledParagraph.java
/**
 * 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;
}
 
源代码20 项目: jdk8u-jdk   文件: StyledParagraph.java
/**
 * 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);
    }
}