1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/common/dom/dom_image.h"
17
18 #include "base/utils/linear_map.h"
19 #include "base/utils/utils.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 #include "frameworks/core/components/common/properties/decoration.h"
23
24 namespace OHOS::Ace::Framework {
25 namespace {
26
27 const char SVG_THEME_FILL[] = "theme_fill";
28
ConvertStrToFit(const std::string & fit)29 ImageFit ConvertStrToFit(const std::string& fit)
30 {
31 static const LinearMapNode<ImageFit> imageFitMap[] = {
32 { "contain", ImageFit::CONTAIN },
33 { "cover", ImageFit::COVER },
34 { "fill", ImageFit::FILL },
35 { "none", ImageFit::NONE },
36 { "scale-down", ImageFit::SCALE_DOWN },
37 };
38 ImageFit imageFit = ImageFit::COVER;
39 auto iter = BinarySearchFindIndex(imageFitMap, ArraySize(imageFitMap), fit.c_str());
40 if (iter != -1) {
41 imageFit = imageFitMap[iter].value;
42 }
43 return imageFit;
44 }
45
46 } // namespace
47
ImageObjectPosition(const std::string & value)48 ImageObjectPosition ImageObjectPosition(const std::string& value)
49 {
50 return ParseImageObjectPosition(value.c_str());
51 }
52
InitializeStyle()53 void DOMImage::InitializeStyle()
54 {
55 theme_ = GetTheme<ImageTheme>();
56 if (!theme_) {
57 LOGW("ImageTheme is null");
58 }
59 }
60
DOMImage(NodeId nodeId,const std::string & nodeName)61 DOMImage::DOMImage(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
62 {
63 imageChild_ = AceType::MakeRefPtr<ImageComponent>();
64 imageChild_->SetFitMaxSize(true);
65 }
66
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)67 bool DOMImage::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
68 {
69 if (attr.first == DOM_SRC) {
70 imageChild_->SetSrc(ParseImageSrc(attr.second));
71 return true;
72 }
73 if (attr.first == DOM_IMAGE_SYNC_LOAD) {
74 imageChild_->SetSyncMode(StringToBool(attr.second));
75 return true;
76 }
77 if (attr.first == DOM_IMAGE_ALT) {
78 imageChild_->SetAlt(ParseImageSrc(attr.second));
79 return true;
80 }
81 return false;
82 }
83
SetSpecializedStyle(const std::pair<std::string,std::string> & style)84 bool DOMImage::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
85 {
86 // static linear map must be sorted by key.
87 static const LinearMapNode<void (*)(const std::string&, DOMImage&)> imageStylesOperators[] = {
88 { DOM_IMAGE_FILL_COLOR,
89 [](const std::string& val, DOMImage& image) {
90 if (val == SVG_THEME_FILL && image.theme_) {
91 image.imageChild_->SetColor(image.theme_->GetFillColor());
92 }
93 } },
94 { DOM_IMAGE_FIT_ORIGINAL_SIZE,
95 [](const std::string& val, DOMImage& image) { image.imageChild_->SetFitMaxSize(!StringToBool(val)); } },
96 { DOM_IMAGE_MATCH_TEXT_DIRECTION,
97 [](const std::string& val, DOMImage& image) {
98 image.imageChild_->SetMatchTextDirection(StringToBool(val));
99 } },
100 { DOM_IMAGE_FIT, [](const std::string& val,
101 DOMImage& image) { image.imageChild_->SetImageFit(ConvertStrToFit(val.c_str())); } },
102 { DOM_IMAGE_POSITION, [](const std::string& val, DOMImage& image) {
103 image.imageChild_->SetImageObjectPosition(ImageObjectPosition(val.c_str())); } },
104 };
105 auto operatorIter =
106 BinarySearchFindIndex(imageStylesOperators, ArraySize(imageStylesOperators), style.first.c_str());
107 if (operatorIter != -1) {
108 imageStylesOperators[operatorIter].value(style.second, *this);
109 return true;
110 }
111 return false;
112 }
113
AddSpecializedEvent(int32_t pageId,const std::string & event)114 bool DOMImage::AddSpecializedEvent(int32_t pageId, const std::string& event)
115 {
116 if (event == DOM_COMPLETE) {
117 loadSuccessEvent_ = EventMarker(GetNodeIdForEvent(), event, pageId);
118 imageChild_->SetLoadSuccessEvent(loadSuccessEvent_);
119 } else if (event == DOM_ERROR) {
120 loadFailEvent_ = EventMarker(GetNodeIdForEvent(), event, pageId);
121 imageChild_->SetLoadFailEvent(loadFailEvent_);
122 } else {
123 return false;
124 }
125 return true;
126 }
127
PrepareSpecializedComponent()128 void DOMImage::PrepareSpecializedComponent()
129 {
130 imageChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
131 // If there is a corresponding box decoration, specialize the box.
132 if (boxComponent_) {
133 auto backDecoration = boxComponent_->GetBackDecoration();
134 if (backDecoration) {
135 imageChild_->SetBorder(backDecoration->GetBorder());
136 }
137 }
138 if (flexItemComponent_ && !imageChild_->GetFitMaxSize()) {
139 flexItemComponent_->SetStretchFlag(false);
140 }
141
142 imageChild_->SetImageFill(GetImageFill());
143 }
144
145 } // namespace OHOS::Ace::Framework
146