1 /* 2 * Copyright (c) 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_PATTERNS_PARAGRAPH_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PARAGRAPH_MANAGER_H 18 #include <list> 19 #include <optional> 20 21 #include "base/geometry/offset.h" 22 #include "base/memory/ace_type.h" 23 #include "core/components/common/properties/text_layout_info.h" 24 #include "core/components_ng/render/paragraph.h" 25 namespace OHOS::Ace::NG { 26 class ParagraphManager : public virtual AceType { 27 DECLARE_ACE_TYPE(ParagraphManager, AceType); 28 29 public: 30 struct ParagraphInfo { 31 RefPtr<Paragraph> paragraph; 32 ParagraphStyle paragraphStyle; 33 int32_t start = 0; 34 int32_t end = 0; 35 float topPos = 0.0f; 36 float bottomPos = 0.0f; 37 38 std::string ToString() const; 39 }; 40 struct TextBox { 41 TextDirection direction_; 42 RectF rect_; 43 TextBox() = default; TextBoxTextBox44 TextBox(RectF rect, TextDirection direction) : direction_(direction), rect_(rect) {} 45 }; 46 47 ParagraphManager() = default; 48 std::optional<double> minParagraphFontSize = std::nullopt; 49 50 int32_t GetIndex(Offset offset, bool clamp = false) const; 51 PositionWithAffinity GetGlyphPositionAtCoordinate(Offset offset); 52 float GetHeight() const; 53 GetParagraphs()54 const std::vector<ParagraphInfo>& GetParagraphs() const 55 { 56 return paragraphs_; 57 } 58 void Reset(); 59 60 virtual std::vector<RectF> GetRects(int32_t start, int32_t end, 61 RectHeightPolicy rectHeightPolicy = RectHeightPolicy::COVER_LINE) const; 62 ParagraphManager::ParagraphInfo GetParagraphInfo(int32_t position) const; 63 std::vector<std::pair<std::vector<RectF>, TextDirection>> GetParagraphsRects( 64 int32_t start, int32_t end, RectHeightPolicy rectHeightPolicy = RectHeightPolicy::COVER_LINE) const; 65 std::vector<std::pair<std::vector<RectF>, ParagraphStyle>> GetTextBoxesForSelect( 66 int32_t start, int32_t end, RectHeightPolicy rectHeightPolicy = RectHeightPolicy::COVER_LINE) const; 67 std::vector<std::pair<std::vector<RectF>, ParagraphStyle>> GetRichEditorBoxesForSelect( 68 int32_t start, int32_t end, RectHeightPolicy rectHeightPolicy = RectHeightPolicy::COVER_LINE) const; 69 std::vector<RectF> GetPlaceholderRects() const; 70 OffsetF ComputeCursorOffset(int32_t index, float& selectLineHeight, bool downStreamFirst = false, 71 bool needLineHighest = true) const; 72 OffsetF ComputeCursorInfoByClick(int32_t index, float& selectLineHeight, const OffsetF& lastTouchOffset) const; 73 bool IsSelectLineHeadAndUseLeadingMargin(int32_t start) const; 74 void LayoutParagraphs(float maxWidth); 75 AddParagraph(ParagraphInfo && info)76 void AddParagraph(ParagraphInfo&& info) 77 { 78 paragraphs_.emplace_back(std::move(info)); 79 } 80 SetParagraphs(const std::vector<ParagraphInfo> & paragraphs)81 void SetParagraphs(const std::vector<ParagraphInfo>& paragraphs) 82 { 83 paragraphs_ = paragraphs; 84 } 85 86 // add for text 87 int32_t GetGlyphIndexByCoordinate(Offset offset, bool isSelectionPos = false) const; 88 bool GetWordBoundary(int32_t offset, int32_t& start, int32_t& end) const; 89 bool CalcCaretMetricsByPosition(int32_t extent, CaretMetricsF& caretCaretMetric, TextAffinity textAffinity) const; 90 float GetMaxIntrinsicWidth() const; 91 bool DidExceedMaxLines() const; 92 float GetLongestLine() const; 93 float GetMaxWidth() const; 94 float GetTextWidth() const; 95 float GetTextWidthIncludeIndent() const; 96 float GetLongestLineWithIndent() const; 97 size_t GetLineCount() const; 98 LineMetrics GetLineMetricsByRectF(RectF rect, int32_t paragraphIndex) const; 99 void GetPaintRegion(RectF& boundsRect, float x, float y) const; 100 std::vector<TextBox> GetRectsForRange(int32_t start, int32_t end, 101 RectHeightStyle heightStyle, RectWidthStyle widthStyle); 102 std::pair<size_t, size_t> GetEllipsisTextRange(); 103 TextLineMetrics GetLineMetrics(size_t lineNumber); 104 bool IsIndexAtParagraphEnd(int32_t index); 105 bool DidExceedMaxLinesInner() const; 106 std::string GetDumpInfo() const; 107 108 protected: 109 std::vector<ParagraphInfo> paragraphs_; 110 111 private: 112 struct SelectData { 113 float y = 0.0f; 114 bool secondResult = false; 115 CaretMetricsF secondMetrics; 116 int32_t relativeStart = 0; 117 int32_t relativeEnd = 0; 118 float paragraphSpacing = 0.0f; 119 }; 120 static void MakeBlankLineRectsInParagraph(std::vector<RectF>& result, const ParagraphInfo& info, 121 const SelectData& selectData); 122 static void MakeBlankRectsInRichEditor(std::vector<RectF>& result, const ParagraphInfo& info, 123 const SelectData& selectData); 124 static void RemoveBlankLineRectByHandler(std::vector<RectF>& rects, const SelectData& selectData); 125 static bool IsRectOutByHandler(const RectF& rect, const SelectData& selectData); 126 static void AddParagraphSpacingBlankRect( 127 std::vector<RectF>& rects, const RectF& lastRect, const SelectData& selectData); 128 static void AppendParagraphSpacingBlankRect(std::vector<RectF>& rects, const SelectData& selectData); 129 }; 130 } // namespace OHOS::Ace::NG 131 #endif 132