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