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 "core/css/CSSPrimitiveValue.h"
34#include "core/platform/graphics/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    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const = 0;
56    virtual bool hasVariableReference() const = 0;
57
58public:
59    virtual ~CSSBasicShape() { }
60
61protected:
62    CSSBasicShape() { }
63};
64
65class CSSBasicShapeRectangle : public CSSBasicShape {
66public:
67    static PassRefPtr<CSSBasicShapeRectangle> create() { return adoptRef(new CSSBasicShapeRectangle); }
68
69    CSSPrimitiveValue* x() const { return m_x.get(); }
70    CSSPrimitiveValue* y() const { return m_y.get(); }
71    CSSPrimitiveValue* width() const { return m_width.get(); }
72    CSSPrimitiveValue* height() const { return m_height.get(); }
73    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
74    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
75
76    void setX(PassRefPtr<CSSPrimitiveValue> x) { m_x = x; }
77    void setY(PassRefPtr<CSSPrimitiveValue> y) { m_y = y; }
78    void setWidth(PassRefPtr<CSSPrimitiveValue> width) { m_width = width; }
79    void setHeight(PassRefPtr<CSSPrimitiveValue> height) { m_height = height; }
80    void setRadiusX(PassRefPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
81    void setRadiusY(PassRefPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
82
83    virtual Type type() const { return CSSBasicShapeRectangleType; }
84    virtual String cssText() const;
85    virtual bool equals(const CSSBasicShape&) const;
86
87    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
88    virtual bool hasVariableReference() const;
89
90private:
91    CSSBasicShapeRectangle() { }
92
93    RefPtr<CSSPrimitiveValue> m_y;
94    RefPtr<CSSPrimitiveValue> m_x;
95    RefPtr<CSSPrimitiveValue> m_width;
96    RefPtr<CSSPrimitiveValue> m_height;
97    RefPtr<CSSPrimitiveValue> m_radiusX;
98    RefPtr<CSSPrimitiveValue> m_radiusY;
99};
100
101class CSSBasicShapeInsetRectangle : public CSSBasicShape {
102public:
103    static PassRefPtr<CSSBasicShapeInsetRectangle> create() { return adoptRef(new CSSBasicShapeInsetRectangle); }
104
105    CSSPrimitiveValue* top() const { return m_top.get(); }
106    CSSPrimitiveValue* right() const { return m_right.get(); }
107    CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
108    CSSPrimitiveValue* left() const { return m_left.get(); }
109    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
110    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
111
112    void setTop(PassRefPtr<CSSPrimitiveValue> top) { m_top = top; }
113    void setRight(PassRefPtr<CSSPrimitiveValue> right) { m_right = right; }
114    void setBottom(PassRefPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
115    void setLeft(PassRefPtr<CSSPrimitiveValue> left) { m_left = left; }
116    void setRadiusX(PassRefPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
117    void setRadiusY(PassRefPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
118
119    virtual Type type() const { return CSSBasicShapeInsetRectangleType; }
120    virtual String cssText() const;
121    virtual bool equals(const CSSBasicShape&) const;
122
123    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
124    virtual bool hasVariableReference() const;
125
126private:
127    CSSBasicShapeInsetRectangle() { }
128
129    RefPtr<CSSPrimitiveValue> m_right;
130    RefPtr<CSSPrimitiveValue> m_top;
131    RefPtr<CSSPrimitiveValue> m_bottom;
132    RefPtr<CSSPrimitiveValue> m_left;
133    RefPtr<CSSPrimitiveValue> m_radiusX;
134    RefPtr<CSSPrimitiveValue> m_radiusY;
135};
136
137class CSSBasicShapeCircle : public CSSBasicShape {
138public:
139    static PassRefPtr<CSSBasicShapeCircle> create() { return adoptRef(new CSSBasicShapeCircle); }
140
141    CSSPrimitiveValue* centerX() const { return m_centerX.get(); }
142    CSSPrimitiveValue* centerY() const { return m_centerY.get(); }
143    CSSPrimitiveValue* radius() const { return m_radius.get(); }
144
145    void setCenterX(PassRefPtr<CSSPrimitiveValue> centerX) { m_centerX = centerX; }
146    void setCenterY(PassRefPtr<CSSPrimitiveValue> centerY) { m_centerY = centerY; }
147    void setRadius(PassRefPtr<CSSPrimitiveValue> radius) { m_radius = radius; }
148
149    virtual Type type() const { return CSSBasicShapeCircleType; }
150    virtual String cssText() const;
151    virtual bool equals(const CSSBasicShape&) const;
152
153    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
154    virtual bool hasVariableReference() const;
155
156private:
157    CSSBasicShapeCircle() { }
158
159    RefPtr<CSSPrimitiveValue> m_centerY;
160    RefPtr<CSSPrimitiveValue> m_centerX;
161    RefPtr<CSSPrimitiveValue> m_radius;
162};
163
164class CSSBasicShapeEllipse : public CSSBasicShape {
165public:
166    static PassRefPtr<CSSBasicShapeEllipse> create() { return adoptRef(new CSSBasicShapeEllipse); }
167
168    CSSPrimitiveValue* centerX() const { return m_centerX.get(); }
169    CSSPrimitiveValue* centerY() const { return m_centerY.get(); }
170    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
171    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
172
173    void setCenterX(PassRefPtr<CSSPrimitiveValue> centerX) { m_centerX = centerX; }
174    void setCenterY(PassRefPtr<CSSPrimitiveValue> centerY) { m_centerY = centerY; }
175    void setRadiusX(PassRefPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
176    void setRadiusY(PassRefPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
177
178    virtual Type type() const { return CSSBasicShapeEllipseType; }
179    virtual String cssText() const;
180    virtual bool equals(const CSSBasicShape&) const;
181
182    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
183    virtual bool hasVariableReference() const;
184
185private:
186    CSSBasicShapeEllipse() { }
187
188    RefPtr<CSSPrimitiveValue> m_centerX;
189    RefPtr<CSSPrimitiveValue> m_centerY;
190    RefPtr<CSSPrimitiveValue> m_radiusX;
191    RefPtr<CSSPrimitiveValue> m_radiusY;
192};
193
194class CSSBasicShapePolygon : public CSSBasicShape {
195public:
196    static PassRefPtr<CSSBasicShapePolygon> create() { return adoptRef(new CSSBasicShapePolygon); }
197
198    void appendPoint(PassRefPtr<CSSPrimitiveValue> x, PassRefPtr<CSSPrimitiveValue> y)
199    {
200        m_values.append(x);
201        m_values.append(y);
202    }
203
204    PassRefPtr<CSSPrimitiveValue> getXAt(unsigned i) const { return m_values.at(i * 2); }
205    PassRefPtr<CSSPrimitiveValue> getYAt(unsigned i) const { return m_values.at(i * 2 + 1); }
206    const Vector<RefPtr<CSSPrimitiveValue> >& values() const { return m_values; }
207
208    void setWindRule(WindRule w) { m_windRule = w; }
209    WindRule windRule() const { return m_windRule; }
210
211    virtual Type type() const { return CSSBasicShapePolygonType; }
212    virtual String cssText() const;
213    virtual bool equals(const CSSBasicShape&) const;
214    virtual String serializeResolvingVariables(const HashMap<AtomicString, String>&) const;
215    virtual bool hasVariableReference() const;
216
217private:
218    CSSBasicShapePolygon()
219        : m_windRule(RULE_NONZERO)
220    {
221    }
222
223    Vector<RefPtr<CSSPrimitiveValue> > m_values;
224    WindRule m_windRule;
225};
226
227} // namespace WebCore
228
229#endif // CSSBasicShapes_h
230