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/memory/referenced.h" 24 #include "core/components/text_field/textfield_theme.h" 25 #include "core/components_ng/layout/layout_wrapper.h" 26 #include "core/components_ng/pattern/text_field/text_field_layout_property.h" 27 28 namespace OHOS::Ace::NG { 29 30 struct InlineMeasureItem { 31 float inlineScrollRectOffsetX = 0.0f; 32 float inlineLastOffsetY = 0.0f; 33 float inlineContentRectHeight = 0.0f; 34 float inlineSizeHeight = 0.0f; 35 }; 36 37 class TextFieldContentModifier; 38 class ACE_EXPORT TextFieldLayoutAlgorithm : public LayoutAlgorithm { 39 DECLARE_ACE_TYPE(TextFieldLayoutAlgorithm, LayoutAlgorithm); 40 41 public: 42 TextFieldLayoutAlgorithm() = default; 43 44 ~TextFieldLayoutAlgorithm() override = default; 45 OnReset()46 void OnReset() override 47 { 48 paragraph_->Reset(); 49 } 50 51 const RefPtr<Paragraph>& GetParagraph() const; 52 GetTextRect()53 const RectF& GetTextRect() const 54 { 55 return textRect_; 56 } 57 GetParentGlobalOffset()58 const OffsetF& GetParentGlobalOffset() const 59 { 60 return parentGlobalOffset_; 61 } 62 GetUnitWidth()63 float GetUnitWidth() const 64 { 65 return unitWidth_; 66 } 67 GetInlineMeasureItem()68 InlineMeasureItem GetInlineMeasureItem() const 69 { 70 return inlineMeasureItem_; 71 } 72 73 static TextDirection GetTextDirection(const std::string& content, TextDirection direction = TextDirection::AUTO); 74 75 static void UpdateTextStyle(const RefPtr<FrameNode>& frameNode, 76 const RefPtr<TextFieldLayoutProperty>& layoutProperty, const RefPtr<TextFieldTheme>& theme, 77 TextStyle& textStyle, bool isDisabled); 78 static void UpdatePlaceholderTextStyle(const RefPtr<FrameNode>& frameNode, 79 const RefPtr<TextFieldLayoutProperty>& layoutProperty, const RefPtr<TextFieldTheme>& theme, 80 TextStyle& textStyle, bool isDisabled); 81 void CounterLayout(LayoutWrapper* layoutWrapper); 82 float CounterNodeMeasure(float contentWidth, LayoutWrapper* layoutWrapper); 83 void UpdateCounterTextMargin(LayoutWrapper* layoutWrapper); 84 void UpdateCounterBorderStyle(uint32_t& textLength, uint32_t& maxLength, LayoutWrapper* layoutWrapper); 85 void UpdateCounterNode(uint32_t textLength, uint32_t maxLength, const LayoutConstraintF& contentConstraint, 86 LayoutWrapper* layoutWrapper); 87 88 protected: 89 static void FontRegisterCallback(const RefPtr<FrameNode>& frameNode, const std::vector<std::string>& fontFamilies); 90 void CreateParagraph(const TextStyle& textStyle, std::string content, bool needObscureText, 91 int32_t nakedCharPosition, bool disableTextAlign = false); 92 void CreateParagraph(const TextStyle& textStyle, const std::vector<std::string>& contents, 93 const std::string& content, bool needObscureText, bool disableTextAlign = false); 94 void CreateInlineParagraph(const TextStyle& textStyle, std::string content, bool needObscureText, 95 int32_t nakedCharPosition, bool disableTextAlign = false); 96 bool CreateParagraphAndLayout( 97 const TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint); 98 bool AdaptMinTextSize(TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint, 99 const RefPtr<PipelineContext>& pipeline); 100 bool DidExceedMaxLines(const LayoutConstraintF& contentConstraint); 101 void SetPropertyToModifier(const TextStyle& textStyle, RefPtr<TextFieldContentModifier> modifier); 102 103 float GetTextFieldDefaultHeight(); 104 105 void ConstructTextStyles( 106 const RefPtr<FrameNode>& frameNode, TextStyle& textStyle, std::string& textContent, bool& showPlaceHolder); 107 LayoutConstraintF CalculateContentMaxSizeWithCalculateConstraint( 108 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper); 109 110 int32_t ConvertTouchOffsetToCaretPosition(const Offset& localOffset); 111 void UpdateUnitLayout(LayoutWrapper* layoutWrapper); 112 ParagraphStyle GetParagraphStyle(const TextStyle& textStyle, const std::string& content) const; 113 void GetInlineMeasureItem( 114 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, float& inlineIdealHeight); 115 float ConstraintWithMinWidth( 116 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, 117 RefPtr<Paragraph>& paragraph, float removeValue = 0.0f); 118 SizeF GetConstraintSize(const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper); 119 std::optional<SizeF> InlineMeasureContent(const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper); 120 SizeF PlaceHolderMeasureContent( 121 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, float imageWidth = 0.0f); 122 SizeF TextInputMeasureContent( 123 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper, float imageWidth); 124 SizeF TextAreaMeasureContent(const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper); 125 RefPtr<Paragraph> paragraph_; 126 RefPtr<Paragraph> inlineParagraph_; 127 InlineMeasureItem inlineMeasureItem_; 128 129 RectF textRect_; 130 OffsetF parentGlobalOffset_; 131 std::string textContent_; 132 bool showPlaceHolder_ = false; 133 float preferredHeight_ = 0.0f; 134 TextDirection direction_ = TextDirection::AUTO; 135 136 float unitWidth_ = 0.0f; 137 bool autoWidth_ = false; 138 139 private: 140 float GetVisualTextWidth() const; 141 void CalcInlineMeasureItem(LayoutWrapper* layoutWrapper); 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