From acb46290e9a8d9ba7ce4386a7f89cdb681936c1b Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 2 Oct 2017 12:01:40 -0700 Subject: [PATCH] libtxt: search for fallback fonts that can match emoji and CJK characters (flutter/engine#4156) --- .../txt/src/txt/asset_font_manager.cc | 1 - .../txt/src/txt/font_collection.cc | 44 ++++++++++++++++++- .../third_party/txt/src/txt/font_collection.h | 4 ++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc index aadf7e44ec..286a0a63ea 100644 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc +++ b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc @@ -58,7 +58,6 @@ SkTypeface* AssetFontManager::onMatchFamilyStyleCharacter( const char* bcp47[], int bcp47Count, SkUnichar character) const { - FXL_DCHECK(false); return nullptr; } diff --git a/engine/src/flutter/third_party/txt/src/txt/font_collection.cc b/engine/src/flutter/third_party/txt/src/txt/font_collection.cc index f4dbd876c1..461468cd80 100644 --- a/engine/src/flutter/third_party/txt/src/txt/font_collection.cc +++ b/engine/src/flutter/third_party/txt/src/txt/font_collection.cc @@ -30,6 +30,17 @@ namespace txt { +namespace { + +// Example characters representing character classes that may require a +// fallback font. +const std::vector fallback_characters{ + 0x1f600, // emoji + 0x4e00, // CJK +}; + +} // anonymous namespace + FontCollection::FontCollection() = default; FontCollection::~FontCollection() = default; @@ -42,6 +53,7 @@ void FontCollection::PushFront(sk_sp skia_font_manager) { if (!skia_font_manager) { return; } + UpdateFallbackFonts(skia_font_manager); skia_font_managers_.push_front(std::move(skia_font_manager)); } @@ -49,6 +61,7 @@ void FontCollection::PushBack(sk_sp skia_font_manager) { if (!skia_font_manager) { return; } + UpdateFallbackFonts(skia_font_manager); skia_font_managers_.push_back(std::move(skia_font_manager)); } @@ -92,11 +105,12 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { auto minikin_family = std::make_shared(std::move(minikin_fonts)); - // Create a vector of font families for the Minikin font collection. For - // now, we only have one family in our collection. + // Create a vector of font families for the Minikin font collection. std::vector> minikin_families = { minikin_family, }; + for (const auto& fallback : fallback_fonts_) + minikin_families.push_back(fallback.second); // Create the minikin font collection. auto font_collection = @@ -117,4 +131,30 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { return nullptr; } +void FontCollection::UpdateFallbackFonts(sk_sp manager) { + char language_tag[ULOC_FULLNAME_CAPACITY]; + UErrorCode uerr; + uloc_toLanguageTag(icu::Locale::getDefault().getName(), language_tag, + ULOC_FULLNAME_CAPACITY, FALSE, &uerr); + if (U_FAILURE(uerr)) + return; + const char* bcp47[] = {language_tag}; + + for (SkUnichar fallback_char : fallback_characters) { + if (fallback_fonts_.count(fallback_char)) + continue; + + sk_sp skia_typeface(manager->matchFamilyStyleCharacter( + 0, SkFontStyle(), bcp47, 1, fallback_char)); + if (!skia_typeface) + continue; + + std::vector minikin_fonts; + minikin_fonts.emplace_back(std::make_shared(skia_typeface), + minikin::FontStyle()); + fallback_fonts_[fallback_char] = + std::make_shared(std::move(minikin_fonts)); + } +} + } // namespace txt diff --git a/engine/src/flutter/third_party/txt/src/txt/font_collection.h b/engine/src/flutter/third_party/txt/src/txt/font_collection.h index ecc4a52bed..c6a838ec34 100644 --- a/engine/src/flutter/third_party/txt/src/txt/font_collection.h +++ b/engine/src/flutter/third_party/txt/src/txt/font_collection.h @@ -50,6 +50,10 @@ class FontCollection { std::deque> skia_font_managers_; std::unordered_map> font_collections_cache_; + std::unordered_map> + fallback_fonts_; + + void UpdateFallbackFonts(sk_sp manager); FXL_DISALLOW_COPY_AND_ASSIGN(FontCollection); };