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.
5 #include "base/files/file_path.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/memory/singleton.h"
8 #include "base/win/registry.h"
9 #include "ui/gfx/font_render_params.h"
10 #include "ui/gfx/win/direct_write.h"
11 #include "ui/gfx/win/singleton_hwnd.h"
17 FontRenderParams::SubpixelRendering
GetSubpixelRenderingGeometry() {
18 DISPLAY_DEVICE display_device
= {sizeof(DISPLAY_DEVICE
), 0};
19 for (int i
= 0; EnumDisplayDevices(nullptr, i
, &display_device
, 0); ++i
) {
20 // TODO(scottmg): We only support the primary device currently.
21 if (display_device
.StateFlags
& DISPLAY_DEVICE_PRIMARY_DEVICE
) {
22 base::FilePath trimmed
=
23 base::FilePath(display_device
.DeviceName
).BaseName();
24 base::win::RegKey
key(
26 (L
"SOFTWARE\\Microsoft\\Avalon.Graphics\\" + trimmed
.value()).c_str(),
28 DWORD pixel_structure
;
29 if (key
.ReadValueDW(L
"PixelStructure", &pixel_structure
) ==
31 if (pixel_structure
== 1)
32 return FontRenderParams::SUBPIXEL_RENDERING_RGB
;
33 if (pixel_structure
== 2)
34 return FontRenderParams::SUBPIXEL_RENDERING_BGR
;
40 // No explicit ClearType settings, default to RGB.
41 return FontRenderParams::SUBPIXEL_RENDERING_RGB
;
44 // Caches font render params and updates them on system notifications.
45 class CachedFontRenderParams
: public gfx::SingletonHwnd::Observer
{
47 static CachedFontRenderParams
* GetInstance() {
48 return Singleton
<CachedFontRenderParams
>::get();
51 const FontRenderParams
& GetParams() {
55 params_
.reset(new FontRenderParams());
56 params_
->antialiasing
= false;
57 params_
->subpixel_positioning
= false;
58 params_
->autohinter
= false;
59 params_
->use_bitmaps
= false;
60 params_
->hinting
= FontRenderParams::HINTING_MEDIUM
;
61 params_
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
64 if (SystemParametersInfo(SPI_GETFONTSMOOTHING
, 0, &enabled
, 0) && enabled
) {
65 params_
->antialiasing
= true;
66 // GDI does not support subpixel positioning.
67 params_
->subpixel_positioning
= win::IsDirectWriteEnabled();
70 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE
, 0, &type
, 0) &&
71 type
== FE_FONTSMOOTHINGCLEARTYPE
) {
72 params_
->subpixel_rendering
= GetSubpixelRenderingGeometry();
75 gfx::SingletonHwnd::GetInstance()->AddObserver(this);
80 friend struct DefaultSingletonTraits
<CachedFontRenderParams
>;
82 CachedFontRenderParams() {}
83 virtual ~CachedFontRenderParams() {
84 // Can't remove the SingletonHwnd observer here since SingletonHwnd may have
85 // been destroyed already (both singletons).
88 virtual void OnWndProc(HWND hwnd
,
91 LPARAM lparam
) override
{
92 if (message
== WM_SETTINGCHANGE
) {
94 gfx::SingletonHwnd::GetInstance()->RemoveObserver(this);
98 scoped_ptr
<FontRenderParams
> params_
;
100 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams
);
105 FontRenderParams
GetFontRenderParams(const FontRenderParamsQuery
& query
,
106 std::string
* family_out
) {
109 // Customized font rendering settings are not supported, only defaults.
110 return CachedFontRenderParams::GetInstance()->GetParams();