Search in sources :

Example 51 with Rectangle2D

use of java.awt.geom.Rectangle2D 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 52 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class DrawSimpleShape method getHeadDecoration.

protected Outline getHeadDecoration(Graphics2D graphics, LineDecoration deco, BasicStroke stroke) {
    if (deco == null || stroke == null) {
        return null;
    }
    DecorationSize headLength = deco.getHeadLength();
    if (headLength == null) {
        headLength = DecorationSize.MEDIUM;
    }
    DecorationSize headWidth = deco.getHeadWidth();
    if (headWidth == null) {
        headWidth = DecorationSize.MEDIUM;
    }
    double lineWidth = Math.max(2.5, stroke.getLineWidth());
    Rectangle2D anchor = getAnchor(graphics, getShape());
    double x1 = anchor.getX(), y1 = anchor.getY();
    double alpha = Math.atan(anchor.getHeight() / anchor.getWidth());
    AffineTransform at = new AffineTransform();
    java.awt.Shape headShape = null;
    Path p = null;
    Rectangle2D bounds;
    final double scaleY = Math.pow(DECO_SIZE_POW, headWidth.ordinal() + 1.);
    final double scaleX = Math.pow(DECO_SIZE_POW, headLength.ordinal() + 1.);
    DecorationShape headShapeEnum = deco.getHeadShape();
    if (headShapeEnum == null) {
        return null;
    }
    switch(headShapeEnum) {
        case OVAL:
            p = new Path();
            headShape = new Ellipse2D.Double(0, 0, lineWidth * scaleX, lineWidth * scaleY);
            bounds = headShape.getBounds2D();
            at.translate(x1 - bounds.getWidth() / 2, y1 - bounds.getHeight() / 2);
            at.rotate(alpha, bounds.getX() + bounds.getWidth() / 2, bounds.getY() + bounds.getHeight() / 2);
            break;
        case STEALTH:
        case ARROW:
            p = new Path(false, true);
            Path2D.Double arrow = new Path2D.Double();
            arrow.moveTo((lineWidth * scaleX), (-lineWidth * scaleY / 2));
            arrow.lineTo(0, 0);
            arrow.lineTo((lineWidth * scaleX), (lineWidth * scaleY / 2));
            headShape = arrow;
            at.translate(x1, y1);
            at.rotate(alpha);
            break;
        case TRIANGLE:
            p = new Path();
            Path2D.Double triangle = new Path2D.Double();
            triangle.moveTo((lineWidth * scaleX), (-lineWidth * scaleY / 2));
            triangle.lineTo(0, 0);
            triangle.lineTo((lineWidth * scaleX), (lineWidth * scaleY / 2));
            triangle.closePath();
            headShape = triangle;
            at.translate(x1, y1);
            at.rotate(alpha);
            break;
        default:
            break;
    }
    if (headShape != null) {
        headShape = at.createTransformedShape(headShape);
    }
    return headShape == null ? null : new Outline(headShape, p);
}
Also used : Path(org.apache.poi.sl.draw.geom.Path) DecorationSize(org.apache.poi.sl.usermodel.LineDecoration.DecorationSize) Path2D(java.awt.geom.Path2D) Rectangle2D(java.awt.geom.Rectangle2D) Outline(org.apache.poi.sl.draw.geom.Outline) Ellipse2D(java.awt.geom.Ellipse2D) AffineTransform(java.awt.geom.AffineTransform) DecorationShape(org.apache.poi.sl.usermodel.LineDecoration.DecorationShape)

Example 53 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class DrawTableShape method draw.

public void draw(Graphics2D graphics) {
    Drawable d = getGroupShape(graphics);
    if (d != null) {
        d.draw(graphics);
        return;
    }
    TableShape<?, ?> ts = getShape();
    DrawPaint drawPaint = DrawFactory.getInstance(graphics).getPaint(ts);
    final int rows = ts.getNumberOfRows();
    final int cols = ts.getNumberOfColumns();
    // draw background boxes
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            TableCell<?, ?> tc = ts.getCell(row, col);
            if (tc == null || tc.isMerged()) {
                continue;
            }
            Paint fillPaint = drawPaint.getPaint(graphics, tc.getFillStyle().getPaint());
            graphics.setPaint(fillPaint);
            Rectangle2D cellAnc = tc.getAnchor();
            graphics.fill(cellAnc);
            for (BorderEdge edge : BorderEdge.values()) {
                StrokeStyle stroke = tc.getBorderStyle(edge);
                if (stroke == null) {
                    continue;
                }
                graphics.setStroke(getStroke(stroke));
                Paint linePaint = drawPaint.getPaint(graphics, stroke.getPaint());
                graphics.setPaint(linePaint);
                double x = cellAnc.getX(), y = cellAnc.getY(), w = cellAnc.getWidth(), h = cellAnc.getHeight();
                Line2D line;
                switch(edge) {
                    default:
                    case bottom:
                        line = new Line2D.Double(x - borderSize, y + h, x + w + borderSize, y + h);
                        break;
                    case left:
                        line = new Line2D.Double(x, y, x, y + h + borderSize);
                        break;
                    case right:
                        line = new Line2D.Double(x + w, y, x + w, y + h + borderSize);
                        break;
                    case top:
                        line = new Line2D.Double(x - borderSize, y, x + w + borderSize, y);
                        break;
                }
                graphics.draw(line);
            }
        }
    }
    // draw text
    drawContent(graphics);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) BorderEdge(org.apache.poi.sl.usermodel.TableCell.BorderEdge) Paint(java.awt.Paint) StrokeStyle(org.apache.poi.sl.usermodel.StrokeStyle) Line2D(java.awt.geom.Line2D) Paint(java.awt.Paint)

