• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DIMENSION_SIZE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_DIMENSION_SIZE_H
18 
19 #include <iomanip>
20 #include <limits>
21 #include <sstream>
22 #include <string>
23 
24 #include "base/geometry/dimension.h"
25 #include "base/utils/utils.h"
26 
27 namespace OHOS::Ace {
28 
29 class DimensionSize {
30 public:
31     static constexpr double INFINITE_SIZE = std::numeric_limits<double>::max();
32     DimensionSize() = default;
33     ~DimensionSize() = default;
DimensionSize(const Dimension & width,const Dimension & height)34     DimensionSize(const Dimension& width, const Dimension& height) : width_(width), height_(height) {}
35 
Width()36     const Dimension& Width() const
37     {
38         return width_;
39     }
40 
Height()41     const Dimension& Height() const
42     {
43         return height_;
44     }
45 
SetWidth(const Dimension & width)46     void SetWidth(const Dimension& width)
47     {
48         width_ = width;
49     }
50 
SetHeight(const Dimension & height)51     void SetHeight(const Dimension& height)
52     {
53         height_ = height;
54     }
55 
SetSize(const DimensionSize & size)56     void SetSize(const DimensionSize& size)
57     {
58         width_ = size.Width();
59         height_ = size.Height();
60     }
61 
62     template<class T>
CalcRatio(const T & rectangle)63     static double CalcRatio(const T& rectangle)
64     {
65         if (NearZero(static_cast<double>(rectangle.Height()))) {
66             return INFINITE_SIZE;
67         }
68         return static_cast<double>(rectangle.Width()) / static_cast<double>(rectangle.Height());
69     }
70 
ToString()71     std::string ToString() const
72     {
73         std::stringstream ss;
74         ss << "[" << std::fixed << std::setprecision(2);
75         if (NearEqual(width_.Value(), INFINITE_SIZE)) {
76             ss << "INFINITE";
77         } else {
78             ss << width_.ToString();
79         }
80         ss << " x ";
81         if (NearEqual(height_.Value(), INFINITE_SIZE)) {
82             ss << "INFINITE";
83         } else {
84             ss << height_.ToString();
85         }
86         ss << "]";
87         std::string output = ss.str();
88         return output;
89     }
90 
91 private:
92     Dimension width_ = 0.0_vp;
93     Dimension height_ = 0.0_vp;
94 };
95 
96 } // namespace OHOS::Ace
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_DIMENSION_SIZE_H
99