• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.. All rights reserved.
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 OHOS_ROSEN_TEXT_TEXT_FLIP_EFFECT_H
17 #define OHOS_ROSEN_TEXT_TEXT_FLIP_EFFECT_H
18 
19 #include <functional>
20 #include <vector>
21 
22 #include "rosen_text/symbol_animation_config.h"
23 #include "text_effect.h"
24 
25 namespace OHOS::Rosen {
26 struct TextFlipEffectCharConfig {
27     std::vector<std::pair<float, int>> yUpOffsets;
28     std::vector<std::pair<float, int>> yDownOffsets;
29     std::vector<std::pair<float, int>> scales;
30     std::vector<std::pair<float, int>> opacity;
31     std::vector<std::pair<int, int>> blur;
32 };
33 
34 enum class TextEffectFlipDirection {
35     UP,
36     DOWN,
37 };
38 
39 class TextFlipEffect : public TextEffect {
40 public:
41     TextFlipEffect();
42     ~TextFlipEffect() override;
43 
44     int UpdateEffectConfig(const std::unordered_map<TextEffectAttribute, std::string>& config) override;
45     int AppendTypography(const std::vector<TypographyConfig>& typographyConfigs) override;
46     void RemoveTypography(const std::vector<TypographyConfig>& typographyConfigs) override;
47     int UpdateTypography(std::vector<std::pair<TypographyConfig, TypographyConfig>>& typographyConfigs) override;
48     void StartEffect(Drawing::Canvas* canvas, double x, double y) override;
49     void StopEffect() override;
50     void NoEffect(Drawing::Canvas* canvas, double x, double y) override;
51 
52 private:
53     bool CheckInputParams(const std::unordered_map<TextEffectAttribute, std::string>& config);
54     bool CheckDirection(const std::string& direction);
55     void SetDirection(const std::string& direction);
56     bool CheckBlurEnable(const std::string& enable);
57     void SetBlurEnable(const std::string& enable);
58     void DrawTextFlip(std::vector<TextBlobRecordInfo>& infos, Drawing::Canvas* canvas, double x, double y);
59     void DrawResidualText(Drawing::Canvas* canvas, double height,
60         const std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& func);
61     void DrawTextFlipElements(Drawing::Canvas* canvas, double height, const Drawing::Color& color,
62         const std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& func,
63         const std::vector<std::vector<TextEngine::TextEffectElement>>& effectElements);
64     void ClearTypography();
65     std::vector<std::vector<TextEngine::TextEffectElement>> GenerateChangeElements(
66         const std::shared_ptr<Drawing::TextBlob>& blob, double x, double y);
67     std::vector<std::vector<Drawing::DrawingPiecewiseParameter>> GenerateFlipConfig(double height);
68 
69     TypographyConfig typographyConfig_;
70     TextEffectFlipDirection direction_{TextEffectFlipDirection::UP};
71     bool blurEnable_{false};
72     TextFlipEffectCharConfig inChar_;
73     TextFlipEffectCharConfig outChar_;
74     std::vector<uint16_t> lastAllBlobGlyphIds_;
75     std::vector<uint16_t> currentBlobGlyphIds_;
76     size_t currentGlyphIndex_{0};
77     int changeNumber_{0};
78 
79     struct FlipAttributeFunction {
80         FlipAttributeFunction() = default;
FlipAttributeFunctionFlipAttributeFunction81         FlipAttributeFunction(TextEffectAttribute inputAttribute,
82             std::function<bool(TextFlipEffect*, const std::string&)> inputCheck,
83             std::function<void(TextFlipEffect*, const std::string&)> inputSet)
84             : attribute(inputAttribute), checkFunc(inputCheck), setFunc(inputSet) {}
85 
86         TextEffectAttribute attribute{TextEffectAttribute::FLIP_DIRECTION};
87         std::function<bool(TextFlipEffect*, const std::string&)> checkFunc{nullptr};
88         std::function<void(TextFlipEffect*, const std::string&)> setFunc{nullptr};
89     };
90     const std::vector<FlipAttributeFunction> supportAttributes_ = {
91         {TextEffectAttribute::FLIP_DIRECTION, &TextFlipEffect::CheckDirection, &TextFlipEffect::SetDirection},
92         {TextEffectAttribute::BLUR_ENABLE, &TextFlipEffect::CheckBlurEnable, &TextFlipEffect::SetBlurEnable},
93     };
94 };
95 } // namespace OHOS::Rosen
96 #endif // OHOS_ROSEN_TEXT_TEXT_FLIP_EFFECT_H