Search in sources :

Example 6 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class TestXSSFComment method setString.

@Test
public void setString() {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sh = wb.createSheet();
    XSSFComment comment = sh.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
    //passing HSSFRichTextString is incorrect
    try {
        comment.setString(new HSSFRichTextString(TEST_RICHTEXTSTRING));
        fail("expected exception");
    } catch (IllegalArgumentException e) {
        assertEquals("Only XSSFRichTextString argument is supported", e.getMessage());
    }
    //simple string argument
    comment.setString(TEST_RICHTEXTSTRING);
    assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
    //if the text is already set, it should be overridden, not added twice!
    comment.setString(TEST_RICHTEXTSTRING);
    CTComment ctComment = comment.getCTComment();
    XmlObject[] obj = ctComment.selectPath("declare namespace w='" + NS_SPREADSHEETML + "' .//w:text");
    assertEquals(1, obj.length);
    assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
    //sequential call of comment.getString() should return the same XSSFRichTextString object
    assertSame(comment.getString(), comment.getString());
    XSSFRichTextString richText = new XSSFRichTextString(TEST_RICHTEXTSTRING);
    XSSFFont font1 = wb.createFont();
    font1.setFontName("Tahoma");
    font1.setFontHeight(8.5);
    font1.setItalic(true);
    font1.setColor(IndexedColors.BLUE_GREY.getIndex());
    richText.applyFont(0, 5, font1);
    //check the low-level stuff
    comment.setString(richText);
    obj = ctComment.selectPath("declare namespace w='" + NS_SPREADSHEETML + "' .//w:text");
    assertEquals(1, obj.length);
    assertSame(comment.getString(), richText);
    //check that the rich text is set in the comment
    CTRPrElt rPr = richText.getCTRst().getRArray(0).getRPr();
    assertEquals(true, rPr.getIArray(0).getVal());
    assertEquals(8.5, rPr.getSzArray(0).getVal(), 0);
    assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
    assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) XmlObject(org.apache.xmlbeans.XmlObject) Test(org.junit.Test)

Example 7 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class TestXSSFRichTextString method testApplyFont_lowlevel.

public void testApplyFont_lowlevel() {
    CTRst st = CTRst.Factory.newInstance();
    String text = "Apache Software Foundation";
    XSSFRichTextString str = new XSSFRichTextString(text);
    assertEquals(26, text.length());
    st.addNewR().setT(text);
    TreeMap<Integer, CTRPrElt> formats = str.getFormatMap(st);
    assertEquals(1, formats.size());
    assertEquals(26, (int) formats.firstKey());
    assertNull(formats.get(formats.firstKey()));
    CTRPrElt fmt1 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 0, 6, fmt1);
    assertEquals(2, formats.size());
    assertEquals("[6, 26]", formats.keySet().toString());
    Object[] runs1 = formats.values().toArray();
    assertSame(fmt1, runs1[0]);
    assertSame(null, runs1[1]);
    CTRPrElt fmt2 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 7, 15, fmt2);
    assertEquals(4, formats.size());
    assertEquals("[6, 7, 15, 26]", formats.keySet().toString());
    Object[] runs2 = formats.values().toArray();
    assertSame(fmt1, runs2[0]);
    assertSame(null, runs2[1]);
    assertSame(fmt2, runs2[2]);
    assertSame(null, runs2[3]);
    CTRPrElt fmt3 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 6, 7, fmt3);
    assertEquals(4, formats.size());
    assertEquals("[6, 7, 15, 26]", formats.keySet().toString());
    Object[] runs3 = formats.values().toArray();
    assertSame(fmt1, runs3[0]);
    assertSame(fmt3, runs3[1]);
    assertSame(fmt2, runs3[2]);
    assertSame(null, runs3[3]);
    CTRPrElt fmt4 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 0, 7, fmt4);
    assertEquals(3, formats.size());
    assertEquals("[7, 15, 26]", formats.keySet().toString());
    Object[] runs4 = formats.values().toArray();
    assertSame(fmt4, runs4[0]);
    assertSame(fmt2, runs4[1]);
    assertSame(null, runs4[2]);
    CTRPrElt fmt5 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 0, 26, fmt5);
    assertEquals(1, formats.size());
    assertEquals("[26]", formats.keySet().toString());
    Object[] runs5 = formats.values().toArray();
    assertSame(fmt5, runs5[0]);
    CTRPrElt fmt6 = CTRPrElt.Factory.newInstance();
    str.applyFont(formats, 15, 26, fmt6);
    assertEquals(2, formats.size());
    assertEquals("[15, 26]", formats.keySet().toString());
    Object[] runs6 = formats.values().toArray();
    assertSame(fmt5, runs6[0]);
    assertSame(fmt6, runs6[1]);
    str.applyFont(formats, 0, 26, null);
    assertEquals(1, formats.size());
    assertEquals("[26]", formats.keySet().toString());
    Object[] runs7 = formats.values().toArray();
    assertSame(null, runs7[0]);
    str.applyFont(formats, 15, 26, fmt6);
    assertEquals(2, formats.size());
    assertEquals("[15, 26]", formats.keySet().toString());
    Object[] runs8 = formats.values().toArray();
    assertSame(null, runs8[0]);
    assertSame(fmt6, runs8[1]);
    str.applyFont(formats, 15, 26, fmt5);
    assertEquals(2, formats.size());
    assertEquals("[15, 26]", formats.keySet().toString());
    Object[] runs9 = formats.values().toArray();
    assertSame(null, runs9[0]);
    assertSame(fmt5, runs9[1]);
    str.applyFont(formats, 2, 20, fmt6);
    assertEquals(3, formats.size());
    assertEquals("[2, 20, 26]", formats.keySet().toString());
    Object[] runs10 = formats.values().toArray();
    assertSame(null, runs10[0]);
    assertSame(fmt6, runs10[1]);
    assertSame(fmt5, runs10[2]);
    str.applyFont(formats, 22, 24, fmt4);
    assertEquals(5, formats.size());
    assertEquals("[2, 20, 22, 24, 26]", formats.keySet().toString());
    Object[] runs11 = formats.values().toArray();
    assertSame(null, runs11[0]);
    assertSame(fmt6, runs11[1]);
    assertSame(fmt5, runs11[2]);
    assertSame(fmt4, runs11[3]);
    assertSame(fmt5, runs11[4]);
    str.applyFont(formats, 0, 10, fmt1);
    assertEquals(5, formats.size());
    assertEquals("[10, 20, 22, 24, 26]", formats.keySet().toString());
    Object[] runs12 = formats.values().toArray();
    assertSame(fmt1, runs12[0]);
    assertSame(fmt6, runs12[1]);
    assertSame(fmt5, runs12[2]);
    assertSame(fmt4, runs12[3]);
    assertSame(fmt5, runs12[4]);
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)

