diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index 05b5d6b66f..2983769494 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -29,9 +29,15 @@ namespace txt { FontCollection& FontCollection::GetFontCollection(std::string dir) { + std::vector dirs = {dir}; + return GetFontCollection(std::move(dirs)); +} + +FontCollection& FontCollection::GetFontCollection( + std::vector dirs) { static FontCollection* collection = nullptr; static std::once_flag once; - std::call_once(once, [dir]() { collection = new FontCollection(dir); }); + std::call_once(once, [dirs]() { collection = new FontCollection(dirs); }); return *collection; } @@ -39,28 +45,33 @@ FontCollection& FontCollection::GetDefaultFontCollection() { return GetFontCollection(""); } -FontCollection::FontCollection(std::string dir) { +FontCollection::FontCollection(const std::vector& dirs) { #ifdef DIRECTORY_FONT_MANAGER_AVAILABLE - skia_font_manager_ = dir.length() != 0 - ? SkFontMgr_New_Custom_Directory(dir.c_str()) - : SkFontMgr::RefDefault(); -#else - skia_font_manager_ = SkFontMgr::RefDefault(); + for (std::string dir : dirs) { + if (dir.length() != 0) { + skia_font_managers_.push_back( + SkFontMgr_New_Custom_Directory(dir.c_str())); + } + } #endif + skia_font_managers_.push_back(SkFontMgr::RefDefault()); + + SkString str; + for (sk_sp mgr : skia_font_managers_) { + for (int i = 0; i < mgr->countFamilies(); i++) { + mgr->getFamilyName(i, &str); + family_names_.insert(std::string{str.writable_str()}); + } + } } FontCollection::~FontCollection() = default; std::set FontCollection::GetFamilyNames() { - std::set names; - SkString str; - for (int i = 0; i < skia_font_manager_->countFamilies(); i++) { - skia_font_manager_->getFamilyName(i, &str); - names.insert(std::string{str.writable_str()}); - } - return names; + return family_names_; } +// TODO(garyq): Rework this to use font fallback system. const std::string FontCollection::ProcessFamilyName(const std::string& family) { #ifdef DIRECTORY_FONT_MANAGER_AVAILABLE return family.length() == 0 ? DEFAULT_FAMILY_NAME : family; @@ -77,49 +88,52 @@ const std::string FontCollection::ProcessFamilyName(const std::string& family) { std::shared_ptr FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) { - FTL_DCHECK(skia_font_manager_ != nullptr); + FTL_DCHECK(skia_font_managers_.size() > 0); // Ask Skia to resolve a font style set for a font family name. // FIXME(chinmaygarde): The name "Coolvetica" is hardcoded because CoreText // crashes when passed a null string. This seems to be a bug in Skia as // SkFontMgr explicitly says passing in nullptr gives the default font. + for (sk_sp mgr : skia_font_managers_) { + FTL_DCHECK(mgr != nullptr); + auto font_style_set = mgr->matchFamily(ProcessFamilyName(family).c_str()); + FTL_DCHECK(font_style_set != nullptr); - auto font_style_set = - skia_font_manager_->matchFamily(ProcessFamilyName(family).c_str()); - FTL_DCHECK(font_style_set != nullptr); + std::vector minikin_fonts; - std::vector minikin_fonts; + // Add fonts to the Minikin font family. + for (int i = 0, style_count = font_style_set->count(); i < style_count; + ++i) { + // Create the skia typeface + auto skia_typeface = + sk_ref_sp(font_style_set->createTypeface(i)); + if (skia_typeface == nullptr) { + continue; + } - // Add fonts to the Minikin font family. - for (int i = 0, style_count = font_style_set->count(); i < style_count; ++i) { - // Create the skia typeface - auto skia_typeface = - sk_ref_sp(font_style_set->createTypeface(i)); - if (skia_typeface == nullptr) { - continue; + // Create the minikin font from the skia typeface. + minikin::Font minikin_font( + std::make_shared(skia_typeface), + minikin::FontStyle{skia_typeface->fontStyle().weight(), + skia_typeface->isItalic()}); + + minikin_fonts.emplace_back(std::move(minikin_font)); } - // Create the minikin font from the skia typeface. - minikin::Font minikin_font( - std::make_shared(skia_typeface), - minikin::FontStyle{skia_typeface->fontStyle().weight(), - skia_typeface->isItalic()}); + // Create a Minikin font family. + auto minikin_family = + std::make_shared(std::move(minikin_fonts)); - minikin_fonts.emplace_back(std::move(minikin_font)); + // Create a vector of font families for the Minkin font collection. For now, + // we only have one family in our collection. + std::vector> minikin_families = { + minikin_family, + }; + + // Return the font collection. + return std::make_shared(minikin_families); } - - // Create a Minikin font family. - auto minikin_family = - std::make_shared(std::move(minikin_fonts)); - - // Create a vector of font families for the Minkin font collection. For now, - // we only have one family in our collection. - std::vector> minikin_families = { - minikin_family, - }; - - // Return the font collection. - return std::make_shared(minikin_families); + return nullptr; } } // namespace txt diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index 836e22e8ff..b8a0c8a6de 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -39,6 +39,8 @@ class FontCollection { static FontCollection& GetFontCollection(std::string dir = ""); + static FontCollection& GetFontCollection(std::vector dirs); + std::shared_ptr GetMinikinFontCollectionForFamily( const std::string& family); @@ -46,7 +48,9 @@ class FontCollection { std::set GetFamilyNames(); private: - sk_sp skia_font_manager_; + std::vector> skia_font_managers_; + // Cache the names because GetFamilyNames() can be frequently called. + std::set family_names_; FRIEND_TEST(FontCollection, HasDefaultRegistrations); FRIEND_TEST(FontCollection, GetMinikinFontCollections); @@ -58,7 +62,7 @@ class FontCollection { return DEFAULT_FAMILY_NAME; }; - FontCollection(std::string dir = ""); + FontCollection(const std::vector& dirs); ~FontCollection(); diff --git a/engine/src/flutter/tests/txt/font_collection_unittests.cc b/engine/src/flutter/tests/txt/font_collection_unittests.cc index f501649a1c..7f98cccb30 100644 --- a/engine/src/flutter/tests/txt/font_collection_unittests.cc +++ b/engine/src/flutter/tests/txt/font_collection_unittests.cc @@ -67,7 +67,7 @@ TEST(FontCollection, GetFamilyNames) { txt::FontCollection::GetFontCollection(txt::GetFontDir()) .GetFamilyNames(); - ASSERT_EQ(names.size(), 19ull); + ASSERT_TRUE(names.size() >= 19ull); ASSERT_EQ(names.count("Roboto"), 1ull); ASSERT_EQ(names.count("Homemade Apple"), 1ull); @@ -92,9 +92,7 @@ TEST(FontCollection, GetFamilyNames) { ASSERT_EQ(names.count("Not a real font!"), 0ull); ASSERT_EQ(names.count(""), 0ull); - ASSERT_EQ(names.count("Helvetica"), 0ull); - ASSERT_EQ(names.count("TimesNewRoman"), 0ull); - ASSERT_EQ(names.count("Arial"), 0ull); + ASSERT_EQ(names.count("Another Fake Font"), 0ull); } } // namespace txt