1 /*
2 * Copyright (c) 2023 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 "frameworks/bridge/declarative_frontend/jsview/js_image_span.h"
17 #include "frameworks/bridge/declarative_frontend/jsview/js_container_span.h"
18
19 #if !defined(PREVIEW)
20 #include <dlfcn.h>
21 #endif
22
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/pattern/image/image_model.h"
25 #include "core/components_ng/pattern/text/image_span_view.h"
26 #include "frameworks/bridge/declarative_frontend/jsview/js_image.h"
27
28 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)29 void JSImageSpan::Create(const JSCallbackInfo& info)
30 {
31 if (!Container::IsCurrentUseNewPipeline()) {
32 return;
33 }
34 if (info.Length() != 1) {
35 return;
36 }
37 JSImage::Create(info);
38 NG::ImageSpanView::Create();
39 }
40
SetObjectFit(const JSCallbackInfo & info)41 void JSImageSpan::SetObjectFit(const JSCallbackInfo& info)
42 {
43 if (info.Length() != 1) {
44 return;
45 }
46
47 if (info[0]->IsNumber()) {
48 auto fit = static_cast<ImageFit>(info[0]->ToNumber<int32_t>());
49 if (fit < ImageFit::FILL || fit > ImageFit::SCALE_DOWN) {
50 fit = ImageFit::COVER;
51 }
52 ImageModel::GetInstance()->SetImageFit(fit);
53 } else {
54 ImageModel::GetInstance()->SetImageFit(ImageFit::COVER);
55 }
56 }
57
SetVerticalAlign(int32_t verticalAlign)58 void JSImageSpan::SetVerticalAlign(int32_t verticalAlign)
59 {
60 auto align = static_cast<VerticalAlign>(verticalAlign);
61 if (align < VerticalAlign::TOP || align > VerticalAlign::NONE) {
62 align = VerticalAlign::BOTTOM;
63 }
64 NG::ImageSpanView::SetVerticalAlign(align);
65 }
66
SetTextBackgroundStyle(const JSCallbackInfo & info)67 void JSImageSpan::SetTextBackgroundStyle(const JSCallbackInfo& info)
68 {
69 auto textBackgroundStyle = JSContainerSpan::ParseTextBackgroundStyle(info);
70 NG::ImageSpanView::SetPlaceHolderStyle(textBackgroundStyle);
71 }
72
JSBind(BindingTarget globalObj)73 void JSImageSpan::JSBind(BindingTarget globalObj)
74 {
75 JSClass<JSImageSpan>::Declare("ImageSpan");
76 MethodOptions opt = MethodOptions::NONE;
77 JSClass<JSImageSpan>::StaticMethod("create", &JSImageSpan::Create, opt);
78 JSClass<JSImageSpan>::StaticMethod("objectFit", &JSImageSpan::SetObjectFit);
79 JSClass<JSImageSpan>::StaticMethod("verticalAlign", &JSImageSpan::SetVerticalAlign);
80 JSClass<JSImageSpan>::StaticMethod("textBackgroundStyle", &JSImageSpan::SetTextBackgroundStyle);
81 JSClass<JSImageSpan>::InheritAndBind<JSViewAbstract>(globalObj);
82 }
83 } // namespace OHOS::Ace::Framework
84