2010-05-11 19:49:43 +04:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-05-11 19:49:43 +04:00
|
|
|
#ifndef CSSCalc_h_
|
|
|
|
#define CSSCalc_h_
|
|
|
|
|
|
|
|
#include "nsCSSValue.h"
|
2010-08-11 23:32:52 +04:00
|
|
|
#include "nsStyleCoord.h"
|
2010-05-11 19:49:43 +04:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace css {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ComputeCalc computes the result of a calc() expression tree.
|
|
|
|
*
|
|
|
|
* It is templatized over a CalcOps class that is expected to provide:
|
|
|
|
*
|
2010-07-03 08:18:55 +04:00
|
|
|
* // input_type and input_array_type have a bunch of very specific
|
|
|
|
* // expectations (which happen to be met by two classes (nsCSSValue
|
|
|
|
* // and nsStyleCoord). There must be methods (roughly):
|
|
|
|
* // input_array_type* input_type::GetArrayValue();
|
2012-08-22 19:56:38 +04:00
|
|
|
* // uint32_t input_array_type::Count() const;
|
|
|
|
* // input_type& input_array_type::Item(uint32_t);
|
2010-07-03 08:18:55 +04:00
|
|
|
* typedef ... input_type;
|
|
|
|
* typedef ... input_array_type;
|
|
|
|
*
|
2010-05-11 19:49:43 +04:00
|
|
|
* typedef ... result_type;
|
|
|
|
*
|
2010-07-03 08:18:55 +04:00
|
|
|
* // GetUnit(avalue) must return the correct nsCSSUnit for any
|
|
|
|
* // value that represents a calc tree node (eCSSUnit_Calc*). For
|
|
|
|
* // other nodes, it may return any non eCSSUnit_Calc* unit.
|
|
|
|
* static nsCSSUnit GetUnit(const input_type& aValue);
|
|
|
|
*
|
2010-05-11 19:49:44 +04:00
|
|
|
* result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
* MergeAdditive(nsCSSUnit aCalcFunction,
|
|
|
|
* result_type aValue1, result_type aValue2);
|
|
|
|
*
|
2010-05-11 19:49:44 +04:00
|
|
|
* result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
* MergeMultiplicativeL(nsCSSUnit aCalcFunction,
|
|
|
|
* float aValue1, result_type aValue2);
|
|
|
|
*
|
2010-05-11 19:49:44 +04:00
|
|
|
* result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
* MergeMultiplicativeR(nsCSSUnit aCalcFunction,
|
|
|
|
* result_type aValue1, float aValue2);
|
|
|
|
*
|
2010-05-11 19:49:44 +04:00
|
|
|
* result_type
|
2010-07-03 08:18:55 +04:00
|
|
|
* ComputeLeafValue(const input_type& aValue);
|
2010-05-11 19:49:43 +04:00
|
|
|
*
|
2010-05-11 19:49:44 +04:00
|
|
|
* float
|
2010-07-03 08:18:55 +04:00
|
|
|
* ComputeNumber(const input_type& aValue);
|
2010-05-11 19:49:43 +04:00
|
|
|
*
|
|
|
|
* The CalcOps methods might compute the calc() expression down to a
|
|
|
|
* number, reduce some parts of it to a number but replicate other
|
|
|
|
* parts, or produce a tree with a different data structure (for
|
|
|
|
* example, nsCSS* for specified values vs nsStyle* for computed
|
|
|
|
* values).
|
|
|
|
*
|
|
|
|
* For each leaf in the calc() expression, ComputeCalc will call either
|
|
|
|
* ComputeNumber (when the leaf is the left side of a Times_L or the
|
|
|
|
* right side of a Times_R or Divided) or ComputeLeafValue (otherwise).
|
|
|
|
* (The CalcOps in the CSS parser that reduces purely numeric
|
|
|
|
* expressions in turn calls ComputeCalc on numbers; other ops can
|
|
|
|
* presume that expressions in the number positions have already been
|
|
|
|
* normalized to a single numeric value and derive from
|
|
|
|
* NumbersAlreadyNormalizedCalcOps.)
|
|
|
|
*
|
|
|
|
* For non-leaves, one of the Merge functions will be called:
|
2010-09-11 20:27:12 +04:00
|
|
|
* MergeAdditive for Plus and Minus
|
2010-05-11 19:49:43 +04:00
|
|
|
* MergeMultiplicativeL for Times_L (number * value)
|
|
|
|
* MergeMultiplicativeR for Times_R (value * number) and Divided
|
|
|
|
*/
|
|
|
|
template <class CalcOps>
|
|
|
|
static typename CalcOps::result_type
|
2010-07-03 08:18:55 +04:00
|
|
|
ComputeCalc(const typename CalcOps::input_type& aValue, CalcOps &aOps)
|
2010-05-11 19:49:43 +04:00
|
|
|
{
|
2010-07-03 08:18:55 +04:00
|
|
|
switch (CalcOps::GetUnit(aValue)) {
|
2010-05-11 19:49:43 +04:00
|
|
|
case eCSSUnit_Calc: {
|
2010-07-03 08:18:55 +04:00
|
|
|
typename CalcOps::input_array_type *arr = aValue.GetArrayValue();
|
2010-05-11 19:49:43 +04:00
|
|
|
NS_ABORT_IF_FALSE(arr->Count() == 1, "unexpected length");
|
2010-05-11 19:49:44 +04:00
|
|
|
return ComputeCalc(arr->Item(0), aOps);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
case eCSSUnit_Calc_Plus:
|
|
|
|
case eCSSUnit_Calc_Minus: {
|
2010-07-03 08:18:55 +04:00
|
|
|
typename CalcOps::input_array_type *arr = aValue.GetArrayValue();
|
2010-05-11 19:49:43 +04:00
|
|
|
NS_ABORT_IF_FALSE(arr->Count() == 2, "unexpected length");
|
2010-05-11 19:49:44 +04:00
|
|
|
typename CalcOps::result_type lhs = ComputeCalc(arr->Item(0), aOps),
|
|
|
|
rhs = ComputeCalc(arr->Item(1), aOps);
|
2010-07-03 08:18:55 +04:00
|
|
|
return aOps.MergeAdditive(CalcOps::GetUnit(aValue), lhs, rhs);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
case eCSSUnit_Calc_Times_L: {
|
2010-07-03 08:18:55 +04:00
|
|
|
typename CalcOps::input_array_type *arr = aValue.GetArrayValue();
|
2010-05-11 19:49:43 +04:00
|
|
|
NS_ABORT_IF_FALSE(arr->Count() == 2, "unexpected length");
|
2010-05-11 19:49:44 +04:00
|
|
|
float lhs = aOps.ComputeNumber(arr->Item(0));
|
|
|
|
typename CalcOps::result_type rhs = ComputeCalc(arr->Item(1), aOps);
|
2010-07-03 08:18:55 +04:00
|
|
|
return aOps.MergeMultiplicativeL(CalcOps::GetUnit(aValue), lhs, rhs);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
case eCSSUnit_Calc_Times_R:
|
|
|
|
case eCSSUnit_Calc_Divided: {
|
2010-07-03 08:18:55 +04:00
|
|
|
typename CalcOps::input_array_type *arr = aValue.GetArrayValue();
|
2010-05-11 19:49:43 +04:00
|
|
|
NS_ABORT_IF_FALSE(arr->Count() == 2, "unexpected length");
|
2010-05-11 19:49:44 +04:00
|
|
|
typename CalcOps::result_type lhs = ComputeCalc(arr->Item(0), aOps);
|
|
|
|
float rhs = aOps.ComputeNumber(arr->Item(1));
|
2010-07-03 08:18:55 +04:00
|
|
|
return aOps.MergeMultiplicativeR(CalcOps::GetUnit(aValue), lhs, rhs);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
default: {
|
2010-05-11 19:49:44 +04:00
|
|
|
return aOps.ComputeLeafValue(aValue);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-03 08:18:55 +04:00
|
|
|
/**
|
|
|
|
* The input unit operation for input_type being nsCSSValue.
|
|
|
|
*/
|
|
|
|
struct CSSValueInputCalcOps
|
|
|
|
{
|
|
|
|
typedef nsCSSValue input_type;
|
|
|
|
typedef nsCSSValue::Array input_array_type;
|
|
|
|
|
|
|
|
static nsCSSUnit GetUnit(const nsCSSValue& aValue)
|
|
|
|
{
|
|
|
|
return aValue.GetUnit();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-05-11 19:49:43 +04:00
|
|
|
/**
|
|
|
|
* Basic*CalcOps provide a partial implementation of the CalcOps
|
|
|
|
* template parameter to ComputeCalc, for those callers whose merging
|
|
|
|
* just consists of mathematics (rather than tree construction).
|
|
|
|
*/
|
2010-07-03 08:18:55 +04:00
|
|
|
|
|
|
|
struct BasicCoordCalcOps
|
2010-05-11 19:49:43 +04:00
|
|
|
{
|
2010-07-03 08:18:55 +04:00
|
|
|
typedef nscoord result_type;
|
2010-05-11 19:49:43 +04:00
|
|
|
|
2010-05-11 19:49:44 +04:00
|
|
|
result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
MergeAdditive(nsCSSUnit aCalcFunction,
|
|
|
|
result_type aValue1, result_type aValue2)
|
|
|
|
{
|
|
|
|
if (aCalcFunction == eCSSUnit_Calc_Plus) {
|
2010-07-03 08:18:55 +04:00
|
|
|
return NSCoordSaturatingAdd(aValue1, aValue2);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
2010-09-11 20:27:12 +04:00
|
|
|
NS_ABORT_IF_FALSE(aCalcFunction == eCSSUnit_Calc_Minus,
|
2010-05-11 19:49:43 +04:00
|
|
|
"unexpected unit");
|
2010-09-11 20:27:12 +04:00
|
|
|
return NSCoordSaturatingSubtract(aValue1, aValue2, 0);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
|
2010-05-11 19:49:44 +04:00
|
|
|
result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
MergeMultiplicativeL(nsCSSUnit aCalcFunction,
|
|
|
|
float aValue1, result_type aValue2)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aCalcFunction == eCSSUnit_Calc_Times_L,
|
|
|
|
"unexpected unit");
|
2010-07-03 08:18:55 +04:00
|
|
|
return NSCoordSaturatingMultiply(aValue2, aValue1);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
|
2010-05-11 19:49:44 +04:00
|
|
|
result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
MergeMultiplicativeR(nsCSSUnit aCalcFunction,
|
|
|
|
result_type aValue1, float aValue2)
|
|
|
|
{
|
2010-07-03 08:18:55 +04:00
|
|
|
NS_ABORT_IF_FALSE(aCalcFunction == eCSSUnit_Calc_Times_R ||
|
|
|
|
aCalcFunction == eCSSUnit_Calc_Divided,
|
2010-05-11 19:49:43 +04:00
|
|
|
"unexpected unit");
|
2010-07-03 08:18:55 +04:00
|
|
|
if (aCalcFunction == eCSSUnit_Calc_Divided) {
|
|
|
|
aValue2 = 1.0f / aValue2;
|
|
|
|
}
|
|
|
|
return NSCoordSaturatingMultiply(aValue1, aValue2);
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-03 08:18:55 +04:00
|
|
|
struct BasicFloatCalcOps
|
2010-05-11 19:49:43 +04:00
|
|
|
{
|
2010-07-03 08:18:55 +04:00
|
|
|
typedef float result_type;
|
|
|
|
|
|
|
|
result_type
|
|
|
|
MergeAdditive(nsCSSUnit aCalcFunction,
|
|
|
|
result_type aValue1, result_type aValue2)
|
|
|
|
{
|
|
|
|
if (aCalcFunction == eCSSUnit_Calc_Plus) {
|
|
|
|
return aValue1 + aValue2;
|
|
|
|
}
|
2010-09-11 20:27:12 +04:00
|
|
|
NS_ABORT_IF_FALSE(aCalcFunction == eCSSUnit_Calc_Minus,
|
2010-07-03 08:18:55 +04:00
|
|
|
"unexpected unit");
|
2010-09-11 20:27:12 +04:00
|
|
|
return aValue1 - aValue2;
|
2010-07-03 08:18:55 +04:00
|
|
|
}
|
|
|
|
|
2010-05-11 19:49:44 +04:00
|
|
|
result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
MergeMultiplicativeL(nsCSSUnit aCalcFunction,
|
|
|
|
float aValue1, result_type aValue2)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aCalcFunction == eCSSUnit_Calc_Times_L,
|
|
|
|
"unexpected unit");
|
|
|
|
return aValue1 * aValue2;
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:49:44 +04:00
|
|
|
result_type
|
2010-05-11 19:49:43 +04:00
|
|
|
MergeMultiplicativeR(nsCSSUnit aCalcFunction,
|
|
|
|
result_type aValue1, float aValue2)
|
|
|
|
{
|
|
|
|
if (aCalcFunction == eCSSUnit_Calc_Times_R) {
|
|
|
|
return aValue1 * aValue2;
|
|
|
|
}
|
|
|
|
NS_ABORT_IF_FALSE(aCalcFunction == eCSSUnit_Calc_Divided,
|
|
|
|
"unexpected unit");
|
|
|
|
return aValue1 / aValue2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A ComputeNumber implementation for callers that can assume numbers
|
|
|
|
* are already normalized (i.e., anything past the parser).
|
|
|
|
*/
|
2010-07-03 08:18:55 +04:00
|
|
|
struct NumbersAlreadyNormalizedOps : public CSSValueInputCalcOps
|
2010-05-11 19:49:43 +04:00
|
|
|
{
|
2010-05-11 19:49:44 +04:00
|
|
|
float ComputeNumber(const nsCSSValue& aValue)
|
2010-05-11 19:49:43 +04:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aValue.GetUnit() == eCSSUnit_Number, "unexpected unit");
|
|
|
|
return aValue.GetFloatValue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-03 08:18:55 +04:00
|
|
|
/**
|
|
|
|
* SerializeCalc appends the serialization of aValue to a string.
|
|
|
|
*
|
|
|
|
* It is templatized over a CalcOps class that is expected to provide:
|
|
|
|
*
|
|
|
|
* // input_type and input_array_type have a bunch of very specific
|
|
|
|
* // expectations (which happen to be met by two classes (nsCSSValue
|
|
|
|
* // and nsStyleCoord). There must be methods (roughly):
|
|
|
|
* // input_array_type* input_type::GetArrayValue();
|
2012-08-22 19:56:38 +04:00
|
|
|
* // uint32_t input_array_type::Count() const;
|
|
|
|
* // input_type& input_array_type::Item(uint32_t);
|
2010-07-03 08:18:55 +04:00
|
|
|
* typedef ... input_type;
|
|
|
|
* typedef ... input_array_type;
|
|
|
|
*
|
|
|
|
* static nsCSSUnit GetUnit(const input_type& aValue);
|
|
|
|
*
|
|
|
|
* void Append(const char* aString);
|
|
|
|
* void AppendLeafValue(const input_type& aValue);
|
|
|
|
* void AppendNumber(const input_type& aValue);
|
|
|
|
*
|
|
|
|
* Data structures given may or may not have a toplevel eCSSUnit_Calc
|
|
|
|
* node representing a calc whose toplevel is not min() or max().
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <class CalcOps>
|
|
|
|
static void
|
|
|
|
SerializeCalcInternal(const typename CalcOps::input_type& aValue, CalcOps &aOps);
|
|
|
|
|
|
|
|
// Serialize the toplevel value in a calc() tree. See big comment
|
|
|
|
// above.
|
|
|
|
template <class CalcOps>
|
|
|
|
static void
|
|
|
|
SerializeCalc(const typename CalcOps::input_type& aValue, CalcOps &aOps)
|
|
|
|
{
|
2012-07-14 02:06:50 +04:00
|
|
|
aOps.Append("calc(");
|
2010-07-03 08:18:55 +04:00
|
|
|
nsCSSUnit unit = CalcOps::GetUnit(aValue);
|
|
|
|
if (unit == eCSSUnit_Calc) {
|
|
|
|
const typename CalcOps::input_array_type *array = aValue.GetArrayValue();
|
|
|
|
NS_ABORT_IF_FALSE(array->Count() == 1, "unexpected length");
|
|
|
|
SerializeCalcInternal(array->Item(0), aOps);
|
|
|
|
} else {
|
|
|
|
SerializeCalcInternal(aValue, aOps);
|
|
|
|
}
|
2010-09-11 20:27:12 +04:00
|
|
|
aOps.Append(")");
|
2010-07-03 08:18:55 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static inline bool
|
2010-07-03 08:18:55 +04:00
|
|
|
IsCalcAdditiveUnit(nsCSSUnit aUnit)
|
|
|
|
{
|
|
|
|
return aUnit == eCSSUnit_Calc_Plus ||
|
|
|
|
aUnit == eCSSUnit_Calc_Minus;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static inline bool
|
2010-07-03 08:18:55 +04:00
|
|
|
IsCalcMultiplicativeUnit(nsCSSUnit aUnit)
|
|
|
|
{
|
|
|
|
return aUnit == eCSSUnit_Calc_Times_L ||
|
|
|
|
aUnit == eCSSUnit_Calc_Times_R ||
|
|
|
|
aUnit == eCSSUnit_Calc_Divided;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize a non-toplevel value in a calc() tree. See big comment
|
|
|
|
// above.
|
|
|
|
template <class CalcOps>
|
|
|
|
/* static */ void
|
|
|
|
SerializeCalcInternal(const typename CalcOps::input_type& aValue, CalcOps &aOps)
|
|
|
|
{
|
|
|
|
nsCSSUnit unit = CalcOps::GetUnit(aValue);
|
2010-09-11 20:27:12 +04:00
|
|
|
if (IsCalcAdditiveUnit(unit)) {
|
2010-07-03 08:18:55 +04:00
|
|
|
const typename CalcOps::input_array_type *array = aValue.GetArrayValue();
|
|
|
|
NS_ABORT_IF_FALSE(array->Count() == 2, "unexpected length");
|
|
|
|
|
|
|
|
SerializeCalcInternal(array->Item(0), aOps);
|
|
|
|
|
|
|
|
if (eCSSUnit_Calc_Plus == unit) {
|
|
|
|
aOps.Append(" + ");
|
|
|
|
} else {
|
|
|
|
NS_ABORT_IF_FALSE(eCSSUnit_Calc_Minus == unit, "unexpected unit");
|
|
|
|
aOps.Append(" - ");
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool needParens = IsCalcAdditiveUnit(CalcOps::GetUnit(array->Item(1)));
|
2010-07-03 08:18:55 +04:00
|
|
|
if (needParens) {
|
|
|
|
aOps.Append("(");
|
|
|
|
}
|
|
|
|
SerializeCalcInternal(array->Item(1), aOps);
|
|
|
|
if (needParens) {
|
|
|
|
aOps.Append(")");
|
|
|
|
}
|
|
|
|
} else if (IsCalcMultiplicativeUnit(unit)) {
|
|
|
|
const typename CalcOps::input_array_type *array = aValue.GetArrayValue();
|
|
|
|
NS_ABORT_IF_FALSE(array->Count() == 2, "unexpected length");
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool needParens = IsCalcAdditiveUnit(CalcOps::GetUnit(array->Item(0)));
|
2010-07-03 08:18:55 +04:00
|
|
|
if (needParens) {
|
|
|
|
aOps.Append("(");
|
|
|
|
}
|
|
|
|
if (unit == eCSSUnit_Calc_Times_L) {
|
|
|
|
aOps.AppendNumber(array->Item(0));
|
|
|
|
} else {
|
|
|
|
SerializeCalcInternal(array->Item(0), aOps);
|
|
|
|
}
|
|
|
|
if (needParens) {
|
|
|
|
aOps.Append(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eCSSUnit_Calc_Times_L == unit || eCSSUnit_Calc_Times_R == unit) {
|
|
|
|
aOps.Append(" * ");
|
|
|
|
} else {
|
|
|
|
NS_ABORT_IF_FALSE(eCSSUnit_Calc_Divided == unit, "unexpected unit");
|
|
|
|
aOps.Append(" / ");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSUnit subUnit = CalcOps::GetUnit(array->Item(1));
|
|
|
|
needParens = IsCalcAdditiveUnit(subUnit) ||
|
|
|
|
IsCalcMultiplicativeUnit(subUnit);
|
|
|
|
if (needParens) {
|
|
|
|
aOps.Append("(");
|
|
|
|
}
|
|
|
|
if (unit == eCSSUnit_Calc_Times_L) {
|
|
|
|
SerializeCalcInternal(array->Item(1), aOps);
|
|
|
|
} else {
|
|
|
|
aOps.AppendNumber(array->Item(1));
|
|
|
|
}
|
|
|
|
if (needParens) {
|
|
|
|
aOps.Append(")");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aOps.AppendLeafValue(aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:49:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(CSSCalc_h_) */
|