KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > AbstractSVGLength


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.dom.svg;
19
20 import org.apache.batik.parser.LengthParser;
21 import org.apache.batik.parser.ParseException;
22 import org.apache.batik.parser.UnitProcessor;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.svg.SVGLength;
26
27 /**
28  * Default implementation for SVGLength.
29  *
30  * This implementation provides the basic
31  * functionalities of SVGLength. To have
32  * a complete implementation, an element is
33  * required to resolve the units.
34  *
35  * According to the usage of this AbstractSVGLength,
36  * the <code>reset()</code> method is after
37  * changes being made to the unitType or the value
38  * of this length. Before any values are return
39  * to the user of the AbstractSVGLength, the
40  * <code>revalidate()</code> method is being called
41  * to insure the validity of the value and unit type
42  * held by this object.
43  *
44  * @author nicolas.socheleau@bitflash.com
45  * @version $Id: AbstractSVGLength.java,v 1.7 2005/03/29 02:26:36 cam Exp $
46  */

47 public abstract class AbstractSVGLength
48     implements SVGLength {
49
50     /**
51      * This constant represents horizontal lengths.
52      */

53     public final static short HORIZONTAL_LENGTH =
54         UnitProcessor.HORIZONTAL_LENGTH;
55
56     /**
57      * This constant represents vertical lengths.
58      */

59     public final static short VERTICAL_LENGTH =
60         UnitProcessor.VERTICAL_LENGTH;
61
62     /**
63      * This constant represents other lengths.
64      */

65     public final static short OTHER_LENGTH =
66         UnitProcessor.OTHER_LENGTH;
67
68     /**
69      * The type of this length.
70      */

71     protected short unitType;
72     
73     /**
74      * The value of this length.
75      */

76     protected float value;
77
78     /**
79      * This length's direction.
80      */

81     protected short direction;
82
83     /**
84      * The context used to resolve the units.
85      */

86     protected UnitProcessor.Context context;
87     
88     /**
89      * The unit string representations.
90      */

91     protected final static String JavaDoc[] UNITS = {
92         "", "", "%", "em", "ex", "px", "cm", "mm", "in", "pt", "pc"
93     };
94
95     /**
96      * Return the SVGElement associated to this length.
97      */

98     protected abstract SVGOMElement getAssociatedElement();
99
100
101     /**
102      * Creates a new AbstractSVGLength.
103      */

104     public AbstractSVGLength(short direction) {
105         context = new DefaultContext();
106         this.direction = direction;
107         this.value = 0.0f;
108         this.unitType = SVGLength.SVG_LENGTHTYPE_NUMBER;
109     }
110
111     /**
112      * <b>DOM</b>: Implements {@link SVGLength#getUnitType()}.
113      */

114     public short getUnitType() {
115         revalidate();
116         return unitType;
117     }
118     
119     /**
120      * <b>DOM</b>: Implements {@link SVGLength#getValue()}.
121      */

122     public float getValue() {
123         revalidate();
124         return UnitProcessor.svgToUserSpace(value, unitType,
125                                             direction, context);
126     }
127
128     /**
129      * <b>DOM</b>: Implements {@link SVGLength#setValue(float)}.
130      */

131     public void setValue(float value) throws DOMException JavaDoc {
132         revalidate();
133         this.value = UnitProcessor.userSpaceToSVG(value, unitType,
134                                                   direction, context);
135         reset();
136     }
137     
138     /**
139      * <b>DOM</b>: Implements {@link SVGLength#getValueInSpecifiedUnits()}.
140      */

141     public float getValueInSpecifiedUnits() {
142         revalidate();
143         return value;
144     }
145     
146     /**
147      * <b>DOM</b>: Implements {@link
148      * SVGLength#setValueInSpecifiedUnits(float)}.
149      */

150     public void setValueInSpecifiedUnits(float value) throws DOMException JavaDoc {
151         revalidate();
152         this.value = value;
153         reset();
154     }
155     
156     /**
157      * <b>DOM</b>: Implements {@link SVGLength#getValueAsString()}.
158      */

159     public String JavaDoc getValueAsString() {
160         revalidate();
161         return Float.toString(value)+UNITS[unitType];
162     }
163     
164     /**
165      * <b>DOM</b>: Implements {@link SVGLength#setValueAsString(String)}.
166      */

167     public void setValueAsString(String JavaDoc value) throws DOMException JavaDoc {
168         parse(value);
169         reset();
170     }
171         
172     /**
173      * <b>DOM</b>: Implements {@link
174      * SVGLength#newValueSpecifiedUnits(short,float)}.
175      */

176     public void newValueSpecifiedUnits(short unit, float value) {
177         unitType = unit;
178         this.value = value;
179         reset();
180     }
181     
182     /**
183      * <b>DOM</b>: Implements {@link
184      * SVGLength#convertToSpecifiedUnits(short)}.
185      */

186     public void convertToSpecifiedUnits(short unit) {
187         float v = getValue();
188         unitType = unit;
189         setValue(v);
190     }
191     
192     /**
193      * Callback method after changes
194      * made to this length.
195      *
196      * The default implementation does nothing.
197      */

198     protected void reset() {
199     }
200
201     /**
202      * Callback method before any value
203      * is return from this length.
204      *
205      * The default implementation does nothing.
206      */

207     protected void revalidate() {
208     }
209
210     /**
211      * Parse a String value as a SVGLength.
212      *
213      * Initialize this length with the result
214      * of the parsing of this value.
215      * @param s String representation of a SVGlength.
216      */

217     protected void parse(String JavaDoc s){
218         try {
219             LengthParser lengthParser = new LengthParser();
220             UnitProcessor.UnitResolver ur =
221                 new UnitProcessor.UnitResolver();
222             lengthParser.setLengthHandler(ur);
223             lengthParser.parse(s);
224             unitType = ur.unit;
225             value = ur.value;
226         } catch (ParseException e) {
227             unitType = SVG_LENGTHTYPE_UNKNOWN;
228             value = 0;
229         }
230     }
231
232     /**
233      * To resolve the units.
234      */

235     protected class DefaultContext implements UnitProcessor.Context {
236         
237         /**
238          * Returns the element.
239          */

240         public Element JavaDoc getElement() {
241             return getAssociatedElement();
242         }
243         
244         /**
245          * Returns the size of a px CSS unit in millimeters.
246          */

247         public float getPixelUnitToMillimeter() {
248             SVGContext ctx = getAssociatedElement().getSVGContext();
249             return ctx.getPixelUnitToMillimeter();
250         }
251         
252         /**
253          * Returns the size of a px CSS unit in millimeters.
254          * This will be removed after next release.
255          * @see #getPixelUnitToMillimeter()
256          */

257         public float getPixelToMM() {
258             return getPixelUnitToMillimeter();
259         }
260         
261         /**
262          * Returns the font-size value.
263          */

264         public float getFontSize() {
265             return getAssociatedElement().getSVGContext().getFontSize();
266         }
267         
268         /**
269          * Returns the x-height value.
270          */

271         public float getXHeight() {
272             return 0.5f;
273         }
274         
275         /**
276          * Returns the viewport width used to compute units.
277          */

278         public float getViewportWidth() {
279             return getAssociatedElement().getSVGContext().
280                 getViewportWidth();
281         }
282         
283         /**
284          * Returns the viewport height used to compute units.
285          */

286         public float getViewportHeight() {
287             return getAssociatedElement().getSVGContext().
288                 getViewportHeight();
289         }
290     }
291 }
292
Popular Tags