• 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 <list>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 
25 #include "core/components_ng/layout/box_layout_algorithm.h"
26 #include "core/components_ng/layout/layout_wrapper.h"
27 #include "core/components_ng/pattern/text/span_node.h"
28 #include "core/components_ng/pattern/text/text_layout_property.h"
29 #include "core/components_ng/pattern/text/text_styles.h"
30 #include "core/components_ng/render/paragraph.h"
31 
32 namespace OHOS::Ace::NG {
33 class PipelineContext;
34 class TextContentModifier;
35 
36 struct DragSpanPosition {
37     int32_t dragStart { 0 };
38     int32_t dragEnd { 0 };
39     int32_t spanStart { 0 };
40     int32_t spanEnd { 0 };
41 };
42 
43 // TextLayoutAlgorithm acts as the underlying text layout.
44 class ACE_EXPORT TextLayoutAlgorithm : public BoxLayoutAlgorithm {
45     DECLARE_ACE_TYPE(TextLayoutAlgorithm, BoxLayoutAlgorithm);
46 
47 public:
48     TextLayoutAlgorithm();
49 
TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spanItemChildren)50     explicit TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spanItemChildren)
51         : spanItemChildren_(std::move(spanItemChildren))
52     {}
53 
54     ~TextLayoutAlgorithm() override = default;
55 
56     void OnReset() override;
57 
58     void Measure(LayoutWrapper* layoutWrapper) override;
59 
60     std::optional<SizeF> MeasureContent(
61         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
62 
63     void Layout(LayoutWrapper* layoutWrapper) override;
64 
65     const RefPtr<Paragraph>& GetParagraph();
66 
67     std::list<RefPtr<SpanItem>>&& GetSpanItemChildren();
68 
69     float GetBaselineOffset() const;
70 
71     size_t GetLineCount() const;
72 
73     std::optional<TextStyle> GetTextStyle() const;
74 
75 protected:
GetSpans()76     const std::list<RefPtr<SpanItem>>& GetSpans() const
77     {
78         return spanItemChildren_;
79     }
SetSpans(const std::list<RefPtr<SpanItem>> & spans)80     void SetSpans(const std::list<RefPtr<SpanItem>>& spans)
81     {
82         spanItemChildren_ = spans;
83     }
84 
SetParagraph(const RefPtr<Paragraph> & paragraph)85     void SetParagraph(const RefPtr<Paragraph>& paragraph)
86     {
87         paragraph_ = paragraph;
88     }
89 
GetPreviousLength()90     virtual int32_t GetPreviousLength() const
91     {
92         return 0;
93     }
94 
95     virtual void GetPlaceholderRects(std::vector<RectF>& rects);
96 
97     virtual ParagraphStyle GetParagraphStyle(
98         const TextStyle& textStyle, const std::string& content, LayoutWrapper* layoutWrapper) const;
99 
100     virtual void UpdateParagraphForAISpan(const TextStyle& textStyle, LayoutWrapper* layoutWrapper);
101 
102     virtual OffsetF GetContentOffset(LayoutWrapper* layoutWrapper);
103 
104     void GrayDisplayAISpan(const DragSpanPosition& dragSpanPosition, const std::wstring textForAI,
105         const TextStyle& textStyle, bool isDragging);
106 
107     std::string StringOutBoundProtection(int32_t position, int32_t length, std::wstring wTextForAI);
108 
109 private:
110     virtual void ApplyIndent(const TextStyle& textStyle, double width);
111     void FontRegisterCallback(const RefPtr<FrameNode>& frameNode, const TextStyle& textStyle);
112     bool CreateParagraph(const TextStyle& textStyle, std::string content, LayoutWrapper* layoutWrapper);
113     void UpdateSymbolSpanEffect(RefPtr<FrameNode>& frameNode);
114     void CreateParagraphDrag(const TextStyle& textStyle, const std::vector<std::string>& contents);
115     bool CreateParagraphAndLayout(const TextStyle& textStyle, const std::string& content,
116         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
117     bool AdaptMinTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
118         const RefPtr<PipelineContext>& pipeline, LayoutWrapper* layoutWrapper);
119     bool DidExceedMaxLines(const SizeF& maxSize);
120     bool AddPropertiesAndAnimations(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& textLayoutProperty,
121         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
122         LayoutWrapper* layoutWrapper);
123     static TextDirection GetTextDirection(const std::string& content, LayoutWrapper* layoutWrapper);
124     float GetTextWidth() const;
125     SizeF GetMaxMeasureSize(const LayoutConstraintF& contentConstraint) const;
126     bool BuildParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
127         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
128         LayoutWrapper* layoutWrapper);
129     bool BuildParagraphAdaptUseMinFontSize(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
130         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
131         LayoutWrapper* layoutWrapper);
132     bool BuildParagraphAdaptUseLayoutConstraint(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
133         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
134         LayoutWrapper* layoutWrapper);
135     std::optional<SizeF> BuildTextRaceParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
136         const LayoutConstraintF& contentConstraint, const RefPtr<PipelineContext>& pipeline,
137         LayoutWrapper* layoutWrapper);
138     void SetPropertyToModifier(const RefPtr<TextLayoutProperty>& layoutProperty, RefPtr<TextContentModifier> modifier);
139     bool AdaptMaxTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
140         const RefPtr<PipelineContext>& pipeline, LayoutWrapper* layoutWrapper);
141     void UpdateTextColorIfForeground(const RefPtr<FrameNode>& frameNode, TextStyle& textStyle);
142     void UpdateParagraph(LayoutWrapper* layoutWrapper);
143     bool CreateImageSpanAndLayout(const TextStyle& textStyle, const std::string& content,
144         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
145     bool IncludeImageSpan(LayoutWrapper* layoutWrapper);
146     void SetImageSpanTextStyle(const TextStyle& textStyle);
147     void GetSpanAndImageSpanList(std::list<RefPtr<SpanItem>>& spanList,
148         std::map<int32_t, std::pair<RectF, RefPtr<PlaceholderSpanItem>>>& placeholderSpanList);
149     void SplitSpanContentByLines(const TextStyle& textStyle, const std::list<RefPtr<SpanItem>>& spanList,
150         std::map<int32_t, std::pair<RectF, std::list<RefPtr<SpanItem>>>>& spanContentLines);
151     void SetImageSpanTextStyleByLines(const TextStyle& textStyle,
152         std::map<int32_t, std::pair<RectF, RefPtr<PlaceholderSpanItem>>>& placeholderSpanList,
153         std::map<int32_t, std::pair<RectF, std::list<RefPtr<SpanItem>>>>& spanContentLines);
154     int32_t GetFirstSpanStartPositon();
155 
156     std::list<RefPtr<SpanItem>> spanItemChildren_;
157     RefPtr<Paragraph> paragraph_;
158     float baselineOffset_ = 0.0f;
159     std::optional<TextStyle> textStyle_;
160     RefPtr<PropertyBool> showSelect_;
161     float indent_ = 0.0f;
162     ACE_DISALLOW_COPY_AND_MOVE(TextLayoutAlgorithm);
163 };
164 } // namespace OHOS::Ace::NG
165 
166 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
167