Search in sources :

Example 1 with TextParagraph

use of org.apache.poi.sl.usermodel.TextParagraph in project poi by apache.

the class DrawTextShape method drawParagraphs.

/**
     * paint the paragraphs starting from top left (x,y)
     *
     * @return  the vertical advance, i.e. the cumulative space occupied by the text
     */
public double drawParagraphs(Graphics2D graphics, double x, double y) {
    DrawFactory fact = DrawFactory.getInstance(graphics);
    double y0 = y;
    //noinspection RedundantCast
    @SuppressWarnings("cast") Iterator<? extends TextParagraph<?, ?, ? extends TextRun>> paragraphs = (Iterator<? extends TextParagraph<?, ?, ? extends TextRun>>) getShape().iterator();
    boolean isFirstLine = true;
    for (int autoNbrIdx = 0; paragraphs.hasNext(); autoNbrIdx++) {
        TextParagraph<?, ?, ? extends TextRun> p = paragraphs.next();
        DrawTextParagraph dp = fact.getDrawable(p);
        BulletStyle bs = p.getBulletStyle();
        if (bs == null || bs.getAutoNumberingScheme() == null) {
            autoNbrIdx = -1;
        } else {
            Integer startAt = bs.getAutoNumberingStartAt();
            if (startAt == null)
                startAt = 1;
            // TODO: handle reset auto number indexes
            if (startAt > autoNbrIdx)
                autoNbrIdx = startAt;
        }
        dp.setAutoNumberingIdx(autoNbrIdx);
        dp.breakText(graphics);
        if (!isFirstLine) {
            // the amount of vertical white space before the paragraph
            Double spaceBefore = p.getSpaceBefore();
            if (spaceBefore == null)
                spaceBefore = 0d;
            if (spaceBefore > 0) {
                // positive value means percentage spacing of the height of the first line, e.g.
                // the higher the first line, the bigger the space before the paragraph
                y += spaceBefore * 0.01 * dp.getFirstLineHeight();
            } else {
                // negative value means the absolute spacing in points
                y += -spaceBefore;
            }
        }
        isFirstLine = false;
        dp.setPosition(x, y);
        dp.draw(graphics);
        y += dp.getY();
        if (paragraphs.hasNext()) {
            Double spaceAfter = p.getSpaceAfter();
            if (spaceAfter == null)
                spaceAfter = 0d;
            if (spaceAfter > 0) {
                // positive value means percentage spacing of the height of the last line, e.g.
                // the higher the last line, the bigger the space after the paragraph
                y += spaceAfter * 0.01 * dp.getLastLineHeight();
            } else {
                // negative value means the absolute spacing in points
                y += -spaceAfter;
            }
        }
    }
    return y - y0;
}
Also used : TextRun(org.apache.poi.sl.usermodel.TextRun) TextParagraph(org.apache.poi.sl.usermodel.TextParagraph) Iterator(java.util.Iterator) BulletStyle(org.apache.poi.sl.usermodel.TextParagraph.BulletStyle)

Aggregations

Iterator (java.util.Iterator)1 TextParagraph (org.apache.poi.sl.usermodel.TextParagraph)1 BulletStyle (org.apache.poi.sl.usermodel.TextParagraph.BulletStyle)1 TextRun (org.apache.poi.sl.usermodel.TextRun)1