Home | History | Annotate | Download | only in gfx
      1 // Copyright (c) 2012 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_smoothing_win.h"
      6 
      7 #include "base/memory/singleton.h"
      8 #include "ui/base/win/singleton_hwnd.h"
      9 
     10 namespace {
     11 
     12 // Helper class to cache font smoothing settings and listen for notifications
     13 // to re-query them from the system.
     14 class CachedFontSmoothingSettings : public ui::SingletonHwnd::Observer {
     15  public:
     16   static CachedFontSmoothingSettings* GetInstance();
     17 
     18   // Returns the cached Windows font smoothing settings. Queries the settings
     19   // via Windows APIs and begins listening for changes when called for the
     20   // first time.
     21   void GetFontSmoothingSettings(bool* smoothing_enabled,
     22                                 bool* cleartype_enabled);
     23 
     24  private:
     25   friend struct DefaultSingletonTraits<CachedFontSmoothingSettings>;
     26 
     27   CachedFontSmoothingSettings();
     28   virtual ~CachedFontSmoothingSettings();
     29 
     30   // Listener for WM_SETTINGCHANGE notifications.
     31   virtual void OnWndProc(HWND hwnd,
     32                          UINT message,
     33                          WPARAM wparam,
     34                          LPARAM lparam) OVERRIDE;
     35 
     36   // Queries the font settings from the system.
     37   void QueryFontSettings();
     38 
     39   // Indicates whether the MessagePumpObserver has been registered.
     40   bool observer_added_;
     41 
     42   // Indicates whether |smoothing_enabled_| and |cleartype_enabled_| are valid
     43   // or need to be re-queried from the system.
     44   bool need_to_query_settings_;
     45 
     46   // Indicates that font smoothing is enabled.
     47   bool smoothing_enabled_;
     48 
     49   // Indicates that the ClearType font smoothing is enabled.
     50   bool cleartype_enabled_;
     51 
     52   DISALLOW_COPY_AND_ASSIGN(CachedFontSmoothingSettings);
     53 };
     54 
     55 // static
     56 CachedFontSmoothingSettings* CachedFontSmoothingSettings::GetInstance() {
     57   return Singleton<CachedFontSmoothingSettings>::get();
     58 }
     59 
     60 void CachedFontSmoothingSettings::GetFontSmoothingSettings(
     61     bool* smoothing_enabled,
     62     bool* cleartype_enabled) {
     63   // If cached settings are stale, query them from the OS.
     64   if (need_to_query_settings_) {
     65     QueryFontSettings();
     66     need_to_query_settings_ = false;
     67   }
     68   if (!observer_added_) {
     69     ui::SingletonHwnd::GetInstance()->AddObserver(this);
     70     observer_added_ = true;
     71   }
     72   *smoothing_enabled = smoothing_enabled_;
     73   *cleartype_enabled = cleartype_enabled_;
     74 }
     75 
     76 CachedFontSmoothingSettings::CachedFontSmoothingSettings()
     77     : observer_added_(false),
     78       need_to_query_settings_(true),
     79       smoothing_enabled_(false),
     80       cleartype_enabled_(false) {
     81 }
     82 
     83 CachedFontSmoothingSettings::~CachedFontSmoothingSettings() {
     84   // Can't remove the SingletonHwnd observer here since SingletonHwnd may have
     85   // been destroyed already (both singletons).
     86 }
     87 
     88 void CachedFontSmoothingSettings::OnWndProc(HWND hwnd,
     89                                             UINT message,
     90                                             WPARAM wparam,
     91                                             LPARAM lparam) {
     92   if (message == WM_SETTINGCHANGE)
     93     need_to_query_settings_ = true;
     94 }
     95 
     96 void CachedFontSmoothingSettings::QueryFontSettings() {
     97   smoothing_enabled_ = false;
     98   cleartype_enabled_ = false;
     99 
    100   BOOL enabled = false;
    101   if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) {
    102     smoothing_enabled_ = true;
    103 
    104     UINT smooth_type = 0;
    105     if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &smooth_type, 0))
    106       cleartype_enabled_ = (smooth_type == FE_FONTSMOOTHINGCLEARTYPE);
    107   }
    108 }
    109 
    110 }  // namespace
    111 
    112 namespace gfx {
    113 
    114 void GetCachedFontSmoothingSettings(bool* smoothing_enabled,
    115                                     bool* cleartype_enabled) {
    116   CachedFontSmoothingSettings::GetInstance()->GetFontSmoothingSettings(
    117       smoothing_enabled,
    118       cleartype_enabled);
    119 }
    120 
    121 }  // namespace gfx
    122