Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / ui / gfx / rect_base_impl.h
blobe44bc00bace6956bcd9a9a97b4d3ea43563d8406
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/rect_base.h"
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
10 // This file provides the implementation for RectBaese template and
11 // used to instantiate the base class for Rect and RectF classes.
12 #if !defined(UI_IMPLEMENTATION)
13 #error "This file is intended for UI implementation only"
14 #endif
16 namespace {
18 template<typename Type>
19 void AdjustAlongAxis(Type dst_origin, Type dst_size, Type* origin, Type* size) {
20 *size = std::min(dst_size, *size);
21 if (*origin < dst_origin)
22 *origin = dst_origin;
23 else
24 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
27 } // namespace
29 namespace gfx {
31 template<typename Class,
32 typename PointClass,
33 typename SizeClass,
34 typename InsetsClass,
35 typename VectorClass,
36 typename Type>
37 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
38 SetRect(Type x, Type y, Type width, Type height) {
39 origin_.SetPoint(x, y);
40 set_width(width);
41 set_height(height);
44 template<typename Class,
45 typename PointClass,
46 typename SizeClass,
47 typename InsetsClass,
48 typename VectorClass,
49 typename Type>
50 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
51 Inset(const InsetsClass& insets) {
52 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
55 template<typename Class,
56 typename PointClass,
57 typename SizeClass,
58 typename InsetsClass,
59 typename VectorClass,
60 typename Type>
61 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
62 Inset(Type left, Type top, Type right, Type bottom) {
63 origin_ += VectorClass(left, top);
64 set_width(std::max(width() - left - right, static_cast<Type>(0)));
65 set_height(std::max(height() - top - bottom, static_cast<Type>(0)));
68 template<typename Class,
69 typename PointClass,
70 typename SizeClass,
71 typename InsetsClass,
72 typename VectorClass,
73 typename Type>
74 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
75 Offset(Type horizontal, Type vertical) {
76 origin_ += VectorClass(horizontal, vertical);
79 template<typename Class,
80 typename PointClass,
81 typename SizeClass,
82 typename InsetsClass,
83 typename VectorClass,
84 typename Type>
85 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
86 operator+=(const VectorClass& offset) {
87 origin_ += offset;
90 template<typename Class,
91 typename PointClass,
92 typename SizeClass,
93 typename InsetsClass,
94 typename VectorClass,
95 typename Type>
96 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
97 operator-=(const VectorClass& offset) {
98 origin_ -= offset;
101 template<typename Class,
102 typename PointClass,
103 typename SizeClass,
104 typename InsetsClass,
105 typename VectorClass,
106 typename Type>
107 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
108 operator<(const Class& other) const {
109 if (origin_ == other.origin_) {
110 if (width() == other.width()) {
111 return height() < other.height();
112 } else {
113 return width() < other.width();
115 } else {
116 return origin_ < other.origin_;
120 template<typename Class,
121 typename PointClass,
122 typename SizeClass,
123 typename InsetsClass,
124 typename VectorClass,
125 typename Type>
126 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
127 Contains(Type point_x, Type point_y) const {
128 return (point_x >= x()) && (point_x < right()) &&
129 (point_y >= y()) && (point_y < bottom());
132 template<typename Class,
133 typename PointClass,
134 typename SizeClass,
135 typename InsetsClass,
136 typename VectorClass,
137 typename Type>
138 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
139 Contains(const Class& rect) const {
140 return (rect.x() >= x() && rect.right() <= right() &&
141 rect.y() >= y() && rect.bottom() <= bottom());
144 template<typename Class,
145 typename PointClass,
146 typename SizeClass,
147 typename InsetsClass,
148 typename VectorClass,
149 typename Type>
150 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
151 Intersects(const Class& rect) const {
152 return !(IsEmpty() || rect.IsEmpty() ||
153 rect.x() >= right() || rect.right() <= x() ||
154 rect.y() >= bottom() || rect.bottom() <= y());
157 template<typename Class,
158 typename PointClass,
159 typename SizeClass,
160 typename InsetsClass,
161 typename VectorClass,
162 typename Type>
163 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
164 Intersect(const Class& rect) {
165 if (IsEmpty() || rect.IsEmpty()) {
166 SetRect(0, 0, 0, 0);
167 return;
170 Type rx = std::max(x(), rect.x());
171 Type ry = std::max(y(), rect.y());
172 Type rr = std::min(right(), rect.right());
173 Type rb = std::min(bottom(), rect.bottom());
175 if (rx >= rr || ry >= rb)
176 rx = ry = rr = rb = 0; // non-intersecting
178 SetRect(rx, ry, rr - rx, rb - ry);
181 template<typename Class,
182 typename PointClass,
183 typename SizeClass,
184 typename InsetsClass,
185 typename VectorClass,
186 typename Type>
187 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
188 Union(const Class& rect) {
189 if (IsEmpty()) {
190 *this = rect;
191 return;
193 if (rect.IsEmpty())
194 return;
196 Type rx = std::min(x(), rect.x());
197 Type ry = std::min(y(), rect.y());
198 Type rr = std::max(right(), rect.right());
199 Type rb = std::max(bottom(), rect.bottom());
201 SetRect(rx, ry, rr - rx, rb - ry);
204 template<typename Class,
205 typename PointClass,
206 typename SizeClass,
207 typename InsetsClass,
208 typename VectorClass,
209 typename Type>
210 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
211 Subtract(const Class& rect) {
212 if (!Intersects(rect))
213 return;
214 if (rect.Contains(*static_cast<const Class*>(this))) {
215 SetRect(0, 0, 0, 0);
216 return;
219 Type rx = x();
220 Type ry = y();
221 Type rr = right();
222 Type rb = bottom();
224 if (rect.y() <= y() && rect.bottom() >= bottom()) {
225 // complete intersection in the y-direction
226 if (rect.x() <= x()) {
227 rx = rect.right();
228 } else if (rect.right() >= right()) {
229 rr = rect.x();
231 } else if (rect.x() <= x() && rect.right() >= right()) {
232 // complete intersection in the x-direction
233 if (rect.y() <= y()) {
234 ry = rect.bottom();
235 } else if (rect.bottom() >= bottom()) {
236 rb = rect.y();
239 SetRect(rx, ry, rr - rx, rb - ry);
242 template<typename Class,
243 typename PointClass,
244 typename SizeClass,
245 typename InsetsClass,
246 typename VectorClass,
247 typename Type>
248 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
249 AdjustToFit(const Class& rect) {
250 Type new_x = x();
251 Type new_y = y();
252 Type new_width = width();
253 Type new_height = height();
254 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
255 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
256 SetRect(new_x, new_y, new_width, new_height);
259 template<typename Class,
260 typename PointClass,
261 typename SizeClass,
262 typename InsetsClass,
263 typename VectorClass,
264 typename Type>
265 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass,
266 Type>::CenterPoint() const {
267 return PointClass(x() + width() / 2, y() + height() / 2);
270 template<typename Class,
271 typename PointClass,
272 typename SizeClass,
273 typename InsetsClass,
274 typename VectorClass,
275 typename Type>
276 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
277 ClampToCenteredSize(const SizeClass& size) {
278 Type new_width = std::min(width(), size.width());
279 Type new_height = std::min(height(), size.height());
280 Type new_x = x() + (width() - new_width) / 2;
281 Type new_y = y() + (height() - new_height) / 2;
282 SetRect(new_x, new_y, new_width, new_height);
285 template<typename Class,
286 typename PointClass,
287 typename SizeClass,
288 typename InsetsClass,
289 typename VectorClass,
290 typename Type>
291 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
292 SplitVertically(Class* left_half, Class* right_half) const {
293 DCHECK(left_half);
294 DCHECK(right_half);
296 left_half->SetRect(x(), y(), width() / 2, height());
297 right_half->SetRect(left_half->right(),
298 y(),
299 width() - left_half->width(),
300 height());
303 template<typename Class,
304 typename PointClass,
305 typename SizeClass,
306 typename InsetsClass,
307 typename VectorClass,
308 typename Type>
309 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
310 SharesEdgeWith(const Class& rect) const {
311 return (y() == rect.y() && height() == rect.height() &&
312 (x() == rect.right() || right() == rect.x())) ||
313 (x() == rect.x() && width() == rect.width() &&
314 (y() == rect.bottom() || bottom() == rect.y()));
317 } // namespace gfx