1 /*
2  * Copyright (c) 2024 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 "cj_pixel_unit_convert_ffi.h"
17 #include "core/pipeline/pipeline_base.h"
18 #include "core/common/container.h"
19 
20 using namespace OHOS::Ace;
21 using namespace OHOS::Ace::Framework;
22 
23 namespace OHOS::Ace::Framework {
24 extern "C" {
FfiOHOSAceFrameworkVp2Px(double value)25 double FfiOHOSAceFrameworkVp2Px(double value)
26 {
27     auto container = Container::Current();
28     if (!container) {
29         return NAN;
30     }
31     double density = PipelineBase::GetCurrentDensity();
32     return value * density;
33 }
34 
FfiOHOSAceFrameworkPx2Vp(double value)35 double FfiOHOSAceFrameworkPx2Vp(double value)
36 {
37     auto container = Container::Current();
38     if (!container) {
39         return NAN;
40     }
41     double density = PipelineBase::GetCurrentDensity();
42     if (NearZero(density)) {
43         return 1.0f;
44     }
45     return value / density;
46 }
47 
48 
GetFp2PxParam()49 double GetFp2PxParam()
50 {
51     double density = PipelineBase::GetCurrentDensity();
52     auto container = Container::Current();
53     if (!container) {
54         return NAN;
55     }
56     auto pipelineContext = container->GetPipelineContext();
57     double fontScale = 1.0;
58 
59     if (pipelineContext) {
60         fontScale = pipelineContext->GetFontScale();
61     }
62     return density * fontScale;
63 }
64 
FfiOHOSAceFrameworkFp2Px(double value)65 double FfiOHOSAceFrameworkFp2Px(double value)
66 {
67     return value * GetFp2PxParam();
68 }
69 
FfiOHOSAceFrameworkPx2Fp(double value)70 double FfiOHOSAceFrameworkPx2Fp(double value)
71 {
72     return value / GetFp2PxParam();
73 }
74 
GetdesignWidthScale()75 double GetdesignWidthScale()
76 {
77     auto container = Container::Current();
78     if (!container) {
79         return NAN;
80     }
81     auto window = container->GetWindow();
82     if (!window) {
83         return NAN;
84     }
85     auto width = window->GetCurrentWindowRect().Width();
86     auto frontend = container->GetFrontend();
87     if (!frontend) {
88         return NAN;
89     }
90     auto windowConfig = frontend->GetWindowConfig();
91 
92     windowConfig.UpdateDesignWidthScale(width);
93     return windowConfig.GetDesignWidthScale(width);
94 }
95 
FfiOHOSAceFrameworkLpx2Px(double value)96 double FfiOHOSAceFrameworkLpx2Px(double value)
97 {
98     auto designWidthScale = GetdesignWidthScale();
99     return value * designWidthScale;
100 }
101 
FfiOHOSAceFrameworkPx2Lpx(double value)102 double FfiOHOSAceFrameworkPx2Lpx(double value)
103 {
104     auto designWidthScale = GetdesignWidthScale();
105     return value / designWidthScale;
106 }
107 }
108 }