• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_TEXT_TEXT_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
18 
19 #include <optional>
20 #include <string>
21 #include <utility>
22 
23 #include "core/components_ng/layout/box_layout_algorithm.h"
24 #include "core/components_ng/layout/layout_wrapper.h"
25 #include "core/components_ng/pattern/text/span_node.h"
26 #include "core/components_ng/pattern/text/text_layout_property.h"
27 #include "core/components_ng/pattern/text/text_styles.h"
28 
29 namespace OHOS::Ace::NG {
30 class PipelineContext;
31 class TextContentModifier;
32 
33 // TextLayoutAlgorithm acts as the underlying text layout.
34 class ACE_EXPORT TextLayoutAlgorithm : public BoxLayoutAlgorithm {
35     DECLARE_ACE_TYPE(TextLayoutAlgorithm, BoxLayoutAlgorithm);
36 
37 public:
38     TextLayoutAlgorithm();
39 
TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spanItemChildren,const RefPtr<Paragraph> & paragraph)40     TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spanItemChildren, const RefPtr<Paragraph>& paragraph)
41         : spanItemChildren_(std::move(spanItemChildren)), paragraph_(paragraph)
42     {}
43 
44     ~TextLayoutAlgorithm() override = default;
45 
46     void OnReset() override;
47 
48     void Measure(LayoutWrapper* layoutWrapper) override;
49 
50     std::optional<SizeF> MeasureContent(
51         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
52 
53     void Layout(LayoutWrapper* layoutWrapper) override;
54 
55     const RefPtr<Paragraph>& GetParagraph();
56 
57     std::list<RefPtr<SpanItem>>&& GetSpanItemChildren();
58 
59     float GetBaselineOffset() const;
60 
61     size_t GetLineCount() const;
62 
63     std::optional<TextStyle> GetTextStyle() const;
64     void ApplyIndents(const TextStyle& textStyle, double width);
65 
66 private:
67     void FontRegisterCallback(const RefPtr<FrameNode>& frameNode, const TextStyle& textStyle);
68     bool CreateParagraph(const TextStyle& textStyle, std::string content, LayoutWrapper* layoutWrapper);
69     bool CreateParagraphAndLayout(const TextStyle& textStyle, const std::string& content,
70         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
71     bool AdaptMinTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
72         const RefPtr<PipelineContext>& pipeline, LayoutWrapper* layoutWrapper);
73     bool DidExceedMaxLines(const SizeF& maxSize);
74     bool AddPropertiesAndAnimations(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& textLayoutProperty,
75         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
76         LayoutWrapper* layoutWrapper);
77     static TextDirection GetTextDirection(const std::string& content);
78     float GetTextWidth() const;
79     SizeF GetMaxMeasureSize(const LayoutConstraintF& contentConstraint) const;
80     bool BuildParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
81         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
82         LayoutWrapper* layoutWrapper);
83     bool BuildParagraphAdaptUseMinFontSize(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
84         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
85         LayoutWrapper* layoutWrapper);
86     bool BuildParagraphAdaptUseLayoutConstraint(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
87         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
88         LayoutWrapper* layoutWrapper);
89     std::optional<SizeF> BuildTextRaceParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
90         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
91         LayoutWrapper* layoutWrapper);
92     void SetPropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty, RefPtr<TextContentModifier> modifier);
93     bool AdaptMaxTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
94         const RefPtr<PipelineContext>& pipeline, LayoutWrapper* layoutWrapper);
95     void UpdateTextColorIfForeground(const RefPtr<FrameNode>& frameNode, TextStyle& textStyle);
96     void UpdateParagraph(LayoutWrapper* layoutWrapper);
97     OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) const;
98     bool CreateImageSpanAndLayout(const TextStyle& textStyle, const std::string& content,
99         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
100     bool IncludeImageSpan(LayoutWrapper* layoutWrapper);
101     void SetImageSpanTextStyle(const TextStyle& textStyle);
102     void GetSpanAndImageSpanList(std::list<RefPtr<SpanItem>>& spanList,
103         std::map<int32_t, std::pair<Rect, RefPtr<ImageSpanItem>>>& imageSpanList);
104     void SplitSpanContentByLines(const TextStyle& textStyle, const std::list<RefPtr<SpanItem>>& spanList,
105         std::map<int32_t, std::pair<Rect, std::list<RefPtr<SpanItem>>>>& spanContentLines);
106     void SetImageSpanTextStyleByLines(const TextStyle& textStyle,
107         std::map<int32_t, std::pair<Rect, RefPtr<ImageSpanItem>>>& imageSpanList,
108         std::map<int32_t, std::pair<Rect, std::list<RefPtr<SpanItem>>>>& spanContentLines);
109 
110     std::list<RefPtr<SpanItem>> spanItemChildren_;
111     RefPtr<Paragraph> paragraph_;
112     float baselineOffset_ = 0.0f;
113     std::optional<TextStyle> textStyle_;
114 
115     ACE_DISALLOW_COPY_AND_MOVE(TextLayoutAlgorithm);
116 };
117 } // namespace OHOS::Ace::NG
118 
119 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
120