Example 8 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class XSSFRichTextString 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 to font to (exclusive)
     * @param font          The index of the font to use.
     */
public void applyFont(int startIndex, int endIndex, Font font) {
    if (startIndex > endIndex)
        throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex);
    if (startIndex < 0 || endIndex > length())
        throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex);
    if (startIndex == endIndex)
        return;
    if (st.sizeOfRArray() == 0 && st.isSetT()) {
        //convert <t>string</t> into a text run: <r><t>string</t></r>
        st.addNewR().setT(st.getT());
        st.unsetT();
    }
    String text = getString();
    XSSFFont xssfFont = (XSSFFont) font;
    TreeMap<Integer, CTRPrElt> formats = getFormatMap(st);
    CTRPrElt fmt = CTRPrElt.Factory.newInstance();
    setRunAttributes(xssfFont.getCTFont(), fmt);
    applyFont(formats, startIndex, endIndex, fmt);
    CTRst newSt = buildCTRst(text, formats);
    st.set(newSt);
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 9 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class XSSFRichTextString method setRunAttributes.

/**
     * Copy font attributes from CTFont bean into CTRPrElt bean
     */
private void setRunAttributes(CTFont ctFont, CTRPrElt pr) {
    if (ctFont.sizeOfBArray() > 0)
        pr.addNewB().setVal(ctFont.getBArray(0).getVal());
    if (ctFont.sizeOfUArray() > 0)
        pr.addNewU().setVal(ctFont.getUArray(0).getVal());
    if (ctFont.sizeOfIArray() > 0)
        pr.addNewI().setVal(ctFont.getIArray(0).getVal());
    if (ctFont.sizeOfColorArray() > 0) {
        CTColor c1 = ctFont.getColorArray(0);
        CTColor c2 = pr.addNewColor();
        if (c1.isSetAuto())
            c2.setAuto(c1.getAuto());
        if (c1.isSetIndexed())
            c2.setIndexed(c1.getIndexed());
        if (c1.isSetRgb())
            c2.setRgb(c1.getRgb());
        if (c1.isSetTheme())
            c2.setTheme(c1.getTheme());
        if (c1.isSetTint())
            c2.setTint(c1.getTint());
    }
    if (ctFont.sizeOfSzArray() > 0)
        pr.addNewSz().setVal(ctFont.getSzArray(0).getVal());
    if (ctFont.sizeOfNameArray() > 0)
        pr.addNewRFont().setVal(ctFont.getNameArray(0).getVal());
    if (ctFont.sizeOfFamilyArray() > 0)
        pr.addNewFamily().setVal(ctFont.getFamilyArray(0).getVal());
    if (ctFont.sizeOfSchemeArray() > 0)
        pr.addNewScheme().setVal(ctFont.getSchemeArray(0).getVal());
    if (ctFont.sizeOfCharsetArray() > 0)
        pr.addNewCharset().setVal(ctFont.getCharsetArray(0).getVal());
    if (ctFont.sizeOfCondenseArray() > 0)
        pr.addNewCondense().setVal(ctFont.getCondenseArray(0).getVal());
    if (ctFont.sizeOfExtendArray() > 0)
        pr.addNewExtend().setVal(ctFont.getExtendArray(0).getVal());
    if (ctFont.sizeOfVertAlignArray() > 0)
        pr.addNewVertAlign().setVal(ctFont.getVertAlignArray(0).getVal());
    if (ctFont.sizeOfOutlineArray() > 0)
        pr.addNewOutline().setVal(ctFont.getOutlineArray(0).getVal());
    if (ctFont.sizeOfShadowArray() > 0)
        pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
    if (ctFont.sizeOfStrikeArray() > 0)
        pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
}
Also used : CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor)

