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_stop_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 
20 namespace OHOS::Ace {
21 
22 namespace {
23 
24 const char DOM_SVG_SRC_STOP_COLOR[] = "stop-color";
25 const char DOM_SVG_SRC_STOP_OPACITY[] = "stop-opacity";
26 
27 } // namespace
28 
29 using namespace Framework;
30 
InitSpecialized()31 void SvgStopDeclaration::InitSpecialized()
32 {
33     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_STOP_ATTR);
34 }
35 
InitializeStyle()36 void SvgStopDeclaration::InitializeStyle()
37 {
38     // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
39     auto& attribute = MaybeResetAttribute<SvgStopAttribute>(AttributeTag::SPECIALIZED_ATTR);
40     attribute.gradientColor.SetDimension(0.0);
41 }
42 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)43 bool SvgStopDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
44 {
45     return SetSpecializedValue(attr);
46 }
47 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)48 bool SvgStopDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
49 {
50     return SetSpecializedValue(style);
51 }
52 
SetSpecializedValue(const std::pair<std::string,std::string> & attr)53 bool SvgStopDeclaration::SetSpecializedValue(const std::pair<std::string, std::string>& attr)
54 {
55     static const LinearMapNode<void (*)(const std::string&, SvgStopDeclaration&)> attrs[] = {
56         { DOM_SVG_OFFSET,
57             [](const std::string& val, SvgStopDeclaration& declaration) {
58                 declaration.SetOffset(declaration.ParseDimension(val));
59             } },
60         { DOM_SVG_SRC_STOP_COLOR,
61             [](const std::string& val, SvgStopDeclaration& declaration) {
62                 Color color = (val == VALUE_NONE ? Color::TRANSPARENT : declaration.GetColor(val));
63                 declaration.SetColor(color);
64             } },
65         { DOM_SVG_SRC_STOP_OPACITY,
66             [](const std::string& val, SvgStopDeclaration& declaration) {
67                 declaration.SetOpacity(declaration.ParseDouble(val));
68             } },
69         { DOM_SVG_STOP_COLOR,
70             [](const std::string& val, SvgStopDeclaration& declaration) {
71                 Color color = (val == VALUE_NONE ? Color::TRANSPARENT : declaration.GetColor(val));
72                 declaration.SetColor(color);
73             } },
74         { DOM_SVG_STOP_OPACITY,
75             [](const std::string& val, SvgStopDeclaration& declaration) {
76                 declaration.SetOpacity(declaration.ParseDouble(val));
77             } },
78     };
79     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), attr.first.c_str());
80     if (attrIter != -1) {
81         attrs[attrIter].value(attr.second, *this);
82         return true;
83     }
84     return false;
85 }
86 
87 } // namespace OHOS::Ace
88