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 "platform/graphics/GraphicsTypes.h"
35#include "wtf/RefPtr.h"
36#include "wtf/Vector.h"
37#include "wtf/text/WTFString.h"
38
39namespace blink {
40
41class CSSBasicShape : public RefCountedWillBeGarbageCollected<CSSBasicShape> {
42    DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(CSSBasicShape);
43public:
44    enum Type {
45        CSSBasicShapeEllipseType,
46        CSSBasicShapePolygonType,
47        CSSBasicShapeCircleType,
48        CSSBasicShapeInsetType
49    };
50
51    virtual Type type() const = 0;
52    virtual String cssText() const = 0;
53    virtual bool equals(const CSSBasicShape&) const = 0;
54
55    CSSPrimitiveValue* referenceBox() const { return m_referenceBox.get(); }
56    void setReferenceBox(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> referenceBox) { m_referenceBox = referenceBox; }
57
58    virtual void trace(Visitor* visitor) { visitor->trace(m_referenceBox); }
59
60protected:
61    CSSBasicShape() { }
62    RefPtrWillBeMember<CSSPrimitiveValue> m_referenceBox;
63};
64
65class CSSBasicShapeCircle FINAL : public CSSBasicShape {
66public:
67    static PassRefPtrWillBeRawPtr<CSSBasicShapeCircle> create() { return adoptRefWillBeNoop(new CSSBasicShapeCircle); }
68
69    virtual Type type() const OVERRIDE { return CSSBasicShapeCircleType; }
70    virtual String cssText() const OVERRIDE;
71    virtual bool equals(const CSSBasicShape&) const OVERRIDE;
72
73    CSSPrimitiveValue* centerX() const { return m_centerX.get(); }
74    CSSPrimitiveValue* centerY() const { return m_centerY.get(); }
75    CSSPrimitiveValue* radius() const { return m_radius.get(); }
76
77    void setCenterX(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> centerX) { m_centerX = centerX; }
78    void setCenterY(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> centerY) { m_centerY = centerY; }
79    void setRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radius) { m_radius = radius; }
80
81    virtual void trace(Visitor*);
82
83private:
84    CSSBasicShapeCircle() { }
85
86    RefPtrWillBeMember<CSSPrimitiveValue> m_centerX;
87    RefPtrWillBeMember<CSSPrimitiveValue> m_centerY;
88    RefPtrWillBeMember<CSSPrimitiveValue> m_radius;
89};
90
91class CSSBasicShapeEllipse FINAL : public CSSBasicShape {
92public:
93    static PassRefPtrWillBeRawPtr<CSSBasicShapeEllipse> create() { return adoptRefWillBeNoop(new CSSBasicShapeEllipse); }
94
95    virtual Type type() const OVERRIDE { return CSSBasicShapeEllipseType; }
96    virtual String cssText() const OVERRIDE;
97    virtual bool equals(const CSSBasicShape&) const OVERRIDE;
98
99    CSSPrimitiveValue* centerX() const { return m_centerX.get(); }
100    CSSPrimitiveValue* centerY() const { return m_centerY.get(); }
101    CSSPrimitiveValue* radiusX() const { return m_radiusX.get(); }
102    CSSPrimitiveValue* radiusY() const { return m_radiusY.get(); }
103
104    void setCenterX(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> centerX) { m_centerX = centerX; }
105    void setCenterY(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> centerY) { m_centerY = centerY; }
106    void setRadiusX(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radiusX) { m_radiusX = radiusX; }
107    void setRadiusY(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radiusY) { m_radiusY = radiusY; }
108
109    virtual void trace(Visitor*);
110
111private:
112    CSSBasicShapeEllipse() { }
113
114    RefPtrWillBeMember<CSSPrimitiveValue> m_centerX;
115    RefPtrWillBeMember<CSSPrimitiveValue> m_centerY;
116    RefPtrWillBeMember<CSSPrimitiveValue> m_radiusX;
117    RefPtrWillBeMember<CSSPrimitiveValue> m_radiusY;
118};
119
120class CSSBasicShapePolygon FINAL : public CSSBasicShape {
121public:
122    static PassRefPtrWillBeRawPtr<CSSBasicShapePolygon> create() { return adoptRefWillBeNoop(new CSSBasicShapePolygon); }
123
124    void appendPoint(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> x, PassRefPtrWillBeRawPtr<CSSPrimitiveValue> y)
125    {
126        m_values.append(x);
127        m_values.append(y);
128    }
129
130    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> getXAt(unsigned i) const { return m_values.at(i * 2); }
131    PassRefPtrWillBeRawPtr<CSSPrimitiveValue> getYAt(unsigned i) const { return m_values.at(i * 2 + 1); }
132    const WillBeHeapVector<RefPtrWillBeMember<CSSPrimitiveValue> >& values() const { return m_values; }
133
134    void setWindRule(WindRule w) { m_windRule = w; }
135    WindRule windRule() const { return m_windRule; }
136
137    virtual Type type() const OVERRIDE { return CSSBasicShapePolygonType; }
138    virtual String cssText() const OVERRIDE;
139    virtual bool equals(const CSSBasicShape&) const OVERRIDE;
140
141    virtual void trace(Visitor*);
142
143private:
144    CSSBasicShapePolygon()
145        : m_windRule(RULE_NONZERO)
146    {
147    }
148
149    WillBeHeapVector<RefPtrWillBeMember<CSSPrimitiveValue> > m_values;
150    WindRule m_windRule;
151};
152
153class CSSBasicShapeInset : public CSSBasicShape {
154public:
155    static PassRefPtrWillBeRawPtr<CSSBasicShapeInset> create() { return adoptRefWillBeNoop(new CSSBasicShapeInset); }
156
157    CSSPrimitiveValue* top() const { return m_top.get(); }
158    CSSPrimitiveValue* right() const { return m_right.get(); }
159    CSSPrimitiveValue* bottom() const { return m_bottom.get(); }
160    CSSPrimitiveValue* left() const { return m_left.get(); }
161
162    CSSPrimitiveValue* topLeftRadius() const { return m_topLeftRadius.get(); }
163    CSSPrimitiveValue* topRightRadius() const { return m_topRightRadius.get(); }
164    CSSPrimitiveValue* bottomRightRadius() const { return m_bottomRightRadius.get(); }
165    CSSPrimitiveValue* bottomLeftRadius() const { return m_bottomLeftRadius.get(); }
166
167    void setTop(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> top) { m_top = top; }
168    void setRight(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> right) { m_right = right; }
169    void setBottom(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> bottom) { m_bottom = bottom; }
170    void setLeft(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> left) { m_left = left; }
171
172    void updateShapeSize4Values(CSSPrimitiveValue* top, CSSPrimitiveValue* right, CSSPrimitiveValue* bottom, CSSPrimitiveValue* left)
173    {
174        setTop(top);
175        setRight(right);
176        setBottom(bottom);
177        setLeft(left);
178    }
179
180    void updateShapeSize1Value(CSSPrimitiveValue* value1)
181    {
182        updateShapeSize4Values(value1, value1, value1, value1);
183    }
184
185    void updateShapeSize2Values(CSSPrimitiveValue* value1,  CSSPrimitiveValue* value2)
186    {
187        updateShapeSize4Values(value1, value2, value1, value2);
188    }
189
190    void updateShapeSize3Values(CSSPrimitiveValue* value1, CSSPrimitiveValue* value2,  CSSPrimitiveValue* value3)
191    {
192        updateShapeSize4Values(value1, value2, value3, value2);
193    }
194
195
196    void setTopLeftRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radius) { m_topLeftRadius = radius; }
197    void setTopRightRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radius) { m_topRightRadius = radius; }
198    void setBottomRightRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radius) { m_bottomRightRadius = radius; }
199    void setBottomLeftRadius(PassRefPtrWillBeRawPtr<CSSPrimitiveValue> radius) { m_bottomLeftRadius = radius; }
200
201    virtual Type type() const OVERRIDE { return CSSBasicShapeInsetType; }
202    virtual String cssText() const OVERRIDE;
203    virtual bool equals(const CSSBasicShape&) const OVERRIDE;
204
205    virtual void trace(Visitor*);
206
207private:
208    CSSBasicShapeInset() { }
209
210    RefPtrWillBeMember<CSSPrimitiveValue> m_top;
211    RefPtrWillBeMember<CSSPrimitiveValue> m_right;
212    RefPtrWillBeMember<CSSPrimitiveValue> m_bottom;
213    RefPtrWillBeMember<CSSPrimitiveValue> m_left;
214
215    RefPtrWillBeMember<CSSPrimitiveValue> m_topLeftRadius;
216    RefPtrWillBeMember<CSSPrimitiveValue> m_topRightRadius;
217    RefPtrWillBeMember<CSSPrimitiveValue> m_bottomRightRadius;
218    RefPtrWillBeMember<CSSPrimitiveValue> m_bottomLeftRadius;
219};
220
221} // namespace blink
222
223#endif // CSSBasicShapes_h
224