下面列出了java.text.AttributedCharacterIterator#setIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
@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;
}
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;
}
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;
}
}
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;
}
}
/**
* 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;
}
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;
}
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);
}
}
}
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;
}
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;
}
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");
}
}
/**
* 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;
}
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;
}
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);
}
}
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");
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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);
}
}