• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "base/utils/string_utils.h"
21 #include "base/utils/utils.h"
22 #include "core/pipeline/pipeline_base.h"
23 
24 namespace OHOS::Ace {
25 
ConvertToVp() const26 double Dimension::ConvertToVp() const
27 {
28     if (unit_ == DimensionUnit::VP) {
29         return value_;
30     }
31 
32     auto pipeline = PipelineBase::GetCurrentContext();
33     CHECK_NULL_RETURN(pipeline, 0.0);
34     if (unit_ == DimensionUnit::PX) {
35         return value_ / pipeline->GetDipScale();
36     }
37     if (unit_ == DimensionUnit::FP) {
38         return value_ * pipeline->GetFontScale();
39     }
40     if (unit_ == DimensionUnit::LPX) {
41         return value_ * pipeline->GetLogicScale() / pipeline->GetDipScale();
42     }
43     LOGE("fail to ConvertToVp, %{public}f, %{public}d", value_, unit_);
44     return 0.0;
45 }
46 
ConvertToPx() const47 double Dimension::ConvertToPx() const
48 {
49     if (unit_ == DimensionUnit::PX) {
50         return value_;
51     }
52 
53     auto pipeline = PipelineBase::GetCurrentContext();
54     CHECK_NULL_RETURN(pipeline, 0.0);
55     if (unit_ == DimensionUnit::VP) {
56         return value_ * pipeline->GetDipScale();
57     }
58     if (unit_ == DimensionUnit::FP) {
59         return value_ * pipeline->GetDipScale() * pipeline->GetFontScale();
60     }
61     if (unit_ == DimensionUnit::LPX) {
62         return value_ * pipeline->GetLogicScale();
63     }
64     LOGE("fail to ConvertToPx, %{public}f, %{public}d", value_, unit_);
65     return 0.0;
66 }
67 
ConvertToPxWithSize(double size) const68 double Dimension::ConvertToPxWithSize(double size) const
69 {
70     if (unit_ == DimensionUnit::PERCENT) {
71         return value_ * size;
72     }
73     return ConvertToPx();
74 }
75 
ToString() const76 std::string Dimension::ToString() const
77 {
78     static const int32_t unitsNum = 6;
79     static const int32_t percentIndex = 3;
80     static const int32_t percentUnit = 100;
81     static std::array<std::string, unitsNum> units = { "px", "vp", "fp", "%", "lpx", "auto" };
82     if (units[static_cast<int>(unit_)] == units[percentIndex]) {
83         return StringUtils::DoubleToString(value_ * percentUnit).append(units[static_cast<int>(unit_)]);
84     }
85     return StringUtils::DoubleToString(value_).append(units[static_cast<int>(unit_)]);
86 }
87 
NormalizeToPx(double vpScale,double fpScale,double lpxScale,double parentLength,double & result) const88 bool Dimension::NormalizeToPx(
89     double vpScale, double fpScale, double lpxScale, double parentLength, double& result) const
90 {
91     if (unit_ == DimensionUnit::PX) {
92         result = value_;
93         return true;
94     }
95     if (unit_ == DimensionUnit::PERCENT) {
96         if (NonNegative(parentLength)) {
97             result = value_ * parentLength;
98             return true;
99         }
100         return false;
101     }
102     if (unit_ == DimensionUnit::VP) {
103         if (Positive(vpScale)) {
104             result = value_ * vpScale;
105             return true;
106         }
107         return false;
108     }
109     if (unit_ == DimensionUnit::FP) {
110         if (Positive(fpScale)) {
111             result = value_ * fpScale * vpScale;
112             return true;
113         }
114         return false;
115     }
116     if (unit_ == DimensionUnit::LPX) {
117         if (Positive(lpxScale)) {
118             result = value_ * lpxScale;
119             return true;
120         }
121         return false;
122     }
123     return false;
124 }
125 } // namespace OHOS::Ace