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 namespace minikin {
23 
24 struct MinikinRect {
MinikinRectMinikinRect25     MinikinRect() : mLeft(0), mTop(0), mRight(0), mBottom(0) {}
MinikinRectMinikinRect26     MinikinRect(float left, float top, float right, float bottom)
27             : mLeft(left), mTop(top), mRight(right), mBottom(bottom) {}
28     bool operator==(const MinikinRect& o) const {
29         return mLeft == o.mLeft && mTop == o.mTop && mRight == o.mRight && mBottom == o.mBottom;
30     }
31     float mLeft;
32     float mTop;
33     float mRight;
34     float mBottom;
35 
isEmptyMinikinRect36     bool isEmpty() const { return mLeft == mRight || mTop == mBottom; }
setMinikinRect37     void set(const MinikinRect& r) {
38         mLeft = r.mLeft;
39         mTop = r.mTop;
40         mRight = r.mRight;
41         mBottom = r.mBottom;
42     }
offsetMinikinRect43     void offset(float dx, float dy) {
44         mLeft += dx;
45         mTop += dy;
46         mRight += dx;
47         mBottom += dy;
48     }
setEmptyMinikinRect49     void setEmpty() { mLeft = mTop = mRight = mBottom = 0.0; }
joinMinikinRect50     void join(const MinikinRect& r) {
51         if (isEmpty()) {
52             set(r);
53         } else if (!r.isEmpty()) {
54             mLeft = std::min(mLeft, r.mLeft);
55             mTop = std::min(mTop, r.mTop);
56             mRight = std::max(mRight, r.mRight);
57             mBottom = std::max(mBottom, r.mBottom);
58         }
59     }
60 };
61 
62 // For gtest output
63 inline std::ostream& operator<<(std::ostream& os, const MinikinRect& r) {
64     return os << "(" << r.mLeft << ", " << r.mTop << ")-(" << r.mRight << ", " << r.mBottom << ")";
65 }
66 
67 }  // namespace minikin
68 
69 #endif  // MINIKIN_MINIKIN_RECT_H
70