| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gfx/platform_font_win.h" | 5 #include "gfx/platform_font_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <math.h> | 8 #include <math.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 PlatformFontWin::PlatformFontWin(const Font& other) { | 64 PlatformFontWin::PlatformFontWin(const Font& other) { |
| 65 InitWithCopyOfHFONT(other.GetNativeFont()); | 65 InitWithCopyOfHFONT(other.GetNativeFont()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 PlatformFontWin::PlatformFontWin(NativeFont native_font) { | 68 PlatformFontWin::PlatformFontWin(NativeFont native_font) { |
| 69 InitWithCopyOfHFONT(native_font); | 69 InitWithCopyOfHFONT(native_font); |
| 70 } | 70 } |
| 71 | 71 |
| 72 PlatformFontWin::PlatformFontWin(const std::wstring& font_name, | 72 PlatformFontWin::PlatformFontWin(const string16& font_name, |
| 73 int font_size) { | 73 int font_size) { |
| 74 InitWithFontNameAndSize(font_name, font_size); | 74 InitWithFontNameAndSize(font_name, font_size); |
| 75 } | 75 } |
| 76 | 76 |
| 77 //////////////////////////////////////////////////////////////////////////////// | 77 //////////////////////////////////////////////////////////////////////////////// |
| 78 // PlatformFontWin, PlatformFont implementation: | 78 // PlatformFontWin, PlatformFont implementation: |
| 79 | 79 |
| 80 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { | 80 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { |
| 81 LOGFONT font_info; | 81 LOGFONT font_info; |
| 82 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); | 82 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 110 } | 110 } |
| 111 | 111 |
| 112 int PlatformFontWin::GetExpectedTextWidth(int length) const { | 112 int PlatformFontWin::GetExpectedTextWidth(int length) const { |
| 113 return length * std::min(font_ref_->dlu_base_x(), GetAverageCharacterWidth()); | 113 return length * std::min(font_ref_->dlu_base_x(), GetAverageCharacterWidth()); |
| 114 } | 114 } |
| 115 | 115 |
| 116 int PlatformFontWin::GetStyle() const { | 116 int PlatformFontWin::GetStyle() const { |
| 117 return font_ref_->style(); | 117 return font_ref_->style(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 const std::wstring& PlatformFontWin::GetFontName() const { | 120 string16 PlatformFontWin::GetFontName() const { |
| 121 return font_ref_->font_name(); | 121 return font_ref_->font_name(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 int PlatformFontWin::GetFontSize() const { | 124 int PlatformFontWin::GetFontSize() const { |
| 125 LOGFONT font_info; | 125 LOGFONT font_info; |
| 126 GetObject(font_ref_->hfont(), sizeof(LOGFONT), &font_info); | 126 GetObject(font_ref_->hfont(), sizeof(LOGFONT), &font_info); |
| 127 long lf_height = font_info.lfHeight; | 127 long lf_height = font_info.lfHeight; |
| 128 HDC hdc = GetDC(NULL); | 128 HDC hdc = GetDC(NULL); |
| 129 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY); | 129 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY); |
| 130 int font_size = 0; | 130 int font_size = 0; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 143 //////////////////////////////////////////////////////////////////////////////// | 143 //////////////////////////////////////////////////////////////////////////////// |
| 144 // Font, private: | 144 // Font, private: |
| 145 | 145 |
| 146 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) { | 146 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) { |
| 147 DCHECK(hfont); | 147 DCHECK(hfont); |
| 148 LOGFONT font_info; | 148 LOGFONT font_info; |
| 149 GetObject(hfont, sizeof(LOGFONT), &font_info); | 149 GetObject(hfont, sizeof(LOGFONT), &font_info); |
| 150 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info)); | 150 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info)); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void PlatformFontWin::InitWithFontNameAndSize(const std::wstring& font_name, | 153 void PlatformFontWin::InitWithFontNameAndSize(const string16& font_name, |
| 154 int font_size) { | 154 int font_size) { |
| 155 HDC hdc = GetDC(NULL); | 155 HDC hdc = GetDC(NULL); |
| 156 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); | 156 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); |
| 157 ReleaseDC(NULL, hdc); | 157 ReleaseDC(NULL, hdc); |
| 158 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 158 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 159 font_name.c_str()); | 159 font_name.c_str()); |
| 160 font_ref_ = CreateHFontRef(hf); | 160 font_ref_ = CreateHFontRef(hf); |
| 161 } | 161 } |
| 162 | 162 |
| 163 // static | 163 // static |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 : hfont_(hfont), | 228 : hfont_(hfont), |
| 229 height_(height), | 229 height_(height), |
| 230 baseline_(baseline), | 230 baseline_(baseline), |
| 231 ave_char_width_(ave_char_width), | 231 ave_char_width_(ave_char_width), |
| 232 style_(style), | 232 style_(style), |
| 233 dlu_base_x_(dlu_base_x) { | 233 dlu_base_x_(dlu_base_x) { |
| 234 DLOG_ASSERT(hfont); | 234 DLOG_ASSERT(hfont); |
| 235 | 235 |
| 236 LOGFONT font_info; | 236 LOGFONT font_info; |
| 237 GetObject(hfont_, sizeof(LOGFONT), &font_info); | 237 GetObject(hfont_, sizeof(LOGFONT), &font_info); |
| 238 font_name_ = std::wstring(font_info.lfFaceName); | 238 font_name_ = string16(font_info.lfFaceName); |
| 239 } | 239 } |
| 240 | 240 |
| 241 PlatformFontWin::HFontRef::~HFontRef() { | 241 PlatformFontWin::HFontRef::~HFontRef() { |
| 242 DeleteObject(hfont_); | 242 DeleteObject(hfont_); |
| 243 } | 243 } |
| 244 | 244 |
| 245 //////////////////////////////////////////////////////////////////////////////// | 245 //////////////////////////////////////////////////////////////////////////////// |
| 246 // PlatformFont, public: | 246 // PlatformFont, public: |
| 247 | 247 |
| 248 // static | 248 // static |
| 249 PlatformFont* PlatformFont::CreateDefault() { | 249 PlatformFont* PlatformFont::CreateDefault() { |
| 250 return new PlatformFontWin; | 250 return new PlatformFontWin; |
| 251 } | 251 } |
| 252 | 252 |
| 253 // static | 253 // static |
| 254 PlatformFont* PlatformFont::CreateFromFont(const Font& other) { | 254 PlatformFont* PlatformFont::CreateFromFont(const Font& other) { |
| 255 return new PlatformFontWin(other); | 255 return new PlatformFontWin(other); |
| 256 } | 256 } |
| 257 | 257 |
| 258 // static | 258 // static |
| 259 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 259 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
| 260 return new PlatformFontWin(native_font); | 260 return new PlatformFontWin(native_font); |
| 261 } | 261 } |
| 262 | 262 |
| 263 // static | 263 // static |
| 264 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::wstring& font_name, | 264 PlatformFont* PlatformFont::CreateFromNameAndSize(const string16& font_name, |
| 265 int font_size) { | 265 int font_size) { |
| 266 return new PlatformFontWin(font_name, font_size); | 266 return new PlatformFontWin(font_name, font_size); |
| 267 } | 267 } |
| 268 | 268 |
| 269 } // namespace gfx | 269 } // namespace gfx |
| OLD | NEW |