下面列出了java.text.AttributedCharacterIterator#Attribute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private RichTextSpec createText() {
Map<AttributedCharacterIterator.Attribute, Object> attrs =
new HashMap<AttributedCharacterIterator.Attribute, Object>();
List<RichTextSpec.StyledChunk> chunks = new ArrayList<RichTextSpec.StyledChunk>();
InstanceID id = new InstanceID();
chunks.add( new RichTextSpec.StyledChunk( 0, 3, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "ABC" ) );
chunks.add( new RichTextSpec.StyledChunk( 3, 6, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "def" ) );
chunks.add( new RichTextSpec.StyledChunk( 6, 9, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "GHI" ) );
chunks.add( new RichTextSpec.StyledChunk( 9, 12, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "jkl" ) );
chunks.add( new RichTextSpec.StyledChunk( 12, 15, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "MNO" ) );
return new RichTextSpec( "ABCdefGHIjklMNO", TextDirection.LTR, chunks );
}
/**
* finds attributes with regards to char index in this
* AttributedCharacterIterator, and puts them in a vector
*
* @param iterator
* @return a vector, each entry in this vector are of type FieldContainer,
* which stores start and end indexes and an attribute this range has
*/
protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
Vector<FieldContainer> result = new Vector<FieldContainer>();
while (iterator.getIndex() != iterator.getEndIndex()) {
int start = iterator.getRunStart();
int end = iterator.getRunLimit();
Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
while (it.hasNext()) {
AttributedCharacterIterator.Attribute attribute = it.next();
Object value = iterator.getAttribute(attribute);
result.add(new FieldContainer(start, end, attribute, value));
// System.out.println(start + " " + end + ": " + attribute + ",
// " + value );
// System.out.println("v.add(new FieldContainer(" + start +"," +
// end +"," + attribute+ "," + value+ "));");
}
iterator.setIndex(end);
}
return result;
}
/**
* Draws a string of attributed characters at {@code (x, y)}.
*
* @param iterator an iterator over the characters ({@code null} not
* permitted).
* @param x the x-coordinate.
* @param y the y-coordinate.
*/
@Override
public void drawString(AttributedCharacterIterator iterator, float x,
float y) {
Set<AttributedCharacterIterator.Attribute>
s = iterator.getAllAttributeKeys();
if (!s.isEmpty()) {
TextLayout layout = new TextLayout(iterator,
getFontRenderContext());
layout.draw(this, x, y);
} else {
StringBuilder strb = new StringBuilder();
iterator.first();
for (int i = iterator.getBeginIndex(); i < iterator.getEndIndex();
i++) {
strb.append(iterator.current());
iterator.next();
}
drawString(strb.toString(), x, y);
}
}
public StyledChunk( final int start, final int end, final RenderNode originatingTextNode,
final Map<AttributedCharacterIterator.Attribute, Object> attributes,
final ReportAttributeMap<Object> originalAttributes, final StyleSheet styleSheet, final InstanceID instanceID,
final String text ) {
ArgumentNullException.validate( "originatingTextNode", originatingTextNode );
ArgumentNullException.validate( "attributes", attributes );
ArgumentNullException.validate( "text", text );
ArgumentNullException.validate( "originalAttributes", originalAttributes );
ArgumentNullException.validate( "styleSheet", styleSheet );
ArgumentNullException.validate( "instanceID", instanceID );
this.instanceID = instanceID;
this.start = start;
this.end = end;
this.originatingTextNode = originatingTextNode;
this.attributes = Collections.unmodifiableMap( attributes );
this.originalAttributes = originalAttributes;
this.styleSheet = styleSheet;
this.text = text;
}
/**
* Reads the object from the object input stream.
*
* @param stream the object input stream from where to read the serialized data.
* @return the generated object.
* @throws IOException if reading the stream failed.
* @throws ClassNotFoundException if serialized object class cannot be found.
*/
public Object readObject( final ObjectInputStream stream )
throws IOException, ClassNotFoundException {
// read string and attributes then create result
final String plainStr = (String) stream.readObject();
final AttributedString result = new AttributedString( plainStr );
char c = stream.readChar();
int start = 0;
while ( c != CharacterIterator.DONE ) {
final int limit = stream.readInt();
final Map<AttributedCharacterIterator.Attribute, Object> atts =
(Map<AttributedCharacterIterator.Attribute, Object>) stream.readObject();
result.addAttributes( atts, start, limit );
start = limit;
c = stream.readChar();
}
return result;
}
public int getRunStart(AttributedCharacterIterator.Attribute att) {
if ((att != TextAttribute.FONT) && (att != TextAttribute.FOREGROUND)) {
return 0; // undefined attribute
}
return getRunStart();
}
/**
* Returns the index of the first character of the run
* with respect to the given attribute containing the current character.
*/
public int getRunStart(AttributedCharacterIterator.Attribute attribute) {
if (attribute instanceof TextAttribute) {
int pos = toModelPosition(getIndex());
int i = v.getViewIndex(pos, Position.Bias.Forward);
if (attribute == TextAttribute.FONT) {
return toIteratorIndex(getFontBoundary(i, -1));
}
}
return getBeginIndex();
}
public int getRunLimit(Set<? extends AttributedCharacterIterator.Attribute> attributes) {
if (attributes.contains(TextAttribute.FONT) || attributes.contains(TextAttribute.FOREGROUND)) {
return getRunLimit();
} else {
return getEndIndex();
}
}
/**
* Returns the value of the named attribute for the current character.
* Returns null if the attribute is not defined.
* @param attribute the key of the attribute whose value is requested.
*/
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
int pos = toModelPosition(getIndex());
int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
if (attribute == TextAttribute.FONT) {
return getFont(childIndex);
} else if( attribute == TextAttribute.RUN_DIRECTION ) {
return
v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
} else if (attribute == TextAttribute.NUMERIC_SHAPING) {
return shaper;
}
return null;
}
@SuppressWarnings("UseSpecificCatch")
static SwingFont getFont(String name, float size, boolean bold, boolean italic) {
if (!Theme.Font.FONTNAME.equals(name))
DesktopGraphicsBridge.loadFonts();
Map<AttributedCharacterIterator.Attribute, Object> attributes = new HashMap<>();
attributes.put(FAMILY, name);
attributes.put(WEIGHT, bold ? WEIGHT_BOLD : WEIGHT_REGULAR);
attributes.put(POSTURE, italic ? POSTURE_OBLIQUE : POSTURE_REGULAR);
attributes.put(SIZE, size);
return new SwingFont(new Font(attributes));
}
/**
* Returns the value of the named attribute for the current character.
* Returns null if the attribute is not defined.
* @param attribute the key of the attribute whose value is requested.
*/
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
int pos = toModelPosition(getIndex());
int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
if (attribute == TextAttribute.FONT) {
return getFont(childIndex);
} else if( attribute == TextAttribute.RUN_DIRECTION ) {
return
v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
} else if (attribute == TextAttribute.NUMERIC_SHAPING) {
return shaper;
}
return null;
}
/**
* Returns the index of the first character following the run
* with respect to the given attribute containing the current character.
*/
public int getRunLimit(AttributedCharacterIterator.Attribute attribute) {
if (attribute instanceof TextAttribute) {
int pos = toModelPosition(getIndex());
int i = v.getViewIndex(pos, Position.Bias.Forward);
if (attribute == TextAttribute.FONT) {
return toIteratorIndex(getFontBoundary(i, 1));
}
}
return getEndIndex();
}
/**
* Returns the value of the named attribute for the current character.
* Returns null if the attribute is not defined.
* @param attribute the key of the attribute whose value is requested.
*/
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
int pos = toModelPosition(getIndex());
int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
if (attribute == TextAttribute.FONT) {
return getFont(childIndex);
} else if( attribute == TextAttribute.RUN_DIRECTION ) {
return
v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
} else if (attribute == TextAttribute.NUMERIC_SHAPING) {
return shaper;
}
return null;
}
@SuppressWarnings("serial")
private static AttributedCharacterIterator.Attribute
getTextAttribute(String name)
{
if (clazz == null) {
// fake attribute
return new AttributedCharacterIterator.Attribute(name) { };
} else {
return (AttributedCharacterIterator.Attribute)getStaticField(clazz, name);
}
}
@SuppressWarnings("serial")
private static AttributedCharacterIterator.Attribute
getTextAttribute(String name)
{
if (clazz == null) {
// fake attribute
return new AttributedCharacterIterator.Attribute(name) { };
} else {
return (AttributedCharacterIterator.Attribute)getStaticField(clazz, name);
}
}
/**
* Returns the index of the first character following the run
* with respect to the given attribute containing the current character.
*/
public int getRunLimit(AttributedCharacterIterator.Attribute attribute) {
if (attribute instanceof TextAttribute) {
int pos = toModelPosition(getIndex());
int i = v.getViewIndex(pos, Position.Bias.Forward);
if (attribute == TextAttribute.FONT) {
return toIteratorIndex(getFontBoundary(i, 1));
}
}
return getEndIndex();
}
private void populateAttributedText(List<Serializable> content, String text, Map<AttributedCharacterIterator.Attribute,Object> attributes, Direction blockDirection) {
Span sEmphasis = null;
Span sRuby = null;
Span sCombine = null;
Span sWideBar = null;
Span sOuter = null;
int numExclusive = 0;
for (Map.Entry<AttributedCharacterIterator.Attribute,Object> e : attributes.entrySet()) {
Attribute a = (Attribute) e.getValue();
if (a.isEmphasis()) {
sEmphasis = createEmphasis(text, a, blockDirection);
++numExclusive;
} else if (a.isRuby()) {
sRuby = createRuby(text, a, blockDirection);
++numExclusive;
} else if (a.isCombine()) {
sCombine = createCombine(text, a, blockDirection);
++numExclusive;
} else if (a.isStretchWide() && isHorizontalBar(text)) {
sWideBar = createWideBar(text, a, blockDirection);
++numExclusive;
} else {
if (sOuter == null)
sOuter = createStyledSpan(null, a);
else
sOuter = augmentStyledSpan(sOuter, a);
}
}
if (numExclusive > 1)
throw new IllegalStateException();
if (sOuter == null) {
if (sEmphasis != null)
sOuter = sEmphasis;
else if (sRuby != null)
sOuter = sRuby;
else if (sCombine != null)
sOuter = sCombine;
else if (sWideBar != null)
sOuter = sWideBar;
} else {
Span sInner = null;
if (sEmphasis != null)
sInner = sEmphasis;
else if (sRuby != null)
sInner = sRuby;
else if (sCombine != null)
sInner = sCombine;
else if (sWideBar != null)
sInner = sWideBar;
if (sInner != null) {
sOuter.getContent().add(ttmlFactory.createSpan(sInner));
} else {
sOuter.getContent().add(text);
}
}
if (sOuter != null)
content.add(ttmlFactory.createSpan(sOuter));
}
public Map<AttributedCharacterIterator.Attribute, Object> getAttributes() {
return attributes;
}
public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute, int value) {
this(start, end, attribute, new Integer(value));
}
@Override
public Font deriveFont(final Map<? extends AttributedCharacterIterator.Attribute, ?> attributes) {
return new DefaultUIResourceFont(super.deriveFont(attributes));
}