gmazza 2003/10/11 07:48:49 Modified: lib batik.jar src/java/org/apache/fop/render/ps PSTranscoder.java src/java/org/apache/fop/svg AbstractFOPTranscoder.java PDFImageElementBridge.java PDFTranscoder.java Log: Applied Tom DeWeese/Batik Team's SVG patches. New Batik.jar created from latest CVS build, using Batik's "all-jar" target. Revision Changes Path 1.11 +2593 -2580xml-fop/lib/batik.jar <<Binary file>> 1.6 +31 -107 xml-fop/src/java/org/apache/fop/render/ps/PSTranscoder.java Index: PSTranscoder.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/render/ps/PSTranscoder.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- PSTranscoder.java 8 Sep 2003 17:00:56 -0000 1.5 +++ PSTranscoder.java 11 Oct 2003 14:48:49 -0000 1.6 @@ -116,6 +116,8 @@ */ public class PSTranscoder extends AbstractFOPTranscoder { + protected PSDocumentGraphics2D graphics = null; + /** * Constructs a new <tt>PSTranscoder</tt>. */ @@ -132,21 +134,38 @@ * @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); - // build the GVT tree - GVTBuilder builder = new GVTBuilder(); + graphics.finish(); + } catch (IOException ex) { + throw new TranscoderException(ex); + } + } + + protected BridgeContext createBridgeContext() { BridgeContext ctx = new BridgeContext(userAgent); TextPainter textPainter = null; textPainter = new StrokingTextPainter(); @@ -162,102 +181,7 @@ //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; } } 1.2 +2 -2 xml-fop/src/java/org/apache/fop/svg/AbstractFOPTranscoder.java Index: AbstractFOPTranscoder.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/svg/AbstractFOPTranscoder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractFOPTranscoder.java 27 Mar 2003 11:21:11 -0000 1.1 +++ AbstractFOPTranscoder.java 11 Oct 2003 14:48:49 -0000 1.2 @@ -65,7 +65,7 @@ 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 @@ /** * 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 { /** 1.3 +33 -16 xml-fop/src/java/org/apache/fop/svg/PDFImageElementBridge.java Index: PDFImageElementBridge.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/svg/PDFImageElementBridge.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PDFImageElementBridge.java 2 Jun 2003 20:20:56 -0000 1.2 +++ PDFImageElementBridge.java 11 Oct 2003 14:48:49 -0000 1.3 @@ -53,12 +53,20 @@ 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 <image> element when jpeg images. @@ -72,7 +80,6 @@ */ public PDFImageElementBridge() { } -/* /** * Create the raster image node. * THis checks if it is a jpeg file and creates a jpeg node @@ -81,14 +88,19 @@ * @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 @@ 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 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 @@ } catch (Exception e) { e.printStackTrace(); } + } else { + // Not going directly into PDF so use + // original implemtation so filters etc work. + origGraphicsNode.primitivePaint(g2d); } } 1.7 +40 -121 xml-fop/src/java/org/apache/fop/svg/PDFTranscoder.java Index: PDFTranscoder.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/svg/PDFTranscoder.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- PDFTranscoder.java 8 Sep 2003 17:00:56 -0000 1.6 +++ PDFTranscoder.java 11 Oct 2003 14:48:49 -0000 1.7 @@ -107,7 +107,8 @@ 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 @@ * @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 @@ "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); + super.transcode(document, uri, output); - PDFTextElementBridge pdfTextElementBridge; - pdfTextElementBridge = new PDFTextElementBridge(graphics.getFontInfo()); - ctx.putBridge(pdfTextElementBridge); + // prepare the image to be painted + int w = (int)(width+.5); + int h = (int)(height+.5); - 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; - - // 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; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]