diff options
Diffstat (limited to 'chromium/third_party/WebKit/Source/core/animation/AnimatableLength.cpp')
-rw-r--r-- | chromium/third_party/WebKit/Source/core/animation/AnimatableLength.cpp | 206 |
1 files changed, 24 insertions, 182 deletions
diff --git a/chromium/third_party/WebKit/Source/core/animation/AnimatableLength.cpp b/chromium/third_party/WebKit/Source/core/animation/AnimatableLength.cpp index 38a46590dbb..c167c20a752 100644 --- a/chromium/third_party/WebKit/Source/core/animation/AnimatableLength.cpp +++ b/chromium/third_party/WebKit/Source/core/animation/AnimatableLength.cpp @@ -31,211 +31,53 @@ #include "config.h" #include "core/animation/AnimatableLength.h" -#include "core/css/CSSPrimitiveValueMappings.h" #include "platform/CalculationValue.h" #include "platform/animation/AnimationUtilities.h" namespace WebCore { -PassRefPtr<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 0; -} +namespace { -bool AnimatableLength::canCreateFrom(const CSSValue* value) +double clampNumber(double value, ValueRange range) { - ASSERT(value); - if (value->isPrimitiveValue()) { - const CSSPrimitiveValue* primitiveValue = WebCore::toCSSPrimitiveValue(value); - if (primitiveValue->cssCalcValue()) - return true; - - NumberUnitType unitType; - // Only returns true if the type is a primitive length unit. - return primitiveUnitToNumberType(primitiveValue->primitiveType(), unitType); - } - return value->isCalcValue(); + if (range == ValueRangeNonNegative) + return std::max(value, 0.0); + ASSERT(range == ValueRangeAll); + return value; } -PassRefPtr<CSSValue> AnimatableLength::toCSSValue(NumberRange range) const -{ - return toCSSPrimitiveValue(range); -} +} // namespace -Length AnimatableLength::toLength(const CSSToLengthConversionData& conversionData, NumberRange range) const +AnimatableLength::AnimatableLength(const Length& length, float zoom) { - // Avoid creating a CSSValue in the common cases - 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); + ASSERT(zoom); + PixelsAndPercent pixelsAndPercent = length.pixelsAndPercent(); + m_pixels = pixelsAndPercent.pixels / zoom; + m_percent = pixelsAndPercent.percent; + m_hasPixels = length.type() != Percent; + m_hasPercent = !length.isFixed(); } -PassRefPtr<AnimatableValue> AnimatableLength::interpolateTo(const AnimatableValue* value, double fraction) const +Length AnimatableLength::length(float zoom, ValueRange range) const { - const AnimatableLength* length = toAnimatableLength(value); - NumberUnitType type = commonUnitType(length); - if (type != UnitTypeCalc) - return AnimatableLength::create(blend(m_number, length->m_number, fraction), type); - - // FIXME(crbug.com/168840): Support for viewport units in calc needs to be added before we can blend them with other units. - if (isViewportUnit() || length->isViewportUnit()) - return defaultInterpolateTo(this, value, fraction); - - return AnimatableLength::create(scale(1 - fraction).get(), length->scale(fraction).get()); + if (!m_hasPercent) + return Length(clampNumber(m_pixels, range) * zoom, Fixed); + if (!m_hasPixels) + return Length(clampNumber(m_percent, range), Percent); + return Length(CalculationValue::create(PixelsAndPercent(m_pixels * zoom, m_percent), range)); } -PassRefPtr<AnimatableValue> AnimatableLength::addWith(const AnimatableValue* value) const +PassRefPtrWillBeRawPtr<AnimatableValue> AnimatableLength::interpolateTo(const AnimatableValue* value, double fraction) const { - // Optimization for adding with 0. - 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); + return create(blend(m_pixels, length->m_pixels, fraction), blend(m_percent, length->m_percent, fraction), + m_hasPixels || length->m_hasPixels, m_hasPercent || length->m_hasPercent); } 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; -} - -PassRefPtr<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; -} - -PassRefPtr<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; -} - -PassRefPtr<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; + return m_pixels == length->m_pixels && m_percent == length->m_percent && m_hasPixels == length->m_hasPixels && m_hasPercent == length->m_hasPercent; } } // namespace WebCore |