// // @author Yves Lafon // // (c) COPYRIGHT MIT, ERCIM, Keio, Beihang, 2020. // Please first read the full copyright statement in file COPYRIGHT.html package org.w3c.css.values; import org.w3c.css.util.ApplContext; import org.w3c.css.util.InvalidParamException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Locale; /** * CSS mathfunction(). * * @spec https://www.w3.org/TR/2019/WD-css-values-4-20190131/#funcdef-max */ public class CssMathFunction extends CssCheckableValue { public static final int type = CssTypes.CSS_MATH_FUNCTION; public final int getRawType() { return type; } public final int getType() { if (computed_type == CssTypes.CSS_MATH_FUNCTION) { return type; } return computed_type; } public static final CssIdent[] rounding_values; static { String[] _allowed_rounding_values = {"nearest", "up", "down", "to-zero"}; int i = 0; rounding_values = new CssIdent[_allowed_rounding_values.length]; for (String s : _allowed_rounding_values) { rounding_values[i++] = CssIdent.getIdent(s); } } public static final boolean isAllowedRounding(CssIdent ident) { for (CssIdent id : rounding_values) { if (id.equals(ident)) { return true; } } return false; } ApplContext ac; int computed_type = CssTypes.CSS_UNKNOWN; ArrayList values = null; String prefix = null; String _toString = null; /** * Create a new CssCalc */ public CssMathFunction(String prefix) { this.prefix = prefix.toLowerCase(Locale.ENGLISH); } public CssMathFunction(ApplContext ac, String prefix) { this(ac, prefix, null); } public CssMathFunction(String prefix, CssValue value) { this(null, prefix, value); } public CssMathFunction(ApplContext ac, String prefix, CssValue value) { if (ac != null) { this.ac = ac; } if (prefix != null) { this.prefix = prefix.toLowerCase(Locale.ENGLISH); } if (value != null) { computed_type = value.getType(); if (values == null) { values = new ArrayList<>(); } values.add(value); try { computed_type = _checkAcceptableType(value.getType()); } catch (Exception ex) { // todo report error here or wait ? } } } public void set(String s, ApplContext ac) throws InvalidParamException { // we don't support this way of setting the value // as we rely on the parsing to create it incrementally throw new InvalidParamException("unrecognize", s, ac); } public void setValue(BigDecimal d) { // we don't support this way of setting the value // as we rely on the parsing to create it incrementally } /** * Add one operand, if we already got one we will... Add one operand. * * @param value * @return */ public CssMathFunction addValue(CssValue value) throws InvalidParamException { boolean first = false; if (values == null) { values = new ArrayList<>(); first = true; } values.add(value); _computeResultingType(false); return this; } public void validate() throws InvalidParamException { _computeResultingType(true); } private int _checkAcceptableType(int type) throws InvalidParamException { // , , ,