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_EFFECT_H 17 #define OHOS_ROSEN_TEXT_TEXT_EFFECT_H 18 19 #include <memory> 20 #include <shared_mutex> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "typography.h" 26 27 namespace OHOS::Rosen { 28 enum class TextEffectAttribute { 29 FLIP_DIRECTION, 30 BLUR_ENABLE, 31 }; 32 33 enum class TextEffectState { 34 START, 35 STOP, 36 }; 37 38 enum class TextEffectStrategy { 39 FLIP, 40 STRATEGY_BUTT 41 }; 42 43 constexpr int TEXT_EFFECT_SUCCESS = 0; 44 constexpr int TEXT_EFFECT_UNKNOWN = -1; 45 constexpr int TEXT_EFFECT_INVALID_INPUT = -2; 46 constexpr int TEXT_EFFECT_HAS_EFFECT = -3; 47 48 struct TypographyConfig { 49 std::shared_ptr<Typography> typography{nullptr}; 50 std::pair<size_t, size_t> rawTextRange{0, 0}; 51 }; 52 53 class TextEffect { 54 public: 55 virtual ~TextEffect() = default; 56 virtual int UpdateEffectConfig(const std::unordered_map<TextEffectAttribute, std::string>& config) = 0; 57 virtual int AppendTypography(const std::vector<TypographyConfig>& typographyConfigs) = 0; 58 virtual void RemoveTypography(const std::vector<TypographyConfig>& typographyConfigs) = 0; 59 virtual int UpdateTypography(std::vector<std::pair<TypographyConfig, TypographyConfig>>& typographyConfigs) = 0; 60 virtual void StartEffect(Drawing::Canvas* canvas, double x, double y) = 0; 61 virtual void StopEffect() = 0; 62 virtual void NoEffect(Drawing::Canvas* canvas, double x, double y) = 0; 63 64 TextEffectStrategy strategy_{TextEffectStrategy::STRATEGY_BUTT}; 65 TextEffectState state_{TextEffectState::STOP}; 66 }; 67 68 class TextEffectFactory { 69 public: 70 virtual ~TextEffectFactory() = default; 71 virtual std::shared_ptr<TextEffect> CreateTextEffect() = 0; 72 }; 73 74 class TextEffectFactoryCreator final { 75 public: 76 static TextEffectFactoryCreator& GetInstance(); 77 ~TextEffectFactoryCreator() = default; 78 bool RegisterFactory(TextEffectStrategy strategy, std::shared_ptr<TextEffectFactory> factory); 79 void UnregisterFactory(TextEffectStrategy strategy); 80 std::shared_ptr<TextEffect> CreateTextEffect(TextEffectStrategy strategy); 81 82 private: 83 TextEffectFactoryCreator() = default; 84 TextEffectFactoryCreator(const TextEffectFactoryCreator&) = delete; 85 TextEffectFactoryCreator& operator=(const TextEffectFactoryCreator&) = delete; 86 TextEffectFactoryCreator(TextEffectFactoryCreator&&) = delete; 87 TextEffectFactoryCreator& operator=(TextEffectFactoryCreator&&) = delete; 88 89 std::unordered_map<TextEffectStrategy, std::shared_ptr<TextEffectFactory>> factoryTable_; 90 std::shared_mutex mutex_; 91 }; 92 } // namespace OHOS::Rosen 93 #endif // OHOS_ROSEN_TEXT_TEXT_EFFECT_H