1/*
2 * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef Rect_h
22#define Rect_h
23
24#include "CSSPrimitiveValue.h"
25#include <wtf/RefPtr.h>
26#include <wtf/text/StringBuilder.h>
27
28namespace WebCore {
29
30class RectBase {
31public:
32    CSSPrimitiveValue* top() const { return m_top.get(); }
33    CSSPrimitiveValue* right() const { return m_right.get(); }
34    CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
35    CSSPrimitiveValue* left() const { return m_left.get(); }
36
37    void setTop(PassRefPtr<CSSPrimitiveValue> top) { m_top = top; }
38    void setRight(PassRefPtr<CSSPrimitiveValue> right) { m_right = right; }
39    void setBottom(PassRefPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
40    void setLeft(PassRefPtr<CSSPrimitiveValue> left) { m_left = left; }
41
42    bool equals(const RectBase& other) const
43    {
44        return compareCSSValuePtr(m_top, other.m_top)
45            && compareCSSValuePtr(m_right, other.m_right)
46            && compareCSSValuePtr(m_left, other.m_left)
47            && compareCSSValuePtr(m_bottom, other.m_bottom);
48    }
49
50#if ENABLE(CSS_VARIABLES)
51    bool hasVariableReference() const
52    {
53        return m_top->hasVariableReference()
54            || m_right->hasVariableReference()
55            || m_bottom->hasVariableReference()
56            || m_left->hasVariableReference();
57    }
58#endif
59
60protected:
61    RectBase() { }
62    RectBase(const RectBase& cloneFrom)
63        : m_top(cloneFrom.m_top ? cloneFrom.m_top->cloneForCSSOM() : 0)
64        , m_right(cloneFrom.m_right ? cloneFrom.m_right->cloneForCSSOM() : 0)
65        , m_bottom(cloneFrom.m_bottom ? cloneFrom.m_bottom->cloneForCSSOM() : 0)
66        , m_left(cloneFrom.m_left ? cloneFrom.m_left->cloneForCSSOM() : 0)
67    {
68    }
69
70    ~RectBase() { }
71
72private:
73    RefPtr<CSSPrimitiveValue> m_top;
74    RefPtr<CSSPrimitiveValue> m_right;
75    RefPtr<CSSPrimitiveValue> m_bottom;
76    RefPtr<CSSPrimitiveValue> m_left;
77};
78
79class Rect : public RectBase, public RefCounted<Rect> {
80public:
81    static PassRefPtr<Rect> create() { return adoptRef(new Rect); }
82
83    PassRefPtr<Rect> cloneForCSSOM() const { return adoptRef(new Rect(*this)); }
84
85    String cssText() const
86    {
87        return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
88    }
89
90#if ENABLE(CSS_VARIABLES)
91    String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
92    {
93        return generateCSSString(top()->customSerializeResolvingVariables(variables),
94            right()->customSerializeResolvingVariables(variables),
95            bottom()->customSerializeResolvingVariables(variables),
96            left()->customSerializeResolvingVariables(variables));
97    }
98#endif
99
100private:
101    Rect() { }
102    Rect(const Rect& cloneFrom) : RectBase(cloneFrom), RefCounted<Rect>() { }
103    static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
104    {
105        return "rect(" + top + ' ' + right + ' ' + bottom + ' ' + left + ')';
106    }
107};
108
109class Quad : public RectBase, public RefCounted<Quad> {
110public:
111    static PassRefPtr<Quad> create() { return adoptRef(new Quad); }
112
113    PassRefPtr<Quad> cloneForCSSOM() const { return adoptRef(new Quad(*this)); }
114
115    String cssText() const
116    {
117        return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
118    }
119
120#if ENABLE(CSS_VARIABLES)
121    String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
122    {
123        return generateCSSString(top()->customSerializeResolvingVariables(variables),
124            right()->customSerializeResolvingVariables(variables),
125            bottom()->customSerializeResolvingVariables(variables),
126            left()->customSerializeResolvingVariables(variables));
127    }
128#endif
129
130private:
131    Quad() { }
132    Quad(const Quad& cloneFrom) : RectBase(cloneFrom), RefCounted<Quad>() { }
133    static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
134    {
135        StringBuilder result;
136        // reserve space for the four strings, plus three space separator characters.
137        result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
138        result.append(top);
139        if (right != top || bottom != top || left != top) {
140            result.append(' ');
141            result.append(right);
142            if (bottom != top || right != left) {
143                result.append(' ');
144                result.append(bottom);
145                if (left != right) {
146                    result.append(' ');
147                    result.append(left);
148                }
149            }
150        }
151        return result.toString();
152    }
153};
154
155} // namespace WebCore
156
157#endif // Rect_h
158