KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > svg > SVGElement


1 /*
2  * $Id: SVGElement.java,v 1.16.2.8 2003/04/11 10:09:16 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.svg;
52
53 // FOP
54
import org.apache.fop.fo.*;
55 import org.apache.fop.layout.Area;
56 import org.apache.fop.layout.FontState;
57 import org.apache.fop.apps.FOPException;
58 import org.apache.fop.layout.inline.*;
59 import org.apache.fop.configuration.Configuration;
60
61 import org.apache.batik.dom.svg.*;
62 import org.apache.batik.dom.util.XMLSupport;
63 import org.w3c.dom.*;
64 import org.w3c.dom.svg.*;
65 import org.apache.batik.bridge.*;
66
67 import org.w3c.dom.DOMImplementation JavaDoc;
68 import org.apache.batik.dom.svg.SVGDOMImplementation;
69
70 import java.net.URL JavaDoc;
71 import java.awt.geom.AffineTransform JavaDoc;
72 import java.awt.geom.Rectangle2D JavaDoc;
73
74 /**
75  * class representing svg:svg pseudo flow object.
76  */

77 public class SVGElement extends SVGObj {
78
79     /**
80      * inner class for making SVG objects.
81      */

82     public static class Maker extends FObj.Maker {
83
84         /**
85          * make an SVG object.
86          *
87          * @param parent the parent formatting object
88          * @param propertyList the explicit properties of this object
89          *
90          * @return the SVG object
91          */

92         public FObj make(FObj parent, PropertyList propertyList,
93                          String JavaDoc systemId, int line, int column)
94             throws FOPException {
95             return new SVGElement(parent, propertyList,
96                                   systemId, line, column);
97         }
98     }
99
100     /**
101      * returns the maker for this object.
102      *
103      * @return the maker for SVG objects
104      */

105     public static FObj.Maker maker() {
106         return new SVGElement.Maker();
107     }
108
109     FontState fs;
110
111     /**
112      * constructs an SVG object (called by Maker).
113      *
114      * @param parent the parent formatting object
115      * @param propertyList the explicit properties of this object
116      */

117     public SVGElement(FObj parent, PropertyList propertyList,
118                       String JavaDoc systemId, int line, int column) {
119         super(parent, propertyList, "svg", systemId, line, column);
120         init();
121     }
122
123     /**
124      * layout this formatting object.
125      *
126      * @param area the area to layout the object into
127      *
128      * @return the status of the layout
129      */

130     public int layout(final Area area) throws FOPException {
131
132         if (!(area instanceof ForeignObjectArea)) {
133             // this is an error
134
throw new FOPException("SVG not in fo:instream-foreign-object");
135         }
136
137         if (this.marker == START) {
138             this.fs = area.getFontState();
139
140             this.marker = 0;
141         }
142
143         final Element svgRoot = element;
144         /* create an SVG area */
145         /* if width and height are zero, get the bounds of the content. */
146         final ForeignObjectArea foa = (ForeignObjectArea)area;
147         SVGContext dc = new SVGContext() {
148             public float getPixelToMM() {
149                 // 72 dpi
150
return 0.35277777777777777778f;
151             }
152
153             public float getPixelUnitToMillimeter() {
154                 // 72 dpi
155
return 0.35277777777777777778f;
156             }
157
158             public Rectangle2D JavaDoc getBBox() {
159                 return new Rectangle2D.Double JavaDoc(0, 0, foa.getContentWidth(), foa.getContentHeight());
160             }
161
162             /**
163              * Returns the transform from the global transform space to pixels.
164              */

165             public AffineTransform JavaDoc getScreenTransform() {
166                 throw new UnsupportedOperationException JavaDoc("NYI");
167             }
168
169             /**
170              * Sets the transform to be used from the global transform space
171              * to pixels.
172              */

173             public void setScreenTransform(AffineTransform JavaDoc at) {
174                 throw new UnsupportedOperationException JavaDoc("NYI");
175             }
176
177             public AffineTransform JavaDoc getCTM() {
178                 return new AffineTransform JavaDoc();
179             }
180
181             public AffineTransform JavaDoc getGlobalTransform() {
182                 return new AffineTransform JavaDoc();
183             }
184
185             public float getViewportWidth() {
186                 return (float)foa.getContentWidth();
187             }
188
189             public float getViewportHeight() {
190                 return (float)foa.getContentHeight();
191             }
192
193             public float getFontSize(){
194                 return fs.getFontSize() / 1000f;
195             }
196         };
197         ((SVGOMElement)svgRoot).setSVGContext(dc);
198
199         try {
200             URL JavaDoc baseURL = Configuration.getBaseURL();
201             if (baseURL != null) {
202                 ((SVGOMDocument)doc).setURLObject(baseURL);
203             }
204         } catch (Exception JavaDoc e) {
205             log.error("Could not set base URL for svg", e);
206         }
207
208         Element e = ((SVGDocument)doc).getRootElement();
209
210         //if(!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
211
e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI);
212         //}
213

214         String JavaDoc s;
215         SVGUserAgent userAgent = new SVGUserAgent(new AffineTransform JavaDoc());
216         userAgent.setLogger(log);
217         BridgeContext ctx = new BridgeContext(userAgent);
218         UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
219
220         // 'width' attribute - default is 100%
221
s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE);
222         if (s.length() == 0) {
223             s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE;
224         }
225         float width = UnitProcessor.svgHorizontalLengthToUserSpace(s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx);
226
227         // 'height' attribute - default is 100%
228
s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE);
229         if (s.length() == 0) {
230             s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE;
231         }
232         float height = UnitProcessor.svgVerticalLengthToUserSpace(s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx);
233
234         SVGArea svg = new SVGArea(fs, width, height);
235         svg.setSVGDocument(doc);
236         svg.start();
237
238         /* finish off the SVG area */
239         svg.end();
240
241         /* add the SVG area to the containing area */
242         foa.setObject(svg);
243         foa.setIntrinsicWidth(svg.getWidth());
244         foa.setIntrinsicHeight(svg.getHeight());
245
246        ((SVGOMElement)svgRoot).setSVGContext(null);
247
248         /* return status */
249         return Status.OK;
250     }
251
252     private void init() {
253         DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
254         String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
255         doc = impl.createDocument(svgNS, "svg", null);
256
257         element = doc.getDocumentElement();
258
259         buildTopLevel(doc, element);
260     }
261
262 }
263
Popular Tags