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.
4
5 #include "ui/gfx/font_render_params.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "base/memory/singleton.h"
14 #include "base/win/registry.h"
15 #include "ui/gfx/win/singleton_hwnd_observer.h"
16
17 namespace gfx {
18
19 namespace {
20
GetSubpixelRenderingGeometry()21 FontRenderParams::SubpixelRendering GetSubpixelRenderingGeometry() {
22 DISPLAY_DEVICE display_device = {sizeof(DISPLAY_DEVICE)};
23 for (int i = 0; EnumDisplayDevices(nullptr, i, &display_device, 0); ++i) {
24 // TODO(scottmg): We only support the primary device currently.
25 if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
26 base::FilePath trimmed =
27 base::FilePath(display_device.DeviceName).BaseName();
28 base::win::RegKey key(
29 HKEY_LOCAL_MACHINE,
30 (L"SOFTWARE\\Microsoft\\Avalon.Graphics\\" + trimmed.value()).c_str(),
31 KEY_READ);
32 DWORD pixel_structure;
33 if (key.ReadValueDW(L"PixelStructure", &pixel_structure) ==
34 ERROR_SUCCESS) {
35 if (pixel_structure == 1)
36 return FontRenderParams::SUBPIXEL_RENDERING_RGB;
37 if (pixel_structure == 2)
38 return FontRenderParams::SUBPIXEL_RENDERING_BGR;
39 }
40 break;
41 }
42 }
43
44 // No explicit ClearType settings, default to RGB.
45 return FontRenderParams::SUBPIXEL_RENDERING_RGB;
46 }
47
48 // Caches font render params and updates them on system notifications.
49 class CachedFontRenderParams {
50 public:
GetInstance()51 static CachedFontRenderParams* GetInstance() {
52 return base::Singleton<CachedFontRenderParams>::get();
53 }
54
GetParams()55 const FontRenderParams& GetParams() {
56 if (params_)
57 return *params_;
58
59 params_ = std::make_unique<FontRenderParams>();
60 params_->antialiasing = false;
61 params_->subpixel_positioning = false;
62 params_->autohinter = false;
63 params_->use_bitmaps = false;
64 params_->hinting = FontRenderParams::HINTING_MEDIUM;
65 params_->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
66
67 BOOL enabled = false;
68 if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) {
69 params_->antialiasing = true;
70 params_->subpixel_positioning = true;
71
72 UINT type = 0;
73 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &type, 0) &&
74 type == FE_FONTSMOOTHINGCLEARTYPE) {
75 params_->subpixel_rendering = GetSubpixelRenderingGeometry();
76 }
77 }
78 singleton_hwnd_observer_ =
79 std::make_unique<SingletonHwndObserver>(base::BindRepeating(
80 &CachedFontRenderParams::OnWndProc, base::Unretained(this)));
81 return *params_;
82 }
83
84 private:
85 friend struct base::DefaultSingletonTraits<CachedFontRenderParams>;
86
CachedFontRenderParams()87 CachedFontRenderParams() {}
~CachedFontRenderParams()88 ~CachedFontRenderParams() {}
89
OnWndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)90 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
91 if (message == WM_SETTINGCHANGE) {
92 // TODO(khushalsagar): This should trigger an update to the
93 // renderer and gpu processes, where the params are cached.
94 params_.reset();
95 singleton_hwnd_observer_.reset(nullptr);
96 }
97 }
98
99 std::unique_ptr<FontRenderParams> params_;
100 std::unique_ptr<SingletonHwndObserver> singleton_hwnd_observer_;
101
102 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams);
103 };
104
105 } // namespace
106
GetFontRenderParams(const FontRenderParamsQuery & query,std::string * family_out)107 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
108 std::string* family_out) {
109 if (family_out)
110 NOTIMPLEMENTED();
111 // Customized font rendering settings are not supported, only defaults.
112 return CachedFontRenderParams::GetInstance()->GetParams();
113 }
114
GetFontRenderParamsDeviceScaleFactor()115 float GetFontRenderParamsDeviceScaleFactor() {
116 return 1.;
117 }
118
119 } // namespace gfx
120