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 ROSEN_MODULES_TEXGINE_SRC_TEXT_SPAN_H 17 #define ROSEN_MODULES_TEXGINE_SRC_TEXT_SPAN_H 18 19 #include <any> 20 21 #include "char_groups.h" 22 #include "texgine_canvas.h" 23 #include "texgine_font_metrics.h" 24 #include "texgine_text_blob.h" 25 #include "texgine/typography.h" 26 #include "texgine/typography_style.h" 27 #include "symbol_animation_config.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 namespace TextEngine { 32 class TextSpan : public std::enable_shared_from_this<TextSpan> { 33 public: 34 static std::shared_ptr<TextSpan> MakeEmpty(); 35 static std::shared_ptr<TextSpan> MakeFromText(std::string const &text); 36 static std::shared_ptr<TextSpan> MakeFromText(std::u16string const &text); 37 static std::shared_ptr<TextSpan> MakeFromText(std::u32string const &text); 38 static std::shared_ptr<TextSpan> MakeFromText(std::vector<uint16_t> const &text); 39 static std::shared_ptr<TextSpan> MakeFromText(std::vector<uint32_t> const &text); 40 static std::shared_ptr<TextSpan> MakeFromCharGroups(const CharGroups &cgs); 41 42 TextSpan() = default; 43 virtual ~TextSpan() = default; 44 void AddUTF16Text(std::vector<uint16_t> const &text); 45 46 double GetWidth() const; 47 double GetHeight() const; 48 double GetPostBreak() const; 49 double GetPreBreak() const; 50 bool IsRTL() const; 51 52 void PaintDecoration(TexgineCanvas &canvas, double offsetX, double offsetY, const TextStyle &xs); 53 void PaintDecorationStyle(TexgineCanvas &canvas, double left, double right, double y, const TextStyle &xs); 54 void Paint(TexgineCanvas &canvas, double offsetX, double offsetY, const TextStyle &xs, const RoundRectType &rType); 55 void PaintShadow(TexgineCanvas &canvas, double offsetX, double offsetY, const std::vector<TextShadow> &shadows); 56 void SymbolAnimation(const TextStyle &xs); 57 58 std::shared_ptr<TextSpan> CloneWithCharGroups(CharGroups const &cgs); 59 60 void operator+=(TextSpan const &textSpan) 61 { 62 u16vect_.insert(u16vect_.end(), textSpan.u16vect_.begin(), textSpan.u16vect_.end()); 63 } 64 65 std::shared_ptr<TexgineFontMetrics> tmetrics_ = std::make_shared<TexgineFontMetrics>(); 66 bool rtl_ = false; 67 std::shared_ptr<Typeface> typeface_ = nullptr; 68 69 std::vector<uint16_t> u16vect_; 70 std::shared_ptr<TexgineTextBlob> textBlob_ = nullptr; 71 std::vector<double> glyphWidths_; 72 CharGroups cgs_; 73 74 double preBreak_ = 0.0; 75 double postBreak_ = 0.0; 76 double width_ = 0.0; 77 double topInGroup_ = 0.0; 78 double bottomInGroup_ = 0.0; 79 double maxRoundRectRadius_ = 0.0; 80 81 uint64_t symbolId_ = 0; // 0: text_span, > 0: symbol 82 SetAnimation(const std::function<bool (const std::shared_ptr<OHOS::Rosen::TextEngine::SymbolAnimationConfig> &)> & animationFunc)83 void SetAnimation( 84 const std::function<bool( 85 const std::shared_ptr<OHOS::Rosen::TextEngine::SymbolAnimationConfig>&)>& animationFunc) 86 { 87 if (animationFunc) { 88 animationFunc_ = animationFunc; 89 } 90 } 91 SetSymbolId(const uint64_t & symbolId)92 void SetSymbolId(const uint64_t& symbolId) 93 { 94 symbolId_ = symbolId; 95 } 96 private: 97 98 std::function<bool( 99 const std::shared_ptr<OHOS::Rosen::TextEngine::SymbolAnimationConfig>&)> animationFunc_ = nullptr; 100 101 friend class TextBreaker; 102 friend class BidiProcesser; 103 friend class ControllerForTest; 104 friend class TextMerger; 105 friend class LineBreaker; 106 friend class TypographyImpl; 107 friend class VariantSpan; 108 friend class TextShaper; 109 friend class TextReverser; 110 friend void ReportMemoryUsage(std::string const &member, TextSpan const &that, bool needThis); 111 }; 112 } // namespace TextEngine 113 } // namespace Rosen 114 } // namespace OHOS 115 116 #endif // ROSEN_MODULES_TEXGINE_SRC_TEXT_SPAN_H 117