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