Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
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
23 changes: 23 additions & 0 deletions third_party/txt/src/minikin/FontCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const uint32_t TEXT_STYLE_VS = 0xFE0E;

uint32_t FontCollection::sNextId = 0;

const uint32_t kMinFallbackScore = 0x20000000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the calcFamilyScore >= kMinFallbackScore checks with a call to calcCoverageScore.

calcFamilyScore is a combination of calcCoverageScore and some other scores. If calcCoverageScore returns a nonzero result, then that is equivalent to calcFamilyScore returning a score >= 0x20000000.

std::vector<std::shared_ptr<FontFamily>> sFallbackFamilies;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a member of the FontCollection class instead of a global. The member will need to be mutable because FontCollection::getFamilyForChar is const.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a unclear to me exactly why this is called sFallbackFamilies, maybe consider a more descriptive name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a unclear to me exactly why this is called sFallbackFamilies, maybe consider a more descriptive name?

@GaryQian What's your suggested name?


// libtxt: return a locale string for a language list ID
std::string GetFontLocale(uint32_t langListId) {
const FontLanguages& langs = FontLanguageListCache::getById(langListId);
Expand All @@ -64,6 +67,10 @@ FontCollection::FontCollection(
init(typefaces);
}

FontCollection::~FontCollection() {
sFallbackFamilies.clear();
}

void FontCollection::init(
const vector<std::shared_ptr<FontFamily>>& typefaces) {
std::scoped_lock _l(gMinikinLock);
Expand Down Expand Up @@ -299,12 +306,20 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(
uint32_t langListId,
int variant) const {
if (ch >= mMaxChar) {
for (size_t i = 0; i < sFallbackFamilies.size(); i++) {
Copy link
Contributor

@GaryQian GaryQian Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) Since this is identical to the snippet below (line 355), consider using helper function to help keep these two sections in sync.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, since it would be helpful to have a short comment explaining why this is necessary.

const uint32_t score =
calcFamilyScore(ch, vs, variant, langListId, sFallbackFamilies[i]);
if (score >= kMinFallbackScore) {
return sFallbackFamilies[i];
}
}
// libtxt: check if the fallback font provider can match this character
if (mFallbackFontProvider) {
const std::shared_ptr<FontFamily>& fallback =
mFallbackFontProvider->matchFallbackFont(ch,
GetFontLocale(langListId));
if (fallback) {
sFallbackFamilies.push_back(fallback);
return fallback;
}
}
Expand Down Expand Up @@ -337,12 +352,20 @@ const std::shared_ptr<FontFamily>& FontCollection::getFamilyForChar(
}
}
if (bestFamilyIndex == -1) {
for (size_t i = 0; i < sFallbackFamilies.size(); i++) {
const uint32_t score =
calcFamilyScore(ch, vs, variant, langListId, sFallbackFamilies[i]);
if (score >= kMinFallbackScore) {
return sFallbackFamilies[i];
}
}
// libtxt: check if the fallback font provider can match this character
if (mFallbackFontProvider) {
const std::shared_ptr<FontFamily>& fallback =
mFallbackFontProvider->matchFallbackFont(ch,
GetFontLocale(langListId));
if (fallback) {
sFallbackFamilies.push_back(fallback);
return fallback;
}
}
Expand Down
2 changes: 2 additions & 0 deletions third_party/txt/src/minikin/FontCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class FontCollection {
const std::vector<std::shared_ptr<FontFamily>>& typefaces);
explicit FontCollection(std::shared_ptr<FontFamily>&& typeface);

~FontCollection();

// libtxt extension: an interface for looking up fallback fonts for characters
// that do not match this collection's font families.
class FallbackFontProvider {
Expand Down