• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/css/CSSPrimitiveValue.h"
25 #include "wtf/RefPtr.h"
26 #include "wtf/text/StringBuilder.h"
27 
28 namespace WebCore {
29 
30 class RectBase {
31 public:
top()32     CSSPrimitiveValue* top() const { return m_top.get(); }
right()33     CSSPrimitiveValue* right() const { return m_right.get(); }
bottom()34     CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
left()35     CSSPrimitiveValue* left() const { return m_left.get(); }
36 
setTop(PassRefPtr<CSSPrimitiveValue> top)37     void setTop(PassRefPtr<CSSPrimitiveValue> top) { m_top = top; }
setRight(PassRefPtr<CSSPrimitiveValue> right)38     void setRight(PassRefPtr<CSSPrimitiveValue> right) { m_right = right; }
setBottom(PassRefPtr<CSSPrimitiveValue> bottom)39     void setBottom(PassRefPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
setLeft(PassRefPtr<CSSPrimitiveValue> left)40     void setLeft(PassRefPtr<CSSPrimitiveValue> left) { m_left = left; }
41 
equals(const RectBase & other)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 
hasVariableReference()50     bool hasVariableReference() const
51     {
52         return m_top->hasVariableReference()
53             || m_right->hasVariableReference()
54             || m_bottom->hasVariableReference()
55             || m_left->hasVariableReference();
56     }
57 
58 protected:
RectBase()59     RectBase() { }
RectBase(const RectBase & cloneFrom)60     RectBase(const RectBase& cloneFrom)
61         : m_top(cloneFrom.m_top ? cloneFrom.m_top->cloneForCSSOM() : 0)
62         , m_right(cloneFrom.m_right ? cloneFrom.m_right->cloneForCSSOM() : 0)
63         , m_bottom(cloneFrom.m_bottom ? cloneFrom.m_bottom->cloneForCSSOM() : 0)
64         , m_left(cloneFrom.m_left ? cloneFrom.m_left->cloneForCSSOM() : 0)
65     {
66     }
67 
~RectBase()68     ~RectBase() { }
69 
70 private:
71     RefPtr<CSSPrimitiveValue> m_top;
72     RefPtr<CSSPrimitiveValue> m_right;
73     RefPtr<CSSPrimitiveValue> m_bottom;
74     RefPtr<CSSPrimitiveValue> m_left;
75 };
76 
77 class Rect : public RectBase, public RefCounted<Rect> {
78 public:
create()79     static PassRefPtr<Rect> create() { return adoptRef(new Rect); }
80 
cloneForCSSOM()81     PassRefPtr<Rect> cloneForCSSOM() const { return adoptRef(new Rect(*this)); }
82 
cssText()83     String cssText() const
84     {
85         return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
86     }
87 
serializeResolvingVariables(const HashMap<AtomicString,String> & variables)88     String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
89     {
90         return generateCSSString(top()->customSerializeResolvingVariables(variables),
91             right()->customSerializeResolvingVariables(variables),
92             bottom()->customSerializeResolvingVariables(variables),
93             left()->customSerializeResolvingVariables(variables));
94     }
95 
96 private:
Rect()97     Rect() { }
Rect(const Rect & cloneFrom)98     Rect(const Rect& cloneFrom) : RectBase(cloneFrom), RefCounted<Rect>() { }
generateCSSString(const String & top,const String & right,const String & bottom,const String & left)99     static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
100     {
101         return "rect(" + top + ' ' + right + ' ' + bottom + ' ' + left + ')';
102     }
103 };
104 
105 class Quad : public RectBase, public RefCounted<Quad> {
106 public:
create()107     static PassRefPtr<Quad> create() { return adoptRef(new Quad); }
108 
cloneForCSSOM()109     PassRefPtr<Quad> cloneForCSSOM() const { return adoptRef(new Quad(*this)); }
110 
cssText()111     String cssText() const
112     {
113         return generateCSSString(top()->cssText(), right()->cssText(), bottom()->cssText(), left()->cssText());
114     }
115 
serializeResolvingVariables(const HashMap<AtomicString,String> & variables)116     String serializeResolvingVariables(const HashMap<AtomicString, String>& variables) const
117     {
118         return generateCSSString(top()->customSerializeResolvingVariables(variables),
119             right()->customSerializeResolvingVariables(variables),
120             bottom()->customSerializeResolvingVariables(variables),
121             left()->customSerializeResolvingVariables(variables));
122     }
123 
124 private:
Quad()125     Quad() { }
Quad(const Quad & cloneFrom)126     Quad(const Quad& cloneFrom) : RectBase(cloneFrom), RefCounted<Quad>() { }
generateCSSString(const String & top,const String & right,const String & bottom,const String & left)127     static String generateCSSString(const String& top, const String& right, const String& bottom, const String& left)
128     {
129         StringBuilder result;
130         // reserve space for the four strings, plus three space separator characters.
131         result.reserveCapacity(top.length() + right.length() + bottom.length() + left.length() + 3);
132         result.append(top);
133         if (right != top || bottom != top || left != top) {
134             result.append(' ');
135             result.append(right);
136             if (bottom != top || right != left) {
137                 result.append(' ');
138                 result.append(bottom);
139                 if (left != right) {
140                     result.append(' ');
141                     result.append(left);
142                 }
143             }
144         }
145         return result.toString();
146     }
147 };
148 
149 } // namespace WebCore
150 
151 #endif // Rect_h
152