int fontSize = 0;
Color textColour = null;
Color backColour = null;
int modifiers = 0;
TextStyle ts = null;
int attrsSet = 0; // If this is 0, we have no extra style
boolean inSequence = false;
boolean sawText = false;
String s = new String(); // For debugging
// Start from the very beginning
for (int i = 0; i < totalLength; i++) {
// Will encounter at least two codes first
if ((byte)(data[i] & 0xF0) == FORMATTING_TAG) {
if (sawText) {
// Style change so dump previous segment and style info
addTextSegment(sb.toString(), ts);
sb = new StringBuffer("");
sawText = false;
}
switch (data[i]) {
case FONT_TAG:
int index = EndianConverter.readShort(
new byte[] { data[i + 1], data[i + 2] } );
/*
* Standard font.
*
* Should really be one, but as the only supported font
* currently is Courier New, want to leave it at Courier
* New for round trip conversions.
*
* Also need to account for the fact that Tahoma is the
* correct standard font.
*/
if (fontName == null || fontName.equals("2")) {
if (index != 2 && index != 1) {
fontName = String.valueOf(index);
attrsSet++;
}
}
else {
// Font is set, but not the default
if (index == 2 || index == 1) {
fontName = "2";
attrsSet--;
}
else {
fontName = String.valueOf(index);
}
}
i += 2;
break;
case FONT_SIZE_TAG:
int size = EndianConverter.readShort(
new byte[] { data[i + 1], data[i + 2] } );
if (size == 0) {
// Flags the end of the last paragraph
isLastParagraph = true;
i += 2;
break;
}
// Standard size
if (fontSize == 0 || fontSize == 10) {
if (size != 10) {
fontSize = size;
attrsSet++;
}
}
else {
// Font size is set, but not to standard
if (size == 10) {
fontSize = 10;
attrsSet--;
}
else {
fontSize = size;
}
}
i += 2;
break;
case COLOUR_TAG:
if (data[i + 1] != 0) {
ColourConverter cc = new ColourConverter();
textColour = cc.convertToRGB(
EndianConverter.readShort(new byte[] { data[i + 1],
data[i + 2] } ));
attrsSet++;
}
else {
textColour = null;
attrsSet--;
}
i += 2;
break;
case FONT_WEIGHT_TAG:
if (data[i + 1] == FONT_WEIGHT_BOLD
|| data[i + 1] == FONT_WEIGHT_THICK) {
modifiers |= TextStyle.BOLD;
attrsSet++;
}
else {
// Its a bit field so subtracting should work okay.
modifiers ^= TextStyle.BOLD;
attrsSet--;
}
i += 2;
break;
case ITALIC_TAG:
if (data[i + 1] == (byte)0x01) {
modifiers |= TextStyle.ITALIC;
attrsSet++;
}
else {
modifiers ^= TextStyle.ITALIC;
attrsSet--;
}
i++;
break;
case UNDERLINE_TAG:
if (data[i + 1] == (byte)0x01) {
modifiers |= TextStyle.UNDERLINE;
attrsSet++;
}
else {
modifiers ^= TextStyle.UNDERLINE;
attrsSet--;
}
i++;
break;
case STRIKETHROUGH_TAG:
if (data[i + 1] == (byte)0x01) {
modifiers |= TextStyle.STRIKETHRU;
attrsSet++;
}
else {
modifiers ^= TextStyle.STRIKETHRU;
attrsSet--;
}
i++;
break;
case HIGHLIGHT_TAG:
/*
* Highlighting is treated by OpenOffice as a
* background colour.
*/
if (data[i + 1] == (byte)0x01) {
backColour = Color.yellow;
attrsSet++;
}
else {
backColour = null;
attrsSet--;
}
i++;
break;
}
inSequence = true;
continue;
}
if (inSequence) {
// Style information has been changed. Create new style here
inSequence = false;
if (attrsSet > 0) {
ts = new TextStyle(null, TEXT_STYLE_FAMILY, DEFAULT_STYLE,
mask, modifiers, fontSize, fontName, null);
ts.setColors(textColour, backColour);
}
else {
ts = null;
}
}