Skip to content

Commit 9bebb11

Browse files
committed
svgom: add getCSSUnitType(), newValueSpecifiedCSSUnits() to SVGLength
1 parent 27ea149 commit 9bebb11

File tree

1 file changed

+170
-3
lines changed

1 file changed

+170
-3
lines changed

svgom-api/src/main/java/org/w3c/dom/svg/SVGLength.java

+170-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212

1313
package org.w3c.dom.svg;
1414

15+
import org.w3c.css.om.unit.CSSUnit;
1516
import org.w3c.dom.DOMException;
1617

18+
/**
19+
* Represents a value that can be a <length>, <percentage> or
20+
* <number>.
21+
*/
1722
public interface SVGLength {
23+
1824
// Length Unit Types
1925
short SVG_LENGTHTYPE_UNKNOWN = 0;
2026
short SVG_LENGTHTYPE_NUMBER = 1;
@@ -28,21 +34,182 @@ public interface SVGLength {
2834
short SVG_LENGTHTYPE_PT = 9;
2935
short SVG_LENGTHTYPE_PC = 10;
3036

31-
short getUnitType();
37+
/**
38+
* Get the unit type according to {@link CSSUnit}.
39+
* <p>
40+
* Optional attribute.
41+
* </p>
42+
*
43+
* @return the CSS unit type.
44+
*/
45+
default short getCSSUnitType() {
46+
return CSSUnit.CSS_INVALID;
47+
}
48+
49+
/**
50+
* The type of the value as specified by one of the SVG_LENGTHTYPE_* constants
51+
* defined on this interface.
52+
*
53+
* @return the SVG unit type.
54+
*/
55+
default short getUnitType() {
56+
short svgUnit;
57+
switch (getCSSUnitType()) {
58+
case CSSUnit.CSS_NUMBER:
59+
svgUnit = SVGLength.SVG_LENGTHTYPE_NUMBER;
60+
break;
61+
case CSSUnit.CSS_PX:
62+
svgUnit = SVGLength.SVG_LENGTHTYPE_PX;
63+
break;
64+
case CSSUnit.CSS_EM:
65+
svgUnit = SVGLength.SVG_LENGTHTYPE_EMS;
66+
break;
67+
case CSSUnit.CSS_EX:
68+
svgUnit = SVGLength.SVG_LENGTHTYPE_EXS;
69+
break;
70+
case CSSUnit.CSS_IN:
71+
svgUnit = SVGLength.SVG_LENGTHTYPE_IN;
72+
break;
73+
case CSSUnit.CSS_CM:
74+
svgUnit = SVGLength.SVG_LENGTHTYPE_CM;
75+
break;
76+
case CSSUnit.CSS_MM:
77+
svgUnit = SVGLength.SVG_LENGTHTYPE_MM;
78+
break;
79+
case CSSUnit.CSS_PC:
80+
svgUnit = SVGLength.SVG_LENGTHTYPE_PC;
81+
break;
82+
case CSSUnit.CSS_PT:
83+
svgUnit = SVGLength.SVG_LENGTHTYPE_PT;
84+
break;
85+
case CSSUnit.CSS_PERCENTAGE:
86+
svgUnit = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
87+
break;
88+
default:
89+
svgUnit = SVGLength.SVG_LENGTHTYPE_UNKNOWN;
90+
break;
91+
}
92+
return svgUnit;
93+
}
3294

95+
/**
96+
* Gets the SVGLength's value in user units.
97+
*
98+
* @return the value in user units.
99+
*/
33100
float getValue();
34101

102+
/**
103+
* Sets the SVGLength's value in user units.
104+
*
105+
* @param value the value in user units.
106+
* @throws DOMException NO_MODIFICATION_ALLOWED_ERR if the object is read-only.
107+
*/
35108
void setValue(float value) throws DOMException;
36109

110+
/**
111+
* Gets the numeric factor of the SVGLength's value.
112+
*
113+
* @return the numeric value in the current unit.
114+
*/
37115
float getValueInSpecifiedUnits();
38116

117+
/**
118+
* Sets the numeric factor of the SVGLength's value.
119+
*
120+
* @param valueInSpecifiedUnits the value in the current unit.
121+
* @throws DOMException NO_MODIFICATION_ALLOWED_ERR if the object is read-only.
122+
*/
39123
void setValueInSpecifiedUnits(float valueInSpecifiedUnits) throws DOMException;
40124

125+
/**
126+
* Gets the SVGLength's value as a string.
127+
*
128+
* @return the value as a string.
129+
*/
41130
String getValueAsString();
42131

132+
/**
133+
* Sets the SVGLength's value as a string.
134+
*
135+
* @param valueAsString the length value as a string.
136+
* @throws DOMException SYNTAX_ERR if the value is not a valid length.
137+
* NO_MODIFICATION_ALLOWED_ERR if the object is read-only.
138+
*
139+
*/
43140
void setValueAsString(String valueAsString) throws DOMException;
44141

45-
void newValueSpecifiedUnits(short unitType, float valueInSpecifiedUnits);
142+
/**
143+
* Sets the SVGLength's value in a typed manner.
144+
*
145+
* @param cssUnitType the unit type according to {@link CSSUnit}.
146+
* @param valueInSpecifiedUnits the value in the given unit.
147+
* @throws DOMException NOT_SUPPORTED_ERR if the unit is not supported or
148+
* invalid. NO_MODIFICATION_ALLOWED_ERR if the object is
149+
* read-only.
150+
*/
151+
default void newValueSpecifiedCSSUnits(short cssUnitType, float valueInSpecifiedUnits)
152+
throws DOMException {
153+
short svgUnit;
154+
switch (getCSSUnitType()) {
155+
case CSSUnit.CSS_NUMBER:
156+
svgUnit = SVGLength.SVG_LENGTHTYPE_NUMBER;
157+
break;
158+
case CSSUnit.CSS_PX:
159+
svgUnit = SVGLength.SVG_LENGTHTYPE_PX;
160+
break;
161+
case CSSUnit.CSS_EM:
162+
svgUnit = SVGLength.SVG_LENGTHTYPE_EMS;
163+
break;
164+
case CSSUnit.CSS_EX:
165+
svgUnit = SVGLength.SVG_LENGTHTYPE_EXS;
166+
break;
167+
case CSSUnit.CSS_IN:
168+
svgUnit = SVGLength.SVG_LENGTHTYPE_IN;
169+
break;
170+
case CSSUnit.CSS_CM:
171+
svgUnit = SVGLength.SVG_LENGTHTYPE_CM;
172+
break;
173+
case CSSUnit.CSS_MM:
174+
svgUnit = SVGLength.SVG_LENGTHTYPE_MM;
175+
break;
176+
case CSSUnit.CSS_PC:
177+
svgUnit = SVGLength.SVG_LENGTHTYPE_PC;
178+
break;
179+
case CSSUnit.CSS_PT:
180+
svgUnit = SVGLength.SVG_LENGTHTYPE_PT;
181+
break;
182+
case CSSUnit.CSS_PERCENTAGE:
183+
svgUnit = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
184+
break;
185+
default:
186+
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unsupported CSS unit: "
187+
+ cssUnitType);
188+
}
189+
newValueSpecifiedUnits(svgUnit, valueInSpecifiedUnits);
190+
}
191+
192+
/**
193+
* Sets the SVGLength's value in a typed manner.
194+
*
195+
* @param unitType the unit type according to the {@code SVGLength}
196+
* constants.
197+
* @param valueInSpecifiedUnits the value in the given unit.
198+
* @throws DOMException NOT_SUPPORTED_ERR if the unit is not supported or
199+
* invalid. NO_MODIFICATION_ALLOWED_ERR if the object is
200+
* read-only.
201+
*/
202+
void newValueSpecifiedUnits(short unitType, float valueInSpecifiedUnits)
203+
throws DOMException;
204+
205+
/**
206+
* Converts this SVGLength's value to a specific unit.
207+
*
208+
* @param unitType the unit type according to the {@code SVGLength} constants.
209+
* @throws DOMException NOT_SUPPORTED_ERR if the unit is not supported or
210+
* invalid. NO_MODIFICATION_ALLOWED_ERR if the object is
211+
* read-only.
212+
*/
213+
void convertToSpecifiedUnits(short unitType) throws DOMException;
46214

47-
void convertToSpecifiedUnits(short unitType);
48215
}

0 commit comments

Comments
 (0)