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_fe_func_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 
20 namespace OHOS::Ace {
21 
22 using namespace Framework;
23 
InitSpecialized()24 void SvgFeFuncDeclaration::InitSpecialized()
25 {
26     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_FE_FUNC_ATTR);
27 }
28 
InitializeStyle()29 void SvgFeFuncDeclaration::InitializeStyle()
30 {
31     // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
32     MaybeResetAttribute<SvgFeFuncAttribute>(AttributeTag::SPECIALIZED_ATTR);
33 }
34 
SetSpecializedValue(const std::pair<std::string,std::string> & attr)35 bool SvgFeFuncDeclaration::SetSpecializedValue(const std::pair<std::string, std::string>& attr)
36 {
37     static const LinearMapNode<void (*)(const std::string&, SvgFeFuncDeclaration&)> attrs[] = {
38         { DOM_SVG_FE_AMPLITUDE,
39             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
40                 declaration.SetAmplitude(declaration.ParseDouble(val));
41             } },
42         { DOM_SVG_FE_EXPONENT,
43             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
44                 declaration.SetExponent(declaration.ParseDouble(val));
45             } },
46         { DOM_SVG_FE_INTERCEPT,
47             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
48                 declaration.SetIntercept(declaration.ParseDouble(val));
49             } },
50         { DOM_SVG_OFFSET,
51             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
52                 declaration.SetOffset(declaration.ParseDouble(val));
53             } },
54         { DOM_SVG_FE_SLOPE,
55             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
56                 declaration.SetSlope(declaration.ParseDouble(val));
57             } },
58         { DOM_SVG_FE_TABLE_VALUES,
59             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
60                 declaration.SetTableValues(val);
61             } },
62         { DOM_SVG_FE_TYPE,
63             [](const std::string& val, SvgFeFuncDeclaration& declaration) {
64                 declaration.SetType(val);
65             } },
66     };
67     std::string key = attr.first;
68     StringUtils::TransformStrCase(key, StringUtils::TEXT_CASE_LOWERCASE);
69     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), key.c_str());
70     if (attrIter != -1) {
71         attrs[attrIter].value(attr.second, *this);
72         return true;
73     }
74     return SvgFeDeclaration::SetSpecializedValue(attr);
75 }
76 
77 } // namespace OHOS::Ace