Search in sources :

Example 1 with FormatRun

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());
}
Also used : LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FormatRun(org.apache.poi.hssf.record.common.UnicodeString.FormatRun) LittleEndianOutputStream(org.apache.poi.util.LittleEndianOutputStream) LittleEndianInputStream(org.apache.poi.util.LittleEndianInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) Test(org.junit.Test)

Example 2 with FormatRun

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();
}
Also used : UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) FormatRun(org.apache.poi.hssf.record.common.UnicodeString.FormatRun) FormatRun(org.apache.poi.hssf.record.common.UnicodeString.FormatRun)

Aggregations

FormatRun (org.apache.poi.hssf.record.common.UnicodeString.FormatRun)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)1 LittleEndianByteArrayInputStream (org.apache.poi.util.LittleEndianByteArrayInputStream)1 LittleEndianByteArrayOutputStream (org.apache.poi.util.LittleEndianByteArrayOutputStream)1 LittleEndianInputStream (org.apache.poi.util.LittleEndianInputStream)1 LittleEndianOutputStream (org.apache.poi.util.LittleEndianOutputStream)1 Test (org.junit.Test)1