Search in sources :

Example 6 with TextHeaderAtom

use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.

the class TestTextRun method testAdvancedSetText.

/**
	 * Test to ensure that changing non rich text between bytes and
	 *  chars works correctly
	 */
@SuppressWarnings("unused")
@Test
public void testAdvancedSetText() {
    HSLFSlide slideOne = ss.getSlides().get(0);
    List<HSLFTextParagraph> paras = slideOne.getTextParagraphs().get(0);
    HSLFTextParagraph para = paras.get(0);
    TextHeaderAtom tha = null;
    TextBytesAtom tba = null;
    TextCharsAtom tca = null;
    for (Record r : para.getRecords()) {
        if (r instanceof TextHeaderAtom)
            tha = (TextHeaderAtom) r;
        else if (r instanceof TextBytesAtom)
            tba = (TextBytesAtom) r;
        else if (r instanceof TextCharsAtom)
            tca = (TextCharsAtom) r;
    }
    // Bytes -> Bytes
    assertNull(tca);
    assertNotNull(tba);
    // assertFalse(run._isUnicode);
    assertEquals("This is a test title", para.getTextRuns().get(0).getRawText());
    String changeBytesOnly = "New Test Title";
    HSLFTextParagraph.setText(paras, changeBytesOnly);
    para = paras.get(0);
    tha = null;
    tba = null;
    tca = null;
    for (Record r : para.getRecords()) {
        if (r instanceof TextHeaderAtom)
            tha = (TextHeaderAtom) r;
        else if (r instanceof TextBytesAtom)
            tba = (TextBytesAtom) r;
        else if (r instanceof TextCharsAtom)
            tca = (TextCharsAtom) r;
    }
    assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
    assertNull(tca);
    assertNotNull(tba);
    // Bytes -> Chars
    assertNull(tca);
    assertNotNull(tba);
    assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
    String changeByteChar = "This is a test title with a 'ġ' g with a dot";
    HSLFTextParagraph.setText(paras, changeByteChar);
    para = paras.get(0);
    tha = null;
    tba = null;
    tca = null;
    for (Record r : para.getRecords()) {
        if (r instanceof TextHeaderAtom)
            tha = (TextHeaderAtom) r;
        else if (r instanceof TextBytesAtom)
            tba = (TextBytesAtom) r;
        else if (r instanceof TextCharsAtom)
            tca = (TextCharsAtom) r;
    }
    assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
    assertNotNull(tca);
    assertNull(tba);
    // Chars -> Chars
    assertNull(tba);
    assertNotNull(tca);
    assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
    String changeCharChar = "This is a test title with a 'Ň' N with a hat";
    HSLFTextParagraph.setText(paras, changeCharChar);
    para = paras.get(0);
    tha = null;
    tba = null;
    tca = null;
    for (Record r : para.getRecords()) {
        if (r instanceof TextHeaderAtom)
            tha = (TextHeaderAtom) r;
        else if (r instanceof TextBytesAtom)
            tba = (TextBytesAtom) r;
        else if (r instanceof TextCharsAtom)
            tca = (TextCharsAtom) r;
    }
    assertEquals(changeCharChar, HSLFTextParagraph.getRawText(paras));
    assertNotNull(tca);
    assertNull(tba);
}
Also used : TextCharsAtom(org.apache.poi.hslf.record.TextCharsAtom) Record(org.apache.poi.hslf.record.Record) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom) TextBytesAtom(org.apache.poi.hslf.record.TextBytesAtom) Test(org.junit.Test)

Example 7 with TextHeaderAtom

use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.

the class HSLFTextParagraph method updateHyperlinks.

private static void updateHyperlinks(List<HSLFTextParagraph> paragraphs) {
    TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
    RecordContainer _txtbox = headerAtom.getParentRecord();
    // remove existing hyperlink records
    for (Record r : _txtbox.getChildRecords()) {
        if (r instanceof InteractiveInfo || r instanceof TxInteractiveInfoAtom) {
            _txtbox.removeChild(r);
        }
    }
    // now go through all the textruns and check for hyperlinks
    HSLFHyperlink lastLink = null;
    for (HSLFTextParagraph para : paragraphs) {
        for (HSLFTextRun run : para) {
            HSLFHyperlink thisLink = run.getHyperlink();
            if (thisLink != null && thisLink == lastLink) {
                // the hyperlink extends over this text run, increase its length
                // TODO: the text run might be longer than the hyperlink
                thisLink.setEndIndex(thisLink.getEndIndex() + run.getLength());
            } else {
                if (lastLink != null) {
                    InteractiveInfo info = lastLink.getInfo();
                    TxInteractiveInfoAtom txinfo = lastLink.getTextRunInfo();
                    assert (info != null && txinfo != null);
                    _txtbox.appendChildRecord(info);
                    _txtbox.appendChildRecord(txinfo);
                }
            }
            lastLink = thisLink;
        }
    }
    if (lastLink != null) {
        InteractiveInfo info = lastLink.getInfo();
        TxInteractiveInfoAtom txinfo = lastLink.getTextRunInfo();
        assert (info != null && txinfo != null);
        _txtbox.appendChildRecord(info);
        _txtbox.appendChildRecord(txinfo);
    }
}
Also used : RecordContainer(org.apache.poi.hslf.record.RecordContainer) TxInteractiveInfoAtom(org.apache.poi.hslf.record.TxInteractiveInfoAtom) Record(org.apache.poi.hslf.record.Record) InteractiveInfo(org.apache.poi.hslf.record.InteractiveInfo) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom)

