diff options
Diffstat (limited to 'src/sandbox')
3 files changed, 150 insertions, 43 deletions
diff --git a/src/sandbox/org/apache/fop/render/svg/SVGRenderer.java b/src/sandbox/org/apache/fop/render/svg/SVGRenderer.java index 7a2b182d7..0b5d93f3d 100644 --- a/src/sandbox/org/apache/fop/render/svg/SVGRenderer.java +++ b/src/sandbox/org/apache/fop/render/svg/SVGRenderer.java @@ -29,21 +29,17 @@ import org.apache.fop.svg.SVGUtilities; import org.apache.fop.fonts.FontInfo; import org.apache.fop.apps.FOUserAgent; -import org.w3c.dom.Node; -import org.w3c.dom.svg.SVGSVGElement; -import org.w3c.dom.svg.SVGDocument; /* org.w3c.dom.Document is not imported to avoid conflict with org.apache.fop.control.Document */ import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; import org.apache.batik.dom.svg.SVGDOMImplementation; -import org.apache.batik.dom.util.XMLSupport; import org.apache.batik.transcoder.svg2svg.SVGTranscoder; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.TranscoderException; -import org.apache.batik.dom.util.DOMUtilities; import java.awt.Color; import java.awt.image.BufferedImage; @@ -60,7 +56,7 @@ import org.apache.fop.render.RendererContext; /** * This is the SVG renderer. */ -public class SVGRenderer extends AbstractRenderer implements XMLHandler { +public class SVGRenderer extends AbstractRenderer { /** SVG MIME type */ public static final String SVG_MIME_TYPE = "image/svg+xml"; @@ -81,8 +77,6 @@ public class SVGRenderer extends AbstractRenderer implements XMLHandler { // first sequence title private LineArea docTitle = null; - private RendererContext context; - private OutputStream ostream; private float totalWidth = 0; @@ -119,7 +113,6 @@ public class SVGRenderer extends AbstractRenderer implements XMLHandler { * Creates a new SVG renderer. */ public SVGRenderer() { - context = new RendererContext(this, SVG_MIME_TYPE); } /** @@ -127,7 +120,15 @@ public class SVGRenderer extends AbstractRenderer implements XMLHandler { */ public void setUserAgent(FOUserAgent agent) { super.setUserAgent(agent); - userAgent.getXMLHandlerRegistry().addXMLHandler(this); + + //Note: This is done here as having two service lookup files in the same IDE project + //will end up with one overwriting the other when all sources are compiled in to the + //same target directory. Remove this code and add an entry in the XMLHandler resource + //file when this renderer exits the sandbox. + XMLHandler handler = agent.getXMLHandlerRegistry().getXMLHandler(this, SVG_NAMESPACE); + if (handler == null) { + agent.getXMLHandlerRegistry().addXMLHandler("org.apache.fop.render.svg.SVGSVGHandler"); + } } /** @@ -319,36 +320,33 @@ public class SVGRenderer extends AbstractRenderer implements XMLHandler { public void renderForeignObject(ForeignObject fo, Rectangle2D pos) { org.w3c.dom.Document doc = fo.getDocument(); String ns = fo.getNameSpace(); - renderXML(context, doc, ns); + renderDocument(doc, ns, pos); } - /** @see org.apache.fop.render.XMLHandler */ - public void handleXML(RendererContext context, - org.w3c.dom.Document doc, String ns) throws Exception { - if (SVG_NAMESPACE.equals(ns)) { - if (!(doc instanceof SVGDocument)) { - DOMImplementation impl = - SVGDOMImplementation.getDOMImplementation(); - doc = DOMUtilities.deepCloneDocument(doc, impl); - } - SVGSVGElement svg = ((SVGDocument) doc).getRootElement(); - Element view = svgDocument.createElementNS(SVG_NAMESPACE, "svg"); - Node newsvg = svgDocument.importNode(svg, true); - //view.setAttributeNS(null, "viewBox", "0 0 "); - view.setAttributeNS(null, "x", "" + currentIPPosition / 1000f); - view.setAttributeNS(null, "y", "" + currentBPPosition / 1000f); - - // this fixes a problem where the xmlns is repeated sometimes - Element ele = (Element) newsvg; - ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", - SVG_NAMESPACE); - if (ele.hasAttributeNS(null, "xmlns")) { - ele.removeAttributeNS(null, "xmlns"); - } - - view.appendChild(newsvg); - currentPageG.appendChild(view); - } + /** + * Renders an XML document (SVG for example). + * + * @param doc DOM document representing the XML document + * @param ns Namespace for the document + * @param pos Position on the page + */ + public void renderDocument(Document doc, String ns, Rectangle2D pos) { + RendererContext context; + context = new RendererContext(this, getMimeType()); + context.setUserAgent(userAgent); + + context.setProperty(SVGRendererContextConstants.SVG_DOCUMENT, svgDocument); + context.setProperty(SVGRendererContextConstants.SVG_PAGE_G, currentPageG); + context.setProperty(SVGRendererContextConstants.XPOS, + new Integer(currentIPPosition + (int)pos.getX())); + context.setProperty(SVGRendererContextConstants.YPOS, + new Integer(currentBPPosition + (int)pos.getY())); + context.setProperty(SVGRendererContextConstants.WIDTH, + new Integer((int)pos.getWidth())); + context.setProperty(SVGRendererContextConstants.HEIGHT, + new Integer((int) pos.getHeight())); + + renderXML(context, doc, ns); } /** @@ -419,11 +417,6 @@ public class SVGRenderer extends AbstractRenderer implements XMLHandler { return SVG_MIME_TYPE; } - /** @see org.apache.fop.render.XMLHandler#getNamespace() */ - public String getNamespace() { - return SVG_NAMESPACE; - } - /** * @see org.apache.fop.render.AbstractRenderer#startVParea(CTM, Rectangle2D) */ diff --git a/src/sandbox/org/apache/fop/render/svg/SVGRendererContextConstants.java b/src/sandbox/org/apache/fop/render/svg/SVGRendererContextConstants.java new file mode 100644 index 000000000..c86afb9ff --- /dev/null +++ b/src/sandbox/org/apache/fop/render/svg/SVGRendererContextConstants.java @@ -0,0 +1,34 @@ +/* + * 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.render.svg; + +import org.apache.fop.render.RendererContextConstants; + +/** + * Defines a number of standard constants (keys) for use by the RendererContext class. + */ +public interface SVGRendererContextConstants extends RendererContextConstants { + + /** The SVG document that this image is being drawn into. */ + String SVG_DOCUMENT = "svgDoc"; + + /** The current SVG page g element. */ + String SVG_PAGE_G = "svgPageG"; + +} diff --git a/src/sandbox/org/apache/fop/render/svg/SVGSVGHandler.java b/src/sandbox/org/apache/fop/render/svg/SVGSVGHandler.java new file mode 100644 index 000000000..47e1b8767 --- /dev/null +++ b/src/sandbox/org/apache/fop/render/svg/SVGSVGHandler.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.render.svg; + +import org.apache.batik.dom.svg.SVGDOMImplementation; +import org.apache.batik.dom.util.DOMUtilities; +import org.apache.batik.dom.util.XMLSupport; +import org.apache.fop.render.Renderer; +import org.apache.fop.render.RendererContext; +import org.apache.fop.render.XMLHandler; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.svg.SVGDocument; +import org.w3c.dom.svg.SVGElement; +import org.w3c.dom.svg.SVGSVGElement; + +public class SVGSVGHandler implements XMLHandler, SVGRendererContextConstants { + + /** @see org.apache.fop.render.XMLHandler */ + public void handleXML(RendererContext context, + org.w3c.dom.Document doc, String ns) throws Exception { + if (getNamespace().equals(ns)) { + if (!(doc instanceof SVGDocument)) { + DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); + doc = DOMUtilities.deepCloneDocument(doc, impl); + } + SVGSVGElement svg = ((SVGDocument) doc).getRootElement(); + SVGDocument targetDoc = (SVGDocument)context.getProperty(SVG_DOCUMENT); + SVGElement currentPageG = (SVGElement)context.getProperty(SVG_PAGE_G); + Element view = targetDoc.createElementNS(getNamespace(), "svg"); + Node newsvg = targetDoc.importNode(svg, true); + //view.setAttributeNS(null, "viewBox", "0 0 "); + int xpos = ((Integer)context.getProperty(XPOS)).intValue(); + int ypos = ((Integer)context.getProperty(YPOS)).intValue(); + view.setAttributeNS(null, "x", "" + xpos / 1000f); + view.setAttributeNS(null, "y", "" + ypos / 1000f); + + // this fixes a problem where the xmlns is repeated sometimes + Element ele = (Element) newsvg; + ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", + getNamespace()); + if (ele.hasAttributeNS(null, "xmlns")) { + ele.removeAttributeNS(null, "xmlns"); + } + + view.appendChild(newsvg); + currentPageG.appendChild(view); + } + } + + + /** @see org.apache.fop.render.XMLHandler#supportsRenderer(org.apache.fop.render.Renderer) */ + public boolean supportsRenderer(Renderer renderer) { + return (renderer instanceof SVGRenderer); + } + + /** @see org.apache.fop.render.XMLHandler#getNamespace() */ + public String getNamespace() { + return SVGRenderer.SVG_NAMESPACE; + } + + +} |