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.
4
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.
11
12#ifndef UI_GFX_GEOMETRY_RECT_H_
13#define UI_GFX_GEOMETRY_RECT_H_
14
15#include <cmath>
16#include <string>
17
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"
23
24#if defined(OS_WIN)
25typedef 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
31
32namespace gfx {
33
34class Insets;
35
36class GFX_EXPORT Rect
37    : public RectBase<Rect, Point, Size, Insets, Vector2d, int> {
38 public:
39  Rect() : RectBase<Rect, Point, Size, Insets, Vector2d, int>(Point()) {}
40
41  Rect(int width, int height)
42      : RectBase<Rect, Point, Size, Insets, Vector2d, int>
43            (Size(width, height)) {}
44
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)) {}
48
49#if defined(OS_WIN)
50  explicit Rect(const RECT& r);
51#elif defined(OS_MACOSX)
52  explicit Rect(const CGRect& r);
53#endif
54
55  explicit Rect(const gfx::Size& size)
56      : RectBase<Rect, Point, Size, Insets, Vector2d, int>(size) {}
57
58  Rect(const gfx::Point& origin, const gfx::Size& size)
59      : RectBase<Rect, Point, Size, Insets, Vector2d, int>(origin, size) {}
60
61  ~Rect() {}
62
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
70
71  operator RectF() const {
72    return RectF(origin().x(), origin().y(), size().width(), size().height());
73  }
74
75  std::string ToString() const;
76};
77
78inline bool operator==(const Rect& lhs, const Rect& rhs) {
79  return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
80}
81
82inline bool operator!=(const Rect& lhs, const Rect& rhs) {
83  return !(lhs == rhs);
84}
85
86GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
87GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
88
89inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
90  return rhs + lhs;
91}
92
93GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
94GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
95GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
96
97// Constructs a rectangle with |p1| and |p2| as opposite corners.
98//
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.
103GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
104
105inline 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);
113}
114
115inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
116  return ScaleToEnclosingRect(rect, scale, scale);
117}
118
119inline 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);
127}
128
129inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
130  return ScaleToEnclosedRect(rect, scale, scale);
131}
132
133#if !defined(COMPILER_MSVC)
134extern template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
135#endif
136
137}  // namespace gfx
138
139#endif  // UI_GFX_GEOMETRY_RECT_H_
140