KickJava   Java API By Example, From Geeks To Geeks.

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


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: SVGElement.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fo.extensions.svg;
21
22 // FOP
23
import org.apache.fop.apps.FOPException;
24 import org.apache.fop.fo.FONode;
25 import org.apache.fop.fo.PropertyList;
26 import org.apache.fop.util.ContentHandlerFactory;
27 import org.apache.fop.util.DOMBuilderContentHandlerFactory;
28
29 import org.apache.batik.dom.svg.SVGOMDocument;
30 import org.apache.batik.dom.svg.SVGOMElement;
31 import org.apache.batik.dom.svg.SVGContext;
32 import org.apache.batik.dom.util.XMLSupport;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.svg.SVGDocument;
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.Locator JavaDoc;
37 import org.apache.batik.bridge.UnitProcessor;
38 import org.apache.batik.util.SVGConstants;
39
40 import org.w3c.dom.DOMImplementation JavaDoc;
41
42 import org.apache.batik.dom.svg.SVGDOMImplementation;
43
44 import java.net.URL JavaDoc;
45 import java.awt.geom.AffineTransform JavaDoc;
46 import java.awt.geom.Point2D JavaDoc;
47 import java.awt.geom.Rectangle2D JavaDoc;
48
49 /**
50  * class representing the SVG root element
51  * for constructing an svg document.
52  */

53 public class SVGElement extends SVGObj {
54
55     /**
56      * Constructs an SVG object
57      *
58      * @param parent the parent formatting object
59      */

60     public SVGElement(FONode parent) {
61         super(parent);
62     }
63
64     /**
65      * @see org.apache.fop.fo.FONode#getContentHandlerFactory()
66      */

67     public ContentHandlerFactory getContentHandlerFactory() {
68         return new DOMBuilderContentHandlerFactory(getNamespaceURI(),
69                 SVGDOMImplementation.getDOMImplementation());
70     }
71     
72     /**
73      * @see org.apache.fop.fo.FONode#processNode
74      */

75     public void processNode(String JavaDoc elementName, Locator JavaDoc locator,
76                             Attributes JavaDoc attlist, PropertyList propertyList) throws FOPException {
77         super.processNode(elementName, locator, attlist, propertyList);
78         init();
79     }
80
81     /**
82      * Get the dimensions of this XML document.
83      * @param view the viewport dimensions
84      * @return the dimensions of this SVG document
85      */

86     public Point2D JavaDoc getDimension(final Point2D JavaDoc view) {
87
88         // TODO change so doesn't hold onto fo, area tree
89
Element svgRoot = element;
90         /* create an SVG area */
91         /* if width and height are zero, get the bounds of the content. */
92
93         try {
94             URL JavaDoc baseURL = new URL JavaDoc(getUserAgent().getBaseURL() == null
95                             ? new java.io.File JavaDoc("").toURL().toExternalForm()
96                             : getUserAgent().getBaseURL());
97             if (baseURL != null) {
98                 SVGOMDocument svgdoc = (SVGOMDocument)doc;
99                 svgdoc.setURLObject(baseURL);
100                 //The following line should not be called to leave FOP compatible to Batik 1.6.
101
//svgdoc.setDocumentURI(baseURL.toString());
102
}
103         } catch (Exception JavaDoc e) {
104             getLogger().error("Could not set base URL for svg", e);
105         }
106
107         Element e = ((SVGDocument)doc).getRootElement();
108         final float ptmm = getUserAgent().getSourcePixelUnitToMillimeter();
109         // temporary svg context
110
SVGContext dc = new SVGContext() {
111             public float getPixelToMM() {
112                 return ptmm;
113             }
114             public float getPixelUnitToMillimeter() {
115                 return ptmm;
116             }
117
118             public Rectangle2D JavaDoc getBBox() {
119                 return new Rectangle2D.Double JavaDoc(0, 0, view.getX(), view.getY());
120             }
121
122             /**
123              * Returns the transform from the global transform space to pixels.
124              */

125             public AffineTransform JavaDoc getScreenTransform() {
126                 throw new UnsupportedOperationException JavaDoc("NYI");
127             }
128
129             /**
130              * Sets the transform to be used from the global transform space
131              * to pixels.
132              */

133             public void setScreenTransform(AffineTransform JavaDoc at) {
134                 throw new UnsupportedOperationException JavaDoc("NYI");
135             }
136
137             public AffineTransform JavaDoc getCTM() {
138                 return new AffineTransform JavaDoc();
139             }
140
141             public AffineTransform JavaDoc getGlobalTransform() {
142                 return new AffineTransform JavaDoc();
143             }
144
145             public float getViewportWidth() {
146                 return (float)view.getX();
147             }
148
149             public float getViewportHeight() {
150                 return (float)view.getY();
151             }
152
153             public float getFontSize() {
154                 return 12;
155             }
156
157             public void deselectAll() {
158             }
159         };
160         ((SVGOMElement)e).setSVGContext(dc);
161
162         //if (!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
163
e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
164                                 SVGDOMImplementation.SVG_NAMESPACE_URI);
165         //}
166
int fontSize = 12;
167         Point2D JavaDoc p2d = getSize(fontSize, svgRoot, getUserAgent().getSourcePixelUnitToMillimeter());
168        ((SVGOMElement)e).setSVGContext(null);
169
170         return p2d;
171     }
172
173     private void init() {
174         DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
175         String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
176         doc = impl.createDocument(svgNS, "svg", null);
177
178         element = doc.getDocumentElement();
179
180         buildTopLevel(doc, element);
181     }
182
183     /**
184      * Get the size of the SVG root element.
185      * @param size the font size
186      * @param svgRoot the svg root element
187      * @param ptmm the pixel to millimeter conversion factor
188      * @return the size of the SVG document
189      */

