• 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_animate_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 
24 using namespace Framework;
25 
26 using SvgMap = std::unordered_map<std::string, void (*)(const std::string&, SvgAnimateDeclaration&)>;
27 
InitSpecialized()28 void SvgAnimateDeclaration::InitSpecialized()
29 {
30     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_ANIMATE_ATTR);
31 }
32 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)33 bool SvgAnimateDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
34 {
35     return SetAnimateAttr(attr);
36 }
37 
SetAnimateAttr(const std::pair<std::string,std::string> & attr)38 bool SvgAnimateDeclaration::SetAnimateAttr(const std::pair<std::string, std::string>& attr)
39 {
40     static const SvgMap svgMap = {
41         { DOM_SVG_ANIMATION_BEGIN,
42             [](const std::string& val, SvgAnimateDeclaration& declaration) {
43                 declaration.SetBegin(ConvertTimeStr(val));
44             } },
45         { DOM_SVG_ANIMATION_DUR,
46             [](const std::string& val, SvgAnimateDeclaration& declaration) {
47                 if (val == "indefinite") {
48                     declaration.SetDur(0);
49                 } else {
50                     declaration.SetDur(ConvertTimeStr(val));
51                 }
52             } },
53         { DOM_SVG_ANIMATION_END,
54             [](const std::string& val, SvgAnimateDeclaration& declaration) {
55                 declaration.SetEnd(ConvertTimeStr(val));
56             } },
57         { DOM_SVG_ANIMATION_REPEAT_COUNT,
58             [](const std::string& val, SvgAnimateDeclaration& declaration) {
59                 if (val == "indefinite") {
60                     declaration.SetRepeatCount(-1);
61                 } else {
62                     declaration.SetRepeatCount(StringToInt(val));
63                 }
64             } },
65         { DOM_SVG_ANIMATION_FILL,
66             [](const std::string& val, SvgAnimateDeclaration& declaration) {
67                 declaration.SetFillMode(val);
68             } },
69         { DOM_SVG_ANIMATION_CALC_MODE,
70             [](const std::string& val, SvgAnimateDeclaration& declaration) {
71                 declaration.SetCalcMode(val);
72             } },
73         { DOM_SVG_ANIMATION_VALUES,
74             [](const std::string& val, SvgAnimateDeclaration& declaration) {
75                 auto& attrs = declaration.MaybeResetAttribute<SvgAnimateAttribute>(AttributeTag::SPECIALIZED_ATTR);
76                 StringUtils::SplitStr(val, ";", attrs.values);
77             } },
78         { DOM_SVG_ANIMATION_KEY_TIMES,
79             [](const std::string& val, SvgAnimateDeclaration& declaration) {
80                 if (val.empty()) {
81                     return;
82                 }
83                 auto& attrs = declaration.MaybeResetAttribute<SvgAnimateAttribute>(AttributeTag::SPECIALIZED_ATTR);
84                 StringUtils::StringSplitter(val, ';', attrs.keyTimes);
85             } },
86         { DOM_SVG_ANIMATION_KEY_SPLINES,
87             [](const std::string& val, SvgAnimateDeclaration& declaration) {
88                 if (val.empty()) {
89                     return;
90                 }
91                 auto& attrs = declaration.MaybeResetAttribute<SvgAnimateAttribute>(AttributeTag::SPECIALIZED_ATTR);
92                 StringUtils::SplitStr(val, ";", attrs.keySplines);
93             } },
94         { DOM_SVG_ANIMATION_FROM,
95             [](const std::string& val, SvgAnimateDeclaration& declaration) {
96                 declaration.SetFrom(val);
97             } },
98         { DOM_SVG_ANIMATION_TO,
99             [](const std::string& val, SvgAnimateDeclaration& declaration) {
100                 declaration.SetTo(val);
101             } },
102         { DOM_SVG_ANIMATION_ATTRIBUTE_NAME,
103             [](const std::string& val, SvgAnimateDeclaration& declaration) {
104                 declaration.SetAttributeName(val);
105             } },
106         { DOM_SVG_ANIMATION_KEY_POINTS,
107             [](const std::string& val, SvgAnimateDeclaration& declaration) {
108                 if (val.empty()) {
109                     return;
110                 }
111                 auto& attrs = declaration.MaybeResetAttribute<SvgAnimateAttribute>(
112                     AttributeTag::SPECIALIZED_ATTR);
113                 StringUtils::StringSplitter(val, ';', attrs.keyPoints);
114             } },
115         { DOM_SVG_ANIMATION_PATH,
116             [](const std::string& val, SvgAnimateDeclaration& declaration) {
117                 declaration.SetPath(val);
118             } },
119         { DOM_SVG_ANIMATION_ROTATE,
120             [](const std::string& val, SvgAnimateDeclaration& declaration) {
121                 declaration.SetRotate(val);
122             } },
123         { DOM_SVG_ANIMATION_TYPE,
124             [](const std::string& val, SvgAnimateDeclaration& declaration) {
125                 declaration.SetTransformType(val);
126             } },
127     };
128     std::string key = attr.first;
129     // convert key to lower case to match dom_type strings
130     std::transform(key.begin(), key.end(), key.begin(), ::tolower);
131     auto attrIter = svgMap.find(key);
132     if (attrIter != svgMap.end()) {
133         attrIter->second(attr.second, *this);
134         return true;
135     }
136     return false;
137 }
138 
139 } // namespace OHOS::Ace