1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/bind_helpers.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/singleton.h"
10 #include "base/win/registry.h"
11 #include "ui/gfx/font_render_params.h"
12 #include "ui/gfx/win/direct_write.h"
13 #include "ui/gfx/win/singleton_hwnd_observer.h"
19 FontRenderParams::SubpixelRendering
GetSubpixelRenderingGeometry() {
20 DISPLAY_DEVICE display_device
= {sizeof(DISPLAY_DEVICE
), 0};
21 for (int i
= 0; EnumDisplayDevices(nullptr, i
, &display_device
, 0); ++i
) {
22 // TODO(scottmg): We only support the primary device currently.
23 if (display_device
.StateFlags
& DISPLAY_DEVICE_PRIMARY_DEVICE
) {
24 base::FilePath trimmed
=
25 base::FilePath(display_device
.DeviceName
).BaseName();
26 base::win::RegKey
key(
28 (L
"SOFTWARE\\Microsoft\\Avalon.Graphics\\" + trimmed
.value()).c_str(),
30 DWORD pixel_structure
;
31 if (key
.ReadValueDW(L
"PixelStructure", &pixel_structure
) ==
33 if (pixel_structure
== 1)
34 return FontRenderParams::SUBPIXEL_RENDERING_RGB
;
35 if (pixel_structure
== 2)
36 return FontRenderParams::SUBPIXEL_RENDERING_BGR
;
42 // No explicit ClearType settings, default to RGB.
43 return FontRenderParams::SUBPIXEL_RENDERING_RGB
;
46 // Caches font render params and updates them on system notifications.
47 class CachedFontRenderParams
{
49 static CachedFontRenderParams
* GetInstance() {
50 return Singleton
<CachedFontRenderParams
>::get();
53 const FontRenderParams
& GetParams() {
57 params_
.reset(new FontRenderParams());
58 params_
->antialiasing
= false;
59 params_
->subpixel_positioning
= false;
60 params_
->autohinter
= false;
61 params_
->use_bitmaps
= false;
62 params_
->hinting
= FontRenderParams::HINTING_MEDIUM
;
63 params_
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
66 if (SystemParametersInfo(SPI_GETFONTSMOOTHING
, 0, &enabled
, 0) && enabled
) {
67 params_
->antialiasing
= true;
68 // GDI does not support subpixel positioning.
69 params_
->subpixel_positioning
= win::IsDirectWriteEnabled();
72 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE
, 0, &type
, 0) &&
73 type
== FE_FONTSMOOTHINGCLEARTYPE
) {
74 params_
->subpixel_rendering
= GetSubpixelRenderingGeometry();
77 singleton_hwnd_observer_
.reset(new SingletonHwndObserver(
78 base::Bind(&CachedFontRenderParams::OnWndProc
,
79 base::Unretained(this))));
84 friend struct DefaultSingletonTraits
<CachedFontRenderParams
>;
86 CachedFontRenderParams() {}
87 ~CachedFontRenderParams() {}
89 void OnWndProc(HWND hwnd
, UINT message
, WPARAM wparam
, LPARAM lparam
) {
90 if (message
== WM_SETTINGCHANGE
) {
92 singleton_hwnd_observer_
.reset(nullptr);
96 scoped_ptr
<FontRenderParams
> params_
;
97 scoped_ptr
<SingletonHwndObserver
> singleton_hwnd_observer_
;
99 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams
);
104 FontRenderParams
GetFontRenderParams(const FontRenderParamsQuery
& query
,
105 std::string
* family_out
) {
108 // Customized font rendering settings are not supported, only defaults.
109 return CachedFontRenderParams::GetInstance()->GetParams();