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