• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <string>
20 #include <utility>
21 
22 #include "core/components_ng/layout/box_layout_algorithm.h"
23 #include "core/components_ng/layout/layout_wrapper.h"
24 #include "core/components_ng/pattern/text/span_node.h"
25 #include "core/components_ng/pattern/text/text_styles.h"
26 #include "core/components_ng/render/drawing.h"
27 #include "core/components_ng/render/paragraph.h"
28 
29 namespace OHOS::Ace::NG {
30 class PipelineContext;
31 
32 // TextLayoutAlgorithm acts as the underlying text layout.
33 class ACE_EXPORT TextLayoutAlgorithm : public BoxLayoutAlgorithm {
34     DECLARE_ACE_TYPE(TextLayoutAlgorithm, BoxLayoutAlgorithm);
35 
36 public:
37     TextLayoutAlgorithm();
38 
TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spanItemChildren,const RefPtr<Paragraph> & paragraph)39     TextLayoutAlgorithm(std::list<RefPtr<SpanItem>> spanItemChildren, const RefPtr<Paragraph>& paragraph)
40         : spanItemChildren_(std::move(spanItemChildren)), paragraph_(paragraph)
41     {}
42 
43     ~TextLayoutAlgorithm() override = default;
44 
45     void OnReset() override;
46 
47     void Measure(LayoutWrapper* layoutWrapper) override;
48 
49     std::optional<SizeF> MeasureContent(
50         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
51 
52     const RefPtr<Paragraph>& GetParagraph();
53 
54     std::list<RefPtr<SpanItem>>&& GetSpanItemChildren();
55 
56     float GetBaselineOffset() const;
57 
58 private:
59     bool CreateParagraph(const TextStyle& textStyle, std::string content);
60     bool CreateParagraphAndLayout(
61         const TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint);
62     bool AdaptMinTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
63         const RefPtr<PipelineContext>& pipeline);
64     bool DidExceedMaxLines(const SizeF& maxSize);
65     static TextDirection GetTextDirection(const std::string& content);
66     float GetTextWidth() const;
67     SizeF GetMaxMeasureSize(const LayoutConstraintF& contentConstraint) const;
68 
69     std::list<RefPtr<SpanItem>> spanItemChildren_;
70     RefPtr<Paragraph> paragraph_;
71     float baselineOffset_ = 0.0f;
72 
73     ACE_DISALLOW_COPY_AND_MOVE(TextLayoutAlgorithm);
74 };
75 } // namespace OHOS::Ace::NG
76 
77 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_LAYOUT_ALGORITHM_H
78