1 /*
2  * Copyright (c) 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 "bridge/declarative_frontend/jsview/models/span_model_impl.h"
17 
18 #include <utility>
19 
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/event/ace_event_handler.h"
23 
24 namespace OHOS::Ace::Framework {
Create(const std::string & content)25 void SpanModelImpl::Create(const std::string& content)
26 {
27     auto spanComponent = AceType::MakeRefPtr<TextSpanComponent>(content);
28     ViewStackProcessor::GetInstance()->ClaimElementId(spanComponent);
29     ViewStackProcessor::GetInstance()->Push(spanComponent);
30 
31     // Init text style, allowScale is not supported in declarative.
32     auto textStyle = spanComponent->GetTextStyle();
33     textStyle.SetAllowScale(false);
34     spanComponent->SetTextStyle(textStyle);
35 }
36 
SetFont(const Font & value)37 void SpanModelImpl::SetFont(const Font& value) {}
38 
SetFontSize(const Dimension & value)39 void SpanModelImpl::SetFontSize(const Dimension& value)
40 {
41     auto component = GetComponent();
42     CHECK_NULL_VOID(component);
43 
44     auto textStyle = component->GetTextStyle();
45     textStyle.SetFontSize(value);
46     component->SetTextStyle(textStyle);
47 
48     auto declaration = component->GetDeclaration();
49     if (declaration) {
50         declaration->SetHasSetFontSize(true);
51     }
52 }
53 
SetTextColor(const Color & value)54 void SpanModelImpl::SetTextColor(const Color& value)
55 {
56     auto component = GetComponent();
57     CHECK_NULL_VOID(component);
58     auto textStyle = component->GetTextStyle();
59     textStyle.SetTextColor(value);
60     component->SetTextStyle(textStyle);
61     auto declaration = component->GetDeclaration();
62     if (declaration) {
63         declaration->SetHasSetFontColor(true);
64     }
65 }
66 
SetItalicFontStyle(Ace::FontStyle value)67 void SpanModelImpl::SetItalicFontStyle(Ace::FontStyle value)
68 {
69     auto component = GetComponent();
70     CHECK_NULL_VOID(component);
71     auto textStyle = component->GetTextStyle();
72     textStyle.SetFontStyle(value);
73     component->SetTextStyle(textStyle);
74 
75     auto declaration = component->GetDeclaration();
76     if (declaration) {
77         declaration->SetHasSetFontStyle(true);
78     }
79 }
80 
SetFontWeight(Ace::FontWeight value)81 void SpanModelImpl::SetFontWeight(Ace::FontWeight value)
82 {
83     auto component = GetComponent();
84     CHECK_NULL_VOID(component);
85 
86     auto textStyle = component->GetTextStyle();
87     textStyle.SetFontWeight(value);
88     component->SetTextStyle(textStyle);
89 
90     auto declaration = component->GetDeclaration();
91     if (declaration) {
92         declaration->SetHasSetFontWeight(true);
93     }
94 }
95 
SetFontFamily(const std::vector<std::string> & value)96 void SpanModelImpl::SetFontFamily(const std::vector<std::string>& value)
97 {
98     auto component = GetComponent();
99     CHECK_NULL_VOID(component);
100 
101     auto textStyle = component->GetTextStyle();
102     textStyle.SetFontFamilies(value);
103     component->SetTextStyle(textStyle);
104 
105     auto declaration = component->GetDeclaration();
106     if (declaration) {
107         declaration->SetHasSetFontFamily(true);
108     }
109 }
110 
SetTextDecoration(Ace::TextDecoration value)111 void SpanModelImpl::SetTextDecoration(Ace::TextDecoration value)
112 {
113     auto component = GetComponent();
114     CHECK_NULL_VOID(component);
115     auto textStyle = component->GetTextStyle();
116     textStyle.SetTextDecoration(value);
117     component->SetTextStyle(textStyle);
118 }
119 
SetTextDecorationColor(const Color & value)120 void SpanModelImpl::SetTextDecorationColor(const Color& value)
121 {
122     auto component = GetComponent();
123     CHECK_NULL_VOID(component);
124     auto textStyle = component->GetTextStyle();
125     textStyle.SetTextDecorationColor(value);
126     component->SetTextStyle(textStyle);
127 }
128 
SetTextDecorationStyle(Ace::TextDecorationStyle value)129 void SpanModelImpl::SetTextDecorationStyle(Ace::TextDecorationStyle value)
130 {
131     auto component = GetComponent();
132     CHECK_NULL_VOID(component);
133     auto textStyle = component->GetTextStyle();
134     textStyle.SetTextDecorationStyle(value);
135     component->SetTextStyle(textStyle);
136 }
137 
SetTextCase(Ace::TextCase value)138 void SpanModelImpl::SetTextCase(Ace::TextCase value)
139 {
140     auto component = GetComponent();
141     CHECK_NULL_VOID(component);
142     auto textStyle = component->GetTextStyle();
143     textStyle.SetTextCase(value);
144     component->SetTextStyle(textStyle);
145 
146     auto declaration = component->GetDeclaration();
147     if (declaration) {
148         declaration->SetHasSetTextCase(true);
149     }
150 }
151 
SetLetterSpacing(const Dimension & value)152 void SpanModelImpl::SetLetterSpacing(const Dimension& value)
153 {
154     auto component = GetComponent();
155     CHECK_NULL_VOID(component);
156 
157     auto textStyle = component->GetTextStyle();
158     textStyle.SetLetterSpacing(value);
159     component->SetTextStyle(textStyle);
160 
161     auto declaration = component->GetDeclaration();
162     if (declaration) {
163         declaration->SetHasSetLetterSpacing(true);
164     }
165 }
166 
SetBaselineOffset(const Dimension & value)167 void SpanModelImpl::SetBaselineOffset(const Dimension& value) {}
168 
GetComponent()169 RefPtr<TextSpanComponent> SpanModelImpl::GetComponent()
170 {
171     auto* stack = ViewStackProcessor::GetInstance();
172     return AceType::DynamicCast<TextSpanComponent>(stack->GetMainComponent());
173 }
174 
SetOnClick(std::function<void (BaseEventInfo *)> && click)175 void SpanModelImpl::SetOnClick(std::function<void(BaseEventInfo*)>&& click)
176 {
177     auto component = GetComponent();
178     if (component) {
179         component->SetOnClick(EventMarker(std::move(click)));
180     }
181 }
182 
SetLineHeight(const Dimension & value)183 void SpanModelImpl::SetLineHeight(const Dimension& value) {}
184 
185 } // namespace OHOS::Ace::Framework
186