diff options
author | Jeremias Maerki <jeremias@apache.org> | 2005-12-12 19:28:33 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2005-12-12 19:28:33 +0000 |
commit | 2e5d2f964f2855112a480d0863a529fd295cde9f (patch) | |
tree | bb2441b6a0ca95fdeac33827474d10601a141984 /src/java/org | |
parent | 2c8dfa0ea8e3f6d125399a556bd4c476b9619465 (diff) | |
download | xmlgraphics-fop-2e5d2f964f2855112a480d0863a529fd295cde9f.tar.gz xmlgraphics-fop-2e5d2f964f2855112a480d0863a529fd295cde9f.zip |
Quick fix to get the size of images right in RTF output. It looks like \picwgoalN|\pichgoalN should not be used together with \picscalexN|\pixscaleyN. Some problems remain with uniform scaling and such.
Added support for SVG in external-graphic. SVG images are converted to JPEG through Batik at 300 dpi. Note that instream-foreign-object is not supported, yet. It might be good to reuse code from external-graphic for that.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@356345 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
3 files changed, 95 insertions, 21 deletions
diff --git a/src/java/org/apache/fop/render/rtf/RTFHandler.java b/src/java/org/apache/fop/render/rtf/RTFHandler.java index 601a24864..d317ffe50 100644 --- a/src/java/org/apache/fop/render/rtf/RTFHandler.java +++ b/src/java/org/apache/fop/render/rtf/RTFHandler.java @@ -88,6 +88,7 @@ import org.apache.fop.render.rtf.rtflib.tools.TableContext; import org.apache.fop.fonts.FontSetup; import org.apache.fop.image.FopImage; import org.apache.fop.image.ImageFactory; +import org.apache.fop.image.XMLImage; /** * RTF Handler: generates RTF output using the structure events from @@ -932,24 +933,34 @@ public class RTFHandler extends FOEventHandler { } try { + String url = eg.getURL(); + //set image data + ImageFactory fact = ImageFactory.getInstance(); + FopImage fopimage = fact.getImage(url, eg.getUserAgent()); + fopimage.load(FopImage.ORIGINAL_DATA); + + byte[] rawData; + if ("image/svg+xml".equals(fopimage.getMimeType())) { + rawData = SVGConverter.convertToJPEG((XMLImage)fopimage); + } else { + rawData = fopimage.getRessourceBytes(); + } + if (rawData == null) { + log.warn(FONode.decorateWithContextInfo( + "Image could not be embedded: " + url, eg)); + return; + } final IRtfTextrunContainer c = (IRtfTextrunContainer)builderContext.getContainer( IRtfTextrunContainer.class, true, this); final RtfExternalGraphic newGraphic = c.getTextrun().newImage(); - + //set URL - String url = eg.getURL(); newGraphic.setURL(url); - - //set image data - ImageFactory fact = ImageFactory.getInstance(); - FopImage fopimage = fact.getImage(url, eg.getUserAgent()); - fopimage.load(FopImage.ORIGINAL_DATA); - - newGraphic.setImageData(fopimage.getRessourceBytes()); + newGraphic.setImageData(rawData); //set scaling if (eg.getScaling() == Constants.EN_UNIFORM) { @@ -1002,10 +1013,12 @@ public class RTFHandler extends FOEventHandler { } //set width in rtf - newGraphic.setWidth((long) (contentwidth / 1000f) + "pt"); + //newGraphic.setWidth((long) (contentwidth / 1000f) + "pt"); + newGraphic.setWidth((long) (contentwidth / 50f) + "twips"); //set height in rtf - newGraphic.setHeight((long) (contentheight / 1000f) + "pt"); + //newGraphic.setHeight((long) (contentheight / 1000f) + "pt"); + newGraphic.setHeight((long) (contentheight / 50f) + "twips"); //TODO: make this configurable: // int compression = m_context.m_options.getRtfExternalGraphicCompressionRate (); diff --git a/src/java/org/apache/fop/render/rtf/SVGConverter.java b/src/java/org/apache/fop/render/rtf/SVGConverter.java new file mode 100644 index 000000000..255c85136 --- /dev/null +++ b/src/java/org/apache/fop/render/rtf/SVGConverter.java @@ -0,0 +1,61 @@ +/* + * 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. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.rtf; + +import org.apache.batik.transcoder.TranscoderException; +import org.apache.batik.transcoder.TranscoderInput; +import org.apache.batik.transcoder.TranscoderOutput; +import org.apache.batik.transcoder.image.ImageTranscoder; +import org.apache.batik.transcoder.image.JPEGTranscoder; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.image.XMLImage; + +/** + * Helper class for converting SVG to bitmap images. + */ +public class SVGConverter { + + /** logger instance */ + private static Log log = LogFactory.getLog(SVGConverter.class); + + /** + * Converts a SVG image to a JPEG bitmap. + * @param image the SVG image + * @return a byte array containing the JPEG image + */ + public static byte[] convertToJPEG(XMLImage image) { + JPEGTranscoder transcoder = new JPEGTranscoder(); + transcoder.addTranscodingHint(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, + new Float(25.4f / 300)); //300dpi should be enough for now. + transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(0.9f)); + TranscoderInput input = new TranscoderInput(image.getDocument()); + ByteArrayOutputStream baout = new ByteArrayOutputStream(16384); + TranscoderOutput output = new TranscoderOutput(baout); + try { + transcoder.transcode(input, output); + return baout.toByteArray(); + } catch (TranscoderException e) { + log.error(e); + return null; + } + } + +} 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 a0d8ebbae..ad1ee1885 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 @@ -227,7 +227,7 @@ public class RtfExternalGraphic extends RtfElement { protected URL url = null; /** - * The height of the image + * The height of the image (in pixels) */ protected int height = -1; @@ -237,7 +237,7 @@ public class RtfExternalGraphic extends RtfElement { protected int heightPercent = -1; /** - * The desired height + * The desired height (in twips) */ protected int heightDesired = -1; @@ -247,7 +247,7 @@ public class RtfExternalGraphic extends RtfElement { protected boolean perCentH = false; /** - * The width of the image + * The width of the image (in pixels) */ protected int width = -1; @@ -257,7 +257,7 @@ public class RtfExternalGraphic extends RtfElement { protected int widthPercent = -1; /** - * The desired width + * The desired width (in twips) */ protected int widthDesired = -1; @@ -491,10 +491,10 @@ public class RtfExternalGraphic extends RtfElement { if (perCentW) { writeControlWord("picscalex" + widthDesired); } else { - writeControlWord("picscalex" + widthDesired * 100 / width); + //writeControlWord("picscalex" + widthDesired * 100 / width); + writeControlWord("picwgoal" + widthDesired); } - writeControlWord("picwgoal" + widthDesired); } else if (scaleUniform && heightDesired != -1) { if (perCentH) { writeControlWord("picscalex" + heightDesired); @@ -507,10 +507,10 @@ public class RtfExternalGraphic extends RtfElement { if (perCentH) { writeControlWord("picscaley" + heightDesired); } else { - writeControlWord("picscaley" + heightDesired * 100 / height); + //writeControlWord("picscaley" + heightDesired * 100 / height); + writeControlWord("pichgoal" + heightDesired); } - writeControlWord("pichgoal" + heightDesired); } else if (scaleUniform && widthDesired != -1) { if (perCentW) { writeControlWord("picscaley" + widthDesired); @@ -527,7 +527,7 @@ public class RtfExternalGraphic extends RtfElement { /** * Sets the desired height of the image. * - * @param theHeight The desired image height + * @param theHeight The desired image height (as a string in twips or as a percentage) */ public void setHeight(String theHeight) { this.heightDesired = ImageUtil.getInt(theHeight); @@ -537,7 +537,7 @@ public class RtfExternalGraphic extends RtfElement { /** * Sets the desired width of the image. * - * @param theWidth The desired image width + * @param theWidth The desired image width (as a string in twips or as a percentage) */ public void setWidth(String theWidth) { this.widthDesired = ImageUtil.getInt(theWidth); |