diff --git a/src/kiwano/2d/TextActor.cpp b/src/kiwano/2d/TextActor.cpp index 79890253..462920e5 100644 --- a/src/kiwano/2d/TextActor.cpp +++ b/src/kiwano/2d/TextActor.cpp @@ -222,16 +222,20 @@ void TextActor::SetTextLayout(TextLayoutPtr layout) void TextActor::SetPreRenderEnabled(bool enable) { - if (enable) + const bool enabled = texture_cached_ != nullptr; + if (enabled != enable) { - texture_cached_ = MakePtr(); + if (enable) + { + texture_cached_ = MakePtr(); + } + else + { + texture_cached_ = nullptr; + } + render_ctx_ = nullptr; + is_cache_dirty_ = true; } - else - { - texture_cached_ = nullptr; - } - render_ctx_ = nullptr; - is_cache_dirty_ = true; } void TextActor::Update(Duration dt) @@ -273,6 +277,10 @@ void TextActor::ForceUpdateLayout() void TextActor::UpdateCachedTexture() { + // 有文字描边或其他额外渲染时,需要开启预渲染以提升性能 + this->SetPreRenderEnabled(outline_brush_ || style_.show_strikethrough || style_.show_underline + || (layout_ && layout_->GetContentLength() > 30)); + if (!texture_cached_) { return; diff --git a/src/kiwano/2d/TextActor.h b/src/kiwano/2d/TextActor.h index 74b01123..f6501a22 100644 --- a/src/kiwano/2d/TextActor.h +++ b/src/kiwano/2d/TextActor.h @@ -142,10 +142,6 @@ public: /// @brief 设置文本布局 void SetTextLayout(TextLayoutPtr layout); - /// \~chinese - /// @brief 设置预渲染模式,在描边等情况下会有更好的性能 - void SetPreRenderEnabled(bool enable); - /// \~chinese /// @brief 更新脏文字布局 /// @details 仅当文字布局脏时更新 @@ -165,6 +161,10 @@ protected: void UpdateCachedTexture(); + /// \~chinese + /// @brief 设置预渲染模式,在描边等情况下会有更好的性能 + void SetPreRenderEnabled(bool enable); + private: bool is_cache_dirty_; String content_; diff --git a/src/kiwano/render/TextLayout.h b/src/kiwano/render/TextLayout.h index c200112f..6df086db 100644 --- a/src/kiwano/render/TextLayout.h +++ b/src/kiwano/render/TextLayout.h @@ -68,6 +68,10 @@ public: /// @brief 获取文本行数 uint32_t GetLineCount() const; + /// \~chinese + /// @brief 获取文本长度 + uint32_t GetContentLength() const; + /// \~chinese /// @brief 设置字体 /// @param font 字体 @@ -136,6 +140,11 @@ inline void TextLayout::Clear() ResetNativePointer(); } +inline uint32_t TextLayout::GetContentLength() const +{ + return content_length_; +} + inline TextLayout::DirtyFlag TextLayout::GetDirtyFlag() const { return dirty_flag_;