1 /* 2 * Copyright (C) 2018 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 MINIKIN_MINIKIN_RECT_H 18 #define MINIKIN_MINIKIN_RECT_H 19 20 #include <ostream> 21 22 #include "minikin/Point.h" 23 24 namespace minikin { 25 26 struct MinikinRect { MinikinRectMinikinRect27 MinikinRect() : mLeft(0), mTop(0), mRight(0), mBottom(0) {} MinikinRectMinikinRect28 MinikinRect(float left, float top, float right, float bottom) 29 : mLeft(left), mTop(top), mRight(right), mBottom(bottom) {} 30 isEmptyMinikinRect31 bool isEmpty() const { return mLeft == mRight || mTop == mBottom; } isValidMinikinRect32 bool isValid() const { return !std::isnan(mLeft); } widthMinikinRect33 float width() const { return mRight - mLeft; } 34 setEmptyMinikinRect35 void setEmpty() { mLeft = mRight = mTop = mBottom = 0; } 36 37 // Shift the rectangle with given amount. offsetMinikinRect38 void offset(float dx, float dy) { 39 mLeft += dx; 40 mTop += dy; 41 mRight += dx; 42 mBottom += dy; 43 } 44 45 // Update the rectangle with the union of the given rectangle with shifting. joinMinikinRect46 void join(float l, float t, float r, float b, float dx, float dy) { 47 if (isEmpty()) { 48 mLeft = l + dx; 49 mTop = t + dy; 50 mRight = r + dx; 51 mBottom = b + dy; 52 } else { 53 mLeft = std::min(mLeft, l + dx); 54 mTop = std::min(mTop, t + dy); 55 mRight = std::max(mRight, r + dx); 56 mBottom = std::max(mBottom, b + dy); 57 } 58 } 59 offsetMinikinRect60 void offset(const Point& p) { offset(p.x, p.y); } joinMinikinRect61 void join(const MinikinRect& r) { return join(r.mLeft, r.mTop, r.mRight, r.mBottom, 0, 0); } joinMinikinRect62 void join(const MinikinRect& r, float dx, float dy) { 63 return join(r.mLeft, r.mTop, r.mRight, r.mBottom, dx, dy); 64 } joinMinikinRect65 void join(const MinikinRect& r, const Point& p) { 66 return join(r.mLeft, r.mTop, r.mRight, r.mBottom, p.x, p.y); 67 } 68 makeInvalidMinikinRect69 static MinikinRect makeInvalid() { 70 return MinikinRect( 71 std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN(), 72 std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::quiet_NaN()); 73 } 74 75 float mLeft; 76 float mTop; 77 float mRight; 78 float mBottom; 79 }; 80 81 // For gtest output 82 inline std::ostream& operator<<(std::ostream& os, const MinikinRect& r) { 83 return os << "(" << r.mLeft << ", " << r.mTop << ")-(" << r.mRight << ", " << r.mBottom << ")"; 84 } 85 86 inline bool operator==(const MinikinRect& l, const MinikinRect& r) { 87 return l.mLeft == r.mLeft && l.mTop == r.mTop && l.mRight == r.mRight && l.mBottom == r.mBottom; 88 } 89 90 inline bool operator!=(const MinikinRect& l, const MinikinRect& r) { 91 return !(l == r); 92 } 93 94 } // namespace minikin 95 96 #endif // MINIKIN_MINIKIN_RECT_H 97