1 /*
2  * Copyright (c) 2021 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 "core/components/declaration/svg/svg_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 
20 namespace OHOS::Ace {
21 namespace {
22 
23 const char DOM_SVG_SRC_VIEW_BOX[] = "viewBox";
24 
25 } // namespace
26 
27 using namespace Framework;
28 
InitSpecialized()29 void SvgDeclaration::InitSpecialized()
30 {
31     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_ATTR);
32 }
33 
InitializeStyle()34 void SvgDeclaration::InitializeStyle()
35 {
36     SetCurrentStyle(std::make_pair(DOM_OVERFLOW_STYLE, "hidden"));
37     SetHasDisplayStyle(true);
38     // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
39     MaybeResetAttribute<SvgAttribute>(AttributeTag::SPECIALIZED_ATTR);
40 }
41 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)42 bool SvgDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
43 {
44     return SetSpecializedValue(attr);
45 }
46 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)47 bool SvgDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
48 {
49     return SetSpecializedValue(style);
50 }
51 
SetSpecializedValue(const std::pair<std::string,std::string> & attr)52 bool SvgDeclaration::SetSpecializedValue(const std::pair<std::string, std::string>& attr)
53 {
54     static const LinearMapNode<void (*)(const std::string&, SvgDeclaration&)> attrs[] = {
55         { DOM_SVG_MIRROR,
56             [](const std::string& val, SvgDeclaration& declaration) {
57                 declaration.SetAutoMirror(val == "true");
58             } },
59         { DOM_SVG_HEIGHT,
60             [](const std::string& val, SvgDeclaration& declaration) {
61                 declaration.SetHeight(declaration.ParseDimension(val));
62             } },
63         { DOM_SVG_SRC_VIEW_BOX,
64             [](const std::string& val, SvgDeclaration& declaration) {
65                 if (val.empty()) {
66                     return;
67                 }
68                 std::vector<double> viewBox;
69                 StringUtils::StringSplitter(val, ' ', viewBox);
70                 if (viewBox.size() == 4) {
71                     declaration.SetViewBox(Rect(viewBox[0], viewBox[1], viewBox[2], viewBox[3]));
72                 }
73             } },
74         { DOM_SVG_VIEW_BOX,
75             [](const std::string& val, SvgDeclaration& declaration) {
76                 if (val.empty()) {
77                     return;
78                 }
79                 std::vector<double> viewBox;
80                 StringUtils::StringSplitter(val, ' ', viewBox);
81                 if (viewBox.size() == 4) {
82                     declaration.SetViewBox(Rect(viewBox[0], viewBox[1], viewBox[2], viewBox[3]));
83                 }
84             } },
85         { DOM_SVG_WIDTH,
86             [](const std::string& val, SvgDeclaration& declaration) {
87                 declaration.SetWidth(declaration.ParseDimension(val));
88             } },
89         { DOM_SVG_X,
90             [](const std::string& val, SvgDeclaration& declaration) {
91                 declaration.SetX(declaration.ParseDimension(val));
92             } },
93         { DOM_SVG_Y,
94             [](const std::string& val, SvgDeclaration& declaration) {
95                 declaration.SetY(declaration.ParseDimension(val));
96             } },
97     };
98     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), attr.first.c_str());
99     if (attrIter != -1) {
100         attrs[attrIter].value(attr.second, *this);
101         return true;
102     }
103     return SetPresentationAttr(attr);
104 }
105 
106 } // namespace OHOS::Ace