Skip to content

Commit 43f624c

Browse files
bwikbsbbrto21swift-kim
committed
Check the presence of glyphs when selecting font (#113)
* Check the presence of glyphs when selecting font * Change default font family name * Fix coding style * Cache the glyphs and fonts used when matching fonts * When matching fonts, if the existing character is a special character, it is checked more. * Fixed sorting to make emoji fonts go backwards Signed-off-by: MuHong Byun <[email protected]> Co-authored-by: Boram Bae <[email protected]> Co-authored-by: Swift Kim <[email protected]>
1 parent ecd46e2 commit 43f624c

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

third_party/txt/src/minikin/FontCollection.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ void FontCollection::itemize(const uint16_t* string,
509509
if (!shouldContinueRun) {
510510
const std::shared_ptr<FontFamily>& family = getFamilyForChar(
511511
ch, isVariationSelector(nextCh) ? nextCh : 0, langListId, variant);
512-
if (utf16Pos == 0 || family.get() != lastFamily) {
512+
if (utf16Pos == 0 || family.get() != lastFamily ||
513+
(!(U_GET_GC_MASK(prevCh) & U_GC_L_MASK) &&
514+
(U_GET_GC_MASK(ch) & U_GC_L_MASK))) {
513515
size_t start = utf16Pos;
514516
// Workaround for combining marks and emoji modifiers until we implement
515517
// per-cluster font selection: if a combining mark or an emoji modifier
@@ -528,8 +530,8 @@ void FontCollection::itemize(const uint16_t* string,
528530
}
529531
start -= prevChLength;
530532
}
531-
result->push_back(
532-
{family->getClosestMatch(style), static_cast<int>(start), 0});
533+
result->push_back({family->getClosestMatch(style, ch, variant),
534+
static_cast<int>(start), 0});
533535
run = &result->back();
534536
lastFamily = family.get();
535537
}

third_party/txt/src/minikin/FontFamily.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,28 @@ static FontFakery computeFakery(FontStyle wanted, FontStyle actual) {
146146
return FontFakery(isFakeBold, isFakeItalic);
147147
}
148148

149-
FakedFont FontFamily::getClosestMatch(FontStyle style) const {
149+
FakedFont FontFamily::getClosestMatch(
150+
FontStyle style,
151+
uint32_t codepoint /* = 0 */,
152+
uint32_t variationSelector /* = 0 */) const {
150153
const Font* bestFont = nullptr;
151-
int bestMatch = 0;
154+
int bestMatch = INT_MAX;
152155
for (size_t i = 0; i < mFonts.size(); i++) {
153156
const Font& font = mFonts[i];
154157
int match = computeMatch(font.style, style);
155-
if (i == 0 || match < bestMatch) {
156-
bestFont = &font;
157-
bestMatch = match;
158+
bool result = false;
159+
if (codepoint != 0) {
160+
hb_font_t* hbFont = getHbFontLocked(font.typeface.get());
161+
uint32_t unusedGlyph = 0;
162+
result =
163+
hb_font_get_glyph(hbFont, codepoint, variationSelector, &unusedGlyph);
164+
hb_font_destroy(hbFont);
165+
}
166+
if (codepoint == 0 || (codepoint != 0 && result)) {
167+
if (match < bestMatch) {
168+
bestFont = &font;
169+
bestMatch = match;
170+
}
158171
}
159172
}
160173
if (bestFont != nullptr) {

third_party/txt/src/minikin/FontFamily.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ class FontFamily {
140140
static bool analyzeStyle(const std::shared_ptr<MinikinFont>& typeface,
141141
int* weight,
142142
bool* italic);
143-
FakedFont getClosestMatch(FontStyle style) const;
143+
FakedFont getClosestMatch(FontStyle style,
144+
uint32_t codepoint = 0,
145+
uint32_t variationSelector = 0) const;
144146

145147
uint32_t langId() const { return mLangId; }
146148
int variant() const { return mVariant; }

third_party/txt/src/txt/font_collection.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ void FontCollection::SortSkTypefaces(
245245
std::sort(
246246
sk_typefaces.begin(), sk_typefaces.end(),
247247
[](const sk_sp<SkTypeface>& a, const sk_sp<SkTypeface>& b) {
248+
{
249+
// A workaround to prevent emoji fonts being selected for normal text
250+
// when normal and emojis are mixed at same font family.
251+
252+
bool a_isEmojiFont = false;
253+
bool b_isEmojiFont = false;
254+
SkString postScriptName;
255+
a->getPostScriptName(&postScriptName);
256+
if (postScriptName.contains("Emoji")) {
257+
a_isEmojiFont = true;
258+
}
259+
b->getPostScriptName(&postScriptName);
260+
if (postScriptName.contains("Emoji")) {
261+
b_isEmojiFont = true;
262+
}
263+
if (a_isEmojiFont && !b_isEmojiFont) {
264+
return false;
265+
} else if (!a_isEmojiFont && b_isEmojiFont) {
266+
return true;
267+
}
268+
}
269+
248270
SkFontStyle a_style = a->fontStyle();
249271
SkFontStyle b_style = b->fontStyle();
250272

third_party/txt/src/txt/platform_linux.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
namespace txt {
1414

1515
std::vector<std::string> GetDefaultFontFamilies() {
16+
#ifdef FLUTTER_USE_FONTCONFIG
17+
return {"TizenDefaultFont"};
18+
#else
1619
return {
1720
"SamsungOneUI",
1821
"SamsungOneUIArabic",
@@ -72,11 +75,12 @@ std::vector<std::string> GetDefaultFontFamilies() {
7275
"BreezeSansFallback",
7376
"BreezeColorEmoji",
7477
};
78+
#endif
7579
}
7680

7781
sk_sp<SkFontMgr> GetDefaultFontManager(uint32_t font_initialization_data) {
7882
#ifdef FLUTTER_USE_FONTCONFIG
79-
return SkFontMgr_New_FontConfig(nullptr);
83+
return SkFontMgr::RefDefault();
8084
#else
8185
return SkFontMgr_New_Custom_Directory("/usr/share/fonts");
8286
#endif

0 commit comments

Comments
 (0)