1 /* 2 * Copyright (c) 2012, Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "core/platform/graphics/LayoutRect.h" 33 34 #include <algorithm> 35 #include "core/platform/LayoutUnit.h" 36 #include "core/platform/graphics/FloatRect.h" 37 38 using std::max; 39 using std::min; 40 41 namespace WebCore { 42 43 LayoutRect::LayoutRect(const FloatRect& r) 44 : m_location(LayoutPoint(r.location())) 45 , m_size(LayoutSize(r.size())) 46 { 47 } 48 49 bool LayoutRect::intersects(const LayoutRect& other) const 50 { 51 // Checking emptiness handles negative widths as well as zero. 52 return !isEmpty() && !other.isEmpty() 53 && x() < other.maxX() && other.x() < maxX() 54 && y() < other.maxY() && other.y() < maxY(); 55 } 56 57 bool LayoutRect::contains(const LayoutRect& other) const 58 { 59 return x() <= other.x() && maxX() >= other.maxX() 60 && y() <= other.y() && maxY() >= other.maxY(); 61 } 62 63 void LayoutRect::intersect(const LayoutRect& other) 64 { 65 LayoutPoint newLocation(max(x(), other.x()), max(y(), other.y())); 66 LayoutPoint newMaxPoint(min(maxX(), other.maxX()), min(maxY(), other.maxY())); 67 68 // Return a clean empty rectangle for non-intersecting cases. 69 if (newLocation.x() >= newMaxPoint.x() || newLocation.y() >= newMaxPoint.y()) { 70 newLocation = LayoutPoint(0, 0); 71 newMaxPoint = LayoutPoint(0, 0); 72 } 73 74 m_location = newLocation; 75 m_size = newMaxPoint - newLocation; 76 } 77 78 void LayoutRect::unite(const LayoutRect& other) 79 { 80 // Handle empty special cases first. 81 if (other.isEmpty()) 82 return; 83 if (isEmpty()) { 84 *this = other; 85 return; 86 } 87 88 LayoutPoint newLocation(min(x(), other.x()), min(y(), other.y())); 89 LayoutPoint newMaxPoint(max(maxX(), other.maxX()), max(maxY(), other.maxY())); 90 91 m_location = newLocation; 92 m_size = newMaxPoint - newLocation; 93 } 94 95 void LayoutRect::uniteIfNonZero(const LayoutRect& other) 96 { 97 // Handle empty special cases first. 98 if (!other.width() && !other.height()) 99 return; 100 if (!width() && !height()) { 101 *this = other; 102 return; 103 } 104 105 LayoutPoint newLocation(min(x(), other.x()), min(y(), other.y())); 106 LayoutPoint newMaxPoint(max(maxX(), other.maxX()), max(maxY(), other.maxY())); 107 108 m_location = newLocation; 109 m_size = newMaxPoint - newLocation; 110 } 111 112 void LayoutRect::scale(float s) 113 { 114 m_location.scale(s, s); 115 m_size.scale(s); 116 } 117 118 LayoutRect unionRect(const Vector<LayoutRect>& rects) 119 { 120 LayoutRect result; 121 122 size_t count = rects.size(); 123 for (size_t i = 0; i < count; ++i) 124 result.unite(rects[i]); 125 126 return result; 127 } 128 129 IntRect enclosingIntRect(const LayoutRect& rect) 130 { 131 IntPoint location = flooredIntPoint(rect.minXMinYCorner()); 132 IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner()); 133 134 return IntRect(location, maxPoint - location); 135 } 136 137 LayoutRect enclosingLayoutRect(const FloatRect& rect) 138 { 139 LayoutPoint location = flooredLayoutPoint(rect.minXMinYCorner()); 140 LayoutPoint maxPoint = ceiledLayoutPoint(rect.maxXMaxYCorner()); 141 return LayoutRect(location, maxPoint - location); 142 } 143 144 } // namespace WebCore 145