Skip to content

Commit

Permalink
svgom: add getCSSUnitType(), newValueSpecifiedCSSUnits() to SVGLength
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosame committed Oct 11, 2024
1 parent 27ea149 commit 9bebb11
Showing 1 changed file with 170 additions and 3 deletions.
173 changes: 170 additions & 3 deletions svgom-api/src/main/java/org/w3c/dom/svg/SVGLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@

package org.w3c.dom.svg;

import org.w3c.css.om.unit.CSSUnit;
import org.w3c.dom.DOMException;

/**
* Represents a value that can be a <length>, <percentage> or
* <number>.
*/
public interface SVGLength {

// Length Unit Types
short SVG_LENGTHTYPE_UNKNOWN = 0;
short SVG_LENGTHTYPE_NUMBER = 1;
Expand All @@ -28,21 +34,182 @@ public interface SVGLength {
short SVG_LENGTHTYPE_PT = 9;
short SVG_LENGTHTYPE_PC = 10;

short getUnitType();
/**
* Get the unit type according to {@link CSSUnit}.
* <p>
* Optional attribute.
* </p>
*
* @return the CSS unit type.
*/
default short getCSSUnitType() {
return CSSUnit.CSS_INVALID;
}

/**
* The type of the value as specified by one of the SVG_LENGTHTYPE_* constants
* defined on this interface.
*
* @return the SVG unit type.
*/
default short getUnitType() {
short svgUnit;
switch (getCSSUnitType()) {
case CSSUnit.CSS_NUMBER:
svgUnit = SVGLength.SVG_LENGTHTYPE_NUMBER;
break;
case CSSUnit.CSS_PX:
svgUnit = SVGLength.SVG_LENGTHTYPE_PX;
break;
case CSSUnit.CSS_EM:
svgUnit = SVGLength.SVG_LENGTHTYPE_EMS;
break;
case CSSUnit.CSS_EX:
svgUnit = SVGLength.SVG_LENGTHTYPE_EXS;
break;
case CSSUnit.CSS_IN:
svgUnit = SVGLength.SVG_LENGTHTYPE_IN;
break;
case CSSUnit.CSS_CM:
svgUnit = SVGLength.SVG_LENGTHTYPE_CM;
break;
case CSSUnit.CSS_MM:
svgUnit = SVGLength.SVG_LENGTHTYPE_MM;
break;
case CSSUnit.CSS_PC:
svgUnit = SVGLength.SVG_LENGTHTYPE_PC;
break;
case CSSUnit.CSS_PT:
svgUnit = SVGLength.SVG_LENGTHTYPE_PT;
break;
case CSSUnit.CSS_PERCENTAGE:
svgUnit = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
break;
default:
svgUnit = SVGLength.SVG_LENGTHTYPE_UNKNOWN;
break;
}
return svgUnit;
}

/**
* Gets the SVGLength's value in user units.
*
* @return the value in user units.
*/
float getValue();

/**
* Sets the SVGLength's value in user units.
*
* @param value the value in user units.
* @throws DOMException NO_MODIFICATION_ALLOWED_ERR if the object is read-only.
*/
void setValue(float value) throws DOMException;

/**
* Gets the numeric factor of the SVGLength's value.
*
* @return the numeric value in the current unit.
*/
float getValueInSpecifiedUnits();

/**
* Sets the numeric factor of the SVGLength's value.
*
* @param valueInSpecifiedUnits the value in the current unit.
* @throws DOMException NO_MODIFICATION_ALLOWED_ERR if the object is read-only.
*/
void setValueInSpecifiedUnits(float valueInSpecifiedUnits) throws DOMException;

/**
* Gets the SVGLength's value as a string.
*
* @return the value as a string.
*/
String getValueAsString();

/**
* Sets the SVGLength's value as a string.
*
* @param valueAsString the length value as a string.
* @throws DOMException SYNTAX_ERR if the value is not a valid length.
* NO_MODIFICATION_ALLOWED_ERR if the object is read-only.
*
*/
void setValueAsString(String valueAsString) throws DOMException;

void newValueSpecifiedUnits(short unitType, float valueInSpecifiedUnits);
/**
* Sets the SVGLength's value in a typed manner.
*
* @param cssUnitType the unit type according to {@link CSSUnit}.
* @param valueInSpecifiedUnits the value in the given unit.
* @throws DOMException NOT_SUPPORTED_ERR if the unit is not supported or
* invalid. NO_MODIFICATION_ALLOWED_ERR if the object is
* read-only.
*/
default void newValueSpecifiedCSSUnits(short cssUnitType, float valueInSpecifiedUnits)
throws DOMException {
short svgUnit;
switch (getCSSUnitType()) {
case CSSUnit.CSS_NUMBER:
svgUnit = SVGLength.SVG_LENGTHTYPE_NUMBER;
break;
case CSSUnit.CSS_PX:
svgUnit = SVGLength.SVG_LENGTHTYPE_PX;
break;
case CSSUnit.CSS_EM:
svgUnit = SVGLength.SVG_LENGTHTYPE_EMS;
break;
case CSSUnit.CSS_EX:
svgUnit = SVGLength.SVG_LENGTHTYPE_EXS;
break;
case CSSUnit.CSS_IN:
svgUnit = SVGLength.SVG_LENGTHTYPE_IN;
break;
case CSSUnit.CSS_CM:
svgUnit = SVGLength.SVG_LENGTHTYPE_CM;
break;
case CSSUnit.CSS_MM:
svgUnit = SVGLength.SVG_LENGTHTYPE_MM;
break;
case CSSUnit.CSS_PC:
svgUnit = SVGLength.SVG_LENGTHTYPE_PC;
break;
case CSSUnit.CSS_PT:
svgUnit = SVGLength.SVG_LENGTHTYPE_PT;
break;
case CSSUnit.CSS_PERCENTAGE:
svgUnit = SVGLength.SVG_LENGTHTYPE_PERCENTAGE;
break;
default:
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unsupported CSS unit: "
+ cssUnitType);
}
newValueSpecifiedUnits(svgUnit, valueInSpecifiedUnits);
}

/**
* Sets the SVGLength's value in a typed manner.
*
* @param unitType the unit type according to the {@code SVGLength}
* constants.
* @param valueInSpecifiedUnits the value in the given unit.
* @throws DOMException NOT_SUPPORTED_ERR if the unit is not supported or
* invalid. NO_MODIFICATION_ALLOWED_ERR if the object is
* read-only.
*/
void newValueSpecifiedUnits(short unitType, float valueInSpecifiedUnits)
throws DOMException;

/**
* Converts this SVGLength's value to a specific unit.
*
* @param unitType the unit type according to the {@code SVGLength} constants.
* @throws DOMException NOT_SUPPORTED_ERR if the unit is not supported or
* invalid. NO_MODIFICATION_ALLOWED_ERR if the object is
* read-only.
*/
void convertToSpecifiedUnits(short unitType) throws DOMException;

void convertToSpecifiedUnits(short unitType);
}

0 comments on commit 9bebb11

Please sign in to comment.