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 static NG::SymbolSpanModelNG instance;
44 return &instance;
45 }
46
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
50
SetFontSize(const JSCallbackInfo & info)51 void JSSymbolSpan::SetFontSize(const JSCallbackInfo& info)
52 {
53 if (info.Length() < 1) {
54 return;
55 }
56 auto theme = GetTheme<TextTheme>();
57 CHECK_NULL_VOID(theme);
58 CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
59 if (!ParseJsDimensionFpNG(info[0], fontSize, false)) {
60 fontSize = theme->GetTextStyle().GetFontSize();
61 SymbolSpanModel::GetInstance()->SetFontSize(fontSize);
62 return;
63 }
64 if (fontSize.IsNegative()) {
65 fontSize = theme->GetTextStyle().GetFontSize();
66 }
67
68 SymbolSpanModel::GetInstance()->SetFontSize(fontSize);
69 }
70
SetFontWeight(const std::string & value)71 void JSSymbolSpan::SetFontWeight(const std::string& value)
72 {
73 SymbolSpanModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(value));
74 }
75
SetFontColor(const JSCallbackInfo & info)76 void JSSymbolSpan::SetFontColor(const JSCallbackInfo& info)
77 {
78 std::vector<Color> symbolColor;
79 if (!ParseJsSymbolColor(info[0], symbolColor)) {
80 return;
81 }
82 SymbolSpanModel::GetInstance()->SetFontColor(symbolColor);
83 }
84
SetSymbolRenderingStrategy(const JSCallbackInfo & info)85 void JSSymbolSpan::SetSymbolRenderingStrategy(const JSCallbackInfo& info)
86 {
87 uint32_t strategy = 0;
88 ParseJsInteger(info[0], strategy);
89 SymbolSpanModel::GetInstance()->SetSymbolRenderingStrategy(strategy);
90 }
91
SetSymbolEffect(const JSCallbackInfo & info)92 void JSSymbolSpan::SetSymbolEffect(const JSCallbackInfo& info)
93 {
94 uint32_t strategy = 0;
95 ParseJsInteger(info[0], strategy);
96 SymbolSpanModel::GetInstance()->SetSymbolEffect(strategy);
97 }
98
Create(const JSCallbackInfo & info)99 void JSSymbolSpan::Create(const JSCallbackInfo& info)
100 {
101 uint32_t symbolId = 0;
102 RefPtr<ResourceObject> resourceObject;
103 if (info.Length() > 0) {
104 ParseJsSymbolId(info[0], symbolId, resourceObject);
105 }
106
107 SymbolSpanModel::GetInstance()->Create(symbolId);
108 }
109
JSBind(BindingTarget globalObj)110 void JSSymbolSpan::JSBind(BindingTarget globalObj)
111 {
112 JSClass<JSSymbolSpan>::Declare("SymbolSpan");
113 MethodOptions opt = MethodOptions::NONE;
114 JSClass<JSSymbolSpan>::StaticMethod("create", &JSSymbolSpan::Create, opt);
115 JSClass<JSSymbolSpan>::StaticMethod("fontSize", &JSSymbolSpan::SetFontSize, opt);
116 JSClass<JSSymbolSpan>::StaticMethod("fontWeight", &JSSymbolSpan::SetFontWeight, opt);
117 JSClass<JSSymbolSpan>::StaticMethod("fontColor", &JSSymbolSpan::SetFontColor, opt);
118 JSClass<JSSymbolSpan>::StaticMethod("renderingStrategy", &JSSymbolSpan::SetSymbolRenderingStrategy, opt);
119 JSClass<JSSymbolSpan>::StaticMethod("effectStrategy", &JSSymbolSpan::SetSymbolEffect, opt);
120 JSClass<JSSymbolSpan>::InheritAndBind<JSContainerBase>(globalObj);
121 }
122 } // namespace OHOS::Ace::Framework
123