1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_RENDER_TEXT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_RENDER_TEXT_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/common/clipboard/clipboard.h"
21 #include "core/components/box/drag_drop_event.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components/common/properties/text_style.h"
25 #include "core/components/text/text_component.h"
26 #include "core/components/text_overlay/text_overlay_manager.h"
27 #include "core/components/text_span/render_text_span.h"
28 #include "core/components/text_span/text_span_component.h"
29 #include "core/gestures/click_recognizer.h"
30 #include "core/gestures/gesture_type.h"
31 #include "core/gestures/long_press_recognizer.h"
32 #include "core/gestures/raw_recognizer.h"
33 #include "core/pipeline/base/render_node.h"
34 
35 namespace OHOS::Ace {
36 
37 class TextComponent;
38 
39 class RenderText : public RenderNode, public TextOverlayBase, public DragDropEvent {
40     DECLARE_ACE_TYPE(RenderText, RenderNode, TextOverlayBase, DragDropEvent);
41 
42 public:
43     ~RenderText() override;
44 
45     static RefPtr<RenderNode> Create();
46 
47     void Update(const RefPtr<Component>& component) override;
48 
49     void PerformLayout() override;
50 
51     void OnStatusChanged(RenderStatus renderStatus) override;
52 
53     void OnPaintFinish() override;
54 
55     Size GetContentSize() override;
56 
57     bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict,
58         TouchTestResult& result) override;
59 
60     std::string GetTextData() const;
61     void SetTextData(const std::string& textData);
62 
SetTextStyle(const TextStyle & textStyle)63     void SetTextStyle(const TextStyle& textStyle)
64     {
65         textStyle_ = textStyle;
66     }
GetTextStyle()67     const TextStyle& GetTextStyle()
68     {
69         return textStyle_;
70     }
71 
MarkNeedMeasure()72     void MarkNeedMeasure()
73     {
74         needMeasure_ = true;
75     }
76 
77     virtual double GetTextWidth() = 0;
78 
79     RefPtr<Component> GetComponent() override;
80 
81     void Dump() override;
82 
SetParagraphWidth(double paragraphWidth)83     void SetParagraphWidth(double paragraphWidth)
84     {
85         paragraphWidth_ = paragraphWidth;
86     }
87 
GetParagraphWidth()88     double GetParagraphWidth() const
89     {
90         return paragraphWidth_;
91     }
92 
SetParagraphHeight(double paragraphHeight)93     void SetParagraphHeight(double paragraphHeight)
94     {
95         paragraphHeight_ = paragraphHeight;
96     }
97 
GetParagraphHeight()98     double GetParagraphHeight() const
99     {
100         return paragraphHeight_;
101     }
102 
103     void OnLongPress(const LongPressInfo& longPressInfo);
104     void ShowTextOverlay(const Offset& showOffset) override;
105     void ShowTextOverlay(const Offset& showOffset, bool isUsingMouse);
106     bool HandleMouseEvent(const MouseEvent& event) override;
107     void RegisterCallbacksToOverlay() override;
GetTextSelect()108     const TextSelection GetTextSelect() const
109     {
110         return textValue_.selection;
111     }
112 
113     std::string GetSelectedContent() const override;
GetTextValue()114     const TextEditingValue GetTextValue() const
115     {
116         return textValue_;
117     }
118 
GetStartOffset()119     const Offset& GetStartOffset() const
120     {
121         return startOffset_;
122     }
123 
SetStartOffset(const Offset & startOffset)124     void SetStartOffset(const Offset& startOffset)
125     {
126         startOffset_ = startOffset;
127     }
128 
GetEndOffset()129     const Offset& GetEndOffset() const
130     {
131         return endOffset_;
132     }
133 
SetEndOffset(const Offset & endOffset)134     void SetEndOffset(const Offset& endOffset)
135     {
136         endOffset_ = endOffset;
137     }
138 
139 protected:
140     void OnTouchTestHit(
141         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
142 
143     virtual Size Measure() = 0;
144     virtual uint32_t GetTextLines() = 0;
145     virtual int32_t GetTouchPosition(const Offset& offset) = 0;
146     void UpdateAccessibilityText();
147 
148     template<class T>
UpdateIfChanged(T & update,const T & val)149     void UpdateIfChanged(T& update, const T& val)
150     {
151         if (update != val) {
152             needMeasure_ = true;
153             update = val;
154         }
155     }
156 
157     void CheckIfNeedMeasure();
158     void ClearRenderObject() override;
159     TextStyle textStyle_;
160     Color focusColor_;
161     Color lostFocusColor_;
162     double fontScale_ = 1.0;
163     double dipScale_ = 1.0;
164     bool isFocus_ = false;
165     bool needMeasure_ = true;
166     bool isCallbackCalled_ = false;
167     uint32_t maxLines_ = UINT32_MAX;
168     RefPtr<TextComponent> text_;
169     std::map<int32_t, std::map<GestureType, EventMarker>> touchRegions_; // key of map is end position of span.
170     double paragraphWidth_ = 0.0;
171     double paragraphHeight_ = 0.0;
172     std::optional<TextAlign> alignment_;
173     bool isCustomFont_ = false;
174 
175 private:
176     bool IsDeclarativePara();
177     void HandleTouchEvent(GestureType type, const Offset& touchPosition);
178     void HandleClick(const ClickInfo& info);
179     void HandleRemoteMessage(const ClickInfo& info);
180     void HandleLongPress(const Offset& longPressPosition);
181     EventMarker GetEventMarker(int32_t position, GestureType type);
182     void FireEvent(const EventMarker& marker);
183     void UpdateOverlay();
184     Offset GetPositionForExtend(int32_t extend);
185     void HandleOnCopy();
186     void HandleOnCopyAll(const std::function<void(const Offset&, const Offset&)>& callback);
187     void HandleOnStartHandleMove(int32_t end, const Offset& startHandleOffset,
188         const std::function<void(const Offset&)>& startCallback, bool isSingleHandle = false);
189     void HandleOnEndHandleMove(
190         int32_t start, const Offset& endHandleOffset, const std::function<void(const Offset&)>& endCallback);
191     void HideTextOverlay();
192     void UpdateTextOverlay();
193     // Drag event
194     void PanOnActionStart(const GestureEvent& info) override;
195     void PanOnActionUpdate(const GestureEvent& info) override;
196     void PanOnActionEnd(const GestureEvent& info) override;
197     void PanOnActionCancel() override;
198     DragItemInfo GenerateDragItemInfo(const RefPtr<PipelineContext>& context, const GestureEvent& info) override;
199     void CreateSelectRecognizer();
200 
201     bool needClickDetector_ = false;
202     bool needLongPressDetector_ = false;
203     bool needTouchDetector_ = false;
204     int32_t touchStartPosition_ = 0;
205     RefPtr<RawRecognizer> rawRecognizer_;
206     RefPtr<ClickRecognizer> clickDetector_;
207     RefPtr<LongPressRecognizer> longPressRecognizer_;
208     RefPtr<LongPressRecognizer> textOverlayRecognizer_;
209     RefPtr<ClickRecognizer> hideTextOverlayRecognizer_;
210     RefPtr<PanRecognizer> selectRecognizer_;
211     RefPtr<Clipboard> clipboard_;
212     CopyOptions copyOption_ = CopyOptions::None;
213     Offset startOffset_;
214     Offset endOffset_;
215     Offset lastDragMoveOffset_;
216 };
217 
218 } // namespace OHOS::Ace
219 
220 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_RENDER_TEXT_H
221