From 6fc3de15475d124f8d9905952903db141c5a6f68 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Sat, 8 Nov 2003 14:01:24 +0000 Subject: [PATCH] RTF: added support for fo:external-graphic Submitted By: Peter Herweg git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196994 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/fo/flow/ExternalGraphic.java | 12 ++++ .../org/apache/fop/render/rtf/RTFHandler.java | 64 +++++++++++++++++++ .../rtf/rtflib/rtfdoc/RtfExternalGraphic.java | 12 ++-- .../render/rtf/rtflib/rtfdoc/RtfTextrun.java | 5 ++ .../render/rtf/rtflib/tools/ImageUtil.java | 4 ++ 5 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/fop/fo/flow/ExternalGraphic.java b/src/java/org/apache/fop/fo/flow/ExternalGraphic.java index 83d603b78..0e78676c0 100644 --- a/src/java/org/apache/fop/fo/flow/ExternalGraphic.java +++ b/src/java/org/apache/fop/fo/flow/ExternalGraphic.java @@ -50,7 +50,11 @@ */ package org.apache.fop.fo.flow; +// XML +import org.xml.sax.Attributes; + // FOP +import org.apache.fop.apps.FOPException; import org.apache.fop.fo.FONode; import org.apache.fop.fo.FObj; import org.apache.fop.fo.FOTreeVisitor; @@ -267,4 +271,12 @@ public class ExternalGraphic extends FObj { return placement; } + /** + * @see org.apache.fop.fo.FObj#handleAttrs + */ + public void handleAttrs(Attributes attlist) throws FOPException { + super.handleAttrs(attlist); + + getFOTreeControl().getFOInputHandler().image(this); + } } diff --git a/src/java/org/apache/fop/render/rtf/RTFHandler.java b/src/java/org/apache/fop/render/rtf/RTFHandler.java index 85c07e96a..ef2df1947 100644 --- a/src/java/org/apache/fop/render/rtf/RTFHandler.java +++ b/src/java/org/apache/fop/render/rtf/RTFHandler.java @@ -59,7 +59,9 @@ import org.apache.avalon.framework.logger.ConsoleLogger; import org.apache.avalon.framework.logger.Logger; import org.apache.fop.apps.FOPException; import org.apache.fop.datatypes.ColorType; +import org.apache.fop.fo.EnumProperty; import org.apache.fop.fo.FOInputHandler; +import org.apache.fop.datatypes.FixedLength; import org.apache.fop.fo.flow.Block; import org.apache.fop.fo.flow.ExternalGraphic; import org.apache.fop.fo.flow.Inline; @@ -78,10 +80,12 @@ import org.apache.fop.fo.pagination.PageSequence; import org.apache.fop.fo.pagination.SimplePageMaster; import org.apache.fop.fo.properties.Constants; import org.apache.fop.fo.Property; +import org.apache.fop.fo.LengthProperty; import org.apache.fop.fo.PropertyList; import org.apache.fop.apps.Document; import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfAfterContainer; import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfBeforeContainer; +import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfExternalGraphicContainer; import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfPageNumberContainer; import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfParagraphContainer; import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfTextrunContainer; @@ -91,6 +95,7 @@ import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfBefore; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfColorTable; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfDocumentArea; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfElement; +import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfExternalGraphic; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfFile; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfFontManager; import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfParagraph; @@ -111,6 +116,7 @@ import org.xml.sax.SAXException; * @author Trembicki-Guy, Ed * @author Boris Poudérous * @author Peter Herweg + * @author Andreas Putz */ public class RTFHandler extends FOInputHandler { @@ -693,6 +699,64 @@ public class RTFHandler extends FOInputHandler { * @see org.apache.fop.fo.FOInputHandler#image(ExternalGraphic) */ public void image(ExternalGraphic eg) { + try { + + + final IRtfTextrunContainer c = + (IRtfTextrunContainer)builderContext.getContainer(IRtfTextrunContainer.class, + true, this); + + final RtfExternalGraphic newGraphic = c.getTextrun().newImage(); + + Property p=null; + + //get source file + if((p=eg.properties.get("src"))!=null) { + newGraphic.setURL (p.getString()); + } else { + log.error("The attribute 'src' of is required."); + return; + } + + //get scaling + if((p=eg.properties.get("scaling"))!=null) { + EnumProperty e=(EnumProperty)p; + if(p.getEnum()==Constants.UNIFORM) { + newGraphic.setScaling ("uniform"); + } + } + + //get width + if((p=eg.properties.get("width"))!=null) { + LengthProperty lengthProp=(LengthProperty)p; + if(lengthProp.getLength() instanceof FixedLength) { + Float f = new Float(lengthProp.getLength().getValue() / 1000f); + String sValue = f.toString() + "pt"; + newGraphic.setWidth(sValue); + } + } + + //get height + if((p=eg.properties.get("height"))!=null) { + LengthProperty lengthProp=(LengthProperty)p; + if(lengthProp.getLength() instanceof FixedLength) { + Float f = new Float(lengthProp.getLength().getValue() / 1000f); + String sValue = f.toString() + "pt"; + newGraphic.setHeight(sValue); + } + } + + //TODO: make this configurable: + // int compression = m_context.m_options.getRtfExternalGraphicCompressionRate (); + int compression = 0; + if (compression != 0) { + if (! newGraphic.setCompressionRate (compression)) { + log.warn("The compression rate " + compression + " is invalid. The value has to be between 1 and 100 %."); + } + } + } catch(Exception e) { + log.error("image: " + e.getMessage()); + } } /** 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 76a6bc663..6af9e4ce9 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 @@ -277,10 +277,10 @@ public class RtfExternalGraphic extends RtfElement { // convert int to = ImageConstants.CONVERT_TO [type - ImageConstants.I_TO_CONVERT_BASIS]; - if (to == ImageConstants.I_JPG) { - ByteArrayOutputStream out = null; +// if (to == ImageConstants.I_JPG) { +// ByteArrayOutputStream out = null; // try { - //convert to jpeg +// //convert to jpeg // out = new ByteArrayOutputStream(); // Encoder jpgEncoder = new Encoder(graphicCompressionRate, out); // jpgEncoder.encodeJPEG(data); @@ -292,11 +292,11 @@ public class RtfExternalGraphic extends RtfElement { // + "not be created (src = '" + url + "'"); // } // finally { - out.close(); +// out.close(); // } - } else { +// } else { type = ImageConstants.I_NOT_SUPPORTED; - } +// } } diff --git a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java index 32910ca1a..7a12202c2 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java @@ -63,6 +63,7 @@ import java.util.List; import java.util.Iterator; import java.io.IOException; import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException; +import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfExternalGraphic; /** Class which contains a linear text run. It has methods to add attributes, text, paragraph breaks.... * @author Peter Herweg, pherweg@web.de @@ -167,6 +168,10 @@ public class RtfTextrun extends RtfContainer { public void addPageNumber(RtfAttributes attr) throws IOException { RtfPageNumber r=new RtfPageNumber(this, writer, attr); } + + public RtfExternalGraphic newImage() throws IOException { + return new RtfExternalGraphic(this, writer); + } /** * Adds a new RtfTextrun to the given container if necessary, and returns it. diff --git a/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageUtil.java b/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageUtil.java index a762083db..dab3338c0 100644 --- a/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageUtil.java +++ b/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageUtil.java @@ -95,6 +95,10 @@ public class ImageUtil { for (int i = 0; i < len; i++) { if (Character.isDigit (s.charAt (i))) { retString += s.charAt (i); + } else { + //for example "600.0pt" has to be exited, + //when the dot is reached. + break; } } -- 2.39.5