KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > java2d > Java2DSVGHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: Java2DSVGHandler.java 454018 2006-10-07 21:00:13Z pietsch $ */
19
20 package org.apache.fop.render.java2d;
21
22 import java.awt.geom.AffineTransform JavaDoc;
23
24 import org.w3c.dom.Document JavaDoc;
25
26 import org.apache.fop.render.Renderer;
27 import org.apache.fop.render.XMLHandler;
28 import org.apache.fop.render.RendererContext;
29 import org.apache.fop.svg.SVGUserAgent;
30
31 // Commons-Logging
32
import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import org.apache.batik.bridge.GVTBuilder;
36 import org.apache.batik.bridge.BridgeContext;
37 import org.apache.batik.dom.svg.SVGDOMImplementation;
38 import org.apache.batik.gvt.GraphicsNode;
39
40 /**
41  * Java2D XML handler for SVG (uses Apache Batik).
42  * This handler handles XML for foreign objects when rendering to Java2D.
43  * The properties from the Java2D renderer are subject to change.
44  */

45 public class Java2DSVGHandler implements XMLHandler, Java2DRendererContextConstants {
46
47     /** logging instance */
48     private static Log log = LogFactory.getLog(Java2DSVGHandler.class);
49
50     /**
51      * Create a new Java2D XML handler for use by the Java2D renderer and its subclasses.
52      */

53     public Java2DSVGHandler() {
54         //nop
55
}
56
57     /** @see org.apache.fop.render.XMLHandler */
58     public void handleXML(RendererContext context,
59                 Document JavaDoc doc, String JavaDoc ns) throws Exception JavaDoc {
60         Java2DInfo pdfi = getJava2DInfo(context);
61
62         if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
63             renderSVGDocument(context, doc, pdfi);
64         }
65     }
66
67     /**
68      * Get the pdf information from the render context.
69      *
70      * @param context the renderer context
71      * @return the pdf information retrieved from the context
72      */

73     public static Java2DInfo getJava2DInfo(RendererContext context) {
74         Java2DInfo pdfi = new Java2DInfo();
75         pdfi.state = (Java2DGraphicsState)context.getProperty(JAVA2D_STATE);
76         pdfi.width = ((Integer JavaDoc)context.getProperty(WIDTH)).intValue();
77         pdfi.height = ((Integer JavaDoc)context.getProperty(HEIGHT)).intValue();
78         pdfi.currentXPosition = ((Integer JavaDoc)context.getProperty(XPOS)).intValue();
79         pdfi.currentYPosition = ((Integer JavaDoc)context.getProperty(YPOS)).intValue();
80         return pdfi;
81     }
82
83     /**
84      * Java2D information structure for drawing the XML document.
85      */

86     public static class Java2DInfo {
87         /** see Java2D_STATE */
88         public Java2DGraphicsState state;
89         /** see Java2D_WIDTH */
90         public int width;
91         /** see Java2D_HEIGHT */
92         public int height;
93         /** see Java2D_XPOS */
94         public int currentXPosition;
95         /** see Java2D_YPOS */
96         public int currentYPosition;
97
98         /** @see java.lang.Object#toString() */
99         public String JavaDoc toString() {
100             return "Java2DInfo {"
101                 + "state = " + state + ", "
102                 + "width = " + width + ", "
103                 + "height = " + height + ", "
104                 + "currentXPosition = " + currentXPosition + ", "
105                 + "currentYPosition = " + currentYPosition + "}";
106         }
107     }
108     
109     /**
110      * Render the svg document.
111      * @param context the renderer context
112      * @param doc the svg document
113      * @param info the pdf information of the current context
114      */

115     protected void renderSVGDocument(RendererContext context,
116                                      Document JavaDoc doc,
117                                      Java2DInfo info) {
118
119         log.debug("renderSVGDocument(" + context + ", " + doc + ", " + info + ")");
120         
121         int x = info.currentXPosition;
122         int y = info.currentYPosition;
123         
124         float ptom = context.getUserAgent().getSourcePixelUnitToMillimeter();
125         SVGUserAgent ua = new SVGUserAgent(ptom, new AffineTransform JavaDoc());
126         
127         GVTBuilder builder = new GVTBuilder();
128         BridgeContext ctx = new BridgeContext(ua);
129         
130         GraphicsNode root;
131         try {
132             root = builder.build(ctx, doc);
133         } catch (Exception JavaDoc e) {
134             log.error("SVG graphic could not be built: " + e.getMessage(), e);
135             return;
136         }
137         
138         // If no viewbox is defined in the svg file, a viewbox of 100x100 is
139
// assumed, as defined in SVGUserAgent.getViewportSize()
140
float iw = (float) ctx.getDocumentSize().getWidth() * 1000f;
141         float ih = (float) ctx.getDocumentSize().getHeight() * 1000f;
142         
143         float w = (float) info.width;
144         float h = (float) info.height;
145
146         AffineTransform JavaDoc origTransform = info.state.getGraph().getTransform();
147         
148         // correct integer roundoff
149
info.state.getGraph().translate(x / 1000f, y / 1000f);
150         
151         //SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
152
// Aspect ratio preserved by layout engine, not here
153
AffineTransform JavaDoc at = AffineTransform.getScaleInstance(w / iw, h / ih);
154         if (!at.isIdentity()) {
155             info.state.getGraph().transform(at);
156         }
157
158         try {
159             root.paint(info.state.getGraph());
160         } catch (Exception JavaDoc e) {
161             log.error("Error while painting SVG", e);
162         }
163         
164         info.state.getGraph().setTransform(origTransform);
165     }
166     
167     /** @see org.apache.fop.render.XMLHandler#supportsRenderer(Renderer) */
168     public boolean supportsRenderer(Renderer renderer) {
169         return (renderer instanceof Java2DRenderer);
170     }
171
172
173     /** @see org.apache.fop.render.XMLHandler#getNamespace() */
174     public String JavaDoc getNamespace() {
175         return SVGDOMImplementation.SVG_NAMESPACE_URI;
176     }
177 }
178
Popular Tags