KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: SVGUtilities.java,v 1.2.2.1 2003/02/25 15:08:11 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 import java.util.*;
54 import java.awt.geom.AffineTransform JavaDoc;
55 import java.awt.geom.Rectangle2D JavaDoc;
56 import java.awt.font.FontRenderContext JavaDoc;
57
58 import org.apache.fop.svg.*;
59 import org.w3c.dom.*;
60
61 import org.apache.batik.dom.svg.SVGDOMImplementation;
62
63 /**
64  * Some utilities for creating svg DOM documents and elements.
65  */

66 public class SVGUtilities {
67     static final String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
68
69
70     public static final Document createSVGDocument(float width,
71             float height) {
72         DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
73         Document doc = impl.createDocument(svgNS, "svg", null);
74
75         Element svgRoot = doc.getDocumentElement();
76         svgRoot.setAttributeNS(null, "width", "" + width);
77         svgRoot.setAttributeNS(null, "height", "" + height);
78         return doc;
79     }
80
81     /**
82      * Get the string width for a particular string given the font.
83      */

84     public static final float getStringWidth(String JavaDoc str, java.awt.Font JavaDoc font) {
85         Rectangle2D JavaDoc rect =
86             font.getStringBounds(str, 0, str.length(),
87                                  new FontRenderContext JavaDoc(new AffineTransform JavaDoc(),
88                                  true, true));
89         return (float)rect.getWidth();
90     }
91
92     /**
93      * Get the string height for a particular string given the font.
94      */

95     public static final float getStringHeight(String JavaDoc str,
96                                               java.awt.Font JavaDoc font) {
97         Rectangle2D JavaDoc rect =
98             font.getStringBounds(str, 0, str.length(),
99                                  new FontRenderContext JavaDoc(new AffineTransform JavaDoc(),
100                                  true, true));
101         return (float)rect.getHeight();
102     }
103
104     /**
105      * Get the string bounds for a particular string given the font.
106      */

107     public static final Rectangle2D JavaDoc getStringBounds(String JavaDoc str,
108             java.awt.Font JavaDoc font) {
109         return font.getStringBounds(str, 0, str.length(),
110                                     new FontRenderContext JavaDoc(new AffineTransform JavaDoc(),
111                                     true, true));
112     }
113
114     /**
115      * Create an SVG Line
116      */

117     public static final Element createLine(Document doc, float x, float y,
118                                            float x2, float y2) {
119         Element ellipse = doc.createElementNS(svgNS, "line");
120         ellipse.setAttributeNS(null, "x1", "" + x);
121         ellipse.setAttributeNS(null, "x2", "" + x2);
122         ellipse.setAttributeNS(null, "y1", "" + y);
123         ellipse.setAttributeNS(null, "y2", "" + y2);
124         return ellipse;
125     }
126
127     /**
128      * Create an SVG Ellipse
129      */

130     public static final Element createEllipse(Document doc, float cx,
131                                               float cy, float rx, float ry) {
132         Element ellipse = doc.createElementNS(svgNS, "ellipse");
133         ellipse.setAttributeNS(null, "cx", "" + cx);
134         ellipse.setAttributeNS(null, "rx", "" + rx);
135         ellipse.setAttributeNS(null, "cy", "" + cy);
136         ellipse.setAttributeNS(null, "ry", "" + ry);
137         return ellipse;
138     }
139
140     /**
141      * Create an SVG Path.
142      */

143     public static final Element createPath(Document doc, String JavaDoc str) {
144         Element path = doc.createElementNS(svgNS, "path");
145         path.setAttributeNS(null, "d", str);
146         return path;
147     }
148
149     /**
150      * Create an SVG Text object.
151      */

152     public static final Element createText(Document doc, float x, float y,
153                                            String JavaDoc str) {
154         Element textGraph = doc.createElementNS(svgNS, "text");
155         textGraph.setAttributeNS(null, "x", "" + x);
156         textGraph.setAttributeNS(null, "y", "" + y);
157         org.w3c.dom.Text JavaDoc text = doc.createTextNode(str);
158         textGraph.appendChild(text);
159         return textGraph;
160     }
161
162     /**
163      * Create an SVG Rectangle.
164      */

165     public static final Element createRect(Document doc, float x, float y,
166                                            float width, float height) {
167         Element border = doc.createElementNS(svgNS, "rect");
168         border.setAttributeNS(null, "x", "" + x);
169         border.setAttributeNS(null, "y", "" + y);
170         border.setAttributeNS(null, "width", "" + width);
171         border.setAttributeNS(null, "height", "" + height);
172         return border;
173     }
174
175     /**
176      * Create an SVG G.
177      */

178     public static final Element createG(Document doc) {
179         Element border = doc.createElementNS(svgNS, "g");
180         return border;
181     }
182
183     /**
184      * Create an SVG Clip.
185      */

186     public static final Element createClip(Document doc, Element els,
187                                            String JavaDoc id) {
188         Element border = doc.createElementNS(svgNS, "clipPath");
189         border.setAttributeNS(null, "id", id);
190         border.appendChild(els);
191         return border;
192     }
193
194     public static final Element createImage(Document doc, String JavaDoc ref,
195                                             float width, float height) {
196         Element border = doc.createElementNS(svgNS, "image");
197         border.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href",
198                               ref);
199         border.setAttributeNS(null, "width", "" + width);
200         border.setAttributeNS(null, "height", "" + height);
201         return border;
202     }
203
204     /**
205      * Create some SVG text that is wrapped into a specified width..
206      */

207     public static final Element wrapText(Document doc, String JavaDoc str,
208                                          java.awt.Font JavaDoc font, float width) {
209         Element g = createG(doc);
210         Element text;
211         StringTokenizer st = new StringTokenizer(str, " \t\r\n");
212         float totalWidth = 0;
213         String JavaDoc totalStr = "";
214         int line = 0;
215         float height = getStringHeight(str, font);
216         while (st.hasMoreTokens()) {
217             String JavaDoc token = st.nextToken();
218             float strwidth = getStringWidth(token, font);
219             totalWidth += strwidth;
220             if (totalWidth > width) {
221                 if (totalStr.equals("")) {
222                     totalStr = token;
223                     token = "";
224                     strwidth = 0;
225                 }
226                 text = createText(doc, 0, line * (height + 5), totalStr);
227                 g.appendChild(text);
228                 totalStr = token;
229                 totalWidth = strwidth;
230                 line++;
231             } else {
232                 totalStr = totalStr + " " + token;
233             }
234         }
235
236         return g;
237     }
238
239 }
240
Popular Tags