KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > output > svg > SVGCanvasProvider


1 /*
2  * $Id: SVGCanvasProvider.java,v 1.7 2003/08/18 19:12:05 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.output.svg;
59
60 import javax.xml.parsers.DocumentBuilder JavaDoc;
61 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
62 import javax.xml.parsers.ParserConfigurationException JavaDoc;
63
64 import org.w3c.dom.DOMImplementation JavaDoc;
65 import org.w3c.dom.Document JavaDoc;
66 import org.w3c.dom.DocumentFragment JavaDoc;
67 import org.w3c.dom.Element JavaDoc;
68
69 import org.krysalis.barcode.BarcodeDimension;
70 import org.krysalis.barcode.output.BarcodeCanvasSetupException;
71
72 /**
73  * Implementation that outputs to a W3C DOM.
74  *
75  * @author Jeremias Maerki
76  */

77 public class SVGCanvasProvider extends AbstractSVGGeneratingCanvasProvider {
78
79     private DOMImplementation JavaDoc domImpl;
80     private Document JavaDoc doc;
81     private Element JavaDoc detailGroup;
82
83     /**
84      * Creates a new SVGCanvasProvider with namespaces enabled.
85      * @param namespacePrefix the namespace prefix to use, null for no prefix
86      * @throws BarcodeCanvasSetupException if setting up the provider fails
87      */

88     public SVGCanvasProvider(String JavaDoc namespacePrefix)
89                 throws BarcodeCanvasSetupException {
90         this(null, namespacePrefix);
91     }
92
93     /**
94      * Creates a new SVGCanvasProvider with namespaces enabled.
95      * @param domImpl DOMImplementation to use (JAXP default is used when
96      * this is null)
97      * @param namespacePrefix the namespace prefix to use, null for no prefix
98      * @throws BarcodeCanvasSetupException if setting up the provider fails
99      */

100     public SVGCanvasProvider(DOMImplementation JavaDoc domImpl, String JavaDoc namespacePrefix)
101                 throws BarcodeCanvasSetupException {
102         super(namespacePrefix);
103         this.domImpl = domImpl;
104         init();
105     }
106
107     /**
108      * Creates a new SVGCanvasProvider.
109      * @param useNamespace Controls whether namespaces should be used
110      * @throws BarcodeCanvasSetupException if setting up the provider fails
111      */

112     public SVGCanvasProvider(boolean useNamespace)
113                 throws BarcodeCanvasSetupException {
114         this(null, useNamespace);
115     }
116
117     /**
118      * Creates a new SVGCanvasProvider.
119      * @param domImpl DOMImplementation to use (JAXP default is used when
120      * this is null)
121      * @param useNamespace Controls whether namespaces should be used
122      * @throws BarcodeCanvasSetupException if setting up the provider fails
123      */

124     public SVGCanvasProvider(DOMImplementation JavaDoc domImpl, boolean useNamespace)
125                 throws BarcodeCanvasSetupException {
126         super(useNamespace);
127         this.domImpl = domImpl;
128         init();
129     }
130
131     /**
132      * Creates a new SVGCanvasProvider with default settings (with namespaces,
133      * but without namespace prefix).
134      * @throws BarcodeCanvasSetupException if setting up the provider fails
135      */

136     public SVGCanvasProvider() throws BarcodeCanvasSetupException {
137         super();
138         init();
139     }
140
141     private void init() {
142         doc = createDocument();
143         Element JavaDoc svg = doc.getDocumentElement();
144
145         detailGroup = createElement("g");
146         svg.appendChild(detailGroup);
147         detailGroup.setAttribute("style", "fill:black; stroke:none");
148     }
149
150
151     private Element JavaDoc createElement(String JavaDoc localName) {
152         Element JavaDoc el;
153         if (isNamespaceEnabled()) {
154             el = doc.createElementNS(SVG_NAMESPACE, getQualifiedName(localName));
155         } else {
156             el = doc.createElement(localName);
157         }
158         return el;
159     }
160
161
162     private Document JavaDoc createDocument() {
163         try {
164             if (this.domImpl == null) {
165                 DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
166                 dbf.setNamespaceAware(true);
167                 dbf.setValidating(false);
168                 DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
169                 this.domImpl = db.getDOMImplementation();
170             }
171
172             if (isNamespaceEnabled()) {
173                 Document JavaDoc doc = this.domImpl.createDocument(
174                         SVG_NAMESPACE, getQualifiedName("svg"), null);
175                 /*
176                 if (getNamespacePrefix() == null) {
177                     doc.getDocumentElement().setAttribute(
178                             "xmlns", SVG_NAMESPACE);
179                 } else {
180                     doc.getDocumentElement().setAttribute(
181                             "xmlns:" + getNamespacePrefix(), SVG_NAMESPACE);
182                 }*/

183                 return doc;
184             } else {
185                 return this.domImpl.createDocument(null, "svg", null);
186             }
187         } catch (ParserConfigurationException JavaDoc pce) {
188             throw new RuntimeException JavaDoc(pce.getMessage());
189         }
190     }
191
192     /**
193      * Returns the DOM document containing the SVG barcode.
194      * @return the DOM document
195      */

196     public org.w3c.dom.Document JavaDoc getDOM() {
197         return this.doc;
198     }
199
200     /**
201      * Returns the DOM fragment containing the SVG barcode.
202      * @return the DOM fragment
203      */

204     public org.w3c.dom.DocumentFragment JavaDoc getDOMFragment() {
205         DocumentFragment JavaDoc frag = doc.createDocumentFragment();
206         frag.appendChild(doc.importNode(doc.getFirstChild(), true));
207         return frag;
208     }
209
210     /** @see org.krysalis.barcode.output.CanvasProvider */
211     public void establishDimensions(BarcodeDimension dim) {
212         super.establishDimensions(dim);
213         Element JavaDoc svg = (Element JavaDoc)doc.getDocumentElement();
214         svg.setAttribute("width", addUnit(dim.getWidthPlusQuiet()));
215         svg.setAttribute("height", addUnit(dim.getHeightPlusQuiet()));
216     }
217
218     /** @see org.krysalis.barcode.output.CanvasProvider */
219     public void deviceFillRect(double x, double y, double w, double h) {
220         Element JavaDoc el = createElement("rect");
221         el.setAttribute("x", addUnit(x));
222         el.setAttribute("y", addUnit(y));
223         el.setAttribute("width", addUnit(w));
224         el.setAttribute("height", addUnit(h));
225         detailGroup.appendChild(el);
226     }
227
228     /** @see org.krysalis.barcode.output.CanvasProvider */
229     public void deviceJustifiedText(String JavaDoc text, double x1, double x2, double y1,
230                             String JavaDoc fontName, double fontSize) {
231         deviceCenteredText(text, x1, x2, y1, fontName, fontSize, true);
232     }
233                             
234     /** @see org.krysalis.barcode.output.CanvasProvider */
235     public void deviceCenteredText(String JavaDoc text, double x1, double x2, double y1,
236                             String JavaDoc fontName, double fontSize) {
237         deviceCenteredText(text, x1, x2, y1, fontName, fontSize, false);
238     }
239                             
240     /**
241      * Draws centered text.
242      * @param text the text to draw
243      * @param x1 the left boundary
244      * @param x2 the right boundary
245      * @param y1 the y coordinate
246      * @param fontName the name of the font
247      * @param fontSize the size of the font
248      * @param justify true if the text should be justified instead of centered
249      */

250     public void deviceCenteredText(String JavaDoc text, double x1, double x2, double y1,
251                             String JavaDoc fontName, double fontSize, boolean justify) {
252         Element JavaDoc el = createElement("text");
253         el.setAttribute("style", "font-family:" + fontName + "; font-size:"
254                     + fontSize + "pt; text-anchor:middle");
255         el.setAttribute("x", addUnit(x1 + (x2 - x1) / 2));
256         el.setAttribute("y", addUnit(y1));
257         if (justify) {
258             el.setAttribute("textLength", addUnit(x2 - x1));
259         }
260         el.appendChild(doc.createTextNode(text));
261         detailGroup.appendChild(el);
262
263     }
264
265 }
Popular Tags