1 /* 2 * Copyright (c) 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_PATTERNS_TEXT_TEXT_CONTENT_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TEXT_CONTENT_MODIFIER_H 18 19 #include <optional> 20 21 #include "base/memory/ace_type.h" 22 #include "core/components/common/properties/text_style.h" 23 #include "core/components_ng/base/modifier.h" 24 #include "core/components_ng/property/property.h" 25 #include "core/components_ng/render/animation_utils.h" 26 #include "core/components_ng/render/paragraph.h" 27 28 namespace OHOS::Ace::NG { 29 class TextContentModifier : public ContentModifier { 30 DECLARE_ACE_TYPE(TextContentModifier, ContentModifier) 31 32 public: 33 TextContentModifier(const std::optional<TextStyle> textStyle); 34 35 void onDraw(DrawingContext& drawingContext) override; 36 37 void SetFontFamilies(const std::vector<std::string>& value); 38 void SetFontSize(const Dimension& value); 39 void SetFontWeight(const FontWeight& value); 40 void SetTextColor(const Color& value); 41 void SetTextShadow(const Shadow& value); 42 void SetTextDecoration(const TextDecoration& value); 43 void SetTextDecorationColor(const Color& value); 44 void SetBaselineOffset(const Dimension& value); 45 void SetContentOffset(OffsetF& value); 46 void SetContentSize(SizeF& value); 47 48 void ContentChange(); 49 50 void ModifyTextStyle(TextStyle& textStyle); 51 52 void StartTextRace(); 53 void StopTextRace(); 54 SetParagraph(RefPtr<Paragraph> paragraph)55 void SetParagraph(RefPtr<Paragraph> paragraph) 56 { 57 paragraph_ = paragraph; 58 } 59 SetPrintOffset(const OffsetF & paintOffset)60 void SetPrintOffset(const OffsetF& paintOffset) 61 { 62 paintOffset_ = paintOffset; 63 } 64 SetObscured(const std::vector<ObscuredReasons> & reasons)65 void SetObscured(const std::vector<ObscuredReasons>& reasons) 66 { 67 obscuredReasons_ = reasons; 68 } 69 SetIfHaveSpanItemChildren(bool value)70 void SetIfHaveSpanItemChildren(bool value) 71 { 72 ifHaveSpanItemChildren_ = value; 73 } 74 SetDrawObscuredRects(const std::vector<Rect> & drawObscuredRects)75 void SetDrawObscuredRects(const std::vector<Rect>& drawObscuredRects) 76 { 77 drawObscuredRects_ = drawObscuredRects; 78 } 79 80 bool NeedMeasureUpdate(PropertyChangeFlag& flag); 81 82 void SetClip(bool clip); 83 84 void SetFontReady(bool value); 85 86 private: 87 double NormalizeToPx(const Dimension& dimension); 88 void SetDefaultAnimatablePropertyValue(const TextStyle& textStyle); 89 void SetDefaultFontSize(const TextStyle& textStyle); 90 void SetDefaultFontWeight(const TextStyle& textStyle); 91 void SetDefaultTextColor(const TextStyle& textStyle); 92 void SetDefaultTextShadow(const TextStyle& textStyle); 93 void SetDefaultTextDecoration(const TextStyle& textStyle); 94 void SetDefaultBaselineOffset(const TextStyle& textStyle); 95 96 float GetTextRacePercent(); 97 98 void ModifyFontSizeInTextStyle(TextStyle& textStyle); 99 void ModifyFontWeightInTextStyle(TextStyle& textStyle); 100 void ModifyTextColorInTextStyle(TextStyle& textStyle); 101 void ModifyTextShadowsInTextStyle(TextStyle& textStyle); 102 void ModifyDecorationInTextStyle(TextStyle& textStyle); 103 void ModifyBaselineOffsetInTextStyle(TextStyle& textStyle); 104 105 void UpdateFontSizeMeasureFlag(PropertyChangeFlag& flag); 106 void UpdateFontWeightMeasureFlag(PropertyChangeFlag& flag); 107 void UpdateTextColorMeasureFlag(PropertyChangeFlag& flag); 108 void UpdateTextShadowMeasureFlag(PropertyChangeFlag& flag); 109 void UpdateTextDecorationMeasureFlag(PropertyChangeFlag& flag); 110 void UpdateBaselineOffsetMeasureFlag(PropertyChangeFlag& flag); 111 112 void DrawObscuration(DrawingContext& drawingContext); 113 114 private: 115 std::optional<Dimension> fontSize_; 116 RefPtr<AnimatablePropertyFloat> fontSizeFloat_; 117 118 std::optional<FontWeight> fontWeight_; 119 RefPtr<AnimatablePropertyFloat> fontWeightFloat_; 120 121 std::optional<Color> textColor_; 122 RefPtr<AnimatablePropertyColor> animatableTextColor_; 123 124 std::optional<Shadow> textShadow_; 125 RefPtr<AnimatablePropertyFloat> shadowBlurRadiusFloat_; 126 RefPtr<AnimatablePropertyFloat> shadowOffsetXFloat_; 127 RefPtr<AnimatablePropertyFloat> shadowOffsetYFloat_; 128 RefPtr<AnimatablePropertyColor> shadowColor_; 129 130 float oldColorAlpha_ { 0.0f }; 131 std::optional<TextDecoration> textDecoration_; 132 std::optional<Color> textDecorationColor_; 133 RefPtr<AnimatablePropertyFloat> textDecorationColorAlpha_; 134 bool textDecorationAnimatable_ { false }; 135 136 std::optional<Dimension> baselineOffset_; 137 RefPtr<AnimatablePropertyFloat> baselineOffsetFloat_; 138 139 bool textRacing_ = false; 140 RefPtr<AnimatablePropertyFloat> racePercentFloat_; 141 std::shared_ptr<AnimationUtils::Animation> raceAnimation_; 142 143 RefPtr<PropertyOffsetF> contentOffset_; 144 RefPtr<PropertySizeF> contentSize_; 145 RefPtr<PropertyBool> contentChange_; 146 RefPtr<PropertyBool> clip_; 147 RefPtr<PropertyString> fontFamilyString_; 148 RefPtr<PropertyBool> fontReady_; 149 150 RefPtr<Paragraph> paragraph_; 151 OffsetF paintOffset_; 152 float textRaceSpaceWidth_ = 0; 153 154 std::vector<ObscuredReasons> obscuredReasons_; 155 bool ifHaveSpanItemChildren_ = false; 156 std::vector<Rect> drawObscuredRects_; 157 158 ACE_DISALLOW_COPY_AND_MOVE(TextContentModifier); 159 }; 160 } // namespace OHOS::Ace::NG 161 162 #endif