Example 54 with Rectangle2D

use of java.awt.geom.Rectangle2D 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 55 with Rectangle2D

use of java.awt.geom.Rectangle2D in project poi by apache.

the class DrawTextParagraph method draw.

@Override
public void draw(Graphics2D graphics) {
    if (lines.isEmpty()) {
        return;
    }
    double penY = y;
    boolean firstLine = true;
    int indentLevel = paragraph.getIndentLevel();
    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);
    }
    Double indent = paragraph.getIndent();
    if (indent == null) {
        indent = Units.toPoints(347663L * indentLevel);
    }
    if (isHSLF()) {
        // special handling for HSLF
        indent -= leftMargin;
    }
    //        Double rightMargin = paragraph.getRightMargin();
    //        if (rightMargin == null) {
    //            rightMargin = 0d;
    //        }
    //The vertical line spacing
    Double spacing = paragraph.getLineSpacing();
    if (spacing == null) {
        spacing = 100d;
    }
    for (DrawTextFragment line : lines) {
        double penX;
        if (firstLine) {
            if (!isEmptyParagraph()) {
                // TODO: find out character style for empty, but bulleted/numbered lines
                bullet = getBullet(graphics, line.getAttributedString().getIterator());
            }
            if (bullet != null) {
                bullet.setPosition(x + leftMargin + indent, penY);
                bullet.draw(graphics);
                // don't let text overlay the bullet and advance by the bullet width
                double bulletWidth = bullet.getLayout().getAdvance() + 1;
                penX = x + Math.max(leftMargin, leftMargin + indent + bulletWidth);
            } else {
                penX = x + leftMargin;
            }
        } else {
            penX = x + leftMargin;
        }
        Rectangle2D anchor = DrawShape.getAnchor(graphics, paragraph.getParentShape());
        // Insets are already applied on DrawTextShape.drawContent
        // but (outer) anchor need to be adjusted
        Insets2D insets = paragraph.getParentShape().getInsets();
        double leftInset = insets.left;
        double rightInset = insets.right;
        TextAlign ta = paragraph.getTextAlign();
        if (ta == null) {
            ta = TextAlign.LEFT;
        }
        switch(ta) {
            case CENTER:
                penX += (anchor.getWidth() - line.getWidth() - leftInset - rightInset - leftMargin) / 2;
                break;
            case RIGHT:
                penX += (anchor.getWidth() - line.getWidth() - leftInset - rightInset);
                break;
            default:
                break;
        }
        line.setPosition(penX, penY);
        line.draw(graphics);
        if (spacing > 0) {
            // If linespacing >= 0, then linespacing is a percentage of normal line height.
            penY += spacing * 0.01 * line.getHeight();
        } else {
            // negative value means absolute spacing in points
            penY += -spacing;
        }
        firstLine = false;
    }
    y = penY - y;
}
Also used : TextAlign(org.apache.poi.sl.usermodel.TextParagraph.TextAlign) Rectangle2D(java.awt.geom.Rectangle2D) Paint(java.awt.Paint) Insets2D(org.apache.poi.sl.usermodel.Insets2D)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)262 Graphics2D (java.awt.Graphics2D)42 AffineTransform (java.awt.geom.AffineTransform)34 Color (java.awt.Color)33 Paint (java.awt.Paint)28 Point2D (java.awt.geom.Point2D)26 BufferedImage (java.awt.image.BufferedImage)23 Font (java.awt.Font)21 FontMetrics (java.awt.FontMetrics)20 Dimension (java.awt.Dimension)19 RoundRectangle2D (java.awt.geom.RoundRectangle2D)18 Rectangle (java.awt.Rectangle)16 FontRenderContext (java.awt.font.FontRenderContext)15 Point (java.awt.Point)11 BasicStroke (java.awt.BasicStroke)10 ArrayList (java.util.ArrayList)10 GradientPaint (java.awt.GradientPaint)9 TextLayout (java.awt.font.TextLayout)9 RadialGradientPaint (java.awt.RadialGradientPaint)8 Shape (java.awt.Shape)7