1 /*
2 * Copyright (c) 2024 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_base.h"
17 #include <cstdint>
18
19 #include "base/utils/utils.h"
20 #include "core/common/container.h"
21 #include "core/components_ng/render/drawing_forward.h"
22 #include "core/pipeline_ng/pipeline_context.h"
23
24 namespace OHOS::Ace::NG {
25
SetSelectionNode(const SelectedByMouseInfo & info)26 void TextBase::SetSelectionNode(const SelectedByMouseInfo& info)
27 {
28 auto pipeline = PipelineContext::GetCurrentContextSafely();
29 CHECK_NULL_VOID(pipeline);
30 auto selectOverlayManager = pipeline->GetSelectOverlayManager();
31 selectOverlayManager->SetSelectedNodeByMouse(info);
32 }
33
GetGraphemeClusterLength(const std::wstring & text,int32_t extend,bool checkPrev)34 int32_t TextBase::GetGraphemeClusterLength(
35 const std::wstring& text, int32_t extend, bool checkPrev)
36 {
37 char16_t aroundChar = 0;
38 if (checkPrev) {
39 if (static_cast<size_t>(extend) <= text.length()) {
40 aroundChar = text[std::max(0, extend - 1)];
41 }
42 } else {
43 if (static_cast<size_t>(extend) <= (text.length())) {
44 aroundChar = text[std::min(text.length() ? static_cast<int32_t>(text.length()) - 1 : 0, extend)];
45 }
46 }
47 return StringUtils::NotInUtf16Bmp(aroundChar) ? 2 : 1;
48 }
49
CalculateSelectedRect(std::vector<RectF> & selectedRect,float longestLine,TextDirection direction)50 void TextBase::CalculateSelectedRect(std::vector<RectF>& selectedRect, float longestLine, TextDirection direction)
51 {
52 if (selectedRect.size() <= 1 || direction == TextDirection::RTL) {
53 return;
54 }
55 std::map<float, RectF> lineGroup;
56 for (const auto& localRect : selectedRect) {
57 if (NearZero(localRect.Width()) && NearZero(localRect.Height())) {
58 continue;
59 }
60 auto it = lineGroup.find(localRect.GetY());
61 if (it == lineGroup.end()) {
62 lineGroup.emplace(localRect.GetY(), localRect);
63 } else {
64 auto lineRect = it->second;
65 it->second = lineRect.CombineRectT(localRect);
66 }
67 }
68 selectedRect.clear();
69 auto firstRect = lineGroup.begin()->second;
70 float lastLineBottom = firstRect.Top();
71 auto end = *(lineGroup.rbegin());
72 for (const auto& line : lineGroup) {
73 if (line == end) {
74 break;
75 }
76 auto rect = RectF(line.second.Left(), lastLineBottom, longestLine - line.second.Left(),
77 line.second.Bottom() - lastLineBottom);
78 selectedRect.emplace_back(rect);
79 lastLineBottom = line.second.Bottom();
80 }
81 selectedRect.emplace_back(RectF(end.second.Left(), lastLineBottom, end.second.Width(), end.second.Height()));
82 }
83
DoGestureSelection(const TouchEventInfo & info)84 void TextGestureSelector::DoGestureSelection(const TouchEventInfo& info)
85 {
86 if (info.GetTouches().empty()) {
87 return;
88 }
89 auto touchType = info.GetTouches().front().GetTouchType();
90 switch (touchType) {
91 case TouchType::UP:
92 EndGestureSelection();
93 break;
94 case TouchType::MOVE:
95 DoTextSelectionTouchMove(info);
96 break;
97 case TouchType::CANCEL:
98 DoTextSelectionTouchCancel();
99 isStarted_ = false;
100 isSelecting_ = false;
101 break;
102 default:
103 break;
104 }
105 }
106
DoTextSelectionTouchMove(const TouchEventInfo & info)107 void TextGestureSelector::DoTextSelectionTouchMove(const TouchEventInfo& info)
108 {
109 if (!isStarted_ || info.GetTouches().empty()) {
110 return;
111 }
112 auto localOffset = info.GetTouches().front().GetLocalLocation();
113 if (!isSelecting_ && LessOrEqual((localOffset - startOffset_).GetDistance(), minMoveDistance_.ConvertToPx())) {
114 return;
115 }
116 isSelecting_ = true;
117 auto index = GetTouchIndex({ localOffset.GetX(), localOffset.GetY() });
118 auto start = std::min(index, start_);
119 auto end = std::max(index, end_);
120 OnTextGestureSelectionUpdate(start, end, info);
121 }
122 }