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