Search in sources :

Example 66 with Rectangle2D

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

the class HwmfPicture method draw.

public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
    AffineTransform at = ctx.getTransform();
    try {
        Rectangle2D wmfBounds = getBounds();
        // scale output bounds to image bounds
        ctx.translate(graphicsBounds.getX(), graphicsBounds.getY());
        ctx.scale(graphicsBounds.getWidth() / wmfBounds.getWidth(), graphicsBounds.getHeight() / wmfBounds.getHeight());
        HwmfGraphics g = new HwmfGraphics(ctx, wmfBounds);
        for (HwmfRecord r : records) {
            r.draw(g);
        }
    } finally {
        ctx.setTransform(at);
    }
}
Also used : HwmfRecord(org.apache.poi.hwmf.record.HwmfRecord) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) HwmfGraphics(org.apache.poi.hwmf.draw.HwmfGraphics)

Example 67 with Rectangle2D

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

the class HwmfGraphics method drawString.

public void drawString(byte[] text, Rectangle2D bounds, int[] dx) {
    HwmfFont font = prop.getFont();
    if (font == null || text == null || text.length == 0) {
        return;
    }
    double fontH = getFontHeight(font);
    // TODO: another approx. ...
    double fontW = fontH / 1.8;
    int len = text.length;
    Charset charset = (font.getCharSet().getCharset() == null) ? DEFAULT_CHARSET : font.getCharSet().getCharset();
    String textString = new String(text, charset);
    AttributedString as = new AttributedString(textString);
    if (dx == null || dx.length == 0) {
        addAttributes(as, font);
    } else {
        int[] dxNormed = dx;
        //dxNormed[1] = 14 textString.get(1) = U+30ED
        if (textString.length() != text.length) {
            int codePoints = textString.codePointCount(0, textString.length());
            dxNormed = new int[codePoints];
            int dxPosition = 0;
            for (int offset = 0; offset < textString.length(); ) {
                dxNormed[offset] = dx[dxPosition];
                int[] chars = new int[1];
                int cp = textString.codePointAt(offset);
                chars[0] = cp;
                //now figure out how many bytes it takes to encode that
                //code point in the charset
                int byteLength = new String(chars, 0, chars.length).getBytes(charset).length;
                dxPosition += byteLength;
                offset += Character.charCount(cp);
            }
        }
        for (int i = 0; i < dxNormed.length; i++) {
            addAttributes(as, font);
            // therefore we need to add the additional/suffix width to the next char
            if (i < dxNormed.length - 1) {
                as.addAttribute(TextAttribute.TRACKING, (dxNormed[i] - fontW) / fontH, i + 1, i + 2);
            }
        }
    }
    double angle = Math.toRadians(-font.getEscapement() / 10.);
    final AffineTransform at = graphicsCtx.getTransform();
    try {
        graphicsCtx.translate(bounds.getX(), bounds.getY() + fontH);
        graphicsCtx.rotate(angle);
        if (prop.getBkMode() == HwmfBkMode.OPAQUE) {
            // TODO: validate bounds
            graphicsCtx.setBackground(prop.getBackgroundColor().getColor());
            graphicsCtx.fill(new Rectangle2D.Double(0, 0, bounds.getWidth(), bounds.getHeight()));
        }
        graphicsCtx.setColor(prop.getTextColor().getColor());
        // (float)bounds.getX(), (float)bounds.getY());
        graphicsCtx.drawString(as.getIterator(), 0, 0);
    } finally {
        graphicsCtx.setTransform(at);
    }
}
Also used : AttributedString(java.text.AttributedString) Rectangle2D(java.awt.geom.Rectangle2D) Charset(java.nio.charset.Charset) AffineTransform(java.awt.geom.AffineTransform) AttributedString(java.text.AttributedString) HwmfFont(org.apache.poi.hwmf.record.HwmfFont) TexturePaint(java.awt.TexturePaint) Paint(java.awt.Paint)

Example 68 with Rectangle2D

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

the class HwmfPicture method draw.

public void draw(Graphics2D ctx) {
    Dimension dim = getSize();
    int width = Units.pointsToPixel(dim.getWidth());
    // keep aspect ratio for height
    int height = Units.pointsToPixel(dim.getHeight());
    Rectangle2D bounds = new Rectangle2D.Double(0, 0, width, height);
    draw(ctx, bounds);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension)

Example 69 with Rectangle2D

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

the class TestDrawPictureShape method testResize.

