1/*
2 * Copyright (C) 2011 Adobe Systems Incorporated. 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 *    copyright notice, this list of conditions and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 *    copyright notice, this list of conditions and the following
13 *    disclaimer in the documentation and/or other materials
14 *    provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef CSSBasicShapes_h
31#define CSSBasicShapes_h
32
33#include "CSSPrimitiveValue.h"
34#include "WindRule.h"
35#include <wtf/RefPtr.h>
36#include <wtf/Vector.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41class CSSBasicShape : public RefCounted<CSSBasicShape> {
42public:
43    enum Type {
44        CSSBasicShapeRectangleType = 1,
45        CSSBasicShapeCircleType = 2,
46        CSSBasicShapeEllipseType = 3,
47        CSSBasicShapePolygonType = 4,
48        CSSBasicShapeInsetRectangleType = 5
49    };
50
51    virtual Type type() const = 0;
52    virtual String cssText() const = 0;
53    virtual bool equals(const CSSBasicShape&) const = 0;
54
55#if ENABLE(CSS_VARIABLES)
56    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const = 0;
57    virtual bool hasVariableReference() const = 0;
58#endif
59
60public:
61    virtual ~CSSBasicShape() { }
62
63protected:
64    CSSBasicShape() { }
65};
66
67class CSSBasicShapeRectangle : public CSSBasicShape {
68public:
69    static PassRefPtr<CSSBasicShapeRectangle> create() { return adoptRef(new CSSBasicShapeRectangle); }
70
71    CSSPrimitiveValue* x() const { return m_x.get(); }
72    CSSPrimitiveValue* y() const { return m_y.get(); }
73    CSSPrimitiveValue* width() const { return m_width.get(); }
74    CSSPrimitiveValue* height() const { return m_height.get(); }
75    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
76    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
77
78    void setX(PassRefPtr<CSSPrimitiveValue> x) { m_x = x; }
79    void setY(PassRefPtr<CSSPrimitiveValue> y) { m_y = y; }
80    void setWidth(PassRefPtr<CSSPrimitiveValue> width) { m_width = width; }
81    void setHeight(PassRefPtr<CSSPrimitiveValue> height) { m_height = height; }
82    void setRadiusX(PassRefPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
83    void setRadiusY(PassRefPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
84
85    virtual Type type() const { return CSSBasicShapeRectangleType; }
86    virtual String cssText() const;
87    virtual bool equals(const CSSBasicShape&) const;
88
89#if ENABLE(CSS_VARIABLES)
90    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
91    virtual bool hasVariableReference() const;
92#endif
93
94private:
95    CSSBasicShapeRectangle() { }
96
97    RefPtr<CSSPrimitiveValue> m_y;
98    RefPtr<CSSPrimitiveValue> m_x;
99    RefPtr<CSSPrimitiveValue> m_width;
100    RefPtr<CSSPrimitiveValue> m_height;
101    RefPtr<CSSPrimitiveValue> m_radiusX;
102    RefPtr<CSSPrimitiveValue> m_radiusY;
103};
104
105class CSSBasicShapeInsetRectangle : public CSSBasicShape {
106public:
107    static PassRefPtr<CSSBasicShapeInsetRectangle> create() { return adoptRef(new CSSBasicShapeInsetRectangle); }
108
109    CSSPrimitiveValue* top() const { return m_top.get(); }
110    CSSPrimitiveValue* right() const { return m_right.get(); }
111    CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
112    CSSPrimitiveValue* left() const { return m_left.get(); }
113    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
114    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
115
116    void setTop(PassRefPtr<CSSPrimitiveValue> top) { m_top = top; }
117    void setRight(PassRefPtr<CSSPrimitiveValue> right) { m_right = right; }
118    void setBottom(PassRefPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
119    void setLeft(PassRefPtr<CSSPrimitiveValue> left) { m_left = left; }
120    void setRadiusX(PassRefPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
121    void setRadiusY(PassRefPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
122
123    virtual Type type() const { return CSSBasicShapeInsetRectangleType; }
124    virtual String cssText() const;
125    virtual bool equals(const CSSBasicShape&) const;
126
127#if ENABLE(CSS_VARIABLES)
128    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
129    virtual bool hasVariableReference() const;
130#endif
131
132private:
133    CSSBasicShapeInsetRectangle() { }
134
135    RefPtr<CSSPrimitiveValue> m_right;
136    RefPtr<CSSPrimitiveValue> m_top;
137    RefPtr<CSSPrimitiveValue> m_bottom;
138    RefPtr<CSSPrimitiveValue> m_left;
139    RefPtr<CSSPrimitiveValue> m_radiusX;
140    RefPtr<CSSPrimitiveValue> m_radiusY;
141};
142
143class CSSBasicShapeCircle : public CSSBasicShape {
144public:
145    static PassRefPtr<CSSBasicShapeCircle> create() { return adoptRef(new CSSBasicShapeCircle); }
146
147    CSSPrimitiveValue* centerX() const { return m_centerX.get(); }
148    CSSPrimitiveValue* centerY() const { return m_centerY.get(); }
149    CSSPrimitiveValue* radius() const { return m_radius.get(); }
150
151    void setCenterX(PassRefPtr<CSSPrimitiveValue> centerX) { m_centerX = centerX; }
152    void setCenterY(PassRefPtr<CSSPrimitiveValue> centerY) { m_centerY = centerY; }
153    void setRadius(PassRefPtr<CSSPrimitiveValue> radius) { m_radius = radius; }
154
155    virtual Type type() const { return CSSBasicShapeCircleType; }
156    virtual String cssText() const;
157    virtual bool equals(const CSSBasicShape&) const;
158
159#if ENABLE(CSS_VARIABLES)
160    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
161    virtual bool hasVariableReference() const;
162#endif
163
164private:
165    CSSBasicShapeCircle() { }
166
167    RefPtr<CSSPrimitiveValue> m_centerY;
168    RefPtr<CSSPrimitiveValue> m_centerX;
169    RefPtr<CSSPrimitiveValue> m_radius;
170};
171
172class CSSBasicShapeEllipse : public CSSBasicShape {
173public:
174    static PassRefPtr<CSSBasicShapeEllipse> create() { return adoptRef(new CSSBasicShapeEllipse); }
175
176    CSSPrimitiveValue* centerX() const { return m_centerX.get(); }
177    CSSPrimitiveValue* centerY() const { return m_centerY.get(); }
178    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
179    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
180
181    void setCenterX(PassRefPtr<CSSPrimitiveValue> centerX) { m_centerX = centerX; }
182    void setCenterY(PassRefPtr<CSSPrimitiveValue> centerY) { m_centerY = centerY; }
183    void setRadiusX(PassRefPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
184    void setRadiusY(PassRefPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
185
186    virtual Type type() const { return CSSBasicShapeEllipseType; }
187    virtual String cssText() const;
188    virtual bool equals(const CSSBasicShape&) const;
189
190#if ENABLE(CSS_VARIABLES)
191    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
192    virtual bool hasVariableReference() const;
193#endif
194
195private:
196    CSSBasicShapeEllipse() { }
197
198    RefPtr<CSSPrimitiveValue> m_centerX;
199    RefPtr<CSSPrimitiveValue> m_centerY;
200    RefPtr<CSSPrimitiveValue> m_radiusX;
201    RefPtr<CSSPrimitiveValue> m_radiusY;
202};
203
204class CSSBasicShapePolygon : public CSSBasicShape {
205public:
206    static PassRefPtr<CSSBasicShapePolygon> create() { return adoptRef(new CSSBasicShapePolygon); }
207
208    void appendPoint(PassRefPtr<CSSPrimitiveValue> x, PassRefPtr<CSSPrimitiveValue> y)
209    {
210        m_values.append(x);
211        m_values.append(y);
212    }
213
214    PassRefPtr<CSSPrimitiveValue> getXAt(unsigned i) const { return m_values.at(i * 2); }
215    PassRefPtr<CSSPrimitiveValue> getYAt(unsigned i) const { return m_values.at(i * 2 + 1); }
216    const Vector<RefPtr<CSSPrimitiveValue> >& values() const { return m_values; }
217
218    void setWindRule(WindRule w) { m_windRule = w; }
219    WindRule windRule() const { return m_windRule; }
220
221    virtual Type type() const { return CSSBasicShapePolygonType; }
222    virtual String cssText() const;
223    virtual bool equals(const CSSBasicShape&) const;
224#if ENABLE(CSS_VARIABLES)
225    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
226    virtual bool hasVariableReference() const;
227#endif
228
229private:
230    CSSBasicShapePolygon()
231        : m_windRule(RULE_NONZERO)
232    {
233    }
234
235    Vector<RefPtr<CSSPrimitiveValue> > m_values;
236    WindRule m_windRule;
237};
238
239} // namespace WebCore
240
241#endif // CSSBasicShapes_h
242