Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions third_party/txt/src/minikin/FontCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(
// libtxt: check if the fallback font provider can match this character
if (mFallbackFontProvider) {
const std::shared_ptr<FontFamily>& fallback =
mFallbackFontProvider->matchFallbackFont(ch,
GetFontLocale(langListId));
findFallbackFont(ch, vs, langListId);
if (fallback) {
return fallback;
}
Expand Down Expand Up @@ -340,8 +339,7 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(
// libtxt: check if the fallback font provider can match this character
if (mFallbackFontProvider) {
const std::shared_ptr<FontFamily>& fallback =
mFallbackFontProvider->matchFallbackFont(ch,
GetFontLocale(langListId));
findFallbackFont(ch, vs, langListId);
if (fallback) {
return fallback;
}
Expand All @@ -365,6 +363,30 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(
: mFamilies[bestFamilyIndex];
}

const std::shared_ptr<FontFamily>& FontCollection::findFallbackFont(
uint32_t ch,
uint32_t vs,
uint32_t langListId) const {
std::string locale = GetFontLocale(langListId);

const auto it = mCachedFallbackFamilies.find(locale);
if (it != mCachedFallbackFamilies.end()) {
for (const auto& fallbackFamily : it->second) {
if (calcCoverageScore(ch, vs, fallbackFamily)) {
return fallbackFamily;
}
}
}

const std::shared_ptr<FontFamily>& fallback =
mFallbackFontProvider->matchFallbackFont(ch, GetFontLocale(langListId));

if (fallback) {
mCachedFallbackFamilies[locale].push_back(fallback);
}
return fallback;
}

const uint32_t NBSP = 0x00A0;
const uint32_t SOFT_HYPHEN = 0x00AD;
const uint32_t ZWJ = 0x200C;
Expand Down
9 changes: 9 additions & 0 deletions third_party/txt/src/minikin/FontCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef MINIKIN_FONT_COLLECTION_H
#define MINIKIN_FONT_COLLECTION_H

#include <map>
#include <memory>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -100,6 +101,9 @@ class FontCollection {
uint32_t langListId,
int variant) const;

const std::shared_ptr<FontFamily>&
findFallbackFont(uint32_t ch, uint32_t vs, uint32_t langListId) const;

uint32_t calcFamilyScore(uint32_t ch,
uint32_t vs,
int variant,
Expand Down Expand Up @@ -148,6 +152,11 @@ class FontCollection {

// libtxt extension: Fallback font provider.
std::unique_ptr<FallbackFontProvider> mFallbackFontProvider;

// libtxt extension: Fallback fonts discovered after this font collection
// was constructed.
mutable std::map<std::string, std::vector<std::shared_ptr<FontFamily>>>
mCachedFallbackFamilies;
};

} // namespace minikin
Expand Down