1 /* 2 * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_SIZE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_SIZE_H 18 19 #include <iomanip> 20 #include <limits> 21 #include <sstream> 22 #include <string> 23 24 #include "base/utils/utils.h" 25 26 namespace OHOS::Ace { 27 28 class Size { 29 public: 30 static constexpr double INFINITE_SIZE = std::numeric_limits<double>::max(); 31 Size() = default; 32 ~Size() = default; Size(double width,double height)33 Size(double width, double height) : width_(width), height_(height) {} 34 Width()35 double Width() const 36 { 37 return width_; 38 } 39 Height()40 double Height() const 41 { 42 return height_; 43 } 44 SetWidth(double width)45 void SetWidth(double width) 46 { 47 width_ = width; 48 } 49 SetHeight(double height)50 void SetHeight(double height) 51 { 52 height_ = height; 53 } 54 SetSize(const Size & size)55 void SetSize(const Size& size) 56 { 57 width_ = size.Width(); 58 height_ = size.Height(); 59 } 60 IsWidthInfinite()61 bool IsWidthInfinite() const 62 { 63 return NearEqual(width_, INFINITE_SIZE); 64 } 65 IsHeightInfinite()66 bool IsHeightInfinite() const 67 { 68 return NearEqual(height_, INFINITE_SIZE); 69 } 70 IsInfinite()71 bool IsInfinite() const 72 { 73 return NearEqual(width_, INFINITE_SIZE) || NearEqual(height_, INFINITE_SIZE); 74 } 75 IsEmpty()76 bool IsEmpty() const 77 { 78 return NearEqual(width_, 0.0) || NearEqual(height_, 0.0); 79 } 80 AddHeight(double height)81 Size& AddHeight(double height) 82 { 83 height_ += height; 84 return *this; 85 } 86 AddWidth(double value)87 Size& AddWidth(double value) 88 { 89 width_ += value; 90 return *this; 91 } 92 MinusHeight(double height)93 Size& MinusHeight(double height) 94 { 95 height_ -= height; 96 return *this; 97 } 98 MinusWidth(double width)99 Size& MinusWidth(double width) 100 { 101 width_ -= width; 102 return *this; 103 } 104 IsValid()105 bool IsValid() const 106 { 107 return width_ > 0.0 && height_ > 0.0; 108 } 109 110 Size operator*(double value) const 111 { 112 return Size(width_ * value, height_ * value); 113 } 114 115 bool operator==(const Size& size) const 116 { 117 return NearEqual(width_, size.width_) && NearEqual(height_, size.height_); 118 } 119 120 bool operator!=(const Size& size) const 121 { 122 return !operator==(size); 123 } 124 125 Size operator+(const Size& size) const 126 { 127 return Size(width_ + size.Width(), height_ + size.Height()); 128 } 129 130 Size operator+=(const Size& size) 131 { 132 width_ += size.Width(); 133 height_ += size.Height(); 134 return *this; 135 } 136 137 Size operator-(const Size& size) const 138 { 139 return Size(width_ - size.Width(), height_ - size.Height()); 140 } 141 142 Size operator-=(const Size& size) 143 { 144 width_ -= size.Width(); 145 height_ -= size.Height(); 146 return *this; 147 } 148 ApplyScale(double scale)149 void ApplyScale(double scale) 150 { 151 width_ *= scale; 152 height_ *= scale; 153 } 154 155 /* 156 * Please make sure that two sizes are both valid. 157 * You can use IsValid() to see if a size is valid. 158 */ 159 bool operator>(const Size& size) const 160 { 161 if (IsValid() && size.IsValid()) { 162 return GreatOrEqual(width_, size.width_) && GreatOrEqual(height_, size.height_); 163 } else { 164 return false; 165 } 166 } 167 168 /* 169 * Please make sure that two sizes are both valid. 170 * You can use IsValid() to see if a size is valid. 171 */ 172 bool operator<(const Size& size) const 173 { 174 if (IsValid() && size.IsValid()) { 175 return LessOrEqual(width_, size.width_) && LessOrEqual(height_, size.height_); 176 } else { 177 return false; 178 } 179 } 180 181 template<class T> CalcRatio(const T & rectangle)182 static double CalcRatio(const T& rectangle) 183 { 184 if (NearZero(static_cast<double>(rectangle.Height()))) { 185 return INFINITE_SIZE; 186 } 187 return static_cast<double>(rectangle.Width()) / static_cast<double>(rectangle.Height()); 188 } 189 ToString()190 std::string ToString() const 191 { 192 std::stringstream ss; 193 ss << "[" << std::fixed << std::setprecision(2); 194 if (NearEqual(width_, INFINITE_SIZE)) { 195 ss << "INFINITE"; 196 } else { 197 ss << width_; 198 } 199 ss << " x "; 200 if (NearEqual(height_, INFINITE_SIZE)) { 201 ss << "INFINITE"; 202 } else { 203 ss << height_; 204 } 205 ss << "]"; 206 std::string output = ss.str(); 207 return output; 208 } 209 210 private: 211 double width_ = 0.0; 212 double height_ = 0.0; 213 }; 214 215 } // namespace OHOS::Ace 216 217 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_SIZE_H 218