コード例 #1
0
ファイル: Text.java プロジェクト: q220/watson
  /**
   * 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