Rect.h revision abf347cca84ffda2452fb86acd58d07051b328a6
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 <utils/Flattenable.h> 21#include <utils/Log.h> 22#include <utils/TypeHelpers.h> 23#include <log/log.h> 24 25#include <ui/FloatRect.h> 26#include <ui/Point.h> 27 28#include <android/rect.h> 29 30namespace android { 31 32class Rect : public ARect, public LightFlattenablePod<Rect> 33{ 34public: 35 typedef ARect::value_type value_type; 36 37 static const Rect INVALID_RECT; 38 static const Rect EMPTY_RECT; 39 40 // we don't provide copy-ctor and operator= on purpose 41 // because we want the compiler generated versions 42 43 inline Rect() : Rect(INVALID_RECT) {} 44 45 template <typename T> 46 inline Rect(T w, T h) { 47 if (w > INT32_MAX) { 48 w = INT32_MAX; 49 } 50 if (h > INT32_MAX) { 51 h = INT32_MAX; 52 } 53 left = top = 0; 54 right = static_cast<int32_t>(w); 55 bottom = static_cast<int32_t>(h); 56 } 57 58 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) { 59 left = l; 60 top = t; 61 right = r; 62 bottom = b; 63 } 64 65 inline Rect(const Point& lt, const Point& rb) { 66 left = lt.x; 67 top = lt.y; 68 right = rb.x; 69 bottom = rb.y; 70 } 71 72 void makeInvalid(); 73 74 inline void clear() { 75 left = top = right = bottom = 0; 76 } 77 78 // a valid rectangle has a non negative width and height 79 inline bool isValid() const { 80 return (getWidth() >= 0) && (getHeight() >= 0); 81 } 82 83 // an empty rect has a zero width or height, or is invalid 84 inline bool isEmpty() const { 85 return (getWidth() <= 0) || (getHeight() <= 0); 86 } 87 88 // rectangle's width 89 __attribute__((no_sanitize("signed-integer-overflow"))) 90 inline int32_t getWidth() const { 91 return right - left; 92 } 93 94 // rectangle's height 95 __attribute__((no_sanitize("signed-integer-overflow"))) 96 inline int32_t getHeight() const { 97 return bottom - top; 98 } 99 100 __attribute__((no_sanitize("signed-integer-overflow"))) 101 inline Rect getBounds() const { 102 return Rect(right - left, bottom - top); 103 } 104 105 void setLeftTop(const Point& lt) { 106 left = lt.x; 107 top = lt.y; 108 } 109 110 void setRightBottom(const Point& rb) { 111 right = rb.x; 112 bottom = rb.y; 113 } 114 115 // the following 4 functions return the 4 corners of the rect as Point 116 Point leftTop() const { 117 return Point(left, top); 118 } 119 Point rightBottom() const { 120 return Point(right, bottom); 121 } 122 Point rightTop() const { 123 return Point(right, top); 124 } 125 Point leftBottom() const { 126 return Point(left, bottom); 127 } 128 129 // comparisons 130 inline bool operator == (const Rect& rhs) const { 131 return (left == rhs.left) && (top == rhs.top) && 132 (right == rhs.right) && (bottom == rhs.bottom); 133 } 134 135 inline bool operator != (const Rect& rhs) const { 136 return !operator == (rhs); 137 } 138 139 // operator < defines an order which allows to use rectangles in sorted 140 // vectors. 141 bool operator < (const Rect& rhs) const; 142 143 const Rect operator + (const Point& rhs) const; 144 const Rect operator - (const Point& rhs) const; 145 146 Rect& operator += (const Point& rhs) { 147 return offsetBy(rhs.x, rhs.y); 148 } 149 Rect& operator -= (const Point& rhs) { 150 return offsetBy(-rhs.x, -rhs.y); 151 } 152 153 Rect& offsetToOrigin() { 154 right -= left; 155 bottom -= top; 156 left = top = 0; 157 return *this; 158 } 159 Rect& offsetTo(const Point& p) { 160 return offsetTo(p.x, p.y); 161 } 162 Rect& offsetBy(const Point& dp) { 163 return offsetBy(dp.x, dp.y); 164 } 165 166 Rect& offsetTo(int32_t x, int32_t y); 167 Rect& offsetBy(int32_t x, int32_t y); 168 169 bool intersect(const Rect& with, Rect* result) const; 170 171 // Create a new Rect by transforming this one using a graphics HAL 172 // transform. This rectangle is defined in a coordinate space starting at 173 // the origin and extending to (width, height). If the transform includes 174 // a ROT90 then the output rectangle is defined in a space extending to 175 // (height, width). Otherwise the output rectangle is in the same space as 176 // the input. 177 Rect transform(uint32_t xform, int32_t width, int32_t height) const; 178 179 // this calculates (Region(*this) - exclude).bounds() efficiently 180 Rect reduce(const Rect& exclude) const; 181 182 // for backward compatibility 183 inline int32_t width() const { return getWidth(); } 184 inline int32_t height() const { return getHeight(); } 185 inline void set(const Rect& rhs) { operator = (rhs); } 186 187 FloatRect toFloatRect() const { 188 return {static_cast<float>(left), static_cast<float>(top), 189 static_cast<float>(right), static_cast<float>(bottom)}; 190 } 191}; 192 193ANDROID_BASIC_TYPES_TRAITS(Rect) 194 195}; // namespace android 196 197#endif // ANDROID_UI_RECT 198