Example 10 with CTRPrElt

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt in project poi by apache.

the class XSSFRichTextString method toCTFont.

/**
     *
     * CTRPrElt --> CTFont adapter
     */
protected static CTFont toCTFont(CTRPrElt pr) {
    CTFont ctFont = CTFont.Factory.newInstance();
    // Bug 58315: there are files where there is no pr-entry for a RichTextString
    if (pr == null) {
        return ctFont;
    }
    if (pr.sizeOfBArray() > 0)
        ctFont.addNewB().setVal(pr.getBArray(0).getVal());
    if (pr.sizeOfUArray() > 0)
        ctFont.addNewU().setVal(pr.getUArray(0).getVal());
    if (pr.sizeOfIArray() > 0)
        ctFont.addNewI().setVal(pr.getIArray(0).getVal());
    if (pr.sizeOfColorArray() > 0) {
        CTColor c1 = pr.getColorArray(0);
        CTColor c2 = ctFont.addNewColor();
        if (c1.isSetAuto())
            c2.setAuto(c1.getAuto());
        if (c1.isSetIndexed())
            c2.setIndexed(c1.getIndexed());
        if (c1.isSetRgb())
            c2.setRgb(c1.getRgb());
        if (c1.isSetTheme())
            c2.setTheme(c1.getTheme());
        if (c1.isSetTint())
            c2.setTint(c1.getTint());
    }
    if (pr.sizeOfSzArray() > 0)
        ctFont.addNewSz().setVal(pr.getSzArray(0).getVal());
    if (pr.sizeOfRFontArray() > 0)
        ctFont.addNewName().setVal(pr.getRFontArray(0).getVal());
    if (pr.sizeOfFamilyArray() > 0)
        ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal());
    if (pr.sizeOfSchemeArray() > 0)
        ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal());
    if (pr.sizeOfCharsetArray() > 0)
        ctFont.addNewCharset().setVal(pr.getCharsetArray(0).getVal());
    if (pr.sizeOfCondenseArray() > 0)
        ctFont.addNewCondense().setVal(pr.getCondenseArray(0).getVal());
    if (pr.sizeOfExtendArray() > 0)
        ctFont.addNewExtend().setVal(pr.getExtendArray(0).getVal());
    if (pr.sizeOfVertAlignArray() > 0)
        ctFont.addNewVertAlign().setVal(pr.getVertAlignArray(0).getVal());
    if (pr.sizeOfOutlineArray() > 0)
        ctFont.addNewOutline().setVal(pr.getOutlineArray(0).getVal());
    if (pr.sizeOfShadowArray() > 0)
        ctFont.addNewShadow().setVal(pr.getShadowArray(0).getVal());
    if (pr.sizeOfStrikeArray() > 0)
        ctFont.addNewStrike().setVal(pr.getStrikeArray(0).getVal());
    return ctFont;
}
Also used : CTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont) CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor)

Aggregations

CTRPrElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)10 CTRElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt)7 RichTextString (org.apache.poi.ss.usermodel.RichTextString)4 CTRst (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)4 CTColor (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor)2 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)1 XmlObject (org.apache.xmlbeans.XmlObject)1 Test (org.junit.Test)1 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)1 CTFont (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont)1