Search in sources :

Example 16 with TextPropCollection

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

the class HSLFTextParagraph method applyParagraphStyles.

protected static void applyParagraphStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> paraStyles) {
    int paraIdx = 0;
    for (TextPropCollection p : paraStyles) {
        for (int ccPara = 0, ccStyle = p.getCharactersCovered(); ccPara < ccStyle; paraIdx++) {
            if (paraIdx >= paragraphs.size()) {
                return;
            }
            HSLFTextParagraph htp = paragraphs.get(paraIdx);
            TextPropCollection pCopy = new TextPropCollection(0, TextPropType.paragraph);
            pCopy.copy(p);
            htp.setParagraphStyle(pCopy);
            int len = 0;
            for (HSLFTextRun trun : htp.getTextRuns()) {
                len += trun.getLength();
            }
            if (paraIdx == paragraphs.size() - 1) {
                len++;
            }
            pCopy.updateTextSize(len);
            ccPara += len;
        }
    }
}
Also used : TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)

Example 17 with TextPropCollection

use of org.apache.poi.hslf.model.textproperties.TextPropCollection 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)

Example 18 with TextPropCollection

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

the class HSLFTextParagraph method appendText.

/**
     * Adds the supplied text onto the end of the TextParagraphs,
     * creating a new RichTextRun for it to sit in.
     *
     * @param text the text string used by this object.
     */
protected static HSLFTextRun appendText(List<HSLFTextParagraph> paragraphs, String text, boolean newParagraph) {
    text = toInternalString(text);
    // check paragraphs
    assert (!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
    HSLFTextParagraph htp = paragraphs.get(paragraphs.size() - 1);
    HSLFTextRun htr = htp.getTextRuns().get(htp.getTextRuns().size() - 1);
    boolean addParagraph = newParagraph;
    for (String rawText : text.split("(?<=\r)")) {
        // special case, if last text paragraph or run is empty, we will reuse it
        boolean lastRunEmpty = (htr.getLength() == 0);
        boolean lastParaEmpty = lastRunEmpty && (htp.getTextRuns().size() == 1);
        if (addParagraph && !lastParaEmpty) {
            TextPropCollection tpc = htp.getParagraphStyle();
            HSLFTextParagraph prevHtp = htp;
            htp = new HSLFTextParagraph(htp._headerAtom, htp._byteAtom, htp._charAtom, paragraphs);
            htp.getParagraphStyle().copy(tpc);
            htp.setParentShape(prevHtp.getParentShape());
            htp.setShapeId(prevHtp.getShapeId());
            htp.supplySheet(prevHtp.getSheet());
            paragraphs.add(htp);
        }
        addParagraph = true;
        if (!lastRunEmpty) {
            TextPropCollection tpc = htr.getCharacterStyle();
            htr = new HSLFTextRun(htp);
            htr.getCharacterStyle().copy(tpc);
            htp.addTextRun(htr);
        }
        htr.setText(rawText);
    }
    storeText(paragraphs);
    return htr;
}
Also used : TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection)

Example 19 with TextPropCollection

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

the class HSLFTextParagraph method applyCharacterStyles.

protected static void applyCharacterStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> charStyles) {
    int paraIdx = 0, runIdx = 0;
    HSLFTextRun trun;
    for (int csIdx = 0; csIdx < charStyles.size(); csIdx++) {
        TextPropCollection p = charStyles.get(csIdx);
        for (int ccRun = 0, ccStyle = p.getCharactersCovered(); ccRun < ccStyle; ) {
            HSLFTextParagraph para = paragraphs.get(paraIdx);
            List<HSLFTextRun> runs = para.getTextRuns();
            trun = runs.get(runIdx);
            final int len = trun.getLength();
            if (ccRun + len <= ccStyle) {
                ccRun += len;
            } else {
                String text = trun.getRawText();
                trun.setText(text.substring(0, ccStyle - ccRun));
                HSLFTextRun nextRun = new HSLFTextRun(para);
                nextRun.setText(text.substring(ccStyle - ccRun));
                runs.add(runIdx + 1, nextRun);
                ccRun += ccStyle - ccRun;
            }
            trun.setCharacterStyle(p);
            if (paraIdx == paragraphs.size() - 1 && runIdx == runs.size() - 1) {
                if (csIdx < charStyles.size() - 1) {
                    // special case, empty trailing text run
                    HSLFTextRun nextRun = new HSLFTextRun(para);
                    nextRun.setText("");
                    runs.add(nextRun);
                    ccRun++;
                } else {
                    // need to add +1 to the last run of the last paragraph
                    trun.getCharacterStyle().updateTextSize(trun.getLength() + 1);
                    ccRun++;
                }
            }
            // need to compare it again, in case a run has been added after
            if (++runIdx == runs.size()) {
                paraIdx++;
                runIdx = 0;
            }
        }
    }
}
Also used : TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)

Example 20 with TextPropCollection

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

the class TextStyleListing method showStyleTextPropAtom.

public static void showStyleTextPropAtom(StyleTextPropAtom stpa) {
    System.out.println("\nFound a StyleTextPropAtom");
    List<TextPropCollection> paragraphStyles = stpa.getParagraphStyles();
    System.out.println("Contains " + paragraphStyles.size() + " paragraph styles:");
    for (int i = 0; i < paragraphStyles.size(); i++) {
        TextPropCollection tpc = paragraphStyles.get(i);
        System.out.println(" In paragraph styling " + i + ":");
        System.out.println("  Characters covered is " + tpc.getCharactersCovered());
        showTextProps(tpc);
    }
    List<TextPropCollection> charStyles = stpa.getCharacterStyles();
    System.out.println("Contains " + charStyles.size() + " character styles:");
    for (int i = 0; i < charStyles.size(); i++) {
        TextPropCollection tpc = charStyles.get(i);
        System.out.println("  In character styling " + i + ":");
        System.out.println("    Characters covered is " + tpc.getCharactersCovered());
        showTextProps(tpc);
    }
}
Also used : TextPropCollection(org.apache.poi.hslf.model.textproperties.TextPropCollection)

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