1 /*
2 * Copyright (c) 2025 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_ng/pattern/text/text_pattern.h"
17
18 #include <cstdint>
19 #include <future>
20 #include <iterator>
21 #include <stack>
22 #include <string>
23
24 #include "adapter/ohos/capability/clipboard/clipboard_impl.h"
25 #include "base/geometry/ng/offset_t.h"
26 #include "base/geometry/ng/point_t.h"
27 #include "base/geometry/ng/rect_t.h"
28 #include "base/geometry/offset.h"
29 #include "base/log/dump_log.h"
30 #include "base/log/log_wrapper.h"
31 #include "base/utils/multi_thread.h"
32 #include "base/utils/string_utils.h"
33 #include "base/utils/utf_helper.h"
34 #include "base/utils/utils.h"
35 #include "base/window/drag_window.h"
36 #include "core/common/ace_engine_ext.h"
37 #include "core/common/ai/data_detector_mgr.h"
38 #include "core/common/container.h"
39 #include "core/common/container_scope.h"
40 #include "core/common/font_manager.h"
41 #include "core/common/recorder/node_data_cache.h"
42 #include "core/common/udmf/udmf_client.h"
43 #include "core/common/vibrator/vibrator_utils.h"
44 #include "core/components/common/properties/text_style_parser.h"
45 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
46 #include "core/components_ng/pattern/rich_editor_drag/rich_editor_drag_pattern.h"
47 #include "core/components_ng/pattern/text/text_styles.h"
48 #include "core/text/html_utils.h"
49 #include "core/components_ng/pattern/text/paragraph_util.h"
50 #include "core/text/text_emoji_processor.h"
51 #ifdef ENABLE_ROSEN_BACKEND
52 #include "core/components/custom_paint/rosen_render_custom_paint.h"
53 #include "render_service_client/core/ui/rs_ui_director.h"
54 #endif
55
56 namespace OHOS::Ace::NG {
57 namespace {
58 constexpr int32_t API_PROTEXTION_GREATER_NINE = 9;
59 }; // namespace
60
OnAttachToFrameNodeMultiThread()61 void TextPattern::OnAttachToFrameNodeMultiThread()
62 {
63 // nothing, thread unsafe
64 }
65
OnDetachFromFrameNodeMultiThread(FrameNode * node)66 void TextPattern::OnDetachFromFrameNodeMultiThread(FrameNode* node)
67 {
68 // nothing, thread unsafe
69 }
70
OnAttachToMainTreeMultiThread()71 void TextPattern::OnAttachToMainTreeMultiThread()
72 {
73 auto host = GetHost();
74 CHECK_NULL_VOID(host);
75 auto pipeline = host->GetContext();
76 CHECK_NULL_VOID(pipeline);
77 pipeline_ = pipeline;
78 auto fontManager = pipeline->GetFontManager();
79 if (fontManager) {
80 fontManager->AddFontNodeNG(host);
81 }
82 if (Container::LessThanAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
83 if (pipeline->GetMinPlatformVersion() > API_PROTEXTION_GREATER_NINE) {
84 host->GetRenderContext()->UpdateClipEdge(true);
85 host->GetRenderContext()->SetClipToFrame(true);
86 }
87 }
88 InitSurfaceChangedCallback();
89 InitSurfacePositionChangedCallback();
90 pipeline->AddWindowStateChangedCallback(host->GetId());
91 pipeline->AddWindowSizeChangeCallback(host->GetId());
92 auto textLayoutProperty = GetLayoutProperty<TextLayoutProperty>();
93 CHECK_NULL_VOID(textLayoutProperty);
94 auto theme = pipeline->GetTheme<TextTheme>();
95 CHECK_NULL_VOID(theme);
96 textLayoutProperty->UpdateTextAlign(theme->GetTextStyle().GetTextAlign());
97 textLayoutProperty->UpdateAlignment(Alignment::CENTER_LEFT);
98 isDetachFromMainTree_ = false;
99 MultiThreadDelayedExecution(); // Delayed operation
100 }
101
OnDetachFromMainTreeMultiThread()102 void TextPattern::OnDetachFromMainTreeMultiThread()
103 {
104 auto host = GetHost();
105 CHECK_NULL_VOID(host);
106 FrameNode* node = RawPtr(host);
107 CHECK_NULL_VOID(node);
108 if (dataDetectorAdapter_ && dataDetectorAdapter_->aiDetectDelayTask_) {
109 dataDetectorAdapter_->aiDetectDelayTask_.Cancel();
110 }
111 if (selectOverlay_) {
112 CloseSelectOverlay();
113 }
114 auto pipeline = pipeline_.Upgrade();
115 CHECK_NULL_VOID(pipeline);
116 if (HasSurfaceChangedCallback()) {
117 pipeline->UnregisterSurfaceChangedCallback(surfaceChangedCallbackId_.value_or(-1));
118 }
119 if (HasSurfacePositionChangedCallback()) {
120 pipeline->UnregisterSurfacePositionChangedCallback(surfacePositionChangedCallbackId_.value_or(-1));
121 }
122 auto frameNode = WeakClaim(node);
123 pipeline->RemoveFontNodeNG(frameNode);
124 auto fontManager = pipeline->GetFontManager();
125 if (fontManager) {
126 fontManager->UnRegisterCallbackNG(frameNode);
127 fontManager->RemoveVariationNodeNG(frameNode);
128 #ifdef ENABLE_ROSEN_BACKEND
129 if (Rosen::RSUIDirector::IsHybridRenderEnabled()) {
130 fontManager->RemoveHybridRenderNode(frameNode);
131 }
132 #endif
133 }
134 pipeline->RemoveOnAreaChangeNode(node->GetId());
135 pipeline->RemoveWindowStateChangedCallback(node->GetId());
136 pipeline->RemoveVisibleAreaChangeNode(node->GetId());
137 pipeline->RemoveWindowSizeChangeCallback(node->GetId());
138 RemoveFormVisibleChangeCallback(node->GetId());
139 isDetachFromMainTree_ = true;
140 }
141
MultiThreadDelayedExecution()142 void TextPattern::MultiThreadDelayedExecution()
143 {
144 auto host = GetHost();
145 CHECK_NULL_VOID(host);
146 auto pipeline = host->GetContext();
147 CHECK_NULL_VOID(pipeline);
148 if (setTextDetectEnableMultiThread_) {
149 setTextDetectEnableMultiThread_ = false;
150 if (textDetectEnable_) {
151 auto callback = [weak = WeakClaim(this)]() {
152 auto pattern = weak.Upgrade();
153 CHECK_NULL_VOID(pattern);
154 pattern->dataDetectorAdapter_->GetAIEntityMenu();
155 };
156 pipeline->SetConfigChangedCallback(host->GetId(), callback);
157 } else {
158 dataDetectorAdapter_->CancelAITask();
159 }
160 host->MarkDirtyWithOnProChange(PROPERTY_UPDATE_MEASURE);
161 }
162 if (setExternalSpanItemMultiThread_) {
163 setExternalSpanItemMultiThread_ = false;
164 ProcessSpanString();
165 auto layoutProperty = GetLayoutProperty<TextLayoutProperty>();
166 CHECK_NULL_VOID(layoutProperty);
167 layoutProperty->UpdateContent(textForDisplay_);
168 }
169 if (closeSelectOverlayMultiThread_) {
170 closeSelectOverlayMultiThread_ = false;
171 CloseSelectOverlayMultiThreadAction(closeSelectOverlayMultiThreadValue_);
172 }
173 if (setTextSelectionMultiThread_) {
174 setTextSelectionMultiThread_ = false;
175 SetTextSelectionMultiThreadAction(
176 setTextSelectionMultiThreadValue0_, setTextSelectionMultiThreadValue1_);
177 }
178 }
179
SetTextDetectEnableMultiThread(bool enable)180 void TextPattern::SetTextDetectEnableMultiThread(bool enable)
181 {
182 auto host = GetHost();
183 CHECK_NULL_VOID(host);
184 CHECK_NULL_VOID(GetDataDetectorAdapter());
185 dataDetectorAdapter_->frameNode_ = host;
186 if (enable == textDetectEnable_) {
187 return;
188 }
189 textDetectEnable_ = enable;
190 setTextDetectEnableMultiThread_ = true;
191 }
192
SetStyledStringMultiThread(const RefPtr<SpanString> & value,bool closeSelectOverlay)193 void TextPattern::SetStyledStringMultiThread(const RefPtr<SpanString>& value, bool closeSelectOverlay)
194 {
195 auto host = GetHost();
196 CHECK_NULL_VOID(host);
197 AllocStyledString();
198 isSpanStringMode_ = true;
199 host->PostAfterAttachMainTreeTask([
200 spanString = WeakClaim(RawPtr(value)), closeSelectOverlay, weakPtr = WeakClaim(this)]() {
201 const auto& pattern = weakPtr.Upgrade();
202 CHECK_NULL_VOID(pattern);
203 auto value = spanString.Upgrade();
204 CHECK_NULL_VOID(value);
205 auto host = pattern->GetHost();
206 CHECK_NULL_VOID(host);
207 if (closeSelectOverlay) {
208 pattern->CloseSelectOverlay();
209 }
210 auto length = pattern->styledString_->GetLength();
211 pattern->styledString_->RemoveCustomSpan();
212 pattern->styledString_->ReplaceSpanString(0, length, value);
213 pattern->spans_ = pattern->styledString_->GetSpanItems();
214 pattern->ProcessSpanString();
215 pattern->styledString_->AddCustomSpan();
216 pattern->styledString_->SetFramNode(WeakClaim(Referenced::RawPtr(host)));
217 host->MarkDirtyWithOnProChange(PROPERTY_UPDATE_MEASURE);
218 });
219 }
220
SetExternalSpanItemMultiThread(const std::list<RefPtr<SpanItem>> & spans)221 void TextPattern::SetExternalSpanItemMultiThread(const std::list<RefPtr<SpanItem>>& spans)
222 {
223 auto host = GetHost();
224 CHECK_NULL_VOID(host);
225 isSpanStringMode_ = !spans.empty();
226 if (isSpanStringMode_) {
227 AllocStyledString();
228 }
229 spans_ = spans;
230 setExternalSpanItemMultiThread_ = true;
231 }
232
CloseSelectOverlayMultiThread(bool animation)233 void TextPattern::CloseSelectOverlayMultiThread(bool animation)
234 {
235 closeSelectOverlayMultiThread_ = true;
236 closeSelectOverlayMultiThreadValue_ = animation;
237 }
238
CloseSelectOverlayMultiThreadAction(bool animation)239 void TextPattern::CloseSelectOverlayMultiThreadAction(bool animation)
240 {
241 // Deprecated use selectOverlay_ instead.
242 if (selectOverlayProxy_ && !selectOverlayProxy_->IsClosed()) {
243 selectOverlayProxy_->Close(animation);
244 RemoveAreaChangeInner();
245 }
246 selectOverlay_->CloseOverlay(animation, CloseReason::CLOSE_REASON_NORMAL);
247 }
248
SetTextSelectionMultiThread(int32_t selectionStart,int32_t selectionEnd)249 void TextPattern::SetTextSelectionMultiThread(int32_t selectionStart, int32_t selectionEnd)
250 {
251 setTextSelectionMultiThread_ = true;
252 setTextSelectionMultiThreadValue0_ = selectionStart;
253 setTextSelectionMultiThreadValue1_ = selectionEnd;
254 }
255
SetTextSelectionMultiThreadAction(int32_t selectionStart,int32_t selectionEnd)256 void TextPattern::SetTextSelectionMultiThreadAction(int32_t selectionStart, int32_t selectionEnd)
257 {
258 auto host = GetHost();
259 CHECK_NULL_VOID(host);
260 if (SystemProperties::GetTextTraceEnabled()) {
261 ACE_TEXT_SCOPED_TRACE("TextPattern::SetTextSelection[id:%d][selectionStart:%d][selectionStart:%d]",
262 host->GetId(), selectionStart, selectionEnd);
263 }
264 auto eventHub = host->GetEventHub<EventHub>();
265 CHECK_NULL_VOID(eventHub);
266 auto context = host->GetContext();
267 if (context) {
268 context->AddAfterLayoutTask([weak = WeakClaim(this), selectionStart, selectionEnd, eventHub]() {
269 auto textPattern = weak.Upgrade();
270 CHECK_NULL_VOID(textPattern);
271 auto host = textPattern->GetHost();
272 CHECK_NULL_VOID(host);
273 auto geometryNode = host->GetGeometryNode();
274 CHECK_NULL_VOID(geometryNode);
275 auto frameRect = geometryNode->GetFrameRect();
276 if (frameRect.IsEmpty()) {
277 return;
278 }
279 auto textLayoutProperty = textPattern->GetLayoutProperty<TextLayoutProperty>();
280 CHECK_NULL_VOID(textLayoutProperty);
281 auto mode = textLayoutProperty->GetTextSelectableModeValue(TextSelectableMode::SELECTABLE_UNFOCUSABLE);
282 if (mode == TextSelectableMode::UNSELECTABLE ||
283 textLayoutProperty->GetCopyOptionValue(CopyOptions::None) == CopyOptions::None ||
284 textLayoutProperty->GetTextOverflowValue(TextOverflow::CLIP) == TextOverflow::MARQUEE) {
285 return;
286 }
287 if (!textPattern->IsSetObscured() && eventHub->IsEnabled()) {
288 textPattern->ActSetSelection(selectionStart, selectionEnd);
289 }
290 });
291 }
292 host->MarkDirtyWithOnProChange(PROPERTY_UPDATE_MEASURE_SELF);
293 }
294 } // namespace OHOS::Ace::NG
295