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
50protected:
51    RectBase() { }
52    RectBase(const RectBase& cloneFrom)
53        : m_top(cloneFrom.m_top ? cloneFrom.m_top->cloneForCSSOM() : 0)
54        , m_right(cloneFrom.m_right ? cloneFrom.m_right->cloneForCSSOM() : 0)
55        , m_bottom(cloneFrom.m_bottom ? cloneFrom.m_bottom->cloneForCSSOM() : 0)
56        , m_left(cloneFrom.m_left ? cloneFrom.m_left->cloneForCSSOM() : 0)
57    {
58    }
59
60    ~RectBase() { }
61
62private:
63    RefPtr<CSSPrimitiveValue> m_top;
64    RefPtr<CSSPrimitiveValue> m_right;
65    RefPtr<CSSPrimitiveValue> m_bottom;
66    RefPtr<CSSPrimitiveValue> m_left;
67};
68
69class Rect : public RectBase, public RefCounted<Rect> {
70public:
71    static PassRefPtr<Rect> create() { return adoptRef(new Rect); }
72
73    PassRefPtr<Rect> cloneForCSSOM() const { return adoptRef(new Rect(*this)); }
74
75    String cssText() const
76    {
77        return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
78    }
79
80private:
81    Rect() { }
82    Rect(const Rect& cloneFrom) : RectBase(cloneFrom), RefCounted<Rect>() { }
83    static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
84    {
85        return "rect(" + top + ", " + right + ", " + bottom + ", " + left + ')';
86    }
87};
88
89class Quad : public RectBase, public RefCounted<Quad> {
90public:
91    static PassRefPtr<Quad> create() { return adoptRef(new Quad); }
92
93    PassRefPtr<Quad> cloneForCSSOM() const { return adoptRef(new Quad(*this)); }
94
95    String cssText() const
96    {
97        return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
98    }
99
100private:
101    Quad() { }
102    Quad(const Quad& cloneFrom) : RectBase(cloneFrom), RefCounted<Quad>() { }
103    static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
104    {
105        StringBuilder result;
106        // reserve space for the four strings, plus three space separator characters.
107        result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
108        result.append(top);
109        if (right != top || bottom != top || left != top) {
110            result.append(' ');
111            result.append(right);
112            if (bottom != top || right != left) {
113                result.append(' ');
114                result.append(bottom);
115                if (left != right) {
116                    result.append(' ');
117                    result.append(left);
118                }
119            }
120        }
121        return result.toString();
122    }
123};
124
125} // namespace WebCore
126
127#endif // Rect_h
128