Search in sources :

Example 6 with TextPropCollection

use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.

the class HSLFTextParagraph method setPropVal.

/**
     * Returns the named TextProp, either by fetching it (if it exists) or
     * adding it (if it didn't)
     *
     * @param props the TextPropCollection to fetch from / add into
     * @param name the name of the TextProp to fetch/add
     * @param val the value, null if unset
     */
protected void setPropVal(TextPropCollection props, TextPropCollection masterProps, String name, Integer val) {
    TextPropCollection pc = props;
    if (getSheet() instanceof MasterSheet && masterProps != null) {
        pc = masterProps;
    }
    if (val == null) {
        pc.removeByName(name);
        return;
    }
    // Fetch / Add the TextProp
    TextProp tp = pc.addWithName(name);
    tp.setValue(val);
}
Also used : BitMaskTextProp(org.apache.poi.hslf.model.textproperties.BitMaskTextProp) TextProp(org.apache.poi.hslf.model.textproperties.TextProp) ParagraphFlagsTextProp(org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp) MasterSheet(org.apache.poi.sl.usermodel.MasterSheet) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection)

Example 7 with TextPropCollection

use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.

the class HSLFTextParagraph method updateStyles.

/**
     * Update paragraph and character styles - merges them when subsequential styles match
     */
private static void updateStyles(List<HSLFTextParagraph> paragraphs) {
    final String rawText = toInternalString(getRawText(paragraphs));
    TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
    StyleTextPropAtom styleAtom = findStyleAtomPresent(headerAtom, rawText.length());
    // Update the text length for its Paragraph and Character stylings
    // * reset the length, to the new string's length
    // * add on +1 if the last block
    styleAtom.clearStyles();
    TextPropCollection lastPTPC = null, lastRTPC = null, ptpc = null, rtpc = null;
    for (HSLFTextParagraph para : paragraphs) {
        ptpc = para.getParagraphStyle();
        ptpc.updateTextSize(0);
        if (!ptpc.equals(lastPTPC)) {
            lastPTPC = styleAtom.addParagraphTextPropCollection(0);
            lastPTPC.copy(ptpc);
        }
        for (HSLFTextRun tr : para.getTextRuns()) {
            rtpc = tr.getCharacterStyle();
            rtpc.updateTextSize(0);
            if (!rtpc.equals(lastRTPC)) {
                lastRTPC = styleAtom.addCharacterTextPropCollection(0);
                lastRTPC.copy(rtpc);
            }
            int len = tr.getLength();
            ptpc.updateTextSize(ptpc.getCharactersCovered() + len);
            rtpc.updateTextSize(len);
            lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + len);
            lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + len);
        }
    }
    if (lastPTPC == null || lastRTPC == null || ptpc == null || rtpc == null) {
        // NOSONAR
        throw new HSLFException("Not all TextPropCollection could be determined.");
    }
    ptpc.updateTextSize(ptpc.getCharactersCovered() + 1);
    rtpc.updateTextSize(rtpc.getCharactersCovered() + 1);
    lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + 1);
    lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + 1);
    /**
         * If TextSpecInfoAtom is present, we must update the text size in it,
         * otherwise the ppt will be corrupted
         */
    for (Record r : paragraphs.get(0).getRecords()) {
        if (r instanceof TextSpecInfoAtom) {
            ((TextSpecInfoAtom) r).setParentSize(rawText.length() + 1);
            break;
        }
    }
}
Also used : TextSpecInfoAtom(org.apache.poi.hslf.record.TextSpecInfoAtom) HSLFException(org.apache.poi.hslf.exceptions.HSLFException) Record(org.apache.poi.hslf.record.Record) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom) StyleTextPropAtom(org.apache.poi.hslf.record.StyleTextPropAtom) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)

Example 8 with TextPropCollection

use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.

the class TestTextRun method testChangeTextInRichTextRun.

/**
	 * Tests to ensure changing the text within rich text runs works
	 *  correctly
	 */
