Search in sources :

Example 61 with AffineTransform

use of java.awt.geom.AffineTransform in project android by JetBrains.

the class FakeImageFactory method getImage.

@Override
public BufferedImage getImage(final int w, final int h) {
    // only in the createGraphics call.
    return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB) {

        @Override
        public Graphics2D createGraphics() {
            myRequestedHeight = h;
            myRequestedWidth = w;
            // TODO: Check if we can stop layoutlib from resetting the transforms.
            final Shape originalClip = myGraphics.getClip();
            final AffineTransform originalTx = ((Graphics2D) myGraphics).getTransform();
            AffineTransform inverse = null;
            if (originalTx != null) {
                try {
                    inverse = originalTx.createInverse();
                } catch (NoninvertibleTransformException e) {
                    LOG.error(e);
                }
            }
            final AffineTransform originalTxInverse = inverse;
            return new Graphics2DDelegate((Graphics2D) myGraphics.create()) {

                @Nullable
                private Shape intersect(@Nullable Shape s1, @Nullable Shape s2) {
                    if (s1 == null || s2 == null) {
                        return s1 == null ? s2 : s1;
                    }
                    Area a1 = new Area(s1);
                    Area a2 = new Area(s2);
                    a1.intersect(a2);
                    return a1;
                }

                @Override
                public void clip(@Nullable Shape s) {
                    if (s == null) {
                        setClip(null);
                        return;
                    }
                    super.clip(s);
                }

                @Override
                public void setClip(@Nullable Shape sh) {
                    try {
                        super.setClip(intersect(getTransform().createInverse().createTransformedShape(originalClip), sh));
                    } catch (NoninvertibleTransformException e) {
                        LOG.error(e);
                    }
                }

                @Override
                public void setTransform(AffineTransform Tx) {
                    if (originalTx != null) {
                        AffineTransform transform = (AffineTransform) originalTx.clone();
                        transform.concatenate(Tx);
                        super.setTransform(transform);
                    } else {
                        super.setTransform(Tx);
                    }
                }

                @Override
                public AffineTransform getTransform() {
                    AffineTransform currentTransform = super.getTransform();
                    if (originalTxInverse != null) {
                        currentTransform.concatenate(originalTxInverse);
                    }
                    return currentTransform;
                }
            };
        }

        @Override
        public int getWidth() {
            return w;
        }

        @Override
        public int getHeight() {
            return h;
        }
    };
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) Area(java.awt.geom.Area) Shape(java.awt.Shape) Graphics2DDelegate(com.intellij.ui.Graphics2DDelegate) AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) Nullable(org.jetbrains.annotations.Nullable) Graphics2D(java.awt.Graphics2D)

Example 62 with AffineTransform

use of java.awt.geom.AffineTransform in project android by JetBrains.

the class GraphicsLayoutRenderer method render.

/**
   * Render the layout to the passed {@link Graphics2D} instance using the defined viewport.
   * <p/>
   * <p/>Please note that this method is not thread safe so, if used from multiple threads, it's the caller's responsibility to synchronize
   * the access to it.
   */
public boolean render(@NotNull final Graphics2D graphics) {
    myRenderSessionLock.readLock().lock();
    try {
        if (!SystemInfo.isMac) {
            // Do not enable anti-aliasing on MAC. It doesn't improve much and causes has performance issues when filling the background using
            // alpha values.
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        }
        myImageFactory.setGraphics(graphics);
        AffineTransform oldTransform = graphics.getTransform();
        if (myScale != 1.0) {
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            AffineTransform scaleTransform = new AffineTransform(oldTransform);
            scaleTransform.scale(myScale, myScale);
            graphics.setTransform(scaleTransform);
        }
        Result result = null;
        try {
            result = RenderService.runRenderAction(new Callable<Result>() {

                @Override
                public Result call() {
                    if (mySecurityManager != null) {
                        mySecurityManager.setActive(true, myCredential);
                    }
                    try {
                        return myRenderSession.render(RenderParams.DEFAULT_TIMEOUT, myInvalidate);
                    } finally {
                        if (mySecurityManager != null) {
                            mySecurityManager.setActive(false, myCredential);
                        }
                    }
                }
            });
        } catch (Exception e) {
            LOG.warn("Exception running render action", e);
        }
        if (myScale != 1.0) {
            graphics.setTransform(oldTransform);
        }
        // access the system properties.
        if (result != null && result.getStatus() != Result.Status.SUCCESS) {
            //noinspection ThrowableResultOfMethodCallIgnored
            if (result.getException() != null) {
                LOG.warn(result.getException());
            } else {
                LOG.warn("Render error (no exception). Status=" + result.getStatus().name());
            }
            return false;
        }
        myInvalidate = false;
        return true;
    } finally {
        myRenderSessionLock.readLock().unlock();
    }
}
Also used : AffineTransform(java.awt.geom.AffineTransform) Callable(java.util.concurrent.Callable) RenderingException(com.android.tools.idea.layoutlib.RenderingException) IOException(java.io.IOException)

