aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/java2d/Java2DSVGHandler.java
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/java/org/apache/fop/render/java2d/Java2DSVGHandler.java
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/java/org/apache/fop/render/java2d/Java2DSVGHandler.java')
-rw-r--r--src/java/org/apache/fop/render/java2d/Java2DSVGHandler.java185
1 files changed, 75 insertions, 110 deletions
diff --git a/src/java/org/apache/fop/render/java2d/Java2DSVGHandler.java b/src/java/org/apache/fop/render/java2d/Java2DSVGHandler.java
index fbf8382eb..c7fc7e9d3 100644
--- a/src/java/org/apache/fop/render/java2d/Java2DSVGHandler.java
+++ b/src/java/org/apache/fop/render/java2d/Java2DSVGHandler.java
@@ -18,79 +18,50 @@
package org.apache.fop.render.java2d;
+import java.awt.geom.AffineTransform;
+
+import org.w3c.dom.Document;
+
+import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.render.Renderer;
import org.apache.fop.render.XMLHandler;
import org.apache.fop.render.RendererContext;
+import org.apache.fop.render.pdf.PDFRenderer;
import org.apache.fop.svg.SVGUserAgent;
// Commons-Logging
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-/* org.w3c.dom.Document is not imported to avoid conflict with
- org.apache.fop.apps.Document */
-
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.gvt.GraphicsNode;
-import java.awt.geom.AffineTransform;
-
/**
* Java2D XML handler for SVG (uses Apache Batik).
* This handler handles XML for foreign objects when rendering to Java2D.
* The properties from the Java2D renderer are subject to change.
*/
-public class Java2DSVGHandler implements XMLHandler {
-
- /**
- * logging instance
- */
- private Log log = LogFactory.getLog(Java2DSVGHandler.class);
-
- /**
- * The current Java2DGraphicsState.
- */
- public static final String JAVA2D_STATE = "state";
-
- /**
- * The width of the svg image/document to render.
- */
- public static final String JAVA2D_WIDTH = "width";
+public class Java2DSVGHandler implements XMLHandler, Java2DRendererContextConstants {
- /**
- * The height of the svg image/document to render.
- */
- public static final String JAVA2D_HEIGHT = "height";
-
- /**
- * The x position that this is being drawn at.
- */
- public static final String JAVA2D_XPOS = "xpos";
+ /** logging instance */
+ private static Log log = LogFactory.getLog(Java2DSVGHandler.class);
/**
- * The y position that this is being drawn at.
+ * Create a new Java2D XML handler for use by the Java2D renderer and its subclasses.
*/
- public static final String JAVA2D_YPOS = "ypos";
-
- private String mimeType;
-
- /**
- * Create a new Java2D XML handler for use by the Java2D renderer.
- * @param mime MIME type that this handler is used for
- */
- public Java2DSVGHandler(String mime) {
- this.mimeType = mime;
+ public Java2DSVGHandler() {
+ //nop
}
/** @see org.apache.fop.render.XMLHandler */
public void handleXML(RendererContext context,
- org.w3c.dom.Document doc, String ns) throws Exception {
+ Document doc, String ns) throws Exception {
Java2DInfo pdfi = getJava2DInfo(context);
if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
- SVGHandler svghandler = new SVGHandler();
- svghandler.renderSVGDocument(context, doc, pdfi);
+ renderSVGDocument(context, doc, pdfi);
}
}
@@ -103,10 +74,10 @@ public class Java2DSVGHandler implements XMLHandler {
public static Java2DInfo getJava2DInfo(RendererContext context) {
Java2DInfo pdfi = new Java2DInfo();
pdfi.state = (Java2DGraphicsState)context.getProperty(JAVA2D_STATE);
- pdfi.width = ((Integer)context.getProperty(JAVA2D_WIDTH)).intValue();
- pdfi.height = ((Integer)context.getProperty(JAVA2D_HEIGHT)).intValue();
- pdfi.currentXPosition = ((Integer)context.getProperty(JAVA2D_XPOS)).intValue();
- pdfi.currentYPosition = ((Integer)context.getProperty(JAVA2D_YPOS)).intValue();
+ pdfi.width = ((Integer)context.getProperty(WIDTH)).intValue();
+ pdfi.height = ((Integer)context.getProperty(HEIGHT)).intValue();
+ pdfi.currentXPosition = ((Integer)context.getProperty(XPOS)).intValue();
+ pdfi.currentYPosition = ((Integer)context.getProperty(YPOS)).intValue();
return pdfi;
}
@@ -137,75 +108,69 @@ public class Java2DSVGHandler implements XMLHandler {
}
/**
- * This method is placed in an inner class so that we don't get class
- * loading errors if batik is not present.
+ * Render the svg document.
+ * @param context the renderer context
+ * @param doc the svg document
+ * @param info the pdf information of the current context
*/
- protected class SVGHandler {
+ protected void renderSVGDocument(RendererContext context,
+ Document doc,
+ Java2DInfo info) {
+
+ log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")");
+
+ int x = info.currentXPosition;
+ int y = info.currentYPosition;
+
+ float ptom = context.getUserAgent().getSourcePixelUnitToMillimeter();
+ SVGUserAgent ua = new SVGUserAgent(ptom, new AffineTransform());
- /**
- * Render the svg document.
- * @param context the renderer context
- * @param doc the svg document
- * @param info the pdf information of the current context
- */
- protected void renderSVGDocument(RendererContext context,
- org.w3c.dom.Document doc,
- Java2DInfo info) {
-
- log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")");
-
- int x = info.currentXPosition;
- int y = info.currentYPosition;
-
- float ptom = context.getUserAgent().getSourcePixelUnitToMillimeter();
- SVGUserAgent ua = new SVGUserAgent(ptom, new AffineTransform());
-
- GVTBuilder builder = new GVTBuilder();
- BridgeContext ctx = new BridgeContext(ua);
-
- GraphicsNode root;
- try {
- root = builder.build(ctx, doc);
- } catch (Exception e) {
- log.error("SVG graphic could not be built: " + e.getMessage(), e);
- return;
- }
-
- // If no viewbox is defined in the svg file, a viewbox of 100x100 is
- // assumed, as defined in SVGUserAgent.getViewportSize()
- float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
- float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;
-
- float w = (float) info.width;
- float h = (float) info.height;
-
- AffineTransform origTransform = info.state.getGraph().getTransform();
-
- // correct integer roundoff
- info.state.getGraph().translate(x / 1000f, y / 1000f);
-
- //SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
- // Aspect ratio preserved by layout engine, not here
- AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih);
- if (!at.isIdentity()) {
- info.state.getGraph().transform(at);
- }
-
- try {
- root.paint(info.state.getGraph());
- } catch (Exception e) {
- log.error("Error while painting SVG", e);
- }
-
- info.state.getGraph().setTransform(origTransform);
+ GVTBuilder builder = new GVTBuilder();
+ BridgeContext ctx = new BridgeContext(ua);
+
+ GraphicsNode root;
+ try {
+ root = builder.build(ctx, doc);
+ } catch (Exception e) {
+ log.error("SVG graphic could not be built: " + e.getMessage(), e);
+ return;
}
+
+ // If no viewbox is defined in the svg file, a viewbox of 100x100 is
+ // assumed, as defined in SVGUserAgent.getViewportSize()
+ float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
+ float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;
+
+ float w = (float) info.width;
+ float h = (float) info.height;
+
+ AffineTransform origTransform = info.state.getGraph().getTransform();
+
+ // correct integer roundoff
+ info.state.getGraph().translate(x / 1000f, y / 1000f);
+
+ //SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
+ // Aspect ratio preserved by layout engine, not here
+ AffineTransform at = AffineTransform.getScaleInstance(w / iw, h / ih);
+ if (!at.isIdentity()) {
+ info.state.getGraph().transform(at);
+ }
+
+ try {
+ root.paint(info.state.getGraph());
+ } catch (Exception e) {
+ log.error("Error while painting SVG", e);
+ }
+
+ info.state.getGraph().setTransform(origTransform);
}
- /** @see org.apache.fop.render.XMLHandler#getMimeType() */
- public String getMimeType() {
- return this.mimeType;
+ /** @see org.apache.fop.render.XMLHandler#supportsRenderer() */
+ public boolean supportsRenderer(Renderer renderer) {
+ return (renderer instanceof Java2DRenderer);
}
+
/** @see org.apache.fop.render.XMLHandler#getNamespace() */
public String getNamespace() {
return SVGDOMImplementation.SVG_NAMESPACE_URI;