• 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, 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(const TextStyle& textStyle, const std::u16string& content,
63         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, bool needLayout = true) override;
64 
65     float GetBaselineOffset() const override;
66 
67     size_t GetLineCount() const;
68 
69     std::optional<TextStyle> GetTextStyle() const;
70 
GetParagraph()71     RefPtr<Paragraph> GetParagraph() const override
72     {
73         CHECK_NULL_RETURN(paragraphManager_, nullptr);
74         CHECK_NULL_RETURN(!paragraphManager_->GetParagraphs().empty(), nullptr);
75         return paragraphManager_->GetParagraphs().front().paragraph;
76     }
77 
78 protected:
GetPreviousLength()79     virtual int32_t GetPreviousLength() const
80     {
81         return 0;
82     }
83 
84     virtual void UpdateParagraphForAISpan(
85         const TextStyle& textStyle, LayoutWrapper* layoutWrapper, const RefPtr<Paragraph>& paragraph);
86 
87     void GrayDisplayAISpan(const DragSpanPosition& dragSpanPosition, const std::u16string textForAI,
88         const TextStyle& textStyle, bool isDragging, const RefPtr<Paragraph>& paragraph);
89     bool DidExceedMaxLines(const SizeF& maxSize) override;
90 
91     std::u16string StringOutBoundProtection(int32_t position, int32_t length, std::u16string wTextForAI);
92 
93 private:
94     OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) override;
95     bool UpdateSingleParagraph(LayoutWrapper* layoutWrapper, ParagraphStyle paraStyle, const TextStyle& textStyle,
96         const std::u16string& content, double maxWidth);
97     bool UpdateSymbolTextStyle(const TextStyle& textStyle, const ParagraphStyle& paraStyle,
98         LayoutWrapper* layoutWrapper, RefPtr<FrameNode>& frameNode);
99     void CreateParagraphDrag(
100         const TextStyle& textStyle, const std::vector<std::u16string>& contents, const RefPtr<Paragraph>& paragraph);
101     void ConstructParagraphSpanGroup(std::list<RefPtr<SpanItem>>& spans);
102     bool AdaptMinTextSize(TextStyle& textStyle, const std::u16string& content,
103         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
104     bool AddPropertiesAndAnimations(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& textLayoutProperty,
105         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
106     bool CreateParagraph(const TextStyle& textStyle, std::u16string content, LayoutWrapper* layoutWrapper,
107         double maxWidth = 0.0) override;
108     bool BuildParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
109         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
110     bool BuildParagraphAdaptUseMinFontSize(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
111         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
112     bool BuildParagraphAdaptUseLayoutConstraint(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
113         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
114     std::optional<SizeF> BuildTextRaceParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
115         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
116     bool AdaptMaxTextSize(TextStyle& textStyle, const std::u16string& content,
117         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
118     void UpdateSensitiveContent(std::u16string& content);
119     void CheckNeedReCreateParagraph(
120         const RefPtr<TextLayoutProperty>& textLayoutProperty, const RefPtr<TextPattern>& textPattern);
121     void ResetNeedReCreateParagraph(const RefPtr<TextLayoutProperty>& textLayoutProperty, bool needRemain);
122     std::pair<bool, double> GetSuitableSize(TextStyle& textStyle, const std::u16string& content,
123         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
124     std::pair<bool, double> GetSuitableSizeLD(TextStyle& textStyle, const std::u16string& content,
125         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, double stepSize);
126     std::pair<bool, double> GetSuitableSizeBS(TextStyle& textStyle, const std::u16string& content,
127         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, double stepSize);
128     bool IsAdaptExceedLimit(const SizeF& maxSize) override;
129 
130     RefPtr<PropertyBool> showSelect_;
131     ACE_DISALLOW_COPY_AND_MOVE(TextLayoutAlgorithm);
132 };
133 } // namespace OHOS::Ace::NG
134 
135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
136