From 073d9af904909f4d03e52a8815132d3fa37bf1db Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 13 Dec 2019 15:18:37 -0800 Subject: [PATCH] libtxt: cache fallback fonts found by a Minikin font collection (flutter/engine#14482) If a new fallback font is discovered during paragraph layout, the fallback font cache in txt::FontCollection will use that font in future layouts. However, that cache is not available if the new fallback font needs to be used for other characters within the current layout. This PR adds a cache to minikin::FontCollection and checks whether fonts in the cache can handle a character before calling the fallback font provider. See https://github.com/flutter/engine/pull/13257 --- .../txt/src/minikin/FontCollection.cpp | 30 ++++++++++++++++--- .../txt/src/minikin/FontCollection.h | 9 ++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/third_party/txt/src/minikin/FontCollection.cpp b/engine/src/flutter/third_party/txt/src/minikin/FontCollection.cpp index f4d18c0296..812e1fd31a 100644 --- a/engine/src/flutter/third_party/txt/src/minikin/FontCollection.cpp +++ b/engine/src/flutter/third_party/txt/src/minikin/FontCollection.cpp @@ -302,8 +302,7 @@ const std::shared_ptr& FontCollection::getFamilyForChar( // libtxt: check if the fallback font provider can match this character if (mFallbackFontProvider) { const std::shared_ptr& fallback = - mFallbackFontProvider->matchFallbackFont(ch, - GetFontLocale(langListId)); + findFallbackFont(ch, vs, langListId); if (fallback) { return fallback; } @@ -340,8 +339,7 @@ const std::shared_ptr& FontCollection::getFamilyForChar( // libtxt: check if the fallback font provider can match this character if (mFallbackFontProvider) { const std::shared_ptr& fallback = - mFallbackFontProvider->matchFallbackFont(ch, - GetFontLocale(langListId)); + findFallbackFont(ch, vs, langListId); if (fallback) { return fallback; } @@ -365,6 +363,30 @@ const std::shared_ptr& FontCollection::getFamilyForChar( : mFamilies[bestFamilyIndex]; } +const std::shared_ptr& FontCollection::findFallbackFont( + uint32_t ch, + uint32_t vs, + uint32_t langListId) const { + std::string locale = GetFontLocale(langListId); + + const auto it = mCachedFallbackFamilies.find(locale); + if (it != mCachedFallbackFamilies.end()) { + for (const auto& fallbackFamily : it->second) { + if (calcCoverageScore(ch, vs, fallbackFamily)) { + return fallbackFamily; + } + } + } + + const std::shared_ptr& fallback = + mFallbackFontProvider->matchFallbackFont(ch, GetFontLocale(langListId)); + + if (fallback) { + mCachedFallbackFamilies[locale].push_back(fallback); + } + return fallback; +} + const uint32_t NBSP = 0x00A0; const uint32_t SOFT_HYPHEN = 0x00AD; const uint32_t ZWJ = 0x200C; diff --git a/engine/src/flutter/third_party/txt/src/minikin/FontCollection.h b/engine/src/flutter/third_party/txt/src/minikin/FontCollection.h index 48365a64ac..f79c24ba6f 100644 --- a/engine/src/flutter/third_party/txt/src/minikin/FontCollection.h +++ b/engine/src/flutter/third_party/txt/src/minikin/FontCollection.h @@ -17,6 +17,7 @@ #ifndef MINIKIN_FONT_COLLECTION_H #define MINIKIN_FONT_COLLECTION_H +#include #include #include #include @@ -100,6 +101,9 @@ class FontCollection { uint32_t langListId, int variant) const; + const std::shared_ptr& + findFallbackFont(uint32_t ch, uint32_t vs, uint32_t langListId) const; + uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, int variant, @@ -148,6 +152,11 @@ class FontCollection { // libtxt extension: Fallback font provider. std::unique_ptr mFallbackFontProvider; + + // libtxt extension: Fallback fonts discovered after this font collection + // was constructed. + mutable std::map>> + mCachedFallbackFamilies; }; } // namespace minikin