#ifndef AnimatableLength_h
#define AnimatableLength_h
#include "core/animation/AnimatableValue.h"
#include "core/css/CSSCalculationValue.h"
#include "core/css/CSSPrimitiveValue.h"
#include "platform/Length.h"
namespace WebCore {
enum NumberRange {
AllValues,
NonNegativeValues,
};
class AnimatableLength FINAL : public AnimatableValue {
public:
enum NumberUnitType {
UnitTypeCalc,
UnitTypePixels,
UnitTypePercentage,
UnitTypeFontSize,
UnitTypeFontXSize,
UnitTypeRootFontSize,
UnitTypeViewportWidth,
UnitTypeViewportHeight,
UnitTypeViewportMin,
UnitTypeViewportMax,
};
virtual ~AnimatableLength() { }
static bool canCreateFrom(const CSSValue*);
static PassRefPtrWillBeRawPtr<AnimatableLength> create(CSSValue*);
static PassRefPtrWillBeRawPtr<AnimatableLength> create(double number, NumberUnitType unitType, CSSPrimitiveValue* cssPrimitiveValue = 0)
{
return adoptRefWillBeNoop(new AnimatableLength(number, unitType, cssPrimitiveValue));
}
static PassRefPtrWillBeRawPtr<AnimatableLength> create(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> calcExpression, CSSPrimitiveValue* cssPrimitiveValue = 0)
{
return adoptRefWillBeNoop(new AnimatableLength(calcExpression, cssPrimitiveValue));
}
PassRefPtrWillBeRawPtr<CSSValue> toCSSValue(NumberRange = AllValues) const;
Length toLength(const CSSToLengthConversionData&, NumberRange = AllValues) const;
virtual void trace(Visitor*) OVERRIDE;
protected:
virtual PassRefPtrWillBeRawPtr<AnimatableValue> interpolateTo(const AnimatableValue*, double fraction) const OVERRIDE;
virtual PassRefPtrWillBeRawPtr<AnimatableValue> addWith(const AnimatableValue*) const OVERRIDE;
virtual bool usesDefaultInterpolationWith(const AnimatableValue*) const OVERRIDE;
private:
AnimatableLength(double number, NumberUnitType unitType, CSSPrimitiveValue* cssPrimitiveValue)
: m_number(number)
, m_unitType(unitType)
, m_cachedCSSPrimitiveValue(cssPrimitiveValue)
{
ASSERT(m_unitType != UnitTypeCalc);
}
AnimatableLength(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> calcExpression, CSSPrimitiveValue* cssPrimitiveValue)
: m_unitType(UnitTypeCalc)
, m_calcExpression(calcExpression)
, m_cachedCSSPrimitiveValue(cssPrimitiveValue)
{
ASSERT(m_calcExpression);
}
virtual AnimatableType type() const OVERRIDE { return TypeLength; }
virtual bool equalTo(const AnimatableValue*) const OVERRIDE;
bool isCalc() const
{
return m_unitType == UnitTypeCalc;
}
bool isViewportUnit() const
{
return m_unitType == UnitTypeViewportWidth || m_unitType == UnitTypeViewportHeight || m_unitType == UnitTypeViewportMin || m_unitType == UnitTypeViewportMax;
}
static PassRefPtrWillBeRawPtr<AnimatableLength> create(const AnimatableLength* leftAddend, const AnimatableLength* rightAddend)
{
ASSERT(leftAddend && rightAddend);
return create(CSSCalcValue::createExpressionNode(leftAddend->toCSSCalcExpressionNode(), rightAddend->toCSSCalcExpressionNode(), CalcAdd));
}
PassRefPtrWillBeRawPtr<CSSPrimitiveValue> toCSSPrimitiveValue(NumberRange) const;
PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> toCSSCalcExpressionNode() const;
PassRefPtrWillBeRawPtr<AnimatableLength> scale(double) const;
double clampedNumber(NumberRange range) const
{
ASSERT(!isCalc());
return (range == NonNegativeValues && m_number <= 0) ? 0 : m_number;
}
static bool primitiveUnitToNumberType(unsigned short primitiveUnit, NumberUnitType& numberType);
static unsigned short numberTypeToPrimitiveUnit(NumberUnitType numberType);
bool isUnitlessZero() const
{
return !isCalc() && !m_number && m_unitType != UnitTypePercentage;
}
NumberUnitType commonUnitType(const AnimatableLength* length) const
{
if (m_unitType == length->m_unitType)
return m_unitType;
if (isUnitlessZero())
return length->m_unitType;
if (length->isUnitlessZero())
return m_unitType;
return UnitTypeCalc;
}
double m_number;
const NumberUnitType m_unitType;
RefPtrWillBeMember<CSSCalcExpressionNode> m_calcExpression;
mutable RefPtrWillBeMember<CSSPrimitiveValue> m_cachedCSSPrimitiveValue;
friend class AnimationAnimatableLengthTest;
};
DEFINE_ANIMATABLE_VALUE_TYPE_CASTS(AnimatableLength, isLength());
}
#endif