Commit 728cd3a1 authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Keep font styles for system fonts

This CL is fixing the imports of the system fonts. The previous code
was not taking care of the font styles.

The bug was introduced here:
 https://chromium-review.googlesource.com/c/chromium/src/+/1544229

The system font is created only by it's name and size:
 return Font(PlatformFont::CreateFromNameAndSize(font_name, font_size));

Some users are overriding the system font with a custom one. That font
may have some styles. The default windows configuration doesn't allow
to change the font styles, but advanced fonts software may do.

  Advanced System Font Changer:
  https://www.wintools.info/index.php/advanced-system-font-changer-icon-settings

R=robliao@chromium.org,asvitkine@chromium.org

Bug: 989476
Change-Id: Ie88ccf4452c5156de48e4f4a4bda87bd167850e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1732733Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Commit-Queue: Etienne Bergeron <etienneb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683774}
parent 6f600503
......@@ -80,15 +80,15 @@ class SystemFonts {
}
}
static Font GetFontFromLOGFONT(const LOGFONT* logfont) {
static Font GetFontFromLOGFONT(const LOGFONT& logfont) {
// Finds a matching font by triggering font mapping. The font mapper finds
// the closest physical font for a given logical font.
base::win::ScopedHFONT font(::CreateFontIndirect(logfont));
base::win::ScopedHFONT font(::CreateFontIndirect(&logfont));
base::win::ScopedGetDC screen_dc(NULL);
base::win::ScopedSelectObject scoped_font(screen_dc, font.get());
DCHECK(font.get()) << "Font for '"
<< base::SysWideToUTF8(logfont->lfFaceName)
<< base::SysWideToUTF8(logfont.lfFaceName)
<< "' has an invalid handle.";
// Retrieve the name and height of the mapped font (physical font).
......@@ -102,7 +102,20 @@ class SystemFonts {
std::max<int>(1, mapped_font_metrics.tmHeight -
mapped_font_metrics.tmInternalLeading);
return Font(PlatformFont::CreateFromNameAndSize(font_name, font_size));
Font system_font =
Font(PlatformFont::CreateFromNameAndSize(font_name, font_size));
// System fonts may have different styles when they are manually changed by
// the users (see crbug.com/989476).
Font::FontStyle style = logfont.lfItalic == 0 ? Font::FontStyle::NORMAL
: Font::FontStyle::ITALIC;
Font::Weight weight = logfont.lfWeight == 0
? Font::Weight::NORMAL
: static_cast<Font::Weight>(logfont.lfWeight);
if (style != Font::FontStyle::NORMAL || weight != Font::Weight::NORMAL)
system_font = system_font.Derive(0, style, weight);
return system_font;
}
static void SetGetMinimumFontSizeCallback(
......@@ -190,7 +203,7 @@ class SystemFonts {
// Cap at minimum font size.
logfont->lfHeight = AdjustFontSize(logfont->lfHeight, 0);
system_fonts_.emplace(system_font, GetFontFromLOGFONT(logfont));
system_fonts_.emplace(system_font, GetFontFromLOGFONT(*logfont));
}
// Returns the system DPI scale (standard DPI being 1.0).
......@@ -241,9 +254,8 @@ const Font& GetSystemFont(SystemFont system_font) {
return SystemFonts::Instance()->GetFont(system_font);
}
GFX_EXPORT NativeFont
AdjustExistingSystemFont(NativeFont existing_font,
const FontAdjustment& font_adjustment) {
NativeFont AdjustExistingSystemFont(NativeFont existing_font,
const FontAdjustment& font_adjustment) {
LOGFONT logfont;
auto result = GetObject(existing_font, sizeof(logfont), &logfont);
DCHECK(result);
......@@ -267,7 +279,12 @@ void AdjustLOGFONTForTesting(const FontAdjustment& font_adjustment,
SystemFonts::AdjustLOGFONT(font_adjustment, logfont);
}
GFX_EXPORT void ResetSystemFontsForTesting() {
// Retrieve a FONT from a LOGFONT structure.
Font GetFontFromLOGFONTForTesting(const LOGFONT& logfont) {
return SystemFonts::GetFontFromLOGFONT(logfont);
}
void ResetSystemFontsForTesting() {
SystemFonts::Instance()->ResetForTesting();
}
......
......@@ -54,7 +54,10 @@ GFX_EXPORT int AdjustFontSize(int lf_height, int size_delta);
GFX_EXPORT void AdjustLOGFONTForTesting(const FontAdjustment& font_adjustment,
LOGFONT* logfont);
// Clear the system fonts cache. SystemFonts is keeping a global state that
// Retrieves a FONT from a LOGFONT structure.
GFX_EXPORT Font GetFontFromLOGFONTForTesting(const LOGFONT& logfont);
// Clears the system fonts cache. SystemFonts is keeping a global state that
// must be reset in unittests when using |GetMinimumFontSizeCallback| or
// |SetAdjustFontCallback|.
GFX_EXPORT void ResetSystemFontsForTesting();
......
......@@ -145,5 +145,22 @@ TEST_F(SystemFontsWinTest, AdjustLOGFONT_ScaleUpWithRounding) {
EXPECT_STREQ(kSegoeUI, logfont.lfFaceName);
}
TEST_F(SystemFontsWinTest, GetFontFromLOGFONT) {
LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
Font font = GetFontFromLOGFONTForTesting(logfont);
EXPECT_EQ(font.GetStyle(), Font::FontStyle::NORMAL);
EXPECT_EQ(font.GetWeight(), Font::Weight::NORMAL);
}
TEST_F(SystemFontsWinTest, GetFontFromLOGFONT_WithStyle) {
LOGFONT logfont = CreateLOGFONT(kSegoeUI, -10);
logfont.lfItalic = 1;
logfont.lfWeight = 700;
Font font = GetFontFromLOGFONTForTesting(logfont);
EXPECT_EQ(font.GetStyle(), Font::FontStyle::ITALIC);
EXPECT_EQ(font.GetWeight(), Font::Weight::BOLD);
}
} // namespace win
} // namespace gfx
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment