From ec2951becef5ffe71542e645627be72c64994ff4 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 22 Jul 2019 21:31:58 +0000 Subject: [PATCH] Bug 60656 - fix image dimensions git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863602 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hemf/record/emf/HemfMisc.java | 30 +-------------- .../poi/hemf/usermodel/HemfPicture.java | 37 ++++++++----------- 2 files changed, 17 insertions(+), 50 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfMisc.java b/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfMisc.java index 4ab50e4a00..36b82946f4 100644 --- a/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfMisc.java +++ b/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfMisc.java @@ -24,9 +24,7 @@ import static org.apache.poi.hemf.record.emf.HemfFill.readXForm; import static org.apache.poi.hemf.record.emf.HemfRecordIterator.HEADER_SIZE; import java.awt.geom.AffineTransform; -import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; -import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; @@ -666,34 +664,8 @@ public class HemfMisc { final AffineTransform tx; switch (modifyWorldTransformMode) { case MWT_LEFTMULTIPLY: - - AffineTransform wsTrans; - final Rectangle2D win = prop.getWindow(); - boolean noSetWindowExYet = win.getWidth() == 1 && win.getHeight() == 1; - if (noSetWindowExYet) { - // TODO: understand world-space transformation [MSDN-WRLDPGSPC] - // experimental and horrible solved, because the world-space transformation behind it - // is not understood :( - // only found one example which had landscape bounds and transform of 90 degress - - try { - wsTrans = xForm.createInverse(); - } catch (NoninvertibleTransformException e) { - wsTrans = new AffineTransform(); - } - - Rectangle2D emfBounds = header.getBoundsRectangle(); - - if (xForm.getShearX() == -1.0 && xForm.getShearY() == 1.0) { - // rotate 90 deg - wsTrans.translate(-emfBounds.getHeight(), emfBounds.getHeight()); - } - } else { - wsTrans = adaptXForm(xForm, ctx.getTransform()); - } - tx = ctx.getTransform(); - tx.concatenate(wsTrans); + tx.concatenate(adaptXForm(xForm, ctx.getTransform())); break; case MWT_RIGHTMULTIPLY: tx = ctx.getTransform(); diff --git a/src/scratchpad/src/org/apache/poi/hemf/usermodel/HemfPicture.java b/src/scratchpad/src/org/apache/poi/hemf/usermodel/HemfPicture.java index d96f0c24cf..b6cacdbaa9 100644 --- a/src/scratchpad/src/org/apache/poi/hemf/usermodel/HemfPicture.java +++ b/src/scratchpad/src/org/apache/poi/hemf/usermodel/HemfPicture.java @@ -18,6 +18,8 @@ package org.apache.poi.hemf.usermodel; +import static java.lang.Math.abs; + import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.AffineTransform; @@ -31,6 +33,7 @@ import java.util.List; import java.util.Spliterator; import java.util.function.Consumer; +import org.apache.poi.hemf.draw.HemfDrawProperties; import org.apache.poi.hemf.draw.HemfGraphics; import org.apache.poi.hemf.record.emf.HemfHeader; import org.apache.poi.hemf.record.emf.HemfRecord; @@ -96,13 +99,12 @@ public class HemfPicture implements Iterable { } /** - * Returns the bounding box in device-independent units. Usually this is taken from the placeable header. + * Returns the bounding box in device-independent units - usually this is in .01 millimeter units * - * @return the bounding box + * @return the bounding box in device-independent units */ public Rectangle2D getBounds() { - HemfHeader header = (HemfHeader)getRecords().get(0); - Rectangle2D dim = header.getFrameRectangle(); + Rectangle2D dim = getHeader().getFrameRectangle(); double x = dim.getX(), y = dim.getY(); double width = dim.getWidth(), height = dim.getHeight(); if (dim.isEmpty() || Math.rint(width) == 0 || Math.rint(height) == 0) { @@ -131,17 +133,8 @@ public class HemfPicture implements Iterable { * @return the image size in points */ public Dimension2D getSize() { - final Rectangle2D bounds = getBounds(); - - if (bounds.isEmpty()) { - return new Dimension2DDouble(100,100); - } - - final double coeff = (double) Units.EMU_PER_CENTIMETER / Units.EMU_PER_POINT / 10.; - double width = Math.abs(bounds.getWidth()*coeff); - double height = Math.abs(bounds.getHeight()*coeff); - - return new Dimension2DDouble(width, height); + final Rectangle2D b = getHeader().getBoundsRectangle(); + return Units.pixelToPoints(new Dimension2DDouble(abs(b.getWidth()), abs(b.getHeight()))); } private static double minX(Rectangle2D bounds) { @@ -153,20 +146,22 @@ public class HemfPicture implements Iterable { } public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) { - final HemfHeader header = (HemfHeader)getRecords().get(0); - final Shape clip = ctx.getClip(); final AffineTransform at = ctx.getTransform(); try { - Rectangle2D emfBounds = header.getBoundsRectangle(); + Rectangle2D emfBounds = getHeader().getBoundsRectangle(); // scale output bounds to image bounds - ctx.translate(minX(graphicsBounds), minY(graphicsBounds)); + ctx.translate(graphicsBounds.getCenterX(), graphicsBounds.getCenterY()); ctx.scale(graphicsBounds.getWidth()/emfBounds.getWidth(), graphicsBounds.getHeight()/emfBounds.getHeight()); - ctx.translate(-minX(emfBounds), -minY(emfBounds)); + ctx.translate(-emfBounds.getCenterX(), -emfBounds.getCenterY()); - int idx = 0; HemfGraphics g = new HemfGraphics(ctx, emfBounds); + HemfDrawProperties prop = g.getProperties(); + prop.setViewportOrg(emfBounds.getX(), emfBounds.getY()); + prop.setViewportExt(emfBounds.getWidth(), emfBounds.getHeight()); + + int idx = 0; for (HemfRecord r : getRecords()) { try { g.draw(r); -- 2.39.5