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/utils/utils.h"
26 #include "core/components_ng/layout/box_layout_algorithm.h"
27 #include "core/components_ng/layout/layout_wrapper.h"
28 #include "core/components_ng/pattern/text/multiple_paragraph_layout_algorithm.h"
29 #include "core/components_ng/pattern/text/span_node.h"
30 #include "core/components_ng/pattern/text/text_adapt_font_sizer.h"
31 #include "core/components_ng/pattern/text/text_layout_property.h"
32 #include "core/components_ng/render/paragraph.h"
33 
34 namespace OHOS::Ace::NG {
35 class PipelineContext;
36 class TextContentModifier;
37 
38 struct DragSpanPosition {
39     int32_t dragStart { 0 };
40     int32_t dragEnd { 0 };
41     int32_t spanStart { 0 };
42     int32_t spanEnd { 0 };
43 };
44 
45 // TextLayoutAlgorithm acts as the underlying text layout.
46 class ACE_EXPORT TextLayoutAlgorithm : public MultipleParagraphLayoutAlgorithm, public TextAdaptFontSizer {
47     DECLARE_ACE_TYPE(TextLayoutAlgorithm, BoxLayoutAlgorithm, TextAdaptFontSizer);
48 
49 public:
50     TextLayoutAlgorithm();
51     explicit TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spans, RefPtr<ParagraphManager> paragraphManager_,
52         bool isSpanStringMode, bool isMarquee = false);
53     ~TextLayoutAlgorithm() override = default;
54 
55     void OnReset() override;
56 
57     std::optional<SizeF> MeasureContent(
58         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
59 
GetSuitableSize(SizeF & maxSize,LayoutWrapper * layoutWrapper)60     void GetSuitableSize(SizeF& maxSize, LayoutWrapper* layoutWrapper) override {};
61     bool CreateParagraphAndLayout(const TextStyle& textStyle, const std::string& content,
62         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, bool needLayout = true) override;
63 
64     float GetBaselineOffset() const override;
65 
66     size_t GetLineCount() const;
67 
68     std::optional<TextStyle> GetTextStyle() const;
69 
GetParagraph()70     RefPtr<Paragraph> GetParagraph() const override
71     {
72         CHECK_NULL_RETURN(paragraphManager_, nullptr);
73         CHECK_NULL_RETURN(!paragraphManager_->GetParagraphs().empty(), nullptr);
74         return paragraphManager_->GetParagraphs().front().paragraph;
75     }
76 
77 protected:
GetPreviousLength()78     virtual int32_t GetPreviousLength() const
79     {
80         return 0;
81     }
82 
83     virtual void UpdateParagraphForAISpan(
84         const TextStyle& textStyle, LayoutWrapper* layoutWrapper, const RefPtr<Paragraph>& paragraph);
85 
86     void GrayDisplayAISpan(const DragSpanPosition& dragSpanPosition, const std::wstring textForAI,
87         const TextStyle& textStyle, bool isDragging, const RefPtr<Paragraph>& paragraph);
88 
89     std::string StringOutBoundProtection(int32_t position, int32_t length, std::wstring wTextForAI);
90 
91 private:
92     OffsetF GetContentOffset(LayoutWrapper* layoutWrapper) override;
93     bool UpdateSingleParagraph(LayoutWrapper* layoutWrapper, ParagraphStyle paraStyle, const TextStyle& textStyle,
94         const std::string& content, double maxWidth);
95     bool UpdateSymbolTextStyle(const TextStyle& textStyle, const ParagraphStyle& paraStyle,
96         LayoutWrapper* layoutWrapper, RefPtr<FrameNode>& frameNode);
97     void CreateParagraphDrag(
98         const TextStyle& textStyle, const std::vector<std::string>& contents, const RefPtr<Paragraph>& paragraph);
99     void ConstructParagraphSpanGroup(std::list<RefPtr<SpanItem>>& spans);
100     bool AdaptMinTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
101         LayoutWrapper* layoutWrapper);
102     bool AddPropertiesAndAnimations(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& textLayoutProperty,
103         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
104     bool CreateParagraph(
105         const TextStyle& textStyle, std::string content, LayoutWrapper* layoutWrapper, double maxWidth = 0.0) override;
106     bool BuildParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
107         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
108     bool BuildParagraphAdaptUseMinFontSize(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
109         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
110     bool BuildParagraphAdaptUseLayoutConstraint(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
111         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
112     std::optional<SizeF> BuildTextRaceParagraph(TextStyle& textStyle, const RefPtr<TextLayoutProperty>& layoutProperty,
113         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper);
114     bool AdaptMaxTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
115         LayoutWrapper* layoutWrapper);
116     void UpdateSensitiveContent(std::string& content);
117 
118     RefPtr<PropertyBool> showSelect_;
119     ACE_DISALLOW_COPY_AND_MOVE(TextLayoutAlgorithm);
120 };
121 } // namespace OHOS::Ace::NG
122 
123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
124