1 /* 2 * Copyright (c) 2022-2023 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_NG_PATTERN_TEXT_DRAG_TEXT_DRAG_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_DRAG_TEXT_DRAG_PATTERN_H 18 19 #include "base/memory/referenced.h" 20 #include "core/components_ng/pattern/text_drag/text_drag_base.h" 21 #include "core/components_ng/pattern/text_drag/text_drag_overlay_modifier.h" 22 #include "core/components_ng/pattern/text_drag/text_drag_paint_method.h" 23 #include "core/components_ng/render/drawing.h" 24 25 namespace OHOS::Ace::NG { 26 constexpr Dimension TEXT_DRAG_RADIUS = 12.0_vp; 27 constexpr Dimension TEXT_DRAG_OFFSET = 8.0_vp; 28 constexpr Dimension TEXT_DRAG_MIN_WIDTH = 64.0_vp; 29 constexpr uint32_t TEXT_DRAG_COLOR_BG = 0xf2ffffff; 30 31 struct SelectPositionInfo { SelectPositionInfoSelectPositionInfo32 SelectPositionInfo() {} SelectPositionInfoSelectPositionInfo33 SelectPositionInfo(float startX, float startY, float endX, float endY) 34 : startX_(startX), startY_(startY), endX_(endX), endY_(endY) 35 {} 36 37 float startX_ = 0; 38 float startY_ = 0; 39 float endX_ = 0; 40 float endY_ = 0; 41 }; 42 43 struct TextDragData { TextDragDataTextDragData44 TextDragData() {} TextDragDataTextDragData45 TextDragData(RectF textRect, float frameWidth, float frameHeight, float lineHeight, SelectPositionInfo position, 46 bool oneLineSelected) 47 : textRect_(textRect), frameWidth_(frameWidth), frameHeight_(frameHeight), lineHeight_(lineHeight), 48 selectPosition_(position), oneLineSelected_(oneLineSelected) 49 {} 50 51 RectF textRect_; 52 float frameWidth_ = 0; 53 float frameHeight_ = 0; 54 float lineHeight_ = 0; 55 SelectPositionInfo selectPosition_; 56 bool oneLineSelected_ = false; 57 }; 58 59 struct TextPoint { TextPointTextPoint60 TextPoint() {} TextPointTextPoint61 TextPoint(float x, float y) : x(x), y(y) {} 62 63 float x = 0; 64 float y = 0; 65 }; 66 67 class TextDragPattern : public Pattern { 68 DECLARE_ACE_TYPE(TextDragPattern, Pattern); 69 70 public: 71 TextDragPattern() = default; 72 ~TextDragPattern() override = default; 73 74 static RefPtr<FrameNode> CreateDragNode(const RefPtr<FrameNode>& hostNode); 75 static RefPtr<FrameNode> CreateDragNode( 76 const RefPtr<FrameNode>& hostNode, std::list<RefPtr<FrameNode>>& imageChildren); 77 Initialize(const ParagraphT & paragraph,const TextDragData & data)78 void Initialize(const ParagraphT& paragraph, const TextDragData& data) 79 { 80 paragraph_ = paragraph; 81 textDragData_ = data; 82 } 83 CreateNodePaintMethod()84 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 85 { 86 if (!overlayModifier_) { 87 overlayModifier_ = AceType::MakeRefPtr<TextDragOverlayModifier>(WeakClaim(this)); 88 } 89 return MakeRefPtr<TextDragPaintMethod>(WeakClaim(this), overlayModifier_); 90 } 91 GetParagraph()92 const ParagraphT& GetParagraph() const 93 { 94 return paragraph_; 95 } 96 GetOverlayModifier()97 const RefPtr<TextDragOverlayModifier>& GetOverlayModifier() const 98 { 99 return overlayModifier_; 100 } 101 GetTextRect()102 const RectF& GetTextRect() const 103 { 104 return textDragData_.textRect_; 105 } 106 GetFrameWidth()107 float GetFrameWidth() const 108 { 109 return textDragData_.frameWidth_; 110 } 111 GetFrameHeight()112 float GetFrameHeight() const 113 { 114 return textDragData_.frameHeight_; 115 } 116 GetLineHeight()117 float GetLineHeight() const 118 { 119 return textDragData_.lineHeight_; 120 } 121 GetSelectPosition()122 const SelectPositionInfo& GetSelectPosition() const 123 { 124 return textDragData_.selectPosition_; 125 } 126 OneLineSelected()127 bool OneLineSelected() const 128 { 129 return textDragData_.oneLineSelected_; 130 } 131 GetClipPath()132 const std::shared_ptr<RSPath>& GetClipPath() 133 { 134 if (!clipPath_) { 135 clipPath_ = GenerateClipPath(); 136 } 137 return clipPath_; 138 } 139 GetBackgroundPath()140 const std::shared_ptr<RSPath>& GetBackgroundPath() 141 { 142 if (!backGroundPath_) { 143 backGroundPath_ = GenerateBackgroundPath(TEXT_DRAG_OFFSET.ConvertToPx()); 144 } 145 return backGroundPath_; 146 } 147 148 std::shared_ptr<RSPath> GenerateBackgroundPath(float offset); 149 SetImageChildren(const std::list<RefPtr<FrameNode>> & imageChildren)150 void SetImageChildren(const std::list<RefPtr<FrameNode>>& imageChildren) 151 { 152 imageChildren_ = imageChildren; 153 } 154 GetImageChildren()155 const std::list<RefPtr<FrameNode>>& GetImageChildren() 156 { 157 return imageChildren_; 158 } 159 InitSpanImageLayout(const std::list<RefPtr<FrameNode>> & imageChildren,const std::vector<Rect> & rectsForPlaceholders)160 void InitSpanImageLayout( 161 const std::list<RefPtr<FrameNode>>& imageChildren, const std::vector<Rect>& rectsForPlaceholders) 162 { 163 imageChildren_ = imageChildren; 164 rectsForPlaceholders_ = rectsForPlaceholders; 165 } 166 GetContentOffset()167 OffsetF GetContentOffset() 168 { 169 return contentOffset_; 170 } 171 SetContentOffset(OffsetF contentOffset)172 void SetContentOffset(OffsetF contentOffset) 173 { 174 contentOffset_ = contentOffset; 175 } 176 GetRectsForPlaceholders()177 const std::vector<Rect>& GetRectsForPlaceholders() 178 { 179 return rectsForPlaceholders_; 180 } 181 182 private: SetLastLineHeight(float lineHeight)183 void SetLastLineHeight(float lineHeight) 184 { 185 lastLineHeight_ = lineHeight; 186 } 187 188 static TextDragData CalculateTextDragData(RefPtr<TextDragBase>& hostPattern, RefPtr<FrameNode>& dragContext); 189 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 190 std::shared_ptr<RSPath> GenerateClipPath(); 191 void GenerateBackgroundPoints(std::vector<TextPoint>& points, float offset); 192 void CalculateLineAndArc(std::vector<TextPoint>& points, std::shared_ptr<RSPath>& path); 193 194 float lastLineHeight_ = 0.0f; 195 OffsetF contentOffset_; 196 ParagraphT paragraph_; 197 TextDragData textDragData_; 198 std::shared_ptr<RSPath> clipPath_; 199 std::shared_ptr<RSPath> backGroundPath_; 200 std::list<RefPtr<FrameNode>> imageChildren_; 201 std::vector<Rect> rectsForPlaceholders_; 202 RefPtr<TextDragOverlayModifier> overlayModifier_; 203 204 ACE_DISALLOW_COPY_AND_MOVE(TextDragPattern); 205 }; 206 } // namespace OHOS::Ace::NG 207 208 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_DRAG_TEXT_DRAG_PATTERN_H 209