• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_MULTIPLE_PARAGRAPH_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_MULTIPLE_PARAGRAPH_LAYOUT_ALGORITHM_H
18 
19 #include <list>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 
25 #include "base/memory/referenced.h"
26 #include "core/components_ng/base/frame_node.h"
27 #include "core/components_ng/layout/box_layout_algorithm.h"
28 #include "core/components_ng/layout/layout_wrapper.h"
29 #include "core/components_ng/pattern/rich_editor/paragraph_manager.h"
30 #include "core/components_ng/pattern/text/span_node.h"
31 #include "core/components_ng/pattern/text/text_content_modifier.h"
32 #include "core/components_ng/pattern/text/text_layout_property.h"
33 #include "core/components_ng/pattern/text/text_styles.h"
34 
35 namespace OHOS::Ace::NG {
36 // TextLayoutAlgorithm acts as the underlying text layout.
37 class ACE_EXPORT MultipleParagraphLayoutAlgorithm : public BoxLayoutAlgorithm {
38     DECLARE_ACE_TYPE(MultipleParagraphLayoutAlgorithm, BoxLayoutAlgorithm);
39 
40 public:
41     MultipleParagraphLayoutAlgorithm() = default;
42     ~MultipleParagraphLayoutAlgorithm() override = default;
43 
44     void Measure(LayoutWrapper* layoutWrapper) override;
45     void Layout(LayoutWrapper* layoutWrapper) override;
GetBaselineOffset()46     virtual float GetBaselineOffset() const
47     {
48         return 0.0f;
49     }
50 
51     static SizeF GetMaxMeasureSize(const LayoutConstraintF& contentConstraint);
52     RefPtr<Paragraph> GetSingleParagraph() const;
53 
54 protected:
55     void GetSpanParagraphStyle(LayoutWrapper* layoutWrapper, const RefPtr<SpanItem>& spanItem, ParagraphStyle& pStyle);
56     virtual ParagraphStyle GetParagraphStyle(
57         const TextStyle& textStyle, const std::u16string& content, LayoutWrapper* layoutWrapper) const;
58     virtual bool CreateParagraph(
59         const TextStyle& textStyle, std::u16string content, LayoutWrapper* layoutWrapper, double maxWidth = 0.0) = 0;
HandleEmptyParagraph(RefPtr<Paragraph> paragraph,const std::list<RefPtr<SpanItem>> & spanGroup)60     virtual void HandleEmptyParagraph(RefPtr<Paragraph> paragraph, const std::list<RefPtr<SpanItem>>& spanGroup) {}
GetParagraphStyleSpanItem(const std::list<RefPtr<SpanItem>> & spanGroup)61     virtual RefPtr<SpanItem> GetParagraphStyleSpanItem(const std::list<RefPtr<SpanItem>>& spanGroup)
62     {
63         CHECK_NULL_RETURN(!spanGroup.empty(), nullptr);
64         return spanGroup.front();
65     }
66     void ApplyIndent(ParagraphStyle& paragraphStyle, const RefPtr<Paragraph>& paragraph, double width,
67         const TextStyle& textStyle);
68     void ConstructTextStyles(const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper,
69         TextStyle& textStyle, bool& needRemain);
70     void ConstructTextStyles(
71         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, TextStyle& textStyle);
72     bool ParagraphReLayout(const LayoutConstraintF& contentConstraint);
73     bool UpdateParagraphBySpan(LayoutWrapper* layoutWrapper, ParagraphStyle paraStyle, double maxWidth,
74         const TextStyle& textStyle);
75     OffsetF SetContentOffset(LayoutWrapper* layoutWrapper);
76     virtual void SetAdaptFontSizeStepToTextStyle(
77         TextStyle& textStyle, const std::optional<Dimension>& adaptFontSizeStep);
SpansToString()78     std::string SpansToString()
79     {
80         std::stringstream ss;
81         for (auto& list : spans_) {
82             ss << "[";
83             for_each(list.begin(), list.end(), [&ss](RefPtr<SpanItem>& item) {
84                 ss << "[" << item->interval.first << "," << item->interval.second << ":"
85                    << StringUtils::RestoreEscape(UtfUtils::Str16DebugToStr8(item->content)) << "], ";
86             });
87             ss << "], ";
88         }
89         return ss.str();
90     }
91 
GetOrCreateParagraph(const std::list<RefPtr<SpanItem>> & group,const ParagraphStyle & paraStyle,const std::map<int32_t,AISpan> & aiSpanMap)92     virtual RefPtr<Paragraph> GetOrCreateParagraph(const std::list<RefPtr<SpanItem>>& group,
93         const ParagraphStyle& paraStyle, const std::map<int32_t, AISpan>& aiSpanMap) {
94         useParagraphCache_ = false;
95         return Paragraph::Create(paraStyle, FontCollection::Current());
96     }
97 
GetParagraphs()98     std::vector<ParagraphManager::ParagraphInfo> GetParagraphs()
99     {
100         std::vector<ParagraphManager::ParagraphInfo> paragraphInfo;
101         if (paragraphManager_) {
102             paragraphInfo = paragraphManager_->GetParagraphs();
103         }
104         return paragraphInfo;
105     }
106 
107     virtual void AddImageToParagraph(RefPtr<ImageSpanItem>& imageSpanItem, const RefPtr<LayoutWrapper>& iterItem,
108         const LayoutConstraintF& layoutConstrain, const RefPtr<Paragraph>& paragraph, int32_t& spanTextLength,
109         const TextStyle& textStyle);
110     virtual void AddPlaceHolderToParagraph(RefPtr<PlaceholderSpanItem>& placeholderSpanItem,
111         const RefPtr<LayoutWrapper>& layoutWrapper, const LayoutConstraintF& layoutConstrain,
112         const RefPtr<Paragraph>& paragraph, int32_t& spanTextLength);
113     virtual void UpdateParagraphByCustomSpan(RefPtr<CustomSpanItem>& customSpanItem, LayoutWrapper* layoutWrapper,
114         const RefPtr<Paragraph>& paragraph, int32_t& spanTextLength, CustomSpanPlaceholderInfo& customSpanPlaceholder);
115 
116     virtual void AddSymbolSpanToParagraph(const RefPtr<SpanItem>& child, int32_t& spanTextLength,
117         const RefPtr<FrameNode>& frameNode, const RefPtr<Paragraph>& paragraph);
118     virtual void AddTextSpanToParagraph(const RefPtr<SpanItem>& child, int32_t& spanTextLength,
119         const RefPtr<FrameNode>& frameNode, const RefPtr<Paragraph>& paragraph);
120 
121     std::vector<std::list<RefPtr<SpanItem>>> spans_;
122     RefPtr<ParagraphManager> paragraphManager_;
123     std::optional<TextStyle> textStyle_;
124     TextStyle inheritTextStyle_;
125     float baselineOffset_ = 0.0f;
126     float shadowOffset_ = 0.0f;
127     bool spanStringHasMaxLines_ = false;
128     bool isSpanStringMode_ = false;
129     bool isMarquee_ = false;
130     bool needReCreateParagraph_ = true;
131     bool useParagraphCache_ = false;
132     int32_t preParagraphsPlaceholderCount_ = 0;
133     int32_t currentParagraphPlaceholderCount_ = 0;
134 
135 private:
136     virtual OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) = 0;
GetShadowOffset(const std::list<RefPtr<SpanItem>> & group)137     virtual float GetShadowOffset(const std::list<RefPtr<SpanItem>>& group)
138     {
139         return 0.0f;
140     }
141     static TextDirection GetTextDirection(const std::u16string& content, LayoutWrapper* layoutWrapper);
142     static TextDirection GetTextDirectionByContent(const std::u16string& content);
143 
144     void UpdateSymbolSpanEffect(
145         RefPtr<FrameNode>& frameNode, const RefPtr<Paragraph>& paragraph, const std::list<RefPtr<SpanItem>>& spans);
146     void FontRegisterCallback(const RefPtr<FrameNode>& frameNode, const TextStyle& textStyle);
147     void UpdateTextColorIfForeground(const RefPtr<FrameNode>& frameNode, TextStyle& textStyle);
148     void SetPropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty,
149         const RefPtr<TextContentModifier>& modifier, const TextStyle& textStyle, const RefPtr<FrameNode>& frameNode);
150     void SetDecorationPropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty,
151         const RefPtr<TextContentModifier>& modifier, const TextStyle& textStyle);
152     void SetFontSizePropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty,
153         const RefPtr<TextContentModifier>&, const TextStyle& textStyle);
154 
155     void GetChildrenPlaceholderIndex(std::vector<int32_t>& placeholderIndex);
156     void InheritParentTextStyle(const TextStyle& textStyle);
157 
158     float paragraphFontSize_ = 0.0f;
159 
160     ACE_DISALLOW_COPY_AND_MOVE(MultipleParagraphLayoutAlgorithm);
161 };
162 } // namespace OHOS::Ace::NG
163 
164 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_MULTIPLE_PARAGRAPH_LAYOUT_ALGORITHM_H
165