aboutsummaryrefslogtreecommitdiffstats
path: root/src/sandbox
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2005-12-16 14:40:20 +0000
committerJeremias Maerki <jeremias@apache.org>2005-12-16 14:40:20 +0000
commitcd600b952b6326195b1670e6a17af75d51f46b57 (patch)
tree814b0f854cb41391f498b25a3412a8ad53d76f96 /src/sandbox
parent811fd3eefbfb06cb945bb3599523b7f3a15f251e (diff)
downloadxmlgraphics-fop-cd600b952b6326195b1670e6a17af75d51f46b57.tar.gz
xmlgraphics-fop-cd600b952b6326195b1670e6a17af75d51f46b57.zip
XMLHandler interface changed so it reports whether it supports a particular Renderer instead of reporting the MIME type. One MIME type could be implemented by multiple Renderer implementations so conflicts could occur. Almost all XMLHandler implementations will have a dependency on a particular Renderer implementation.
XMLHandlers are now configurable. Standard XMLHandlers now get registered through service lookup. Simplification: XMLHandlers don't need nested classes for the handling functionality anymore. If Batik, for example, is not in the classpath it will already be detected while registering the XMLHandlers. The RendererContextConstants interface now standardizes some of the keys used in the RendererContext and helps with actually decoupling the XMLHandler implementations from the renderers. This is one step towards making Batik an optional dependency and making it possible to move the SVG functionality to a separate place later. Extracted the SVG XMLHandler functionality from the sandbox SVGRenderer into a separate class following the example of the other renderers. Bugfix in PSSVGHandler: Fixed a copy/paste error which could lead to wrong clipping of an SVG image for PostScript output. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@357166 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/sandbox')
-rw-r--r--src/sandbox/org/apache/fop/render/svg/SVGRenderer.java79
-rw-r--r--src/sandbox/org/apache/fop/render/svg/SVGRendererContextConstants.java34
-rw-r--r--src/sandbox/org/apache/fop/render/svg/SVGSVGHandler.java80
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;
+ }
+
+
+}