| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2005 Nokia. All rights reserved. | 3 * Copyright (C) 2005 Nokia. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
| 13 * | 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 #include "core/platform/graphics/FloatRect.h" | 28 #include "platform/geometry/FloatRect.h" |
| 29 | 29 |
| 30 #include "core/platform/FloatConversion.h" | 30 #include "platform/FloatConversion.h" |
| 31 #include "core/platform/graphics/IntRect.h" | 31 #include "platform/geometry/IntRect.h" |
| 32 #include "core/platform/graphics/LayoutRect.h" | 32 #include "platform/geometry/LayoutRect.h" |
| 33 #include "third_party/skia/include/core/SkRect.h" | 33 #include "third_party/skia/include/core/SkRect.h" |
| 34 #include "wtf/MathExtras.h" | 34 #include "wtf/MathExtras.h" |
| 35 | 35 |
| 36 #include <algorithm> | 36 #include <algorithm> |
| 37 #include <math.h> | 37 #include <math.h> |
| 38 | 38 |
| 39 using std::max; | |
| 40 using std::min; | |
| 41 | |
| 42 namespace WebCore { | 39 namespace WebCore { |
| 43 | 40 |
| 44 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size
()) | 41 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size
()) |
| 45 { | 42 { |
| 46 } | 43 } |
| 47 | 44 |
| 48 FloatRect::FloatRect(const LayoutRect& r) : m_location(r.location()), m_size(r.s
ize()) | 45 FloatRect::FloatRect(const LayoutRect& r) : m_location(r.location()), m_size(r.s
ize()) |
| 49 { | 46 { |
| 50 } | 47 } |
| 51 | 48 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 81 | 78 |
| 82 bool FloatRect::contains(const FloatPoint& point, ContainsMode containsMode) con
st | 79 bool FloatRect::contains(const FloatPoint& point, ContainsMode containsMode) con
st |
| 83 { | 80 { |
| 84 if (containsMode == InsideOrOnStroke) | 81 if (containsMode == InsideOrOnStroke) |
| 85 return contains(point.x(), point.y()); | 82 return contains(point.x(), point.y()); |
| 86 return x() < point.x() && maxX() > point.x() && y() < point.y() && maxY() >
point.y(); | 83 return x() < point.x() && maxX() > point.x() && y() < point.y() && maxY() >
point.y(); |
| 87 } | 84 } |
| 88 | 85 |
| 89 void FloatRect::intersect(const FloatRect& other) | 86 void FloatRect::intersect(const FloatRect& other) |
| 90 { | 87 { |
| 91 float l = max(x(), other.x()); | 88 float left = std::max(x(), other.x()); |
| 92 float t = max(y(), other.y()); | 89 float top = std::max(y(), other.y()); |
| 93 float r = min(maxX(), other.maxX()); | 90 float right = std::min(maxX(), other.maxX()); |
| 94 float b = min(maxY(), other.maxY()); | 91 float bottom = std::min(maxY(), other.maxY()); |
| 95 | 92 |
| 96 // Return a clean empty rectangle for non-intersecting cases. | 93 // Return a clean empty rectangle for non-intersecting cases. |
| 97 if (l >= r || t >= b) { | 94 if (left >= right || top >= bottom) { |
| 98 l = 0; | 95 left = 0; |
| 99 t = 0; | 96 top = 0; |
| 100 r = 0; | 97 right = 0; |
| 101 b = 0; | 98 bottom = 0; |
| 102 } | 99 } |
| 103 | 100 |
| 104 setLocationAndSizeFromEdges(l, t, r, b); | 101 setLocationAndSizeFromEdges(left, top, right, bottom); |
| 105 } | 102 } |
| 106 | 103 |
| 107 void FloatRect::unite(const FloatRect& other) | 104 void FloatRect::unite(const FloatRect& other) |
| 108 { | 105 { |
| 109 // Handle empty special cases first. | 106 // Handle empty special cases first. |
| 110 if (other.isEmpty()) | 107 if (other.isEmpty()) |
| 111 return; | 108 return; |
| 112 if (isEmpty()) { | 109 if (isEmpty()) { |
| 113 *this = other; | 110 *this = other; |
| 114 return; | 111 return; |
| 115 } | 112 } |
| 116 | 113 |
| 117 uniteEvenIfEmpty(other); | 114 uniteEvenIfEmpty(other); |
| 118 } | 115 } |
| 119 | 116 |
| 120 void FloatRect::uniteEvenIfEmpty(const FloatRect& other) | 117 void FloatRect::uniteEvenIfEmpty(const FloatRect& other) |
| 121 { | 118 { |
| 122 float minX = min(x(), other.x()); | 119 float minX = std::min(x(), other.x()); |
| 123 float minY = min(y(), other.y()); | 120 float minY = std::min(y(), other.y()); |
| 124 float maxX = max(this->maxX(), other.maxX()); | 121 float maxX = std::max(this->maxX(), other.maxX()); |
| 125 float maxY = max(this->maxY(), other.maxY()); | 122 float maxY = std::max(this->maxY(), other.maxY()); |
| 126 | 123 |
| 127 setLocationAndSizeFromEdges(minX, minY, maxX, maxY); | 124 setLocationAndSizeFromEdges(minX, minY, maxX, maxY); |
| 128 } | 125 } |
| 129 | 126 |
| 130 void FloatRect::uniteIfNonZero(const FloatRect& other) | 127 void FloatRect::uniteIfNonZero(const FloatRect& other) |
| 131 { | 128 { |
| 132 // Handle empty special cases first. | 129 // Handle empty special cases first. |
| 133 if (other.isZero()) | 130 if (other.isZero()) |
| 134 return; | 131 return; |
| 135 if (isZero()) { | 132 if (isZero()) { |
| 136 *this = other; | 133 *this = other; |
| 137 return; | 134 return; |
| 138 } | 135 } |
| 139 | 136 |
| 140 uniteEvenIfEmpty(other); | 137 uniteEvenIfEmpty(other); |
| 141 } | 138 } |
| 142 | 139 |
| 143 void FloatRect::extend(const FloatPoint& p) | 140 void FloatRect::extend(const FloatPoint& p) |
| 144 { | 141 { |
| 145 float minX = min(x(), p.x()); | 142 float minX = std::min(x(), p.x()); |
| 146 float minY = min(y(), p.y()); | 143 float minY = std::min(y(), p.y()); |
| 147 float maxX = max(this->maxX(), p.x()); | 144 float maxX = std::max(this->maxX(), p.x()); |
| 148 float maxY = max(this->maxY(), p.y()); | 145 float maxY = std::max(this->maxY(), p.y()); |
| 149 | 146 |
| 150 setLocationAndSizeFromEdges(minX, minY, maxX, maxY); | 147 setLocationAndSizeFromEdges(minX, minY, maxX, maxY); |
| 151 } | 148 } |
| 152 | 149 |
| 153 void FloatRect::scale(float sx, float sy) | 150 void FloatRect::scale(float sx, float sy) |
| 154 { | 151 { |
| 155 m_location.setX(x() * sx); | 152 m_location.setX(x() * sx); |
| 156 m_location.setY(y() * sy); | 153 m_location.setY(y() * sy); |
| 157 m_size.setWidth(width() * sx); | 154 m_size.setWidth(width() * sx); |
| 158 m_size.setHeight(height() * sy); | 155 m_size.setHeight(height() * sy); |
| 159 } | 156 } |
| 160 | 157 |
| 161 FloatRect unionRect(const Vector<FloatRect>& rects) | 158 FloatRect unionRect(const Vector<FloatRect>& rects) |
| 162 { | 159 { |
| 163 FloatRect result; | 160 FloatRect result; |
| 164 | 161 |
| 165 size_t count = rects.size(); | 162 size_t count = rects.size(); |
| 166 for (size_t i = 0; i < count; ++i) | 163 for (size_t i = 0; i < count; ++i) |
| 167 result.unite(rects[i]); | 164 result.unite(rects[i]); |
| 168 | 165 |
| 169 return result; | 166 return result; |
| 170 } | 167 } |
| 171 | 168 |
| 172 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1) | 169 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1) |
| 173 { | 170 { |
| 174 float left = min(p0.x(), p1.x()); | 171 float left = std::min(p0.x(), p1.x()); |
| 175 float top = min(p0.y(), p1.y()); | 172 float top = std::min(p0.y(), p1.y()); |
| 176 float right = max(p0.x(), p1.x()); | 173 float right = std::max(p0.x(), p1.x()); |
| 177 float bottom = max(p0.y(), p1.y()); | 174 float bottom = std::max(p0.y(), p1.y()); |
| 178 | 175 |
| 179 setLocationAndSizeFromEdges(left, top, right, bottom); | 176 setLocationAndSizeFromEdges(left, top, right, bottom); |
| 180 } | 177 } |
| 181 | 178 |
| 182 namespace { | 179 namespace { |
| 183 // Helpers for 3- and 4-way max and min. | 180 // Helpers for 3- and 4-way max and min. |
| 184 | 181 |
| 185 template <typename T> | 182 template <typename T> |
| 186 T min3(const T& v1, const T& v2, const T& v3) | 183 T min3(const T& v1, const T& v2, const T& v3) |
| 187 { | 184 { |
| 188 return min(min(v1, v2), v3); | 185 return std::min(std::min(v1, v2), v3); |
| 189 } | 186 } |
| 190 | 187 |
| 191 template <typename T> | 188 template <typename T> |
| 192 T max3(const T& v1, const T& v2, const T& v3) | 189 T max3(const T& v1, const T& v2, const T& v3) |
| 193 { | 190 { |
| 194 return max(max(v1, v2), v3); | 191 return std::max(std::max(v1, v2), v3); |
| 195 } | 192 } |
| 196 | 193 |
| 197 template <typename T> | 194 template <typename T> |
| 198 T min4(const T& v1, const T& v2, const T& v3, const T& v4) | 195 T min4(const T& v1, const T& v2, const T& v3, const T& v4) |
| 199 { | 196 { |
| 200 return min(min(v1, v2), min(v3, v4)); | 197 return std::min(std::min(v1, v2), std::min(v3, v4)); |
| 201 } | 198 } |
| 202 | 199 |
| 203 template <typename T> | 200 template <typename T> |
| 204 T max4(const T& v1, const T& v2, const T& v3, const T& v4) | 201 T max4(const T& v1, const T& v2, const T& v3, const T& v4) |
| 205 { | 202 { |
| 206 return max(max(v1, v2), max(v3, v4)); | 203 return std::max(std::max(v1, v2), std::max(v3, v4)); |
| 207 } | 204 } |
| 208 | 205 |
| 209 } // anonymous namespace | 206 } // anonymous namespace |
| 210 | 207 |
| 211 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const Fl
oatPoint& p2) | 208 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const Fl
oatPoint& p2) |
| 212 { | 209 { |
| 213 float left = min3(p0.x(), p1.x(), p2.x()); | 210 float left = min3(p0.x(), p1.x(), p2.x()); |
| 214 float top = min3(p0.y(), p1.y(), p2.y()); | 211 float top = min3(p0.y(), p1.y(), p2.y()); |
| 215 float right = max3(p0.x(), p1.x(), p2.x()); | 212 float right = max3(p0.x(), p1.x(), p2.x()); |
| 216 float bottom = max3(p0.y(), p1.y(), p2.y()); | 213 float bottom = max3(p0.y(), p1.y(), p2.y()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 return IntRect(location, size); | 249 return IntRect(location, size); |
| 253 } | 250 } |
| 254 | 251 |
| 255 IntRect roundedIntRect(const FloatRect& rect) | 252 IntRect roundedIntRect(const FloatRect& rect) |
| 256 { | 253 { |
| 257 return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size())
); | 254 return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size())
); |
| 258 } | 255 } |
| 259 | 256 |
| 260 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect&
destRect) | 257 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect&
destRect) |
| 261 { | 258 { |
| 262 if (srcRect.width() == 0 || srcRect.height() == 0) | 259 if (!srcRect.width() || !srcRect.height()) |
| 263 return FloatRect(); | 260 return FloatRect(); |
| 264 | 261 |
| 265 float widthScale = destRect.width() / srcRect.width(); | 262 float widthScale = destRect.width() / srcRect.width(); |
| 266 float heightScale = destRect.height() / srcRect.height(); | 263 float heightScale = destRect.height() / srcRect.height(); |
| 267 return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale, | 264 return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale, |
| 268 destRect.y() + (r.y() - srcRect.y()) * heightScale, | 265 destRect.y() + (r.y() - srcRect.y()) * heightScale, |
| 269 r.width() * widthScale, r.height() * heightScale); | 266 r.width() * widthScale, r.height() * heightScale); |
| 270 } | 267 } |
| 271 | 268 |
| 272 } | 269 } |
| OLD | NEW |