OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/platform_font_win.h" | 5 #include "ui/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 string16& font_name, | 72 PlatformFontWin::PlatformFontWin(const std::string& 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 string16 PlatformFontWin::GetFontName() const { | 120 std::string 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 DCHECK_LT(font_info.lfHeight, 0); | 127 DCHECK_LT(font_info.lfHeight, 0); |
128 return -font_info.lfHeight; | 128 return -font_info.lfHeight; |
129 } | 129 } |
130 | 130 |
131 NativeFont PlatformFontWin::GetNativeFont() const { | 131 NativeFont PlatformFontWin::GetNativeFont() const { |
132 return font_ref_->hfont(); | 132 return font_ref_->hfont(); |
133 } | 133 } |
134 | 134 |
135 //////////////////////////////////////////////////////////////////////////////// | 135 //////////////////////////////////////////////////////////////////////////////// |
136 // Font, private: | 136 // Font, private: |
137 | 137 |
138 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) { | 138 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) { |
139 DCHECK(hfont); | 139 DCHECK(hfont); |
140 LOGFONT font_info; | 140 LOGFONT font_info; |
141 GetObject(hfont, sizeof(LOGFONT), &font_info); | 141 GetObject(hfont, sizeof(LOGFONT), &font_info); |
142 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info)); | 142 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info)); |
143 } | 143 } |
144 | 144 |
145 void PlatformFontWin::InitWithFontNameAndSize(const string16& font_name, | 145 void PlatformFontWin::InitWithFontNameAndSize(const std::string& font_name, |
146 int font_size) { | 146 int font_size) { |
147 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 147 HFONT hf = ::CreateFont(-font_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
148 font_name.c_str()); | 148 UTF8ToUTF16(font_name).c_str()); |
149 font_ref_ = CreateHFontRef(hf); | 149 font_ref_ = CreateHFontRef(hf); |
150 } | 150 } |
151 | 151 |
152 // static | 152 // static |
153 PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() { | 153 PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() { |
154 if (base_font_ref_ == NULL) { | 154 if (base_font_ref_ == NULL) { |
155 NONCLIENTMETRICS metrics; | 155 NONCLIENTMETRICS metrics; |
156 base::win::GetNonClientMetrics(&metrics); | 156 base::win::GetNonClientMetrics(&metrics); |
157 | 157 |
158 if (adjust_font_callback) | 158 if (adjust_font_callback) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 : hfont_(hfont), | 217 : hfont_(hfont), |
218 height_(height), | 218 height_(height), |
219 baseline_(baseline), | 219 baseline_(baseline), |
220 ave_char_width_(ave_char_width), | 220 ave_char_width_(ave_char_width), |
221 style_(style), | 221 style_(style), |
222 dlu_base_x_(dlu_base_x) { | 222 dlu_base_x_(dlu_base_x) { |
223 DLOG_ASSERT(hfont); | 223 DLOG_ASSERT(hfont); |
224 | 224 |
225 LOGFONT font_info; | 225 LOGFONT font_info; |
226 GetObject(hfont_, sizeof(LOGFONT), &font_info); | 226 GetObject(hfont_, sizeof(LOGFONT), &font_info); |
227 font_name_ = string16(font_info.lfFaceName); | 227 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); |
228 } | 228 } |
229 | 229 |
230 PlatformFontWin::HFontRef::~HFontRef() { | 230 PlatformFontWin::HFontRef::~HFontRef() { |
231 DeleteObject(hfont_); | 231 DeleteObject(hfont_); |
232 } | 232 } |
233 | 233 |
234 //////////////////////////////////////////////////////////////////////////////// | 234 //////////////////////////////////////////////////////////////////////////////// |
235 // PlatformFont, public: | 235 // PlatformFont, public: |
236 | 236 |
237 // static | 237 // static |
238 PlatformFont* PlatformFont::CreateDefault() { | 238 PlatformFont* PlatformFont::CreateDefault() { |
239 return new PlatformFontWin; | 239 return new PlatformFontWin; |
240 } | 240 } |
241 | 241 |
242 // static | 242 // static |
243 PlatformFont* PlatformFont::CreateFromFont(const Font& other) { | 243 PlatformFont* PlatformFont::CreateFromFont(const Font& other) { |
244 return new PlatformFontWin(other); | 244 return new PlatformFontWin(other); |
245 } | 245 } |
246 | 246 |
247 // static | 247 // static |
248 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { | 248 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) { |
249 return new PlatformFontWin(native_font); | 249 return new PlatformFontWin(native_font); |
250 } | 250 } |
251 | 251 |
252 // static | 252 // static |
253 PlatformFont* PlatformFont::CreateFromNameAndSize(const string16& font_name, | 253 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
254 int font_size) { | 254 int font_size) { |
255 return new PlatformFontWin(font_name, font_size); | 255 return new PlatformFontWin(font_name, font_size); |
256 } | 256 } |
257 | 257 |
258 } // namespace gfx | 258 } // namespace gfx |
OLD | NEW |