Example 8 with TextHeaderAtom

use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.

the class HSLFTextParagraph method refreshRecords.

/**
     * Writes the textbox records back to the document record 
     */
private static void refreshRecords(List<HSLFTextParagraph> paragraphs) {
    TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
    RecordContainer _txtbox = headerAtom.getParentRecord();
    if (_txtbox instanceof EscherTextboxWrapper) {
        try {
            ((EscherTextboxWrapper) _txtbox).writeOut(null);
        } catch (IOException e) {
            throw new HSLFException("failed dummy write", e);
        }
    }
}
Also used : HSLFException(org.apache.poi.hslf.exceptions.HSLFException) RecordContainer(org.apache.poi.hslf.record.RecordContainer) EscherTextboxWrapper(org.apache.poi.hslf.record.EscherTextboxWrapper) IOException(java.io.IOException) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom)

Example 9 with TextHeaderAtom

use of org.apache.poi.hslf.record.TextHeaderAtom in project poi by apache.

the class HSLFTextShape method createEmptyParagraph.

private void createEmptyParagraph() {
    TextHeaderAtom tha = (TextHeaderAtom) _txtbox.findFirstOfType(TextHeaderAtom._type);
    if (tha == null) {
        tha = new TextHeaderAtom();
        tha.setParentRecord(_txtbox);
        _txtbox.appendChildRecord(tha);
    }
    TextBytesAtom tba = (TextBytesAtom) _txtbox.findFirstOfType(TextBytesAtom._type);
    TextCharsAtom tca = (TextCharsAtom) _txtbox.findFirstOfType(TextCharsAtom._type);
    if (tba == null && tca == null) {
        tba = new TextBytesAtom();
        tba.setText(new byte[0]);
        _txtbox.appendChildRecord(tba);
    }
    final String text = ((tba != null) ? tba.getText() : tca.getText());
    StyleTextPropAtom sta = (StyleTextPropAtom) _txtbox.findFirstOfType(StyleTextPropAtom._type);
    TextPropCollection paraStyle = null, charStyle = null;
    if (sta == null) {
        int parSiz = text.length();
        sta = new StyleTextPropAtom(parSiz + 1);
        if (_paragraphs.isEmpty()) {
            paraStyle = sta.addParagraphTextPropCollection(parSiz + 1);
            charStyle = sta.addCharacterTextPropCollection(parSiz + 1);
        } else {
            for (HSLFTextParagraph htp : _paragraphs) {
                int runsLen = 0;
                for (HSLFTextRun htr : htp.getTextRuns()) {
                    runsLen += htr.getLength();
                    charStyle = sta.addCharacterTextPropCollection(htr.getLength());
                    htr.setCharacterStyle(charStyle);
                }
                paraStyle = sta.addParagraphTextPropCollection(runsLen);
                htp.setParagraphStyle(paraStyle);
            }
            assert (paraStyle != null && charStyle != null);
        }
        _txtbox.appendChildRecord(sta);
    } else {
        paraStyle = sta.getParagraphStyles().get(0);
        charStyle = sta.getCharacterStyles().get(0);
    }
    if (_paragraphs.isEmpty()) {
        HSLFTextParagraph htp = new HSLFTextParagraph(tha, tba, tca, _paragraphs);
        htp.setParagraphStyle(paraStyle);
        htp.setParentShape(this);
        _paragraphs.add(htp);
        HSLFTextRun htr = new HSLFTextRun(htp);
        htr.setCharacterStyle(charStyle);
        htr.setText(text);
        htp.addTextRun(htr);
    }
}
Also used : TextCharsAtom(org.apache.poi.hslf.record.TextCharsAtom) TextHeaderAtom(org.apache.poi.hslf.record.TextHeaderAtom) TextBytesAtom(org.apache.poi.hslf.record.TextBytesAtom) StyleTextPropAtom(org.apache.poi.hslf.record.StyleTextPropAtom) TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection)

Aggregations

TextHeaderAtom (org.apache.poi.hslf.record.TextHeaderAtom)9 Record (org.apache.poi.hslf.record.Record)7 DrawPaint (org.apache.poi.sl.draw.DrawPaint)5 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)5 StyleTextPropAtom (org.apache.poi.hslf.record.StyleTextPropAtom)4 TextBytesAtom (org.apache.poi.hslf.record.TextBytesAtom)4 TextCharsAtom (org.apache.poi.hslf.record.TextCharsAtom)4 RecordContainer (org.apache.poi.hslf.record.RecordContainer)3 HSLFException (org.apache.poi.hslf.exceptions.HSLFException)2 TextPropCollection (org.apache.poi.hslf.model.textproperties.TextPropCollection)2 SlideListWithText (org.apache.poi.hslf.record.SlideListWithText)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)1 Document (org.apache.poi.hslf.record.Document)1 EscherTextboxWrapper (org.apache.poi.hslf.record.EscherTextboxWrapper)1 InteractiveInfo (org.apache.poi.hslf.record.InteractiveInfo)1 MasterTextPropAtom (org.apache.poi.hslf.record.MasterTextPropAtom)1