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 <iosfwd>
17#include <string>
18
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"
24
25#if defined(OS_WIN)
26typedef struct tagRECT RECT;
27#elif defined(OS_IOS)
28#include <CoreGraphics/CoreGraphics.h>
29#elif defined(OS_MACOSX)
30#include <ApplicationServices/ApplicationServices.h>
31#endif
32
33namespace gfx {
34
35class Insets;
36
37class GFX_EXPORT Rect
38    : public RectBase<Rect, Point, Size, Insets, Vector2d, int> {
39 public:
40  Rect() : RectBase<Rect, Point, Size, Insets, Vector2d, int>(Point()) {}
41
42  Rect(int width, int height)
43      : RectBase<Rect, Point, Size, Insets, Vector2d, int>
44            (Size(width, height)) {}
45
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)) {}
49
50#if defined(OS_WIN)
51  explicit Rect(const RECT& r);
52#elif defined(OS_MACOSX)
53  explicit Rect(const CGRect& r);
54#endif
55
56  explicit Rect(const gfx::Size& size)
57      : RectBase<Rect, Point, Size, Insets, Vector2d, int>(size) {}
58
59  Rect(const gfx::Point& origin, const gfx::Size& size)
60      : RectBase<Rect, Point, Size, Insets, Vector2d, int>(origin, size) {}
61
62  ~Rect() {}
63
64#if defined(OS_WIN)
65  // Construct an equivalent Win32 RECT object.
66  RECT ToRECT() const;
67#elif defined(OS_MACOSX)
68  // Construct an equivalent CoreGraphics object.
69  CGRect ToCGRect() const;
70#endif
71
72  operator RectF() const {
73    return RectF(origin().x(), origin().y(), size().width(), size().height());
74  }
75
76  std::string ToString() const;
77};
78
79inline bool operator==(const Rect& lhs, const Rect& rhs) {
80  return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
81}
82
83inline bool operator!=(const Rect& lhs, const Rect& rhs) {
84  return !(lhs == rhs);
85}
86
87GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
88GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
89
90inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
91  return rhs + lhs;
92}
93
94GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
95GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
96GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
97
98// Constructs a rectangle with |p1| and |p2| as opposite corners.
99//
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.
104GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
105
106inline Rect ScaleToEnclosingRect(const Rect& rect,
107                                 float x_scale,
108                                 float y_scale) {
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);
114}
115
116inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
117  return ScaleToEnclosingRect(rect, scale, scale);
118}
119
120inline Rect ScaleToEnclosedRect(const Rect& rect,
121                                float x_scale,
122                                float y_scale) {
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);
128}
129
130inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
131  return ScaleToEnclosedRect(rect, scale, scale);
132}
133
134#if !defined(COMPILER_MSVC) && !defined(__native_client__)
135extern template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
136#endif
137
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.
141void PrintTo(const Rect& rect, ::std::ostream* os);
142
143}  // namespace gfx
144
145#endif  // UI_GFX_GEOMETRY_RECT_H_
146