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_gradient_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_GRADIENT_TRANSFORM[] = "gradientTransform";
25 const char DOM_SVG_SRC_SPREAD_METHOD[] = "spreadMethod";
26
27 } // namespace
28
29 using namespace Framework;
30
InitSpecialized()31 void SvgGradientDeclaration::InitSpecialized()
32 {
33 AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_GRADIENT_ATTR);
34 }
35
InitializeStyle()36 void SvgGradientDeclaration::InitializeStyle()
37 {
38 // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
39 MaybeResetAttribute<SvgGradientAttribute>(AttributeTag::SPECIALIZED_ATTR);
40 }
41
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)42 bool SvgGradientDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
43 {
44 return SetSpecializedValue(attr);
45 }
46
SetSpecializedStyle(const std::pair<std::string,std::string> & style)47 bool SvgGradientDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
48 {
49 return SetSpecializedValue(style);
50 }
51
SetSpecializedValue(const std::pair<std::string,std::string> & attr)52 bool SvgGradientDeclaration::SetSpecializedValue(const std::pair<std::string, std::string>& attr)
53 {
54 static const LinearMapNode<void (*)(const std::string&, SvgGradientDeclaration&)> attrs[] = {
55 { DOM_SVG_CX,
56 [](const std::string& val, SvgGradientDeclaration& declaration) {
57 declaration.SetCx(declaration.ParseDimension(val));
58 } },
59 { DOM_SVG_CY,
60 [](const std::string& val, SvgGradientDeclaration& declaration) {
61 declaration.SetCy(declaration.ParseDimension(val));
62 } },
63 { DOM_SVG_FX,
64 [](const std::string& val, SvgGradientDeclaration& declaration) {
65 declaration.SetFx(declaration.ParseDimension(val));
66 } },
67 { DOM_SVG_FY,
68 [](const std::string& val, SvgGradientDeclaration& declaration) {
69 declaration.SetFy(declaration.ParseDimension(val));
70 } },
71 { DOM_SVG_SRC_GRADIENT_TRANSFORM,
72 [](const std::string& val, SvgGradientDeclaration& declaration) {
73 declaration.SetTransform(val);
74 } },
75 { DOM_SVG_GRADIENT_TRANSFORM,
76 [](const std::string& val, SvgGradientDeclaration& declaration) {
77 declaration.SetTransform(val);
78 } },
79 { DOM_SVG_R,
80 [](const std::string& val, SvgGradientDeclaration& declaration) {
81 declaration.SetR(declaration.ParseDimension(val));
82 } },
83 { DOM_SVG_SRC_SPREAD_METHOD,
84 [](const std::string& val, SvgGradientDeclaration& declaration) {
85 if (val == "pad") {
86 declaration.SetSpreadMethod(SpreadMethod::PAD);
87 }
88 if (val == "reflect") {
89 declaration.SetSpreadMethod(SpreadMethod::REFLECT);
90 }
91 if (val == "repeat") {
92 declaration.SetSpreadMethod(SpreadMethod::REPEAT);
93 }
94 } },
95 { DOM_SVG_SPREAD_METHOD,
96 [](const std::string& val, SvgGradientDeclaration& declaration) {
97 if (val == "pad") {
98 declaration.SetSpreadMethod(SpreadMethod::PAD);
99 }
100 if (val == "reflect") {
101 declaration.SetSpreadMethod(SpreadMethod::REFLECT);
102 }
103 if (val == "repeat") {
104 declaration.SetSpreadMethod(SpreadMethod::REPEAT);
105 }
106 } },
107 { DOM_SVG_X1,
108 [](const std::string& val, SvgGradientDeclaration& declaration) {
109 declaration.SetX1(declaration.ParseDimension(val));
110 } },
111 { DOM_SVG_X2,
112 [](const std::string& val, SvgGradientDeclaration& declaration) {
113 declaration.SetX2(declaration.ParseDimension(val));
114 } },
115 { DOM_SVG_Y1,
116 [](const std::string& val, SvgGradientDeclaration& declaration) {
117 declaration.SetY1(declaration.ParseDimension(val));
118 } },
119 { DOM_SVG_Y2,
120 [](const std::string& val, SvgGradientDeclaration& declaration) {
121 declaration.SetY2(declaration.ParseDimension(val));
122 } },
123 };
124 auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), attr.first.c_str());
125 if (attrIter != -1) {
126 attrs[attrIter].value(attr.second, *this);
127 return true;
128 }
129 return false;
130 }
131
132 } // namespace OHOS::Ace
133