/** * Constructor. * * <p>A pleasant side-effect of constructing a Text() is that consecutive (redundant) colour * escape sequences are collapsed down to just the last colour. * * @param text the text with embedded colour escape sequences. */ public Text(String text) { // By default, text is white. Format format = new Format(Colour.white, 0); for (int i = 0; i < text.length(); ++i) { char c = text.charAt(i); if (c == Colour.ESCAPE_CHAR) { ++i; // Guard against the pathological case of a chat line ending in // Colour.ESCAPE_CHAR. if (i < text.length()) { char code = text.charAt(i); if (Colour.isColour(code)) { format.setColour(Colour.getByCode(code)); // Setting the colour disables any style attributes. format.setStyles(0); } // Note: different from Format.isAttribute() else if (isAttribute(code)) { format.applyStyle(code); } // else: silently delete format codes we don't understand. // At the time of writing, no such codes exist. } } else { // An ordinary, non-colour-escape character. _unformatted.append(c); _colourStyles.append(format.getColourStyle()); } } // for } // Text
/** * Set the format of the specified range of characters, [begin,end), (from inclusive begin to * exclusive end). * * <p>Note that (begin == end) is an empty range. * * @param begin the index of the first character in the range. * @param end one more than the index of the last character in the range. * @param format the Format to set. * @throws IllegalArgumentException if begin or end are out of the range * [0,toUnformattedString().length()] (inclusive) or (begin > end). */ public void setFormat(int begin, int end, Format format) { if (begin < 0 || end > _unformatted.length() || begin > end) { throw new IllegalArgumentException("illegal range in setColour()"); } // Does the format have a colour set? if (format.getColour() != null) { char colourStyle = format.getColourStyle(); for (int i = begin; i < end; ++i) { _colourStyles.setCharAt(i, colourStyle); } } else { // No colour. Just set the style bits of the characters in the range. for (int i = begin; i < end; ++i) { int colourStyle = (_colourStyles.charAt(i) & Format.COLOUR_MASK) | format.getStyles(); _colourStyles.setCharAt(i, (char) colourStyle); } } } // setFormat