Make GetDefaultFontFamilies return a vector<string> instead of… (flutter/engine#16928)

On Linux, there is rarely just one default font that can reasonably be expected to be on the platform. This PR changes the GetDefaultFontFamily call to be GetDefaultFontFamilies, and it now returns a vector<string> so that the font collection code can look up all of them, and if any of them exist, add them to the fallback list.

For Linux, I supplied the list "Ubuntu", "Cantarell", "DejaVu Sans", "Liberation Sans", and "Arial", which should cover a large proportion of linux machines. For the other platforms, I supplied a list of length one, containing the one fallback font that used to be defined. On Windows, I added "Segoe UI" as a default, since that is the default system font on newer Windows.

The goal of this function is to provide at least one font family that is installed, since otherwise linux (or any platform) will just have no font at all if the default font isn't found.
This commit is contained in:
Greg Spencer
2020-03-05 00:45:53 +01:00
committed by GitHub
parent f35ba0b489
commit 6a2e36c37f
10 changed files with 27 additions and 23 deletions

View File

@@ -16,7 +16,7 @@ group("linux") {
}
}
# Temporary workraround for the issue describe in
# Temporary workaround for the issue describe in
# https://github.com/flutter/flutter/issues/14509 and
# https://github.com/flutter/flutter/issues/14438
# Remove once the build infrastructure moves to Ubuntu 18.04 or newer, where

View File

@@ -150,11 +150,14 @@ FontCollection::GetMinikinFontCollectionForFamilies(
}
// Search for default font family if no user font families were found.
if (minikin_families.empty()) {
const auto default_font_family = GetDefaultFontFamily();
std::shared_ptr<minikin::FontFamily> minikin_family =
FindFontFamilyInManagers(default_font_family);
if (minikin_family != nullptr) {
minikin_families.push_back(minikin_family);
const auto default_font_families = GetDefaultFontFamilies();
for (auto family : default_font_families) {
std::shared_ptr<minikin::FontFamily> minikin_family =
FindFontFamilyInManagers(family);
if (minikin_family != nullptr) {
minikin_families.push_back(minikin_family);
break;
}
}
}
// Default font family also not found. We fail to get a FontCollection.
@@ -319,7 +322,7 @@ FontCollection::CreateSktFontCollection() {
skt_collection_ = sk_make_sp<skia::textlayout::FontCollection>();
skt_collection_->setDefaultFontManager(default_font_manager_,
GetDefaultFontFamily().c_str());
GetDefaultFontFamilies()[0].c_str());
skt_collection_->setAssetFontManager(asset_font_manager_);
skt_collection_->setDynamicFontManager(dynamic_font_manager_);
skt_collection_->setTestFontManager(test_font_manager_);

View File

@@ -6,8 +6,8 @@
namespace txt {
std::string GetDefaultFontFamily() {
return "Arial";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Arial"};
}
sk_sp<SkFontMgr> GetDefaultFontManager() {

View File

@@ -6,13 +6,15 @@
#define TXT_PLATFORM_H_
#include <string>
#include <vector>
#include "flutter/fml/macros.h"
#include "third_party/skia/include/core/SkFontMgr.h"
namespace txt {
std::string GetDefaultFontFamily();
std::vector<std::string> GetDefaultFontFamilies();
sk_sp<SkFontMgr> GetDefaultFontManager();

View File

@@ -6,8 +6,8 @@
namespace txt {
std::string GetDefaultFontFamily() {
return "sans-serif";
std::vector<std::string> GetDefaultFontFamilies() {
return {"sans-serif"};
}
sk_sp<SkFontMgr> GetDefaultFontManager() {

View File

@@ -6,8 +6,8 @@
namespace txt {
std::string GetDefaultFontFamily() {
return "Roboto";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Roboto"};
}
sk_sp<SkFontMgr> GetDefaultFontManager() {

View File

@@ -12,8 +12,8 @@
namespace txt {
std::string GetDefaultFontFamily() {
return "Arial";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Ubuntu", "Cantarell", "DejaVu Sans", "Liberation Sans", "Arial"};
}
sk_sp<SkFontMgr> GetDefaultFontManager() {

View File

@@ -16,11 +16,11 @@
namespace txt {
std::string GetDefaultFontFamily() {
std::vector<std::string> GetDefaultFontFamilies() {
if (fml::IsPlatformVersionAtLeast(9)) {
return [FONT_CLASS systemFontOfSize:14].familyName.UTF8String;
return {[FONT_CLASS systemFontOfSize:14].familyName.UTF8String};
} else {
return "Helvetica";
return {"Helvetica"};
}
}

View File

@@ -7,8 +7,8 @@
namespace txt {
std::string GetDefaultFontFamily() {
return "Arial";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Segoe UI", "Arial"};
}
sk_sp<SkFontMgr> GetDefaultFontManager() {

View File

@@ -23,8 +23,7 @@
namespace txt {
TextStyle::TextStyle()
: font_families(std::vector<std::string>(1, GetDefaultFontFamily())) {}
TextStyle::TextStyle() : font_families(GetDefaultFontFamilies()) {}
bool TextStyle::equals(const TextStyle& other) const {
if (color != other.color)