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 "CSSParserValues.h"
35#include "CSSValue.h"
36#include "CalculationValue.h"
37#include <wtf/PassOwnPtr.h>
38#include <wtf/RefCounted.h>
39#include <wtf/RefPtr.h>
40
41namespace WebCore {
42
43class CSSParserValueList;
44class CSSValueList;
45class RenderStyle;
46class CalculationValue;
47class CalcExpressionNode;
48
49enum CalculationCategory {
50    CalcNumber = 0,
51    CalcLength,
52    CalcPercent,
53    CalcPercentNumber,
54    CalcPercentLength,
55#if ENABLE(CSS_VARIABLES)
56    CalcVariable,
57#endif
58    CalcOther
59};
60
61class CSSCalcExpressionNode : public RefCounted<CSSCalcExpressionNode> {
62public:
63    enum Type {
64        CssCalcPrimitiveValue = 1,
65        CssCalcBinaryOperation
66    };
67
68    virtual ~CSSCalcExpressionNode() = 0;
69    virtual bool isZero() const = 0;
70    virtual PassOwnPtr<CalcExpressionNode> toCalcValue(const RenderStyle*, const RenderStyle* rootStyle, double zoom = 1.0) const = 0;
71    virtual double doubleValue() const = 0;
72    virtual double computeLengthPx(const RenderStyle* currentStyle, const RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false) const = 0;
73    virtual String customCssText() const = 0;
74#if ENABLE(CSS_VARIABLES)
75    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const = 0;
76    virtual bool hasVariableReference() const = 0;
77#endif
78    virtual bool equals(const CSSCalcExpressionNode& other) const { return m_category == other.m_category && m_isInteger == other.m_isInteger; }
79    virtual Type type() const = 0;
80
81    CalculationCategory category() const { return m_category; }
82    bool isInteger() const { return m_isInteger; }
83
84protected:
85    CSSCalcExpressionNode(CalculationCategory category, bool isInteger)
86        : m_category(category)
87        , m_isInteger(isInteger)
88    {
89    }
90
91    CalculationCategory m_category;
92    bool m_isInteger;
93};
94
95class CSSCalcValue : public CSSValue {
96public:
97    static PassRefPtr<CSSCalcValue> create(CSSParserString name, CSSParserValueList*, CalculationPermittedValueRange);
98    static PassRefPtr<CSSCalcValue> create(CalculationValue*);
99
100    PassRefPtr<CalculationValue> toCalcValue(const RenderStyle* style, const RenderStyle* rootStyle, double zoom = 1.0) const
101    {
102        return CalculationValue::create(m_expression->toCalcValue(style, rootStyle, zoom), m_nonNegative ? CalculationRangeNonNegative : CalculationRangeAll);
103    }
104    CalculationCategory category() const { return m_expression->category(); }
105    bool isInt() const { return m_expression->isInteger(); }
106    double doubleValue() const;
107    bool isNegative() const { return m_expression->doubleValue() < 0; }
108    double computeLengthPx(const RenderStyle* currentStyle, const RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false) const;
109
110    String customCssText() const;
111    bool equals(const CSSCalcValue&) const;
112#if ENABLE(CSS_VARIABLES)
113    String customSerializeResolvingVariables(const HashMap<AtomicString, String>&) const;
114    bool hasVariableReference() const;
115#endif
116
117private:
118    CSSCalcValue(PassRefPtr<CSSCalcExpressionNode> expression, CalculationPermittedValueRange range)
119        : CSSValue(CalculationClass)
120        , m_expression(expression)
121        , m_nonNegative(range == CalculationRangeNonNegative)
122    {
123    }
124
125    double clampToPermittedRange(double) const;
126
127    const RefPtr<CSSCalcExpressionNode> m_expression;
128    const bool m_nonNegative;
129};
130
131} // namespace WebCore
132
133#endif // CSSCalculationValue_h
134