]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
renders images and ifo to pdf doc
authorKeiron Liddle <keiron@apache.org>
Fri, 8 Mar 2002 11:00:20 +0000 (11:00 +0000)
committerKeiron Liddle <keiron@apache.org>
Fri, 8 Mar 2002 11:00:20 +0000 (11:00 +0000)
added xml reader for external graphics

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194696 13f79535-47bb-0310-9956-ffa450edef68

13 files changed:
src/org/apache/fop/fo/flow/ExternalGraphic.java
src/org/apache/fop/image/AbstractFopImage.java
src/org/apache/fop/image/FopImage.java
src/org/apache/fop/image/ImageFactory.java
src/org/apache/fop/image/SVGImage.java [deleted file]
src/org/apache/fop/image/XMLImage.java [new file with mode: 0644]
src/org/apache/fop/image/analyser/ImageReaderFactory.java
src/org/apache/fop/image/analyser/SVGReader.java
src/org/apache/fop/image/analyser/XMLReader.java [new file with mode: 0644]
src/org/apache/fop/render/pdf/PDFRenderer.java
src/org/apache/fop/render/pdf/PDFXMLHandler.java [new file with mode: 0644]
src/org/apache/fop/svg/PDFGraphics2D.java
src/org/apache/fop/svg/SVGElementMapping.java

