| Index: gfx/platform_font_win.cc
|
| ===================================================================
|
| --- gfx/platform_font_win.cc (revision 55264)
|
| +++ gfx/platform_font_win.cc (working copy)
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "gfx/font.h"
|
| +#include "gfx/platform_font_win.h"
|
|
|
| #include <windows.h>
|
| #include <math.h>
|
| @@ -13,31 +13,25 @@
|
| #include "base/string_util.h"
|
| #include "base/win_util.h"
|
| #include "gfx/canvas_skia.h"
|
| +#include "gfx/font.h"
|
|
|
| -namespace gfx {
|
| +namespace {
|
|
|
| -// static
|
| -Font::HFontRef* Font::base_font_ref_;
|
| -
|
| -// static
|
| -Font::AdjustFontCallback Font::adjust_font_callback = NULL;
|
| -Font::GetMinimumFontSizeCallback Font::get_minimum_font_size_callback = NULL;
|
| -
|
| // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
|
| // font is bold.
|
| -static const int kTextMetricWeightBold = 700;
|
| +const int kTextMetricWeightBold = 700;
|
|
|
| // Returns either minimum font allowed for a current locale or
|
| // lf_height + size_delta value.
|
| -static int AdjustFontSize(int lf_height, int size_delta) {
|
| +int AdjustFontSize(int lf_height, int size_delta) {
|
| if (lf_height < 0) {
|
| lf_height -= size_delta;
|
| } else {
|
| lf_height += size_delta;
|
| }
|
| int min_font_size = 0;
|
| - if (Font::get_minimum_font_size_callback)
|
| - min_font_size = Font::get_minimum_font_size_callback();
|
| + if (gfx::PlatformFontWin::get_minimum_font_size_callback)
|
| + min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback();
|
| // Make sure lf_height is not smaller than allowed min font size for current
|
| // locale.
|
| if (abs(lf_height) < min_font_size) {
|
| @@ -47,57 +41,126 @@
|
| }
|
| }
|
|
|
| -//
|
| -// Font
|
| -//
|
| +} // namespace
|
|
|
| -Font::Font()
|
| - : font_ref_(GetBaseFontRef()) {
|
| +namespace gfx {
|
| +
|
| +// static
|
| +PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
|
| +
|
| +// static
|
| +PlatformFontWin::AdjustFontCallback
|
| + PlatformFontWin::adjust_font_callback = NULL;
|
| +PlatformFontWin::GetMinimumFontSizeCallback
|
| + PlatformFontWin::get_minimum_font_size_callback = NULL;
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// PlatformFontWin, public
|
| +
|
| +PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
|
| }
|
|
|
| -int Font::height() const {
|
| +PlatformFontWin::PlatformFontWin(const Font& other) {
|
| + InitWithCopyOfHFONT(other.GetNativeFont());
|
| +}
|
| +
|
| +PlatformFontWin::PlatformFontWin(NativeFont native_font) {
|
| + InitWithCopyOfHFONT(native_font);
|
| +}
|
| +
|
| +PlatformFontWin::PlatformFontWin(const std::wstring& font_name,
|
| + int font_size) {
|
| + InitWithFontNameAndSize(font_name, font_size);
|
| +}
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// PlatformFontWin, PlatformFont implementation:
|
| +
|
| +Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
|
| + LOGFONT font_info;
|
| + GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
|
| + font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta);
|
| + font_info.lfUnderline =
|
| + ((style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED);
|
| + font_info.lfItalic = ((style & gfx::Font::ITALIC) == gfx::Font::ITALIC);
|
| + font_info.lfWeight = (style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL;
|
| +
|
| + HFONT hfont = CreateFontIndirect(&font_info);
|
| + return Font(new PlatformFontWin(CreateHFontRef(hfont)));
|
| +}
|
| +
|
| +int PlatformFontWin::GetHeight() const {
|
| return font_ref_->height();
|
| }
|
|
|
| -int Font::baseline() const {
|
| +int PlatformFontWin::GetBaseline() const {
|
| return font_ref_->baseline();
|
| }
|
|
|
| -int Font::ave_char_width() const {
|
| +int PlatformFontWin::GetAverageCharacterWidth() const {
|
| return font_ref_->ave_char_width();
|
| }
|
|
|
| -int Font::GetExpectedTextWidth(int length) const {
|
| - return length * std::min(font_ref_->dlu_base_x(), ave_char_width());
|
| +int PlatformFontWin::GetStringWidth(const std::wstring& text) const {
|
| + int width = 0, height = 0;
|
| + CanvasSkia::SizeStringInt(text, Font(const_cast<PlatformFontWin*>(this)),
|
| + &width, &height, gfx::Canvas::NO_ELLIPSIS);
|
| + return width;
|
| }
|
|
|
| -int Font::style() const {
|
| +int PlatformFontWin::GetExpectedTextWidth(int length) const {
|
| + return length * std::min(font_ref_->dlu_base_x(), GetAverageCharacterWidth());
|
| +}
|
| +
|
| +int PlatformFontWin::GetStyle() const {
|
| return font_ref_->style();
|
| }
|
|
|
| -NativeFont Font::nativeFont() const {
|
| - return hfont();
|
| +const std::wstring& PlatformFontWin::GetFontName() const {
|
| + return font_ref_->font_name();
|
| }
|
|
|
| -// static
|
| -Font Font::CreateFont(HFONT font) {
|
| - DCHECK(font);
|
| +int PlatformFontWin::GetFontSize() const {
|
| LOGFONT font_info;
|
| - GetObject(font, sizeof(LOGFONT), &font_info);
|
| - return Font(CreateHFontRef(CreateFontIndirect(&font_info)));
|
| + GetObject(font_ref_->hfont(), sizeof(LOGFONT), &font_info);
|
| + long lf_height = font_info.lfHeight;
|
| + HDC hdc = GetDC(NULL);
|
| + int device_caps = GetDeviceCaps(hdc, LOGPIXELSY);
|
| + int font_size = 0;
|
| + if (device_caps != 0) {
|
| + float font_size_float = -static_cast<float>(lf_height)*72/device_caps;
|
| + font_size = static_cast<int>(::ceil(font_size_float - 0.5));
|
| + }
|
| + ReleaseDC(NULL, hdc);
|
| + return font_size;
|
| }
|
|
|
| -Font Font::CreateFont(const std::wstring& font_name, int font_size) {
|
| +NativeFont PlatformFontWin::GetNativeFont() const {
|
| + return font_ref_->hfont();
|
| +}
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// Font, private:
|
| +
|
| +void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) {
|
| + DCHECK(hfont);
|
| + LOGFONT font_info;
|
| + GetObject(hfont, sizeof(LOGFONT), &font_info);
|
| + font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info));
|
| +}
|
| +
|
| +void PlatformFontWin::InitWithFontNameAndSize(const std::wstring& font_name,
|
| + int font_size) {
|
| HDC hdc = GetDC(NULL);
|
| long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72);
|
| ReleaseDC(NULL, hdc);
|
| HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
| - font_name.c_str());
|
| - return Font::CreateFont(hf);
|
| + font_name.c_str());
|
| + font_ref_ = CreateHFontRef(hf);
|
| }
|
|
|
| // static
|
| -Font::HFontRef* Font::GetBaseFontRef() {
|
| +PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() {
|
| if (base_font_ref_ == NULL) {
|
| NONCLIENTMETRICS metrics;
|
| win_util::GetNonClientMetrics(&metrics);
|
| @@ -108,33 +171,54 @@
|
| AdjustFontSize(metrics.lfMessageFont.lfHeight, 0);
|
| HFONT font = CreateFontIndirect(&metrics.lfMessageFont);
|
| DLOG_ASSERT(font);
|
| - base_font_ref_ = Font::CreateHFontRef(font);
|
| + base_font_ref_ = PlatformFontWin::CreateHFontRef(font);
|
| // base_font_ref_ is global, up the ref count so it's never deleted.
|
| base_font_ref_->AddRef();
|
| }
|
| return base_font_ref_;
|
| }
|
|
|
| -const std::wstring& Font::FontName() const {
|
| - return font_ref_->font_name();
|
| +PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
|
| + TEXTMETRIC font_metrics;
|
| + HDC screen_dc = GetDC(NULL);
|
| + HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font));
|
| + int last_map_mode = SetMapMode(screen_dc, MM_TEXT);
|
| + GetTextMetrics(screen_dc, &font_metrics);
|
| + // Yes, this is how Microsoft recommends calculating the dialog unit
|
| + // conversions.
|
| + SIZE ave_text_size;
|
| + GetTextExtentPoint32(screen_dc,
|
| + L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
| + 52, &ave_text_size);
|
| + const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2;
|
| + // To avoid the DC referencing font_handle_, select the previous font.
|
| + SelectObject(screen_dc, previous_font);
|
| + SetMapMode(screen_dc, last_map_mode);
|
| + ReleaseDC(NULL, screen_dc);
|
| +
|
| + const int height = std::max(1, static_cast<int>(font_metrics.tmHeight));
|
| + const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent));
|
| + const int ave_char_width =
|
| + std::max(1, static_cast<int>(font_metrics.tmAveCharWidth));
|
| + int style = 0;
|
| + if (font_metrics.tmItalic)
|
| + style |= Font::ITALIC;
|
| + if (font_metrics.tmUnderlined)
|
| + style |= Font::UNDERLINED;
|
| + if (font_metrics.tmWeight >= kTextMetricWeightBold)
|
| + style |= Font::BOLD;
|
| +
|
| + return new HFontRef(font, height, baseline, ave_char_width, style,
|
| + dlu_base_x);
|
| }
|
|
|
| -int Font::FontSize() {
|
| - LOGFONT font_info;
|
| - GetObject(hfont(), sizeof(LOGFONT), &font_info);
|
| - long lf_height = font_info.lfHeight;
|
| - HDC hdc = GetDC(NULL);
|
| - int device_caps = GetDeviceCaps(hdc, LOGPIXELSY);
|
| - int font_size = 0;
|
| - if (device_caps != 0) {
|
| - float font_size_float = -static_cast<float>(lf_height)*72/device_caps;
|
| - font_size = static_cast<int>(::ceil(font_size_float - 0.5));
|
| - }
|
| - ReleaseDC(NULL, hdc);
|
| - return font_size;
|
| +PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
|
| }
|
|
|
| -Font::HFontRef::HFontRef(HFONT hfont,
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// PlatformFontWin::HFontRef:
|
| +
|
| +PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
|
| int height,
|
| int baseline,
|
| int ave_char_width,
|
| @@ -153,64 +237,32 @@
|
| font_name_ = std::wstring(font_info.lfFaceName);
|
| }
|
|
|
| -Font::HFontRef::~HFontRef() {
|
| +PlatformFontWin::HFontRef::~HFontRef() {
|
| DeleteObject(hfont_);
|
| }
|
|
|
| -Font Font::DeriveFont(int size_delta, int style) const {
|
| - LOGFONT font_info;
|
| - GetObject(hfont(), sizeof(LOGFONT), &font_info);
|
| - font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta);
|
| - font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED);
|
| - font_info.lfItalic = ((style & ITALIC) == ITALIC);
|
| - font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL;
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// PlatformFont, public:
|
|
|
| - HFONT hfont = CreateFontIndirect(&font_info);
|
| - return Font(CreateHFontRef(hfont));
|
| +// static
|
| +PlatformFont* PlatformFont::CreateDefault() {
|
| + return new PlatformFontWin;
|
| }
|
|
|
| -int Font::GetStringWidth(const std::wstring& text) const {
|
| - int width = 0, height = 0;
|
| - CanvasSkia::SizeStringInt(text, *this, &width, &height,
|
| - gfx::Canvas::NO_ELLIPSIS);
|
| - return width;
|
| +// static
|
| +PlatformFont* PlatformFont::CreateFromFont(const Font& other) {
|
| + return new PlatformFontWin(other);
|
| }
|
|
|
| -Font::HFontRef* Font::CreateHFontRef(HFONT font) {
|
| - TEXTMETRIC font_metrics;
|
| - HDC screen_dc = GetDC(NULL);
|
| - HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font));
|
| - int last_map_mode = SetMapMode(screen_dc, MM_TEXT);
|
| - GetTextMetrics(screen_dc, &font_metrics);
|
| - // Yes, this is how Microsoft recommends calculating the dialog unit
|
| - // conversions.
|
| - SIZE ave_text_size;
|
| - GetTextExtentPoint32(screen_dc,
|
| - L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
| - 52, &ave_text_size);
|
| - const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2;
|
| - // To avoid the DC referencing font_handle_, select the previous font.
|
| - SelectObject(screen_dc, previous_font);
|
| - SetMapMode(screen_dc, last_map_mode);
|
| - ReleaseDC(NULL, screen_dc);
|
| +// static
|
| +PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
|
| + return new PlatformFontWin(native_font);
|
| +}
|
|
|
| - const int height = std::max(1, static_cast<int>(font_metrics.tmHeight));
|
| - const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent));
|
| - const int ave_char_width =
|
| - std::max(1, static_cast<int>(font_metrics.tmAveCharWidth));
|
| - int style = 0;
|
| - if (font_metrics.tmItalic) {
|
| - style |= Font::ITALIC;
|
| - }
|
| - if (font_metrics.tmUnderlined) {
|
| - style |= Font::UNDERLINED;
|
| - }
|
| - if (font_metrics.tmWeight >= kTextMetricWeightBold) {
|
| - style |= Font::BOLD;
|
| - }
|
| -
|
| - return new HFontRef(font, height, baseline, ave_char_width, style,
|
| - dlu_base_x);
|
| +// static
|
| +PlatformFont* PlatformFont::CreateFromNameAndSize(const std::wstring& font_name,
|
| + int font_size) {
|
| + return new PlatformFontWin(font_name, font_size);
|
| }
|
|
|
| } // namespace gfx
|
|
|