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 "platform/Length.h" 34 #include "platform/graphics/WindRule.h" 35 #include "wtf/RefCounted.h" 36 #include "wtf/RefPtr.h" 37 #include "wtf/Vector.h" 38 39 namespace WebCore { 40 41 class FloatRect; 42 class Path; 43 44 class BasicShape : public RefCounted<BasicShape> { 45 public: ~BasicShape()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; windRule()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; 63 protected: BasicShape()64 BasicShape() { } 65 }; 66 67 class BasicShapeRectangle : public BasicShape { 68 public: create()69 static PassRefPtr<BasicShapeRectangle> create() { return adoptRef(new BasicShapeRectangle); } 70 x()71 Length x() const { return m_x; } y()72 Length y() const { return m_y; } width()73 Length width() const { return m_width; } height()74 Length height() const { return m_height; } cornerRadiusX()75 Length cornerRadiusX() const { return m_cornerRadiusX; } cornerRadiusY()76 Length cornerRadiusY() const { return m_cornerRadiusY; } 77 setX(Length x)78 void setX(Length x) { m_x = x; } setY(Length y)79 void setY(Length y) { m_y = y; } setWidth(Length width)80 void setWidth(Length width) { m_width = width; } setHeight(Length height)81 void setHeight(Length height) { m_height = height; } setCornerRadiusX(Length radiusX)82 void setCornerRadiusX(Length radiusX) 83 { 84 ASSERT(!radiusX.isUndefined()); 85 m_cornerRadiusX = radiusX; 86 } setCornerRadiusY(Length radiusY)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 type()96 virtual Type type() const { return BasicShapeRectangleType; } 97 private: BasicShapeRectangle()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 108 class BasicShapeCircle : public BasicShape { 109 public: create()110 static PassRefPtr<BasicShapeCircle> create() { return adoptRef(new BasicShapeCircle); } 111 centerX()112 Length centerX() const { return m_centerX; } centerY()113 Length centerY() const { return m_centerY; } radius()114 Length radius() const { return m_radius; } 115 setCenterX(Length centerX)116 void setCenterX(Length centerX) { m_centerX = centerX; } setCenterY(Length centerY)117 void setCenterY(Length centerY) { m_centerY = centerY; } setRadius(Length radius)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 type()123 virtual Type type() const { return BasicShapeCircleType; } 124 private: BasicShapeCircle()125 BasicShapeCircle() { } 126 127 Length m_centerX; 128 Length m_centerY; 129 Length m_radius; 130 }; 131 132 class BasicShapeEllipse : public BasicShape { 133 public: create()134 static PassRefPtr<BasicShapeEllipse> create() { return adoptRef(new BasicShapeEllipse); } 135 centerX()136 Length centerX() const { return m_centerX; } centerY()137 Length centerY() const { return m_centerY; } radiusX()138 Length radiusX() const { return m_radiusX; } radiusY()139 Length radiusY() const { return m_radiusY; } 140 setCenterX(Length centerX)141 void setCenterX(Length centerX) { m_centerX = centerX; } setCenterY(Length centerY)142 void setCenterY(Length centerY) { m_centerY = centerY; } setRadiusX(Length radiusX)143 void setRadiusX(Length radiusX) { m_radiusX = radiusX; } setRadiusY(Length radiusY)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 type()149 virtual Type type() const { return BasicShapeEllipseType; } 150 private: BasicShapeEllipse()151 BasicShapeEllipse() { } 152 153 Length m_centerX; 154 Length m_centerY; 155 Length m_radiusX; 156 Length m_radiusY; 157 }; 158 159 class BasicShapePolygon : public BasicShape { 160 public: create()161 static PassRefPtr<BasicShapePolygon> create() { return adoptRef(new BasicShapePolygon); } 162 values()163 const Vector<Length>& values() const { return m_values; } getXAt(unsigned i)164 Length getXAt(unsigned i) const { return m_values.at(2 * i); } getYAt(unsigned i)165 Length getYAt(unsigned i) const { return m_values.at(2 * i + 1); } 166 setWindRule(WindRule windRule)167 void setWindRule(WindRule windRule) { m_windRule = windRule; } appendPoint(Length x,Length y)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 windRule()173 virtual WindRule windRule() const { return m_windRule; } 174 type()175 virtual Type type() const { return BasicShapePolygonType; } 176 private: BasicShapePolygon()177 BasicShapePolygon() 178 : m_windRule(RULE_NONZERO) 179 { } 180 181 WindRule m_windRule; 182 Vector<Length> m_values; 183 }; 184 185 class BasicShapeInsetRectangle : public BasicShape { 186 public: create()187 static PassRefPtr<BasicShapeInsetRectangle> create() { return adoptRef(new BasicShapeInsetRectangle); } 188 top()189 Length top() const { return m_top; } right()190 Length right() const { return m_right; } bottom()191 Length bottom() const { return m_bottom; } left()192 Length left() const { return m_left; } cornerRadiusX()193 Length cornerRadiusX() const { return m_cornerRadiusX; } cornerRadiusY()194 Length cornerRadiusY() const { return m_cornerRadiusY; } 195 setTop(Length top)196 void setTop(Length top) { m_top = top; } setRight(Length right)197 void setRight(Length right) { m_right = right; } setBottom(Length bottom)198 void setBottom(Length bottom) { m_bottom = bottom; } setLeft(Length left)199 void setLeft(Length left) { m_left = left; } setCornerRadiusX(Length radiusX)200 void setCornerRadiusX(Length radiusX) 201 { 202 ASSERT(!radiusX.isUndefined()); 203 m_cornerRadiusX = radiusX; 204 } setCornerRadiusY(Length radiusY)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 type()214 virtual Type type() const { return BasicShapeInsetRectangleType; } 215 private: BasicShapeInsetRectangle()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