1/* 2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2005 Nokia. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 25 */ 26 27#include "config.h" 28#include "FloatRect.h" 29 30#include "FloatConversion.h" 31#include "IntRect.h" 32#include <algorithm> 33#include <math.h> 34#include <wtf/MathExtras.h> 35#include <wtf/PrintStream.h> 36 37namespace WebCore { 38 39FloatRect::FloatRect(const IntRect& r) 40 : m_location(r.location()) 41 , m_size(r.size()) 42{ 43} 44 45FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height) 46{ 47 return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height)); 48} 49 50bool FloatRect::isExpressibleAsIntRect() const 51{ 52 return isWithinIntRange(x()) && isWithinIntRange(y()) 53 && isWithinIntRange(width()) && isWithinIntRange(height()) 54 && isWithinIntRange(maxX()) && isWithinIntRange(maxY()); 55} 56 57bool FloatRect::intersects(const FloatRect& other) const 58{ 59 // Checking emptiness handles negative widths as well as zero. 60 return !isEmpty() && !other.isEmpty() 61 && x() < other.maxX() && other.x() < maxX() 62 && y() < other.maxY() && other.y() < maxY(); 63} 64 65bool FloatRect::contains(const FloatRect& other) const 66{ 67 return x() <= other.x() && maxX() >= other.maxX() 68 && y() <= other.y() && maxY() >= other.maxY(); 69} 70 71bool FloatRect::contains(const FloatPoint& point, ContainsMode containsMode) const 72{ 73 if (containsMode == InsideOrOnStroke) 74 return contains(point.x(), point.y()); 75 return x() < point.x() && maxX() > point.x() && y() < point.y() && maxY() > point.y(); 76} 77 78void FloatRect::intersect(const FloatRect& other) 79{ 80 float l = std::max(x(), other.x()); 81 float t = std::max(y(), other.y()); 82 float r = std::min(maxX(), other.maxX()); 83 float b = std::min(maxY(), other.maxY()); 84 85 // Return a clean empty rectangle for non-intersecting cases. 86 if (l >= r || t >= b) { 87 l = 0; 88 t = 0; 89 r = 0; 90 b = 0; 91 } 92 93 setLocationAndSizeFromEdges(l, t, r, b); 94} 95 96void FloatRect::unite(const FloatRect& other) 97{ 98 // Handle empty special cases first. 99 if (other.isEmpty()) 100 return; 101 if (isEmpty()) { 102 *this = other; 103 return; 104 } 105 106 uniteEvenIfEmpty(other); 107} 108 109void FloatRect::uniteEvenIfEmpty(const FloatRect& other) 110{ 111 float minX = std::min(x(), other.x()); 112 float minY = std::min(y(), other.y()); 113 float maxX = std::max(this->maxX(), other.maxX()); 114 float maxY = std::max(this->maxY(), other.maxY()); 115 116 setLocationAndSizeFromEdges(minX, minY, maxX, maxY); 117} 118 119void FloatRect::uniteIfNonZero(const FloatRect& other) 120{ 121 // Handle empty special cases first. 122 if (other.isZero()) 123 return; 124 if (isZero()) { 125 *this = other; 126 return; 127 } 128 129 uniteEvenIfEmpty(other); 130} 131 132void FloatRect::extend(const FloatPoint& p) 133{ 134 float minX = std::min(x(), p.x()); 135 float minY = std::min(y(), p.y()); 136 float maxX = std::max(this->maxX(), p.x()); 137 float maxY = std::max(this->maxY(), p.y()); 138 139 setLocationAndSizeFromEdges(minX, minY, maxX, maxY); 140} 141 142void FloatRect::scale(float sx, float sy) 143{ 144 m_location.setX(x() * sx); 145 m_location.setY(y() * sy); 146 m_size.setWidth(width() * sx); 147 m_size.setHeight(height() * sy); 148} 149 150void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1) 151{ 152 float left = std::min(p0.x(), p1.x()); 153 float top = std::min(p0.y(), p1.y()); 154 float right = std::max(p0.x(), p1.x()); 155 float bottom = std::max(p0.y(), p1.y()); 156 157 setLocationAndSizeFromEdges(left, top, right, bottom); 158} 159 160namespace { 161// Helpers for 3- and 4-way max and min. 162 163template <typename T> 164T min3(const T& v1, const T& v2, const T& v3) 165{ 166 return std::min(std::min(v1, v2), v3); 167} 168 169template <typename T> 170T max3(const T& v1, const T& v2, const T& v3) 171{ 172 return std::max(std::max(v1, v2), v3); 173} 174 175template <typename T> 176T min4(const T& v1, const T& v2, const T& v3, const T& v4) 177{ 178 return std::min(std::min(v1, v2), std::min(v3, v4)); 179} 180 181template <typename T> 182T max4(const T& v1, const T& v2, const T& v3, const T& v4) 183{ 184 return std::max(std::max(v1, v2), std::max(v3, v4)); 185} 186 187} // anonymous namespace 188 189void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2) 190{ 191 float left = min3(p0.x(), p1.x(), p2.x()); 192 float top = min3(p0.y(), p1.y(), p2.y()); 193 float right = max3(p0.x(), p1.x(), p2.x()); 194 float bottom = max3(p0.y(), p1.y(), p2.y()); 195 196 setLocationAndSizeFromEdges(left, top, right, bottom); 197} 198 199void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3) 200{ 201 float left = min4(p0.x(), p1.x(), p2.x(), p3.x()); 202 float top = min4(p0.y(), p1.y(), p2.y(), p3.y()); 203 float right = max4(p0.x(), p1.x(), p2.x(), p3.x()); 204 float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y()); 205 206 setLocationAndSizeFromEdges(left, top, right, bottom); 207} 208 209FloatRect enclosingRectExtendedToDevicePixels(const FloatRect& rect, float deviceScaleFactor) 210{ 211 FloatPoint location = flooredToDevicePixels(rect.minXMinYCorner(), deviceScaleFactor); 212 FloatPoint maxPoint = ceiledToDevicePixels(rect.maxXMaxYCorner(), deviceScaleFactor); 213 return FloatRect(location, maxPoint - location); 214} 215 216IntRect enclosingIntRect(const FloatRect& rect) 217{ 218 IntPoint location = flooredIntPoint(rect.minXMinYCorner()); 219 IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner()); 220 221 return IntRect(location, maxPoint - location); 222} 223 224IntRect enclosedIntRect(const FloatRect& rect) 225{ 226 IntPoint location = ceiledIntPoint(rect.minXMinYCorner()); 227 IntPoint maxPoint = flooredIntPoint(rect.maxXMaxYCorner()); 228 IntSize size = maxPoint - location; 229 size.clampNegativeToZero(); 230 231 return IntRect(location, size); 232} 233 234IntRect roundedIntRect(const FloatRect& rect) 235{ 236 return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size())); 237} 238 239void FloatRect::dump(PrintStream& out) const 240{ 241 out.print(location(), " ", size()); 242} 243 244} 245