Search in sources :

Example 1 with BulletStyle

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

the class DrawTextParagraph method getBullet.

protected DrawTextFragment getBullet(Graphics2D graphics, AttributedCharacterIterator firstLineAttr) {
    BulletStyle bulletStyle = paragraph.getBulletStyle();
    if (bulletStyle == null) {
        return null;
    }
    String buCharacter;
    AutoNumberingScheme ans = bulletStyle.getAutoNumberingScheme();
    if (ans != null) {
        buCharacter = ans.format(autoNbrIdx);
    } else {
        buCharacter = bulletStyle.getBulletCharacter();
    }
    if (buCharacter == null) {
        return null;
    }
    String buFont = bulletStyle.getBulletFont();
    if (buFont == null) {
        buFont = paragraph.getDefaultFontFamily();
    }
    assert (buFont != null);
    PlaceableShape<?, ?> ps = getParagraphShape();
    PaintStyle fgPaintStyle = bulletStyle.getBulletFontColor();
    Paint fgPaint;
    if (fgPaintStyle == null) {
        fgPaint = (Paint) firstLineAttr.getAttribute(TextAttribute.FOREGROUND);
    } else {
        fgPaint = new DrawPaint(ps).getPaint(graphics, fgPaintStyle);
    }
    float fontSize = (Float) firstLineAttr.getAttribute(TextAttribute.SIZE);
    Double buSz = bulletStyle.getBulletFontSize();
    if (buSz == null) {
        buSz = 100d;
    }
    if (buSz > 0) {
        fontSize *= buSz * 0.01;
    } else {
        fontSize = (float) -buSz;
    }
    AttributedString str = new AttributedString(mapFontCharset(buCharacter, buFont));
    str.addAttribute(TextAttribute.FOREGROUND, fgPaint);
    str.addAttribute(TextAttribute.FAMILY, buFont);
    str.addAttribute(TextAttribute.SIZE, fontSize);
    TextLayout layout = new TextLayout(str.getIterator(), graphics.getFontRenderContext());
    DrawFactory fact = DrawFactory.getInstance(graphics);
    return fact.getTextFragment(layout, str);
}
Also used : AttributedString(java.text.AttributedString) Paint(java.awt.Paint) TextLayout(java.awt.font.TextLayout) AttributedString(java.text.AttributedString) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) BulletStyle(org.apache.poi.sl.usermodel.TextParagraph.BulletStyle) AutoNumberingScheme(org.apache.poi.sl.usermodel.AutoNumberingScheme)

Example 2 with BulletStyle

use of org.apache.poi.sl.usermodel.TextParagraph.BulletStyle 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

BulletStyle (org.apache.poi.sl.usermodel.TextParagraph.BulletStyle)2 Paint (java.awt.Paint)1 TextLayout (java.awt.font.TextLayout)1 AttributedString (java.text.AttributedString)1 Iterator (java.util.Iterator)1 AutoNumberingScheme (org.apache.poi.sl.usermodel.AutoNumberingScheme)1 PaintStyle (org.apache.poi.sl.usermodel.PaintStyle)1 TextParagraph (org.apache.poi.sl.usermodel.TextParagraph)1 TextRun (org.apache.poi.sl.usermodel.TextRun)1