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 "bindings/core/v8/ScriptWrappable.h"
25#include "core/css/CSSPrimitiveValue.h"
26#include "wtf/RefPtr.h"
27#include "wtf/text/StringBuilder.h"
28
29namespace blink {
30
31class RectBase : public RefCountedWillBeGarbageCollected<RectBase>, public ScriptWrappableBase {
32    DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(RectBase);
33public:
34    CSSPrimitiveValue* top() const { return m_top.get(); }
35    CSSPrimitiveValue* right() const { return m_right.get(); }
36    CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
37    CSSPrimitiveValue* left() const { return m_left.get(); }
38
39    void setTop(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> top) { m_top = top; }
40    void setRight(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> right) { m_right = right; }
41    void setBottom(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
42    void setLeft(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> left) { m_left = left; }
43
44    bool equals(const RectBase& other) const
45    {
46        return compareCSSValuePtr(m_top, other.m_top)
47            && compareCSSValuePtr(m_right, other.m_right)
48            && compareCSSValuePtr(m_left, other.m_left)
49            && compareCSSValuePtr(m_bottom, other.m_bottom);
50    }
51
52    void trace(Visitor*);
53
54protected:
55    RectBase() { }
56    RectBase(const RectBase& cloneFrom)
57        : m_top(cloneFrom.m_top ? cloneFrom.m_top->cloneForCSSOM() : nullptr)
58        , m_right(cloneFrom.m_right ? cloneFrom.m_right->cloneForCSSOM() : nullptr)
59        , m_bottom(cloneFrom.m_bottom ? cloneFrom.m_bottom->cloneForCSSOM() : nullptr)
60        , m_left(cloneFrom.m_left ? cloneFrom.m_left->cloneForCSSOM() : nullptr)
61    {
62    }
63
64private:
65    RefPtrWillBeMember<CSSPrimitiveValue> m_top;
66    RefPtrWillBeMember<CSSPrimitiveValue> m_right;
67    RefPtrWillBeMember<CSSPrimitiveValue> m_bottom;
68    RefPtrWillBeMember<CSSPrimitiveValue> m_left;
69};
70
71class Rect : public RectBase {
72public:
73    static PassRefPtrWillBeRawPtr<Rect> create() { return adoptRefWillBeNoop(new Rect); }
74
75    PassRefPtrWillBeRawPtr<Rect> cloneForCSSOM() const { return adoptRefWillBeNoop(new Rect(*this)); }
76
77    String cssText() const
78    {
79        return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
80    }
81
82private:
83    Rect() { }
84    Rect(const Rect& cloneFrom) : RectBase(cloneFrom) { }
85    static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
86    {
87        return "rect(" + top + ' ' + right + ' ' + bottom + ' ' + left + ')';
88    }
89
90    // NOTE: If adding fields to this class please make the RectBase trace
91    // method virtual and add a trace method in this subclass tracing the new
92    // fields.
93};
94
95class Quad : public RectBase {
96public:
97    static PassRefPtrWillBeRawPtr<Quad> create() { return adoptRefWillBeNoop(new Quad); }
98
99    PassRefPtrWillBeRawPtr<Quad> cloneForCSSOM() const { return adoptRefWillBeNoop(new Quad(*this)); }
100
101    String cssText() const
102    {
103        return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
104    }
105
106private:
107    Quad() { }
108    Quad(const Quad& cloneFrom) : RectBase(cloneFrom) { }
109    static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
110    {
111        StringBuilder result;
112        // reserve space for the four strings, plus three space separator characters.
113        result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
114        result.append(top);
115        if (right != top || bottom != top || left != top) {
116            result.append(' ');
117            result.append(right);
118            if (bottom != top || right != left) {
119                result.append(' ');
120                result.append(bottom);
121                if (left != right) {
122                    result.append(' ');
123                    result.append(left);
124                }
125            }
126        }
127        return result.toString();
128    }
129
130    // NOTE: If adding fields to this class please make the RectBase trace
131    // method virtual and add a trace method in this subclass tracing the new
132    // fields.
133};
134
135} // namespace blink
136
137#endif // Rect_h
138