1 /*
2 * Copyright (c) 2023 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 "frameworks/bridge/declarative_frontend/jsview/js_symbol_span.h"
17
18 #include <optional>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22
23 #include "base/geometry/dimension.h"
24 #include "base/utils/utils.h"
25 #include "bridge/common/utils/utils.h"
26 #include "bridge/declarative_frontend/jsview/js_utils.h"
27 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
28 #include "bridge/declarative_frontend/jsview/models/span_model_impl.h"
29 #include "bridge/declarative_frontend/jsview/models/text_model_impl.h"
30 #include "bridge/declarative_frontend/jsview/js_text.h"
31 #include "core/common/container.h"
32 #include "core/components_ng/pattern/text/symbol_span_model.h"
33 #include "core/components_ng/pattern/text/symbol_span_model_ng.h"
34 #include "core/components_ng/pattern/text/text_model.h"
35
36 namespace OHOS::Ace {
37
38 std::unique_ptr<SymbolSpanModel> SymbolSpanModel::instance_ = nullptr;
39 std::mutex SymbolSpanModel::mutex_;
40
GetInstance()41 SymbolSpanModel* SymbolSpanModel::GetInstance()
42 {
43 if (!instance_) {
44 std::lock_guard<std::mutex> lock(mutex_);
45 if (!instance_) {
46 instance_.reset(new NG::SymbolSpanModelNG());
47 }
48 }
49 return instance_.get();
50 }
51
52 } // namespace OHOS::Ace
53
54 namespace OHOS::Ace::Framework {
55
SetFontSize(const JSCallbackInfo & info)56 void JSSymbolSpan::SetFontSize(const JSCallbackInfo& info)
57 {
58 if (info.Length() < 1) {
59 return;
60 }
61 auto theme = GetTheme<TextTheme>();
62 CHECK_NULL_VOID(theme);
63 CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
64 if (!ParseJsDimensionFpNG(info[0], fontSize, false)) {
65 fontSize = theme->GetTextStyle().GetFontSize();
66 SymbolSpanModel::GetInstance()->SetFontSize(fontSize);
67 return;
68 }
69 if (fontSize.IsNegative()) {
70 fontSize = theme->GetTextStyle().GetFontSize();
71 }
72
73 SymbolSpanModel::GetInstance()->SetFontSize(fontSize);
74 }
75
SetFontWeight(const std::string & value)76 void JSSymbolSpan::SetFontWeight(const std::string& value)
77 {
78 SymbolSpanModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(value));
79 }
80
SetFontColor(const JSCallbackInfo & info)81 void JSSymbolSpan::SetFontColor(const JSCallbackInfo& info)
82 {
83 std::vector<Color> symbolColor;
84 if (!ParseJsSymbolColor(info[0], symbolColor)) {
85 return;
86 }
87 SymbolSpanModel::GetInstance()->SetFontColor(symbolColor);
88 }
89
SetSymbolRenderingStrategy(const JSCallbackInfo & info)90 void JSSymbolSpan::SetSymbolRenderingStrategy(const JSCallbackInfo& info)
91 {
92 uint32_t strategy = 0;
93 ParseJsInteger(info[0], strategy);
94 SymbolSpanModel::GetInstance()->SetSymbolRenderingStrategy(strategy);
95 }
96
SetSymbolEffect(const JSCallbackInfo & info)97 void JSSymbolSpan::SetSymbolEffect(const JSCallbackInfo& info)
98 {
99 uint32_t strategy = 0;
100 ParseJsInteger(info[0], strategy);
101 SymbolSpanModel::GetInstance()->SetSymbolEffect(strategy);
102 }
103
Create(const JSCallbackInfo & info)104 void JSSymbolSpan::Create(const JSCallbackInfo& info)
105 {
106 uint32_t symbolId = 0;
107 RefPtr<ResourceObject> resourceObject;
108 if (info.Length() > 0) {
109 ParseJsSymbolId(info[0], symbolId, resourceObject);
110 }
111
112 SymbolSpanModel::GetInstance()->Create(symbolId);
113 }
114
JSBind(BindingTarget globalObj)115 void JSSymbolSpan::JSBind(BindingTarget globalObj)
116 {
117 JSClass<JSSymbolSpan>::Declare("SymbolSpan");
118 MethodOptions opt = MethodOptions::NONE;
119 JSClass<JSSymbolSpan>::StaticMethod("create", &JSSymbolSpan::Create, opt);
120 JSClass<JSSymbolSpan>::StaticMethod("fontSize", &JSSymbolSpan::SetFontSize, opt);
121 JSClass<JSSymbolSpan>::StaticMethod("fontWeight", &JSSymbolSpan::SetFontWeight, opt);
122 JSClass<JSSymbolSpan>::StaticMethod("fontColor", &JSSymbolSpan::SetFontColor, opt);
123 JSClass<JSSymbolSpan>::StaticMethod("renderingStrategy", &JSSymbolSpan::SetSymbolRenderingStrategy, opt);
124 JSClass<JSSymbolSpan>::StaticMethod("effectStrategy", &JSSymbolSpan::SetSymbolEffect, opt);
125 JSClass<JSSymbolSpan>::InheritAndBind<JSContainerBase>(globalObj);
126 }
127 } // namespace OHOS::Ace::Framework
128