Performance histograms for extension content verification
[chromium-blink-merge.git] / ui / gfx / geometry / rect.h
blob28d37bc31b3013dcdc9ee96a325a4f8c1012b9f5
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_
15 #include <cmath>
16 #include <string>
18 #include "ui/gfx/geometry/point.h"
19 #include "ui/gfx/geometry/rect_base.h"
20 #include "ui/gfx/geometry/rect_f.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "ui/gfx/geometry/vector2d.h"
24 #if defined(OS_WIN)
25 typedef struct tagRECT RECT;
26 #elif defined(OS_IOS)
27 #include <CoreGraphics/CoreGraphics.h>
28 #elif defined(OS_MACOSX)
29 #include <ApplicationServices/ApplicationServices.h>
30 #endif
32 namespace gfx {
34 class Insets;
36 class GFX_EXPORT Rect
37 : public RectBase<Rect, Point, Size, Insets, Vector2d, int> {
38 public:
39 Rect() : RectBase<Rect, Point, Size, Insets, Vector2d, int>(Point()) {}
41 Rect(int width, int height)
42 : RectBase<Rect, Point, Size, Insets, Vector2d, int>
43 (Size(width, height)) {}
45 Rect(int x, int y, int width, int height)
46 : RectBase<Rect, Point, Size, Insets, Vector2d, int>
47 (Point(x, y), Size(width, height)) {}
49 #if defined(OS_WIN)
50 explicit Rect(const RECT& r);
51 #elif defined(OS_MACOSX)
52 explicit Rect(const CGRect& r);
53 #endif
55 explicit Rect(const gfx::Size& size)
56 : RectBase<Rect, Point, Size, Insets, Vector2d, int>(size) {}
58 Rect(const gfx::Point& origin, const gfx::Size& size)
59 : RectBase<Rect, Point, Size, Insets, Vector2d, int>(origin, size) {}
61 ~Rect() {}
63 #if defined(OS_WIN)
64 // Construct an equivalent Win32 RECT object.
65 RECT ToRECT() const;
66 #elif defined(OS_MACOSX)
67 // Construct an equivalent CoreGraphics object.
68 CGRect ToCGRect() const;
69 #endif
71 operator RectF() const {
72 return RectF(origin().x(), origin().y(), size().width(), size().height());
75 std::string ToString() const;
78 inline bool operator==(const Rect& lhs, const Rect& rhs) {
79 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
82 inline bool operator!=(const Rect& lhs, const Rect& rhs) {
83 return !(lhs == rhs);
86 GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
87 GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
89 inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
90 return rhs + lhs;
93 GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
94 GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
95 GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
97 // Constructs a rectangle with |p1| and |p2| as opposite corners.
99 // This could also be thought of as "the smallest rect that contains both
100 // points", except that we consider points on the right/bottom edges of the
101 // rect to be outside the rect. So technically one or both points will not be
102 // contained within the rect, because they will appear on one of these edges.
103 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
105 inline Rect ScaleToEnclosingRect(const Rect& rect,
106 float x_scale,
107 float y_scale) {
108 int x = std::floor(rect.x() * x_scale);
109 int y = std::floor(rect.y() * y_scale);
110 int r = rect.width() == 0 ? x : std::ceil(rect.right() * x_scale);
111 int b = rect.height() == 0 ? y : std::ceil(rect.bottom() * y_scale);
112 return Rect(x, y, r - x, b - y);
115 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
116 return ScaleToEnclosingRect(rect, scale, scale);
119 inline Rect ScaleToEnclosedRect(const Rect& rect,
120 float x_scale,
121 float y_scale) {
122 int x = std::ceil(rect.x() * x_scale);
123 int y = std::ceil(rect.y() * y_scale);
124 int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale);
125 int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale);
126 return Rect(x, y, r - x, b - y);
129 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
130 return ScaleToEnclosedRect(rect, scale, scale);
133 #if !defined(COMPILER_MSVC)
134 extern template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
135 #endif
137 } // namespace gfx
139 #endif // UI_GFX_GEOMETRY_RECT_H_