@Test
public void testChangeTextInRichTextRun() {
    HSLFSlide slideOne = ssRich.getSlides().get(0);
    List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
    List<HSLFTextParagraph> trB = textParass.get(1);
    assertEquals(3, trB.size());
    // We start with 3 text runs, each with their own set of styles,
    // but all sharing the same paragraph styles
    HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
    HSLFTextRun rtrC = trB.get(1).getTextRuns().get(0);
    HSLFTextRun rtrD = trB.get(2).getTextRuns().get(0);
    TextPropCollection tpBP = rtrB.getTextParagraph().getParagraphStyle();
    TextPropCollection tpBC = rtrB.getCharacterStyle();
    TextPropCollection tpCP = rtrC.getTextParagraph().getParagraphStyle();
    TextPropCollection tpCC = rtrC.getCharacterStyle();
    TextPropCollection tpDP = rtrD.getTextParagraph().getParagraphStyle();
    TextPropCollection tpDC = rtrD.getCharacterStyle();
    // Check text and stylings
    assertEquals(HSLFTextParagraph.getRawText(trB).substring(0, 30), rtrB.getRawText());
    assertNotNull(tpBP);
    assertNotNull(tpBC);
    assertNotNull(tpCP);
    assertNotNull(tpCC);
    assertNotNull(tpDP);
    assertNotNull(tpDC);
    assertEquals(tpBP, tpCP);
    assertEquals(tpBP, tpDP);
    assertEquals(tpCP, tpDP);
    assertNotEquals(tpBC, tpCC);
    assertNotEquals(tpBC, tpDC);
    assertNotEquals(tpCC, tpDC);
    // Check text in the rich runs
    assertEquals("This is the subtitle, in bold\r", rtrB.getRawText());
    assertEquals("This bit is blue and italic\r", rtrC.getRawText());
    assertEquals("This bit is red (normal)", rtrD.getRawText());
    String newBText = "New Subtitle, will still be bold\n";
    String newCText = "New blue and italic text\n";
    String newDText = "Funky new normal red text";
    rtrB.setText(newBText);
    rtrC.setText(newCText);
    rtrD.setText(newDText);
    HSLFTextParagraph.storeText(trB);
    assertEquals(newBText.replace('\n', '\r'), rtrB.getRawText());
    assertEquals(newCText.replace('\n', '\r'), rtrC.getRawText());
    assertEquals(newDText.replace('\n', '\r'), rtrD.getRawText());
    assertEquals(newBText.replace('\n', '\r') + newCText.replace('\n', '\r') + newDText.replace('\n', '\r'), HSLFTextParagraph.getRawText(trB));
    // The styles should have been updated for the new sizes
    assertEquals(newBText.length(), tpBC.getCharactersCovered());
    assertEquals(newCText.length(), tpCC.getCharactersCovered());
    // Last one is always one larger
    assertEquals(newDText.length() + 1, tpDC.getCharactersCovered());
    // Paragraph style should be sum of text length
    assertEquals(newBText.length() + newCText.length() + newDText.length() + 1, tpBP.getCharactersCovered() + tpCP.getCharactersCovered() + tpDP.getCharactersCovered());
    // Check stylings still as expected
    TextPropCollection ntpBC = rtrB.getCharacterStyle();
    TextPropCollection ntpCC = rtrC.getCharacterStyle();
    TextPropCollection ntpDC = rtrD.getCharacterStyle();
    assertEquals(tpBC.getTextPropList(), ntpBC.getTextPropList());
    assertEquals(tpCC.getTextPropList(), ntpCC.getTextPropList());
    assertEquals(tpDC.getTextPropList(), ntpDC.getTextPropList());
}
Also used : List(java.util.List) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) Test(org.junit.Test)

Example 9 with TextPropCollection

use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.

the class TestNumberedList3 method checkSingleRunWrapper.

private void checkSingleRunWrapper(final int exceptedLength, final EscherTextboxWrapper wrapper) {
    final StyleTextPropAtom styleTextPropAtom = wrapper.getStyleTextPropAtom();
    final List<TextPropCollection> textProps = styleTextPropAtom.getCharacterStyles();
    assertEquals(1, textProps.size());
    assertEquals(exceptedLength, textProps.get(0).getCharactersCovered());
}
Also used : StyleTextPropAtom(org.apache.poi.hslf.record.StyleTextPropAtom) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection)

