Skip to content

Commit

Permalink
Make GetDefaultFontFamilies return a vector<string> instead of… (flut…
Browse files Browse the repository at this point in the history
…ter#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.
  • Loading branch information
gspencergoog authored Mar 4, 2020
1 parent d20f6d7 commit 8ff6948
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion shell/platform/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions third_party/txt/src/txt/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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_);
Expand Down
4 changes: 2 additions & 2 deletions third_party/txt/src/txt/platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace txt {

std::string GetDefaultFontFamily() {
return "Arial";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Arial"};
}

sk_sp<SkFontMgr> GetDefaultFontManager() {
Expand Down
4 changes: 3 additions & 1 deletion third_party/txt/src/txt/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions third_party/txt/src/txt/platform_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace txt {

std::string GetDefaultFontFamily() {
return "sans-serif";
std::vector<std::string> GetDefaultFontFamilies() {
return {"sans-serif"};
}

sk_sp<SkFontMgr> GetDefaultFontManager() {
Expand Down
4 changes: 2 additions & 2 deletions third_party/txt/src/txt/platform_fuchsia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace txt {

std::string GetDefaultFontFamily() {
return "Roboto";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Roboto"};
}

sk_sp<SkFontMgr> GetDefaultFontManager() {
Expand Down
4 changes: 2 additions & 2 deletions third_party/txt/src/txt/platform_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions third_party/txt/src/txt/platform_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
}
}

Expand Down
4 changes: 2 additions & 2 deletions third_party/txt/src/txt/platform_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace txt {

std::string GetDefaultFontFamily() {
return "Arial";
std::vector<std::string> GetDefaultFontFamilies() {
return {"Segoe UI", "Arial"};
}

sk_sp<SkFontMgr> GetDefaultFontManager() {
Expand Down
3 changes: 1 addition & 2 deletions third_party/txt/src/txt/text_style.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8ff6948

Please sign in to comment.