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_PATTERN_RICH_EDITOR_RICH_EDITOR_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RICH_EDITOR_RICH_EDITOR_LAYOUT_ALGORITHM_H
18 
19 #include "core/components_ng/pattern/text/multiple_paragraph_layout_algorithm.h"
20 #include "core/components_ng/pattern/text/text_layout_algorithm.h"
21 
22 namespace OHOS::Ace::NG {
23 class ACE_EXPORT RichEditorLayoutAlgorithm : public MultipleParagraphLayoutAlgorithm {
24     DECLARE_ACE_TYPE(RichEditorLayoutAlgorithm, MultipleParagraphLayoutAlgorithm);
25 
26 public:
27     RichEditorLayoutAlgorithm() = delete;
28     RichEditorLayoutAlgorithm(std::list<RefPtr<SpanItem>> spans, ParagraphManager* paragraphs,
29         std::optional<TextStyle> typingTextStyle);
30     ~RichEditorLayoutAlgorithm() override = default;
31 
GetParentGlobalOffset()32     const OffsetF& GetParentGlobalOffset() const
33     {
34         return parentGlobalOffset_;
35     }
36     std::optional<SizeF> MeasureContent(
37         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
38     void Layout(LayoutWrapper* layoutWrapper) override;
39     void Measure(LayoutWrapper* layoutWrapper) override;
40 
GetTextRect()41     const RectF& GetTextRect()
42     {
43         return richTextRect_;
44     }
45 
46 protected:
47     void HandleEmptyParagraph(RefPtr<Paragraph> paragraph, const std::list<RefPtr<SpanItem>>& spanGroup) override;
48     RefPtr<SpanItem> GetParagraphStyleSpanItem(const std::list<RefPtr<SpanItem>>& spanGroup) override;
49 
50 private:
51     OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) override;
52     bool CreateParagraph(
53         const TextStyle& textStyle, std::string content, LayoutWrapper* layoutWrapper, double maxWidth = 0.0) override;
54     bool BuildParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
55         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
56     ParagraphStyle GetParagraphStyle(
57         const TextStyle& textStyle, const std::string& content, LayoutWrapper* layoutWrapper) const override;
58     RefPtr<SpanItem> GetFirstTextSpanItem() const;
59     float GetShadowOffset(const std::list<RefPtr<SpanItem>>& group) override;
60     void UpdateRichTextRect(const SizeF& textSize, LayoutWrapper* layoutWrapper);
61     RefPtr<RichEditorPattern> GetRichEditorPattern(LayoutWrapper* layoutWrapper);
62 
63     bool SetPlaceholder(LayoutWrapper* layoutWrapper);
64 
65     void CopySpanStyle(RefPtr<SpanItem> source, RefPtr<SpanItem> target);
66     void AppendNewLineSpan();
67     std::optional<SizeF> MeasureContentSize(const LayoutConstraintF& constraint, LayoutWrapper* layoutWrapper);
68     std::optional<SizeF> MeasureEmptyContentSize(const LayoutConstraintF& constraint, LayoutWrapper* layoutWrapper);
69 
GetSpans()70     const std::list<RefPtr<SpanItem>>& GetSpans() const
71     {
72         return allSpans_;
73     }
74 
75     std::list<RefPtr<SpanItem>> allSpans_;
76     ParagraphManager* pManager_;
77     OffsetF parentGlobalOffset_;
78     RectF richTextRect_;
79     std::optional<TextStyle> typingTextStyle_;
80     ACE_DISALLOW_COPY_AND_MOVE(RichEditorLayoutAlgorithm);
81 };
82 } // namespace OHOS::Ace::NG
83 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RICH_EDITOR_RICH_EDITOR_LAYOUT_ALGORITHM_H
84