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 "bridge/cj_frontend/interfaces/cj_ffi/cj_image_span_ffi.h"
17 
18 #include "cj_lambda.h"
19 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
20 #include "core/common/container.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components_ng/pattern/image/image_model.h"
23 #include "core/components_ng/pattern/text/image_span_view.h"
24 
25 using namespace OHOS::Ace::Framework;
26 using namespace OHOS::Ace;
27 
28 const std::vector<ImageFit> IMAGE_FITS = {
29     ImageFit::FILL,
30     ImageFit::CONTAIN,
31     ImageFit::COVER,
32     ImageFit::NONE,
33     ImageFit::SCALE_DOWN,
34     ImageFit::FITWIDTH
35 };
36 
37 const std::vector<VerticalAlign> VERTICAL_ALIGNS = {
38     VerticalAlign::TOP,
39     VerticalAlign::CENTER,
40     VerticalAlign::BOTTOM,
41     VerticalAlign::BASELINE
42 };
43 
ParseTextBackgroundStyle(uint32_t color,double radiusDouble,int32_t unit)44 TextBackgroundStyle ParseTextBackgroundStyle(uint32_t color, double radiusDouble, int32_t unit)
45 {
46     TextBackgroundStyle textBackgroundStyle;
47     Color colorVal = Color(color);
48     Dimension value(radiusDouble, static_cast<DimensionUnit>(unit));
49     CalcDimension radius = CalcDimension(value);
50     if (radius.Unit() == DimensionUnit::PERCENT) {
51         radius.Reset();
52     }
53     textBackgroundStyle.backgroundColor = colorVal;
54     textBackgroundStyle.backgroundRadius = { radius, radius, radius, radius };
55     textBackgroundStyle.backgroundRadius->multiValued = false;
56     return textBackgroundStyle;
57 }
58 
ParseTextBackgroundStyle(uint32_t color,CBorderRadiuses radiusValue)59 TextBackgroundStyle ParseTextBackgroundStyle(uint32_t color, CBorderRadiuses radiusValue)
60 {
61     TextBackgroundStyle textBackgroundStyle;
62     Color colorVal = Color(color);
63     Dimension topLeftValue(radiusValue.topLeftRadiuses,
64         static_cast<DimensionUnit>(radiusValue.topLeftUnit));
65     CalcDimension topLeft = CalcDimension(topLeftValue);
66     Dimension topRightValue(radiusValue.topRightRadiuses,
67         static_cast<DimensionUnit>(radiusValue.topRightUnit));
68     CalcDimension topRight = CalcDimension(topRightValue);
69     Dimension bottomLeftValue(radiusValue.bottomLeftRadiuses,
70         static_cast<DimensionUnit>(radiusValue.bottomLeftUnit));
71     CalcDimension bottomLeft = CalcDimension(bottomLeftValue);
72     Dimension bottomRightValue(radiusValue.bottomRightRadiuses,
73         static_cast<DimensionUnit>(radiusValue.bottomRightUnit));
74     CalcDimension bottomRight = CalcDimension(bottomRightValue);
75     textBackgroundStyle.backgroundColor = colorVal;
76     textBackgroundStyle.backgroundRadius = { topLeft, topRight, bottomRight, bottomLeft };
77     textBackgroundStyle.backgroundRadius->multiValued = false;
78     return textBackgroundStyle;
79 }
80 
81 extern "C" {
FfiOHOSAceFrameworkImageSpanCreateWithUrl(const char * url)82 void FfiOHOSAceFrameworkImageSpanCreateWithUrl(const char* url)
83 {
84     if (!Container::IsCurrentUseNewPipeline()) {
85         return;
86     }
87     FfiOHOSAceFrameworkImageCreateWithUrl(url);
88     NG::ImageSpanView::Create();
89 }
90 
FfiOHOSAceFrameworkImageSpanCreateWithPixelMap(int64_t id)91 void FfiOHOSAceFrameworkImageSpanCreateWithPixelMap(int64_t id)
92 {
93     if (!Container::IsCurrentUseNewPipeline()) {
94         return;
95     }
96     FfiOHOSAceFrameworkImageCreateWithPixelMap(id);
97     NG::ImageSpanView::Create();
98 }
99 
FfiOHOSAceFrameworkImageSpanVerticalAlign(int32_t value)100 void FfiOHOSAceFrameworkImageSpanVerticalAlign(int32_t value)
101 {
102     if (!OHOS::Ace::Framework::Utils::CheckParamsValid(value, VERTICAL_ALIGNS.size())) {
103         LOGE("invalid value for vertical align");
104         return;
105     }
106     NG::ImageSpanView::SetVerticalAlign(VERTICAL_ALIGNS[value]);
107 }
108 
FfiOHOSAceFrameworkImageSpanObjectFit(int32_t value)109 void FfiOHOSAceFrameworkImageSpanObjectFit(int32_t value)
110 {
111     if (!OHOS::Ace::Framework::Utils::CheckParamsValid(value, IMAGE_FITS.size())) {
112         LOGE("invalid value for image fit");
113         return;
114     }
115     ImageModel::GetInstance()->SetImageFit(IMAGE_FITS[value]);
116 }
117 
FfiOHOSAceFrameworkImageSpanTextBackgroundStyle(uint32_t color,double radius,int32_t unit)118 void FfiOHOSAceFrameworkImageSpanTextBackgroundStyle(uint32_t color, double radius, int32_t unit)
119 {
120     auto textBackgroundStyle = ParseTextBackgroundStyle(color, radius, unit);
121     NG::ImageSpanView::SetPlaceHolderStyle(textBackgroundStyle);
122 }
123 
FfiOHOSAceFrameworkImageSpanTextBackgroundStyleBorder(uint32_t color,CBorderRadiuses radius)124 void FfiOHOSAceFrameworkImageSpanTextBackgroundStyleBorder(uint32_t color, CBorderRadiuses radius)
125 {
126     auto textBackgroundStyle = ParseTextBackgroundStyle(color, radius);
127     NG::ImageSpanView::SetPlaceHolderStyle(textBackgroundStyle);
128 }
129 }
130