• 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     virtual bool CreateParagraph(
56         const TextStyle& textStyle, std::u16string content, LayoutWrapper* layoutWrapper, double maxWidth = 0.0) = 0;
HandleEmptyParagraph(RefPtr<Paragraph> paragraph,const std::list<RefPtr<SpanItem>> & spanGroup)57     virtual void HandleEmptyParagraph(RefPtr<Paragraph> paragraph, const std::list<RefPtr<SpanItem>>& spanGroup) {}
GetParagraphStyleSpanItem(const std::list<RefPtr<SpanItem>> & spanGroup)58     virtual RefPtr<SpanItem> GetParagraphStyleSpanItem(const std::list<RefPtr<SpanItem>>& spanGroup)
59     {
60         CHECK_NULL_RETURN(!spanGroup.empty(), nullptr);
61         return spanGroup.front();
62     }
63     void ConstructTextStyles(
64         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, TextStyle& textStyle);
65     bool ParagraphReLayout(const LayoutConstraintF& contentConstraint);
66     bool UpdateParagraphBySpan(LayoutWrapper* layoutWrapper, ParagraphStyle paraStyle, double maxWidth,
67         const TextStyle& textStyle);
68     OffsetF SetContentOffset(LayoutWrapper* layoutWrapper);
69     virtual void SetAdaptFontSizeStepToTextStyle(
70         TextStyle& textStyle, const std::optional<Dimension>& adaptFontSizeStep);
SpansToString()71     std::string SpansToString()
72     {
73         std::stringstream ss;
74         for (auto& list : spans_) {
75             ss << "[";
76             for_each(list.begin(), list.end(), [&ss](RefPtr<SpanItem>& item) {
77                 ss << "[" << item->interval.first << "," << item->interval.second << ":"
78                    << StringUtils::RestoreEscape(UtfUtils::Str16DebugToStr8(item->content)) << "], ";
79             });
80             ss << "], ";
81         }
82         return ss.str();
83     }
84 
GetOrCreateParagraph(const std::list<RefPtr<SpanItem>> & group,const ParagraphStyle & paraStyle,const std::map<int32_t,AISpan> & aiSpanMap)85     virtual RefPtr<Paragraph> GetOrCreateParagraph(const std::list<RefPtr<SpanItem>>& group,
86         const ParagraphStyle& paraStyle, const std::map<int32_t, AISpan>& aiSpanMap) {
87         useParagraphCache_ = false;
88         return Paragraph::Create(paraStyle, FontCollection::Current());
89     }
90 
GetParagraphs()91     std::vector<ParagraphManager::ParagraphInfo> GetParagraphs()
92     {
93         std::vector<ParagraphManager::ParagraphInfo> paragraphInfo;
94         if (paragraphManager_) {
95             paragraphInfo = paragraphManager_->GetParagraphs();
96         }
97         return paragraphInfo;
98     }
99 
100     virtual void AddImageToParagraph(RefPtr<ImageSpanItem>& imageSpanItem, const RefPtr<LayoutWrapper>& iterItem,
101         const RefPtr<Paragraph>& paragraph, int32_t& spanTextLength);
102     virtual void AddPlaceHolderToParagraph(RefPtr<PlaceholderSpanItem>& placeholderSpanItem,
103         const RefPtr<LayoutWrapper>& layoutWrapper, const RefPtr<Paragraph>& paragraph, int32_t& spanTextLength);
104     virtual void UpdateParagraphByCustomSpan(RefPtr<CustomSpanItem>& customSpanItem, const RefPtr<Paragraph>& paragraph,
105         int32_t& spanTextLength, CustomSpanPlaceholderInfo& customSpanPlaceholder);
106 
107     virtual void AddSymbolSpanToParagraph(const RefPtr<SpanItem>& child, int32_t& spanTextLength,
108         const RefPtr<FrameNode>& frameNode, const RefPtr<Paragraph>& paragraph);
109     virtual void AddTextSpanToParagraph(const RefPtr<SpanItem>& child, int32_t& spanTextLength,
110         const RefPtr<FrameNode>& frameNode, const RefPtr<Paragraph>& paragraph);
111     void MeasureChildren(LayoutWrapper* layoutWrapper, const TextStyle& textStyle);
112     bool ReLayoutParagraphBySpan(LayoutWrapper* layoutWrapper, ParagraphStyle& paraStyle, const TextStyle& textStyle,
113         std::vector<TextStyle>& textStyles);
114     void UpdateShaderStyle(const RefPtr<TextLayoutProperty>& layoutProperty, TextStyle& textStyle);
115     virtual ChildrenListWithGuard GetAllChildrenWithBuild(LayoutWrapper* layoutWrapper);
IsNeedParagraphReLayout()116     virtual bool IsNeedParagraphReLayout() const
117     {
118         return false;
119     }
GetIndentMaxWidth(double width)120     virtual double GetIndentMaxWidth(double width) const
121     {
122         return width;
123     }
MeasureWidthLayoutCalPolicy(LayoutWrapper * layoutWrapper)124     virtual void MeasureWidthLayoutCalPolicy(LayoutWrapper* layoutWrapper) {}
125 
126     std::vector<std::list<RefPtr<SpanItem>>> spans_;
127     RefPtr<ParagraphManager> paragraphManager_;
128     TextStyle textStyle_;
129     TextStyle inheritTextStyle_;
130     float baselineOffset_ = 0.0f;
131     float shadowOffset_ = 0.0f;
132     bool spanStringHasMaxLines_ = false;
133     bool isSpanStringMode_ = false;
134     bool isMarquee_ = false;
135     bool needReCreateParagraph_ = true;
136     bool useParagraphCache_ = false;
137     int32_t preParagraphsPlaceholderCount_ = 0;
138     int32_t currentParagraphPlaceholderCount_ = 0;
139 
140 private:
141     virtual OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) = 0;
GetShadowOffset(const std::list<RefPtr<SpanItem>> & group)142     virtual float GetShadowOffset(const std::list<RefPtr<SpanItem>>& group)
143     {
144         return 0.0f;
145     }
146 
147     void UpdateSymbolSpanEffect(
148         RefPtr<FrameNode>& frameNode, const RefPtr<Paragraph>& paragraph, const std::list<RefPtr<SpanItem>>& spans);
149     void FontRegisterCallback(const RefPtr<FrameNode>& frameNode, const TextStyle& textStyle);
150     void UpdateTextColorIfForeground(const RefPtr<FrameNode>& frameNode, TextStyle& textStyle, const Color& textColor);
151     void SetPropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty,
152         const RefPtr<TextContentModifier>& modifier, const TextStyle& textStyle, const RefPtr<FrameNode>& frameNode,
153         const Color& textColor);
154     void SetDecorationPropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty,
155         const RefPtr<TextContentModifier>& modifier, const TextStyle& textStyle);
156     void SetFontSizePropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty,
157         const RefPtr<TextContentModifier>&, const TextStyle& textStyle);
158 
159     void GetChildrenPlaceholderIndex(std::vector<int32_t>& placeholderIndex);
160     void InheritParentTextStyle(const TextStyle& textStyle);
161     bool ImageSpanMeasure(const RefPtr<ImageSpanItem>& imageSpanItem, const RefPtr<LayoutWrapper>& layoutWrapper,
162         const LayoutConstraintF& layoutConstrain, const TextStyle& textStyle);
163     bool CustomSpanMeasure(const RefPtr<CustomSpanItem>& customSpanItem, LayoutWrapper* layoutWrapper);
164     bool PlaceholderSpanMeasure(const RefPtr<PlaceholderSpanItem>& placeholderSpanItem,
165         const RefPtr<LayoutWrapper>& layoutWrapper, const LayoutConstraintF& layoutConstrain);
166     void UpdateFontFamilyWithSymbol(TextStyle& textStyle, std::vector<std::string>& fontFamilies, bool isSymbol);
167     void UpdateSymbolStyle(TextStyle& textStyle, bool isSymbol);
168     std::optional<OHOS::Ace::Gradient> ToGradient(const NG::Gradient& gradient);
169     AnimatableDimension ToAnimatableDimension(const Dimension& dimension);
170 
171     ACE_DISALLOW_COPY_AND_MOVE(MultipleParagraphLayoutAlgorithm);
172 };
173 }  // namespace OHOS::Ace::NG
174 
175 #endif  // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_MULTIPLE_PARAGRAPH_LAYOUT_ALGORITHM_H
176