From 2b1ee6f4fb72c96c92b97a56efc263c4fb17c641 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Mon, 23 Jun 2008 12:54:27 +0000 Subject: [PATCH] Use configured source and target resolution for foreign objects in RTF output. Fixed intrinsic size calculation for SVG images when source resolution is not 72 dpi. Fixed calculation of px2mm values in the SVG to G2D converter (used by RTF output). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_95@670551 13f79535-47bb-0310-9956-ffa450edef68 --- .../loader/batik/ImageConverterSVG2G2D.java | 12 ++-- .../fop/image/loader/batik/PreloaderSVG.java | 28 +++++--- .../org/apache/fop/render/rtf/RTFHandler.java | 3 +- status.xml | 4 ++ .../loader/batik/ImageLoaderTestCase.java | 71 +++++++++++++++---- test/resources/images/circles.svg | 8 +++ 6 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 test/resources/images/circles.svg diff --git a/src/java/org/apache/fop/image/loader/batik/ImageConverterSVG2G2D.java b/src/java/org/apache/fop/image/loader/batik/ImageConverterSVG2G2D.java index 81b3b4c07..0c96dc336 100644 --- a/src/java/org/apache/fop/image/loader/batik/ImageConverterSVG2G2D.java +++ b/src/java/org/apache/fop/image/loader/batik/ImageConverterSVG2G2D.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,7 +16,7 @@ */ /* $Id$ */ - + package org.apache.fop.image.loader.batik; import java.awt.Dimension; @@ -48,7 +48,7 @@ import org.apache.fop.svg.SVGUserAgent; * Note: The target flavor is "generic" Java2D. No Batik-specific bridges are hooked into the * conversion process. Specialized renderers may want to provide specialized adapters to profit * from target-format features (for example with PDF or PS). This converter is mainly for formats - * which only support bitmap images or rudimentary Java2D support. + * which only support bitmap images or rudimentary Java2D support. */ public class ImageConverterSVG2G2D extends AbstractImageConverter { @@ -62,10 +62,10 @@ public class ImageConverterSVG2G2D extends AbstractImageConverter { } //Prepare - float pxToMillimeter = (float)UnitConv.mm2in(72); //default: 72dpi + float pxToMillimeter = UnitConv.IN2MM / 72; //default: 72dpi Number ptm = (Number)hints.get(ImageProcessingHints.SOURCE_RESOLUTION); if (ptm != null) { - pxToMillimeter = (float)UnitConv.mm2in(ptm.doubleValue()); + pxToMillimeter = (float)(UnitConv.IN2MM / ptm.doubleValue()); } SVGUserAgent ua = new SVGUserAgent( pxToMillimeter, diff --git a/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java b/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java index e59e06b6b..65c282bb8 100644 --- a/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java +++ b/src/java/org/apache/fop/image/loader/batik/PreloaderSVG.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,7 +16,7 @@ */ /* $Id$ */ - + package org.apache.fop.image.loader.batik; import java.awt.geom.AffineTransform; @@ -45,6 +45,7 @@ import org.apache.xmlgraphics.image.loader.impl.AbstractImagePreloader; import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM; import org.apache.xmlgraphics.image.loader.util.ImageUtil; import org.apache.xmlgraphics.util.MimeConstants; +import org.apache.xmlgraphics.util.UnitConv; import org.apache.fop.svg.SVGUserAgent; import org.apache.fop.util.UnclosableInputStream; @@ -58,8 +59,8 @@ public class PreloaderSVG extends AbstractImagePreloader { private static Log log = LogFactory.getLog(PreloaderSVG.class); private boolean batikAvailable = true; - - /** {@inheritDoc} */ + + /** {@inheritDoc} */ public ImageInfo preloadImage(String uri, Source src, ImageContext context) throws IOException { ImageInfo info = null; @@ -118,10 +119,10 @@ public class PreloaderSVG extends AbstractImagePreloader { in.mark(length + 1); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory( getParserName()); - doc = (SVGDocument) factory.createSVGDocument(src.getSystemId(), in); + doc = factory.createSVGDocument(src.getSystemId(), in); } ImageInfo info = createImageInfo(uri, context, doc); - + return info; } catch (NoClassDefFoundError ncdfe) { if (in != null) { @@ -153,7 +154,7 @@ public class PreloaderSVG extends AbstractImagePreloader { private ImageInfo createImageInfo(String uri, ImageContext context, SVGDocument doc) { Element e = doc.getRootElement(); - float pxUnitToMillimeter = 25.4f / context.getSourceResolution(); + float pxUnitToMillimeter = UnitConv.IN2MM / context.getSourceResolution(); SVGUserAgent userAg = new SVGUserAgent(pxUnitToMillimeter, new AffineTransform()); BridgeContext ctx = new BridgeContext(userAg); @@ -176,9 +177,12 @@ public class PreloaderSVG extends AbstractImagePreloader { float height = UnitProcessor.svgVerticalLengthToUserSpace( s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); + int widthMpt = (int)Math.round(px2mpt(width, context.getSourceResolution())); + int heightMpt = (int)Math.round(px2mpt(height, context.getSourceResolution())); + ImageInfo info = new ImageInfo(uri, MimeConstants.MIME_SVG); ImageSize size = new ImageSize(); - size.setSizeInMillipoints(Math.round(width * 1000), Math.round(height * 1000)); + size.setSizeInMillipoints(widthMpt, heightMpt); //Set the resolution to that of the FOUserAgent size.setResolution(context.getSourceResolution()); size.calcPixelsFromSize(); @@ -190,7 +194,7 @@ public class PreloaderSVG extends AbstractImagePreloader { info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, xmlImage); return info; } - + private boolean isSupportedSource(Source src) { if (src instanceof DOMSource) { DOMSource domSrc = (DOMSource)src; @@ -202,4 +206,8 @@ public class PreloaderSVG extends AbstractImagePreloader { } + private static double px2mpt(double px, double resolution) { + return px * 1000 * UnitConv.IN2PT / resolution; + } + } diff --git a/src/java/org/apache/fop/render/rtf/RTFHandler.java b/src/java/org/apache/fop/render/rtf/RTFHandler.java index d2f2c4192..568fe75aa 100644 --- a/src/java/org/apache/fop/render/rtf/RTFHandler.java +++ b/src/java/org/apache/fop/render/rtf/RTFHandler.java @@ -1154,7 +1154,8 @@ public class RTFHandler extends FOEventHandler { FOUserAgent userAgent = ifo.getUserAgent(); ImageManager manager = userAgent.getFactory().getImageManager(); - Image converted = manager.convertImage(image, FLAVORS); + Map hints = ImageUtil.getDefaultHints(ua.getImageSessionContext()); + Image converted = manager.convertImage(image, FLAVORS, hints); putGraphic(ifo, converted); } catch (Exception e) { diff --git a/status.xml b/status.xml index 66cd2bbf7..6c557c251 100644 --- a/status.xml +++ b/status.xml @@ -60,6 +60,10 @@ --> + + Fixed two bugs concerning resolution handling with SVG images and their + conversion to bitmaps for RTF output. + Fixed potential multi-threading problem concerning the use of DecimalFormat. diff --git a/test/java/org/apache/fop/image/loader/batik/ImageLoaderTestCase.java b/test/java/org/apache/fop/image/loader/batik/ImageLoaderTestCase.java index 9261e6d84..457aa91d4 100644 --- a/test/java/org/apache/fop/image/loader/batik/ImageLoaderTestCase.java +++ b/test/java/org/apache/fop/image/loader/batik/ImageLoaderTestCase.java @@ -40,26 +40,26 @@ import org.apache.fop.apps.FopFactory; public class ImageLoaderTestCase extends TestCase { private static final File DEBUG_TARGET_DIR = null; //new File("D:/"); - + private FopFactory fopFactory; - + public ImageLoaderTestCase(String name) { super(name); fopFactory = FopFactory.newInstance(); fopFactory.setSourceResolution(72); fopFactory.setTargetResolution(300); } - + public void testSVG() throws Exception { String uri = "test/resources/images/img-w-size.svg"; - + FOUserAgent userAgent = fopFactory.newFOUserAgent(); - + ImageManager manager = fopFactory.getImageManager(); ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext()); assertNotNull("ImageInfo must not be null", info); - - Image img = manager.getImage(info, ImageFlavor.XML_DOM, + + Image img = manager.getImage(info, ImageFlavor.XML_DOM, userAgent.getImageSessionContext()); assertNotNull("Image must not be null", img); assertEquals(ImageFlavor.XML_DOM, img.getFlavor()); @@ -69,7 +69,7 @@ public class ImageLoaderTestCase extends TestCase { info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image assertEquals(16000, info.getSize().getWidthMpt()); assertEquals(16000, info.getSize().getHeightMpt()); - + img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE, userAgent.getImageSessionContext()); assertNotNull("Image must not be null", img); @@ -87,16 +87,61 @@ public class ImageLoaderTestCase extends TestCase { assertEquals(16000, info.getSize().getWidthMpt()); assertEquals(16000, info.getSize().getHeightMpt()); } - + + public void testSVGNoViewbox() throws Exception { + String uri = "test/resources/images/circles.svg"; + + FopFactory ff = FopFactory.newInstance(); + ff.setSourceResolution(96); + ff.setTargetResolution(300); + + FOUserAgent userAgent = ff.newFOUserAgent(); + + ImageManager manager = ff.getImageManager(); + ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext()); + assertNotNull("ImageInfo must not be null", info); + + Image img = manager.getImage(info, ImageFlavor.XML_DOM, + userAgent.getImageSessionContext()); + assertNotNull("Image must not be null", img); + assertEquals(ImageFlavor.XML_DOM, img.getFlavor()); + ImageXMLDOM imgDom = (ImageXMLDOM)img; + assertNotNull(imgDom.getDocument()); + assertEquals("http://www.w3.org/2000/svg", imgDom.getRootNamespace()); + info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image + assertEquals(96, info.getSize().getDpiHorizontal(), 0); + assertEquals(340158, info.getSize().getWidthMpt()); + assertEquals(340158, info.getSize().getHeightMpt()); + assertEquals(454, info.getSize().getWidthPx()); + assertEquals(454, info.getSize().getHeightPx()); + + img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE, + userAgent.getImageSessionContext()); + assertNotNull("Image must not be null", img); + assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor()); + ImageRendered imgRed = (ImageRendered)img; + assertNotNull(imgRed.getRenderedImage()); + if (DEBUG_TARGET_DIR != null) { + ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(), + (int)userAgent.getTargetResolution(), + new File(DEBUG_TARGET_DIR, "circles.svg.png")); + } + assertEquals(1418, imgRed.getRenderedImage().getWidth()); + assertEquals(1418, imgRed.getRenderedImage().getHeight()); + info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image + assertEquals(340158, info.getSize().getWidthMpt()); + assertEquals(340158, info.getSize().getHeightMpt()); + } + public void testWMF() throws Exception { String uri = "test/resources/images/testChart.wmf"; - + FOUserAgent userAgent = fopFactory.newFOUserAgent(); - + ImageManager manager = fopFactory.getImageManager(); ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext()); assertNotNull("ImageInfo must not be null", info); - + Image img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE, userAgent.getImageSessionContext()); assertNotNull("Image must not be null", img); @@ -114,5 +159,5 @@ public class ImageLoaderTestCase extends TestCase { assertEquals(792000, info.getSize().getWidthMpt()); assertEquals(612000, info.getSize().getHeightMpt()); } - + } diff --git a/test/resources/images/circles.svg b/test/resources/images/circles.svg new file mode 100644 index 000000000..8b71e8224 --- /dev/null +++ b/test/resources/images/circles.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file -- 2.39.5