Skip to content

Commit

Permalink
css: full support for lh,rlh,rem,rex,vh,vw,vmin,vmax length units
Browse files Browse the repository at this point in the history
The viewport-based units lack updates via events. This could be done by implementing
the Visual Viewport API but is a lot of work:

- https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API
- https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport
  • Loading branch information
carlosame committed Aug 21, 2024
1 parent 943374b commit 1ee68e7
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Set;

import org.w3c.css.om.unit.CSSUnit;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.css.CSSStyleDeclaration;
Expand All @@ -37,6 +38,7 @@
import org.w3c.dom.svg.SVGLength;
import org.w3c.dom.svg.SVGPreserveAspectRatio;

import io.sf.carte.doc.style.css.property.NumberValue;
import io.sf.carte.echosvg.anim.AnimationEngine;
import io.sf.carte.echosvg.anim.AnimationException;
import io.sf.carte.echosvg.anim.dom.AnimationTarget;
Expand Down Expand Up @@ -1815,8 +1817,14 @@ protected AnimatableValue createAnimatableValue(AnimationTarget target, String p
unit = SVGAngle.SVG_ANGLETYPE_GRAD;
break;
default:
// XXX Do something better than returning null.
return null;
try {
float f = NumberValue.floatValueConversion(v.getFloatValue(), v.getCSSUnit(),
CSSUnit.CSS_DEG);
return new AnimatableAngleOrIdentValue(target, f, SVGAngle.SVG_ANGLETYPE_DEG);
} catch (DOMException e) {
// XXX Do something better than returning null.
return null;
}
}
return new AnimatableAngleValue(target, v.getFloatValue(), unit);
}
Expand Down Expand Up @@ -1846,8 +1854,14 @@ protected AnimatableValue createAnimatableValue(AnimationTarget target, String p
unit = SVGAngle.SVG_ANGLETYPE_GRAD;
break;
default:
// XXX Do something better than returning null.
return null;
try {
float f = NumberValue.floatValueConversion(v.getFloatValue(), v.getCSSUnit(),
CSSUnit.CSS_DEG);
return new AnimatableAngleOrIdentValue(target, f, SVGAngle.SVG_ANGLETYPE_DEG);
} catch (DOMException e) {
// XXX Do something better than returning null.
return null;
}
}
return new AnimatableAngleOrIdentValue(target, v.getFloatValue(), unit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.w3c.css.om.unit.CSSUnit;
import org.w3c.dom.DOMException;

import io.sf.carte.doc.style.css.property.NumberValue;
import io.sf.carte.echosvg.css.engine.value.ColorValue;
import io.sf.carte.echosvg.css.engine.value.RectValue;
import io.sf.carte.echosvg.css.engine.value.Value;
Expand Down Expand Up @@ -108,16 +109,24 @@ public float getFloatValue() throws DOMException {
* Converts the actual float value to the given unit type.
*/
public static float convertFloatValue(short unitType, Value value) {
if (value.getCSSUnit() == unitType) {
return value.getFloatValue();
}
switch (unitType) {
case CSSUnit.CSS_NUMBER:
case CSSUnit.CSS_PERCENTAGE:
case CSSUnit.CSS_EM:
case CSSUnit.CSS_EX:
case CSSUnit.CSS_OTHER:
case CSSUnit.CSS_PX:
if (value.getCSSUnit() == unitType) {
return value.getFloatValue();
}
case CSSUnit.CSS_REM:
case CSSUnit.CSS_REX:
case CSSUnit.CSS_LH:
case CSSUnit.CSS_RLH:
case CSSUnit.CSS_VW:
case CSSUnit.CSS_VH:
case CSSUnit.CSS_VMIN:
case CSSUnit.CSS_VMAX:
case CSSUnit.CSS_OTHER:
break;
case CSSUnit.CSS_CM:
return toCentimeters(value);
Expand All @@ -143,6 +152,9 @@ public static float convertFloatValue(short unitType, Value value) {
return toHertz(value);
case CSSUnit.CSS_KHZ:
return tokHertz(value);
default:
return NumberValue.floatValueConversion(value.getFloatValue(), value.getCSSUnit(),
unitType);
}
throw new DOMException(DOMException.INVALID_ACCESS_ERR, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.w3c.css.om.typed.CSSKeywordValue;
import org.w3c.css.om.typed.CSSStringValue;
import org.w3c.css.om.typed.CSSStyleValue;
import org.w3c.css.om.unit.CSSUnit;
import org.w3c.dom.DOMException;

import org.w3c.css.om.unit.CSSUnit;
import io.sf.carte.doc.style.css.property.KeywordValue;
import io.sf.carte.echosvg.css.engine.value.ListValue;
import io.sf.carte.echosvg.css.engine.value.NumericValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1249,9 +1249,9 @@ protected void parseStyleSheet(StyleSheet ss, InputSource is, ParsedURL uri) thr
/**
* Puts an author property from a style-map in another style-map, if possible.
*/
protected void putAuthorProperty(StyleMap dest, int idx, Value sval, boolean imp, short origin) {
protected void putAuthorProperty(StyleMap dest, int idx, Value sval, boolean imp, int origin) {
Value dval = dest.getValue(idx);
short dorg = dest.getOrigin(idx);
int dorg = dest.getOrigin(idx);
boolean dimp = dest.isImportant(idx);

boolean cond = dval == null;
Expand Down Expand Up @@ -1313,7 +1313,7 @@ protected void addMatchingRules(List<Rule> rules, StyleSheet ss, SelectorMatcher
/**
* Adds the rules contained in the given list to a stylemap.
*/
protected void addRules(SelectorMatcher matcher, StyleMap sm, ArrayList<Rule> rules, short origin) {
protected void addRules(SelectorMatcher matcher, StyleMap sm, ArrayList<Rule> rules, int origin) {
sortRules(rules, matcher);

if (origin == StyleMap.AUTHOR_ORIGIN) {
Expand Down Expand Up @@ -2007,7 +2007,7 @@ protected void inlineStyleAttributeUpdated(CSSStylableElement elt, StyleMap styl
// come from the inline style attribute or override style.
for (int i = getNumberOfProperties() - 1; i >= 0; --i) {
if (style.isComputed(i) && !updated[i]) {
short origin = style.getOrigin(i);
int origin = style.getOrigin(i);
if (origin >= StyleMap.INLINE_AUTHOR_ORIGIN) { // ToDo Jlint says: always same result ??
removed = true;
updated[i] = true;
Expand All @@ -2024,12 +2024,14 @@ protected void inlineStyleAttributeUpdated(CSSStylableElement elt, StyleMap styl
boolean fs = (fontSizeIndex == -1) ? false : updated[fontSizeIndex];
boolean lh = (lineHeightIndex == -1) ? false : updated[lineHeightIndex];
boolean cl = (colorIndex == -1) ? false : updated[colorIndex];
boolean isRoot = elt.getOwnerDocument().getDocumentElement() == elt;

for (int i = getNumberOfProperties() - 1; i >= 0; --i) {
if (updated[i]) {
count++;
} else if ((fs && style.isFontSizeRelative(i)) || (lh && style.isLineHeightRelative(i))
|| (cl && style.isColorRelative(i))) {
|| (cl && style.isColorRelative(i)) || (fs && isRoot && style.isRootFontSizeRelative(i))
|| (lh && isRoot && style.isRootLineHeightRelative(i))) {
updated[i] = true;
clearComputedValue(style, i);
count++;
Expand Down Expand Up @@ -2170,13 +2172,14 @@ protected void propagateChanges(Node node, int[] props, boolean recascade) {
boolean fs = (fontSizeIndex == -1) ? false : updated[fontSizeIndex];
boolean lh = (lineHeightIndex == -1) ? false : updated[lineHeightIndex];
boolean cl = (colorIndex == -1) ? false : updated[colorIndex];
boolean isRootFs = fs && elt.getOwnerDocument().getDocumentElement() == elt;

int count = 0;
for (int i = getNumberOfProperties() - 1; i >= 0; --i) {
if (updated[i]) {
count++;
} else if ((fs && style.isFontSizeRelative(i)) || (lh && style.isLineHeightRelative(i))
|| (cl && style.isColorRelative(i))) {
|| (cl && style.isColorRelative(i)) || (isRootFs && style.isRootFontSizeRelative(i))) {
updated[i] = true;
clearComputedValue(style, i);
count++;
Expand Down Expand Up @@ -2265,7 +2268,7 @@ public void property(String name, LexicalUnit value, boolean important) {
updatedProperties[i] = true;

Value v = valueManagers[i].createValue(value, CSSEngine.this);
styleMap.putMask(i, (short) 0);
styleMap.putMask(i, 0);
styleMap.putValue(i, v);
styleMap.putOrigin(i, StyleMap.INLINE_AUTHOR_ORIGIN);
}
Expand Down Expand Up @@ -2300,7 +2303,7 @@ protected void nonCSSPresentationalHintUpdated(CSSStylableElement elt, StyleMap
lu = parser.parsePropertyValue(new StringReader(newValue));
ValueManager vm = valueManagers[idx];
Value v = vm.createValue(lu, CSSEngine.this);
style.putMask(idx, (short) 0);
style.putMask(idx, 0);
style.putValue(idx, v);
style.putOrigin(idx, StyleMap.NON_CSS_ORIGIN);
} catch (Exception e) {
Expand Down Expand Up @@ -2338,13 +2341,14 @@ protected void nonCSSPresentationalHintUpdated(CSSStylableElement elt, StyleMap
boolean fs = idx == fontSizeIndex;
boolean lh = idx == lineHeightIndex;
boolean cl = idx == colorIndex;
boolean isRootFs = fs && elt.getOwnerDocument().getDocumentElement() == elt;
int count = 0;

for (int i = getNumberOfProperties() - 1; i >= 0; --i) {
if (updated[i]) {
count++;
} else if ((fs && style.isFontSizeRelative(i)) || (lh && style.isLineHeightRelative(i))
|| (cl && style.isColorRelative(i))) {
|| (cl && style.isColorRelative(i)) || (isRootFs && style.isRootFontSizeRelative(i))) {
updated[i] = true;
clearComputedValue(style, i);
count++;
Expand Down
Loading

0 comments on commit 1ee68e7

Please sign in to comment.