From: Jeremias Maerki Date: Thu, 10 Nov 2005 16:37:06 +0000 (+0000) Subject: Moved PDFBridgeContext to a separate class so it can be used by both the PDFTranscode... X-Git-Tag: fop-0_90-alpha1~61 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a887507a4088b5374c2ec395396d48f641ea775f;p=xmlgraphics-fop.git Moved PDFBridgeContext to a separate class so it can be used by both the PDFTranscoder and the PDFSVGHandler. Added support for higher-resolution on-the-fly bitmaps from Batik for SVG inside FO documents. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@332324 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java b/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java index 2380738e1..324c89068 100644 --- a/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java +++ b/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java @@ -21,10 +21,12 @@ package org.apache.fop.render.pdf; import org.apache.fop.render.XMLHandler; import org.apache.fop.render.RendererContext; import org.apache.fop.pdf.PDFDocument; +import org.apache.fop.pdf.PDFNumber; import org.apache.fop.pdf.PDFPage; import org.apache.fop.pdf.PDFState; import org.apache.fop.pdf.PDFStream; import org.apache.fop.pdf.PDFResourceContext; +import org.apache.fop.svg.PDFBridgeContext; import org.apache.fop.svg.PDFTextElementBridge; import org.apache.fop.svg.PDFAElementBridge; import org.apache.fop.svg.PDFGraphics2D; @@ -41,12 +43,14 @@ import org.apache.commons.logging.LogFactory; import java.io.OutputStream; +import org.apache.batik.bridge.Bridge; import org.apache.batik.bridge.GVTBuilder; import org.apache.batik.bridge.BridgeContext; import org.apache.batik.bridge.ViewBox; import org.apache.batik.dom.svg.SVGDOMImplementation; import org.apache.batik.gvt.GraphicsNode; +import org.apache.batik.util.SVGConstants; import org.w3c.dom.svg.SVGDocument; import org.w3c.dom.svg.SVGSVGElement; @@ -222,22 +226,26 @@ public class PDFSVGHandler implements XMLHandler { int xOffset = pdfInfo.currentXPosition; int yOffset = pdfInfo.currentYPosition; - SVGUserAgent ua - = new SVGUserAgent(context.getUserAgent().getPixelUnitToMillimeter(), - new AffineTransform()); + log.debug("Generating SVG at " + + context.getUserAgent().getResolution() + + "dpi."); + + final int uaResolution = 72; //Should not be changed + final float deviceResolution + = context.getUserAgent().getResolution(); + SVGUserAgent ua = new SVGUserAgent(25.4f / uaResolution, new AffineTransform()); GVTBuilder builder = new GVTBuilder(); - BridgeContext ctx = new BridgeContext(ua); - PDFTextElementBridge tBridge = new PDFTextElementBridge(pdfInfo.fi); - ctx.putBridge(tBridge); - - PDFAElementBridge aBridge = new PDFAElementBridge(); - // to get the correct transform we need to use the PDFState - AffineTransform transform = pdfInfo.pdfState.getTransform(); - transform.translate(xOffset / 1000f, yOffset / 1000f); - aBridge.setCurrentTransform(transform); - ctx.putBridge(aBridge); - + + //TODO This AffineTransform here has to be fixed!!! + AffineTransform linkTransform = pdfInfo.pdfState.getTransform(); + linkTransform.translate(xOffset / 1000f, yOffset / 1000f); + + final boolean strokeText = false; + BridgeContext ctx = new PDFBridgeContext(ua, + (strokeText ? null : pdfInfo.fi), + linkTransform); + GraphicsNode root; try { root = builder.build(ctx, doc); @@ -290,9 +298,19 @@ public class PDFSVGHandler implements XMLHandler { pdfInfo.currentFontSize); graphics.setGraphicContext(new org.apache.batik.ext.awt.g2d.GraphicContext()); pdfInfo.pdfState.push(); - transform = new AffineTransform(); + AffineTransform transform = new AffineTransform(); // scale to viewbox transform.translate(xOffset / 1000f, yOffset / 1000f); + + if (deviceResolution != uaResolution) { + //Scale for higher resolution on-the-fly images from Batik + double s = uaResolution / deviceResolution; + at.scale(s, s); + pdfInfo.currentStream.add("" + PDFNumber.doubleOut(s) + " 0 0 " + + PDFNumber.doubleOut(s) + " 0 0 cm\n"); + graphics.scale(1 / s, 1 / s); + } + pdfInfo.pdfState.setTransform(transform); graphics.setPDFState(pdfInfo.pdfState); graphics.setOutputStream(pdfInfo.outputStream); diff --git a/src/java/org/apache/fop/svg/PDFBridgeContext.java b/src/java/org/apache/fop/svg/PDFBridgeContext.java new file mode 100644 index 000000000..486cadb97 --- /dev/null +++ b/src/java/org/apache/fop/svg/PDFBridgeContext.java @@ -0,0 +1,80 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.svg; + +import java.awt.geom.AffineTransform; + +import org.apache.batik.bridge.BridgeContext; +import org.apache.batik.bridge.UserAgent; +import org.apache.fop.fonts.FontInfo; + +/** + * BridgeContext which registers the custom bridges for PDF output. + */ +public class PDFBridgeContext extends BridgeContext { + + /** The font list. */ + private final FontInfo fontInfo; + + private AffineTransform linkTransform; + + /** + * Constructs a new bridge context. + * @param userAgent the user agent + * @param fontInfo the font list for the text painter, may be null in which case text is + * painted as shapes + * @param linkTransform AffineTransform to properly place links, may be null + */ + public PDFBridgeContext(UserAgent userAgent, FontInfo fontInfo, + AffineTransform linkTransform) { + super(userAgent); + this.fontInfo = fontInfo; + this.linkTransform = linkTransform; + } + + /** + * Constructs a new bridge context. + * @param userAgent the user agent + * @param fontInfo the font list for the text painter, may be null in which case text is + * painted as shapes + */ + public PDFBridgeContext(UserAgent userAgent, FontInfo fontInfo) { + this(userAgent, fontInfo, null); + } + + /** @see org.apache.batik.bridge.BridgeContext#registerSVGBridges() */ + public void registerSVGBridges() { + super.registerSVGBridges(); + + if (fontInfo != null) { + putBridge(new PDFTextElementBridge(fontInfo)); + } + + PDFAElementBridge pdfAElementBridge = new PDFAElementBridge(); + if (linkTransform != null) { + pdfAElementBridge.setCurrentTransform(linkTransform); + } else { + pdfAElementBridge.setCurrentTransform(new AffineTransform()); + } + putBridge(pdfAElementBridge); + + putBridge(new PDFImageElementBridge()); + } + +} \ No newline at end of file diff --git a/src/java/org/apache/fop/svg/PDFTranscoder.java b/src/java/org/apache/fop/svg/PDFTranscoder.java index a07b1c9f5..b9e2e1215 100644 --- a/src/java/org/apache/fop/svg/PDFTranscoder.java +++ b/src/java/org/apache/fop/svg/PDFTranscoder.java @@ -19,7 +19,6 @@ package org.apache.fop.svg; import java.awt.Color; -import java.awt.geom.AffineTransform; import java.io.IOException; import org.apache.avalon.framework.configuration.Configurable; @@ -180,47 +179,8 @@ public class PDFTranscoder extends AbstractFOPTranscoder /** @see org.apache.batik.transcoder.SVGAbstractTranscoder#createBridgeContext() */ protected BridgeContext createBridgeContext() { - BridgeContext ctx = new PDFBridgeContext(userAgent); - + BridgeContext ctx = new PDFBridgeContext(userAgent, graphics.getFontInfo()); return ctx; } - /** - * BridgeContext which registers the custom bridges for PDF output. - */ - public class PDFBridgeContext extends BridgeContext { - /** - * Constructs a new bridge context. - * @param userAgent the user agent - */ - public PDFBridgeContext(UserAgent userAgent) { - super(userAgent); - } - - /** @see org.apache.batik.bridge.BridgeContext#registerSVGBridges() */ - public void registerSVGBridges() { - super.registerSVGBridges(); - - /* - boolean stroke = true; - if (hints.containsKey(KEY_STROKE_TEXT)) { - stroke = ((Boolean)hints.get(KEY_STROKE_TEXT)).booleanValue(); - } - if (!stroke) { - textPainter = new PDFTextPainter(graphics.getFontInfo()); - ctx.setTextPainter(textPainter); - } - */ - - putBridge(new PDFTextElementBridge(graphics.getFontInfo())); - - PDFAElementBridge pdfAElementBridge = new PDFAElementBridge(); - pdfAElementBridge.setCurrentTransform(new AffineTransform()); - putBridge(pdfAElementBridge); - - putBridge(new PDFImageElementBridge()); - } - - } - }