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_RICH_EDITOR_PARAGRAPH_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_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 
36         std::string ToString() const;
37     };
38     struct TextBox {
39         TextDirection direction_;
40         RectF rect_;
41         TextBox() = default;
TextBoxTextBox42         TextBox(RectF rect, TextDirection direction) : direction_(direction), rect_(rect) {}
43     };
44 
45     ParagraphManager() = default;
46     std::optional<double> minParagraphFontSize = std::nullopt;
47 
48     int32_t GetIndex(Offset offset, bool clamp = false) const;
49     PositionWithAffinity GetGlyphPositionAtCoordinate(Offset offset);
50     float GetHeight() const;
51 
GetParagraphs()52     const std::list<ParagraphInfo>& GetParagraphs() const
53     {
54         return paragraphs_;
55     }
56     void Reset();
57 
58     std::vector<RectF> GetRects(int32_t start, int32_t end,
59         RectHeightPolicy rectHeightPolicy = RectHeightPolicy::COVER_LINE) const;
60     std::vector<std::pair<std::vector<RectF>, TextDirection>> GetParagraphsRects(
61         int32_t start, int32_t end, RectHeightPolicy rectHeightPolicy = RectHeightPolicy::COVER_LINE) const;
62     std::vector<RectF> GetPlaceholderRects() const;
63     OffsetF ComputeCursorOffset(int32_t index, float& selectLineHeight, bool downStreamFirst = false,
64             bool needLineHighest = true) const;
65     OffsetF ComputeCursorInfoByClick(int32_t index, float& selectLineHeight, const OffsetF& lastTouchOffset) const;
66     bool IsSelectLineHeadAndUseLeadingMargin(int32_t start) const;
67 
AddParagraph(ParagraphInfo && info)68     void AddParagraph(ParagraphInfo&& info)
69     {
70         paragraphs_.emplace_back(std::move(info));
71     }
72 
SetParagraphs(const std::list<ParagraphInfo> & paragraphs)73     void SetParagraphs(const std::list<ParagraphInfo>& paragraphs)
74     {
75         paragraphs_ = paragraphs;
76     }
77 
78     // add for text
79     int32_t GetGlyphIndexByCoordinate(Offset offset, bool isSelectionPos = false) const;
80     bool GetWordBoundary(int32_t offset, int32_t& start, int32_t& end) const;
81     bool CalcCaretMetricsByPosition(int32_t extent, CaretMetricsF& caretCaretMetric, TextAffinity textAffinity) const;
82     float GetMaxIntrinsicWidth() const;
83     bool DidExceedMaxLines() const;
84     float GetLongestLine() const;
85     float GetMaxWidth() const;
86     float GetTextWidth() const;
87     float GetTextWidthIncludeIndent() const;
88     float GetLongestLineWithIndent() const;
89     size_t GetLineCount() const;
90     LineMetrics GetLineMetricsByRectF(RectF rect, int32_t paragraphIndex) const;
91     void GetPaintRegion(RectF& boundsRect, float x, float y) const;
92     std::vector<TextBox> GetRectsForRange(int32_t start, int32_t end,
93         RectHeightStyle heightStyle, RectWidthStyle widthStyle);
94     TextLineMetrics GetLineMetrics(size_t lineNumber);
95 
96 private:
97     std::list<ParagraphInfo> paragraphs_;
98 };
99 } // namespace OHOS::Ace::NG
100 #endif
101