OLD | NEW |
| (Empty) |
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 "core/platform/graphics/IntRect.h" | |
28 | |
29 #include "core/platform/graphics/FloatRect.h" | |
30 #include "core/platform/graphics/LayoutRect.h" | |
31 #include "third_party/skia/include/core/SkRect.h" | |
32 | |
33 #include <algorithm> | |
34 | |
35 using std::max; | |
36 using std::min; | |
37 | |
38 namespace WebCore { | |
39 | |
40 IntRect::IntRect(const FloatRect& r) | |
41 : m_location(clampToInteger(r.x()), clampToInteger(r.y())) | |
42 , m_size(clampToInteger(r.width()), clampToInteger(r.height())) | |
43 { | |
44 } | |
45 | |
46 IntRect::IntRect(const LayoutRect& r) | |
47 : m_location(r.x(), r.y()) | |
48 , m_size(r.width(), r.height()) | |
49 { | |
50 } | |
51 | |
52 bool IntRect::intersects(const IntRect& other) const | |
53 { | |
54 // Checking emptiness handles negative widths as well as zero. | |
55 return !isEmpty() && !other.isEmpty() | |
56 && x() < other.maxX() && other.x() < maxX() | |
57 && y() < other.maxY() && other.y() < maxY(); | |
58 } | |
59 | |
60 bool IntRect::contains(const IntRect& other) const | |
61 { | |
62 return x() <= other.x() && maxX() >= other.maxX() | |
63 && y() <= other.y() && maxY() >= other.maxY(); | |
64 } | |
65 | |
66 void IntRect::intersect(const IntRect& other) | |
67 { | |
68 int l = max(x(), other.x()); | |
69 int t = max(y(), other.y()); | |
70 int r = min(maxX(), other.maxX()); | |
71 int b = min(maxY(), other.maxY()); | |
72 | |
73 // Return a clean empty rectangle for non-intersecting cases. | |
74 if (l >= r || t >= b) { | |
75 l = 0; | |
76 t = 0; | |
77 r = 0; | |
78 b = 0; | |
79 } | |
80 | |
81 m_location.setX(l); | |
82 m_location.setY(t); | |
83 m_size.setWidth(r - l); | |
84 m_size.setHeight(b - t); | |
85 } | |
86 | |
87 void IntRect::unite(const IntRect& other) | |
88 { | |
89 // Handle empty special cases first. | |
90 if (other.isEmpty()) | |
91 return; | |
92 if (isEmpty()) { | |
93 *this = other; | |
94 return; | |
95 } | |
96 | |
97 int l = min(x(), other.x()); | |
98 int t = min(y(), other.y()); | |
99 int r = max(maxX(), other.maxX()); | |
100 int b = max(maxY(), other.maxY()); | |
101 | |
102 m_location.setX(l); | |
103 m_location.setY(t); | |
104 m_size.setWidth(r - l); | |
105 m_size.setHeight(b - t); | |
106 } | |
107 | |
108 void IntRect::uniteIfNonZero(const IntRect& other) | |
109 { | |
110 // Handle empty special cases first. | |
111 if (!other.width() && !other.height()) | |
112 return; | |
113 if (!width() && !height()) { | |
114 *this = other; | |
115 return; | |
116 } | |
117 | |
118 int left = min(x(), other.x()); | |
119 int top = min(y(), other.y()); | |
120 int right = max(maxX(), other.maxX()); | |
121 int bottom = max(maxY(), other.maxY()); | |
122 | |
123 m_location.setX(left); | |
124 m_location.setY(top); | |
125 m_size.setWidth(right - left); | |
126 m_size.setHeight(bottom - top); | |
127 } | |
128 | |
129 void IntRect::scale(float s) | |
130 { | |
131 m_location.setX((int)(x() * s)); | |
132 m_location.setY((int)(y() * s)); | |
133 m_size.setWidth((int)(width() * s)); | |
134 m_size.setHeight((int)(height() * s)); | |
135 } | |
136 | |
137 static inline int distanceToInterval(int pos, int start, int end) | |
138 { | |
139 if (pos < start) | |
140 return start - pos; | |
141 if (pos > end) | |
142 return end - pos; | |
143 return 0; | |
144 } | |
145 | |
146 IntSize IntRect::differenceToPoint(const IntPoint& point) const | |
147 { | |
148 int xdistance = distanceToInterval(point.x(), x(), maxX()); | |
149 int ydistance = distanceToInterval(point.y(), y(), maxY()); | |
150 return IntSize(xdistance, ydistance); | |
151 } | |
152 | |
153 IntRect::operator SkIRect() const | |
154 { | |
155 SkIRect rect = { x(), y(), maxX(), maxY() }; | |
156 return rect; | |
157 } | |
158 | |
159 IntRect::operator SkRect() const | |
160 { | |
161 SkRect rect; | |
162 rect.set(SkIntToScalar(x()), SkIntToScalar(y()), SkIntToScalar(maxX()), SkIn
tToScalar(maxY())); | |
163 return rect; | |
164 } | |
165 | |
166 IntRect unionRect(const Vector<IntRect>& rects) | |
167 { | |
168 IntRect result; | |
169 | |
170 size_t count = rects.size(); | |
171 for (size_t i = 0; i < count; ++i) | |
172 result.unite(rects[i]); | |
173 | |
174 return result; | |
175 } | |
176 | |
177 } // namespace WebCore | |
OLD | NEW |