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 FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_CALC_LENGTH_H
17 #define FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_CALC_LENGTH_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/geometry/ng/size_t.h"
21 #include "base/utils/utils.h"
22 
23 namespace OHOS::Ace::NG {
24 struct ACE_EXPORT ScaleProperty {
25     float vpScale = 0.0f;
26     float fpScale = 0.0f;
27     float lpxScale = 0.0f;
28 
ResetScaleProperty29     void Reset()
30     {
31         vpScale = 0.0;
32         fpScale = 0.0;
33         lpxScale = 0.0;
34     }
35 
36     bool operator==(const ScaleProperty& scaleProperty) const
37     {
38         return NearEqual(vpScale, scaleProperty.vpScale) && NearEqual(fpScale, scaleProperty.fpScale) &&
39                NearEqual(lpxScale, scaleProperty.lpxScale);
40     }
41 
42     bool operator!=(const ScaleProperty& scaleProperty) const
43     {
44         return !(*this == scaleProperty);
45     }
46 
47     static ScaleProperty CreateScaleProperty();
48 };
49 
50 class CalcLength {
51 public:
52     CalcLength() = default;
CalcLength(const std::string & value)53     explicit CalcLength(const std::string& value) : calcValue_(value)
54     {
55         dimension_.SetUnit(DimensionUnit::CALC);
56     }
57     ~CalcLength() = default;
58 
59     explicit CalcLength(double value, DimensionUnit unit = DimensionUnit::PX) : dimension_(value, unit) {};
CalcLength(const Dimension & dimension)60     explicit CalcLength(const Dimension& dimension) : dimension_(dimension) {};
61 
Reset()62     void Reset()
63     {
64         calcValue_ = "";
65         dimension_.Reset();
66     }
67 
CalcValue()68     const std::string& CalcValue() const
69     {
70         return calcValue_;
71     }
72 
SetCalcValue(const std::string & value)73     void SetCalcValue(const std::string& value)
74     {
75         calcValue_ = value;
76     }
77 
78     bool NormalizeToPx(double vpScale, double fpScale, double lpxScale, double parentLength, double& result) const;
79 
IsValid()80     bool IsValid() const
81     {
82         if (calcValue_.empty()) {
83             return dimension_.IsNonNegative();
84         }
85         return true;
86     }
87 
88     bool operator==(const CalcLength& length) const
89     {
90         if (calcValue_.empty() && length.calcValue_.empty()) {
91             return dimension_ == length.dimension_;
92         }
93         return calcValue_ == length.calcValue_;
94     }
95 
96     bool operator!=(const CalcLength& length) const
97     {
98         return !(*this == length);
99     }
100 
ToString()101     std::string ToString() const
102     {
103         if (calcValue_.empty()) {
104             return dimension_.ToString();
105         }
106         return calcValue_;
107     }
108 
FromString(const std::string & str)109     static CalcLength FromString(const std::string& str)
110     {
111         return CalcLength(Dimension::FromString(str));
112     }
113 
GetDimension()114     Dimension GetDimension() const
115     {
116         if (!IsValid()) {
117             return Dimension();
118         }
119         return dimension_;
120     }
121 
GetDimensionContainsNegative()122     Dimension GetDimensionContainsNegative() const
123     {
124         return dimension_;
125     }
126 
127 private:
128     std::string calcValue_;
129     Dimension dimension_;
130 };
131 } // namespace OHOS::Ace::NG
132 
133 #endif // FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_CALC_LENGTH_H
134