1 /* 2 * Copyright (c) 2021 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/components/common/layout/constants.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/common/properties/text_style.h" 23 #include "core/components/text/text_component.h" 24 #include "core/components/text_span/render_text_span.h" 25 #include "core/components/text_span/text_span_component.h" 26 #include "core/gestures/click_recognizer.h" 27 #include "core/gestures/gesture_type.h" 28 #include "core/gestures/long_press_recognizer.h" 29 #include "core/gestures/raw_recognizer.h" 30 #include "core/pipeline/base/render_node.h" 31 32 namespace OHOS::Ace { 33 34 class TextComponent; 35 36 class RenderText : public RenderNode { 37 DECLARE_ACE_TYPE(RenderText, RenderNode); 38 39 public: 40 ~RenderText() override; 41 42 static RefPtr<RenderNode> Create(); 43 44 void Update(const RefPtr<Component>& component) override; 45 46 void PerformLayout() override; 47 48 void OnStatusChanged(RenderStatus renderStatus) override; 49 50 void OnPaintFinish() override; 51 52 Size GetContentSize() override; 53 54 bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict, 55 TouchTestResult& result) override; 56 57 std::string GetTextData() const; 58 void SetTextData(const std::string& textData); 59 SetTextStyle(const TextStyle & textStyle)60 void SetTextStyle(const TextStyle& textStyle) 61 { 62 textStyle_ = textStyle; 63 } GetTextStyle()64 const TextStyle& GetTextStyle() 65 { 66 return textStyle_; 67 } 68 MarkNeedMeasure()69 void MarkNeedMeasure() 70 { 71 needMeasure_ = true; 72 } 73 74 virtual double GetTextWidth() = 0; 75 76 RefPtr<Component> GetComponent() override; 77 78 void Dump() override; 79 SetParagraphWidth(double paragraphWidth)80 void SetParagraphWidth(double paragraphWidth) 81 { 82 paragraphWidth_ = paragraphWidth; 83 } 84 GetParagraphWidth()85 double GetParagraphWidth() const 86 { 87 return paragraphWidth_; 88 } 89 SetParagraphHeight(double paragraphHeight)90 void SetParagraphHeight(double paragraphHeight) 91 { 92 paragraphHeight_ = paragraphHeight; 93 } 94 GetParagraphHeight()95 double GetParagraphHeight() const 96 { 97 return paragraphHeight_; 98 } 99 100 protected: 101 void OnTouchTestHit( 102 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 103 104 virtual Size Measure() = 0; 105 virtual uint32_t GetTextLines() = 0; 106 virtual int32_t GetTouchPosition(const Offset& offset) = 0; 107 void UpdateAccessibilityText(); 108 109 template<class T> UpdateIfChanged(T & update,const T & val)110 void UpdateIfChanged(T& update, const T& val) 111 { 112 if (update != val) { 113 needMeasure_ = true; 114 update = val; 115 } 116 } 117 118 void CheckIfNeedMeasure(); 119 void ClearRenderObject() override; 120 TextStyle textStyle_; 121 TextDirection textDirection_ = TextDirection::LTR; 122 Color focusColor_; 123 Color lostFocusColor_; 124 double fontScale_ = 1.0; 125 double dipScale_ = 1.0; 126 bool isFocus_ = false; 127 bool needMeasure_ = true; 128 bool isCallbackCalled_ = false; 129 uint32_t maxLines_ = UINT32_MAX; 130 RefPtr<TextComponent> text_; 131 std::map<int32_t, std::map<GestureType, EventMarker>> touchRegions_; // key of map is end position of span. 132 double paragraphWidth_ = 0.0; 133 double paragraphHeight_ = 0.0; 134 135 private: 136 void HandleTouchEvent(GestureType type, const Offset& touchPosition); 137 void HandleClick(const ClickInfo& info); 138 void HandleRemoteMessage(const ClickInfo& info); 139 void HandleLongPress(const Offset& longPressPosition); 140 EventMarker GetEventMarker(int32_t position, GestureType type); 141 void FireEvent(const EventMarker& marker); 142 143 bool needClickDetector_ = false; 144 bool needLongPressDetector_ = false; 145 bool needTouchDetector_ = false; 146 int32_t touchStartPosition_ = 0; 147 RefPtr<RawRecognizer> rawRecognizer_; 148 RefPtr<ClickRecognizer> clickDetector_; 149 RefPtr<LongPressRecognizer> longPressRecognizer_; 150 }; 151 152 } // namespace OHOS::Ace 153 154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_RENDER_TEXT_H 155