KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > AbstractGenericSVGHandler


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$ */
19
20 package org.apache.fop.render;
21
22 // Java
23
import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.geom.AffineTransform JavaDoc;
26 import java.awt.geom.Rectangle2D JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 // DOM
30
import org.w3c.dom.Document JavaDoc;
31
32 // Batik
33
import org.apache.batik.bridge.GVTBuilder;
34 import org.apache.batik.bridge.BridgeContext;
35 import org.apache.batik.dom.svg.SVGDOMImplementation;
36 import org.apache.batik.gvt.GraphicsNode;
37
38 // FOP
39
import org.apache.fop.render.Graphics2DAdapter;
40 import org.apache.fop.render.Graphics2DImagePainter;
41 import org.apache.fop.render.RendererContextConstants;
42 import org.apache.fop.render.XMLHandler;
43 import org.apache.fop.render.RendererContext;
44 import org.apache.fop.render.RendererContext.RendererContextWrapper;
45 import org.apache.fop.svg.SVGUserAgent;
46
47 // Commons-Logging
48
import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51 /**
52  * Generic XML handler for SVG. Uses Apache Batik for SVG processing and simply paints to
53  * a Graphics2DAdapter and thus ultimatively to Graphics2D interface that is presented.
54  * <p>
55  * To use this class, subclass it and implement the missing methods (supportsRenderer, for example).
56  */

57 public abstract class AbstractGenericSVGHandler implements XMLHandler, RendererContextConstants {
58
59     /** logging instance */
60     private static Log log = LogFactory.getLog(AbstractGenericSVGHandler.class);
61
62     /** @see org.apache.fop.render.XMLHandler */
63     public void handleXML(RendererContext context,
64                 Document JavaDoc doc, String JavaDoc ns) throws Exception JavaDoc {
65
66         if (SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns)) {
67             renderSVGDocument(context, doc);
68         }
69     }
70
71     /**
72      * Render the SVG document.
73      * @param context the renderer context
74      * @param doc the SVG document
75      * @throws IOException In case of an I/O error while painting the image
76      */

77     protected void renderSVGDocument(final RendererContext context,
78             final Document JavaDoc doc) throws IOException JavaDoc {
79         final RendererContextWrapper wrappedContext = RendererContext.wrapRendererContext(context);
80         int x = wrappedContext.getCurrentXPosition();
81         int y = wrappedContext.getCurrentYPosition();
82
83         //Prepare
84
SVGUserAgent ua = new SVGUserAgent(
85                 context.getUserAgent().getSourcePixelUnitToMillimeter(),
86                 new AffineTransform JavaDoc());
87         GVTBuilder builder = new GVTBuilder();
88         final BridgeContext ctx = new BridgeContext(ua);
89
90         //Build the GVT tree
91
final GraphicsNode root;
92         try {
93             root = builder.build(ctx, doc);
94         } catch (Exception JavaDoc e) {
95             log.error("SVG graphic could not be built: " + e.getMessage(), e);
96             return;
97         }
98
99         //Create the painter
100
Graphics2DImagePainter painter = new Graphics2DImagePainter() {
101
102             public void paint(Graphics2D JavaDoc g2d, Rectangle2D JavaDoc area) {
103                 // If no viewbox is defined in the svg file, a viewbox of 100x100 is
104
// assumed, as defined in SVGUserAgent.getViewportSize()
105
float iw = (float) ctx.getDocumentSize().getWidth();
106                 float ih = (float) ctx.getDocumentSize().getHeight();
107                 float w = (float) area.getWidth();
108                 float h = (float) area.getHeight();
109                 g2d.scale(w / iw, h / ih);
110
111                 root.paint(g2d);
112             }
113
114             public Dimension JavaDoc getImageSize() {
115                 return new Dimension JavaDoc(wrappedContext.getWidth(), wrappedContext.getHeight());
116             }
117
118         };
119
120         //Let the painter paint the SVG on the Graphics2D instance
121
Graphics2DAdapter adapter = context.getRenderer().getGraphics2DAdapter();
122         adapter.paintImage(painter, context,
123                 x, y, wrappedContext.getWidth(), wrappedContext.getHeight());
124     }
125
126     /** @see org.apache.fop.render.XMLHandler#getNamespace() */
127     public String JavaDoc getNamespace() {
128         return SVGDOMImplementation.SVG_NAMESPACE_URI;
129     }
130
131 }
132
133
Popular Tags