git-svn-id: https://samskivert.googlecode.com/svn/trunk@1769 6335cc39-0255-0410-8fd6-9bcaacd3b74c

This commit is contained in:
ray
2006-01-23 19:12:55 +00:00
parent ae49badf90
commit c0b7ad1942
@@ -636,6 +636,7 @@ public class Label implements SwingConstants, LabelStyleConstants
*/ */
protected AttributedCharacterIterator textIterator (Graphics2D gfx) protected AttributedCharacterIterator textIterator (Graphics2D gfx)
{ {
// first set up any attributes that apply to the entire text
Font font = (_font == null) ? gfx.getFont() : _font; Font font = (_font == null) ? gfx.getFont() : _font;
HashMap map = new HashMap(); HashMap map = new HashMap();
map.put(TextAttribute.FONT, font); map.put(TextAttribute.FONT, font);
@@ -645,20 +646,20 @@ public class Label implements SwingConstants, LabelStyleConstants
} }
AttributedString text = new AttributedString(_text, map); AttributedString text = new AttributedString(_text, map);
// add any color attributes // add any color attributes for specific segments
if (_rawText != null) { if (_rawText != null) {
Matcher m = COLOR_PATTERN.matcher(_rawText); Matcher m = COLOR_PATTERN.matcher(_rawText);
int startSeg = 0; int startSeg = 0, endSeg = 0;
StringBuffer buf = new StringBuffer();
Color lastColor = null; Color lastColor = null;
while (m.find()) { while (m.find()) {
startSeg = buf.length(); // color the segment just passed
m.appendReplacement(buf, ""); endSeg += m.start();
if (lastColor != null) { if (lastColor != null) {
text.addAttribute(TextAttribute.FOREGROUND, lastColor, text.addAttribute(TextAttribute.FOREGROUND, lastColor,
startSeg, buf.length()); startSeg, endSeg);
} }
// parse the tag: start or end a color
String group = m.group(1); String group = m.group(1);
if ("x".equalsIgnoreCase(group)) { if ("x".equalsIgnoreCase(group)) {
lastColor = null; lastColor = null;
@@ -670,12 +671,18 @@ public class Label implements SwingConstants, LabelStyleConstants
"is precise [badcolor=" + group + "]."); "is precise [badcolor=" + group + "].");
} }
} }
// prepare for the next segment
startSeg = endSeg;
// Subtract the end of the segment from endSeg
// so that when we add the start of the next match we have
// actually added the length of the characters in between.
endSeg -= m.end();
} }
startSeg = buf.length(); // apply any final color to the tail segment
m.appendTail(buf);
if (lastColor != null) { if (lastColor != null) {
text.addAttribute(TextAttribute.FOREGROUND, lastColor, text.addAttribute(TextAttribute.FOREGROUND, lastColor,
startSeg, buf.length()); startSeg, _text.length());
} }
} }