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
RevertLocalPointWithTransform(const RefPtr<FrameNode> & targetNode,OffsetF & point)84 void TextBase::RevertLocalPointWithTransform(const RefPtr<FrameNode>& targetNode, OffsetF& point)
85 {
86 auto pattern = targetNode->GetPattern<Pattern>();
87 CHECK_NULL_VOID(pattern);
88 auto parent = pattern->GetHost();
89 CHECK_NULL_VOID(parent);
90 std::stack<RefPtr<FrameNode>> nodeStack;
91 while (parent) {
92 nodeStack.push(parent);
93 parent = parent->GetAncestorNodeOfFrame(true);
94 }
95 CHECK_NULL_VOID(!nodeStack.empty());
96 PointF localPoint(point.GetX(), point.GetY());
97 while (!nodeStack.empty()) {
98 parent = nodeStack.top();
99 CHECK_NULL_VOID(parent);
100 nodeStack.pop();
101 auto renderContext = parent->GetRenderContext();
102 CHECK_NULL_VOID(renderContext);
103 renderContext->GetPointWithRevert(localPoint);
104 auto rectOffset = renderContext->GetPaintRectWithoutTransform().GetOffset();
105 localPoint = localPoint - rectOffset;
106 }
107 point.SetX(localPoint.GetX());
108 point.SetY(localPoint.GetY());
109 }
110
HasRenderTransform(const RefPtr<FrameNode> & targetNode)111 bool TextBase::HasRenderTransform(const RefPtr<FrameNode>& targetNode)
112 {
113 auto pattern = targetNode->GetPattern<Pattern>();
114 CHECK_NULL_RETURN(pattern, false);
115 auto host = pattern->GetHost();
116 CHECK_NULL_RETURN(host, false);
117 auto hasTransform = false;
118 while (host) {
119 auto renderContext = host->GetRenderContext();
120 CHECK_NULL_RETURN(renderContext, false);
121 if (host->GetTag() == V2::WINDOW_SCENE_ETS_TAG) {
122 break;
123 }
124 if (!hasTransform) {
125 auto noTransformRect = renderContext->GetPaintRectWithoutTransform();
126 auto transformRect = renderContext->GetPaintRectWithTransform();
127 hasTransform = noTransformRect != transformRect;
128 } else {
129 break;
130 }
131 host = host->GetAncestorNodeOfFrame(true);
132 }
133 return hasTransform;
134 }
DoGestureSelection(const TouchEventInfo & info)135 void TextGestureSelector::DoGestureSelection(const TouchEventInfo& info)
136 {
137 if (info.GetTouches().empty()) {
138 return;
139 }
140 auto touchType = info.GetTouches().front().GetTouchType();
141 switch (touchType) {
142 case TouchType::UP:
143 EndGestureSelection();
144 break;
145 case TouchType::MOVE:
146 DoTextSelectionTouchMove(info);
147 break;
148 case TouchType::CANCEL:
149 DoTextSelectionTouchCancel();
150 isStarted_ = false;
151 isSelecting_ = false;
152 break;
153 default:
154 break;
155 }
156 }
157
DoTextSelectionTouchMove(const TouchEventInfo & info)158 void TextGestureSelector::DoTextSelectionTouchMove(const TouchEventInfo& info)
159 {
160 if (!isStarted_ || info.GetTouches().empty()) {
161 return;
162 }
163 auto localOffset = info.GetTouches().front().GetLocalLocation();
164 if (!isSelecting_ && LessOrEqual((localOffset - startOffset_).GetDistance(), minMoveDistance_.ConvertToPx())) {
165 return;
166 }
167 isSelecting_ = true;
168 auto index = GetTouchIndex({ localOffset.GetX(), localOffset.GetY() });
169 auto start = std::min(index, start_);
170 auto end = std::max(index, end_);
171 OnTextGestureSelectionUpdate(start, end, info);
172 }
173 }