Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / font_render_params_win.cc
blob1fd15ab02f9ee2c85ff885d6ae6a621698f23b6a
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/bind.h"
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"
15 namespace gfx {
17 namespace {
19 FontRenderParams::SubpixelRendering GetSubpixelRenderingGeometry() {
20 DISPLAY_DEVICE display_device = {sizeof(DISPLAY_DEVICE)};
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(
27 HKEY_LOCAL_MACHINE,
28 (L"SOFTWARE\\Microsoft\\Avalon.Graphics\\" + trimmed.value()).c_str(),
29 KEY_READ);
30 DWORD pixel_structure;
31 if (key.ReadValueDW(L"PixelStructure", &pixel_structure) ==
32 ERROR_SUCCESS) {
33 if (pixel_structure == 1)
34 return FontRenderParams::SUBPIXEL_RENDERING_RGB;
35 if (pixel_structure == 2)
36 return FontRenderParams::SUBPIXEL_RENDERING_BGR;
38 break;
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 {
48 public:
49 static CachedFontRenderParams* GetInstance() {
50 return base::Singleton<CachedFontRenderParams>::get();
53 const FontRenderParams& GetParams() {
54 if (params_)
55 return *params_;
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;
65 BOOL enabled = false;
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();
71 UINT type = 0;
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))));
80 return *params_;
83 private:
84 friend struct base::DefaultSingletonTraits<CachedFontRenderParams>;
86 CachedFontRenderParams() {}
87 ~CachedFontRenderParams() {}
89 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
90 if (message == WM_SETTINGCHANGE) {
91 params_.reset();
92 singleton_hwnd_observer_.reset(nullptr);
96 scoped_ptr<FontRenderParams> params_;
97 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_;
99 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams);
102 } // namespace
104 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
105 std::string* family_out) {
106 if (family_out)
107 NOTIMPLEMENTED();
108 // Customized font rendering settings are not supported, only defaults.
109 return CachedFontRenderParams::GetInstance()->GetParams();
112 } // namespace gfx