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 }
57
DOMImage(NodeId nodeId,const std::string & nodeName)58 DOMImage::DOMImage(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
59 {
60 imageChild_ = AceType::MakeRefPtr<ImageComponent>();
61 imageChild_->SetFitMaxSize(true);
62 }
63
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)64 bool DOMImage::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
65 {
66 if (attr.first == DOM_SRC) {
67 imageChild_->SetSrc(ParseImageSrc(attr.second));
68 return true;
69 }
70 if (attr.first == DOM_IMAGE_SYNC_LOAD) {
71 imageChild_->SetSyncMode(StringToBool(attr.second));
72 return true;
73 }
74 if (attr.first == DOM_IMAGE_ALT) {
75 imageChild_->SetAlt(ParseImageSrc(attr.second));
76 return true;
77 }
78 return false;
79 }
80
SetSpecializedStyle(const std::pair<std::string,std::string> & style)81 bool DOMImage::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
82 {
83 // static linear map must be sorted by key.
84 static const LinearMapNode<void (*)(const std::string&, DOMImage&)> imageStylesOperators[] = {
85 { DOM_IMAGE_FILL_COLOR,
86 [](const std::string& val, DOMImage& image) {
87 if (val == SVG_THEME_FILL && image.theme_) {
88 image.imageChild_->SetColor(image.theme_->GetFillColor());
89 }
90 } },
91 { DOM_IMAGE_FIT_ORIGINAL_SIZE,
92 [](const std::string& val, DOMImage& image) { image.imageChild_->SetFitMaxSize(!StringToBool(val)); } },
93 { DOM_IMAGE_MATCH_TEXT_DIRECTION,
94 [](const std::string& val, DOMImage& image) {
95 image.imageChild_->SetMatchTextDirection(StringToBool(val));
96 } },
97 { DOM_IMAGE_FIT, [](const std::string& val,
98 DOMImage& image) { image.imageChild_->SetImageFit(ConvertStrToFit(val.c_str())); } },
99 { DOM_IMAGE_POSITION, [](const std::string& val, DOMImage& image) {
100 image.imageChild_->SetImageObjectPosition(ImageObjectPosition(val.c_str())); } },
101 };
102 auto operatorIter =
103 BinarySearchFindIndex(imageStylesOperators, ArraySize(imageStylesOperators), style.first.c_str());
104 if (operatorIter != -1) {
105 imageStylesOperators[operatorIter].value(style.second, *this);
106 return true;
107 }
108 return false;
109 }
110
AddSpecializedEvent(int32_t pageId,const std::string & event)111 bool DOMImage::AddSpecializedEvent(int32_t pageId, const std::string& event)
112 {
113 if (event == DOM_COMPLETE) {
114 loadSuccessEvent_ = EventMarker(GetNodeIdForEvent(), event, pageId);
115 imageChild_->SetLoadSuccessEvent(loadSuccessEvent_);
116 } else if (event == DOM_ERROR) {
117 loadFailEvent_ = EventMarker(GetNodeIdForEvent(), event, pageId);
118 imageChild_->SetLoadFailEvent(loadFailEvent_);
119 } else {
120 return false;
121 }
122 return true;
123 }
124
PrepareSpecializedComponent()125 void DOMImage::PrepareSpecializedComponent()
126 {
127 imageChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
128 // If there is a corresponding box decoration, specialize the box.
129 if (boxComponent_) {
130 auto backDecoration = boxComponent_->GetBackDecoration();
131 if (backDecoration) {
132 imageChild_->SetBorder(backDecoration->GetBorder());
133 }
134 }
135 if (flexItemComponent_ && !imageChild_->GetFitMaxSize()) {
136 flexItemComponent_->SetStretchFlag(false);
137 }
138
139 imageChild_->SetImageFill(GetImageFill());
140 }
141
142 } // namespace OHOS::Ace::Framework
143