Skip to content

Commit

Permalink
Do not use the SVGLength unit constants
Browse files Browse the repository at this point in the history
This removes the last bottleneck before all the units can be used anywhere in an SVG document.
  • Loading branch information
carlosame committed Oct 12, 2024
1 parent 548ed21 commit 54b14d6
Show file tree
Hide file tree
Showing 25 changed files with 699 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package io.sf.carte.echosvg.anim.dom;

import org.w3c.css.om.unit.CSSUnit;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.svg.SVGAnimatedLength;
Expand Down Expand Up @@ -109,7 +110,7 @@ public float getDefault() {
AnimSVGLength length = new AnimSVGLength(direction);
length.parse(getDefaultValue());
try {
return UnitProcessor.svgToUserSpace(length.value, length.unitType, direction,
return UnitProcessor.cssToUserSpace(length.value, length.unitType, direction,
length.context);
} catch (IllegalArgumentException ex) {
// Only if the default is broken
Expand Down Expand Up @@ -161,7 +162,7 @@ public float getCheckedValue() {
if (baseVal.missing) {
throw new LiveAttributeException(element, localName, LiveAttributeException.ERR_ATTRIBUTE_MISSING,
null);
} else if (baseVal.unitType == SVGLength.SVG_LENGTHTYPE_UNKNOWN) {
} else if (baseVal.unitType == CSSUnit.CSS_INVALID) {
throw new LiveAttributeException(element, localName, LiveAttributeException.ERR_ATTRIBUTE_MALFORMED,
baseVal.getValueAsString());
}
Expand Down Expand Up @@ -197,7 +198,7 @@ protected void updateAnimatedValue(AnimatableValue val) {
@Override
public AnimatableValue getUnderlyingValue(AnimationTarget target) {
SVGLength base = getBaseVal();
return new AnimatableLengthValue(target, base.getUnitType(), base.getValueInSpecifiedUnits(),
return new AnimatableLengthValue(target, base.getCSSUnitType(), base.getValueInSpecifiedUnits(),
target.getPercentageInterpretation(getNamespaceURI(), getLocalName(), false));
}

Expand Down Expand Up @@ -339,9 +340,14 @@ public AnimSVGLength(short direction) {
super(direction);
}

/**
* <b>DOM</b>: Implements {@link SVGLength#getUnitType()}.
*/
@Override
public short getCSSUnitType() {
if (hasAnimVal) {
return super.getCSSUnitType();
}
return getBaseVal().getCSSUnitType();
}

@Override
public short getUnitType() {
if (hasAnimVal) {
Expand All @@ -350,9 +356,6 @@ public short getUnitType() {
return getBaseVal().getUnitType();
}

/**
* <b>DOM</b>: Implements {@link SVGLength#getValue()}.
*/
@Override
public float getValue() {
if (hasAnimVal) {
Expand All @@ -361,9 +364,6 @@ public float getValue() {
return getBaseVal().getValue();
}

/**
* <b>DOM</b>: Implements {@link SVGLength#getValueInSpecifiedUnits()}.
*/
@Override
public float getValueInSpecifiedUnits() {
if (hasAnimVal) {
Expand All @@ -372,9 +372,6 @@ public float getValueInSpecifiedUnits() {
return getBaseVal().getValueInSpecifiedUnits();
}

/**
* <b>DOM</b>: Implements {@link SVGLength#getValueAsString()}.
*/
@Override
public String getValueAsString() {
if (hasAnimVal) {
Expand All @@ -383,41 +380,31 @@ public String getValueAsString() {
return getBaseVal().getValueAsString();
}

/**
* <b>DOM</b>: Implements {@link SVGLength#setValue(float)}.
*/
@Override
public void setValue(float value) throws DOMException {
throw element.createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly.length", null);
}

/**
* <b>DOM</b>: Implements {@link SVGLength#setValueInSpecifiedUnits(float)}.
*/
@Override
public void setValueInSpecifiedUnits(float value) throws DOMException {
throw element.createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly.length", null);
}

/**
* <b>DOM</b>: Implements {@link SVGLength#setValueAsString(String)}.
*/
@Override
public void setValueAsString(String value) throws DOMException {
throw element.createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly.length", null);
}

/**
* <b>DOM</b>: Implements {@link SVGLength#newValueSpecifiedUnits(short,float)}.
*/
@Override
public void newValueSpecifiedCSSUnits(short unit, float value) {
throw element.createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly.length", null);
}

@Override
public void newValueSpecifiedUnits(short unit, float value) {
throw element.createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly.length", null);
}

/**
* <b>DOM</b>: Implements {@link SVGLength#convertToSpecifiedUnits(short)}.
*/
@Override
public void convertToSpecifiedUnits(short unit) {
throw element.createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly.length", null);
Expand All @@ -434,11 +421,11 @@ protected SVGOMElement getAssociatedElement() {
/**
* Sets the animated value.
*
* @param type one of the values defines in org.w3c.dom.svg.SVGLength
* @param val the length
* @param type one of the values defines in {@link org.w3c.css.om.unit.CSSUnit CSSUnit}.
* @param val the length.
*/
protected void setAnimatedValue(int type, float val) {
super.newValueSpecifiedUnits((short) type, val);
super.newValueSpecifiedCSSUnits((short) type, val);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/
package io.sf.carte.echosvg.anim.dom;

import org.w3c.css.om.unit.CSSUnit;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGLength;

import io.sf.carte.doc.style.css.property.NumberValue;
import io.sf.carte.echosvg.parser.LengthParser;
import io.sf.carte.echosvg.parser.ParseException;
import io.sf.carte.echosvg.parser.UnitProcessor;
Expand Down Expand Up @@ -60,7 +62,7 @@ public abstract class AbstractSVGLength implements SVGLength {
public static final short OTHER_LENGTH = UnitProcessor.OTHER_LENGTH;

/**
* The type of this length.
* The unit of this length, as one of the constants in {@link CSSUnit}.
*/
protected short unitType;

Expand All @@ -79,11 +81,6 @@ public abstract class AbstractSVGLength implements SVGLength {
*/
protected UnitProcessor.Context context;

/**
* The unit string representations.
*/
protected static final String[] UNITS = { "", "", "%", "em", "ex", "px", "cm", "mm", "in", "pt", "pc" };

/**
* Return the SVGElement associated to this length.
*/
Expand All @@ -96,26 +93,68 @@ public AbstractSVGLength(short direction) {
context = new DefaultContext();
this.direction = direction;
this.value = 0.0f;
this.unitType = SVGLength.SVG_LENGTHTYPE_NUMBER;
this.unitType = CSSUnit.CSS_NUMBER;
}

/**
* <b>DOM</b>: Implements {@link SVGLength#getUnitType()}.
*/
@Override
public short getUnitType() {
public short getCSSUnitType() {
revalidate();
return unitType;
}

@Override
public short getUnitType() {
return getSVGUnitType(getCSSUnitType());
}

private static short getSVGUnitType(short cssUnit) {
short svgUnit;
switch (cssUnit) {
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;
}

/**
* <b>DOM</b>: Implements {@link SVGLength#getValue()}.
*/
@Override
public float getValue() {
revalidate();
try {
return UnitProcessor.svgToUserSpace(value, unitType, direction, context);
return UnitProcessor.cssToUserSpace(value, unitType, direction, context);
} catch (IllegalArgumentException ex) {
// XXX Should we throw an exception here when the length
// type is unknown?
Expand All @@ -128,7 +167,7 @@ public float getValue() {
*/
@Override
public void setValue(float value) throws DOMException {
this.value = UnitProcessor.userSpaceToSVG(value, unitType, direction, context);
this.value = UnitProcessor.userSpaceToCSS(value, unitType, direction, context);
reset();
}

Expand Down Expand Up @@ -157,10 +196,10 @@ public void setValueInSpecifiedUnits(float value) throws DOMException {
@Override
public String getValueAsString() {
revalidate();
if (unitType == SVGLength.SVG_LENGTHTYPE_UNKNOWN) {
if (unitType == CSSUnit.CSS_INVALID) {
return "";
}
return Float.toString(value) + UNITS[unitType];
return Float.toString(value) + CSSUnit.dimensionUnitString(unitType);
}

/**
Expand All @@ -176,18 +215,60 @@ public void setValueAsString(String value) throws DOMException {
* <b>DOM</b>: Implements {@link SVGLength#newValueSpecifiedUnits(short,float)}.
*/
@Override
public void newValueSpecifiedUnits(short unit, float value) {
public void newValueSpecifiedCSSUnits(short unit, float value) {
unitType = unit;
this.value = value;
reset();
}

/**
* <b>DOM</b>: Implements {@link SVGLength#convertToSpecifiedUnits(short)}.
*/
@Override
public void convertToSpecifiedUnits(short unit) {
public void newValueSpecifiedUnits(short unitType, float valueInSpecifiedUnits) throws DOMException {
newValueSpecifiedCSSUnits(getCSSUnitType(unitType), valueInSpecifiedUnits);
}

private static short getCSSUnitType(short svgUnit) throws DOMException {
short cssUnit;
switch (svgUnit) {
case SVGLength.SVG_LENGTHTYPE_NUMBER:
cssUnit = CSSUnit.CSS_NUMBER;
break;
case SVGLength.SVG_LENGTHTYPE_PX:
cssUnit = CSSUnit.CSS_PX;
break;
case SVGLength.SVG_LENGTHTYPE_EMS:
cssUnit = CSSUnit.CSS_EM;
break;
case SVGLength.SVG_LENGTHTYPE_EXS:
cssUnit = CSSUnit.CSS_EX;
break;
case SVGLength.SVG_LENGTHTYPE_IN:
cssUnit = CSSUnit.CSS_IN;
break;
case SVGLength.SVG_LENGTHTYPE_CM:
cssUnit = CSSUnit.CSS_CM;
break;
case SVGLength.SVG_LENGTHTYPE_MM:
cssUnit = CSSUnit.CSS_MM;
break;
case SVGLength.SVG_LENGTHTYPE_PC:
cssUnit = CSSUnit.CSS_PC;
break;
case SVGLength.SVG_LENGTHTYPE_PT:
cssUnit = CSSUnit.CSS_PT;
break;
case SVGLength.SVG_LENGTHTYPE_PERCENTAGE:
cssUnit = CSSUnit.CSS_PERCENTAGE;
break;
default:
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unit not supported: " + svgUnit);
}
return cssUnit;
}

@Override
public void convertToSpecifiedUnits(short unit) throws DOMException {
float v = getValue();
v = NumberValue.floatValueConversion(v, unitType, getCSSUnitType(unit));
unitType = unit;
setValue(v);
}
Expand Down Expand Up @@ -224,7 +305,7 @@ protected void parse(String s) {
unitType = ur.unit;
value = ur.value;
} catch (ParseException e) {
unitType = SVG_LENGTHTYPE_UNKNOWN;
unitType = CSSUnit.CSS_INVALID;
value = 0;
}
}
Expand Down Expand Up @@ -263,6 +344,24 @@ public float getXHeight() {
return 0.5f;
}

@Override
public float getLineHeight() {
return getAssociatedElement().getSVGContext().getLineHeight();
}

/**
* Returns the :root font-size value.
*/
@Override
public float getRootFontSize() {
return getAssociatedElement().getSVGContext().getRootFontSize();
}

@Override
public float getRootLineHeight() {
return getAssociatedElement().getSVGContext().getRootLineHeight();
}

/**
* Returns the viewport width used to compute units.
*/
Expand Down
Loading

0 comments on commit 54b14d6

Please sign in to comment.