• 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 "base/geometry/dimension.h"
26 #include "base/utils/utils.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/text/multiple_paragraph_layout_algorithm.h"
30 #include "core/components_ng/pattern/text/span_node.h"
31 #include "core/components_ng/pattern/text/text_adapt_font_sizer.h"
32 #include "core/components_ng/pattern/text/text_layout_property.h"
33 #include "core/components_ng/render/paragraph.h"
34 
35 namespace OHOS::Ace::NG {
36 class PipelineContext;
37 class TextContentModifier;
38 
39 struct DragSpanPosition {
40     int32_t dragStart { 0 };
41     int32_t dragEnd { 0 };
42     int32_t spanStart { 0 };
43     int32_t spanEnd { 0 };
44 };
45 
46 // TextLayoutAlgorithm acts as the underlying text layout.
47 class ACE_EXPORT TextLayoutAlgorithm : public MultipleParagraphLayoutAlgorithm, public TextAdaptFontSizer {
48     DECLARE_ACE_TYPE(TextLayoutAlgorithm, BoxLayoutAlgorithm, TextAdaptFontSizer);
49 
50 public:
51     TextLayoutAlgorithm();
52     explicit TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spans, RefPtr<ParagraphManager> paragraphManager_,
53         bool isSpanStringMode, const TextStyle& textStyle, const bool isMarquee = false);
54     ~TextLayoutAlgorithm() override = default;
55 
56     void OnReset() override;
57 
58     std::optional<SizeF> MeasureContent(
59         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
60 
GetSuitableSize(SizeF & maxSize,LayoutWrapper * layoutWrapper)61     void GetSuitableSize(SizeF& maxSize, LayoutWrapper* layoutWrapper) override {};
62     bool CreateParagraphAndLayout(TextStyle& textStyle, const std::u16string& content,
63         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, bool needLayout = true) override;
64     bool ReLayoutParagraphs(const TextStyle& textStyle, LayoutWrapper* layoutWrapper, const SizeF& maxSize);
65     bool LayoutParagraphs(float maxWidth);
66 
67     float GetBaselineOffset() const override;
68 
69     size_t GetLineCount() const;
70 
71     const TextStyle& GetTextStyle() const;
72 
GetParagraph()73     RefPtr<Paragraph> GetParagraph() const override
74     {
75         CHECK_NULL_RETURN(paragraphManager_, nullptr);
76         CHECK_NULL_RETURN(!paragraphManager_->GetParagraphs().empty(), nullptr);
77         return paragraphManager_->GetParagraphs().front().paragraph;
78     }
79 
80 protected:
GetPreviousLength()81     virtual int32_t GetPreviousLength() const
82     {
83         return 0;
84     }
85 
86     virtual void UpdateParagraphForAISpan(
87         const TextStyle& textStyle, LayoutWrapper* layoutWrapper, const RefPtr<Paragraph>& paragraph);
88 
89     void GrayDisplayAISpan(const DragSpanPosition& dragSpanPosition, const std::u16string textForAI,
90         const TextStyle& textStyle, bool isDragging, const RefPtr<Paragraph>& paragraph);
91     bool DidExceedMaxLines(const SizeF& maxSize) override;
92 
93     std::u16string StringOutBoundProtection(int32_t position, int32_t length, std::u16string wTextForAI);
94     bool IsNeedParagraphReLayout() const override;
95     double GetIndentMaxWidth(double width) const override;
96     void MeasureWidthLayoutCalPolicy(LayoutWrapper* layoutWrapper) override;
97 
98 private:
99     OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) override;
100     bool UpdateSingleParagraph(LayoutWrapper* layoutWrapper, ParagraphStyle paraStyle, const TextStyle& textStyle,
101         const std::u16string& content, double maxWidth);
102     void UpdateRelayoutShaderStyle(LayoutWrapper* layoutWrapper);
103     bool UpdateSymbolTextStyle(const TextStyle& textStyle, const ParagraphStyle& paraStyle,
104         LayoutWrapper* layoutWrapper, RefPtr<FrameNode>& frameNode);
105     void CreateParagraphDrag(
106         const TextStyle& textStyle, const std::vector<std::u16string>& contents, const RefPtr<Paragraph>& paragraph);
107     bool AdaptMinTextSize(TextStyle& textStyle, const std::u16string& content,
108         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
109     bool AddPropertiesAndAnimations(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& textLayoutProperty,
110         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
111     bool CreateParagraph(const TextStyle& textStyle, std::u16string content, LayoutWrapper* layoutWrapper,
112         double maxWidth = 0.0) override;
113     bool BuildParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
114         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
115     bool BuildParagraphAdaptUseMinFontSize(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
116         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
117     bool BuildParagraphAdaptUseLayoutConstraint(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
118         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
119     std::optional<SizeF> BuildTextRaceParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
120         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
121     bool AdaptMaxTextSize(TextStyle& textStyle, const std::u16string& content,
122         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
123     void UpdateSensitiveContent(std::u16string& content);
124     void CheckNeedReCreateParagraph(LayoutWrapper* layoutWrapper, const TextStyle& textStyle);
125     void ResetNeedReCreateParagraph(LayoutWrapper* layoutWrapper);
126     void RelayoutShaderStyle(const RefPtr<TextLayoutProperty>& layoutProperty);
127     bool AlwaysReCreateParagraph(LayoutWrapper* layoutWrapper);
128     std::pair<bool, double> GetSuitableSize(TextStyle& textStyle, const std::u16string& content,
129         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
130     std::pair<bool, double> GetSuitableSizeLD(TextStyle& textStyle, const std::u16string& content,
131         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, double stepSize);
132     std::pair<bool, double> GetSuitableSizeBS(TextStyle& textStyle, const std::u16string& content,
133         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, double stepSize);
134     bool IsAdaptExceedLimit(const SizeF& maxSize) override;
135     void CreateOrUpdateTextEffect(const RefPtr<Paragraph>& oldParagraph, const RefPtr<Paragraph>& newParagraph,
136         const RefPtr<TextPattern>& textPattern, const std::u16string& content);
137     bool IsParentSizeNearZero(const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
138     bool IsFixIdealSizeAndNoMaxSize(LayoutWrapper* layoutWrapper, bool isWidth);
139     LayoutConstraintF CalcContentConstraint(const LayoutConstraintF& constraint, LayoutWrapper* layoutWrapper);
140     std::optional<float> GetCalcLayoutConstraintLength(LayoutWrapper* layoutWrapper, bool isMax, bool isWidth);
141     void MeasureWithFixAtIdealSize(LayoutWrapper* layoutWrapper);
142     RefPtr<PropertyBool> showSelect_;
143     std::optional<LayoutConstraintF> cachedCalcContentConstraint_;
144     bool isFixIdealSizeAndNoMaxWidth_ = false;
145     bool alwaysReCreateParagraph_ = false;
146     ACE_DISALLOW_COPY_AND_MOVE(TextLayoutAlgorithm);
147 };
148 } // namespace OHOS::Ace::NG
149 
150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
151