1 /**********************************************
2 * Copyright (C) 2010 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29
30 package org.vectomatic.dom.svg;
31
32 import com.google.gwt.core.client.JavaScriptException;
33 import com.google.gwt.core.client.JavaScriptObject;
34
35 /**
36 * The {@link org.vectomatic.dom.svg.OMSVGLength} interface corresponds to
37 * the <a href='types.html#DataTypeLength'><length></a> basic data type.
38 * <p id="ReadOnlyLength">An {@link org.vectomatic.dom.svg.OMSVGLength} object
39 * can be designated as <em>read only</em>, which means that attempts to modify
40 * the object will result in an exception being thrown, as described below.</p>
41 */
42 public class OMSVGLength extends JavaScriptObject {
43 /**
44 * The unit type is not one of predefined unit types. It is invalid to attempt
45 * to define a new value of this type or to attempt to switch an existing
46 * value to this type.
47 */
48 public static final short SVG_LENGTHTYPE_UNKNOWN = 0;
49 /**
50 * No unit type was provided (i.e., a unitless value was specified), which
51 * indicates a value in user units.
52 */
53 public static final short SVG_LENGTHTYPE_NUMBER = 1;
54 /**
55 * A percentage value was specified.
56 */
57 public static final short SVG_LENGTHTYPE_PERCENTAGE = 2;
58 /**
59 * A value was specified using the em units defined in CSS2.
60 */
61 public static final short SVG_LENGTHTYPE_EMS = 3;
62 /**
63 * A value was specified using the ex units defined in CSS2.
64 */
65 public static final short SVG_LENGTHTYPE_EXS = 4;
66 /**
67 * A value was specified using the px units defined in CSS2.
68 */
69 public static final short SVG_LENGTHTYPE_PX = 5;
70 /**
71 * A value was specified using the cm units defined in CSS2.
72 */
73 public static final short SVG_LENGTHTYPE_CM = 6;
74 /**
75 * A value was specified using the mm units defined in CSS2.
76 */
77 public static final short SVG_LENGTHTYPE_MM = 7;
78 /**
79 * A value was specified using the in units defined in CSS2.
80 */
81 public static final short SVG_LENGTHTYPE_IN = 8;
82 /**
83 * A value was specified using the pt units defined in CSS2.
84 */
85 public static final short SVG_LENGTHTYPE_PT = 9;
86 /**
87 * A value was specified using the pc units defined in CSS2.
88 */
89 public static final short SVG_LENGTHTYPE_PC = 10;
90 protected OMSVGLength() {
91 }
92
93 // Implementation of the svg::SVGLength W3C IDL interface
94 /**
95 * The type of the value as specified by one of the SVG_LENGTHTYPE_ constants
96 * defined on this interface.
97 */
98 public final native short getUnitType() /*-{
99 return this.unitType;
100 }-*/;
101 /**
102 * The value as a floating point value, in user units. Setting this attribute
103 * will cause {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()}
104 * and {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()} to be
105 * updated automatically to reflect this setting.
106 */
107 public final native float getValue() /*-{
108 return this.value;
109 }-*/;
110 /**
111 * The value as a floating point value, in user units. Setting this attribute
112 * will cause {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()}
113 * and {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()} to be
114 * updated automatically to reflect this setting.
115 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
116 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
117 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
118 */
119 public final native void setValue(float value) throws JavaScriptException /*-{
120 this.value = value;
121 }-*/;
122 /**
123 * The value as a floating point value, in the units expressed by {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()}.
124 * Setting this attribute will cause {@link org.vectomatic.dom.svg.OMSVGLength#getValue()}
125 * and {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()} to be
126 * updated automatically to reflect this setting.
127 */
128 public final native float getValueInSpecifiedUnits() /*-{
129 return this.valueInSpecifiedUnits;
130 }-*/;
131 /**
132 * The value as a floating point value, in the units expressed by {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()}.
133 * Setting this attribute will cause {@link org.vectomatic.dom.svg.OMSVGLength#getValue()}
134 * and {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()} to be
135 * updated automatically to reflect this setting.
136 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
137 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
138 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
139 */
140 public final native void setValueInSpecifiedUnits(float value) throws JavaScriptException /*-{
141 this.valueInSpecifiedUnits = value;
142 }-*/;
143 /**
144 * The value as a string value, in the units expressed by {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()}.
145 * Setting this attribute will cause {@link org.vectomatic.dom.svg.OMSVGLength#getValue()},
146 * {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()} and
147 * {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()} to be updated
148 * automatically to reflect this setting.
149 */
150 public final native String getValueAsString() /*-{
151 return this.valueAsString;
152 }-*/;
153 /**
154 * The value as a string value, in the units expressed by {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()}.
155 * Setting this attribute will cause {@link org.vectomatic.dom.svg.OMSVGLength#getValue()},
156 * {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()} and
157 * {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()} to be updated
158 * automatically to reflect this setting.
159 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
160 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
161 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
162 * @throws DOMException(SYNTAX_ERR) Raised if the assigned string cannot
163 * be parsed as a valid <code><length></code>.
164 */
165 public final native void setValueAsString(String value) throws JavaScriptException /*-{
166 this.valueAsString = value;
167 }-*/;
168 /**
169 * Reset the value as a number with an associated {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()},
170 * thereby replacing the values for all of the attributes on the object.
171 * @param unitType The unit type for the value (e.g., <code>SVG_LENGTHTYPE_MM</code>).
172 * @param valueInSpecifiedUnits The new value.
173 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
174 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
175 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
176 * @throws DOMException(NOT_SUPPORTED_ERR) Raised if unitType is SVG_LENGTHTYPE_UNKNOWN
177 * or not a valid unit type constant (one of the other SVG_LENGTHTYPE_ constants
178 * defined on this interface).
179 */
180 public final native void newValueSpecifiedUnits(short unitType, float valueInSpecifiedUnits) throws JavaScriptException /*-{
181 this.newValueSpecifiedUnits(unitType, valueInSpecifiedUnits);
182 }-*/;
183 /**
184 * Preserve the same underlying stored value, but reset the stored unit identifier
185 * to the given <var>unitType</var>. Object attributes {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()},
186 * {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()} and
187 * {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()} might be
188 * modified as a result of this method. For example, if the original value
189 * were "0.5cm" and the method was invoked to convert to millimeters, then
190 * the {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()} would be changed
191 * to <code>SVG_LENGTHTYPE_MM</code>, {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()}
192 * would be changed to the numeric value 5 and {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()}
193 * would be changed to "5mm".
194 * @param unitType The unit type to switch to (e.g., <code>SVG_LENGTHTYPE_MM</code>).
195 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
196 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
197 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
198 * @throws DOMException(NOT_SUPPORTED_ERR) Raised if unitType is SVG_LENGTHTYPE_UNKNOWN
199 * or not a valid unit type constant (one of the other SVG_LENGTHTYPE_ constants
200 * defined on this interface).
201 */
202 public final native void convertToSpecifiedUnits(short unitType) throws JavaScriptException /*-{
203 this.convertToSpecifiedUnits(unitType);
204 }-*/;
205
206 // Helper methods
207 /**
208 * Converts a {@link com.google.gwt.dom.client.Style.Unit} enum to an
209 * SVG length constant.
210 * @return an SVG length constant.
211 */
212 public static final short unitToCode(com.google.gwt.dom.client.Style.Unit unit) {
213 if (unit != null) {
214 switch(unit) {
215 case PCT:
216 return SVG_LENGTHTYPE_PERCENTAGE;
217 case EM:
218 return SVG_LENGTHTYPE_EMS;
219 case EX:
220 return SVG_LENGTHTYPE_EXS;
221 case PX:
222 return SVG_LENGTHTYPE_PX;
223 case CM:
224 return SVG_LENGTHTYPE_CM;
225 case MM:
226 return SVG_LENGTHTYPE_MM;
227 case IN:
228 return SVG_LENGTHTYPE_IN;
229 case PT:
230 return SVG_LENGTHTYPE_PT;
231 case PC:
232 return SVG_LENGTHTYPE_PC;
233 }
234 }
235 return SVG_LENGTHTYPE_NUMBER;
236 }
237 /**
238 * Converts an SVG length constant to
239 * a {@link com.google.gwt.dom.client.Style.Unit} enum.
240 * @return a {@link com.google.gwt.dom.client.Style.Unit} enum.
241 */
242 public static final com.google.gwt.dom.client.Style.Unit codeToUnit(short unit) {
243 switch(unit) {
244 case SVG_LENGTHTYPE_NUMBER:
245 return null;
246 case SVG_LENGTHTYPE_PERCENTAGE:
247 return com.google.gwt.dom.client.Style.Unit.PCT;
248 case SVG_LENGTHTYPE_EMS:
249 return com.google.gwt.dom.client.Style.Unit.EM;
250 case SVG_LENGTHTYPE_EXS:
251 return com.google.gwt.dom.client.Style.Unit.EX;
252 case SVG_LENGTHTYPE_PX:
253 return com.google.gwt.dom.client.Style.Unit.PX;
254 case SVG_LENGTHTYPE_CM:
255 return com.google.gwt.dom.client.Style.Unit.CM;
256 case SVG_LENGTHTYPE_MM:
257 return com.google.gwt.dom.client.Style.Unit.MM;
258 case SVG_LENGTHTYPE_IN:
259 return com.google.gwt.dom.client.Style.Unit.IN;
260 case SVG_LENGTHTYPE_PT:
261 return com.google.gwt.dom.client.Style.Unit.PT;
262 case SVG_LENGTHTYPE_PC:
263 return com.google.gwt.dom.client.Style.Unit.PC;
264 }
265 throw new IllegalStateException("Unsupported unit conversion");
266 }
267 /**
268 * Returns the type of the value as defined by the
269 * {@link com.google.gwt.dom.client.Style.Unit} enum.
270 */
271 public final com.google.gwt.dom.client.Style.Unit getUnit() {
272 return codeToUnit(getUnitType());
273 }
274 /**
275 * Reset the value as a number with an associated {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()},
276 * thereby replacing the values for all of the attributes on the object.
277 * @param unitType The unit type for the value.
278 * @param valueInSpecifiedUnits The new value.
279 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
280 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
281 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
282 * @throws DOMException(NOT_SUPPORTED_ERR) Raised if unitType is SVG_LENGTHTYPE_UNKNOWN
283 * or not a valid unit type constant (one of the other SVG_LENGTHTYPE_ constants
284 * defined on this interface).
285 */
286 public final void newValueSpecifiedUnits(com.google.gwt.dom.client.Style.Unit unitType, float valueInSpecifiedUnits) throws JavaScriptException {
287 newValueSpecifiedUnits(unitToCode(unitType), valueInSpecifiedUnits);
288 }
289 /**
290 * Preserve the same underlying stored value, but reset the stored unit identifier
291 * to the given <var>unitType</var>. Object attributes {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()},
292 * {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()} and
293 * {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()} might be
294 * modified as a result of this method. For example, if the original value
295 * were "0.5cm" and the method was invoked to convert to millimeters, then
296 * the {@link org.vectomatic.dom.svg.OMSVGLength#getUnitType()} would be changed
297 * to <code>SVG_LENGTHTYPE_MM</code>, {@link org.vectomatic.dom.svg.OMSVGLength#getValueInSpecifiedUnits()}
298 * would be changed to the numeric value 5 and {@link org.vectomatic.dom.svg.OMSVGLength#getValueAsString()}
299 * would be changed to "5mm".
300 * @param unitType The unit type to switch to.
301 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised when the length
302 * corresponds to a <a href="svgdom.html#ReadOnlyNodes">read only attribute</a>
303 * or when the object itself is <a href="#ReadOnlyLength">read only</a>.
304 * @throws DOMException(NOT_SUPPORTED_ERR) Raised if unitType is SVG_LENGTHTYPE_UNKNOWN
305 * or not a valid unit type constant (one of the other SVG_LENGTHTYPE_ constants
306 * defined on this interface).
307 */
308 public final void convertToSpecifiedUnits(com.google.gwt.dom.client.Style.Unit unitType) throws JavaScriptException {
309 convertToSpecifiedUnits(unitToCode(unitType));
310 }
311
312 /**
313 * The value as a floating point value, in the units expressed by the given <var>unitType</var>.
314 * @param unitType The unit type to use to express the value.
315 */
316 public final float getValueInSpecifiedUnits(com.google.gwt.dom.client.Style.Unit unitType) {
317 return getValueInSpecifiedUnits(unitToCode(unitType));
318 }
319
320 /**
321 * The value as a floating point value, in the units expressed by the given <var>unitType</var>.
322 * @param unitType The unit type to use to express the value.
323 */
324 public final native float getValueInSpecifiedUnits(short unitType) /*-{
325 var u = this.unitType;
326 this.convertToSpecifiedUnits(unitType);
327 var v = this.valueInSpecifiedUnits;
328 this.convertToSpecifiedUnits(u);
329 return v;
330 }-*/;
331 }