1/*
2 * Copyright (C) 2012 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 BasicShapes_h
31#define BasicShapes_h
32
33#include "Length.h"
34#include "WindRule.h"
35#include <wtf/RefCounted.h>
36#include <wtf/RefPtr.h>
37#include <wtf/Vector.h>
38
39namespace WebCore {
40
41class FloatRect;
42class Path;
43
44class BasicShape : public RefCounted<BasicShape> {
45public:
46    virtual ~BasicShape() { }
47
48    enum Type {
49        BasicShapeRectangleType = 1,
50        BasicShapeCircleType = 2,
51        BasicShapeEllipseType = 3,
52        BasicShapePolygonType = 4,
53        BasicShapeInsetRectangleType = 5
54    };
55
56    bool canBlend(const BasicShape*) const;
57
58    virtual void path(Path&, const FloatRect&) = 0;
59    virtual WindRule windRule() const { return RULE_NONZERO; }
60    virtual PassRefPtr<BasicShape> blend(const BasicShape*, double) const = 0;
61
62    virtual Type type() const = 0;
63protected:
64    BasicShape() { }
65};
66
67class BasicShapeRectangle : public BasicShape {
68public:
69    static PassRefPtr<BasicShapeRectangle> create() { return adoptRef(new BasicShapeRectangle); }
70
71    Length x() const { return m_x; }
72    Length y() const { return m_y; }
73    Length width() const { return m_width; }
74    Length height() const { return m_height; }
75    Length cornerRadiusX() const { return m_cornerRadiusX; }
76    Length cornerRadiusY() const { return m_cornerRadiusY; }
77
78    void setX(Length x) { m_x = x; }
79    void setY(Length y) { m_y = y; }
80    void setWidth(Length width) { m_width = width; }
81    void setHeight(Length height) { m_height = height; }
82    void setCornerRadiusX(Length radiusX)
83    {
84        ASSERT(!radiusX.isUndefined());
85        m_cornerRadiusX = radiusX;
86    }
87    void setCornerRadiusY(Length radiusY)
88    {
89        ASSERT(!radiusY.isUndefined());
90        m_cornerRadiusY = radiusY;
91    }
92
93    virtual void path(Path&, const FloatRect&) OVERRIDE;
94    virtual PassRefPtr<BasicShape> blend(const BasicShape*, double) const OVERRIDE;
95
96    virtual Type type() const { return BasicShapeRectangleType; }
97private:
98    BasicShapeRectangle() { }
99
100    Length m_y;
101    Length m_x;
102    Length m_width;
103    Length m_height;
104    Length m_cornerRadiusX;
105    Length m_cornerRadiusY;
106};
107
108class BasicShapeCircle : public BasicShape {
109public:
110    static PassRefPtr<BasicShapeCircle> create() { return adoptRef(new BasicShapeCircle); }
111
112    Length centerX() const { return m_centerX; }
113    Length centerY() const { return m_centerY; }
114    Length radius() const { return m_radius; }
115
116    void setCenterX(Length centerX) { m_centerX = centerX; }
117    void setCenterY(Length centerY) { m_centerY = centerY; }
118    void setRadius(Length radius) { m_radius = radius; }
119
120    virtual void path(Path&, const FloatRect&) OVERRIDE;
121    virtual PassRefPtr<BasicShape> blend(const BasicShape*, double) const OVERRIDE;
122
123    virtual Type type() const { return BasicShapeCircleType; }
124private:
125    BasicShapeCircle() { }
126
127    Length m_centerX;
128    Length m_centerY;
129    Length m_radius;
130};
131
132class BasicShapeEllipse : public BasicShape {
133public:
134    static PassRefPtr<BasicShapeEllipse> create() { return adoptRef(new BasicShapeEllipse); }
135
136    Length centerX() const { return m_centerX; }
137    Length centerY() const { return m_centerY; }
138    Length radiusX() const { return m_radiusX; }
139    Length radiusY() const { return m_radiusY; }
140
141    void setCenterX(Length centerX) { m_centerX = centerX; }
142    void setCenterY(Length centerY) { m_centerY = centerY; }
143    void setRadiusX(Length radiusX) { m_radiusX = radiusX; }
144    void setRadiusY(Length radiusY) { m_radiusY = radiusY; }
145
146    virtual void path(Path&, const FloatRect&) OVERRIDE;
147    virtual PassRefPtr<BasicShape> blend(const BasicShape*, double) const OVERRIDE;
148
149    virtual Type type() const { return BasicShapeEllipseType; }
150private:
151    BasicShapeEllipse() { }
152
153    Length m_centerX;
154    Length m_centerY;
155    Length m_radiusX;
156    Length m_radiusY;
157};
158
159class BasicShapePolygon : public BasicShape {
160public:
161    static PassRefPtr<BasicShapePolygon> create() { return adoptRef(new BasicShapePolygon); }
162
163    const Vector<Length>& values() const { return m_values; }
164    Length getXAt(unsigned i) const { return m_values.at(2 * i); }
165    Length getYAt(unsigned i) const { return m_values.at(2 * i + 1); }
166
167    void setWindRule(WindRule windRule) { m_windRule = windRule; }
168    void appendPoint(Length x, Length y) { m_values.append(x); m_values.append(y); }
169
170    virtual void path(Path&, const FloatRect&) OVERRIDE;
171    virtual PassRefPtr<BasicShape> blend(const BasicShape*, double) const OVERRIDE;
172
173    virtual WindRule windRule() const { return m_windRule; }
174
175    virtual Type type() const { return BasicShapePolygonType; }
176private:
177    BasicShapePolygon()
178        : m_windRule(RULE_NONZERO)
179    { }
180
181    WindRule m_windRule;
182    Vector<Length> m_values;
183};
184
185class BasicShapeInsetRectangle : public BasicShape {
186public:
187    static PassRefPtr<BasicShapeInsetRectangle> create() { return adoptRef(new BasicShapeInsetRectangle); }
188
189    Length top() const { return m_top; }
190    Length right() const { return m_right; }
191    Length bottom() const { return m_bottom; }
192    Length left() const { return m_left; }
193    Length cornerRadiusX() const { return m_cornerRadiusX; }
194    Length cornerRadiusY() const { return m_cornerRadiusY; }
195
196    void setTop(Length top) { m_top = top; }
197    void setRight(Length right) { m_right = right; }
198    void setBottom(Length bottom) { m_bottom = bottom; }
199    void setLeft(Length left) { m_left = left; }
200    void setCornerRadiusX(Length radiusX)
201    {
202        ASSERT(!radiusX.isUndefined());
203        m_cornerRadiusX = radiusX;
204    }
205    void setCornerRadiusY(Length radiusY)
206    {
207        ASSERT(!radiusY.isUndefined());
208        m_cornerRadiusY = radiusY;
209    }
210
211    virtual void path(Path&, const FloatRect&) OVERRIDE;
212    virtual PassRefPtr<BasicShape> blend(const BasicShape*, double) const OVERRIDE;
213
214    virtual Type type() const { return BasicShapeInsetRectangleType; }
215private:
216    BasicShapeInsetRectangle() { }
217
218    Length m_right;
219    Length m_top;
220    Length m_bottom;
221    Length m_left;
222    Length m_cornerRadiusX;
223    Length m_cornerRadiusY;
224};
225}
226#endif
227