use of org.apache.poi.hssf.record.common.UnicodeString.FormatRun in project poi by apache.
the class TestUnicodeString method formatRun.
@Test
public void formatRun() throws Exception {
FormatRun fr = new FormatRun((short) 4, (short) 0x15c);
assertEquals(4, fr.getCharacterPos());
assertEquals(0x15c, fr.getFontIndex());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LittleEndianOutputStream out = new LittleEndianOutputStream(baos);
fr.serialize(out);
byte[] b = baos.toByteArray();
assertEquals(4, b.length);
assertEquals(4, b[0]);
assertEquals(0, b[1]);
assertEquals(0x5c, b[2]);
assertEquals(0x01, b[3]);
LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(b));
fr = new FormatRun(inp);
assertEquals(4, fr.getCharacterPos());
assertEquals(0x15c, fr.getFontIndex());
}
use of org.apache.poi.hssf.record.common.UnicodeString.FormatRun in project poi by apache.
the class HSSFRichTextString method applyFont.
/**
* Applies a font to the specified characters of a string.
*
* @param startIndex The start index to apply the font to (inclusive)
* @param endIndex The end index to apply the font to (exclusive)
* @param fontIndex The font to use.
*/
public void applyFont(int startIndex, int endIndex, short fontIndex) {
if (startIndex > endIndex)
throw new IllegalArgumentException("Start index must be less than end index.");
if (startIndex < 0 || endIndex > length())
throw new IllegalArgumentException("Start and end index not in range.");
if (startIndex == endIndex)
return;
//Need to check what the font is currently, so we can reapply it after
//the range is completed
short currentFont = NO_FONT;
if (endIndex != length()) {
currentFont = this.getFontAtIndex(endIndex);
}
//Need to clear the current formatting between the startIndex and endIndex
_string = cloneStringIfRequired();
Iterator<FormatRun> formatting = _string.formatIterator();
if (formatting != null) {
while (formatting.hasNext()) {
UnicodeString.FormatRun r = formatting.next();
if ((r.getCharacterPos() >= startIndex) && (r.getCharacterPos() < endIndex))
formatting.remove();
}
}
_string.addFormatRun(new UnicodeString.FormatRun((short) startIndex, fontIndex));
if (endIndex != length())
_string.addFormatRun(new UnicodeString.FormatRun((short) endIndex, currentFont));
addToSSTIfRequired();
}
Aggregations