Search in sources :

Example 1 with TextDirection

use of org.apache.poi.sl.usermodel.TextShape.TextDirection in project poi by apache.

the class DrawTextShape method drawContent.

@Override
public void drawContent(Graphics2D graphics) {
    DrawFactory.getInstance(graphics).fixFonts(graphics);
    TextShape<?, ?> s = getShape();
    Rectangle2D anchor = DrawShape.getAnchor(graphics, s);
    Insets2D insets = s.getInsets();
    double x = anchor.getX() + insets.left;
    double y = anchor.getY();
    // remember the initial transform
    AffineTransform tx = graphics.getTransform();
    // Transform of text in flipped shapes is special.
    // At this point the flip and rotation transform is already applied
    // (see DrawShape#applyTransform ), but we need to restore it to avoid painting "upside down".
    // See Bugzilla 54210.
    boolean vertFlip = s.getFlipVertical();
    boolean horzFlip = s.getFlipHorizontal();
    ShapeContainer<?, ?> sc = s.getParent();
    while (sc instanceof PlaceableShape) {
        PlaceableShape<?, ?> ps = (PlaceableShape<?, ?>) sc;
        vertFlip ^= ps.getFlipVertical();
        horzFlip ^= ps.getFlipHorizontal();
        sc = ps.getParent();
    }
    // Applying flip second time restores the original not-flipped transform
    if (horzFlip ^ vertFlip) {
        final double ax = anchor.getX();
        final double ay = anchor.getY();
        graphics.translate(ax + anchor.getWidth(), ay);
        graphics.scale(-1, 1);
        graphics.translate(-ax, -ay);
    }
    Double textRot = s.getTextRotation();
    if (textRot != null && textRot != 0) {
        final double cx = anchor.getCenterX();
        final double cy = anchor.getCenterY();
        graphics.translate(cx, cy);
        graphics.rotate(Math.toRadians(textRot));
        graphics.translate(-cx, -cy);
    }
    // first dry-run to calculate the total height of the text
    double textHeight;
    switch(s.getVerticalAlignment()) {
        default:
        case TOP:
            y += insets.top;
            break;
        case BOTTOM:
            textHeight = getTextHeight(graphics);
            y += anchor.getHeight() - textHeight - insets.bottom;
            break;
        case MIDDLE:
            textHeight = getTextHeight(graphics);
            double delta = anchor.getHeight() - textHeight - insets.top - insets.bottom;
            y += insets.top + delta / 2;
            break;
    }
    TextDirection textDir = s.getTextDirection();
    if (textDir == TextDirection.VERTICAL || textDir == TextDirection.VERTICAL_270) {
        final double deg = (textDir == TextDirection.VERTICAL) ? 90 : 270;
        final double cx = anchor.getCenterX();
        final double cy = anchor.getCenterY();
        graphics.translate(cx, cy);
        graphics.rotate(Math.toRadians(deg));
        graphics.translate(-cx, -cy);
        // old top/left edge is now bottom/left or top/right - as we operate on the already
        // rotated drawing context, both verticals can be moved in the same direction
        final double w = anchor.getWidth();
        final double h = anchor.getHeight();
        final double dx = (w - h) / 2d;
        graphics.translate(dx, -dx);
    }
    drawParagraphs(graphics, x, y);
    // restore the transform
    graphics.setTransform(tx);
}
Also used : TextDirection(org.apache.poi.sl.usermodel.TextShape.TextDirection) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) Insets2D(org.apache.poi.sl.usermodel.Insets2D) PlaceableShape(org.apache.poi.sl.usermodel.PlaceableShape)

Example 2 with TextDirection

use of org.apache.poi.sl.usermodel.TextShape.TextDirection in project poi by apache.

the class DrawTextParagraph method getWrappingWidth.

/**
     * Returns wrapping width to break lines in this paragraph
     *
     * @param firstLine whether the first line is breaking
     *
     * @return  wrapping width in points
     */
