From 6fc86a336ca80ed240cb2e7df853a311778fdd3b Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Mon, 20 Oct 2008 13:20:26 +0000 Subject: [PATCH] Fixed size of images when vector graphic images are painted using fox:conversion-mode="bitmap" (for PDF output). Added support for fox:conversion-mode="bitmap" for PS output. Avoid "unsupported target ImageFlavor" error after the fix in XML Graphics Commons (rev 706270). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@706276 13f79535-47bb-0310-9956-ffa450edef68 --- .../image/loader/batik/ImageLoaderSVG.java | 5 +- .../fop/render/AbstractGenericSVGHandler.java | 6 ++ .../fop/render/pdf/PDFGraphics2DAdapter.java | 4 +- .../fop/render/ps/PSGraphics2DAdapter.java | 37 ++++++++++-- .../org/apache/fop/render/ps/PSRenderer.java | 60 ++++++++++--------- .../apache/fop/render/ps/PSSVGHandler.java | 19 ++++++ 6 files changed, 94 insertions(+), 37 deletions(-) diff --git a/src/java/org/apache/fop/image/loader/batik/ImageLoaderSVG.java b/src/java/org/apache/fop/image/loader/batik/ImageLoaderSVG.java index c5c113b04..bb9c7d395 100644 --- a/src/java/org/apache/fop/image/loader/batik/ImageLoaderSVG.java +++ b/src/java/org/apache/fop/image/loader/batik/ImageLoaderSVG.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.util.Map; import org.apache.batik.dom.svg.SVGDOMImplementation; + import org.apache.xmlgraphics.image.loader.Image; import org.apache.xmlgraphics.image.loader.ImageException; import org.apache.xmlgraphics.image.loader.ImageFlavor; @@ -45,8 +46,8 @@ public class ImageLoaderSVG extends AbstractImageLoader { * @param targetFlavor the target flavor */ public ImageLoaderSVG(ImageFlavor targetFlavor) { - if (!(XMLNamespaceEnabledImageFlavor.SVG_DOM.equals(targetFlavor))) { - throw new IllegalArgumentException("Unsupported target ImageFlavor: " + targetFlavor); + if (!(XMLNamespaceEnabledImageFlavor.SVG_DOM.isCompatible(targetFlavor))) { + throw new IllegalArgumentException("Incompatible target ImageFlavor: " + targetFlavor); } this.targetFlavor = targetFlavor; } diff --git a/src/java/org/apache/fop/render/AbstractGenericSVGHandler.java b/src/java/org/apache/fop/render/AbstractGenericSVGHandler.java index 2d4b20e6a..fb62e653d 100644 --- a/src/java/org/apache/fop/render/AbstractGenericSVGHandler.java +++ b/src/java/org/apache/fop/render/AbstractGenericSVGHandler.java @@ -35,7 +35,9 @@ import org.apache.batik.dom.svg.SVGDOMImplementation; import org.apache.batik.gvt.GraphicsNode; import org.apache.xmlgraphics.java2d.Graphics2DImagePainter; +import org.apache.xmlgraphics.util.QName; +import org.apache.fop.fo.extensions.ExtensionElementMapping; import org.apache.fop.render.RendererContext.RendererContextWrapper; import org.apache.fop.svg.SVGEventProducer; import org.apache.fop.svg.SVGUserAgent; @@ -48,6 +50,10 @@ import org.apache.fop.svg.SVGUserAgent; */ public abstract class AbstractGenericSVGHandler implements XMLHandler, RendererContextConstants { + /** Qualified name for the "conversion-mode" extension attribute. */ + protected static final QName CONVERSION_MODE = new QName( + ExtensionElementMapping.URI, null, "conversion-mode"); + /** {@inheritDoc} */ public void handleXML(RendererContext context, Document doc, String ns) throws Exception { diff --git a/src/java/org/apache/fop/render/pdf/PDFGraphics2DAdapter.java b/src/java/org/apache/fop/render/pdf/PDFGraphics2DAdapter.java index f05a16f1a..2aa11227a 100644 --- a/src/java/org/apache/fop/render/pdf/PDFGraphics2DAdapter.java +++ b/src/java/org/apache/fop/render/pdf/PDFGraphics2DAdapter.java @@ -66,8 +66,8 @@ public class PDFGraphics2DAdapter extends AbstractGraphics2DAdapter { float imw = (float)dim.getWidth() / 1000f; float imh = (float)dim.getHeight() / 1000f; - float sx = fwidth / (float)imw; - float sy = fheight / (float)imh; + float sx = pdfInfo.paintAsBitmap ? 1.0f : (fwidth / (float)imw); + float sy = pdfInfo.paintAsBitmap ? 1.0f : (fheight / (float)imh); renderer.saveGraphicsState(); renderer.setColor(Color.black, false, null); diff --git a/src/java/org/apache/fop/render/ps/PSGraphics2DAdapter.java b/src/java/org/apache/fop/render/ps/PSGraphics2DAdapter.java index 5f204ad70..e5d8200b7 100644 --- a/src/java/org/apache/fop/render/ps/PSGraphics2DAdapter.java +++ b/src/java/org/apache/fop/render/ps/PSGraphics2DAdapter.java @@ -22,20 +22,31 @@ package org.apache.fop.render.ps; import java.awt.Dimension; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.Map; import org.apache.xmlgraphics.java2d.Graphics2DImagePainter; import org.apache.xmlgraphics.java2d.ps.PSGraphics2D; import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.util.QName; +import org.apache.fop.fo.extensions.ExtensionElementMapping; import org.apache.fop.render.AbstractGraphics2DAdapter; import org.apache.fop.render.RendererContext; +import org.apache.fop.render.RendererContextConstants; +import org.apache.fop.render.RendererContext.RendererContextWrapper; +import org.apache.fop.render.pdf.PDFRenderer; /** * Graphics2DAdapter implementation for PostScript. */ public class PSGraphics2DAdapter extends AbstractGraphics2DAdapter { + /** Qualified name for the "conversion-mode" extension attribute. */ + protected static final QName CONVERSION_MODE = new QName( + ExtensionElementMapping.URI, null, "conversion-mode"); + private PSGenerator gen; private boolean clip = true; @@ -71,8 +82,15 @@ public class PSGraphics2DAdapter extends AbstractGraphics2DAdapter { float imw = (float)dim.getWidth() / 1000f; float imh = (float)dim.getHeight() / 1000f; - float sx = fwidth / (float)imw; - float sy = fheight / (float)imh; + boolean paintAsBitmap = false; + if (context != null) { + Map foreign = (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES); + paintAsBitmap = (foreign != null + && "bitmap".equalsIgnoreCase((String)foreign.get(CONVERSION_MODE))); + } + + float sx = paintAsBitmap ? 1.0f : (fwidth / (float)imw); + float sy = paintAsBitmap ? 1.0f : (fheight / (float)imh); gen.commentln("%FOPBeginGraphics2D"); gen.saveGraphicsState(); @@ -95,8 +113,19 @@ public class PSGraphics2DAdapter extends AbstractGraphics2DAdapter { // scale to viewbox transform.translate(fx, fy); gen.getCurrentState().concatMatrix(transform); - Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh); - painter.paint(graphics, area); + if (paintAsBitmap) { + //Fallback solution: Paint to a BufferedImage + int resolution = (int)Math.round(context.getUserAgent().getTargetResolution()); + RendererContextWrapper ctx = RendererContext.wrapRendererContext(context); + BufferedImage bi = paintToBufferedImage(painter, ctx, resolution, false, false); + + float scale = PDFRenderer.NORMAL_PDF_RESOLUTION + / context.getUserAgent().getTargetResolution(); + graphics.drawImage(bi, new AffineTransform(scale, 0, 0, scale, 0, 0), null); + } else { + Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh); + painter.paint(graphics, area); + } gen.restoreGraphicsState(); gen.commentln("%FOPEndGraphics2D"); diff --git a/src/java/org/apache/fop/render/ps/PSRenderer.java b/src/java/org/apache/fop/render/ps/PSRenderer.java index 085ee8230..4ccb0bd52 100644 --- a/src/java/org/apache/fop/render/ps/PSRenderer.java +++ b/src/java/org/apache/fop/render/ps/PSRenderer.java @@ -40,6 +40,36 @@ import javax.xml.transform.Source; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + +import org.apache.xmlgraphics.image.loader.ImageException; +import org.apache.xmlgraphics.image.loader.ImageFlavor; +import org.apache.xmlgraphics.image.loader.ImageInfo; +import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.ImageSessionContext; +import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D; +import org.apache.xmlgraphics.image.loader.impl.ImageRawCCITTFax; +import org.apache.xmlgraphics.image.loader.impl.ImageRawEPS; +import org.apache.xmlgraphics.image.loader.impl.ImageRawJPEG; +import org.apache.xmlgraphics.image.loader.impl.ImageRawStream; +import org.apache.xmlgraphics.image.loader.impl.ImageRendered; +import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM; +import org.apache.xmlgraphics.image.loader.pipeline.ImageProviderPipeline; +import org.apache.xmlgraphics.image.loader.util.ImageUtil; +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.ImageEncoder; +import org.apache.xmlgraphics.ps.PSDictionary; +import org.apache.xmlgraphics.ps.PSDictionaryFormatException; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.PSImageUtils; +import org.apache.xmlgraphics.ps.PSPageDeviceDictionary; +import org.apache.xmlgraphics.ps.PSProcSets; +import org.apache.xmlgraphics.ps.PSResource; +import org.apache.xmlgraphics.ps.PSState; +import org.apache.xmlgraphics.ps.dsc.DSCException; +import org.apache.xmlgraphics.ps.dsc.ResourceTracker; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox; + import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.area.Area; @@ -77,34 +107,6 @@ import org.apache.fop.render.ps.extensions.PSSetPageDevice; import org.apache.fop.render.ps.extensions.PSSetupCode; import org.apache.fop.util.CharUtilities; import org.apache.fop.util.ColorUtil; -import org.apache.xmlgraphics.image.loader.ImageException; -import org.apache.xmlgraphics.image.loader.ImageFlavor; -import org.apache.xmlgraphics.image.loader.ImageInfo; -import org.apache.xmlgraphics.image.loader.ImageManager; -import org.apache.xmlgraphics.image.loader.ImageSessionContext; -import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D; -import org.apache.xmlgraphics.image.loader.impl.ImageRawCCITTFax; -import org.apache.xmlgraphics.image.loader.impl.ImageRawEPS; -import org.apache.xmlgraphics.image.loader.impl.ImageRawJPEG; -import org.apache.xmlgraphics.image.loader.impl.ImageRawStream; -import org.apache.xmlgraphics.image.loader.impl.ImageRendered; -import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM; -import org.apache.xmlgraphics.image.loader.pipeline.ImageProviderPipeline; -import org.apache.xmlgraphics.image.loader.util.ImageUtil; -import org.apache.xmlgraphics.ps.DSCConstants; -import org.apache.xmlgraphics.ps.ImageEncoder; -import org.apache.xmlgraphics.ps.PSDictionary; -import org.apache.xmlgraphics.ps.PSDictionaryFormatException; -import org.apache.xmlgraphics.ps.PSGenerator; -import org.apache.xmlgraphics.ps.PSImageUtils; -import org.apache.xmlgraphics.ps.PSPageDeviceDictionary; -import org.apache.xmlgraphics.ps.PSProcSets; -import org.apache.xmlgraphics.ps.PSResource; -import org.apache.xmlgraphics.ps.PSState; -import org.apache.xmlgraphics.ps.dsc.DSCException; -import org.apache.xmlgraphics.ps.dsc.ResourceTracker; -import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox; -import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox; /** * Renderer that renders to PostScript. @@ -1610,7 +1612,7 @@ public class PSRenderer extends AbstractPathOrientedRenderer * {@inheritDoc} */ public void renderImage(Image image, Rectangle2D pos) { - drawImage(image.getURL(), pos); + drawImage(image.getURL(), pos, image.getForeignAttributes()); } /** diff --git a/src/java/org/apache/fop/render/ps/PSSVGHandler.java b/src/java/org/apache/fop/render/ps/PSSVGHandler.java index 1d293fa71..d4a478fa4 100644 --- a/src/java/org/apache/fop/render/ps/PSSVGHandler.java +++ b/src/java/org/apache/fop/render/ps/PSSVGHandler.java @@ -22,6 +22,7 @@ package org.apache.fop.render.ps; // Java import java.awt.geom.AffineTransform; import java.io.IOException; +import java.util.Map; import org.w3c.dom.Document; @@ -39,6 +40,7 @@ import org.apache.fop.fonts.FontInfo; import org.apache.fop.render.AbstractGenericSVGHandler; import org.apache.fop.render.Renderer; import org.apache.fop.render.RendererContext; +import org.apache.fop.render.RendererContextConstants; import org.apache.fop.svg.SVGEventProducer; import org.apache.fop.svg.SVGUserAgent; @@ -226,6 +228,23 @@ public class PSSVGHandler extends AbstractGenericSVGHandler int yOffset = psInfo.currentYPosition; PSGenerator gen = psInfo.psGenerator; + boolean paintAsBitmap = false; + if (context != null) { + Map foreign = (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES); + paintAsBitmap = (foreign != null + && "bitmap".equalsIgnoreCase((String)foreign.get(CONVERSION_MODE))); + } + if (paintAsBitmap) { + try { + super.renderSVGDocument(context, doc); + } catch (IOException ioe) { + SVGEventProducer eventProducer = SVGEventProducer.Provider.get( + context.getUserAgent().getEventBroadcaster()); + eventProducer.svgRenderingError(this, ioe, getDocumentURI(doc)); + } + return; + } + //Controls whether text painted by Batik is generated using text or path operations boolean strokeText = false; Configuration cfg = psInfo.getHandlerConfiguration(); -- 2.39.5