2 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef FloatRoundedRect_h
31 #define FloatRoundedRect_h
33 #include "FloatRect.h"
34 #include "FloatSize.h"
35 #include "RoundedRect.h"
39 class FloatRoundedRect {
44 Radii(const FloatSize& topLeft, const FloatSize& topRight, const FloatSize& bottomLeft, const FloatSize& bottomRight)
46 , m_topRight(topRight)
47 , m_bottomLeft(bottomLeft)
48 , m_bottomRight(bottomRight)
52 Radii(const RoundedRect::Radii& intRadii)
53 : m_topLeft(intRadii.topLeft())
54 , m_topRight(intRadii.topRight())
55 , m_bottomLeft(intRadii.bottomLeft())
56 , m_bottomRight(intRadii.bottomRight())
60 explicit Radii(float uniformRadius)
61 : m_topLeft(uniformRadius, uniformRadius)
62 , m_topRight(uniformRadius, uniformRadius)
63 , m_bottomLeft(uniformRadius, uniformRadius)
64 , m_bottomRight(uniformRadius, uniformRadius)
68 void setTopLeft(const FloatSize& size) { m_topLeft = size; }
69 void setTopRight(const FloatSize& size) { m_topRight = size; }
70 void setBottomLeft(const FloatSize& size) { m_bottomLeft = size; }
71 void setBottomRight(const FloatSize& size) { m_bottomRight = size; }
72 const FloatSize& topLeft() const { return m_topLeft; }
73 const FloatSize& topRight() const { return m_topRight; }
74 const FloatSize& bottomLeft() const { return m_bottomLeft; }
75 const FloatSize& bottomRight() const { return m_bottomRight; }
78 bool isUniformCornerRadius() const; // Including no radius.
80 void scale(float factor);
81 void scale(float horizontalFactor, float verticalFactor);
82 void expand(float topWidth, float bottomWidth, float leftWidth, float rightWidth);
83 void expand(float size) { expand(size, size, size, size); }
84 void shrink(float topWidth, float bottomWidth, float leftWidth, float rightWidth) { expand(-topWidth, -bottomWidth, -leftWidth, -rightWidth); }
85 void shrink(float size) { shrink(size, size, size, size); }
90 FloatSize m_bottomLeft;
91 FloatSize m_bottomRight;
94 WEBCORE_EXPORT explicit FloatRoundedRect(const FloatRect& = FloatRect(), const Radii& = Radii());
95 explicit FloatRoundedRect(const RoundedRect&);
96 FloatRoundedRect(float x, float y, float width, float height);
97 FloatRoundedRect(const FloatRect&, const FloatSize& topLeft, const FloatSize& topRight, const FloatSize& bottomLeft, const FloatSize& bottomRight);
99 const FloatRect& rect() const { return m_rect; }
100 const Radii& radii() const { return m_radii; }
101 bool isRounded() const { return !m_radii.isZero(); }
102 bool isEmpty() const { return m_rect.isEmpty(); }
104 void setRect(const FloatRect& rect) { m_rect = rect; }
105 void setRadii(const Radii& radii) { m_radii = radii; }
107 void move(const FloatSize& size) { m_rect.move(size); }
108 void inflate(float size) { m_rect.inflate(size); }
109 void expandRadii(float size) { m_radii.expand(size); }
110 void shrinkRadii(float size) { m_radii.shrink(size); }
111 void inflateWithRadii(float size);
114 FloatRect topLeftCorner() const
116 return FloatRect(m_rect.x(), m_rect.y(), m_radii.topLeft().width(), m_radii.topLeft().height());
118 FloatRect topRightCorner() const
120 return FloatRect(m_rect.maxX() - m_radii.topRight().width(), m_rect.y(), m_radii.topRight().width(), m_radii.topRight().height());
122 FloatRect bottomLeftCorner() const
124 return FloatRect(m_rect.x(), m_rect.maxY() - m_radii.bottomLeft().height(), m_radii.bottomLeft().width(), m_radii.bottomLeft().height());
126 FloatRect bottomRightCorner() const
128 return FloatRect(m_rect.maxX() - m_radii.bottomRight().width(), m_rect.maxY() - m_radii.bottomRight().height(), m_radii.bottomRight().width(), m_radii.bottomRight().height());
131 bool isRenderable() const;
132 bool xInterceptsAtY(float y, float& minXIntercept, float& maxXIntercept) const;
139 inline bool operator==(const FloatRoundedRect::Radii& a, const FloatRoundedRect::Radii& b)
141 return a.topLeft() == b.topLeft() && a.topRight() == b.topRight() && a.bottomLeft() == b.bottomLeft() && a.bottomRight() == b.bottomRight();
144 inline bool operator==(const FloatRoundedRect& a, const FloatRoundedRect& b)
146 return a.rect() == b.rect() && a.radii() == b.radii();
149 inline float calcBorderRadiiConstraintScaleFor(const FloatRect& rect, const FloatRoundedRect::Radii& radii)
151 // Constrain corner radii using CSS3 rules:
152 // http://www.w3.org/TR/css3-background/#the-border-radius
158 radiiSum = radii.topLeft().width() + radii.topRight().width(); // Casts to avoid integer overflow.
159 if (radiiSum > rect.width())
160 factor = std::min(rect.width() / radiiSum, factor);
163 radiiSum = radii.bottomLeft().width() + radii.bottomRight().width();
164 if (radiiSum > rect.width())
165 factor = std::min(rect.width() / radiiSum, factor);
168 radiiSum = radii.topLeft().height() + radii.bottomLeft().height();
169 if (radiiSum > rect.height())
170 factor = std::min(rect.height() / radiiSum, factor);
173 radiiSum = radii.topRight().height() + radii.bottomRight().height();
174 if (radiiSum > rect.height())
175 factor = std::min(rect.height() / radiiSum, factor);
181 WEBCORE_EXPORT TextStream& operator<<(TextStream&, const FloatRoundedRect&);
183 } // namespace WebCore
185 #endif // FloatRoundedRect_h