• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SIZE_H
17 #define SIZE_H
18 
19 #include <iomanip>
20 #include <limits>
21 #include <sstream>
22 #include <string>
23 #include "utils.h"
24 
25 namespace OHOS::uitest {
26 class Size {
27 public:
28     static constexpr double INFINITE_SIZE = std::numeric_limits<double>::max();
29     Size() = default;
30     ~Size() = default;
Size(double width,double height)31     Size(double width, double height) : width_(width), height_(height) {}
32 
IsValueInfinite(double inputValue)33     inline static bool IsValueInfinite(double inputValue)
34     {
35         return NearEqual(inputValue, INFINITE_SIZE);
36     }
37 
ErrorSize()38     static Size ErrorSize()
39     {
40         return Size((std::numeric_limits<double>::max)(), (std::numeric_limits<double>::max)());
41     }
42 
IsErrorSize()43     bool IsErrorSize() const
44     {
45         return operator==(ErrorSize());
46     }
47 
Width()48     double Width() const
49     {
50         return width_;
51     }
52 
Height()53     double Height() const
54     {
55         return height_;
56     }
57 
SetWidth(double width)58     void SetWidth(double width)
59     {
60         width_ = width;
61     }
62 
SetHeight(double height)63     void SetHeight(double height)
64     {
65         height_ = height;
66     }
67 
SetSize(const Size & size)68     void SetSize(const Size& size)
69     {
70         width_ = size.Width();
71         height_ = size.Height();
72     }
73 
IsWidthInfinite()74     bool IsWidthInfinite() const
75     {
76         return NearEqual(width_, INFINITE_SIZE);
77     }
78 
IsHeightInfinite()79     bool IsHeightInfinite() const
80     {
81         return NearEqual(height_, INFINITE_SIZE);
82     }
83 
IsInfinite()84     bool IsInfinite() const
85     {
86         return NearEqual(width_, INFINITE_SIZE) || NearEqual(height_, INFINITE_SIZE);
87     }
88 
IsEmpty()89     bool IsEmpty() const
90     {
91         return NearEqual(width_, ZERO) || NearEqual(height_, ZERO);
92     }
93 
AddHeight(double height)94     Size& AddHeight(double height)
95     {
96         height_ += height;
97         return *this;
98     }
99 
AddWidth(double value)100     Size& AddWidth(double value)
101     {
102         width_ += value;
103         return *this;
104     }
105 
MinusHeight(double height)106     Size& MinusHeight(double height)
107     {
108         height_ -= height;
109         return *this;
110     }
111 
MinusWidth(double width)112     Size& MinusWidth(double width)
113     {
114         width_ -= width;
115         return *this;
116     }
117 
IsValid()118     bool IsValid() const
119     {
120         return width_ > ZERO && height_ > ZERO;
121     }
122 
123     Size operator+(const Size& size) const
124     {
125         return Size(width_ + size.Width(), height_ + size.Height());
126     }
127 
128     Size operator-(const Size& size) const
129     {
130         return Size(width_ - size.Width(), height_ - size.Height());
131     }
132 
133     Size operator*(double value) const
134     {
135         return Size(width_ * value, height_ * value);
136     }
137 
138     Size operator/(double value) const
139     {
140         if (NearZero(value)) {
141             return ErrorSize();
142         }
143         return Size(width_ / value, height_ / value);
144     }
145 
146     bool operator==(const Size& size) const
147     {
148         return NearEqual(width_, size.width_) && NearEqual(height_, size.height_);
149     }
150 
151     bool operator!=(const Size& size) const
152     {
153         return !operator==(size);
154     }
155 
156     Size operator+=(const Size& size)
157     {
158         width_ += size.Width();
159         height_ += size.Height();
160         return *this;
161     }
162 
163     Size operator-=(const Size& size)
164     {
165         width_ -= size.Width();
166         height_ -= size.Height();
167         return *this;
168     }
169 
ApplyScale(double scale)170     void ApplyScale(double scale)
171     {
172         width_ *= scale;
173         height_ *= scale;
174     }
175 
176     /*
177      * Please make sure that two sizes are both valid.
178      * You can use IsValid() to see if a size is valid.
179      */
180     bool operator>(const Size& size) const
181     {
182         if (IsValid() && size.IsValid()) {
183             return GreatOrEqual(width_, size.width_) && GreatOrEqual(height_, size.height_);
184         } else {
185             return false;
186         }
187     }
188 
189     /*
190      * Please make sure that two sizes are both valid.
191      * You can use IsValid() to see if a size is valid.
192      */
193     bool operator<(const Size& size) const
194     {
195         if (IsValid() && size.IsValid()) {
196             return LessOrEqual(width_, size.width_) && LessOrEqual(height_, size.height_);
197         } else {
198             return false;
199         }
200     }
201 
ToString()202     std::string ToString() const
203     {
204         std::stringstream ss;
205         ss << "[" << std::fixed << std::setprecision(TWO);
206         if (NearEqual(width_, INFINITE_SIZE)) {
207             ss << "INFINITE";
208         } else {
209             ss << width_;
210         }
211         ss << " x ";
212         if (NearEqual(height_, INFINITE_SIZE)) {
213             ss << "INFINITE";
214         } else {
215             ss << height_;
216         }
217         ss << "]";
218         std::string output = ss.str();
219         return output;
220     }
221 
222 private:
223     double width_ = 0.0;
224     double height_ = 0.0;
225 };
226 } // namespace OHOS::uitest
227 
228 #endif // SIZE_H
229