This source file includes following definitions.
- create
- canCreateFrom
- toCSSValue
- toLength
- usesDefaultInterpolationWith
- interpolateTo
- addWith
- equalTo
- toCSSCalcExpressionNode
- isCompatibleWithRange
- toCSSPrimitiveValue
- scale
- primitiveUnitToNumberType
- numberTypeToPrimitiveUnit
- trace
#include "config.h"
#include "core/animation/AnimatableLength.h"
#include "core/css/CSSPrimitiveValueMappings.h"
#include "platform/CalculationValue.h"
#include "platform/animation/AnimationUtilities.h"
namespace WebCore {
PassRefPtrWillBeRawPtr<AnimatableLength> AnimatableLength::create(CSSValue* value)
{
ASSERT(canCreateFrom(value));
if (value->isPrimitiveValue()) {
CSSPrimitiveValue* primitiveValue = WebCore::toCSSPrimitiveValue(value);
const CSSCalcValue* calcValue = primitiveValue->cssCalcValue();
if (calcValue)
return create(calcValue->expressionNode(), primitiveValue);
NumberUnitType unitType;
bool isPrimitiveLength = primitiveUnitToNumberType(primitiveValue->primitiveType(), unitType);
ASSERT_UNUSED(isPrimitiveLength, isPrimitiveLength);
const double scale = CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(primitiveValue->primitiveType());
return create(primitiveValue->getDoubleValue() * scale, unitType, primitiveValue);
}
if (value->isCalcValue())
return create(toCSSCalcValue(value)->expressionNode());
ASSERT_NOT_REACHED();
return nullptr;
}
bool AnimatableLength::canCreateFrom(const CSSValue* value)
{
ASSERT(value);
if (value->isPrimitiveValue()) {
const CSSPrimitiveValue* primitiveValue = WebCore::toCSSPrimitiveValue(value);
if (primitiveValue->cssCalcValue())
return true;
NumberUnitType unitType;
return primitiveUnitToNumberType(primitiveValue->primitiveType(), unitType);
}
return value->isCalcValue();
}
PassRefPtrWillBeRawPtr<CSSValue> AnimatableLength::toCSSValue(NumberRange range) const
{
return toCSSPrimitiveValue(range);
}
Length AnimatableLength::toLength(const CSSToLengthConversionData& conversionData, NumberRange range) const
{
if (m_unitType == UnitTypePixels)
return Length(clampedNumber(range) * conversionData.zoom(), Fixed);
if (m_unitType == UnitTypePercentage)
return Length(clampedNumber(range), Percent);
return toCSSPrimitiveValue(range)->convertToLength<AnyConversion>(conversionData);
}
bool AnimatableLength::usesDefaultInterpolationWith(const AnimatableValue* value) const
{
const AnimatableLength* length = toAnimatableLength(value);
NumberUnitType type = commonUnitType(length);
return type == UnitTypeCalc && (isViewportUnit() || length->isViewportUnit());
}
PassRefPtrWillBeRawPtr<AnimatableValue> AnimatableLength::interpolateTo(const AnimatableValue* value, double fraction) const
{
const AnimatableLength* length = toAnimatableLength(value);
NumberUnitType type = commonUnitType(length);
if (type != UnitTypeCalc)
return AnimatableLength::create(blend(m_number, length->m_number, fraction), type);
if (isViewportUnit() || length->isViewportUnit())
return defaultInterpolateTo(this, value, fraction);
return AnimatableLength::create(scale(1 - fraction).get(), length->scale(fraction).get());
}
PassRefPtrWillBeRawPtr<AnimatableValue> AnimatableLength::addWith(const AnimatableValue* value) const
{
if (isUnitlessZero())
return takeConstRef(value);
const AnimatableLength* length = toAnimatableLength(value);
if (length->isUnitlessZero())
return takeConstRef(this);
NumberUnitType type = commonUnitType(length);
if (type != UnitTypeCalc)
return AnimatableLength::create(m_number + length->m_number, type);
return AnimatableLength::create(this, length);
}
bool AnimatableLength::equalTo(const AnimatableValue* value) const
{
const AnimatableLength* length = toAnimatableLength(value);
if (m_unitType != length->m_unitType)
return false;
if (isCalc())
return m_calcExpression == length->m_calcExpression || m_calcExpression->equals(*length->m_calcExpression);
return m_number == length->m_number;
}
PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> AnimatableLength::toCSSCalcExpressionNode() const
{
if (isCalc())
return m_calcExpression;
return CSSCalcValue::createExpressionNode(toCSSPrimitiveValue(AllValues), m_number == trunc(m_number));
}
static bool isCompatibleWithRange(const CSSPrimitiveValue* primitiveValue, NumberRange range)
{
ASSERT(primitiveValue);
if (range == AllValues)
return true;
if (primitiveValue->isCalculated())
return primitiveValue->cssCalcValue()->permittedValueRange() == ValueRangeNonNegative;
return primitiveValue->getDoubleValue() >= 0;
}
PassRefPtrWillBeRawPtr<CSSPrimitiveValue> AnimatableLength::toCSSPrimitiveValue(NumberRange range) const
{
if (!m_cachedCSSPrimitiveValue || !isCompatibleWithRange(m_cachedCSSPrimitiveValue.get(), range)) {
if (isCalc())
m_cachedCSSPrimitiveValue = CSSPrimitiveValue::create(CSSCalcValue::create(m_calcExpression, range == AllValues ? ValueRangeAll : ValueRangeNonNegative));
else
m_cachedCSSPrimitiveValue = CSSPrimitiveValue::create(clampedNumber(range), static_cast<CSSPrimitiveValue::UnitTypes>(numberTypeToPrimitiveUnit(m_unitType)));
}
return m_cachedCSSPrimitiveValue;
}
PassRefPtrWillBeRawPtr<AnimatableLength> AnimatableLength::scale(double factor) const
{
if (isCalc()) {
return AnimatableLength::create(CSSCalcValue::createExpressionNode(
m_calcExpression,
CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(factor, CSSPrimitiveValue::CSS_NUMBER)),
CalcMultiply));
}
return AnimatableLength::create(m_number * factor, m_unitType);
}
bool AnimatableLength::primitiveUnitToNumberType(unsigned short primitiveUnit, NumberUnitType& numberType)
{
switch (primitiveUnit) {
case CSSPrimitiveValue::CSS_PX:
case CSSPrimitiveValue::CSS_CM:
case CSSPrimitiveValue::CSS_MM:
case CSSPrimitiveValue::CSS_IN:
case CSSPrimitiveValue::CSS_PT:
case CSSPrimitiveValue::CSS_PC:
numberType = UnitTypePixels;
return true;
case CSSPrimitiveValue::CSS_EMS:
numberType = UnitTypeFontSize;
return true;
case CSSPrimitiveValue::CSS_EXS:
numberType = UnitTypeFontXSize;
return true;
case CSSPrimitiveValue::CSS_REMS:
numberType = UnitTypeRootFontSize;
return true;
case CSSPrimitiveValue::CSS_PERCENTAGE:
numberType = UnitTypePercentage;
return true;
case CSSPrimitiveValue::CSS_VW:
numberType = UnitTypeViewportWidth;
return true;
case CSSPrimitiveValue::CSS_VH:
numberType = UnitTypeViewportHeight;
return true;
case CSSPrimitiveValue::CSS_VMIN:
numberType = UnitTypeViewportMin;
return true;
case CSSPrimitiveValue::CSS_VMAX:
numberType = UnitTypeViewportMax;
return true;
default:
return false;
}
}
unsigned short AnimatableLength::numberTypeToPrimitiveUnit(NumberUnitType numberType)
{
switch (numberType) {
case UnitTypePixels:
return CSSPrimitiveValue::CSS_PX;
case UnitTypeFontSize:
return CSSPrimitiveValue::CSS_EMS;
case UnitTypeFontXSize:
return CSSPrimitiveValue::CSS_EXS;
case UnitTypeRootFontSize:
return CSSPrimitiveValue::CSS_REMS;
case UnitTypePercentage:
return CSSPrimitiveValue::CSS_PERCENTAGE;
case UnitTypeViewportWidth:
return CSSPrimitiveValue::CSS_VW;
case UnitTypeViewportHeight:
return CSSPrimitiveValue::CSS_VH;
case UnitTypeViewportMin:
return CSSPrimitiveValue::CSS_VMIN;
case UnitTypeViewportMax:
return CSSPrimitiveValue::CSS_VMAX;
case UnitTypeCalc:
return CSSPrimitiveValue::CSS_UNKNOWN;
}
ASSERT_NOT_REACHED();
return CSSPrimitiveValue::CSS_UNKNOWN;
}
void AnimatableLength::trace(Visitor* visitor)
{
visitor->trace(m_calcExpression);
visitor->trace(m_cachedCSSPrimitiveValue);
}
}