Skip to content

Commit a2b871a

Browse files
committed
Cache the glyphs and fonts used when matching fonts
Signed-off-by: MuHong Byun <mh.byun@samsung.com>
1 parent 3ca988a commit a2b871a

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

third_party/txt/src/minikin/FontCollection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ void FontCollection::itemize(const uint16_t* string,
510510
const std::shared_ptr<FontFamily>& family = getFamilyForChar(
511511
ch, isVariationSelector(nextCh) ? nextCh : 0, langListId, variant);
512512

513-
if (utf16Pos == 0 || family.get() != lastFamily) {
513+
if (utf16Pos == 0 || family.get() != lastFamily ||
514+
family.get()->getLastMatchedCodePoint() != ch) {
514515
size_t start = utf16Pos;
515516
// Workaround for combining marks and emoji modifiers until we implement
516517
// per-cluster font selection: if a combining mark or an emoji modifier

third_party/txt/src/minikin/FontFamily.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,18 @@ FakedFont FontFamily::getClosestMatch(FontStyle style) const {
165165
}
166166

167167
FakedFont FontFamily::getClosestMatchWithChar(FontStyle style,
168-
uint32_t codepoint) const {
168+
uint32_t codepoint) {
169+
int bestMatch = INT_MAX;
169170
const Font* bestFont = nullptr;
170-
int bestMatch = 0;
171+
if (mLastMatchedCodePoint == codepoint && style == mLastMatchedFontStyle) {
172+
if (mFonts.size() > mLastMatchedFontIndex) {
173+
printf("[MONG] Cache Hit!!\n");
174+
bestFont = &mFonts[mLastMatchedFontIndex];
175+
return FakedFont{bestFont->typeface.get(),
176+
computeFakery(style, bestFont->style)};
177+
}
178+
}
179+
171180
for (size_t i = 0; i < mFonts.size(); i++) {
172181
const Font& font = mFonts[i];
173182
int match = computeMatch(font.style, style);
@@ -180,13 +189,16 @@ FakedFont FontFamily::getClosestMatchWithChar(FontStyle style,
180189
}
181190

182191
if (result) {
183-
if (i == 0 || match < bestMatch || bestMatch == 0) {
192+
if (match < bestMatch) {
184193
bestFont = &font;
185194
bestMatch = match;
195+
mLastMatchedFontIndex = i;
186196
}
187197
}
188198
}
189199
if (bestFont != nullptr) {
200+
mLastMatchedFontStyle = style;
201+
mLastMatchedCodePoint = codepoint;
190202
return FakedFont{bestFont->typeface.get(),
191203
computeFakery(style, bestFont->style)};
192204
}

third_party/txt/src/minikin/FontFamily.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class FontFamily {
141141
int* weight,
142142
bool* italic);
143143
FakedFont getClosestMatch(FontStyle style) const;
144-
FakedFont getClosestMatchWithChar(FontStyle style, uint32_t codepoint) const;
144+
FakedFont getClosestMatchWithChar(FontStyle style, uint32_t codepoint);
145145

146146
uint32_t langId() const { return mLangId; }
147147
int variant() const { return mVariant; }
@@ -174,6 +174,8 @@ class FontFamily {
174174
std::shared_ptr<FontFamily> createFamilyWithVariation(
175175
const std::vector<FontVariation>& variations) const;
176176

177+
uint32_t getLastMatchedCodePoint() { return mLastMatchedCodePoint; }
178+
177179
private:
178180
void computeCoverage();
179181

@@ -185,6 +187,10 @@ class FontFamily {
185187
SparseBitSet mCoverage;
186188
bool mHasVSTable;
187189

190+
uint mLastMatchedFontIndex;
191+
uint32_t mLastMatchedCodePoint;
192+
FontStyle mLastMatchedFontStyle;
193+
188194
// Forbid copying and assignment.
189195
FontFamily(const FontFamily&) = delete;
190196
void operator=(const FontFamily&) = delete;

0 commit comments

Comments
 (0)