Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef AnimatableNumber_h
     32 #define AnimatableNumber_h
     33 
     34 #include "core/animation/AnimatableValue.h"
     35 #include "core/css/CSSCalculationValue.h"
     36 #include "core/css/CSSPrimitiveValue.h"
     37 
     38 namespace WebCore {
     39 
     40 // Handles animation of CSSPrimitiveValues that can be represented by doubles including CSSCalcValue.
     41 // See primitiveUnitToNumberType() for the list of supported units (with the exception of calc).
     42 // If created from a CSSPrimitiveValue this class will cache it to be returned in toCSSValue().
     43 class AnimatableNumber : public AnimatableValue {
     44 public:
     45     enum NumberUnitType {
     46         UnitTypeNumber,
     47         UnitTypeLength,
     48         UnitTypeFontSize,
     49         UnitTypeFontXSize,
     50         UnitTypeRootFontSize,
     51         UnitTypePercentage,
     52         UnitTypeViewportWidth,
     53         UnitTypeViewportHeight,
     54         UnitTypeViewportMin,
     55         UnitTypeViewportMax,
     56         UnitTypeTime,
     57         UnitTypeAngle,
     58         UnitTypeFrequency,
     59         UnitTypeResolution,
     60         UnitTypeInvalid,
     61     };
     62 
     63     virtual ~AnimatableNumber() { }
     64     static bool canCreateFrom(const CSSValue*);
     65     static PassRefPtr<AnimatableNumber> create(CSSValue*);
     66     static PassRefPtr<AnimatableNumber> create(double number, NumberUnitType unitType, CSSPrimitiveValue* cssPrimitiveValue = 0)
     67     {
     68         return adoptRef(new AnimatableNumber(number, unitType, cssPrimitiveValue));
     69     }
     70     PassRefPtr<CSSValue> toCSSValue() const;
     71     double toDouble() const;
     72     Length toLength(const RenderStyle* currStyle, const RenderStyle* rootStyle, double zoom) const;
     73 
     74 protected:
     75     virtual PassRefPtr<AnimatableValue> interpolateTo(const AnimatableValue*, double fraction) const OVERRIDE;
     76     virtual PassRefPtr<AnimatableValue> addWith(const AnimatableValue*) const OVERRIDE;
     77 
     78 private:
     79     AnimatableNumber(double number, NumberUnitType unitType, CSSPrimitiveValue* cssPrimitiveValue)
     80         : AnimatableValue(TypeNumber)
     81         , m_number(number)
     82         , m_unitType(unitType)
     83         , m_isCalc(false)
     84         , m_cachedCSSPrimitiveValue(cssPrimitiveValue)
     85     {
     86         ASSERT(m_unitType != UnitTypeInvalid);
     87     }
     88     AnimatableNumber(PassRefPtr<CSSCalcExpressionNode> calcExpression, CSSPrimitiveValue* cssPrimitiveValue)
     89         : AnimatableValue(TypeNumber)
     90         , m_isCalc(true)
     91         , m_calcExpression(calcExpression)
     92         , m_cachedCSSPrimitiveValue(cssPrimitiveValue)
     93     {
     94         ASSERT(m_calcExpression);
     95     }
     96 
     97     static PassRefPtr<AnimatableNumber> create(PassRefPtr<CSSCalcExpressionNode> calcExpression, CSSPrimitiveValue* cssPrimitiveValue = 0)
     98     {
     99         return adoptRef(new AnimatableNumber(calcExpression, cssPrimitiveValue));
    100     }
    101     static PassRefPtr<AnimatableNumber> create(const AnimatableNumber* leftAddend, const AnimatableNumber* rightAddend);
    102 
    103     PassRefPtr<CSSPrimitiveValue> toCSSPrimitiveValue() const;
    104     PassRefPtr<CSSCalcExpressionNode> toCSSCalcExpressionNode() const;
    105 
    106     PassRefPtr<AnimatableNumber> scale(double) const;
    107     static NumberUnitType primitiveUnitToNumberType(unsigned short primitiveUnit);
    108     static unsigned short numberTypeToPrimitiveUnit(NumberUnitType numberType);
    109 
    110     double m_number;
    111     NumberUnitType m_unitType;
    112 
    113     bool m_isCalc;
    114     RefPtr<CSSCalcExpressionNode> m_calcExpression;
    115 
    116     mutable RefPtr<CSSPrimitiveValue> m_cachedCSSPrimitiveValue;
    117 };
    118 
    119 inline const AnimatableNumber* toAnimatableNumber(const AnimatableValue* value)
    120 {
    121     ASSERT_WITH_SECURITY_IMPLICATION(value && value->isNumber());
    122     return static_cast<const AnimatableNumber*>(value);
    123 }
    124 
    125 } // namespace WebCore
    126 
    127 #endif // AnimatableNumber_h
    128