diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-17 15:53:01 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-17 15:53:01 +0000 |
commit | be5756e2d20c2aa54d8865c3695efb3cc2cba13e (patch) | |
tree | a9045beff453f5528ab6372ef22583b50ff2fc75 /ui | |
parent | 195588e411d59afe5c5c59f45b83db254ce9a9a0 (diff) | |
download | chromium_src-be5756e2d20c2aa54d8865c3695efb3cc2cba13e.zip chromium_src-be5756e2d20c2aa54d8865c3695efb3cc2cba13e.tar.gz chromium_src-be5756e2d20c2aa54d8865c3695efb3cc2cba13e.tar.bz2 |
Respect the system ClearType setting in RenderTextWin.
BUG=113184, 105550
TEST=Build Chrome Windows with gyp flag use_canvas_skia_skia=1. Open Chrome and observe text rendering in the tab strip, bookmarks bar, etc. Change Windows ClearType settings while Chrome is running. Text rendering should be updated appropriately.
Review URL: http://codereview.chromium.org/9370026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@122514 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/win/singleton_hwnd.cc | 81 | ||||
-rw-r--r-- | ui/base/win/singleton_hwnd.h | 59 | ||||
-rw-r--r-- | ui/gfx/font_smoothing_win.cc | 121 | ||||
-rw-r--r-- | ui/gfx/font_smoothing_win.h | 17 | ||||
-rw-r--r-- | ui/gfx/render_text.cc | 7 | ||||
-rw-r--r-- | ui/gfx/render_text.h | 1 | ||||
-rw-r--r-- | ui/gfx/render_text_win.cc | 7 | ||||
-rw-r--r-- | ui/ui.gyp | 4 |
8 files changed, 297 insertions, 0 deletions
diff --git a/ui/base/win/singleton_hwnd.cc b/ui/base/win/singleton_hwnd.cc new file mode 100644 index 0000000..7132be3 --- /dev/null +++ b/ui/base/win/singleton_hwnd.cc @@ -0,0 +1,81 @@ +// 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/base/win/singleton_hwnd.h" + +#include "base/memory/singleton.h" +#include "base/process_util.h" +#include "base/win/wrapped_window_proc.h" +#include "ui/base/win/hwnd_util.h" + +namespace { + +// Windows class name to use for the listener window. +const wchar_t kWindowClassName[] = L"Chrome_SingletonHwnd"; + +// Windows callback for listening for WM_* messages. +LRESULT CALLBACK ListenerWindowProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) { + ui::SingletonHwnd::GetInstance()->OnWndProc(hwnd, message, wparam, lparam); + return ::DefWindowProc(hwnd, message, wparam, lparam); +} + +// Creates a listener window to receive WM_* messages. +HWND CreateListenerWindow() { + WNDCLASSEX wc = {0}; + wc.cbSize = sizeof(wc); + wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>; + wc.hInstance = base::GetModuleFromAddress(&ListenerWindowProc); + wc.lpszClassName = kWindowClassName; + ATOM window_class = ::RegisterClassEx(&wc); + DCHECK(window_class); + + return ::CreateWindow(MAKEINTATOM(window_class), 0, 0, 0, 0, 0, 0, 0, 0, + wc.hInstance, 0); +} + +} // namespace + +namespace ui { + +// static +SingletonHwnd* SingletonHwnd::GetInstance() { + return Singleton<SingletonHwnd>::get(); +} + +void SingletonHwnd::AddObserver(Observer* observer) { + if (!listener_window_) { + listener_window_ = CreateListenerWindow(); + ui::CheckWindowCreated(listener_window_); + } + observer_list_.AddObserver(observer); +} + +void SingletonHwnd::RemoveObserver(Observer* observer) { + observer_list_.RemoveObserver(observer); +} + +void SingletonHwnd::OnWndProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) { + FOR_EACH_OBSERVER(Observer, + observer_list_, + OnWndProc(hwnd, message, wparam, lparam)); +} + +SingletonHwnd::SingletonHwnd() + : listener_window_(NULL) { +} + +SingletonHwnd::~SingletonHwnd() { + if (listener_window_) { + ::DestroyWindow(listener_window_); + ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL)); + } +} + +} // namespace ui diff --git a/ui/base/win/singleton_hwnd.h b/ui/base/win/singleton_hwnd.h new file mode 100644 index 0000000..49fb209 --- /dev/null +++ b/ui/base/win/singleton_hwnd.h @@ -0,0 +1,59 @@ +// 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. + +#ifndef UI_BASE_WIN_SINGLETON_HWND_H_ +#define UI_BASE_WIN_SINGLETON_HWND_H_ +#pragma once + +#include <windows.h> +#include <vector> + +#include "base/basictypes.h" +#include "base/callback_forward.h" +#include "base/observer_list.h" + +template<typename T> struct DefaultSingletonTraits; + +namespace ui { + +// Singleton message-only HWND that allows interested clients to receive WM_* +// notifications. +class SingletonHwnd { + public: + static SingletonHwnd* GetInstance(); + + // Observer interface for receiving Windows WM_* notifications. + class Observer { + public: + virtual void OnWndProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) = 0; + }; + + // Add/remove observer to receive WM_* notifications. + void AddObserver(Observer* observer); + void RemoveObserver(Observer* observer); + + // Windows callback for WM_* notifications. + void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); + + private: + friend struct DefaultSingletonTraits<SingletonHwnd>; + + SingletonHwnd(); + ~SingletonHwnd(); + + // Listener HWND for WM_* notifications. + HWND listener_window_; + + // List of registered observers. + ObserverList<Observer> observer_list_; + + DISALLOW_COPY_AND_ASSIGN(SingletonHwnd); +}; + +} // namespace ui + +#endif // UI_BASE_WIN_SINGLETON_HWND_H_ diff --git a/ui/gfx/font_smoothing_win.cc b/ui/gfx/font_smoothing_win.cc new file mode 100644 index 0000000..a4be729 --- /dev/null +++ b/ui/gfx/font_smoothing_win.cc @@ -0,0 +1,121 @@ +// 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/memory/singleton.h" +#include "ui/base/win/singleton_hwnd.h" + +namespace { + +// Helper class to cache font smoothing settings and listen for notifications +// to re-query them from the system. +class CachedFontSmoothingSettings : public ui::SingletonHwnd::Observer { + public: + static CachedFontSmoothingSettings* GetInstance(); + + // Returns the cached Windows font smoothing settings. Queries the settings + // via Windows APIs and begins listening for changes when called for the + // first time. + void GetFontSmoothingSettings(bool* smoothing_enabled, + bool* cleartype_enabled); + + private: + friend struct DefaultSingletonTraits<CachedFontSmoothingSettings>; + + CachedFontSmoothingSettings(); + virtual ~CachedFontSmoothingSettings(); + + // Listener for WM_SETTINGCHANGE notifications. + virtual void OnWndProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) OVERRIDE; + + // Queries the font settings from the system. + void QueryFontSettings(); + + // Indicates whether the MessagePumpObserver has been registered. + bool observer_added_; + + // Indicates whether |smoothing_enabled_| and |cleartype_enabled_| are valid + // or need to be re-queried from the system. + bool need_to_query_settings_; + + // 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); +}; + +// static +CachedFontSmoothingSettings* CachedFontSmoothingSettings::GetInstance() { + return Singleton<CachedFontSmoothingSettings>::get(); +} + +void CachedFontSmoothingSettings::GetFontSmoothingSettings( + bool* smoothing_enabled, + bool* cleartype_enabled) { + // If cached settings are stale, query them from the OS. + if (need_to_query_settings_) { + QueryFontSettings(); + need_to_query_settings_ = false; + } + if (!observer_added_) { + ui::SingletonHwnd::GetInstance()->AddObserver(this); + observer_added_ = true; + } + *smoothing_enabled = smoothing_enabled_; + *cleartype_enabled = cleartype_enabled_; +} + +CachedFontSmoothingSettings::CachedFontSmoothingSettings() + : observer_added_(false), + need_to_query_settings_(true), + smoothing_enabled_(false), + cleartype_enabled_(false) { +} + +CachedFontSmoothingSettings::~CachedFontSmoothingSettings() { + // Can't remove the SingletonHwnd observer here since SingletonHwnd may have + // been destroyed already (both singletons). +} + +void CachedFontSmoothingSettings::OnWndProc(HWND hwnd, + UINT message, + WPARAM wparam, + LPARAM lparam) { + if (message == WM_SETTINGCHANGE) + need_to_query_settings_ = true; +} + +void CachedFontSmoothingSettings::QueryFontSettings() { + smoothing_enabled_ = false; + cleartype_enabled_ = false; + + BOOL enabled = false; + if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) { + smoothing_enabled_ = true; + + UINT smooth_type = 0; + 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); +} + +} // namespace gfx diff --git a/ui/gfx/font_smoothing_win.h b/ui/gfx/font_smoothing_win.h new file mode 100644 index 0000000..2ab6688 --- /dev/null +++ b/ui/gfx/font_smoothing_win.h @@ -0,0 +1,17 @@ +// 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. + +#ifndef UI_GFX_FONT_SMOOTHING_WIN_H_ +#define UI_GFX_FONT_SMOOTHING_WIN_H_ +#pragma once + +namespace gfx { + +// Returns the Windows system font smoothing and ClearType settings. +void GetCachedFontSmoothingSettings(bool* smoothing_enabled, + bool* cleartype_enabled); + +} // namespace gfx + +#endif // UI_GFX_FONT_SMOOTHING_WIN_H_ diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc index 5b9b8c4..80550c3 100644 --- a/ui/gfx/render_text.cc +++ b/ui/gfx/render_text.cc @@ -185,6 +185,13 @@ SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) SkiaTextRenderer::~SkiaTextRenderer() { } +void SkiaTextRenderer::SetFontSmoothingSettings(bool enable_smoothing, + bool enable_lcd_text) { + paint_.setAntiAlias(enable_smoothing); + paint_.setSubpixelText(enable_smoothing); + paint_.setLCDRenderText(enable_lcd_text); +} + void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { paint_.setTypeface(typeface); } diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h index db98b84..2498bcb 100644 --- a/ui/gfx/render_text.h +++ b/ui/gfx/render_text.h @@ -37,6 +37,7 @@ class SkiaTextRenderer { explicit SkiaTextRenderer(Canvas* canvas); ~SkiaTextRenderer(); + void SetFontSmoothingSettings(bool enable_smoothing, bool enable_lcd_text); void SetTypeface(SkTypeface* typeface); void SetTextSize(int size); void SetFont(const gfx::Font& font); diff --git a/ui/gfx/render_text_win.cc b/ui/gfx/render_text_win.cc index 36cd23b..608cc7d 100644 --- a/ui/gfx/render_text_win.cc +++ b/ui/gfx/render_text_win.cc @@ -11,6 +11,7 @@ #include "base/stl_util.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "ui/gfx/font_smoothing_win.h" #include "ui/gfx/canvas.h" #include "ui/gfx/canvas_skia.h" #include "ui/gfx/platform_font.h" @@ -441,6 +442,12 @@ void RenderTextWin::DrawVisualText(Canvas* canvas) { internal::SkiaTextRenderer renderer(canvas); ApplyFadeEffects(&renderer); + bool smoothing_enabled; + bool cleartype_enabled; + GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); + // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. + renderer.SetFontSmoothingSettings(smoothing_enabled, cleartype_enabled); + for (size_t i = 0; i < runs_.size(); ++i) { // Get the run specified by the visual-to-logical map. internal::TextRun* run = runs_[visual_to_logical_[i]]; @@ -220,6 +220,8 @@ 'base/win/mouse_wheel_util.h', 'base/win/shell.cc', 'base/win/shell.h', + 'base/win/singleton_hwnd.cc', + 'base/win/singleton_hwnd.h', 'base/win/window_impl.cc', 'base/win/window_impl.h', 'base/x/active_window_watcher_x.cc', @@ -261,6 +263,8 @@ 'gfx/font.cc', 'gfx/font_list.h', 'gfx/font_list.cc', + 'gfx/font_smoothing_win.cc', + 'gfx/font_smoothing_win.h', 'gfx/gfx_paths.cc', 'gfx/gfx_paths.h', 'gfx/image/image.cc', |