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