]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
made the batik pdf transcoder work again
authorKeiron Liddle <keiron@apache.org>
Thu, 15 Nov 2001 08:12:37 +0000 (08:12 +0000)
committerKeiron Liddle <keiron@apache.org>
Thu, 15 Nov 2001 08:12:37 +0000 (08:12 +0000)
some improvements to svg handling
Submitted by: Thomas E Deweese <thomas.deweese@kodak.com>

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

src/org/apache/fop/fo/XMLObj.java
src/org/apache/fop/image/analyser/SVGReader.java
src/org/apache/fop/layout/FontInfo.java
src/org/apache/fop/pdf/PDFDocument.java
src/org/apache/fop/svg/PDFDocumentGraphics2D.java
src/org/apache/fop/svg/PDFTranscoder.java
src/org/apache/fop/svg/SVGElement.java
src/org/apache/fop/svg/SVGUserAgent.java
src/org/apache/fop/svg/SVGUtilities.java

index db4dd3962c42e774e4e2aebc9ef41fc5420b1c5a..c52006d8a10ba270aa852d010275b147e0687266 100644 (file)
@@ -53,51 +53,52 @@ public abstract class XMLObj extends FONode {
     public abstract String getNameSpace();
 
     protected static HashMap ns = new HashMap();
+    static {
+        ns.put("xlink", "http://www.w3.org/1999/xlink");
+    }
 
     public void addElement(Document doc, Element parent) {
         this.doc = doc;
         element = doc.createElementNS(getNameSpace(), name);
 
-            for (int count = 0; count < attr.getLength(); count++) {
-                String rf = attr.getValue(count);
-                String qname = attr.getQName(count);
-                if (qname.indexOf(":") == -1) {
-                    element.setAttribute(qname, rf);
+        for (int count = 0; count < attr.getLength(); count++) {
+            String rf = attr.getValue(count);
+            String qname = attr.getQName(count);
+            int idx = qname.indexOf(":");
+            if (idx == -1) {
+                element.setAttribute(qname, rf);
+            } else {
+                String pref = qname.substring(0, idx);
+                String tail = qname.substring(idx + 1);
+                if (pref.equals("xmlns")) {
+                    ns.put(tail, rf);
                 } else {
-                    String pref =
-                        qname.substring(0, qname.indexOf(":"));
-                    if (pref.equals("xmlns")) {
-                        ns.put(qname.substring(qname.indexOf(":")
-                                                      + 1), rf);
-                    }
-                    ns.put("xlink", "http://www.w3.org/1999/xlink");
-                    element.setAttributeNS((String)ns.get(pref),
-                                           qname, rf);
+                    element.setAttributeNS((String)ns.get(pref), tail, rf);
                 }
             }
+        }
         attr = null;
         parent.appendChild(element);
     }
 
     public void buildTopLevel(Document doc, Element svgRoot) {
         // build up the info for the top level element
-            for (int count = 0; count < attr.getLength(); count++) {
-                String rf = attr.getValue(count);
-                String qname = attr.getQName(count);
-                if (qname.indexOf(":") == -1) {
-                    element.setAttribute(qname, rf);
+        for (int count = 0; count < attr.getLength(); count++) {
+            String rf = attr.getValue(count);
+            String qname = attr.getQName(count);
+            int idx = qname.indexOf(":");
+            if (idx == -1) {
+                element.setAttribute(qname, rf);
+            } else {
+                String pref = qname.substring(0, idx);
+                String tail = qname.substring(idx + 1);
+                if (pref.equals("xmlns")) {
+                    ns.put(tail, rf);
                 } else {
-                    String pref =
-                       qname.substring(0, qname.indexOf(":"));
-                    if (pref.equals("xmlns")) {
-                        ns.put(qname.substring(qname.indexOf(":")
-                                                      + 1), rf);
-                    }
-                    ns.put("xlink", "http://www.w3.org/1999/xlink");
-                    element.setAttributeNS((String)ns.get(pref),
-                                           qname, rf);
+                    element.setAttributeNS((String)ns.get(pref), tail, rf);
                 }
             }
+        }
     }
 
     public Document createBasicDocument() {
index 2f60b62ea9940147bc9a5a347b6965e5af1afa3f..a27080380d4e6524ab7cc5eacdc93d72908bc611 100644 (file)
@@ -157,7 +157,7 @@ public class SVGReader extends AbstractImageReader {
         }
 
         public String getMedia() {
-            return "";
+            return "print";
         }
 
         /**
index 4f145e3eab62023b21376eb5a9c5c2eb1fd9bf71..edfefc1ebd8c8dd00457f10f99f95e81ab269495 100644 (file)
@@ -8,7 +8,6 @@
 package org.apache.fop.layout;
 
 import java.util.HashMap;
-import org.apache.fop.messaging.MessageHandler;
 import java.util.Enumeration;
 
 import org.apache.fop.apps.FOPException;
@@ -58,10 +57,10 @@ public class FontInfo {
                 if (f == null) {
                     throw new FOPException("no default font defined by OutputConverter");
                 }
-                MessageHandler.errorln("defaulted font to any,normal,normal");
+                //MessageHandler.errorln("defaulted font to any,normal,normal");
             }
-            MessageHandler.errorln("unknown font " + key
-                                   + " so defaulted font to any");
+            //MessageHandler.errorln("unknown font " + key
+            //                       + " so defaulted font to any");
         }
 
         usedFonts.put(f, fonts.get(f));
index 06bf33b54683404509f69e4d174b29224ab6a735..b21fc706f9e1cfe31e215b92899f414171854eae 100644 (file)
@@ -946,7 +946,7 @@ public class PDFDocument {
 
         /* add it to the list of objects */
         this.objects.add(page);
-
+        pages.addPage(page);
         return page;
     }
 
index a977633534e85c803b88b23856ee3e9048bd81ef..3ddbba1df29fc051af778d851a26a8dec990f2b3 100644 (file)
@@ -78,16 +78,16 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D {
         currentFontSize = 0;
         currentYPosition = 0;
         currentXPosition = 0;
-
-        PDFResources pdfResources = this.pdfDoc.getResources();
-        currentPage = this.pdfDoc.makePage(pdfResources,
-                                                   width, height);
     }
 
     void setupDocument(OutputStream stream, int width, int height) {
         this.width = width;
         this.height = height;
         this.stream = stream;
+
+        PDFResources pdfResources = this.pdfDoc.getResources();
+        currentPage = this.pdfDoc.makePage(pdfResources,
+                                                   width, height);
         currentStream.write("1 0 0 -1 0 " + height + " cm\n");
     }
 
@@ -167,10 +167,6 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D {
         pdfDoc.outputTrailer(stream);
     }
 
-    public void setGraphicContext(GraphicContext c) {
-        gc = c;
-    }
-
     /**
      * This constructor supports the create method
      */
index 9d5ab9ff8f42664dbe14d5b5eb96c13d32c3695a..dea3df6d2bf335201126cf344ea4d840470a9a51 100644 (file)
@@ -382,7 +382,7 @@ public class PDFTranscoder extends XMLAbstractTranscoder {
         }
 
         public String getMedia() {
-            return "";
+            return "print";
         }
 
         /**
index cf4fd9c6383676838c3d8dda84c543632ee34b2e..c30177de8475d89d9a7dd995e7b0e55dc7d620fe 100644 (file)
@@ -29,8 +29,14 @@ 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.apache.batik.bridge.UnitProcessor;
+import org.apache.batik.css.value.ImmutableFloat;
+import org.apache.batik.css.CSSOMReadOnlyValue;
+import org.apache.batik.util.SVGConstants;
 
 import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.css.CSSPrimitiveValue;
+
 import org.apache.batik.dom.svg.SVGDOMImplementation;
 
 import java.io.File;
@@ -38,6 +44,7 @@ import java.net.URL;
 import java.util.List;
 import java.util.ArrayList;
 import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
 
 /**
  * class representing svg:svg pseudo flow object.
@@ -127,29 +134,10 @@ public class SVGElement extends SVGObj {
             e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI);
         //}
 
-        String s;
-        SVGUserAgent userAgent = new SVGUserAgent(new AffineTransform());
-        userAgent.setLogger(log);
-        BridgeContext ctx = new BridgeContext(userAgent);
-        UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
+        Point2D p2d = getSize(this.fs, svgRoot);
 
-        // 'width' attribute - default is 100%
-        s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE);
-        if (s.length() == 0) {
-            s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE;
-        }
-        float width = UnitProcessor.svgHorizontalLengthToUserSpace
-                     (s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx);
-
-        // 'height' attribute - default is 100%
-        s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE);
-        if (s.length() == 0) {
-            s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE;
-        }
-        float height = UnitProcessor.svgVerticalLengthToUserSpace
-                     (s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx);
-
-        SVGArea svg = new SVGArea(fs, width, height);
+        SVGArea svg = new SVGArea(fs, (float)p2d.getX(),
+                                  (float)p2d.getY());
         svg.setSVGDocument(doc);
         svg.start();
 
@@ -186,4 +174,96 @@ public class SVGElement extends SVGObj {
         FONT_FAMILY.add("serif");
     }
 
+    public static Point2D getSize(FontState fs, Element svgRoot) {
+        String str;
+        UnitProcessor.Context ctx;
+        ctx = new PDFUnitContext(fs, svgRoot);
+        str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
+        if (str.length() == 0) str = "100%";
+        float width = UnitProcessor.svgHorizontalLengthToUserSpace
+            (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx); 
+
+        str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
+        if (str.length() == 0) str = "100%";
+        float height = UnitProcessor.svgVerticalLengthToUserSpace
+            (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx);
+        return new Point2D.Float(width, height);
+    }
+    /**
+     * This class is the default context for a particular
+     * element. Informations not available on the element are get from
+     * the bridge context (such as the viewport or the pixel to
+     * millimeter factor.
+     */
+    public static class PDFUnitContext implements UnitProcessor.Context {
+
+        /** The element. */
+        protected Element e;
+        protected FontState fs;
+        public PDFUnitContext(FontState fs, Element e) { 
+            this.e  = e;
+            this.fs = fs;
+        }
+
+        /**
+         * Returns the element.
+         */
+        public Element getElement() {
+            return e;
+        }
+
+        /**
+      * Returns the context of the parent element of this context.
+         * Since this is always for the root SVG element there never
+         * should be one...
+         */
+        public UnitProcessor.Context getParentElementContext() {
+            return null;
+        }
+
+        /**
+         * Returns the pixel to mm factor.
+         */
+        public float getPixelToMM() {
+                return 0.264583333333333333333f;
+                // 72 dpi
+        }
+
+        /**
+         * Returns the font-size medium value in pt.
+         */
+        public float getMediumFontSize() {
+            return 9f;
+        }
+
+        /**
+         * Returns the font-size value.
+         */
+        public CSSPrimitiveValue getFontSize() {
+            return new CSSOMReadOnlyValue
+                (new ImmutableFloat(CSSPrimitiveValue.CSS_PT,
+                                    fs.getFontSize()));
+        }
+
+        /**
+         * Returns the x-height value.
+         */
+        public float getXHeight() {
+            return 0.5f;
+        }
+
+        /**
+         * Returns the viewport width used to compute units.
+         */
+        public float getViewportWidth() {
+            return 100;
+        }
+
+        /**
+         * Returns the viewport height used to compute units.
+         */
+        public float getViewportHeight() {
+            return 100;
+        }
+    }
 }
index 9c38085688f892f0d4b4829a8a6be5ad75b155f8..dc4c8a24746df4f94ce0835d4dab5f0f9a453d40 100644 (file)
@@ -85,7 +85,7 @@ public class SVGUserAgent implements UserAgent {
     }
 
     public String getMedia() {
-        return "";
+        return "print";
     }
 
     /**
index fe854d271136e11385ed289cbf9598c95ab0ee13..6ff4e404f3720637827d1f66757b334be5345f35 100644 (file)
@@ -161,7 +161,7 @@ public class SVGUtilities {
     public static final Element createImage(Document doc, String ref,
                                             float width, float height) {
         Element border = doc.createElementNS(svgNS, "image");
-        border.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href",
+        border.setAttributeNS("http://www.w3.org/1999/xlink", "href",
                               ref);
         border.setAttributeNS(null, "width", "" + width);
         border.setAttributeNS(null, "height", "" + height);