From 1fd69f9824d51a375a0dba7ffb0680b1f6cca49e Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Fri, 28 Jul 2017 15:21:39 -0700 Subject: [PATCH] Refactor FontCollection to allow for adding in new font managers, and initial android font loading. Change-Id: I719b14a515aabf5d6b3894f79ca64ea20ef5625d --- engine/src/flutter/BUILD.gn | 3 ++ engine/src/flutter/src/font_collection.cc | 47 +++++++++++++++++++---- engine/src/flutter/src/font_collection.h | 12 ++++++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index 9aabc9ec04..2888897efa 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -24,6 +24,9 @@ source_set("txt") { if (current_toolchain == host_toolchain) { defines = [ "DIRECTORY_FONT_MANAGER_AVAILABLE" ] } + if (is_android) { + defines = [ "ANDROID_FONT_MANAGER_AVAILABLE" ] + } sources = [ "src/font_collection.cc", diff --git a/engine/src/flutter/src/font_collection.cc b/engine/src/flutter/src/font_collection.cc index ae59ddf639..fceb5fb527 100644 --- a/engine/src/flutter/src/font_collection.cc +++ b/engine/src/flutter/src/font_collection.cc @@ -28,6 +28,7 @@ #include "lib/txt/src/font_skia.h" #include "third_party/skia/include/core/SkString.h" #include "third_party/skia/include/ports/SkFontMgr.h" +#include "third_party/skia/include/ports/SkFontMgr_android.h" #include "third_party/skia/include/ports/SkFontMgr_directory.h" namespace txt { @@ -69,16 +70,50 @@ FontCollection::FontCollection(std::string dir, CacheMethod cache_method) { FontCollection::FontCollection(const std::vector& dirs, CacheMethod cache_method) { -#ifdef DIRECTORY_FONT_MANAGER_AVAILABLE for (std::string dir : dirs) { if (dir.length() != 0) { - skia_font_managers_.push_back( - SkFontMgr_New_Custom_Directory(dir.c_str())); + AddFontMgr(dir, false); } } -#endif skia_font_managers_.push_back(SkFontMgr::RefDefault()); + DiscoverFamilyNames(); + + cache_method_ = cache_method; +} + +FontCollection::~FontCollection() = default; + +void FontCollection::AddFontMgr(std::string dir, bool rediscover_family_names) { +#ifdef DIRECTORY_FONT_MANAGER_AVAILABLE + // On Linux systems: + skia_font_managers_.push_back(SkFontMgr_New_Custom_Directory(dir.c_str())); +#endif +#ifdef ANDROID_FONT_MANAGER_AVAILABLE + // On Android: + SkFontMgr_Android_CustomFonts android_custom_font_data; + // Ensure the dir string is '/' terminated. + if (dir.back() != '/') + dir += '/'; + android_custom_font_data.fBasePath = dir.data(); + android_custom_font_data.fSystemFontUse = + SkFontMgr_Android_CustomFonts::SystemFontUse::kOnlyCustom; + skia_font_managers_.push_back( + SkFontMgr_New_Android(&android_custom_font_data)); +#endif + + if (rediscover_family_names) + DiscoverFamilyNames(); +} + +void FontCollection::AddFontMgr(sk_sp font_mgr, + bool rediscover_family_names) { + skia_font_managers_.push_back(font_mgr); + if (rediscover_family_names) + DiscoverFamilyNames(); +} + +void FontCollection::DiscoverFamilyNames() { SkString str; for (sk_sp mgr : skia_font_managers_) { for (int i = 0; i < mgr->countFamilies(); i++) { @@ -86,12 +121,8 @@ FontCollection::FontCollection(const std::vector& dirs, family_names_.insert(std::string{str.writable_str()}); } } - - cache_method_ = cache_method; } -FontCollection::~FontCollection() = default; - std::set FontCollection::GetFamilyNames() { return family_names_; } diff --git a/engine/src/flutter/src/font_collection.h b/engine/src/flutter/src/font_collection.h index fb3a3be0bd..f305241669 100644 --- a/engine/src/flutter/src/font_collection.h +++ b/engine/src/flutter/src/font_collection.h @@ -84,6 +84,13 @@ class FontCollection { // managers. bool HasFamily(const std::string family) const; + // Adds a new SkFontMgr to the front of the stack of font managers. + void AddFontMgr(std::string dir, bool rediscover_family_names = true); + + // Adds the provided SkFontMgr to the front of the stack of font managers. + void AddFontMgr(sk_sp font_mgr, + bool rediscover_family_names = true); + // Removes all fonts that do not fit in the cache capacity from memory. void FlushCache(); @@ -112,8 +119,13 @@ class FontCollection { FRIEND_TEST(FontCollection, GetMinikinFontCollections); FRIEND_TEST(FontCollection, GetFamilyNames); + // Postprocess the family name to handle the following properties: fallback + // when not found and reverting to the default name when no fallback is found. const std::string ProcessFamilyName(const std::string& family); + // Polls the SkFontMgrs to obtain a set of all available font family names. + void DiscoverFamilyNames(); + void TrimCache(); static const std::string GetDefaultFamilyName() {