index f3b6e59b0c076a0d7836945d22689d5375035a59..1183afdeab4eb36263c599c0dbd3542ddada9125 100644 (file)
@@ -48,12 +48,14 @@ public class ExternalGraphic extends FObj {
     }
 
     protected InlineArea getInlineArea() {
+        setup();
         if(url == null) {
             return null;
         }
         url = ImageFactory.getURL(url);
         // if we need to load this image to get its size
-        // FopImage fopimage = ImageFactory.getImage(url, userAgent);
+        ImageFactory fact = ImageFactory.getInstance();
+        FopImage fopimage = fact.getImage(url, userAgent);
         // if(fopimage == null) {
         // error
         // }
@@ -65,7 +67,7 @@ public class ExternalGraphic extends FObj {
         return vp;
     }
 
-    public void setup() throws FOPException {
+    public void setup() {
 
         // Common Accessibility Properties
         AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
@@ -102,7 +104,7 @@ public class ExternalGraphic extends FObj {
         // this.properties.get("overflow");
         // this.properties.get("scaling");
         // this.properties.get("scaling-method");
-        // this.properties.get("src");
+        url = this.properties.get("src").getString();
         // this.properties.get("text-align");
         // this.properties.get("width");
     }
index 3fa021eead715faa984c6dffcd18c9a498ed5188..9b3a91d7325df2fe2bd6ad7ba7ed2cd4bbc46916 100644 (file)
@@ -100,7 +100,7 @@ public abstract class AbstractFopImage implements FopImage {
         this.imageInfo = info;
         this.m_width = this.imageInfo.width;
         this.m_height = this.imageInfo.height;
-        loaded = loaded & DIMENSIONS;
+        loaded = loaded | DIMENSIONS;
     }
 
     public String getMimeType() {
@@ -121,12 +121,12 @@ public abstract class AbstractFopImage implements FopImage {
             if(!success) {
                 return false;
             }
-            loaded = loaded & DIMENSIONS;
+            loaded = loaded | DIMENSIONS;
         }
         if(((type & BITMAP) != 0) && ((loaded & BITMAP) == 0)) {
             success = success && loadBitmap(ua);
             if(success) {
-                loaded = loaded & BITMAP;
+                loaded = loaded | BITMAP;
             }
         }
         return success;
index 703616aaee523ed23a711f368217b20165d0e0bf..d7b60a62cbc236b29260a02936664974bcf33188 100644 (file)
@@ -70,6 +70,7 @@ public interface FopImage {
         public int height;
         public Object data;
         public String mimeType;
+        public String str;
     }
 
 }
index e2217ba56057701c739d1e0dd6b5af2850fb272e..6eccd37c27c0dfa996b58354928423710bed1b51 100644 (file)
@@ -186,7 +186,9 @@ public class ImageFactory {
             imgClassName = "org.apache.fop.image.JimiImage";
             // imgClassName = "org.apache.fop.image.JAIImage";
         } else if ("image/svg+xml".equals(imgMimeType)) {
-            imgClassName = "org.apache.fop.image.SVGImage";
+            imgClassName = "org.apache.fop.image.XMLImage";
+        } else if ("text/xml".equals(imgMimeType)) {
+            imgClassName = "org.apache.fop.image.XMLImage";
         }
         if (imgClassName == null) {
             log.error("Unsupported image type (" +
diff --git a/src/org/apache/fop/image/SVGImage.java b/src/org/apache/fop/image/SVGImage.java
deleted file mode 100644 (file)
index 95d7b5d..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
- * For details on use and redistribution please refer to the
- * LICENSE file included with these sources.
- */
-
-package org.apache.fop.image;
-
-// Java
-import java.net.URL;
-import org.w3c.dom.svg.SVGDocument;
-
-// FOP
-import org.apache.fop.apps.Driver;
-import org.apache.fop.image.analyser.ImageReader;
-import org.apache.fop.image.analyser.SVGReader;
-import org.apache.fop.fo.FOUserAgent;
-
-import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
-
-/**
- * @see AbstractFopImage
- * @see FopImage
- */
-public class SVGImage extends AbstractFopImage {
-    SVGDocument doc;
-
-    public SVGImage(URL href, FopImage.ImageInfo imgInfo) {
-        super(href, imgInfo);
-        if(imgInfo.data instanceof SVGDocument) {
-            doc = (SVGDocument)imgInfo.data;
-        }
-    }
-
-    /**
-     * creates a SAX parser, using the value of org.xml.sax.parser
-     * defaulting to org.apache.xerces.parsers.SAXParser
-     *
-     * @return the created SAX parser
-     */
-    public static String getParserName() {
-        String parserClassName = Driver.getParserClassName();
-        return parserClassName;
-    }
-
-    protected boolean loadData(FOUserAgent ua) {
-        try {
-            SAXSVGDocumentFactory factory =
-              new SAXSVGDocumentFactory(SVGImage.getParserName());
-            doc = factory.createDocument(this.m_href.toExternalForm());
-        } catch (Exception e) {
-            ua.getLogger().error("Could not load external SVG: "
-                                   + e.getMessage(), e);
-            return false;
-        }
-        return true;
-    }
-
-    public SVGDocument getSVGDocument() {
-        return doc;
-    }
-
-}
diff --git a/src/org/apache/fop/image/XMLImage.java b/src/org/apache/fop/image/XMLImage.java
new file mode 100644 (file)
index 0000000..a6e47df
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.image;
+
+// Java
+import java.net.URL;
+import org.w3c.dom.Document;
+
+// FOP
+import org.apache.fop.apps.Driver;
+import org.apache.fop.image.analyser.ImageReader;
+import org.apache.fop.image.analyser.SVGReader;
+import org.apache.fop.fo.FOUserAgent;
+
+/**
+ * @see AbstractFopImage
+ * @see FopImage
+ */
+public class XMLImage extends AbstractFopImage {
+    Document doc;
+    String ns = "";
+
+    public XMLImage(URL href, FopImage.ImageInfo imgInfo) {
+        super(href, imgInfo);
+        if(imgInfo.data instanceof Document) {
+            doc = (Document)imgInfo.data;
+        }
+        ns = imgInfo.str;
+    }
+
+    /**
+     * creates a SAX parser, using the value of org.xml.sax.parser
+     * defaulting to org.apache.xerces.parsers.SAXParser
+     *
+     * @return the created SAX parser
+     */
+    public static String getParserName() {
+        String parserClassName = Driver.getParserClassName();
+        return parserClassName;
+    }
+
+    protected boolean loadData(FOUserAgent ua) {
+        return true;
+    }
+
+    public Document getDocument() {
+        return doc;
+    }
+
+    public String getNameSpace() {
+        return ns;
+    }
+}
index 8d45e48a7d43ad2596d9d5048a2a36e04ec785a1..66d6aeb35ee01121ebece811f72ebc7478ba763d 100644 (file)
@@ -30,7 +30,10 @@ public class ImageReaderFactory {
         formats.add(new PNGReader());
         formats.add(new TIFFReader());
         formats.add(new EPSReader());
+        // the xml parser through batik closes the stream when finished
+        // so there is a workaround in the SVGReader
         formats.add(new SVGReader());
+        formats.add(new XMLReader());
     };
 
     // TODO - a way to add other readers
index feecadcb27540b0fc8f587c2a3c26027e92a14ca..8791b3420db62e142fbf33c8f93a7c9edaed7fd1 100644 (file)
@@ -15,7 +15,7 @@ import org.w3c.dom.svg.SVGDocument;
 import org.w3c.dom.svg.SVGSVGElement;
 
 // FOP
-import org.apache.fop.image.SVGImage;
+import org.apache.fop.image.XMLImage;
 
 import org.xml.sax.InputSource;
 import org.xml.sax.XMLReader;
@@ -38,6 +38,7 @@ import org.w3c.dom.DOMImplementation;
 import org.apache.batik.dom.svg.SVGDOMImplementation;
 
 import java.io.File;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.List;
 import java.util.ArrayList;
@@ -94,18 +95,69 @@ public class SVGReader implements ImageReader {
      * the SVGReader class.
      */
     class Loader {
-        private FopImage.ImageInfo getImage(String uri, BufferedInputStream fis,
+        private FopImage.ImageInfo getImage(String uri, InputStream fis,
                                    FOUserAgent ua) {
             // parse document and get the size attributes of the svg element
 
             try {
+                int length = 5;
+                fis.mark(length);
+                byte[] b = new byte[length];
+                fis.read(b);
+                String start = new String(b);
+                fis.reset();
+
+                if(start.equals("<?xml")) {
+                    // we have xml, might be another doc
+                    // so stop batik from closing the stream
+                    final InputStream input = fis;
+                    fis = new InputStream() {
+                        public int read() throws IOException {
+                            return input.read();
+                        }
+
+                        public int read(byte[] b) throws IOException {
+                            return input.read(b);
+                        }
+
+                        public int read(byte[] b, int off, int len) throws IOException {
+                            return input.read(b, off, len);
+                        }
+
+                        public long skip(long n) throws IOException {
+                            return input.skip(n);
+                        }
+
+                        public int available() throws IOException {
+                            return input.available();
+                        }
+
+                        public void mark(int rl) {
+                            input.mark(rl);
+                        }
+
+                        public boolean markSupported() {
+                            return input.markSupported();
+                        }
+
+                        public void reset() throws IOException {
+                            input.reset();
+                        }
+
+                        public void close() throws IOException {
+                        }
+                    };
+                }
+
                 FopImage.ImageInfo info = new FopImage.ImageInfo();
+
                 info.mimeType = getMimeType();
+                info.str = SVGDOMImplementation.SVG_NAMESPACE_URI;
 
-                int length = fis.available();
-                fis.mark(length);
+                length = fis.available();
+                fis.mark(length + 1);
                 SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(
-                                                  SVGImage.getParserName());
+                                                  XMLImage.getParserName());
                 SVGDocument doc = factory.createDocument(uri, fis);
                 info.data = doc;
 
@@ -138,6 +190,9 @@ public class SVGReader implements ImageReader {
 
                 return info;
             } catch (NoClassDefFoundError ncdfe) {
+                try {
+                    fis.reset();
+                } catch (IOException ioe) { }
                 batik = false;
                 //ua.getLogger().error("Batik not in class path", ncdfe);
                 return null;
diff --git a/src/org/apache/fop/image/analyser/XMLReader.java b/src/org/apache/fop/image/analyser/XMLReader.java
new file mode 100644 (file)
index 0000000..ab66be9
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.image.analyser;
+
+// Java
+import java.io.BufferedInputStream;
+import java.io.IOException;
+
+import org.xml.sax.InputSource;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.w3c.dom.*;
+import org.w3c.dom.DOMImplementation;
+
+import java.io.File;
+import java.net.URL;
+import java.util.HashMap;
+
+import org.apache.fop.image.FopImage;
+import org.apache.fop.fo.FOUserAgent;
+import org.apache.fop.image.XMLImage;
+
+/**
+ * ImageReader object for XML document image type.
+ */
+public class XMLReader implements ImageReader {
+    private static HashMap converters = new HashMap();
+
+    public static void setConverter(String ns, Converter conv) {
+        converters.put(ns, conv);
+    }
+
+    public XMLReader() {
+    }
+
+    public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis,
+                                   FOUserAgent ua) throws IOException {
+        return loadImage(uri, fis, ua);
+    }
+
+    public String getMimeType() {
+        return "text/xml";
+    }
+
+    /**
+     * This means the external svg document will be loaded twice.
+     * Possibly need a slightly different design for the image stuff.
+     */
+    protected FopImage.ImageInfo loadImage(String uri, BufferedInputStream fis,
+                                   FOUserAgent ua) {
+        return createDocument(fis, ua);
+    }
+
+    public FopImage.ImageInfo createDocument(BufferedInputStream is, FOUserAgent ua) {
+        Document doc = null;
+        FopImage.ImageInfo info = new FopImage.ImageInfo();
+        info.mimeType = getMimeType();
+
+        try {
+            int length = is.available();
+            is.mark(length);
+
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            doc = dbf.newDocumentBuilder().parse(is);
+            info.data = doc;
+
+            Element root = doc.getDocumentElement();
+            ua.getLogger().debug("ns:" + root.getAttribute("xmlns"));
+            String ns = root.getAttribute("xmlns");
+            info.str = ns;
+
+            Converter conv = (Converter)converters.get(ns);
+            if(conv != null) {
+                FopImage.ImageInfo i = conv.convert(doc);
+                if(i != null) {
+                    info = i;
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            try {
+                is.reset();
+            } catch (IOException ioe) { }
+            return null;
+        }
+        return info;
+    }
+
+    public static interface Converter {
+        public FopImage.ImageInfo convert(Document doc);
+    }
+}
+
index 1aadad141c75919d0a4961dcdf4b6b94ea313595..4713c38efdcd01224009f443a156feadc53e99fd 100644 (file)
@@ -9,7 +9,10 @@ package org.apache.fop.render.pdf;
 
 // FOP
 import org.apache.fop.render.PrintRenderer;
-import org.apache.fop.image.FopImage;
+import org.apache.fop.render.XMLHandler;
+import org.apache.fop.render.RendererContext;
+import org.apache.fop.fo.FOUserAgent;
+import org.apache.fop.image.*;
 import org.apache.fop.apps.FOPException;
 import org.apache.fop.fo.properties.*;
 import org.apache.fop.datatypes.*;
@@ -54,6 +57,7 @@ text decoration
  *
  */
 public class PDFRenderer extends PrintRenderer {
+    public static final String mimeType = "application/pdf";
 
     /**
      * the PDF Document being created
@@ -142,6 +146,14 @@ public class PDFRenderer extends PrintRenderer {
         producer = prod;
     }
 
+    public void setUserAgent(FOUserAgent agent) {
+        super.setUserAgent(agent);
+        PDFXMLHandler xmlHandler = new PDFXMLHandler();
+        //userAgent.setDefaultXMLHandler(mimeType, xmlHandler);
+        String svg = "http://www.w3.org/2000/svg";
+        userAgent.addXMLHandler(mimeType, svg, xmlHandler);
+    }
+
     public void startRenderer(OutputStream stream) throws IOException {
         ostream = stream;
         this.pdfDoc = new PDFDocument();
@@ -225,21 +237,20 @@ public class PDFRenderer extends PrintRenderer {
        // multiply with current CTM
        currentStream.add(ctm.toPDFctm() + " cm\n");
        // Set clip?
+  currentStream.add("BT\n");
     }
 
     protected void endVParea() {
+  currentStream.add("ET\n");
        currentStream.add("Q\n");
     }
 
     protected void renderRegion(RegionReference region) {
        // Draw a rectangle so we can see it!
        // x=0,y=0,w=ipd,h=bpd
-       currentStream.add("BT\n");
        super.renderRegion(region);
-       currentStream.add("ET\n");
     }
 
-
     protected void renderLineArea(LineArea line) {
        super.renderLineArea(line);
        closeText();
@@ -448,5 +459,115 @@ public class PDFRenderer extends PrintRenderer {
             pdf = pdf.append("/" + name + " " + (size / 1000) + " Tf\n");
         }
     }
+
+    public void renderImage(Image image) {
+        String url = image.getURL();
+        ImageFactory fact = ImageFactory.getInstance();
+        FopImage fopimage = fact.getImage(url, userAgent);
+        if(fopimage == null) {
+            return;
+        }
+        if(!fopimage.load(FopImage.DIMENSIONS, userAgent)) {
+            return;
+        }
+        String mime = fopimage.getMimeType();
+        if("text/xml".equals(mime)) {
+            if(!fopimage.load(FopImage.ORIGINAL_DATA, userAgent)) {
+                return;
+            }
+            Document doc = ((XMLImage)fopimage).getDocument();
+            String ns = ((XMLImage)fopimage).getNameSpace();
+
+            renderDocument(doc, ns);
+
+        } else if("image/svg+xml".equals(mime)) {
+            if(!fopimage.load(FopImage.ORIGINAL_DATA, userAgent)) {
+                return;
+            }
+            Document doc = ((XMLImage)fopimage).getDocument();
+            String ns = ((XMLImage)fopimage).getNameSpace();
+
+            renderDocument(doc, ns);
+
+        } else if("image/eps".equals(mime)) {
+            if(!fopimage.load(FopImage.ORIGINAL_DATA, userAgent)) {
+                return;
+            }
+            int xobj = pdfDoc.addImage(fopimage);
+            fact.releaseImage(url, userAgent);
+        } else if("image/jpg".equals(mime)) {
+            if(!fopimage.load(FopImage.ORIGINAL_DATA, userAgent)) {
+                return;
+            }
+            int xobj = pdfDoc.addImage(fopimage);
+            fact.releaseImage(url, userAgent);
+        } else {
+            if(!fopimage.load(FopImage.BITMAP, userAgent)) {
+                return;
+            }
+            int xobj = pdfDoc.addImage(fopimage);
+            fact.releaseImage(url, userAgent);
+
+            closeText();
+            int w = fopimage.getWidth();
+            int h = fopimage.getHeight();
+
+            currentStream.add("ET\nq\n" + ((float)w) + " 0 0 "
+                              + ((float)-h) + " "
+                              + (((float)currentBlockIPPosition) / 1000f) + " "
+                              + (((float)(currentBPPosition - 1000 * h)) / 1000f) + " cm\n" + "/Im"
+                              + xobj + " Do\nQ\nBT\n");
+        }
+
+    }
+
+    public void renderForeignObject(ForeignObject fo) {
+        Document doc = fo.getDocument();
+        String ns = fo.getNameSpace();
+        renderDocument(doc, ns);
+    }
+
+    public void renderDocument(Document doc, String ns) {
+        RendererContext context;
+        context = new RendererContext(mimeType);
+        context.setLogger(log);
+
+        context.setProperty(PDFXMLHandler.PDF_DOCUMENT, pdfDoc);
+        context.setProperty(PDFXMLHandler.PDF_STREAM, currentStream);
+        context.setProperty(PDFXMLHandler.PDF_X, new Integer(currentBlockIPPosition));
+        context.setProperty(PDFXMLHandler.PDF_Y, new Integer(currentBPPosition));
+        FontState fs = null;
+            try {
+                fs = new FontState(fontInfo, "Helvetica", "",
+                                          "", 12 * 1000, 0);
+            } catch (org.apache.fop.apps.FOPException fope) {
+                fope.printStackTrace();
+            }
+
+        context.setProperty(PDFXMLHandler.PDF_FONT_STATE, fs);
+        context.setProperty(PDFXMLHandler.PDF_FONT_NAME, currentFontName);
+        context.setProperty(PDFXMLHandler.PDF_FONT_SIZE, new Integer(currentFontSize));
+        context.setProperty(PDFXMLHandler.PDF_XPOS, new Integer(currentBlockIPPosition));
+        context.setProperty(PDFXMLHandler.PDF_YPOS, new Integer(currentBPPosition));
+        closeText();
+        currentStream.add("ET\n");
+        userAgent.renderXML(context, doc, ns);
+        currentStream.add("BT\n");
+
+    }
+
+    public void renderViewport(Viewport viewport) {
+        /*if (clip && w != 0 && h != 0) {
+            currentStream.add(x / 1000f + " " + y / 1000f + " m\n");
+            currentStream.add((x + w) / 1000f + " " + y / 1000f + " l\n");
+            currentStream.add((x + w) / 1000f + " " + (y - h) / 1000f
+                              + " l\n");
+            currentStream.add(x / 1000f + " " + (y - h) / 1000f + " l\n");
+            currentStream.add("h\n");
+            currentStream.add("W\n");
+            currentStream.add("n\n");
+        }*/
+        super.renderViewport(viewport);
+    }
 }
 
diff --git a/src/org/apache/fop/render/pdf/PDFXMLHandler.java b/src/org/apache/fop/render/pdf/PDFXMLHandler.java
new file mode 100644 (file)
index 0000000..6d98320
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+ * $Id$
+ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * For details on use and redistribution please refer to the
+ * LICENSE file included with these sources.
+ */
+
+package org.apache.fop.render.pdf;
+
+import org.apache.fop.fo.FOUserAgent;
+import org.apache.fop.render.XMLHandler;
+import org.apache.fop.render.RendererContext;
+import org.apache.fop.pdf.*;
+import org.apache.fop.svg.*;
+import org.apache.fop.layout.FontState;
+
+import org.apache.log.Logger;
+
+import org.apache.batik.dom.util.DOMUtilities;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Attr;
+
+import java.io.Writer;
+import java.io.IOException;
+
+import org.apache.batik.bridge.*;
+import org.apache.batik.swing.svg.*;
+import org.apache.batik.swing.gvt.*;
+import org.apache.batik.gvt.*;
+import org.apache.batik.gvt.renderer.*;
+import org.apache.batik.gvt.filter.*;
+import org.apache.batik.gvt.event.*;
+
+import org.w3c.dom.*;
+import org.w3c.dom.svg.*;
+import org.w3c.dom.css.*;
+import org.w3c.dom.svg.SVGLength;
+
+import java.awt.geom.AffineTransform;
+
+/**
+ */
+public class PDFXMLHandler implements XMLHandler {
+public static final String PDF_DOCUMENT = "pdfDoc";
+public static final String PDF_STREAM = "pdfStream";
+public static final String PDF_X = "x";
+public static final String PDF_Y = "y";
+public static final String PDF_FONT_STATE = "fontState";
+public static final String PDF_FONT_NAME = "fontName";
+public static final String PDF_FONT_SIZE = "fontSize";
+public static final String PDF_XPOS = "xpos";
+public static final String PDF_YPOS = "ypos";
+
+    public PDFXMLHandler() {
+    }
+
+    public void handleXML(RendererContext context, Document doc,
+                          String ns) throws Exception {
+        PDFDocument pdfDoc = (PDFDocument) context.getProperty(PDF_DOCUMENT);
+        PDFInfo pdfi = new PDFInfo();
+        pdfi.pdfDoc = (PDFDocument)context.getProperty(PDF_DOCUMENT);
+        pdfi.currentStream = (PDFStream)context.getProperty(PDF_STREAM);
+        pdfi.x = ((Integer)context.getProperty(PDF_X)).intValue();
+        pdfi.y = ((Integer)context.getProperty(PDF_Y)).intValue();
+        pdfi.fs = (FontState)context.getProperty(PDF_FONT_STATE);
+        pdfi.currentFontName = (String)context.getProperty(PDF_FONT_NAME);
+        pdfi.currentFontSize = ((Integer)context.getProperty(PDF_FONT_SIZE)).intValue();
+        pdfi.currentXPosition = ((Integer)context.getProperty(PDF_XPOS)).intValue();
+        pdfi.currentYPosition = ((Integer)context.getProperty(PDF_YPOS)).intValue();
+
+        String svg = "http://www.w3.org/2000/svg";
+        if (svg.equals(ns)) {
+            SVGHandler svghandler = new SVGHandler();
+            svghandler.renderSVGDocument(context, doc, pdfi);
+        } else {
+        }
+    }
+
+    class PDFInfo {
+        PDFDocument pdfDoc;
+        PDFStream currentStream;
+        int x;
+        int y;
+        FontState fs;
+        String currentFontName;
+        int currentFontSize;
+        int currentXPosition;
+        int currentYPosition;
+    }
+
+    protected class SVGHandler {
+        protected void renderSVGDocument(RendererContext context, Document doc, PDFInfo pdfInfo) {
+            float sx = 1, sy = 1;
+            int xOffset = pdfInfo.x, yOffset = pdfInfo.y;
+
+            org.apache.fop.svg.SVGUserAgent ua
+                 = new org.apache.fop.svg.SVGUserAgent(new AffineTransform());
+            ua.setLogger(context.getLogger());
+
+            GVTBuilder builder = new GVTBuilder();
+            BridgeContext ctx = new BridgeContext(ua);
+            TextPainter textPainter = null;
+            textPainter = new PDFTextPainter(pdfInfo.fs);
+            ctx.setTextPainter(textPainter);
+
+            PDFAElementBridge aBridge = new PDFAElementBridge();
+            aBridge.setCurrentTransform(new AffineTransform(sx, 0, 0, sy, xOffset / 1000f, yOffset / 1000f));
+            ctx.putBridge(aBridge);
+
+            GraphicsNode root;
+            try {
+                root = builder.build(ctx, doc);
+            } catch (Exception e) {
+                context.getLogger().error("svg graphic could not be built: "
+                                       + e.getMessage(), e);
+                return;
+            }
+            // get the 'width' and 'height' attributes of the SVG document
+            float w = (float)ctx.getDocumentSize().getWidth() * 1000f;
+            float h = (float)ctx.getDocumentSize().getHeight() * 1000f;
+            ctx = null;
+            builder = null;
+
+            /*
+             * Clip to the svg area.
+             * Note: To have the svg overlay (under) a text area then use
+             * an fo:block-container
+             */
+            pdfInfo.currentStream.add("q\n");
+            // transform so that the coordinates (0,0) is from the top left
+            // and positive is down and to the right. (0,0) is where the
+            // viewBox puts it.
+            pdfInfo.currentStream.add(sx + " 0 0 " + sy + " " + xOffset / 1000f + " "
+                              + yOffset / 1000f + " cm\n");
+
+            SVGSVGElement svg = ((SVGDocument)doc).getRootElement();
+            AffineTransform at = ViewBox.getPreserveAspectRatioTransform(svg, w / 1000f, h / 1000f);
+            if(!at.isIdentity()) {
+                double[] vals = new double[6];
+                at.getMatrix(vals);
+                pdfInfo.currentStream.add(PDFNumber.doubleOut(vals[0]) + " "
+                                + PDFNumber.doubleOut(vals[1]) + " "
+                                + PDFNumber.doubleOut(vals[2]) + " "
+                                + PDFNumber.doubleOut(vals[3]) + " "
+                                + PDFNumber.doubleOut(vals[4]) + " "
+                                + PDFNumber.doubleOut(vals[5]) + " cm\n");
+            }
+
+            PDFGraphics2D graphics = new PDFGraphics2D(true, pdfInfo.fs, pdfInfo.pdfDoc,
+                                     pdfInfo.currentFontName,
+                                     pdfInfo.currentFontSize,
+                                     pdfInfo.currentXPosition,
+                                     pdfInfo.currentYPosition);
+            graphics.setGraphicContext(new org.apache.batik.ext.awt.g2d.GraphicContext());
+
+            try {
+                root.paint(graphics);
+                pdfInfo.currentStream.add(graphics.getString());
+            } catch (Exception e) {
+                context.getLogger().error("svg graphic could not be rendered: "
+                                       + e.getMessage(), e);
+            }
+
+            //currentAnnotList = graphics.getAnnotList();
+
+            pdfInfo.currentStream.add("Q\n");
+        }
+    }
+}
+
index 21e9598e2d017b8d9bfba718cd838e57292407db..2dd96ad340d78e72212234a930f79c837c939159 100644 (file)
@@ -104,6 +104,7 @@ public class PDFGraphics2D extends AbstractGraphics2D {
         currentYPosition = ypos;
         currentXPosition = xpos;
         fontState = fs;
+        graphicsState = new PDFState();
     }
 
     protected PDFGraphics2D(boolean textAsShapes) {
index c4e4a19c0aa06d058d09ae1f4ab899895a5bc715..785f6608085c9e94f8a63311209e9ed620938b51 100644 (file)
@@ -16,10 +16,11 @@ import org.apache.fop.apps.Driver;
 
 import org.apache.batik.util.XMLResourceDescriptor;
 import org.apache.batik.dom.svg.SVGDOMImplementation;
+import org.w3c.dom.DOMImplementation;
 
 public class SVGElementMapping implements ElementMapping {
-
     private static HashMap foObjs = null;
+    private static boolean batik = true;
 
     private static synchronized void setupSVG() {
         if (foObjs == null) {
@@ -36,12 +37,16 @@ public class SVGElementMapping implements ElementMapping {
     }
 
     public void addToBuilder(FOTreeBuilder builder) {
-        try {
-            setupSVG();
-            String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
-            builder.addMapping(svgNS, foObjs);
-        } catch (Throwable t) {
-            // if the classes are not available
+        if(batik) {
+            try {
+                setupSVG();
+                String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
+                builder.addMapping(svgNS, foObjs);
+            } catch (Throwable t) {
+                // if the classes are not available
+                // the DISPLAY is not checked
+                batik = false;
+            }
         }
     }