• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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/span/span_declaration.h"
17 
18 #include "base/utils/string_utils.h"
19 #include "core/components/common/properties/text_style_parser.h"
20 #include "core/components/declaration/common/declaration_constants.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 
23 namespace OHOS::Ace {
24 namespace {
25 
26 constexpr Dimension DEFAULT_FONT_SIZE = 14.0_px;
27 
28 } // namespace
29 
30 using namespace Framework;
31 
InitCommonEvent()32 void SpanDeclaration::InitCommonEvent()
33 {
34     AddCommonEvent(EventTag::COMMON_RAW_EVENT);
35     AddCommonEvent(EventTag::COMMON_GESTURE_EVENT);
36     AddCommonEvent(EventTag::COMMON_REMOTE_MESSAGE_GESTURE_EVENT);
37 }
38 
InitSpecialized()39 void SpanDeclaration::InitSpecialized()
40 {
41     AddSpecializedAttribute(DeclarationConstants::DEFAULT_SPAN_ATTR);
42     AddSpecializedStyle(DeclarationConstants::DEFAULT_SPAN_STYLE);
43 }
44 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)45 bool SpanDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
46 {
47     static const LinearMapNode<void (*)(const std::string&, SpanDeclaration&)> spanStyleOperators[] = {
48         { DOM_SHOW,
49             [](const std::string& val, SpanDeclaration& declaration) { declaration.SetIsShow(StringToBool(val)); } },
50         { DOM_VALUE, [](const std::string& val, SpanDeclaration& declaration) { declaration.SetSpanData(val); } },
51     };
52     auto spanStyleIter = BinarySearchFindIndex(spanStyleOperators, ArraySize(spanStyleOperators), attr.first.c_str());
53     if (spanStyleIter != -1) {
54         spanStyleOperators[spanStyleIter].value(attr.second, *this);
55         return true;
56     }
57     return false;
58 }
59 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)60 bool SpanDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
61 {
62     static const LinearMapNode<void (*)(const std::string&, SpanDeclaration&)> spanStyleOperators[] = {
63         { DOM_TEXT_ALLOW_SCALE,
64             [](const std::string& val, SpanDeclaration& declaration) {
65                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
66                 if (specializedStyle.IsValid()) {
67                     specializedStyle.spanStyle.SetAllowScale(StringToBool(val));
68                 }
69                 declaration.isSetAllowScale_ = true;
70             } },
71         { DOM_TEXT_COLOR,
72             [](const std::string& val, SpanDeclaration& declaration) {
73                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
74                 if (specializedStyle.IsValid()) {
75                     auto textColor = val.empty() ? Color::BLACK : declaration.ParseColor(val);
76                     specializedStyle.spanStyle.SetTextColor(textColor);
77                 }
78                 declaration.isSetFontColor_ = true;
79             } },
80         { DOM_TEXT_FONT_FAMILY,
81             [](const std::string& val, SpanDeclaration& declaration) {
82                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
83                 if (specializedStyle.IsValid()) {
84                     specializedStyle.spanStyle.SetFontFamilies(declaration.ParseFontFamilies(val));
85                 }
86                 declaration.isSetFontFamily_ = true;
87             } },
88         { DOM_TEXT_FONT_FEATURE_SETTINGS,
89             [](const std::string& val, SpanDeclaration& declaration) {
90                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
91                 if (specializedStyle.IsValid()) {
92                     specializedStyle.spanStyle.SetFontFeatures(ParseFontFeatureSettings(val));
93                 }
94                 declaration.isSetFontFeatures_ = true;
95             } },
96         { DOM_TEXT_FONT_SIZE,
97             [](const std::string& val, SpanDeclaration& declaration) {
98                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
99                 if (specializedStyle.IsValid()) {
100                     auto fontSize = val.empty() ? DEFAULT_FONT_SIZE : declaration.ParseDimension(val);
101                     specializedStyle.spanStyle.SetFontSize(fontSize);
102                 }
103                 declaration.isSetFontSize_ = true;
104             } },
105         { DOM_TEXT_FONT_STYLE,
106             [](const std::string& val, SpanDeclaration& declaration) {
107                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
108                 if (specializedStyle.IsValid()) {
109                     specializedStyle.spanStyle.SetFontStyle(ConvertStrToFontStyle(val));
110                 }
111                 declaration.isSetFontStyle_ = true;
112             } },
113         { DOM_TEXT_FONT_VARIANT,
114             [](const std::string& val, SpanDeclaration& declaration) {
115                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
116                 if (specializedStyle.IsValid()) {
117                     specializedStyle.spanStyle.SetFontFeatures(ParseFontVariants(val));
118                 }
119                 declaration.isSetFontFeatures_ = true;
120             } },
121         { DOM_TEXT_FONT_WEIGHT,
122             [](const std::string& val, SpanDeclaration& declaration) {
123                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
124                 if (specializedStyle.IsValid()) {
125                     specializedStyle.spanStyle.SetFontWeight(ConvertStrToFontWeight(val));
126                 }
127                 declaration.isSetFontWeight_ = true;
128             } },
129         { DOM_TEXT_DECORATION,
130             [](const std::string& val, SpanDeclaration& declaration) {
131                 auto& specializedStyle = declaration.MaybeResetStyle<SpanStyle>(StyleTag::SPECIALIZED_STYLE);
132                 if (specializedStyle.IsValid()) {
133                     specializedStyle.spanStyle.SetTextDecoration(ConvertStrToTextDecoration(val));
134                 }
135                 declaration.isSetTextDecoration_ = true;
136             } },
137     };
138     auto spanStyleIter = BinarySearchFindIndex(spanStyleOperators, ArraySize(spanStyleOperators), style.first.c_str());
139     if (spanStyleIter != -1) {
140         spanStyleOperators[spanStyleIter].value(style.second, *this);
141     }
142 
143     // span has no box component, set true to prevent setting style to box (empty object).
144     return true;
145 }
146 
147 } // namespace OHOS::Ace
148