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;
import java.util.List;
import java.util.function.Function;
+import org.apache.poi.hemf.draw.HemfDrawProperties;
import org.apache.poi.hemf.draw.HemfGraphics;
import org.apache.poi.hwmf.draw.HwmfDrawProperties;
import org.apache.poi.hwmf.draw.HwmfGraphics;
public static class EmfModifyWorldTransform implements HemfRecord {
protected final AffineTransform xForm = new AffineTransform();
protected HemfModifyWorldTransformMode modifyWorldTransformMode;
+ protected HemfHeader header;
@Override
public HemfRecordType getEmfRecordType() {
return size + LittleEndianConsts.INT_SIZE;
}
+ @Override
+ public void setHeader(HemfHeader header) {
+ this.header = header;
+ }
+
@Override
public void draw(HemfGraphics ctx) {
if (modifyWorldTransformMode == null) {
return;
}
+ final HemfDrawProperties prop = ctx.getProperties();
+
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(ctx.getTransform());
+ }
+
tx = ctx.getTransform();
- tx.concatenate(adaptXForm(tx));
+ tx.concatenate(wsTrans);
break;
case MWT_RIGHTMULTIPLY:
tx = ctx.getTransform();
Function<Double,Double> nn = (d) -> (d == 0. ? 0. : d);
double yDiff = Math.signum(nn.apply(xForm.getTranslateY())) == Math.signum(nn.apply(other.getTranslateY())) ? 1. : -1.;
double xDiff = Math.signum(nn.apply(xForm.getTranslateX())) == Math.signum(nn.apply(other.getTranslateX())) ? 1. : -1.;
- return new AffineTransform(
+ return new AffineTransform(
xForm.getScaleX() == 0 ? 1. : xForm.getScaleX(),
yDiff * xForm.getShearY(),
xDiff * xForm.getShearX(),
// in case the (first) parsing throws an exception, we can provide the
// records up to that point
isParsed = true;
- new HemfRecordIterator(stream).forEachRemaining(records::add);
+ HemfHeader[] header = new HemfHeader[1];
+ new HemfRecordIterator(stream).forEachRemaining(r -> {
+ if (r instanceof HemfHeader) {
+ header[0] = (HemfHeader) r;
+ }
+ r.setHeader(header[0]);
+ records.add(r);
+ });
}
return records;
}
return new Dimension2DDouble(Math.abs(width*coeff), Math.abs(height*coeff));
}
+ private static double minX(Rectangle2D bounds) {
+ return Math.min(bounds.getMinX(), bounds.getMaxX());
+ }
+
+ private static double minY(Rectangle2D bounds) {
+ return Math.min(bounds.getMinY(), bounds.getMaxY());
+ }
+
public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
HemfHeader header = (HemfHeader)getRecords().get(0);
AffineTransform at = ctx.getTransform();
try {
Rectangle2D emfBounds = header.getBoundsRectangle();
- ctx.translate(graphicsBounds.getCenterX()-emfBounds.getCenterX(), graphicsBounds.getCenterY()-emfBounds.getCenterY());
// scale output bounds to image bounds
- ctx.translate(emfBounds.getCenterX(), emfBounds.getCenterY());
+ ctx.translate(minX(graphicsBounds), minY(graphicsBounds));
ctx.scale(graphicsBounds.getWidth()/emfBounds.getWidth(), graphicsBounds.getHeight()/emfBounds.getHeight());
- ctx.translate(-emfBounds.getCenterX(), -emfBounds.getCenterY());
+ ctx.translate(-minX(emfBounds), -minY(emfBounds));
int idx = 0;
HemfGraphics g = new HemfGraphics(ctx, emfBounds);
for (HemfRecord r : getRecords()) {
- g.draw(r);
+ try {
+ g.draw(r);
+ } catch (RuntimeException ignored) {
+
+ }
idx++;
}
} finally {