• 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_FIELD_TEXT_FIELD_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_LAYOUT_ALGORITHM_H
18 
19 #include <string>
20 #include <utility>
21 
22 #include "base/geometry/ng/offset_t.h"
23 #include "base/geometry/rect.h"
24 #include "base/memory/referenced.h"
25 #include "core/components/text_field/textfield_theme.h"
26 #include "core/components_ng/layout/layout_wrapper.h"
27 #include "core/components_ng/pattern/text/text_styles.h"
28 #include "core/components_ng/pattern/text_field/text_field_layout_property.h"
29 #include "core/components_ng/render/drawing.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 constexpr Dimension SCROLL_BAR_LEFT_WIDTH = 2.0_vp;
34 
35 class TextFieldContentModifier;
36 class ACE_EXPORT TextFieldLayoutAlgorithm : public LayoutAlgorithm {
37     DECLARE_ACE_TYPE(TextFieldLayoutAlgorithm, LayoutAlgorithm);
38 
39 public:
40     TextFieldLayoutAlgorithm() = default;
41 
42     ~TextFieldLayoutAlgorithm() override = default;
43 
OnReset()44     void OnReset() override
45     {
46         paragraph_.reset();
47     }
48 
49     void Measure(LayoutWrapper* layoutWrapper) override;
50 
51     std::optional<SizeF> MeasureContent(
52         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override;
53 
54     void Layout(LayoutWrapper* layoutWrapper) override;
55 
56     const std::shared_ptr<RSParagraph>& GetParagraph();
57 
58     const std::shared_ptr<RSParagraph>& GetCounterParagraph() const;
59     const std::shared_ptr<RSParagraph>& GetErrorParagraph() const;
60 
GetTextRect()61     const RectF& GetTextRect() const
62     {
63         return textRect_;
64     }
65 
GetImageRect()66     const RectF& GetImageRect() const
67     {
68         return imageRect_;
69     }
70 
GetFrameRect()71     const RectF& GetFrameRect() const
72     {
73         return frameRect_;
74     }
75 
GetCaretOffsetX()76     float GetCaretOffsetX() const
77     {
78         return caretOffsetX_;
79     }
80 
SetCaretOffset(float offsetX)81     void SetCaretOffset(float offsetX)
82     {
83         caretOffsetX_ = offsetX;
84     }
85 
GetParentGlobalOffset()86     const OffsetF& GetParentGlobalOffset() const
87     {
88         return parentGlobalOffset_;
89     }
90 
GetUnitWidth()91     float GetUnitWidth() const
92     {
93         return unitWidth_;
94     }
95 
GetParagraphWidth()96     float GetParagraphWidth() const
97     {
98         return paragraphWidth_;
99     }
100 
101     static TextDirection GetTextDirection(const std::string& content);
102 
103     static void UpdateTextStyle(const RefPtr<FrameNode>& frameNode,
104         const RefPtr<TextFieldLayoutProperty>& layoutProperty, const RefPtr<TextFieldTheme>& theme,
105         TextStyle& textStyle, bool isDisabled);
106     static void UpdatePlaceholderTextStyle(const RefPtr<FrameNode>& frameNode,
107         const RefPtr<TextFieldLayoutProperty>& layoutProperty,  const RefPtr<TextFieldTheme>& theme,
108         TextStyle& textStyle, bool isDisabled);
109 
110 private:
111     static void FontRegisterCallback(const RefPtr<FrameNode>& frameNode, const std::vector<std::string>& fontFamilies);
112     void CreateParagraph(const TextStyle& textStyle, std::string content, bool needObscureText,
113         int32_t nakedCharPosition, bool disableTextAlign);
114     void CreateParagraph(const std::vector<TextStyle>& textStyles, const std::vector<std::string>& contents,
115         const std::string& content, bool needObscureText, bool disableTextAlign);
116     void CreateCounterParagraph(int32_t textLength, int32_t maxLength, const RefPtr<TextFieldTheme>& theme);
117     void CreateErrorParagraph(const std::string& content, const RefPtr<TextFieldTheme>& theme);
118     bool CreateParagraphAndLayout(
119         const TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint);
120     bool AdaptMinTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint,
121         const RefPtr<PipelineContext>& pipeline);
122     bool DidExceedMaxLines(const LayoutConstraintF& contentConstraint);
123     void SetPropertyToModifier(const TextStyle& textStyle, RefPtr<TextFieldContentModifier> modifier);
124 
125     float GetTextFieldDefaultHeight();
126     float GetTextFieldDefaultImageHeight();
127 
128     int32_t ConvertTouchOffsetToCaretPosition(const Offset& localOffset);
129     void UpdateUnitLayout(LayoutWrapper* layoutWrapper);
130 
131     std::shared_ptr<RSParagraph> paragraph_;
132     std::shared_ptr<RSParagraph> counterParagraph_;
133     std::shared_ptr<RSParagraph> errorParagraph_;
134     RectF frameRect_;
135     RectF textRect_;
136     RectF imageRect_;
137     OffsetF parentGlobalOffset_;
138     float paragraphWidth_ = 0.0f;
139 
140     float caretOffsetX_ = 0.0f;
141     float unitWidth_ = 0.0f;
142 
143     ACE_DISALLOW_COPY_AND_MOVE(TextFieldLayoutAlgorithm);
144 };
145 } // namespace OHOS::Ace::NG
146 
147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_LAYOUT_ALGORITHM_H
148