Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2011, 2012 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 CSSCalculationValue_h
     32 #define CSSCalculationValue_h
     33 
     34 #include "core/css/CSSPrimitiveValue.h"
     35 #include "core/css/CSSValue.h"
     36 #include "core/css/parser/CSSParserValues.h"
     37 #include "platform/CalculationValue.h"
     38 #include "wtf/PassOwnPtr.h"
     39 #include "wtf/RefCounted.h"
     40 #include "wtf/RefPtr.h"
     41 
     42 namespace blink {
     43 
     44 class CSSParserValueList;
     45 class CalculationValue;
     46 
     47 enum CalcOperator {
     48     CalcAdd = '+',
     49     CalcSubtract = '-',
     50     CalcMultiply = '*',
     51     CalcDivide = '/'
     52 };
     53 
     54 // The order of this enum should not change since its elements are used as indices
     55 // in the addSubtractResult matrix.
     56 enum CalculationCategory {
     57     CalcNumber = 0,
     58     CalcLength,
     59     CalcPercent,
     60     CalcPercentNumber,
     61     CalcPercentLength,
     62     CalcAngle,
     63     CalcTime,
     64     CalcFrequency,
     65     CalcOther
     66 };
     67 
     68 class CSSCalcExpressionNode : public RefCountedWillBeGarbageCollected<CSSCalcExpressionNode> {
     69     DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(CSSCalcExpressionNode);
     70 public:
     71     enum Type {
     72         CssCalcPrimitiveValue = 1,
     73         CssCalcBinaryOperation
     74     };
     75 
     76     virtual bool isZero() const = 0;
     77     virtual double doubleValue() const = 0;
     78     virtual double computeLengthPx(const CSSToLengthConversionData&) const = 0;
     79     virtual void accumulateLengthArray(CSSLengthArray&, double multiplier) const = 0;
     80     virtual void accumulatePixelsAndPercent(const CSSToLengthConversionData&, PixelsAndPercent&, float multiplier = 1) const = 0;
     81     virtual String customCSSText() const = 0;
     82     virtual bool equals(const CSSCalcExpressionNode& other) const { return m_category == other.m_category && m_isInteger == other.m_isInteger; }
     83     virtual Type type() const = 0;
     84 
     85     CalculationCategory category() const { return m_category; }
     86     virtual CSSPrimitiveValue::UnitType primitiveType() const = 0;
     87     bool isInteger() const { return m_isInteger; }
     88 
     89     virtual void trace(Visitor*) { }
     90 
     91 protected:
     92     CSSCalcExpressionNode(CalculationCategory category, bool isInteger)
     93         : m_category(category)
     94         , m_isInteger(isInteger)
     95     {
     96         ASSERT(category != CalcOther);
     97     }
     98 
     99     CalculationCategory m_category;
    100     bool m_isInteger;
    101 };
    102 
    103 class CSSCalcValue : public CSSValue {
    104 public:
    105     static PassRefPtrWillBeRawPtr<CSSCalcValue> create(CSSParserString name, CSSParserValueList*, ValueRange);
    106     static PassRefPtrWillBeRawPtr<CSSCalcValue> create(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode>, ValueRange = ValueRangeAll);
    107 
    108     static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtrWillBeRawPtr<CSSPrimitiveValue>, bool isInteger = false);
    109     static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode>, PassRefPtrWillBeRawPtr<CSSCalcExpressionNode>, CalcOperator);
    110     static PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> createExpressionNode(double pixels, double percent);
    111 
    112     PassRefPtr<CalculationValue> toCalcValue(const CSSToLengthConversionData& conversionData) const
    113     {
    114         PixelsAndPercent value(0, 0);
    115         m_expression->accumulatePixelsAndPercent(conversionData, value);
    116         return CalculationValue::create(value, m_nonNegative ? ValueRangeNonNegative : ValueRangeAll);
    117     }
    118     CalculationCategory category() const { return m_expression->category(); }
    119     bool isInt() const { return m_expression->isInteger(); }
    120     double doubleValue() const;
    121     bool isNegative() const { return m_expression->doubleValue() < 0; }
    122     ValueRange permittedValueRange() { return m_nonNegative ? ValueRangeNonNegative : ValueRangeAll; }
    123     double computeLengthPx(const CSSToLengthConversionData&) const;
    124     void accumulateLengthArray(CSSLengthArray& lengthArray, double multiplier) const { m_expression->accumulateLengthArray(lengthArray, multiplier); }
    125     CSSCalcExpressionNode* expressionNode() const { return m_expression.get(); }
    126 
    127     String customCSSText() const;
    128     bool equals(const CSSCalcValue&) const;
    129 
    130     void traceAfterDispatch(Visitor*);
    131 
    132 private:
    133     CSSCalcValue(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> expression, ValueRange range)
    134         : CSSValue(CalculationClass)
    135         , m_expression(expression)
    136         , m_nonNegative(range == ValueRangeNonNegative)
    137     {
    138     }
    139 
    140     double clampToPermittedRange(double) const;
    141 
    142     const RefPtrWillBeMember<CSSCalcExpressionNode> m_expression;
    143     const bool m_nonNegative;
    144 };
    145 
    146 DEFINE_CSS_VALUE_TYPE_CASTS(CSSCalcValue, isCalcValue());
    147 
    148 } // namespace blink
    149 
    150 
    151 #endif // CSSCalculationValue_h
    152