• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool operator!=(const MinikinRect& o) const { return !(*this == o); }
32     float mLeft;
33     float mTop;
34     float mRight;
35     float mBottom;
36 
isEmptyMinikinRect37     bool isEmpty() const { return mLeft == mRight || mTop == mBottom; }
setMinikinRect38     void set(const MinikinRect& r) {
39         mLeft = r.mLeft;
40         mTop = r.mTop;
41         mRight = r.mRight;
42         mBottom = r.mBottom;
43     }
offsetMinikinRect44     void offset(float dx, float dy) {
45         mLeft += dx;
46         mTop += dy;
47         mRight += dx;
48         mBottom += dy;
49     }
setEmptyMinikinRect50     void setEmpty() { mLeft = mTop = mRight = mBottom = 0.0; }
joinMinikinRect51     void join(const MinikinRect& r) {
52         if (isEmpty()) {
53             set(r);
54         } else if (!r.isEmpty()) {
55             mLeft = std::min(mLeft, r.mLeft);
56             mTop = std::min(mTop, r.mTop);
57             mRight = std::max(mRight, r.mRight);
58             mBottom = std::max(mBottom, r.mBottom);
59         }
60     }
61 };
62 
63 // For gtest output
64 inline std::ostream& operator<<(std::ostream& os, const MinikinRect& r) {
65     return os << "(" << r.mLeft << ", " << r.mTop << ")-(" << r.mRight << ", " << r.mBottom << ")";
66 }
67 
68 }  // namespace minikin
69 
70 #endif  // MINIKIN_MINIKIN_RECT_H
71