]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Applied Tom DeWeese/Batik Team's SVG patches. New Batik.jar created from latest...
authorGlen Mazza <gmazza@apache.org>
Sat, 11 Oct 2003 14:48:49 +0000 (14:48 +0000)
committerGlen Mazza <gmazza@apache.org>
Sat, 11 Oct 2003 14:48:49 +0000 (14:48 +0000)
PR:
Obtained from:
Submitted by:
Reviewed by:

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

lib/batik.jar
src/java/org/apache/fop/render/ps/PSTranscoder.java
src/java/org/apache/fop/svg/AbstractFOPTranscoder.java
src/java/org/apache/fop/svg/PDFImageElementBridge.java
src/java/org/apache/fop/svg/PDFTranscoder.java

index cddad09ce866029ee87429d0184008445530c2b9..9a5dbb58b7864f572785b1be84b3f4b7da40f150 100644 (file)
Binary files a/lib/batik.jar and b/lib/batik.jar differ
index d5be860d3c0b4f74bcc2e47721624acd42625ca9..76f2771c9aa5725e90d3c5b007062d65aedda266 100644 (file)
@@ -116,6 +116,8 @@ import org.w3c.dom.svg.SVGSVGElement;
  */
 public class PSTranscoder extends AbstractFOPTranscoder {
 
+    protected PSDocumentGraphics2D graphics = null;
+
     /**
      * Constructs a new <tt>PSTranscoder</tt>.
      */
@@ -132,21 +134,38 @@ public class PSTranscoder extends AbstractFOPTranscoder {
      * @exception TranscoderException if an error occured while transcoding
      */
     protected void transcode(Document document, String uri,
-                             TranscoderOutput output) throws TranscoderException {
+                             TranscoderOutput output) 
+        throws TranscoderException {
+
+        graphics = new PSDocumentGraphics2D(false);
+
+        super.transcode(document, uri, output);
 
-        if (!(document instanceof SVGOMDocument)) {
-            throw new TranscoderException(Messages.formatMessage("notsvg",
-                    null));
+        // prepare the image to be painted
+        int w = (int)(width+.5);
+        int h = (int)(height+.5);
+
+        try {
+            graphics.setupDocument(output.getOutputStream(), w, h);
+            graphics.setSVGDimension(width, height);
+
+            if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
+                graphics.setBackgroundColor
+                    ((Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
         }
-        SVGDocument svgDoc = (SVGDocument)document;
-        SVGSVGElement root = svgDoc.getRootElement();
-        // initialize the SVG document with the appropriate context
-        String parserClassname = (String)hints.get(KEY_XML_PARSER_CLASSNAME);
+            graphics.setGraphicContext
+                (new org.apache.batik.ext.awt.g2d.GraphicContext());
+            graphics.setTransform(curTxf);
 
-        PSDocumentGraphics2D graphics = new PSDocumentGraphics2D(false);
+            this.root.paint(graphics);
+
+            graphics.finish();
+        } catch (IOException ex) {
+            throw new TranscoderException(ex);
+        }
+    }
 
-        // build the GVT tree
-        GVTBuilder builder = new GVTBuilder();
+    protected BridgeContext createBridgeContext() {
         BridgeContext ctx = new BridgeContext(userAgent);
         TextPainter textPainter = null;
         textPainter = new StrokingTextPainter();
@@ -162,102 +181,7 @@ public class PSTranscoder extends AbstractFOPTranscoder {
         //ctx.putBridge(pdfAElementBridge);
 
         //ctx.putBridge(new PSImageElementBridge());
-        GraphicsNode gvtRoot;
-        try {
-            gvtRoot = builder.build(ctx, svgDoc);
-        } catch (BridgeException ex) {
-            throw new TranscoderException(ex);
-        }
-        // get the 'width' and 'height' attributes of the SVG document
-        float docWidth = (float)ctx.getDocumentSize().getWidth();
-        float docHeight = (float)ctx.getDocumentSize().getHeight();
-        ctx = null;
-        builder = null;
-
-        // compute the image's width and height according the hints
-        float imgWidth = -1;
-        if (hints.containsKey(ImageTranscoder.KEY_WIDTH)) {
-            imgWidth =
-                ((Float)hints.get(ImageTranscoder.KEY_WIDTH)).floatValue();
-        }
-        float imgHeight = -1;
-        if (hints.containsKey(ImageTranscoder.KEY_HEIGHT)) {
-            imgHeight =
-                ((Float)hints.get(ImageTranscoder.KEY_HEIGHT)).floatValue();
-        }
-        float width, height;
-        if (imgWidth > 0 && imgHeight > 0) {
-            width = imgWidth;
-            height = imgHeight;
-        } else if (imgHeight > 0) {
-            width = (docWidth * imgHeight) / docHeight;
-            height = imgHeight;
-        } else if (imgWidth > 0) {
-            width = imgWidth;
-            height = (docHeight * imgWidth) / docWidth;
-        } else {
-            width = docWidth;
-            height = docHeight;
-        }
-        // compute the preserveAspectRatio matrix
-        AffineTransform px;
-        String ref = null;
-        try {
-            ref = new URL(uri).getRef();
-        } catch (MalformedURLException ex) {
-            // nothing to do, catched previously
-        }
-
-        try {
-            px = ViewBox.getViewTransform(ref, root, width, height);
-        } catch (BridgeException ex) {
-            throw new TranscoderException(ex);
-        }
-
-        if (px.isIdentity() && (width != docWidth || height != docHeight)) {
-            // The document has no viewBox, we need to resize it by hand.
-            // we want to keep the document size ratio
-            float d = Math.max(docWidth, docHeight);
-            float dd = Math.max(width, height);
-            float scale = dd / d;
-            px = AffineTransform.getScaleInstance(scale, scale);
-        }
-        // take the AOI into account if any
-        if (hints.containsKey(ImageTranscoder.KEY_AOI)) {
-            Rectangle2D aoi = (Rectangle2D)hints.get(ImageTranscoder.KEY_AOI);
-            // transform the AOI into the image's coordinate system
-            aoi = px.createTransformedShape(aoi).getBounds2D();
-            AffineTransform mx = new AffineTransform();
-            double sx = width / aoi.getWidth();
-            double sy = height / aoi.getHeight();
-            mx.scale(sx, sy);
-            double tx = -aoi.getX();
-            double ty = -aoi.getY();
-            mx.translate(tx, ty);
-            // take the AOI transformation matrix into account
-            // we apply first the preserveAspectRatio matrix
-            px.preConcatenate(mx);
-        }
-        // prepare the image to be painted
-        int w = (int)width;
-        int h = (int)height;
-
-        try {
-            graphics.setupDocument(output.getOutputStream(), w, h);
-            graphics.setSVGDimension(docWidth, docHeight);
-
-            if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
-                graphics.setBackgroundColor((Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
-            }
-            graphics.setGraphicContext(new org.apache.batik.ext.awt.g2d.GraphicContext());
-            graphics.setTransform(px);
-
-            gvtRoot.paint(graphics);
-
-            graphics.finish();
-        } catch (IOException ex) {
-            throw new TranscoderException(ex);
-        }
+        return ctx;
     }
 
 }
index bb93e52d90ee25988c9fca92a2220c8dfea1c542..e4f17c6d4179a0d9c7e7bb0eae0580d8963b6be8 100644 (file)
@@ -65,7 +65,7 @@ import org.apache.batik.dom.svg.SVGDOMImplementation;
 import org.apache.batik.dom.util.DocumentFactory;
 import org.apache.batik.transcoder.ErrorHandler;
 import org.apache.batik.transcoder.TranscoderException;
-import org.apache.batik.transcoder.XMLAbstractTranscoder;
+import org.apache.batik.transcoder.SVGAbstractTranscoder;
 import org.apache.batik.transcoder.image.ImageTranscoder;
 import org.apache.batik.util.SVGConstants;
 import org.apache.batik.util.XMLResourceDescriptor;
@@ -74,7 +74,7 @@ import org.w3c.dom.DOMImplementation;
 /**
  * This is the common base class of all of FOP's transcoders.
  */
-public abstract class AbstractFOPTranscoder extends XMLAbstractTranscoder
+public abstract class AbstractFOPTranscoder extends SVGAbstractTranscoder
             implements LogEnabled {
 
     /**
index 5507dbe220ba2a86c9ab89602351678ac90fbba1..3c629276118c502acea4c89fa8303bbfad7ecb06 100644 (file)
@@ -53,12 +53,20 @@ package org.apache.fop.svg;
 import org.apache.batik.bridge.SVGImageElementBridge;
 
 import org.apache.fop.image.JpegImage;
+import org.apache.fop.image.FopImage;
+import org.apache.fop.image.analyser.ImageReaderFactory;
 
 import java.awt.Shape;
 import java.awt.Graphics2D;
 import java.awt.geom.Rectangle2D;
+import java.net.URL;
 
+import org.w3c.dom.Element;
+
+import org.apache.batik.bridge.BridgeContext;
 import org.apache.batik.gvt.AbstractGraphicsNode;
+import org.apache.batik.gvt.GraphicsNode;
+import org.apache.batik.util.ParsedURL;
 
 /**
  * Bridge class for the &lt;image> element when jpeg images.
@@ -72,7 +80,6 @@ public class PDFImageElementBridge extends SVGImageElementBridge {
      */
     public PDFImageElementBridge() { }
 
-/*
     /**
      * Create the raster image node.
      * THis checks if it is a jpeg file and creates a jpeg node
@@ -81,14 +88,19 @@ public class PDFImageElementBridge extends SVGImageElementBridge {
      * @param e the svg element for the image
      * @param purl the parsed url for the image resource
      * @return a new graphics node
-     *
-    protected GraphicsNode createRasterImageNode(BridgeContext ctx,
-            Element e, ParsedURL purl) {
-
+     */
+    protected GraphicsNode createImageGraphicsNode
+        (BridgeContext ctx, Element e, ParsedURL purl) {
+        GraphicsNode origGN = super.createImageGraphicsNode
+            (ctx, e, purl);
         try {
-            JpegImage jpeg = new JpegImage(new URL(purl.toString()));
-            PDFFilter filter = jpeg.getPDFFilter();
-            PDFJpegNode node = new PDFJpegNode(jpeg);
+            FopImage.ImageInfo ii = ImageReaderFactory.make
+                (purl.toString(), purl.openStream(), null);
+            if (ii.mimeType.toLowerCase() == "image/jpeg") {
+                JpegImage jpeg = new JpegImage(ii);
+                PDFJpegNode node = new PDFJpegNode(jpeg, origGN);
+
+                Rectangle2D imgBounds = getImageBounds(ctx, e);
             Rectangle2D bounds = node.getPrimitiveBounds();
             float [] vb = new float[4];
             vb[0] = 0; // x
@@ -96,17 +108,16 @@ public class PDFImageElementBridge extends SVGImageElementBridge {
             vb[2] = (float) bounds.getWidth(); // width
             vb[3] = (float) bounds.getHeight(); // height
 
-            // handles the 'preserveAspectRatio', 'overflow' and 'clip' and sets the
-            // appropriate AffineTransform to the image node
-            initializeViewport(ctx, e, node, vb, bounds);
-
+                // handles the 'preserveAspectRatio', 'overflow' and 'clip' 
+                // and sets the appropriate AffineTransform to the image node
+                initializeViewport(ctx, e, node, vb, imgBounds);
             return node;
+            }
         } catch (Exception ex) {
         }
 
-        return super.createRasterImageNode(ctx, e, purl);
+        return origGN;
     }
-*/
 
     /**
      * A PDF jpeg node.
@@ -115,14 +126,16 @@ public class PDFImageElementBridge extends SVGImageElementBridge {
      */
     public static class PDFJpegNode extends AbstractGraphicsNode {
         private JpegImage jpeg;
-
+        private GraphicsNode origGraphicsNode ;
         /**
          * Create a new pdf jpeg node for drawing jpeg images
          * into pdf graphics.
          * @param j the jpeg image
          */
-        public PDFJpegNode(JpegImage j) {
+        public PDFJpegNode(JpegImage j,
+                           GraphicsNode origGraphicsNode) {
             jpeg = j;
+            this.origGraphicsNode = origGraphicsNode;
         }
 
         /**
@@ -152,6 +165,10 @@ public class PDFImageElementBridge extends SVGImageElementBridge {
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
+            } else {
+                // Not going directly into PDF so use
+                // original implemtation so filters etc work.
+                origGraphicsNode.primitivePaint(g2d);
             }
         }
 
index 6b69c659be832081fc3ed0b84e2bc3d92b686a58..000312c2cfab754facf7debfd1a7e74446c39b68 100644 (file)
@@ -107,7 +107,8 @@ import org.w3c.dom.svg.SVGSVGElement;
 public class PDFTranscoder extends AbstractFOPTranscoder
         implements Configurable {
 
-    private Configuration cfg;
+    private   Configuration         cfg      = null;
+    protected PDFDocumentGraphics2D graphics = null;
 
     /**
      * Constructs a new <tt>ImageTranscoder</tt>.
@@ -134,22 +135,10 @@ public class PDFTranscoder extends AbstractFOPTranscoder
      * @exception TranscoderException if an error occured while transcoding
      */
     protected void transcode(Document document, String uri,
-                             TranscoderOutput output) throws TranscoderException {
+                             TranscoderOutput output) 
+        throws TranscoderException {
 
-        if (!(document instanceof SVGOMDocument)) {
-            throw new TranscoderException(Messages.formatMessage("notsvg",
-                    null));
-        }
-        SVGDocument svgDoc = (SVGDocument)document;
-        SVGSVGElement root = svgDoc.getRootElement();
-        // initialize the SVG document with the appropriate context
-        String parserClassname = (String)hints.get(KEY_XML_PARSER_CLASSNAME);
-
-        /*boolean stroke = true;
-        if (hints.containsKey(KEY_STROKE_TEXT)) {
-            stroke = ((Boolean)hints.get(KEY_STROKE_TEXT)).booleanValue();
-        }*/
-        PDFDocumentGraphics2D graphics = new PDFDocumentGraphics2D();
+        graphics = new PDFDocumentGraphics2D();
         ContainerUtil.enableLogging(graphics, getLogger());
         try {
             if (this.cfg != null) {
@@ -161,127 +150,57 @@ public class PDFTranscoder extends AbstractFOPTranscoder
                 "Error while setting up PDFDocumentGraphics2D", e);
         }
 
-        // build the GVT tree
-        GVTBuilder builder = new GVTBuilder();
-        BridgeContext ctx = new BridgeContext(userAgent);
-        TextPainter textPainter = null;
-        textPainter = new StrokingTextPainter();
-        ctx.setTextPainter(textPainter);
-
-        PDFTextElementBridge pdfTextElementBridge;
-        pdfTextElementBridge = new PDFTextElementBridge(graphics.getFontInfo());
-        ctx.putBridge(pdfTextElementBridge);
+        super.transcode(document, uri, output);
 
-        PDFAElementBridge pdfAElementBridge = new PDFAElementBridge();
-        AffineTransform currentTransform = new AffineTransform(1, 0, 0, 1, 0, 0);
-        pdfAElementBridge.setCurrentTransform(currentTransform);
-        ctx.putBridge(pdfAElementBridge);
-        ctx.putBridge(new PDFImageElementBridge());
-        GraphicsNode gvtRoot;
-        try {
-            gvtRoot = builder.build(ctx, svgDoc);
-        } catch (BridgeException ex) {
-            throw new TranscoderException(ex);
-        }
-        // get the 'width' and 'height' attributes of the SVG document
-        float docWidth = (float)ctx.getDocumentSize().getWidth();
-        float docHeight = (float)ctx.getDocumentSize().getHeight();
-        ctx = null;
-        builder = null;
+        // prepare the image to be painted
+        int w = (int)(width+.5);
+        int h = (int)(height+.5);
 
-        // compute the image's width and height according the hints
-        float imgWidth = -1;
-        if (hints.containsKey(ImageTranscoder.KEY_WIDTH)) {
-            imgWidth =
-                ((Float)hints.get(ImageTranscoder.KEY_WIDTH)).floatValue();
-        }
-        float imgHeight = -1;
-        if (hints.containsKey(ImageTranscoder.KEY_HEIGHT)) {
-            imgHeight =
-                ((Float)hints.get(ImageTranscoder.KEY_HEIGHT)).floatValue();
-        }
-        float width, height;
-        if (imgWidth > 0 && imgHeight > 0) {
-            width = imgWidth;
-            height = imgHeight;
-        } else if (imgHeight > 0) {
-            width = (docWidth * imgHeight) / docHeight;
-            height = imgHeight;
-        } else if (imgWidth > 0) {
-            width = imgWidth;
-            height = (docHeight * imgWidth) / docWidth;
-        } else {
-            width = docWidth;
-            height = docHeight;
-        }
-        // compute the preserveAspectRatio matrix
-        AffineTransform px;
-        String ref = null;
         try {
-            ref = new URL(uri).getRef();
-        } catch (MalformedURLException ex) {
-            // nothing to do, catched previously
-        }
+            graphics.setupDocument(output.getOutputStream(), w, h);
+            graphics.setSVGDimension(width, height);
 
-        try {
-            px = ViewBox.getViewTransform(ref, root, width, height);
-        } catch (BridgeException ex) {
-            throw new TranscoderException(ex);
+            if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
+                graphics.setBackgroundColor
+                    ((Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
         }
+            graphics.setGraphicContext
+                (new org.apache.batik.ext.awt.g2d.GraphicContext());
+            graphics.setTransform(curTxf);
 
-        if (px.isIdentity() && (width != docWidth || height != docHeight)) {
-            // The document has no viewBox, we need to resize it by hand.
-            // we want to keep the document size ratio
-            float d = Math.max(docWidth, docHeight);
-            float dd = Math.max(width, height);
-            float scale = dd / d;
-            px = AffineTransform.getScaleInstance(scale, scale);
-        }
-        // take the AOI into account if any
-        if (hints.containsKey(ImageTranscoder.KEY_AOI)) {
-            Rectangle2D aoi = (Rectangle2D)hints.get(ImageTranscoder.KEY_AOI);
-            // transform the AOI into the image's coordinate system
-            aoi = px.createTransformedShape(aoi).getBounds2D();
-            AffineTransform mx = new AffineTransform();
-            double sx = width / aoi.getWidth();
-            double sy = height / aoi.getHeight();
-            mx.scale(sx, sy);
-            double tx = -aoi.getX();
-            double ty = -aoi.getY();
-            mx.translate(tx, ty);
-            // take the AOI transformation matrix into account
-            // we apply first the preserveAspectRatio matrix
-            px.preConcatenate(mx);
-        }
-        // prepare the image to be painted
-        int w = (int)width;
-        int h = (int)height;
+            this.root.paint(graphics);
 
-        try {
-            graphics.setupDocument(output.getOutputStream(), w, h);
+            graphics.finish();
         } catch (IOException ex) {
             throw new TranscoderException(ex);
         }
-        graphics.setSVGDimension(docWidth, docHeight);
-        currentTransform.setTransform(1, 0, 0, -1, 0, height);
+    }
+
+    protected BridgeContext createBridgeContext() {
+        /*boolean stroke = true;
+        if (hints.containsKey(KEY_STROKE_TEXT)) {
+            stroke = ((Boolean)hints.get(KEY_STROKE_TEXT)).booleanValue();
+        }*/
+
+        BridgeContext ctx = new BridgeContext(userAgent);
+        TextPainter textPainter = null;
+        textPainter = new StrokingTextPainter();
+        ctx.setTextPainter(textPainter);
         /*if (!stroke) {
             textPainter = new PDFTextPainter(graphics.getFontInfo());
             ctx.setTextPainter(textPainter);
         }*/
 
-        if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) {
-            graphics.setBackgroundColor((Color)hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR));
-        }
-        graphics.setGraphicContext(new org.apache.batik.ext.awt.g2d.GraphicContext());
-        graphics.setTransform(px);
-
-        gvtRoot.paint(graphics);
+        PDFTextElementBridge pdfTextElementBridge;
+        pdfTextElementBridge = new PDFTextElementBridge(graphics.getFontInfo());
+        ctx.putBridge(pdfTextElementBridge);
 
-        try {
-            graphics.finish();
-        } catch (IOException ex) {
-            throw new TranscoderException(ex);
-        }
+        PDFAElementBridge pdfAElementBridge = new PDFAElementBridge();
+        AffineTransform currentTransform = new AffineTransform(1, 0, 0, 1, 0, 0);
+        pdfAElementBridge.setCurrentTransform(currentTransform);
+        ctx.putBridge(pdfAElementBridge);
+        ctx.putBridge(new PDFImageElementBridge());
+        return ctx;
     }
 
 }