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
18 #if !defined(PREVIEW)
19 #include <dlfcn.h>
20 #endif
21
22 #include "core/components/common/layout/constants.h"
23 #include "core/components_ng/pattern/image/image_model.h"
24 #include "core/components_ng/pattern/text/image_span_view.h"
25 #include "frameworks/bridge/declarative_frontend/jsview/js_image.h"
26
27 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)28 void JSImageSpan::Create(const JSCallbackInfo& info)
29 {
30 if (!Container::IsCurrentUseNewPipeline()) {
31 return;
32 }
33 if (info.Length() != 1) {
34 LOGE("The arg is wrong, it is supposed to have 1 argument");
35 return;
36 }
37 JSImage::Create(info);
38 }
39
SetObjectFit(const JSCallbackInfo & info)40 void JSImageSpan::SetObjectFit(const JSCallbackInfo& info)
41 {
42 if (info.Length() != 1) {
43 LOGE("The arg is wrong, it is supposed to have 1 argument");
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 LOGW("The value of objectFit is out of range %{public}d", fit);
51 fit = ImageFit::COVER;
52 }
53 ImageModel::GetInstance()->SetImageFit(fit);
54 } else {
55 ImageModel::GetInstance()->SetImageFit(ImageFit::COVER);
56 }
57 }
58
SetVerticalAlign(int32_t verticalAlign)59 void JSImageSpan::SetVerticalAlign(int32_t verticalAlign)
60 {
61 auto align = static_cast<VerticalAlign>(verticalAlign);
62 if (align < VerticalAlign::TOP || align > VerticalAlign::NONE) {
63 LOGW("The value of verticalAlign is out of range %{public}d", verticalAlign);
64 align = VerticalAlign::BOTTOM;
65 }
66 NG::ImageSpanView::SetVerticalAlign(align);
67 }
68
JSBind(BindingTarget globalObj)69 void JSImageSpan::JSBind(BindingTarget globalObj)
70 {
71 JSClass<JSImageSpan>::Declare("ImageSpan");
72 MethodOptions opt = MethodOptions::NONE;
73 JSClass<JSImageSpan>::StaticMethod("create", &JSImageSpan::Create, opt);
74 JSClass<JSImageSpan>::StaticMethod("objectFit", &JSImageSpan::SetObjectFit);
75 JSClass<JSImageSpan>::StaticMethod("verticalAlign", &JSImageSpan::SetVerticalAlign);
76 JSClass<JSImageSpan>::InheritAndBind<JSViewAbstract>(globalObj);
77 }
78 } // namespace OHOS::Ace::Framework
79