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