• 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_pattern_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 using namespace Framework;
25 
InitSpecialized()26 void SvgPatternDeclaration::InitSpecialized()
27 {
28     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_PATTERN_ATTR);
29 }
30 
InitializeStyle()31 void SvgPatternDeclaration::InitializeStyle()
32 {
33     // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
34     MaybeResetAttribute<SvgPatternAttribute>(AttributeTag::SPECIALIZED_ATTR);
35 }
36 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)37 bool SvgPatternDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
38 {
39     static const LinearMapNode<void (*)(const std::string&, SvgPatternDeclaration&)> attrs[] = {
40         { DOM_SVG_HEIGHT,
41             [](const std::string& val, SvgPatternDeclaration& declaration) {
42                 declaration.SetHeight(declaration.ParseDimension(val));
43             } },
44         { DOM_SVG_PATTERN_CONTENT_UNITS,
45             [](const std::string& val, SvgPatternDeclaration& declaration) {
46                 declaration.SetPatternContentUnits(val);
47             } },
48         { DOM_SVG_PATTERN_TRANSFORM,
49             [](const std::string& val, SvgPatternDeclaration& declaration) {
50                 declaration.SetPresentationAttr(std::make_pair(DOM_TRANSFORM, val));
51             } },
52         { DOM_SVG_PATTERN_UNITS,
53             [](const std::string& val, SvgPatternDeclaration& declaration) {
54                 declaration.SetPatternUnits(val);
55             } },
56         { DOM_SVG_VIEW_BOX,
57             [](const std::string& val, SvgPatternDeclaration& declaration) {
58                 if (val.empty()) {
59                     return;
60                 }
61                 std::vector<double> viewBox;
62                 StringUtils::StringSplitter(val, ' ', viewBox);
63                 if (viewBox.size() == 4) {
64                     declaration.SetViewBox(Rect(viewBox[0], viewBox[1], viewBox[2], viewBox[3]));
65                 }
66             } },
67         { DOM_SVG_WIDTH,
68             [](const std::string& val, SvgPatternDeclaration& declaration) {
69                 declaration.SetWidth(declaration.ParseDimension(val));
70             } },
71         { DOM_SVG_X,
72             [](const std::string& val, SvgPatternDeclaration& declaration) {
73                 declaration.SetX(declaration.ParseDimension(val));
74             } },
75         { DOM_SVG_Y,
76             [](const std::string& val, SvgPatternDeclaration& declaration) {
77                 declaration.SetY(declaration.ParseDimension(val));
78             } },
79     };
80 
81     std::string key = attr.first;
82     StringUtils::TransformStrCase(key, StringUtils::TEXT_CASE_LOWERCASE);
83     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), key.c_str());
84     if (attrIter != -1) {
85         attrs[attrIter].value(attr.second, *this);
86         return true;
87     }
88     return SvgBaseDeclaration::SetPresentationAttr(attr);
89 }
90 
91 } // namespace OHOS::Ace