• 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_text_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 SvgTextDeclaration::InitSpecialized()
27 {
28     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SVG_TEXT_ATTR);
29 }
30 
InitializeStyle()31 void SvgTextDeclaration::InitializeStyle()
32 {
33     // self attribute must be initialized first. Otherwise, may be initialized as a base attribute.
34     MaybeResetAttribute<SvgTextAttribute>(AttributeTag::SPECIALIZED_ATTR);
35 }
36 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)37 bool SvgTextDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
38 {
39     return SetSpecializedValue(attr);
40 }
41 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)42 bool SvgTextDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
43 {
44     return SetSpecializedValue(style);
45 }
46 
SetSpecializedValue(const std::pair<std::string,std::string> & attr)47 bool SvgTextDeclaration::SetSpecializedValue(const std::pair<std::string, std::string>& attr)
48 {
49     static const LinearMapNode<void (*)(const std::string&, SvgTextDeclaration&)> attrs[] = {
50         { DOM_SVG_DX,
51             [](const std::string& val, SvgTextDeclaration& declaration) {
52                 declaration.SetDx(declaration.ParseDimension(val));
53             } },
54         { DOM_SVG_DY,
55             [](const std::string& val, SvgTextDeclaration& declaration) {
56                 declaration.SetDy(declaration.ParseDimension(val));
57             } },
58         { DOM_SVG_ROTATE,
59             [](const std::string& val, SvgTextDeclaration& declaration) {
60                 declaration.SetRotate(declaration.ParseDouble(val));
61                 declaration.hasRotate_ = true;
62             } },
63         { DOM_VALUE,
64             [](const std::string& val, SvgTextDeclaration& declaration) {
65                 declaration.SetTextData(val);
66             } },
67         { DOM_SVG_X,
68             [](const std::string& val, SvgTextDeclaration& declaration) {
69                 declaration.SetX(declaration.ParseDimension(val));
70                 declaration.hasX_ = true;
71             } },
72         { DOM_SVG_Y,
73             [](const std::string& val, SvgTextDeclaration& declaration) {
74                 declaration.SetY(declaration.ParseDimension(val));
75                 declaration.hasY_ = true;
76             } },
77     };
78     auto attrIter = BinarySearchFindIndex(attrs, ArraySize(attrs), attr.first.c_str());
79     if (attrIter != -1) {
80         attrs[attrIter].value(attr.second, *this);
81         return true;
82     }
83     return SvgBaseDeclaration::SetPresentationAttr(attr);
84 }
85 
86 } // namespace OHOS::Ace