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 Initialize(const ParagraphT & paragraph,const TextDragData & data)76 void Initialize(const ParagraphT& paragraph, const TextDragData& data) 77 { 78 paragraph_ = paragraph; 79 textDragData_ = data; 80 } 81 CreateNodePaintMethod()82 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 83 { 84 if (!overlayModifier_) { 85 overlayModifier_ = AceType::MakeRefPtr<TextDragOverlayModifier>(WeakClaim(this)); 86 } 87 return MakeRefPtr<TextDragPaintMethod>(WeakClaim(this), overlayModifier_); 88 } 89 GetParagraph()90 const ParagraphT& GetParagraph() const 91 { 92 return paragraph_; 93 } 94 GetOverlayModifier()95 virtual const RefPtr<TextDragOverlayModifier>& GetOverlayModifier() const 96 { 97 return overlayModifier_; 98 } 99 GetTextRect()100 const RectF& GetTextRect() const 101 { 102 return textDragData_.textRect_; 103 } 104 GetFrameWidth()105 float GetFrameWidth() const 106 { 107 return textDragData_.frameWidth_; 108 } 109 GetFrameHeight()110 float GetFrameHeight() const 111 { 112 return textDragData_.frameHeight_; 113 } 114 GetLineHeight()115 float GetLineHeight() const 116 { 117 return textDragData_.lineHeight_; 118 } 119 GetSelectPosition()120 const SelectPositionInfo& GetSelectPosition() const 121 { 122 return textDragData_.selectPosition_; 123 } 124 OneLineSelected()125 bool OneLineSelected() const 126 { 127 return textDragData_.oneLineSelected_; 128 } 129 GetClipPath()130 const std::shared_ptr<RSPath>& GetClipPath() 131 { 132 if (!clipPath_) { 133 clipPath_ = GenerateClipPath(); 134 } 135 return clipPath_; 136 } 137 GetBackgroundPath()138 const std::shared_ptr<RSPath>& GetBackgroundPath() 139 { 140 if (!backGroundPath_) { 141 backGroundPath_ = GenerateBackgroundPath(TEXT_DRAG_OFFSET.ConvertToPx()); 142 } 143 return backGroundPath_; 144 } 145 146 std::shared_ptr<RSPath> GenerateBackgroundPath(float offset); 147 SetImageChildren(const std::list<RefPtr<FrameNode>> & imageChildren)148 void SetImageChildren(const std::list<RefPtr<FrameNode>>& imageChildren) 149 { 150 imageChildren_ = imageChildren; 151 } 152 GetImageChildren()153 const std::list<RefPtr<FrameNode>>& GetImageChildren() 154 { 155 return imageChildren_; 156 } 157 InitSpanImageLayout(const std::list<RefPtr<FrameNode>> & imageChildren,const std::vector<RectF> & rectsForPlaceholders)158 void InitSpanImageLayout( 159 const std::list<RefPtr<FrameNode>>& imageChildren, const std::vector<RectF>& rectsForPlaceholders) 160 { 161 imageChildren_ = imageChildren; 162 rectsForPlaceholders_ = rectsForPlaceholders; 163 } 164 GetContentOffset()165 OffsetF GetContentOffset() 166 { 167 return contentOffset_; 168 } 169 SetContentOffset(OffsetF contentOffset)170 void SetContentOffset(OffsetF contentOffset) 171 { 172 contentOffset_ = contentOffset; 173 } 174 GetRectsForPlaceholders()175 const std::vector<RectF>& GetRectsForPlaceholders() 176 { 177 return rectsForPlaceholders_; 178 } 179 180 protected: 181 static TextDragData CalculateTextDragData(RefPtr<TextDragBase>& hostPattern, RefPtr<FrameNode>& dragContext); 182 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 183 std::shared_ptr<RSPath> GenerateClipPath(); 184 void GenerateBackgroundPoints(std::vector<TextPoint>& points, float offset); 185 void CalculateLineAndArc(std::vector<TextPoint>& points, std::shared_ptr<RSPath>& path); 186 SetLastLineHeight(float lineHeight)187 void SetLastLineHeight(float lineHeight) 188 { 189 lastLineHeight_ = lineHeight; 190 } 191 192 protected: 193 RefPtr<TextDragOverlayModifier> overlayModifier_; 194 195 TextDragData textDragData_; 196 private: 197 float lastLineHeight_ = 0.0f; 198 OffsetF contentOffset_; 199 ParagraphT paragraph_; 200 std::shared_ptr<RSPath> clipPath_; 201 std::shared_ptr<RSPath> backGroundPath_; 202 std::list<RefPtr<FrameNode>> imageChildren_; 203 std::vector<RectF> rectsForPlaceholders_; 204 205 ACE_DISALLOW_COPY_AND_MOVE(TextDragPattern); 206 }; 207 } // namespace OHOS::Ace::NG 208 209 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_DRAG_TEXT_DRAG_PATTERN_H 210