Search in sources :

Example 1 with TextSpecInfoAtom

use of org.apache.poi.hslf.record.TextSpecInfoAtom 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)

Aggregations

HSLFException (org.apache.poi.hslf.exceptions.HSLFException)1 TextPropCollection (org.apache.poi.hslf.model.textproperties.TextPropCollection)1 Record (org.apache.poi.hslf.record.Record)1 StyleTextPropAtom (org.apache.poi.hslf.record.StyleTextPropAtom)1 TextHeaderAtom (org.apache.poi.hslf.record.TextHeaderAtom)1 TextSpecInfoAtom (org.apache.poi.hslf.record.TextSpecInfoAtom)1 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1