Index: ui/gfx/font_smoothing_win.cc |
=================================================================== |
--- ui/gfx/font_smoothing_win.cc (revision 0) |
+++ ui/gfx/font_smoothing_win.cc (revision 0) |
@@ -0,0 +1,111 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/gfx/font_smoothing_win.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/memory/singleton.h" |
+#include "ui/base/win/singleton_hwnd.h" |
+ |
+namespace { |
+ |
+// Helper class to cache font smoothing settings and listen for WM_SETTINGCHANGE |
+// notifications to re-query them from the system. |
+class CachedFontSmoothingSettings { |
+ public: |
+ // Singleton getter. |
+ static CachedFontSmoothingSettings* GetInstance(); |
+ |
+ // Returns the cached screen compatible DC or creates one if needed. |
+ void GetFontSmoothingSettings(bool* smoothing_enabled, |
+ bool* cleartype_enabled); |
+ |
+ // Listener for WM_SETTINGCHANGE notifications. |
+ void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
+ |
+ private: |
+ friend struct DefaultSingletonTraits<CachedFontSmoothingSettings>; |
+ |
+ CachedFontSmoothingSettings(); |
+ ~CachedFontSmoothingSettings(); |
+ |
+ // Queries the font settings from the system. |
+ void QueryFontSettings(); |
+ |
+ // Indicates whether the listener has been registered with the SingletonHwnd. |
+ bool listener_registered_; |
+ |
+ // Indicates that font smoothing is enabled. |
+ bool smoothing_enabled_; |
+ |
+ // Indicates that the ClearType font smoothing is enabled. |
+ bool cleartype_enabled_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CachedFontSmoothingSettings); |
+}; |
+ |
+CachedFontSmoothingSettings::CachedFontSmoothingSettings() |
+ : listener_registered_(false), |
+ smoothing_enabled_(false), |
+ cleartype_enabled_(false) { |
+} |
+ |
+CachedFontSmoothingSettings::~CachedFontSmoothingSettings() { |
+} |
+ |
+// static |
+CachedFontSmoothingSettings* CachedFontSmoothingSettings::GetInstance() { |
+ return Singleton<CachedFontSmoothingSettings>::get(); |
+} |
+ |
+void CachedFontSmoothingSettings::OnWndProc(HWND hwnd, |
+ UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam) { |
+ if (message == WM_SETTINGCHANGE) |
+ QueryFontSettings(); |
+} |
+ |
+// Returns the cached screen compatible DC or creates one if needed. |
msw
2012/02/15 00:33:16
This comment just seems incorrect.
asvitkine_google
2012/02/15 15:10:44
Oops. Fixed.
|
+void CachedFontSmoothingSettings::GetFontSmoothingSettings( |
+ bool* smoothing_enabled, |
+ bool* cleartype_enabled) { |
+ if (!listener_registered_) { |
+ ui::WndProcCallback callback = |
+ base::Bind(&CachedFontSmoothingSettings::OnWndProc, |
+ base::Unretained(this)); |
+ ui::SingletonHwnd::GetInstance()->RegisterListener(callback); |
+ QueryFontSettings(); |
+ listener_registered_ = true; |
msw
2012/02/15 00:33:16
Bad advice?: I don't expect re-entrance here, but
asvitkine_google
2012/02/15 15:10:44
If folks are calling this on different threads, th
|
+ } |
+ *smoothing_enabled = smoothing_enabled_; |
+ *cleartype_enabled = cleartype_enabled_; |
+} |
+ |
+void CachedFontSmoothingSettings::QueryFontSettings() { |
+ smoothing_enabled_ = false; |
+ cleartype_enabled_ = false; |
+ |
+ BOOL enabled; |
msw
2012/02/15 00:33:16
You should explicitly init this local.
asvitkine_google
2012/02/15 15:10:44
Done.
|
+ if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) { |
msw
2012/02/15 00:33:16
Should we explicitly check for the BOOL to != 0?
asvitkine_google
2012/02/15 15:10:44
If we treat it like 'bool', we shouldn't..
|
+ smoothing_enabled_ = true; |
+ |
+ UINT smooth_type; |
msw
2012/02/15 00:33:16
You should explicitly init this local.
asvitkine_google
2012/02/15 15:10:44
Done.
|
+ if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &smooth_type, 0)) |
+ cleartype_enabled_ = (smooth_type == FE_FONTSMOOTHINGCLEARTYPE); |
+ } |
+} |
+ |
+} // namespace |
+ |
+namespace gfx { |
+ |
+void GetCachedFontSmoothingSettings(bool* smoothing_enabled, |
+ bool* cleartype_enabled) { |
+ CachedFontSmoothingSettings::GetInstance()->GetFontSmoothingSettings( |
+ smoothing_enabled, cleartype_enabled); |
msw
2012/02/15 00:33:16
These params should each be on their own lines.
asvitkine_google
2012/02/15 15:10:44
Done.
|
+} |
+ |
+} // namespace gfx |