OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/win/scoped_gdi_object.h" | |
17 #include "base/win/scoped_hdc.h" | 18 #include "base/win/scoped_hdc.h" |
18 #include "base/win/scoped_select_object.h" | 19 #include "base/win/scoped_select_object.h" |
19 #include "base/win/win_util.h" | 20 #include "base/win/win_util.h" |
20 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
21 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
22 #include "ui/gfx/win/scoped_set_map_mode.h" | 23 #include "ui/gfx/win/scoped_set_map_mode.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the | 27 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the |
(...skipping 26 matching lines...) Expand all Loading... | |
53 } | 54 } |
54 } | 55 } |
55 | 56 |
56 // Sets style properties on |font_info| based on |font_style|. | 57 // Sets style properties on |font_info| based on |font_style|. |
57 void SetLogFontStyle(int font_style, LOGFONT* font_info) { | 58 void SetLogFontStyle(int font_style, LOGFONT* font_info) { |
58 font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0; | 59 font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0; |
59 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0; | 60 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0; |
60 font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; | 61 font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; |
61 } | 62 } |
62 | 63 |
64 void GetTextMetricsForFont(HDC hdc, HFONT font, TEXTMETRIC* text_metrics) { | |
65 base::win::ScopedSelectObject scoped_font(hdc, font); | |
66 GetTextMetrics(hdc, text_metrics); | |
67 } | |
68 | |
63 } // namespace | 69 } // namespace |
64 | 70 |
65 namespace gfx { | 71 namespace gfx { |
66 | 72 |
67 // static | 73 // static |
68 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; | 74 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; |
69 | 75 |
70 // static | 76 // static |
71 PlatformFontWin::AdjustFontCallback | 77 PlatformFontWin::AdjustFontCallback |
72 PlatformFontWin::adjust_font_callback = NULL; | 78 PlatformFontWin::adjust_font_callback = NULL; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 } | 115 } |
110 return font; | 116 return font; |
111 } | 117 } |
112 | 118 |
113 LOGFONT font_info; | 119 LOGFONT font_info; |
114 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); | 120 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
115 font_info.lfHeight = height; | 121 font_info.lfHeight = height; |
116 SetLogFontStyle(style, &font_info); | 122 SetLogFontStyle(style, &font_info); |
117 | 123 |
118 HFONT hfont = CreateFontIndirect(&font_info); | 124 HFONT hfont = CreateFontIndirect(&font_info); |
119 return Font(new PlatformFontWin(CreateHFontRef(hfont))); | 125 return DeriveWithCorrectedSize(hfont); |
120 } | 126 } |
121 | 127 |
122 //////////////////////////////////////////////////////////////////////////////// | 128 //////////////////////////////////////////////////////////////////////////////// |
123 // PlatformFontWin, PlatformFont implementation: | 129 // PlatformFontWin, PlatformFont implementation: |
124 | 130 |
125 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { | 131 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { |
126 LOGFONT font_info; | 132 LOGFONT font_info; |
127 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); | 133 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
128 const int requested_font_size = font_ref_->requested_font_size(); | 134 const int requested_font_size = font_ref_->requested_font_size(); |
129 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta); | 135 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 base_font_ref_->AddRef(); | 229 base_font_ref_->AddRef(); |
224 } | 230 } |
225 return base_font_ref_; | 231 return base_font_ref_; |
226 } | 232 } |
227 | 233 |
228 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { | 234 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { |
229 TEXTMETRIC font_metrics; | 235 TEXTMETRIC font_metrics; |
230 | 236 |
231 { | 237 { |
232 base::win::ScopedGetDC screen_dc(NULL); | 238 base::win::ScopedGetDC screen_dc(NULL); |
233 base::win::ScopedSelectObject scoped_font(screen_dc, font); | |
234 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); | 239 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
235 GetTextMetrics(screen_dc, &font_metrics); | 240 GetTextMetricsForFont(screen_dc, font, &font_metrics); |
236 } | 241 } |
237 | 242 |
243 return CreateHFontRef(font, font_metrics); | |
244 } | |
245 | |
246 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef( | |
247 HFONT font, | |
248 const TEXTMETRIC& font_metrics) { | |
238 const int height = std::max<int>(1, font_metrics.tmHeight); | 249 const int height = std::max<int>(1, font_metrics.tmHeight); |
239 const int baseline = std::max<int>(1, font_metrics.tmAscent); | 250 const int baseline = std::max<int>(1, font_metrics.tmAscent); |
240 const int cap_height = | 251 const int cap_height = |
241 std::max<int>(1, font_metrics.tmAscent - font_metrics.tmInternalLeading); | 252 std::max<int>(1, font_metrics.tmAscent - font_metrics.tmInternalLeading); |
242 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); | 253 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); |
243 const int font_size = | 254 const int font_size = |
244 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); | 255 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); |
245 int style = 0; | 256 int style = 0; |
246 if (font_metrics.tmItalic) | 257 if (font_metrics.tmItalic) |
247 style |= Font::ITALIC; | 258 style |= Font::ITALIC; |
248 if (font_metrics.tmUnderlined) | 259 if (font_metrics.tmUnderlined) |
249 style |= Font::UNDERLINE; | 260 style |= Font::UNDERLINE; |
250 if (font_metrics.tmWeight >= kTextMetricWeightBold) | 261 if (font_metrics.tmWeight >= kTextMetricWeightBold) |
251 style |= Font::BOLD; | 262 style |= Font::BOLD; |
252 | 263 |
253 return new HFontRef(font, font_size, height, baseline, cap_height, | 264 return new HFontRef(font, font_size, height, baseline, cap_height, |
254 ave_char_width, style); | 265 ave_char_width, style); |
255 } | 266 } |
256 | 267 |
268 Font PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font) { | |
269 base::win::ScopedGetDC screen_dc(NULL); | |
270 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); | |
271 | |
272 base::win::ScopedGDIObject<HFONT> best_font(base_font); | |
273 TEXTMETRIC best_font_metrics; | |
274 GetTextMetricsForFont(screen_dc, best_font, &best_font_metrics); | |
275 | |
276 LOGFONT font_info; | |
277 GetObject(base_font, sizeof(LOGFONT), &font_info); | |
278 | |
279 do { | |
280 // Increment font size. Prefer font with greater size if its height isn't | |
281 // greater than height of base font. | |
282 font_info.lfHeight = | |
283 -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading + 1); | |
Alexei Svitkine (slow)
2014/06/11 16:58:48
Question: Is it necessary to do this calculation o
Tomasz Moniuszko
2014/06/12 08:04:24
Good catch! In the worst case size might be not in
| |
284 base::win::ScopedGDIObject<HFONT> font(CreateFontIndirect(&font_info)); | |
285 TEXTMETRIC font_metrics; | |
286 GetTextMetricsForFont(screen_dc, font, &font_metrics); | |
287 if (font_metrics.tmHeight > best_font_metrics.tmHeight) | |
288 break; | |
289 best_font.Set(font.release()); | |
290 best_font_metrics = font_metrics; | |
291 } while (true); | |
292 | |
293 return Font(new PlatformFontWin(CreateHFontRef(best_font.release()))); | |
294 } | |
295 | |
257 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { | 296 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { |
258 } | 297 } |
259 | 298 |
260 //////////////////////////////////////////////////////////////////////////////// | 299 //////////////////////////////////////////////////////////////////////////////// |
261 // PlatformFontWin::HFontRef: | 300 // PlatformFontWin::HFontRef: |
262 | 301 |
263 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, | 302 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, |
264 int font_size, | 303 int font_size, |
265 int height, | 304 int height, |
266 int baseline, | 305 int baseline, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 return new PlatformFontWin(native_font); | 361 return new PlatformFontWin(native_font); |
323 } | 362 } |
324 | 363 |
325 // static | 364 // static |
326 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 365 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
327 int font_size) { | 366 int font_size) { |
328 return new PlatformFontWin(font_name, font_size); | 367 return new PlatformFontWin(font_name, font_size); |
329 } | 368 } |
330 | 369 |
331 } // namespace gfx | 370 } // namespace gfx |
OLD | NEW |