Refactor FontCollection to allow for adding in new font managers, and initial android font loading.

Change-Id: I719b14a515aabf5d6b3894f79ca64ea20ef5625d
This commit is contained in:
Gary Qian
2017-07-28 15:21:39 -07:00
parent d09bbdfd37
commit 1fd69f9824
3 changed files with 54 additions and 8 deletions

View File

@@ -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",

View File

@@ -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<std::string>& 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<SkFontMgr> 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<SkFontMgr> mgr : skia_font_managers_) {
for (int i = 0; i < mgr->countFamilies(); i++) {
@@ -86,12 +121,8 @@ FontCollection::FontCollection(const std::vector<std::string>& dirs,
family_names_.insert(std::string{str.writable_str()});
}
}
cache_method_ = cache_method;
}
FontCollection::~FontCollection() = default;
std::set<std::string> FontCollection::GetFamilyNames() {
return family_names_;
}

View File

@@ -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<SkFontMgr> 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() {