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

Unified Diff: third_party/WebKit/Source/core/layout/LayoutTextControl.cpp

Issue 2145903002: Add heuristic to LayoutTextControl::hasValidAvgCharWidth() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
Index: third_party/WebKit/Source/core/layout/LayoutTextControl.cpp
diff --git a/third_party/WebKit/Source/core/layout/LayoutTextControl.cpp b/third_party/WebKit/Source/core/layout/LayoutTextControl.cpp
index 0b87e69471d1ea3a3d4236788629a02bb3089c0b..e8ee104847271c40bf0e0a1bf8b00699bf61c968 100644
--- a/third_party/WebKit/Source/core/layout/LayoutTextControl.cpp
+++ b/third_party/WebKit/Source/core/layout/LayoutTextControl.cpp
@@ -189,8 +189,15 @@ static const char* const fontFamiliesWithInvalidCharWidth[] = {
// from the width of a '0'. This only seems to apply to a fixed number of Mac fonts,
// but, in order to get similar rendering across platforms, we do this check for
// all platforms.
-bool LayoutTextControl::hasValidAvgCharWidth(const AtomicString& family)
+bool LayoutTextControl::hasValidAvgCharWidth(const SimpleFontData* font, const AtomicString& family)
kochi 2016/07/13 09:36:12 Can't this be "const SimpleFontData& font"? (that
kojii 2016/07/13 11:20:35 Making "*" to "&" doesn't assure non-null, so it c
tkent 2016/07/13 23:14:35 In this case, * making the argument type |const S
tkent 2016/07/14 00:07:09 eae@ just added a runtime null check. https://code
{
+ // Some fonts match avgCharWidth to CJK full-width characters.
+ // Heuristic check to avoid such fonts.
+ DCHECK(font);
+ const FontMetrics& metrics = font->getFontMetrics();
+ if (metrics.hasZeroWidth() && font->avgCharWidth() > metrics.zeroWidth() * 1.7)
+ return false;
+
static HashSet<AtomicString>* fontFamiliesWithInvalidCharWidthMap = nullptr;
if (family.isEmpty())
@@ -209,10 +216,9 @@ bool LayoutTextControl::hasValidAvgCharWidth(const AtomicString& family)
float LayoutTextControl::getAvgCharWidth(const AtomicString& family) const
{
const Font& font = style()->font();
- if (hasValidAvgCharWidth(family)) {
- ASSERT(font.primaryFont());
- return roundf(font.primaryFont()->avgCharWidth());
- }
+ const SimpleFontData* primaryFont = font.primaryFont();
+ if (hasValidAvgCharWidth(primaryFont, family))
+ return roundf(primaryFont->avgCharWidth());
const UChar ch = '0';
const String str = String(&ch, 1);

Powered by Google App Engine
This is Rietveld 408576698