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_rect_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 SvgRectDeclaration::InitSpecialized()
25 {
26     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_RECT_ATTR);
27 }
28 
InitializeStyle()29 void SvgRectDeclaration::InitializeStyle()
30 {
31     // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
32     MaybeResetAttribute<SvgRectAttribute>(AttributeTag::SPECIALIZED_ATTR);
33 }
34 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)35 bool SvgRectDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
36 {
37     return SetSpecializedValue(attr);
38 }
39 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)40 bool SvgRectDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
41 {
42     return SetSpecializedValue(style);
43 }
44 
SetSpecializedValue(const std::pair<std::string,std::string> & attr)45 bool SvgRectDeclaration::SetSpecializedValue(const std::pair<std::string, std::string>& attr)
46 {
47     static const LinearMapNode<void (*)(const std::string&, SvgRectDeclaration&)> attrs[] = {
48         { DOM_SVG_HEIGHT,
49             [](const std::string& val, SvgRectDeclaration& declaration) {
50                 declaration.SetHeight(declaration.ParseDimension(val));
51             } },
52         { DOM_SVG_RX,
53             [](const std::string& val, SvgRectDeclaration& declaration) {
54                 declaration.SetRx(declaration.ParseDimension(val));
55             } },
56         { DOM_SVG_RY,
57             [](const std::string& val, SvgRectDeclaration& declaration) {
58                 declaration.SetRy(declaration.ParseDimension(val));
59             } },
60         { DOM_SVG_WIDTH,
61             [](const std::string& val, SvgRectDeclaration& declaration) {
62                 declaration.SetWidth(declaration.ParseDimension(val));
63             } },
64         { DOM_SVG_X,
65             [](const std::string& val, SvgRectDeclaration& declaration) {
66                 declaration.SetX(declaration.ParseDimension(val));
67             } },
68         { DOM_SVG_Y,
69             [](const std::string& val, SvgRectDeclaration& declaration) {
70                 declaration.SetY(declaration.ParseDimension(val));
71             } },
72     };
73     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), attr.first.c_str());
74     if (attrIter != -1) {
75         attrs[attrIter].value(attr.second, *this);
76         return true;
77     }
78     return SetPresentationAttr(attr);
79 }
80 
81 } // namespace OHOS::Ace