Example 63 with AffineTransform

use of java.awt.geom.AffineTransform in project adempiere by adempiere.

the class ImageElement method paint.

/**
	 * 	Paint Image
	 * 	@param g2D Graphics
	 *  @param pageStart top left Location of page
	 *  @param pageNo page number for multi page support (0 = header/footer) - ignored
	 *  @param ctx print context
	 *  @param isView true if online view (IDs are links)
	 */
public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView) {
    if (m_image == null)
        return;
    //	Position
    Point2D.Double location = getAbsoluteLocation(pageStart);
    int x = (int) location.x;
    if (MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight.equals(p_FieldAlignmentType))
        x += p_maxWidth - p_width;
    else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Center.equals(p_FieldAlignmentType))
        x += (p_maxWidth - p_width) / 2;
    int y = (int) location.y;
    // 	map a scaled and shifted version of the image to device space
    AffineTransform transform = new AffineTransform();
    transform.translate(x, y);
    transform.scale(m_scaleFactor, m_scaleFactor);
    g2D.drawImage(m_image, transform, this);
}
Also used : Point2D(java.awt.geom.Point2D) AffineTransform(java.awt.geom.AffineTransform)

Example 64 with AffineTransform

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

the class ShapeRenderer method visit.

@Override
public void visit(XDGFShape shape, AffineTransform globalTransform, int level) {
    AffineTransform savedTr = _graphics.getTransform();
    _graphics.transform(globalTransform);
    drawPath(shape);
    drawText(shape);
    // we're done, undo the transforms
    _graphics.setTransform(savedTr);
}
Also used : AffineTransform(java.awt.geom.AffineTransform)

Example 65 with AffineTransform

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

the class TestXSLFBugs method bug54542.

@Test
@Ignore("Similar to TestFontRendering it doesn't make sense to compare images because of tiny rendering differences in windows/unix")
public void bug54542() throws Exception {
    XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("54542_cropped_bitmap.pptx");
    Dimension pgsize = ss.getPageSize();
    XSLFSlide slide = ss.getSlides().get(0);
    // render it
    double zoom = 1;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);
    BufferedImage imgActual = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D graphics = imgActual.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    graphics.setTransform(at);
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
    slide.draw(graphics);
    ImageIO.write(imgActual, "PNG", new File("bug54542.png"));
    ss.close();
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) Dimension(java.awt.Dimension) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

AffineTransform (java.awt.geom.AffineTransform)370 BufferedImage (java.awt.image.BufferedImage)60 Graphics2D (java.awt.Graphics2D)54 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)42 Rectangle2D (java.awt.geom.Rectangle2D)40 Point2D (java.awt.geom.Point2D)28 Shape (java.awt.Shape)24 Font (java.awt.Font)23 Paint (java.awt.Paint)23 GcSnapshot (com.android.layoutlib.bridge.impl.GcSnapshot)20 ArrayList (java.util.ArrayList)18 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)17 Rectangle (java.awt.Rectangle)16 PathIterator (java.awt.geom.PathIterator)16 Color (java.awt.Color)15 Point (java.awt.Point)15 FontRenderContext (java.awt.font.FontRenderContext)15 Area (java.awt.geom.Area)14 GeneralPath (java.awt.geom.GeneralPath)14 AffineTransformOp (java.awt.image.AffineTransformOp)13