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 return 0.0;
113 }
114
ConvertToPx() const115 double Dimension::ConvertToPx() const
116 {
117 if (unit_ == DimensionUnit::NONE) {
118 return value_;
119 }
120 if (unit_ == DimensionUnit::PX) {
121 return value_;
122 }
123
124 auto pipeline = PipelineBase::GetCurrentContext();
125 CHECK_NULL_RETURN(pipeline, 0.0);
126 if (unit_ == DimensionUnit::VP) {
127 return value_ * pipeline->GetDipScale();
128 }
129 if (unit_ == DimensionUnit::FP) {
130 return value_ * pipeline->GetDipScale() * pipeline->GetFontScale();
131 }
132 if (unit_ == DimensionUnit::LPX) {
133 return value_ * pipeline->GetLogicScale();
134 }
135 return 0.0;
136 }
137
ConvertToPxWithSize(double size) const138 double Dimension::ConvertToPxWithSize(double size) const
139 {
140 if (unit_ == DimensionUnit::PERCENT) {
141 return value_ * size;
142 }
143 return ConvertToPx();
144 }
145
ToString() const146 std::string Dimension::ToString() const
147 {
148 static const int32_t unitsNum = 6;
149 static const int32_t percentIndex = 3;
150 static const int32_t percentUnit = 100;
151 static std::array<std::string, unitsNum> units = { "px", "vp", "fp", "%", "lpx", "auto" };
152 if (static_cast<int>(unit_) > unitsNum) {
153 return StringUtils::DoubleToString(value_).append("px");
154 }
155 if (unit_ == DimensionUnit::NONE) {
156 return StringUtils::DoubleToString(value_).append("none");
157 }
158 if (units[static_cast<int>(unit_)] == units[percentIndex]) {
159 return StringUtils::DoubleToString(value_ * percentUnit).append(units[static_cast<int>(unit_)]);
160 }
161 return StringUtils::DoubleToString(value_).append(units[static_cast<int>(unit_)]);
162 }
163
164 // for example str = 0.00px
FromString(const std::string & str)165 Dimension Dimension::FromString(const std::string& str)
166 {
167 static const int32_t percentUnit = 100;
168 static const std::unordered_map<std::string, DimensionUnit> uMap {
169 { "px", DimensionUnit::PX },
170 { "vp", DimensionUnit::VP },
171 { "fp", DimensionUnit::FP },
172 { "%", DimensionUnit::PERCENT },
173 { "lpx", DimensionUnit::LPX },
174 { "auto", DimensionUnit::AUTO },
175 };
176
177 double value = 0.0;
178 DimensionUnit unit = DimensionUnit::FP;
179
180 if (str.empty()) {
181 LOGE("UITree |ERROR| empty string");
182 return Dimension(value, unit);
183 }
184
185 for (int32_t i = str.length() - 1; i >= 0; --i) {
186 if (str[i] >= '0' && str[i] <= '9') {
187 value = StringUtils::StringToDouble(str.substr(0, i + 1));
188 auto subStr = str.substr(i + 1);
189 unit = uMap.count(subStr) ? uMap.at(subStr) : unit;
190 value = unit == DimensionUnit::PERCENT ? value / percentUnit : value;
191 break;
192 }
193 }
194 return Dimension(value, unit);
195 }
196
NormalizeToPx(double vpScale,double fpScale,double lpxScale,double parentLength,double & result) const197 bool Dimension::NormalizeToPx(
198 double vpScale, double fpScale, double lpxScale, double parentLength, double& result) const
199 {
200 auto func = calcDimensionFuncMap_.find(unit_);
201 if (func != calcDimensionFuncMap_.end()) {
202 CalcDimensionParam param = { value_, vpScale, fpScale, lpxScale, parentLength };
203 return func->second(param, result);
204 }
205 return false;
206 }
207 } // namespace OHOS::Ace
208