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/pattern/rich_editor_drag/rich_editor_drag_info.h" 24 #include "core/components_ng/render/drawing.h" 25 #include "core/components_ng/render/paragraph.h" 26 27 namespace OHOS::Ace::NG { 28 constexpr Dimension TEXT_DRAG_RADIUS_2IN1 = 8.0_vp; 29 constexpr Dimension TEXT_DRAG_RADIUS = 18.0_vp; 30 constexpr Dimension TEXT_DRAG_OFFSET = 8.0_vp; 31 constexpr Dimension TEXT_DRAG_MIN_WIDTH = 64.0_vp; 32 constexpr uint32_t TEXT_DRAG_COLOR_BG = 0xf2ffffff; 33 34 struct SelectPositionInfo { SelectPositionInfoSelectPositionInfo35 SelectPositionInfo() {} SelectPositionInfoSelectPositionInfo36 SelectPositionInfo(float startX, float startY, float endX, float endY) 37 : startX_(startX), startY_(startY), endX_(endX), endY_(endY) 38 {} 39 InitGlobalInfoSelectPositionInfo40 void InitGlobalInfo(float globalX, float globalY) 41 { 42 globalX_ = globalX; 43 globalY_ = globalY; 44 } 45 46 float startX_ = 0; 47 float startY_ = 0; 48 float endX_ = 0; 49 float endY_ = 0; 50 float globalX_ = 0; 51 float globalY_ = 0; 52 }; 53 54 struct TextDragData { TextDragDataTextDragData55 TextDragData() {} TextDragDataTextDragData56 TextDragData(RectF textRect, float frameWidth, float frameHeight, float lineHeight, float lastLineHeight) 57 : textRect_(textRect), frameWidth_(frameWidth), frameHeight_(frameHeight), lineHeight_(lineHeight), 58 lastLineHeight_(lastLineHeight) 59 {} 60 61 RectF textRect_; 62 float frameWidth_ = 0; 63 float frameHeight_ = 0; 64 float lineHeight_ = 0; 65 float lastLineHeight_ = 0; 66 SelectPositionInfo selectPosition_; 67 bool oneLineSelected_ = false; initSelecitonInfoTextDragData68 void initSelecitonInfo(SelectPositionInfo selectionInfo, bool oneLineSelected) 69 { 70 selectPosition_ = selectionInfo; 71 oneLineSelected_ = oneLineSelected; 72 } 73 }; 74 75 struct TextPoint { TextPointTextPoint76 TextPoint() {} TextPointTextPoint77 TextPoint(float x, float y) : x(x), y(y) {} 78 79 float x = 0; 80 float y = 0; 81 }; 82 83 class TextDragPattern : public Pattern { 84 DECLARE_ACE_TYPE(TextDragPattern, Pattern); 85 86 public: 87 TextDragPattern() = default; 88 ~TextDragPattern() override = default; 89 static RefPtr<FrameNode> CreateDragNode(const RefPtr<FrameNode>& hostNode); Initialize(const RefPtr<Paragraph> & paragraph,const TextDragData & data)90 void Initialize(const RefPtr<Paragraph>& paragraph, const TextDragData& data) 91 { 92 paragraph_ = paragraph; 93 textDragData_ = data; 94 } 95 UpdateParagraph(const RefPtr<Paragraph> & paragraph)96 void UpdateParagraph(const RefPtr<Paragraph>& paragraph) 97 { 98 paragraph_ = paragraph; 99 } 100 CreateNodePaintMethod()101 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 102 { 103 if (!overlayModifier_) { 104 overlayModifier_ = AceType::MakeRefPtr<TextDragOverlayModifier>(WeakClaim(this)); 105 } 106 auto paintMethod = AceType::MakeRefPtr<TextDragPaintMethod>(WeakClaim(this), overlayModifier_); 107 paintMethod->UpdateHandleInfo(info_); 108 return paintMethod; 109 } 110 GetParagraph()111 const WeakPtr<Paragraph>& GetParagraph() const 112 { 113 return paragraph_; 114 } 115 GetOverlayModifier()116 virtual const RefPtr<TextDragOverlayModifier>& GetOverlayModifier() const 117 { 118 return overlayModifier_; 119 } 120 GetTextRect()121 const RectF& GetTextRect() const 122 { 123 return textDragData_.textRect_; 124 } 125 GetFrameWidth()126 float GetFrameWidth() const 127 { 128 return textDragData_.frameWidth_; 129 } 130 GetFrameHeight()131 float GetFrameHeight() const 132 { 133 return textDragData_.frameHeight_; 134 } 135 GetLineHeight()136 float GetLineHeight() const 137 { 138 return textDragData_.lineHeight_; 139 } 140 GetSelectPosition()141 const SelectPositionInfo& GetSelectPosition() const 142 { 143 return textDragData_.selectPosition_; 144 } 145 OneLineSelected()146 bool OneLineSelected() const 147 { 148 return textDragData_.oneLineSelected_; 149 } 150 GetClipPath()151 const std::shared_ptr<RSPath>& GetClipPath() 152 { 153 if (!clipPath_) { 154 clipPath_ = GenerateClipPath(); 155 } 156 return clipPath_; 157 } 158 GetBackgroundPath()159 const std::shared_ptr<RSPath>& GetBackgroundPath() 160 { 161 if (!backGroundPath_) { 162 backGroundPath_ = GenerateBackgroundPath(TEXT_DRAG_OFFSET.ConvertToPx()); 163 } 164 return backGroundPath_; 165 } 166 GetSelBackgroundPath()167 const std::shared_ptr<RSPath>& GetSelBackgroundPath() 168 { 169 if (!selBackGroundPath_) { 170 selBackGroundPath_ = GenerateSelBackgroundPath(0.0); 171 } 172 return selBackGroundPath_; 173 } 174 175 std::shared_ptr<RSPath> GenerateBackgroundPath(float offset, float radiusRatio = 1.0f); 176 177 std::shared_ptr<RSPath> GenerateSelBackgroundPath(float offset); 178 SetImageChildren(const std::list<RefPtr<FrameNode>> & imageChildren)179 void SetImageChildren(const std::list<RefPtr<FrameNode>>& imageChildren) 180 { 181 imageChildren_ = imageChildren; 182 } 183 GetImageChildren()184 const std::list<RefPtr<FrameNode>>& GetImageChildren() 185 { 186 return imageChildren_; 187 } 188 InitSpanImageLayout(const std::list<RefPtr<FrameNode>> & imageChildren,const std::vector<RectF> & rectsForPlaceholders)189 void InitSpanImageLayout( 190 const std::list<RefPtr<FrameNode>>& imageChildren, const std::vector<RectF>& rectsForPlaceholders) 191 { 192 imageChildren_ = imageChildren; 193 rectsForPlaceholders_ = rectsForPlaceholders; 194 } 195 GetContentOffset()196 OffsetF GetContentOffset() 197 { 198 return contentOffset_; 199 } 200 SetContentOffset(OffsetF contentOffset)201 void SetContentOffset(OffsetF contentOffset) 202 { 203 contentOffset_ = contentOffset; 204 } 205 GetRectsForPlaceholders()206 const std::vector<RectF>& GetRectsForPlaceholders() 207 { 208 return rectsForPlaceholders_; 209 } 210 211 virtual Dimension GetDragCornerRadius(); 212 UpdateHandleAnimationInfo(const TextDragInfo & info)213 void UpdateHandleAnimationInfo(const TextDragInfo& info) 214 { 215 info_ = info; 216 } 217 218 Color GetDragBackgroundColor(); 219 protected: 220 static TextDragData CalculateTextDragData(RefPtr<TextDragBase>& pattern, RefPtr<FrameNode>& dragNode); 221 virtual void AdjustMaxWidth(float& width, const RectF& contentRect, const std::vector<RectF>& boxes); 222 static RectF GetHandler(const bool isLeftHandler, const std::vector<RectF> boxes, const RectF contentRect, 223 const OffsetF globalOffset, const OffsetF textStartOffset); 224 static void AdjustHandlers(const RectF contentRect, RectF& leftHandler, RectF& rightHandler); 225 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 226 std::shared_ptr<RSPath> GenerateClipPath(); 227 void GenerateBackgroundPoints(std::vector<TextPoint>& points, float offset, bool needAdjust = true); 228 void CalculateLineAndArc(std::vector<TextPoint>& points, std::shared_ptr<RSPath>& path, float radiusRatio); 229 void CalculateLine(std::vector<TextPoint>& points, std::shared_ptr<RSPath>& path); 230 static void CalculateFloatTitleOffset(RefPtr<FrameNode>& dragNode, OffsetF& offset); 231 SetLastLineHeight(float lineHeight)232 void SetLastLineHeight(float lineHeight) 233 { 234 lastLineHeight_ = lineHeight; 235 } 236 237 protected: 238 RefPtr<TextDragOverlayModifier> overlayModifier_; 239 TextDragData textDragData_; 240 241 private: 242 float lastLineHeight_ = 0.0f; 243 OffsetF contentOffset_; 244 WeakPtr<Paragraph> paragraph_; 245 std::shared_ptr<RSPath> clipPath_; 246 std::shared_ptr<RSPath> backGroundPath_; 247 std::shared_ptr<RSPath> selBackGroundPath_; 248 std::list<RefPtr<FrameNode>> imageChildren_; 249 std::vector<RectF> rectsForPlaceholders_; 250 TextDragInfo info_; 251 252 ACE_DISALLOW_COPY_AND_MOVE(TextDragPattern); 253 }; 254 } // namespace OHOS::Ace::NG 255 256 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_DRAG_TEXT_DRAG_PATTERN_H 257