1/* 2 * Copyright (C) 2003, 2006, 2009 Apple Inc. 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 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "IntRect.h" 28 29#include "FloatRect.h" 30#include "LayoutRect.h" 31#include <algorithm> 32 33using std::max; 34using std::min; 35 36namespace WebCore { 37 38IntRect::IntRect(const FloatRect& r) 39 : m_location(clampToInteger(r.x()), clampToInteger(r.y())) 40 , m_size(clampToInteger(r.width()), clampToInteger(r.height())) 41{ 42} 43 44IntRect::IntRect(const LayoutRect& r) 45 : m_location(r.x(), r.y()) 46 , m_size(r.width(), r.height()) 47{ 48} 49 50bool IntRect::intersects(const IntRect& other) const 51{ 52 // Checking emptiness handles negative widths as well as zero. 53 return !isEmpty() && !other.isEmpty() 54 && x() < other.maxX() && other.x() < maxX() 55 && y() < other.maxY() && other.y() < maxY(); 56} 57 58bool IntRect::contains(const IntRect& other) const 59{ 60 return x() <= other.x() && maxX() >= other.maxX() 61 && y() <= other.y() && maxY() >= other.maxY(); 62} 63 64void IntRect::intersect(const IntRect& other) 65{ 66 int l = max(x(), other.x()); 67 int t = max(y(), other.y()); 68 int r = min(maxX(), other.maxX()); 69 int b = min(maxY(), other.maxY()); 70 71 // Return a clean empty rectangle for non-intersecting cases. 72 if (l >= r || t >= b) { 73 l = 0; 74 t = 0; 75 r = 0; 76 b = 0; 77 } 78 79 m_location.setX(l); 80 m_location.setY(t); 81 m_size.setWidth(r - l); 82 m_size.setHeight(b - t); 83} 84 85void IntRect::unite(const IntRect& other) 86{ 87 // Handle empty special cases first. 88 if (other.isEmpty()) 89 return; 90 if (isEmpty()) { 91 *this = other; 92 return; 93 } 94 95 int l = min(x(), other.x()); 96 int t = min(y(), other.y()); 97 int r = max(maxX(), other.maxX()); 98 int b = max(maxY(), other.maxY()); 99 100 m_location.setX(l); 101 m_location.setY(t); 102 m_size.setWidth(r - l); 103 m_size.setHeight(b - t); 104} 105 106void IntRect::uniteIfNonZero(const IntRect& other) 107{ 108 // Handle empty special cases first. 109 if (!other.width() && !other.height()) 110 return; 111 if (!width() && !height()) { 112 *this = other; 113 return; 114 } 115 116 int left = min(x(), other.x()); 117 int top = min(y(), other.y()); 118 int right = max(maxX(), other.maxX()); 119 int bottom = max(maxY(), other.maxY()); 120 121 m_location.setX(left); 122 m_location.setY(top); 123 m_size.setWidth(right - left); 124 m_size.setHeight(bottom - top); 125} 126 127void IntRect::scale(float s) 128{ 129 m_location.setX((int)(x() * s)); 130 m_location.setY((int)(y() * s)); 131 m_size.setWidth((int)(width() * s)); 132 m_size.setHeight((int)(height() * s)); 133} 134 135static inline int distanceToInterval(int pos, int start, int end) 136{ 137 if (pos < start) 138 return start - pos; 139 if (pos > end) 140 return end - pos; 141 return 0; 142} 143 144IntSize IntRect::differenceToPoint(const IntPoint& point) const 145{ 146 int xdistance = distanceToInterval(point.x(), x(), maxX()); 147 int ydistance = distanceToInterval(point.y(), y(), maxY()); 148 return IntSize(xdistance, ydistance); 149} 150 151IntRect unionRect(const Vector<IntRect>& rects) 152{ 153 IntRect result; 154 155 size_t count = rects.size(); 156 for (size_t i = 0; i < count; ++i) 157 result.unite(rects[i]); 158 159 return result; 160} 161 162} // namespace WebCore 163