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