190     public static Point2D JavaDoc getSize(int size, Element svgRoot, float ptmm) {
191         String JavaDoc str;
192         UnitProcessor.Context ctx;
193         ctx = new PDFUnitContext(size, svgRoot, ptmm);
194         str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
195         if (str.length() == 0) {
196             str = "100%";
197         }
198         float width = UnitProcessor.svgHorizontalLengthToUserSpace
199             (str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx);
200
201         str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
202         if (str.length() == 0) {
203             str = "100%";
204         }
205         float height = UnitProcessor.svgVerticalLengthToUserSpace
206             (str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx);
207         return new Point2D.Float JavaDoc(width, height);
208     }
209
210     /**
211      * This class is the default context for a particular
212      * element. Information not available on the element are obtained from
213      * the bridge context (such as the viewport or the pixel to
214      * millimeter factor.
215      */

216     public static class PDFUnitContext implements UnitProcessor.Context {
217
218         /** The element. */
219         private Element e;
220         private int fontSize;
221         private float pixeltoMM;
222
223         /**
224          * Create a PDF unit context.
225          * @param size the font size.
226          * @param e the svg element
227          * @param ptmm the pixel to millimeter factor
228          */

229         public PDFUnitContext(int size, Element e, float ptmm) {
230             this.e = e;
231             this.fontSize = size;
232             this.pixeltoMM = ptmm;
233         }
234
235         /**
236          * Returns the element.
237          * @return the element
238          */

239         public Element getElement() {
240             return e;
241         }
242
243         /**
244          * Returns the context of the parent element of this context.
245          * Since this is always for the root SVG element there never
246          * should be one...
247          * @return null
248          */

249         public UnitProcessor.Context getParentElementContext() {
250             return null;
251         }
252
253         /**
254          * Returns the pixel to mm factor. (this is deprecated)
255          * @return the pixel to millimeter factor
256          */

257         public float getPixelToMM() {
258             return pixeltoMM;
259         }
260
261         /**
262          * Returns the pixel to mm factor.
263          * @return the pixel to millimeter factor
264          */

265         public float getPixelUnitToMillimeter() {
266             return pixeltoMM;
267         }
268
269         /**
270          * Returns the font-size value.
271          * @return the default font size
272          */

273         public float getFontSize() {
274             return fontSize;
275         }
276
277         /**
278          * Returns the x-height value.
279          * @return the x-height value
280          */

281         public float getXHeight() {
282             return 0.5f;
283         }
284
285         /**
286          * Returns the viewport width used to compute units.
287          * @return the default viewport width of 100
288          */

289         public float getViewportWidth() {
290             return 100;
291         }
292
293         /**
294          * Returns the viewport height used to compute units.
295          * @return the default viewport height of 100
296          */

297         public float getViewportHeight() {
298             return 100;
299         }
300     }
301
302 }
303
304
Popular Tags