Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Unified Diff: third_party/WebKit/Source/platform/fonts/FontFallbackList.cpp

Issue 2248693003: Take style into account when matching fonts Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/fast/text/font-ligatures-linebreak-word.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/platform/fonts/FontFallbackList.cpp
diff --git a/third_party/WebKit/Source/platform/fonts/FontFallbackList.cpp b/third_party/WebKit/Source/platform/fonts/FontFallbackList.cpp
index 2840a61a4330ecf81b1610978817d38652cd573d..3de39caf2ece4bded7c0a9d3eedadfdec5590aef 100644
--- a/third_party/WebKit/Source/platform/fonts/FontFallbackList.cpp
+++ b/third_party/WebKit/Source/platform/fonts/FontFallbackList.cpp
@@ -150,12 +150,43 @@ const SimpleFontData* FontFallbackList::determinePrimarySimpleFontData(const Fon
}
}
+unsigned evaluateFontMatch(const FontDescription& desiredFont, PassRefPtr<FontData> actualFont)
+{
+ const SkFontStyle& actualStyle = actualFont->fontDataForCharacter(' ')->platformData().typeface()->fontStyle();
+ // TODO: use desiredFont.skiaFontStyle();
+
+ // Blink maps font weights into [0,8], with 3 being 'normal' (400).
+ // Skia maps the same font weights into [100,900] with 400 being 'normal'.
+ int fontWeightScore = abs((desiredFont.weight() + 1) - (actualStyle.weight() / 100));
+ if (fontWeightScore == 1)
+ fontWeightScore = 0; // Allow for a small amount of tolerance.
+ if (fontWeightScore > 3)
+ fontWeightScore *= 10; // Larger weight differenecs are disproportionately bad.
+
+ int fontStretchScore = abs(desiredFont.stretch() - actualStyle.width()); // Skia and Blink agree on the definition of font stretch
+ if (fontStretchScore == 1)
+ fontStretchScore = 0; // Allow for a small amount of tolerance.
+ if (fontStretchScore > 3)
+ fontStretchScore *= 3; // Larger stretch differences are bad, but not as noticeable as weight differences.
+
+ // We allow italic and oblique to substitute for each other, but substituting either for upright/normal is undesirable.
+ int fontSlantScore = 0;
+ if ((desiredFont.style() == FontStyleNormal) != (actualStyle.slant() == SkFontStyle::kUpright_Slant))
+ fontSlantScore += 50;
+
+ return fontWeightScore + fontStretchScore + fontSlantScore;
+}
+
PassRefPtr<FontData> FontFallbackList::getFontData(const FontDescription& fontDescription, int& familyIndex) const
{
const FontFamily* currFamily = &fontDescription.family();
for (int i = 0; currFamily && i < familyIndex; i++)
currFamily = currFamily->next();
+ int bestMatchIndex = cAllFamiliesScanned;
+ unsigned bestMatchScore = UINT_MAX;
+ RefPtr<FontData> bestMatch;
+
for (; currFamily; currFamily = currFamily->next()) {
familyIndex++;
if (currFamily->family().length()) {
@@ -164,10 +195,22 @@ PassRefPtr<FontData> FontFallbackList::getFontData(const FontDescription& fontDe
result = m_fontSelector->getFontData(fontDescription, currFamily->family());
if (!result)
result = FontCache::fontCache()->getFontData(fontDescription, currFamily->family());
- if (result)
- return result.release();
+ if (result) {
+ unsigned fontMatchScore = evaluateFontMatch(fontDescription, result);
+ if (fontMatchScore == 0)
+ return result.release();
+ if (fontMatchScore < bestMatchScore) {
+ bestMatch = result;
+ bestMatchScore = fontMatchScore;
+ bestMatchIndex = familyIndex;
+ }
+ }
}
}
+ if (bestMatchScore != UINT_MAX) {
+ familyIndex = bestMatchIndex;
+ return bestMatch.release();
+ }
familyIndex = cAllFamiliesScanned;
if (m_fontSelector) {
« no previous file with comments | « third_party/WebKit/LayoutTests/fast/text/font-ligatures-linebreak-word.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698