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 // Defines a simple integer rectangle class. The containment semantics
6 // are array-like; that is, the coordinate (x, y) is considered to be
7 // contained by the rectangle, but the coordinate (x + width, y) is not.
8 // The class will happily let you create malformed rectangles (that is,
9 // rectangles with negative width and/or height), but there will be assertions
10 // in the operations (such as Contains()) to complain in this case.
12 #ifndef UI_GFX_GEOMETRY_RECT_H_
13 #define UI_GFX_GEOMETRY_RECT_H_
19 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/rect_base.h"
21 #include "ui/gfx/geometry/rect_f.h"
22 #include "ui/gfx/geometry/size.h"
23 #include "ui/gfx/geometry/vector2d.h"
26 typedef struct tagRECT RECT
;
28 #include <CoreGraphics/CoreGraphics.h>
29 #elif defined(OS_MACOSX)
30 #include <ApplicationServices/ApplicationServices.h>
38 : public RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int> {
40 Rect() : RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int>(Point()) {}
42 Rect(int width
, int height
)
43 : RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int>
44 (Size(width
, height
)) {}
46 Rect(int x
, int y
, int width
, int height
)
47 : RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int>
48 (Point(x
, y
), Size(width
, height
)) {}
51 explicit Rect(const RECT
& r
);
52 #elif defined(OS_MACOSX)
53 explicit Rect(const CGRect
& r
);
56 explicit Rect(const gfx::Size
& size
)
57 : RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int>(size
) {}
59 Rect(const gfx::Point
& origin
, const gfx::Size
& size
)
60 : RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int>(origin
, size
) {}
65 // Construct an equivalent Win32 RECT object.
67 #elif defined(OS_MACOSX)
68 // Construct an equivalent CoreGraphics object.
69 CGRect
ToCGRect() const;
72 operator RectF() const {
73 return RectF(origin().x(), origin().y(), size().width(), size().height());
76 std::string
ToString() const;
79 inline bool operator==(const Rect
& lhs
, const Rect
& rhs
) {
80 return lhs
.origin() == rhs
.origin() && lhs
.size() == rhs
.size();
83 inline bool operator!=(const Rect
& lhs
, const Rect
& rhs
) {
87 GFX_EXPORT Rect
operator+(const Rect
& lhs
, const Vector2d
& rhs
);
88 GFX_EXPORT Rect
operator-(const Rect
& lhs
, const Vector2d
& rhs
);
90 inline Rect
operator+(const Vector2d
& lhs
, const Rect
& rhs
) {
94 GFX_EXPORT Rect
IntersectRects(const Rect
& a
, const Rect
& b
);
95 GFX_EXPORT Rect
UnionRects(const Rect
& a
, const Rect
& b
);
96 GFX_EXPORT Rect
SubtractRects(const Rect
& a
, const Rect
& b
);
98 // Constructs a rectangle with |p1| and |p2| as opposite corners.
100 // This could also be thought of as "the smallest rect that contains both
101 // points", except that we consider points on the right/bottom edges of the
102 // rect to be outside the rect. So technically one or both points will not be
103 // contained within the rect, because they will appear on one of these edges.
104 GFX_EXPORT Rect
BoundingRect(const Point
& p1
, const Point
& p2
);
106 inline Rect
ScaleToEnclosingRect(const Rect
& rect
,
109 int x
= std::floor(rect
.x() * x_scale
);
110 int y
= std::floor(rect
.y() * y_scale
);
111 int r
= rect
.width() == 0 ? x
: std::ceil(rect
.right() * x_scale
);
112 int b
= rect
.height() == 0 ? y
: std::ceil(rect
.bottom() * y_scale
);
113 return Rect(x
, y
, r
- x
, b
- y
);
116 inline Rect
ScaleToEnclosingRect(const Rect
& rect
, float scale
) {
117 return ScaleToEnclosingRect(rect
, scale
, scale
);
120 inline Rect
ScaleToEnclosedRect(const Rect
& rect
,
123 int x
= std::ceil(rect
.x() * x_scale
);
124 int y
= std::ceil(rect
.y() * y_scale
);
125 int r
= rect
.width() == 0 ? x
: std::floor(rect
.right() * x_scale
);
126 int b
= rect
.height() == 0 ? y
: std::floor(rect
.bottom() * y_scale
);
127 return Rect(x
, y
, r
- x
, b
- y
);
130 inline Rect
ScaleToEnclosedRect(const Rect
& rect
, float scale
) {
131 return ScaleToEnclosedRect(rect
, scale
, scale
);
134 #if !defined(COMPILER_MSVC)
135 extern template class RectBase
<Rect
, Point
, Size
, Insets
, Vector2d
, int>;
138 // This is declared here for use in gtest-based unit tests but is defined in
139 // the gfx_test_support target. Depend on that to use this in your unit test.
140 // This should not be used in production code - call ToString() instead.
141 void PrintTo(const Rect
& rect
, ::std::ostream
* os
);
145 #endif // UI_GFX_GEOMETRY_RECT_H_