protected double getWrappingWidth(boolean firstLine, Graphics2D graphics) {
    TextShape<?, ?> ts = paragraph.getParentShape();
    // internal margins for the text box
    Insets2D insets = ts.getInsets();
    double leftInset = insets.left;
    double rightInset = insets.right;
    int indentLevel = paragraph.getIndentLevel();
    if (indentLevel == -1) {
        // default to 0, if indentLevel is not set
        indentLevel = 0;
    }
    Double leftMargin = paragraph.getLeftMargin();
    if (leftMargin == null) {
        // if the marL attribute is omitted, then a value of 347663 is implied
        leftMargin = Units.toPoints(347663L * (indentLevel + 1));
    }
    Double indent = paragraph.getIndent();
    if (indent == null) {
        indent = Units.toPoints(347663L * indentLevel);
    }
    Double rightMargin = paragraph.getRightMargin();
    if (rightMargin == null) {
        rightMargin = 0d;
    }
    Rectangle2D anchor = DrawShape.getAnchor(graphics, ts);
    TextDirection textDir = ts.getTextDirection();
    double width;
    if (!ts.getWordWrap()) {
        Dimension pageDim = ts.getSheet().getSlideShow().getPageSize();
        // if wordWrap == false then we return the advance to the (right) border of the sheet
        switch(textDir) {
            default:
                width = pageDim.getWidth() - anchor.getX();
                break;
            case VERTICAL:
                width = pageDim.getHeight() - anchor.getX();
                break;
            case VERTICAL_270:
                width = anchor.getX();
                break;
        }
    } else {
        switch(textDir) {
            default:
                width = anchor.getWidth() - leftInset - rightInset - leftMargin - rightMargin;
                break;
            case VERTICAL:
            case VERTICAL_270:
                width = anchor.getHeight() - leftInset - rightInset - leftMargin - rightMargin;
                break;
        }
        if (firstLine && !isHSLF()) {
            if (bullet != null) {
                if (indent > 0) {
                    width -= indent;
                }
            } else {
                if (indent > 0) {
                    // first line indentation
                    width -= indent;
                } else if (indent < 0) {
                    // hanging indentation: the first line start at the left margin
                    width += leftMargin;
                }
            }
        }
    }
    return width;
}
Also used : TextDirection(org.apache.poi.sl.usermodel.TextShape.TextDirection) Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) Insets2D(org.apache.poi.sl.usermodel.Insets2D) Paint(java.awt.Paint)

Example 3 with TextDirection

use of org.apache.poi.sl.usermodel.TextShape.TextDirection in project poi by apache.

the class TestTable method testTextDirection.

private void testTextDirection(SlideShow<?, ?> ppt1) throws IOException {
    TextDirection[] tds = { TextDirection.HORIZONTAL, TextDirection.VERTICAL, TextDirection.VERTICAL_270 };
    TableShape<?, ?> tbl1 = ppt1.createSlide().createTable(1, 3);
    tbl1.setAnchor(new Rectangle2D.Double(50, 50, 200, 200));
    int col = 0;
    for (TextDirection td : tds) {
        TableCell<?, ?> c = tbl1.getCell(0, col++);
        if (c != null) {
            c.setTextDirection(td);
            c.setText("bla");
        }
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ppt1.write(bos);
    ppt1.close();
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    SlideShow<?, ?> ppt2 = SlideShowFactory.create(is);
    TableShape<?, ?> tbl2 = (TableShape<?, ?>) ppt2.getSlides().get(0).getShapes().get(0);
    col = 0;
    for (TextDirection td : tds) {
        TableCell<?, ?> c = tbl2.getCell(0, col++);
        assertEquals(td, c.getTextDirection());
    }
    ppt2.close();
}
Also used : TextDirection(org.apache.poi.sl.usermodel.TextShape.TextDirection) TableShape(org.apache.poi.sl.usermodel.TableShape) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Rectangle2D(java.awt.geom.Rectangle2D) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)3 TextDirection (org.apache.poi.sl.usermodel.TextShape.TextDirection)3 Insets2D (org.apache.poi.sl.usermodel.Insets2D)2 Dimension (java.awt.Dimension)1 Paint (java.awt.Paint)1 AffineTransform (java.awt.geom.AffineTransform)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 PlaceableShape (org.apache.poi.sl.usermodel.PlaceableShape)1 TableShape (org.apache.poi.sl.usermodel.TableShape)1