1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_UI_RECT
18 #define ANDROID_UI_RECT
19 
20 #include <ostream>
21 
22 #include <log/log.h>
23 #include <utils/Flattenable.h>
24 #include <utils/Log.h>
25 #include <utils/TypeHelpers.h>
26 
27 #include <math/HashCombine.h>
28 #include <ui/FloatRect.h>
29 #include <ui/Point.h>
30 #include <ui/Size.h>
31 
32 #include <android/rect.h>
33 
34 namespace android {
35 
36 class Rect : public ARect, public LightFlattenablePod<Rect>
37 {
38 public:
39     typedef ARect::value_type value_type;
40 
41     static const Rect INVALID_RECT;
42     static const Rect EMPTY_RECT;
43 
44     // we don't provide copy-ctor and operator= on purpose
45     // because we want the compiler generated versions
46 
Rect()47     inline Rect() : Rect(INVALID_RECT) {}
48 
49     template <typename T>
Rect(T w,T h)50     inline Rect(T w, T h) {
51         if (w > INT32_MAX) {
52             w = INT32_MAX;
53         }
54         if (h > INT32_MAX) {
55             h = INT32_MAX;
56         }
57         left = top = 0;
58         right = static_cast<int32_t>(w);
59         bottom = static_cast<int32_t>(h);
60     }
61 
Rect(int32_t l,int32_t t,int32_t r,int32_t b)62     inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
63         left = l;
64         top = t;
65         right = r;
66         bottom = b;
67     }
68 
Rect(const Point & lt,const Point & rb)69     inline Rect(const Point& lt, const Point& rb) {
70         left = lt.x;
71         top = lt.y;
72         right = rb.x;
73         bottom = rb.y;
74     }
75 
Rect(const FloatRect & floatRect)76     inline explicit Rect(const FloatRect& floatRect) {
77         // Ideally we would use std::round, but we don't want to add an STL
78         // dependency here, so we use an approximation
79         left = static_cast<int32_t>(floatRect.left + 0.5f);
80         top = static_cast<int32_t>(floatRect.top + 0.5f);
81         right = static_cast<int32_t>(floatRect.right + 0.5f);
82         bottom = static_cast<int32_t>(floatRect.bottom + 0.5f);
83     }
84 
Rect(const ui::Size & size)85     inline explicit Rect(const ui::Size& size) {
86         left = 0;
87         top = 0;
88         right = size.width;
89         bottom = size.height;
90     }
91 
92     void makeInvalid();
93 
clear()94     inline void clear() {
95         left = top = right = bottom = 0;
96     }
97 
98     // a valid rectangle has a non negative width and height
isValid()99     inline bool isValid() const {
100         return (getWidth() >= 0) && (getHeight() >= 0);
101     }
102 
103     // an empty rect has a zero width or height, or is invalid
isEmpty()104     inline bool isEmpty() const {
105         return (getWidth() <= 0) || (getHeight() <= 0);
106     }
107 
108     // rectangle's width
109     __attribute__((no_sanitize("signed-integer-overflow")))
getWidth()110     inline int32_t getWidth() const {
111         return right - left;
112     }
113 
114     // rectangle's height
115     __attribute__((no_sanitize("signed-integer-overflow")))
getHeight()116     inline int32_t getHeight() const {
117         return bottom - top;
118     }
119 
getSize()120     ui::Size getSize() const { return ui::Size(getWidth(), getHeight()); }
121 
122     __attribute__((no_sanitize("signed-integer-overflow")))
getBounds()123     inline Rect getBounds() const {
124         return Rect(right - left, bottom - top);
125     }
126 
setLeftTop(const Point & lt)127     void setLeftTop(const Point& lt) {
128         left = lt.x;
129         top = lt.y;
130     }
131 
setRightBottom(const Point & rb)132     void setRightBottom(const Point& rb) {
133         right = rb.x;
134         bottom = rb.y;
135     }
136 
137     // the following 4 functions return the 4 corners of the rect as Point
leftTop()138     Point leftTop() const {
139         return Point(left, top);
140     }
rightBottom()141     Point rightBottom() const {
142         return Point(right, bottom);
143     }
rightTop()144     Point rightTop() const {
145         return Point(right, top);
146     }
leftBottom()147     Point leftBottom() const {
148         return Point(left, bottom);
149     }
150 
151     // comparisons
152     inline bool operator == (const Rect& rhs) const {
153         return (left == rhs.left) && (top == rhs.top) &&
154                (right == rhs.right) && (bottom == rhs.bottom);
155     }
156 
157     inline bool operator != (const Rect& rhs) const {
158         return !operator == (rhs);
159     }
160 
161     // operator < defines an order which allows to use rectangles in sorted
162     // vectors.
163     bool operator < (const Rect& rhs) const;
164 
165     const Rect operator + (const Point& rhs) const;
166     const Rect operator - (const Point& rhs) const;
167 
168     Rect& operator += (const Point& rhs) {
169         return offsetBy(rhs.x, rhs.y);
170     }
171     Rect& operator -= (const Point& rhs) {
172         return offsetBy(-rhs.x, -rhs.y);
173     }
174 
offsetToOrigin()175     Rect& offsetToOrigin() {
176         right -= left;
177         bottom -= top;
178         left = top = 0;
179         return *this;
180     }
offsetTo(const Point & p)181     Rect& offsetTo(const Point& p) {
182         return offsetTo(p.x, p.y);
183     }
offsetBy(const Point & dp)184     Rect& offsetBy(const Point& dp) {
185         return offsetBy(dp.x, dp.y);
186     }
187 
188     Rect& offsetTo(int32_t x, int32_t y);
189     Rect& offsetBy(int32_t x, int32_t y);
190 
191     /**
192      * Insets the rectangle on all sides specified by the insets.
193      */
194     Rect& inset(int32_t _left, int32_t _top, int32_t _right, int32_t _bottom);
195 
196     bool intersect(const Rect& with, Rect* result) const;
197 
198     // Create a new Rect by transforming this one using a graphics HAL
199     // transform.  This rectangle is defined in a coordinate space starting at
200     // the origin and extending to (width, height).  If the transform includes
201     // a ROT90 then the output rectangle is defined in a space extending to
202     // (height, width).  Otherwise the output rectangle is in the same space as
203     // the input.
204     Rect transform(uint32_t xform, int32_t width, int32_t height) const;
205 
scale(float scaleX,float scaleY)206     Rect scale(float scaleX, float scaleY) const {
207         return Rect(FloatRect(left * scaleX, top * scaleY, right * scaleX, bottom * scaleY));
208     }
209 
scaleSelf(float scaleX,float scaleY)210     Rect& scaleSelf(float scaleX, float scaleY) {
211         set(scale(scaleX, scaleY));
212         return *this;
213     }
214 
215     // this calculates (Region(*this) - exclude).bounds() efficiently
216     Rect reduce(const Rect& exclude) const;
217 
218     // for backward compatibility
width()219     inline int32_t width() const { return getWidth(); }
height()220     inline int32_t height() const { return getHeight(); }
set(const Rect & rhs)221     inline void set(const Rect& rhs) { operator = (rhs); }
222 
toFloatRect()223     FloatRect toFloatRect() const {
224         return {static_cast<float>(left), static_cast<float>(top),
225                 static_cast<float>(right), static_cast<float>(bottom)};
226     }
227 };
228 
229 std::string to_string(const android::Rect& rect);
230 
231 // Defining PrintTo helps with Google Tests.
232 void PrintTo(const Rect& rect, ::std::ostream* os);
233 
234 ANDROID_BASIC_TYPES_TRAITS(Rect)
235 
236 }; // namespace android
237 
238 namespace std {
239 template <>
240 struct hash<android::Rect> {
241     size_t operator()(const android::Rect& rect) const {
242         return android::hashCombine(rect.left, rect.top, rect.right, rect.bottom);
243     }
244 };
245 } // namespace std
246 
247 #endif // ANDROID_UI_RECT
248