public void testResize(String file) throws IOException {
    SlideShow<?, ?> ss = openSampleDocument(file);
    Slide<?, ?> slide = ss.getSlides().get(0);
    PictureShape<?, ?> picShape = null;
    for (Shape<?, ?> shape : slide.getShapes()) {
        if (shape instanceof PictureShape) {
            picShape = (PictureShape<?, ?>) shape;
            break;
        }
    }
    assertNotNull(picShape);
    PictureData pd = picShape.getPictureData();
    Dimension dimPd = pd.getImageDimension();
    new DrawPictureShape(picShape).resize();
    Dimension dimShape = new Dimension((int) picShape.getAnchor().getWidth(), (int) picShape.getAnchor().getHeight());
    assertEquals(dimPd, dimShape);
    double newWidth = (dimPd.getWidth() * (100d / dimPd.getHeight()));
    // ... -1 is a rounding error
    Rectangle2D expRect = new Rectangle2D.Double(rbf(50 + 300 - newWidth, picShape), 50, rbf(newWidth, picShape), 100);
    Rectangle2D target = new Rectangle2D.Double(50, 50, 300, 100);
    new DrawPictureShape(picShape).resize(target, RectAlign.BOTTOM_RIGHT);
    Rectangle2D actRect = picShape.getAnchor();
    assertEquals(expRect.getX(), actRect.getX(), .0001);
    assertEquals(expRect.getY(), actRect.getY(), .0001);
    assertEquals(expRect.getWidth(), actRect.getWidth(), .0001);
    assertEquals(expRect.getHeight(), actRect.getHeight(), .0001);
    ss.close();
}
Also used : PictureShape(org.apache.poi.sl.usermodel.PictureShape) Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) PictureData(org.apache.poi.sl.usermodel.PictureData)

Example 70 with Rectangle2D

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

the class TestXSLFTextParagraph method testWrappingWidth.

@Test
public void testWrappingWidth() throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();
    XSLFSlide slide = ppt.createSlide();
    XSLFTextShape sh = slide.createAutoShape();
    sh.setLineColor(Color.black);
    XSLFTextParagraph p = sh.addNewTextParagraph();
    p.addNewTextRun().setText("Paragraph formatting allows for more granular control " + "of text within a shape. Properties here apply to all text " + "residing within the corresponding paragraph.");
    Rectangle2D anchor = new Rectangle2D.Double(50, 50, 300, 200);
    sh.setAnchor(anchor);
    DrawTextParagraphProxy dtp = new DrawTextParagraphProxy(p);
    Double leftInset = sh.getLeftInset();
    Double rightInset = sh.getRightInset();
    assertEquals(7.2, leftInset, 0);
    assertEquals(7.2, rightInset, 0);
    Double leftMargin = p.getLeftMargin();
    assertEquals(0.0, leftMargin, 0);
    Double indent = p.getIndent();
    // default
    assertNull(indent);
    double expectedWidth;
    // Case 1: bullet=false, leftMargin=0, indent=0.
    expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
    // 300 - 7.2 - 7.2 - 0
    assertEquals(285.6, expectedWidth, 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
    // 0.5"
    p.setLeftMargin(36d);
    leftMargin = p.getLeftMargin();
    assertEquals(36.0, leftMargin, 0);
    expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
    // 300 - 7.2 - 7.2 - 36
    assertEquals(249.6, expectedWidth, 1E-5);
    assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
    // increase insets, the wrapping width should get smaller
    sh.setLeftInset(10);
    sh.setRightInset(10);
    leftInset = sh.getLeftInset();
    rightInset = sh.getRightInset();
    assertEquals(10.0, leftInset, 0);
    assertEquals(10.0, rightInset, 0);
    expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
    // 300 - 10 - 10 - 36
    assertEquals(244.0, expectedWidth, 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
    // set a positive indent of a 0.5 inch. This means "First Line" indentation:
    // |<---  indent -->|Here goes first line of the text
    // Here go other lines (second and subsequent)
    // 0.5"
    p.setIndent(36.0);
    indent = p.getIndent();
    assertEquals(36.0, indent, 0);
    expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin - indent;
    // 300 - 10 - 10 - 36 - 6.4
    assertEquals(208.0, expectedWidth, 0);
    // first line is indented
    assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
    // other lines are not indented
    expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
    // 300 - 10 - 10 - 36
    assertEquals(244.0, expectedWidth, 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
    // set a negative indent of a 1 inch. This means "Hanging" indentation:
    // Here goes first line of the text
    // |<---  indent -->|Here go other lines (second and subsequent)
    // 1"
    p.setIndent(-72.0);
    indent = p.getIndent();
    assertEquals(-72.0, indent, 0);
    expectedWidth = anchor.getWidth() - leftInset - rightInset;
    // 300 - 10 - 10 
    assertEquals(280.0, expectedWidth, 0);
    // first line is NOT indented
    assertEquals(expectedWidth, dtp.getWrappingWidth(true, null), 0);
    // other lines are indented by leftMargin (the value of indent is not used)
    expectedWidth = anchor.getWidth() - leftInset - rightInset - leftMargin;
    // 300 - 10 - 10 - 36 
    assertEquals(244.0, expectedWidth, 0);
    assertEquals(expectedWidth, dtp.getWrappingWidth(false, null), 0);
    ppt.close();
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Test(org.junit.Test)

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