1 /*
2 * Copyright (c) 2021-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 #include "base/geometry/dimension.h"
17
18 #include <array>
19 #include <functional>
20
21 #include "base/utils/string_utils.h"
22 #include "base/utils/utils.h"
23 #include "core/pipeline/pipeline_base.h"
24
25 namespace OHOS::Ace {
26
27 namespace {
28 struct CalcDimensionParam {
29 float value = 0.0f;
30 float vpScale = 0.0f;
31 float fpScale = 0.0f;
32 float lpxScale = 0.0f;
33 float parentLength = 0.0f;
34 };
35
36 using CalcDimensionFunc = std::function<bool(const CalcDimensionParam& param, double& result)>;
CalcDimensionNone(const CalcDimensionParam & param,double & result)37 bool CalcDimensionNone(const CalcDimensionParam& param, double& result)
38 {
39 result = param.value;
40 return true;
41 }
42
CalcDimensionPx(const CalcDimensionParam & param,double & result)43 bool CalcDimensionPx(const CalcDimensionParam& param, double& result)
44 {
45 result = param.value;
46 return true;
47 }
48
CalcDimensionPercent(const CalcDimensionParam & param,double & result)49 bool CalcDimensionPercent(const CalcDimensionParam& param, double& result)
50 {
51 if (NonNegative(param.parentLength)) {
52 result = param.value * param.parentLength;
53 return true;
54 }
55 return false;
56 }
57
CalcDimensionVp(const CalcDimensionParam & param,double & result)58 bool CalcDimensionVp(const CalcDimensionParam& param, double& result)
59 {
60 if (Positive(param.vpScale)) {
61 result = param.value * param.vpScale;
62 return true;
63 }
64 return false;
65 }
66
CalcDimensionFp(const CalcDimensionParam & param,double & result)67 bool CalcDimensionFp(const CalcDimensionParam& param, double& result)
68 {
69 if (Positive(param.fpScale) && Positive(param.vpScale)) {
70 result = param.value * param.fpScale * param.vpScale;
71 return true;
72 }
73 return false;
74 }
75
CalcDimensionLpx(const CalcDimensionParam & param,double & result)76 bool CalcDimensionLpx(const CalcDimensionParam& param, double& result)
77 {
78 if (Positive(param.lpxScale)) {
79 result = param.value * param.lpxScale;
80 return true;
81 }
82 return false;
83 }
84
85 std::unordered_map<DimensionUnit, CalcDimensionFunc> calcDimensionFuncMap_ = {
86 { DimensionUnit::NONE, &CalcDimensionNone }, { DimensionUnit::PX, &CalcDimensionPx },
87 { DimensionUnit::PERCENT, &CalcDimensionPercent }, { DimensionUnit::VP, &CalcDimensionVp },
88 { DimensionUnit::FP, &CalcDimensionFp }, { DimensionUnit::LPX, &CalcDimensionLpx }
89 };
90 } // namespace
91
ConvertToVp() const92 double Dimension::ConvertToVp() const
93 {
94 if (unit_ == DimensionUnit::VP) {
95 return value_;
96 }
97
98 auto pipeline = PipelineBase::GetCurrentContext();
99 CHECK_NULL_RETURN(pipeline, 0.0);
100 if (unit_ == DimensionUnit::NONE) {
101 return value_ / pipeline->GetDipScale();
102 }
103 if (unit_ == DimensionUnit::PX) {
104 return value_ / pipeline->GetDipScale();
105 }
106 if (unit_ == DimensionUnit::FP) {
107 return value_ * pipeline->GetFontScale();
108 }
109 if (unit_ == DimensionUnit::LPX) {
110 return value_ * pipeline->GetLogicScale() / pipeline->GetDipScale();
111 }
112 LOGE("fail to ConvertToVp, %{public}f, %{public}d", value_, unit_);
113 return 0.0;
114 }
115
ConvertToPx() const116 double Dimension::ConvertToPx() const
117 {
118 if (unit_ == DimensionUnit::NONE) {
119 return value_;
120 }
121 if (unit_ == DimensionUnit::PX) {
122 return value_;
123 }
124
125 auto pipeline = PipelineBase::GetCurrentContext();
126 CHECK_NULL_RETURN(pipeline, 0.0);
127 if (unit_ == DimensionUnit::VP) {
128 return value_ * pipeline->GetDipScale();
129 }
130 if (unit_ == DimensionUnit::FP) {
131 return value_ * pipeline->GetDipScale() * pipeline->GetFontScale();
132 }
133 if (unit_ == DimensionUnit::LPX) {
134 return value_ * pipeline->GetLogicScale();
135 }
136 LOGE("fail to ConvertToPx, %{public}f, %{public}d", value_, unit_);
137 return 0.0;
138 }
139
ConvertToPxWithSize(double size) const140 double Dimension::ConvertToPxWithSize(double size) const
141 {
142 if (unit_ == DimensionUnit::PERCENT) {
143 return value_ * size;
144 }
145 return ConvertToPx();
146 }
147
ToString() const148 std::string Dimension::ToString() const
149 {
150 static const int32_t unitsNum = 6;
151 static const int32_t percentIndex = 3;
152 static const int32_t percentUnit = 100;
153 static std::array<std::string, unitsNum> units = { "px", "vp", "fp", "%", "lpx", "auto" };
154 if (static_cast<int>(unit_) > unitsNum) {
155 return StringUtils::DoubleToString(value_).append("px");
156 }
157 if (unit_ == DimensionUnit::NONE) {
158 return StringUtils::DoubleToString(value_).append("none");
159 }
160 if (units[static_cast<int>(unit_)] == units[percentIndex]) {
161 return StringUtils::DoubleToString(value_ * percentUnit).append(units[static_cast<int>(unit_)]);
162 }
163 return StringUtils::DoubleToString(value_).append(units[static_cast<int>(unit_)]);
164 }
165
166 // for example str = 0.00px
FromString(const std::string & str)167 Dimension Dimension::FromString(const std::string& str)
168 {
169 static const int32_t percentUnit = 100;
170 static const std::unordered_map<std::string, DimensionUnit> uMap {
171 { "px", DimensionUnit::PX },
172 { "vp", DimensionUnit::VP },
173 { "fp", DimensionUnit::FP },
174 { "%", DimensionUnit::PERCENT },
175 { "lpx", DimensionUnit::LPX },
176 { "auto", DimensionUnit::AUTO },
177 };
178
179 LOGD("UITree str=%{public}s", str.c_str());
180 double value = 0.0;
181 DimensionUnit unit = DimensionUnit::FP;
182
183 if (str.empty()) {
184 LOGE("UITree |ERROR| empty string");
185 return Dimension(value, unit);
186 }
187
188 for (int32_t i = str.length() - 1; i >= 0; --i) {
189 if (str[i] >= '0' && str[i] <= '9') {
190 value = StringUtils::StringToDouble(str.substr(0, i + 1));
191 auto subStr = str.substr(i + 1);
192 unit = uMap.count(subStr) ? uMap.at(subStr) : unit;
193 value = unit == DimensionUnit::PERCENT ? value / percentUnit : value;
194 break;
195 }
196 }
197 return Dimension(value, unit);
198 }
199
NormalizeToPx(double vpScale,double fpScale,double lpxScale,double parentLength,double & result) const200 bool Dimension::NormalizeToPx(
201 double vpScale, double fpScale, double lpxScale, double parentLength, double& result) const
202 {
203 auto func = calcDimensionFuncMap_.find(unit_);
204 if (func != calcDimensionFuncMap_.end()) {
205 CalcDimensionParam param = { value_, vpScale, fpScale, lpxScale, parentLength };
206 return func->second(param, result);
207 }
208 return false;
209 }
210 } // namespace OHOS::Ace
211