1 | /* |
2 | * Copyright (C) 2013 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 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. |
28 | */ |
29 | |
30 | #include "config.h" |
31 | #include "FloatRoundedRect.h" |
32 | |
33 | #include <algorithm> |
34 | #include <wtf/text/TextStream.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | FloatRoundedRect::FloatRoundedRect(const RoundedRect& rect) |
39 | : m_rect(rect.rect()) |
40 | , m_radii(rect.radii()) |
41 | { |
42 | } |
43 | |
44 | FloatRoundedRect::FloatRoundedRect(float x, float y, float width, float height) |
45 | : m_rect(x, y, width, height) |
46 | { |
47 | } |
48 | |
49 | FloatRoundedRect::FloatRoundedRect(const FloatRect& rect, const Radii& radii) |
50 | : m_rect(rect) |
51 | , m_radii(radii) |
52 | { |
53 | } |
54 | |
55 | FloatRoundedRect::FloatRoundedRect(const FloatRect& rect, const FloatSize& topLeft, const FloatSize& topRight, const FloatSize& bottomLeft, const FloatSize& bottomRight) |
56 | : m_rect(rect) |
57 | , m_radii(topLeft, topRight, bottomLeft, bottomRight) |
58 | { |
59 | } |
60 | |
61 | bool FloatRoundedRect::Radii::isZero() const |
62 | { |
63 | return m_topLeft.isZero() && m_topRight.isZero() && m_bottomLeft.isZero() && m_bottomRight.isZero(); |
64 | } |
65 | |
66 | bool FloatRoundedRect::Radii::isUniformCornerRadius() const |
67 | { |
68 | return WTF::areEssentiallyEqual(m_topLeft.width(), m_topLeft.height()) |
69 | && areEssentiallyEqual(m_topLeft, m_topRight) |
70 | && areEssentiallyEqual(m_topLeft, m_bottomLeft) |
71 | && areEssentiallyEqual(m_topLeft, m_bottomRight); |
72 | } |
73 | |
74 | void FloatRoundedRect::Radii::scale(float factor) |
75 | { |
76 | scale(factor, factor); |
77 | } |
78 | |
79 | void FloatRoundedRect::Radii::scale(float horizontalFactor, float verticalFactor) |
80 | { |
81 | if (horizontalFactor == 1 && verticalFactor == 1) |
82 | return; |
83 | |
84 | // If either radius on a corner becomes zero, reset both radii on that corner. |
85 | m_topLeft.scale(horizontalFactor, verticalFactor); |
86 | if (!m_topLeft.width() || !m_topLeft.height()) |
87 | m_topLeft = FloatSize(); |
88 | m_topRight.scale(horizontalFactor, verticalFactor); |
89 | if (!m_topRight.width() || !m_topRight.height()) |
90 | m_topRight = FloatSize(); |
91 | m_bottomLeft.scale(horizontalFactor, verticalFactor); |
92 | if (!m_bottomLeft.width() || !m_bottomLeft.height()) |
93 | m_bottomLeft = FloatSize(); |
94 | m_bottomRight.scale(horizontalFactor, verticalFactor); |
95 | if (!m_bottomRight.width() || !m_bottomRight.height()) |
96 | m_bottomRight = FloatSize(); |
97 | } |
98 | |
99 | void FloatRoundedRect::Radii::expand(float topWidth, float bottomWidth, float leftWidth, float rightWidth) |
100 | { |
101 | if (m_topLeft.width() > 0 && m_topLeft.height() > 0) { |
102 | m_topLeft.setWidth(std::max<float>(0, m_topLeft.width() + leftWidth)); |
103 | m_topLeft.setHeight(std::max<float>(0, m_topLeft.height() + topWidth)); |
104 | } |
105 | if (m_topRight.width() > 0 && m_topRight.height() > 0) { |
106 | m_topRight.setWidth(std::max<float>(0, m_topRight.width() + rightWidth)); |
107 | m_topRight.setHeight(std::max<float>(0, m_topRight.height() + topWidth)); |
108 | } |
109 | if (m_bottomLeft.width() > 0 && m_bottomLeft.height() > 0) { |
110 | m_bottomLeft.setWidth(std::max<float>(0, m_bottomLeft.width() + leftWidth)); |
111 | m_bottomLeft.setHeight(std::max<float>(0, m_bottomLeft.height() + bottomWidth)); |
112 | } |
113 | if (m_bottomRight.width() > 0 && m_bottomRight.height() > 0) { |
114 | m_bottomRight.setWidth(std::max<float>(0, m_bottomRight.width() + rightWidth)); |
115 | m_bottomRight.setHeight(std::max<float>(0, m_bottomRight.height() + bottomWidth)); |
116 | } |
117 | } |
118 | |
119 | static inline float cornerRectIntercept(float y, const FloatRect& cornerRect) |
120 | { |
121 | ASSERT(cornerRect.height() > 0); |
122 | return cornerRect.width() * sqrt(1 - (y * y) / (cornerRect.height() * cornerRect.height())); |
123 | } |
124 | |
125 | bool FloatRoundedRect::xInterceptsAtY(float y, float& minXIntercept, float& maxXIntercept) const |
126 | { |
127 | if (y < rect().y() || y > rect().maxY()) |
128 | return false; |
129 | |
130 | if (!isRounded()) { |
131 | minXIntercept = rect().x(); |
132 | maxXIntercept = rect().maxX(); |
133 | return true; |
134 | } |
135 | |
136 | const FloatRect& topLeftRect = topLeftCorner(); |
137 | const FloatRect& bottomLeftRect = bottomLeftCorner(); |
138 | |
139 | if (!topLeftRect.isEmpty() && y >= topLeftRect.y() && y < topLeftRect.maxY()) |
140 | minXIntercept = topLeftRect.maxX() - cornerRectIntercept(topLeftRect.maxY() - y, topLeftRect); |
141 | else if (!bottomLeftRect.isEmpty() && y >= bottomLeftRect.y() && y <= bottomLeftRect.maxY()) |
142 | minXIntercept = bottomLeftRect.maxX() - cornerRectIntercept(y - bottomLeftRect.y(), bottomLeftRect); |
143 | else |
144 | minXIntercept = m_rect.x(); |
145 | |
146 | const FloatRect& topRightRect = topRightCorner(); |
147 | const FloatRect& bottomRightRect = bottomRightCorner(); |
148 | |
149 | if (!topRightRect.isEmpty() && y >= topRightRect.y() && y <= topRightRect.maxY()) |
150 | maxXIntercept = topRightRect.x() + cornerRectIntercept(topRightRect.maxY() - y, topRightRect); |
151 | else if (!bottomRightRect.isEmpty() && y >= bottomRightRect.y() && y <= bottomRightRect.maxY()) |
152 | maxXIntercept = bottomRightRect.x() + cornerRectIntercept(y - bottomRightRect.y(), bottomRightRect); |
153 | else |
154 | maxXIntercept = m_rect.maxX(); |
155 | |
156 | return true; |
157 | } |
158 | |
159 | bool FloatRoundedRect::isRenderable() const |
160 | { |
161 | return m_radii.topLeft().width() >= 0 && m_radii.topLeft().height() >= 0 |
162 | && m_radii.bottomLeft().width() >= 0 && m_radii.bottomLeft().height() >= 0 |
163 | && m_radii.topRight().width() >= 0 && m_radii.topRight().height() >= 0 |
164 | && m_radii.bottomRight().width() >= 0 && m_radii.bottomRight().height() >= 0 |
165 | && m_radii.topLeft().width() + m_radii.topRight().width() <= m_rect.width() |
166 | && m_radii.bottomLeft().width() + m_radii.bottomRight().width() <= m_rect.width() |
167 | && m_radii.topLeft().height() + m_radii.bottomLeft().height() <= m_rect.height() |
168 | && m_radii.topRight().height() + m_radii.bottomRight().height() <= m_rect.height(); |
169 | } |
170 | |
171 | void FloatRoundedRect::inflateWithRadii(float size) |
172 | { |
173 | FloatRect old = m_rect; |
174 | |
175 | m_rect.inflate(size); |
176 | // Considering the inflation factor of shorter size to scale the radii seems appropriate here |
177 | float factor; |
178 | if (m_rect.width() < m_rect.height()) |
179 | factor = old.width() ? m_rect.width() / old.width() : 0; |
180 | else |
181 | factor = old.height() ? m_rect.height() / old.height() : 0; |
182 | |
183 | m_radii.scale(factor); |
184 | } |
185 | |
186 | void FloatRoundedRect::adjustRadii() |
187 | { |
188 | float maxRadiusWidth = std::max(m_radii.topLeft().width() + m_radii.topRight().width(), m_radii.bottomLeft().width() + m_radii.bottomRight().width()); |
189 | float maxRadiusHeight = std::max(m_radii.topLeft().height() + m_radii.bottomLeft().height(), m_radii.topRight().height() + m_radii.bottomRight().height()); |
190 | |
191 | if (maxRadiusWidth <= 0 || maxRadiusHeight <= 0) { |
192 | m_radii.scale(0.0f); |
193 | return; |
194 | } |
195 | float widthRatio = m_rect.width() / maxRadiusWidth; |
196 | float heightRatio = m_rect.height() / maxRadiusHeight; |
197 | m_radii.scale(widthRatio < heightRatio ? widthRatio : heightRatio); |
198 | } |
199 | |
200 | // This is conservative; it does not test intrusion into the corner rects. |
201 | bool FloatRoundedRect::intersectionIsRectangular(const FloatRect& rect) const |
202 | { |
203 | return !(rect.intersects(topLeftCorner()) || rect.intersects(topRightCorner()) || rect.intersects(bottomLeftCorner()) || rect.intersects(bottomRightCorner())); |
204 | } |
205 | |
206 | TextStream& operator<<(TextStream& ts, const FloatRoundedRect& roundedRect) |
207 | { |
208 | ts << roundedRect.rect().x() << " " << roundedRect.rect().y() << " " << roundedRect.rect().width() << " " << roundedRect.rect().height() << "\n" ; |
209 | |
210 | TextStream::IndentScope indentScope(ts); |
211 | ts << indent << "topLeft=" << roundedRect.topLeftCorner().width() << " " << roundedRect.topLeftCorner().height() << "\n" ; |
212 | ts << indent << "topRight=" << roundedRect.topRightCorner().width() << " " << roundedRect.topRightCorner().height() << "\n" ; |
213 | ts << indent << "bottomLeft=" << roundedRect.bottomLeftCorner().width() << " " << roundedRect.bottomLeftCorner().height() << "\n" ; |
214 | ts << indent << "bottomRight=" << roundedRect.bottomRightCorner().width() << " " << roundedRect.bottomRightCorner().height(); |
215 | return ts; |
216 | } |
217 | |
218 | |
219 | |
220 | } // namespace WebCore |
221 | |