Example 10 with TextPropCollection

use of org.apache.poi.hslf.model.textproperties.TextPropCollection in project poi by apache.

the class TestTextRun method testSetTextWhereRich.

/**
	 * Tests to ensure that setting the text where the text is rich
	 *  sets everything to the same styling
	 */
@Test
public void testSetTextWhereRich() {
    HSLFSlide slideOne = ssRich.getSlides().get(0);
    List<List<HSLFTextParagraph>> textParass = slideOne.getTextParagraphs();
    List<HSLFTextParagraph> trB = textParass.get(1);
    assertEquals(3, trB.size());
    HSLFTextRun rtrB = trB.get(0).getTextRuns().get(0);
    HSLFTextRun rtrC = trB.get(1).getTextRuns().get(0);
    HSLFTextRun rtrD = trB.get(2).getTextRuns().get(0);
    TextPropCollection tpBP = rtrB.getTextParagraph().getParagraphStyle();
    TextPropCollection tpBC = rtrB.getCharacterStyle();
    TextPropCollection tpCP = rtrC.getTextParagraph().getParagraphStyle();
    TextPropCollection tpCC = rtrC.getCharacterStyle();
    TextPropCollection tpDP = rtrD.getTextParagraph().getParagraphStyle();
    TextPropCollection tpDC = rtrD.getCharacterStyle();
    //		assertEquals(trB.getRawText().substring(0, 30), rtrB.getRawText());
    assertNotNull(tpBP);
    assertNotNull(tpBC);
    assertNotNull(tpCP);
    assertNotNull(tpCC);
    assertNotNull(tpDP);
    assertNotNull(tpDC);
    assertEquals(tpBP, tpCP);
    assertEquals(tpBP, tpDP);
    assertEquals(tpCP, tpDP);
    assertNotEquals(tpBC, tpCC);
    assertNotEquals(tpBC, tpDC);
    assertNotEquals(tpCC, tpDC);
    // Change text via normal
    HSLFTextParagraph.setText(trB, "Test Foo Test");
    // Ensure now have first style
    assertEquals(1, trB.get(0).getTextRuns().size());
    rtrB = trB.get(0).getTextRuns().get(0);
    assertEquals("Test Foo Test", HSLFTextParagraph.getRawText(trB));
    assertEquals("Test Foo Test", rtrB.getRawText());
    assertNotNull(rtrB.getCharacterStyle());
    assertNotNull(rtrB.getTextParagraph().getParagraphStyle());
    assertEquals(tpBP, rtrB.getTextParagraph().getParagraphStyle());
    assertEquals(tpBC, rtrB.getCharacterStyle());
}
Also used : List(java.util.List) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) Test(org.junit.Test)

Aggregations

TextPropCollection (org.apache.poi.hslf.model.textproperties.TextPropCollection)25 TextProp (org.apache.poi.hslf.model.textproperties.TextProp)6 StyleTextPropAtom (org.apache.poi.hslf.record.StyleTextPropAtom)4 List (java.util.List)3 HSLFException (org.apache.poi.hslf.exceptions.HSLFException)3 DrawPaint (org.apache.poi.sl.draw.DrawPaint)3 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TextHeaderAtom (org.apache.poi.hslf.record.TextHeaderAtom)2 TxMasterStyleAtom (org.apache.poi.hslf.record.TxMasterStyleAtom)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 BitMaskTextProp (org.apache.poi.hslf.model.textproperties.BitMaskTextProp)1 ParagraphFlagsTextProp (org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp)1 TextPFException9 (org.apache.poi.hslf.model.textproperties.TextPFException9)1 EscherTextboxWrapper (org.apache.poi.hslf.record.EscherTextboxWrapper)1 MainMaster (org.apache.poi.hslf.record.MainMaster)1 Record (org.apache.poi.hslf.record.Record)1 StyleTextProp9Atom (org.apache.poi.hslf.record.StyleTextProp9Atom)1 TextBytesAtom (org.apache.poi.hslf.record.TextBytesAtom)1