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 #include "core/components_ng/property/calc_length.h"
17 
18 #include "base/utils/string_expression.h"
19 #include "base/utils/utils.h"
20 #include "core/pipeline/pipeline_base.h"
21 
22 namespace OHOS::Ace::NG {
23 
CreateScaleProperty()24 ScaleProperty ScaleProperty::CreateScaleProperty()
25 {
26     ScaleProperty scaleProperty;
27     auto pipeline = PipelineBase::GetCurrentContextSafely();
28     CHECK_NULL_RETURN(pipeline, scaleProperty);
29     scaleProperty.fpScale = pipeline->GetFontScale();
30     scaleProperty.vpScale = static_cast<float>(pipeline->GetDipScale());
31     scaleProperty.lpxScale = static_cast<float>(pipeline->GetLogicScale());
32     return scaleProperty;
33 }
34 
NormalizeToPx(double vpScale,double fpScale,double lpxScale,double parentLength,double & result) const35 bool CalcLength::NormalizeToPx(
36     double vpScale, double fpScale, double lpxScale, double parentLength, double& result) const
37 {
38     // don't use this function for calc.
39     if (!calcValue_.empty()) {
40         result = StringExpression::CalculateExp(
41             calcValue_, [vpScale, fpScale, lpxScale, parentLength](const Dimension& dim) -> double {
42                 double result = -1.0;
43                 dim.NormalizeToPx(vpScale, fpScale, lpxScale, parentLength, result);
44                 return result;
45             });
46         return result >= 0;
47     }
48     return dimension_.NormalizeToPx(vpScale, fpScale, lpxScale, parentLength, result);
49 }
50 
51 } // namespace OHOS::Ace::NG
52