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/pattern/pattern.h"
25 #include "core/components_ng/pattern/rich_editor/paragraph_manager.h"
26 #include "core/components_ng/property/property.h"
27 #include "core/components_ng/render/animation_utils.h"
28 #include "core/components_ng/render/paragraph.h"
29 
30 namespace OHOS::Ace::NG {
31 
32 enum class MarqueeState {
33     IDLE, RUNNING, PAUSED, STOPPED
34 };
35 
36 class TextContentModifier : public ContentModifier {
37     DECLARE_ACE_TYPE(TextContentModifier, ContentModifier)
38 
39 public:
40     explicit TextContentModifier(const std::optional<TextStyle>& textStyle, const WeakPtr<Pattern>& pattern = nullptr);
41     ~TextContentModifier() override = default;
42 
43     void onDraw(DrawingContext& drawingContext) override;
44 
45     void SetFontSize(const Dimension& value, const TextStyle& textStyle, bool isReset = false);
46     void SetAdaptMinFontSize(const Dimension& value, const TextStyle& textStyle, bool isReset = false);
47     void SetAdaptMaxFontSize(const Dimension& value, const TextStyle& textStyle, bool isReset = false);
48     void SetFontWeight(const FontWeight& value, bool isReset = false);
49     void SetTextColor(const Color& value, bool isReset = false);
50     void SetTextShadow(const std::vector<Shadow>& value);
51     void SetTextDecoration(const TextDecoration& value, bool isReset = false);
52     void SetTextDecorationColor(const Color& value, bool isReset = false);
53     void SetBaselineOffset(const Dimension& value, const TextStyle& textStyle, bool isReset = false);
54     void SetLineHeight(const Dimension& value, const TextStyle& textStyle, bool isReset = false);
55     void SetContentOffset(OffsetF& value);
56     void SetContentSize(SizeF& value);
57 
58     void ContentChange();
59 
60     void ModifyTextStyle(TextStyle& textStyle);
61 
62     void StartTextRace();
63     void StopTextRace();
64     void ResumeAnimation();
65     void PauseAnimation();
66 
SetPrintOffset(const OffsetF & paintOffset)67     void SetPrintOffset(const OffsetF& paintOffset)
68     {
69         paintOffset_ = paintOffset;
70     }
71 
SetIfPaintObscuration(bool value)72     void SetIfPaintObscuration(bool value)
73     {
74         ifPaintObscuration_ = value;
75     }
76 
SetDrawObscuredRects(const std::vector<RectF> & drawObscuredRects)77     void SetDrawObscuredRects(const std::vector<RectF>& drawObscuredRects)
78     {
79         drawObscuredRects_ = drawObscuredRects;
80     }
81 
82     bool NeedMeasureUpdate(PropertyChangeFlag& flag);
83 
84     void SetClip(bool clip);
85 
86     void SetFontReady(bool value);
87     void ChangeDragStatus();
88 
SetImageSpanNodeList(std::vector<WeakPtr<FrameNode>> imageNodeList)89     void SetImageSpanNodeList(std::vector<WeakPtr<FrameNode>> imageNodeList)
90     {
91         imageNodeList_ = imageNodeList;
92     }
93 protected:
GetPaintOffset()94     OffsetF GetPaintOffset() const
95     {
96         return paintOffset_;
97     }
98 private:
99     double NormalizeToPx(const Dimension& dimension);
100     void SetDefaultAnimatablePropertyValue(const TextStyle& textStyle);
101     void SetDefaultFontSize(const TextStyle& textStyle);
102     void SetDefaultAdaptMinFontSize(const TextStyle& textStyle);
103     void SetDefaultAdaptMaxFontSize(const TextStyle& textStyle);
104     void SetDefaultFontWeight(const TextStyle& textStyle);
105     void SetDefaultTextColor(const TextStyle& textStyle);
106     void SetDefaultTextShadow(const TextStyle& textStyle);
107     void AddShadow(const Shadow& shadow);
108     void AddDefaultShadow();
109     void SetDefaultTextDecoration(const TextStyle& textStyle);
110     void SetDefaultBaselineOffset(const TextStyle& textStyle);
111     void SetDefaultLineHeight(const TextStyle& textStyle);
112     float GetTextRacePercent();
113     TextDirection GetTextRaceDirection() const;
114 
115     void ModifyFontSizeInTextStyle(TextStyle& textStyle);
116     void ModifyAdaptMinFontSizeInTextStyle(TextStyle& textStyle);
117     void ModifyAdaptMaxFontSizeInTextStyle(TextStyle& textStyle);
118     void ModifyFontWeightInTextStyle(TextStyle& textStyle);
119     void ModifyTextColorInTextStyle(TextStyle& textStyle);
120     void ModifyTextShadowsInTextStyle(TextStyle& textStyle);
121     void ModifyDecorationInTextStyle(TextStyle& textStyle);
122     void ModifyBaselineOffsetInTextStyle(TextStyle& textStyle);
123     void ModifyLineHeightInTextStyle(TextStyle& textStyle);
124 
125     void UpdateFontSizeMeasureFlag(PropertyChangeFlag& flag);
126     void UpdateAdaptMinFontSizeMeasureFlag(PropertyChangeFlag& flag);
127     void UpdateAdaptMaxFontSizeMeasureFlag(PropertyChangeFlag& flag);
128     void UpdateFontWeightMeasureFlag(PropertyChangeFlag& flag);
129     void UpdateTextColorMeasureFlag(PropertyChangeFlag& flag);
130     void UpdateTextShadowMeasureFlag(PropertyChangeFlag& flag);
131     void UpdateTextDecorationMeasureFlag(PropertyChangeFlag& flag);
132     void UpdateBaselineOffsetMeasureFlag(PropertyChangeFlag& flag);
133     void UpdateLineHeightMeasureFlag(PropertyChangeFlag& flag);
134     bool CheckNeedMeasure(float finalValue, float lastValue, float currentValue);
135 
136     void DrawObscuration(DrawingContext& drawingContext);
137     void UpdateImageNodeVisible(const VisibleType visible);
138     void PaintImage(RSCanvas& canvas, float x, float y);
139     bool DrawImage(const RefPtr<FrameNode>& imageNode, RSCanvas& canvas, float x, float y, const RectF& rect);
140     void PaintCustomSpan(DrawingContext& drawingContext);
141     void DrawTextRacing(DrawingContext& drawingContext);
142     void SetMarqueeState(MarqueeState state);
CheckMarqueeState(MarqueeState state)143     bool CheckMarqueeState(MarqueeState state)
144     {
145         return marqueeState_ == state;
146     }
147     int32_t GetDuration() const;
148 
149     std::optional<Dimension> fontSize_;
150     float lastFontSize_ = 0.0f;
151     RefPtr<AnimatablePropertyFloat> fontSizeFloat_;
152 
153     std::optional<Dimension> adaptMinFontSize_;
154     RefPtr<AnimatablePropertyFloat> adaptMinFontSizeFloat_;
155     float lastMinFontSize_ = 0.0f;
156 
157     std::optional<Dimension> adaptMaxFontSize_;
158     RefPtr<AnimatablePropertyFloat> adaptMaxFontSizeFloat_;
159     float lastMaxFontSize_ = 0.0f;
160 
161     std::optional<FontWeight> fontWeight_;
162     RefPtr<AnimatablePropertyFloat> fontWeightFloat_;
163     float lastFontWeight_ = 0.0f;
164 
165     std::optional<Color> textColor_;
166     RefPtr<AnimatablePropertyColor> animatableTextColor_;
167     Color lastTextColor_;
168 
169     struct ShadowProp {
170         Shadow shadow; // final shadow configuration of the animation
171         Shadow lastShadow;
172         RefPtr<AnimatablePropertyFloat> blurRadius;
173         RefPtr<AnimatablePropertyFloat> offsetX;
174         RefPtr<AnimatablePropertyFloat> offsetY;
175         RefPtr<AnimatablePropertyColor> color;
176     };
177     std::vector<ShadowProp> shadows_;
178 
179     std::optional<TextDecoration> textDecoration_;
180     std::optional<Color> textDecorationColor_;
181     Color lastTextDecorationColor_;
182     RefPtr<AnimatablePropertyFloat> textDecorationColorAlpha_;
183     float lastTextDecorationColorAlpha_ = 0.0f;
184     bool textDecorationAnimatable_ { false };
185 
186     std::optional<Dimension> baselineOffset_;
187     RefPtr<AnimatablePropertyFloat> baselineOffsetFloat_;
188     float lastBaselineOffsetFloat_ = 0.0f;
189 
190     std::optional<Dimension> lineHeight_;
191     RefPtr<AnimatablePropertyFloat> lineHeightFloat_;
192     float lastLineHeight_ = 0.0f;
193 
194     WeakPtr<Pattern> pattern_;
195     RefPtr<AnimatablePropertyFloat> racePercentFloat_;
196     std::shared_ptr<AnimationUtils::Animation> raceAnimation_;
197 
198     RefPtr<PropertyOffsetF> contentOffset_;
199     RefPtr<PropertySizeF> contentSize_;
200     RefPtr<PropertyInt> contentChange_;
201     RefPtr<PropertyBool> clip_;
202     RefPtr<PropertyBool> fontReady_;
203     RefPtr<PropertyBool> dragStatus_;
204     OffsetF paintOffset_;
205     float textRaceSpaceWidth_ = 0;
206 
207     bool ifPaintObscuration_ = false;
208     std::vector<RectF> drawObscuredRects_;
209     std::vector<WeakPtr<FrameNode>> imageNodeList_;
210     MarqueeState marqueeState_ = MarqueeState::IDLE;
211     ACE_DISALLOW_COPY_AND_MOVE(TextContentModifier);
212 };
213 } // namespace OHOS::Ace::NG
214 
215 #endif