Skip to content

Commit

Permalink
libtxt: search for fallback fonts that can match emoji and CJK charac…
Browse files Browse the repository at this point in the history
…ters (flutter#4156)
  • Loading branch information
jason-simmons authored Oct 2, 2017
1 parent 267e7a8 commit 8061df1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 0 additions & 1 deletion third_party/txt/src/txt/asset_font_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ SkTypeface* AssetFontManager::onMatchFamilyStyleCharacter(
const char* bcp47[],
int bcp47Count,
SkUnichar character) const {
FXL_DCHECK(false);
return nullptr;
}

Expand Down
44 changes: 42 additions & 2 deletions third_party/txt/src/txt/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@

namespace txt {

namespace {

// Example characters representing character classes that may require a
// fallback font.
const std::vector<SkUnichar> fallback_characters{
0x1f600, // emoji
0x4e00, // CJK
};

} // anonymous namespace

FontCollection::FontCollection() = default;

FontCollection::~FontCollection() = default;
Expand All @@ -42,13 +53,15 @@ void FontCollection::PushFront(sk_sp<SkFontMgr> skia_font_manager) {
if (!skia_font_manager) {
return;
}
UpdateFallbackFonts(skia_font_manager);
skia_font_managers_.push_front(std::move(skia_font_manager));
}

void FontCollection::PushBack(sk_sp<SkFontMgr> skia_font_manager) {
if (!skia_font_manager) {
return;
}
UpdateFallbackFonts(skia_font_manager);
skia_font_managers_.push_back(std::move(skia_font_manager));
}

Expand Down Expand Up @@ -92,11 +105,12 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) {
auto minikin_family =
std::make_shared<minikin::FontFamily>(std::move(minikin_fonts));

// Create a vector of font families for the Minikin font collection. For
// now, we only have one family in our collection.
// Create a vector of font families for the Minikin font collection.
std::vector<std::shared_ptr<minikin::FontFamily>> minikin_families = {
minikin_family,
};
for (const auto& fallback : fallback_fonts_)
minikin_families.push_back(fallback.second);

// Create the minikin font collection.
auto font_collection =
Expand All @@ -117,4 +131,30 @@ FontCollection::GetMinikinFontCollectionForFamily(const std::string& family) {
return nullptr;
}

void FontCollection::UpdateFallbackFonts(sk_sp<SkFontMgr> manager) {
char language_tag[ULOC_FULLNAME_CAPACITY];
UErrorCode uerr;
uloc_toLanguageTag(icu::Locale::getDefault().getName(), language_tag,
ULOC_FULLNAME_CAPACITY, FALSE, &uerr);
if (U_FAILURE(uerr))
return;
const char* bcp47[] = {language_tag};

for (SkUnichar fallback_char : fallback_characters) {
if (fallback_fonts_.count(fallback_char))
continue;

sk_sp<SkTypeface> skia_typeface(manager->matchFamilyStyleCharacter(
0, SkFontStyle(), bcp47, 1, fallback_char));
if (!skia_typeface)
continue;

std::vector<minikin::Font> minikin_fonts;
minikin_fonts.emplace_back(std::make_shared<FontSkia>(skia_typeface),
minikin::FontStyle());
fallback_fonts_[fallback_char] =
std::make_shared<minikin::FontFamily>(std::move(minikin_fonts));
}
}

} // namespace txt
4 changes: 4 additions & 0 deletions third_party/txt/src/txt/font_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class FontCollection {
std::deque<sk_sp<SkFontMgr>> skia_font_managers_;
std::unordered_map<std::string, std::shared_ptr<minikin::FontCollection>>
font_collections_cache_;
std::unordered_map<SkUnichar, std::shared_ptr<minikin::FontFamily>>
fallback_fonts_;

void UpdateFallbackFonts(sk_sp<SkFontMgr> manager);

FXL_DISALLOW_COPY_AND_ASSIGN(FontCollection);
};
Expand Down

0 comments on commit 8061df1

Please sign in to comment.