From 01945b97d64b065915c7d3ab75d5c51ffd5a8778 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Tue, 8 Nov 2005 12:56:25 +0000 Subject: [PATCH] EMF support review: Copyright years, rounding problems, code simplifications PDF Renderer should not fail with an exception if it receives an EMF image. It should ignore it. Test case for EMF image and an EMF image added. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@331805 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/image/EmfImage.java | 25 +++------ .../apache/fop/image/analyser/EMFReader.java | 16 +++--- .../apache/fop/render/pdf/PDFRenderer.java | 3 ++ .../rtf/rtflib/rtfdoc/RtfExternalGraphic.java | 4 +- .../testcases/external-graphic_emf.xml | 48 ++++++++++++++++++ test/resources/images/img.emf | Bin 0 -> 604 bytes 6 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 test/layoutengine/testcases/external-graphic_emf.xml create mode 100644 test/resources/images/img.emf diff --git a/src/java/org/apache/fop/image/EmfImage.java b/src/java/org/apache/fop/image/EmfImage.java index bbe3794c5..0954e97d7 100644 --- a/src/java/org/apache/fop/image/EmfImage.java +++ b/src/java/org/apache/fop/image/EmfImage.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2005 The Apache Software Foundation. + * Copyright 2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,23 +14,22 @@ * limitations under the License. */ +/* $Id$ */ package org.apache.fop.image; -// Java -import java.io.ByteArrayOutputStream; - import org.apache.commons.io.IOUtils; /** * Enhanced metafile image. - * This supports loading a emf image. + * This supports loading a EMF image. * * @author Peter Herweg * @see AbstractFopImage * @see FopImage */ public class EmfImage extends AbstractFopImage { + /** * Create a bitmap image with the image data. * @@ -41,31 +40,23 @@ public class EmfImage extends AbstractFopImage { } /** - * Load the original jpeg data. - * This loads the original jpeg data and reads the color space, + * Load the original EMF data. + * This loads the original EMF data and reads the color space, * and icc profile if any. * * @return true if loaded false for any error */ protected boolean loadOriginalData() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - byte[] readBuf = new byte[4096]; - int bytesRead; - while ((bytesRead = inputStream.read(readBuf)) != -1) { - baos.write(readBuf, 0, bytesRead); - } + this.raw = IOUtils.toByteArray(inputStream); } catch (java.io.IOException ex) { - log.error("Error while loading image (Emf): " + ex.getMessage(), ex); + log.error("Error while loading image (EMF): " + ex.getMessage(), ex); return false; } finally { IOUtils.closeQuietly(inputStream); inputStream = null; } - this.raw = baos.toByteArray(); - return true; } } diff --git a/src/java/org/apache/fop/image/analyser/EMFReader.java b/src/java/org/apache/fop/image/analyser/EMFReader.java index a5c01845f..404c65945 100644 --- a/src/java/org/apache/fop/image/analyser/EMFReader.java +++ b/src/java/org/apache/fop/image/analyser/EMFReader.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2005 The Apache Software Foundation. + * Copyright 2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,6 +14,8 @@ * limitations under the License. */ +/* $Id$ */ + package org.apache.fop.image.analyser; // Java @@ -53,8 +55,8 @@ public class EMFReader implements ImageReader { public FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(bis); - boolean supported = - ( (header[SIGNATURE_OFFSET + 0] == (byte) 0x20) + boolean supported + = ( (header[SIGNATURE_OFFSET + 0] == (byte) 0x20) && (header[SIGNATURE_OFFSET + 1] == (byte) 0x45) && (header[SIGNATURE_OFFSET + 2] == (byte) 0x4D) && (header[SIGNATURE_OFFSET + 3] == (byte) 0x46) ); @@ -114,8 +116,8 @@ public class EMFReader implements ImageReader { byte4 = header[VRES_PIXEL_OFFSET + 3] & 0xff; long vresPixel = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); - info.dpiHorizontal = hresPixel / (hresMM/25.4); - info.dpiVertical = vresPixel / (vresMM/25.4);; + info.dpiHorizontal = hresPixel / (hresMM / 25.4f); + info.dpiVertical = vresPixel / (vresMM / 25.4f); //width byte1 = header[WIDTH_OFFSET] & 0xff; @@ -124,7 +126,7 @@ public class EMFReader implements ImageReader { byte4 = header[WIDTH_OFFSET + 3] & 0xff; value = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); - value = (long) (value / 100f / 25.4 * info.dpiHorizontal); + value = Math.round(value / 100f / 25.4f * info.dpiHorizontal); info.width = (int) (value & 0xffffffff); //height @@ -133,7 +135,7 @@ public class EMFReader implements ImageReader { byte3 = header[HEIGHT_OFFSET + 2] & 0xff; byte4 = header[HEIGHT_OFFSET + 3] & 0xff; value = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); - value = (long) (value / 100f / 25.4 * info.dpiVertical); + value = Math.round(value / 100f / 25.4f * info.dpiVertical); info.height = (int) (value & 0xffffffff); return info; diff --git a/src/java/org/apache/fop/render/pdf/PDFRenderer.java b/src/java/org/apache/fop/render/pdf/PDFRenderer.java index d9591802e..f30b0e0d0 100644 --- a/src/java/org/apache/fop/render/pdf/PDFRenderer.java +++ b/src/java/org/apache/fop/render/pdf/PDFRenderer.java @@ -1394,6 +1394,9 @@ public class PDFRenderer extends AbstractPathOrientedRenderer { placeImage((float) pos.getX() / 1000, (float) pos.getY() / 1000, w, h, xobj); } else { + if (!fopimage.load(FopImage.BITMAP)) { + return; + } FopPDFImage pdfimage = new FopPDFImage(fopimage, url); int xobj = pdfDoc.addImage(currentContext, pdfimage).getXNumber(); fact.releaseImage(url, userAgent); diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java index 5f0bb1e3d..a0d8ebbae 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -280,7 +280,7 @@ public class RtfExternalGraphic extends RtfElement { private byte[] imagedata = null; /** The image format */ - FormatBase imageformat; + private FormatBase imageformat; ////////////////////////////////////////////////// // @@ Construction diff --git a/test/layoutengine/testcases/external-graphic_emf.xml b/test/layoutengine/testcases/external-graphic_emf.xml new file mode 100644 index 000000000..6cf940530 --- /dev/null +++ b/test/layoutengine/testcases/external-graphic_emf.xml @@ -0,0 +1,48 @@ + + + + + +

+ This test checks external-graphics. +

+
+ + + + + + + + + + EMF external-graphic + + EOG + + EOF + + + + + + + + + + +
diff --git a/test/resources/images/img.emf b/test/resources/images/img.emf new file mode 100644 index 0000000000000000000000000000000000000000..f8534daa5e595fd4923f8519143b9a10c1ce7971 GIT binary patch literal 604 zcmb7=%?<%k423&L`~(pZu^>U>XKigKY=$?m^dR2QBaQEHk4(bCH2LPVz3si7W)X1% zTaF`ak7k6JA1|kfEUvYP8HaTjBt4^Ilt}59bodTRiFS(1Jza7=7=ru^7SQ^kdc*Y_ z{0XRDR=g!vsP9?hPl3_D%yl}`Kkbvs-z6F;=h&(m<9nFnz9sLs|C!IdtohTcd8*Fs g-5=-`np-)?R@X_Fpjit{vG-av-=|movN6bi0uN~&VgLXD literal 0 HcmV?d00001 -- 2.39.5