Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: ui/gfx/platform_font_win.cc

Issue 251773002: PlatformFontWin::DeriveFontWithHeight insensitive to base font's height (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: PlatformFontWin::DeriveFontWithHeight consistency fix after code review changes Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/platform_font_win.h ('k') | ui/gfx/platform_font_win_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 // Set |lfHeight| to negative value to indicate it's the size, not the height.
280 font_info.lfHeight =
281 -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading);
282
283 do {
284 // Increment font size. Prefer font with greater size if its height isn't
285 // greater than height of base font.
286 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, 1);
287 base::win::ScopedGDIObject<HFONT> font(CreateFontIndirect(&font_info));
288 TEXTMETRIC font_metrics;
289 GetTextMetricsForFont(screen_dc, font, &font_metrics);
290 if (font_metrics.tmHeight > best_font_metrics.tmHeight)
291 break;
292 best_font.Set(font.release());
293 best_font_metrics = font_metrics;
294 } while (true);
295
296 return Font(new PlatformFontWin(CreateHFontRef(best_font.release())));
297 }
298
257 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { 299 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
258 } 300 }
259 301
260 //////////////////////////////////////////////////////////////////////////////// 302 ////////////////////////////////////////////////////////////////////////////////
261 // PlatformFontWin::HFontRef: 303 // PlatformFontWin::HFontRef:
262 304
263 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, 305 PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
264 int font_size, 306 int font_size,
265 int height, 307 int height,
266 int baseline, 308 int baseline,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 return new PlatformFontWin(native_font); 364 return new PlatformFontWin(native_font);
323 } 365 }
324 366
325 // static 367 // static
326 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 368 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
327 int font_size) { 369 int font_size) {
328 return new PlatformFontWin(font_name, font_size); 370 return new PlatformFontWin(font_name, font_size);
329 } 371 }
330 372
331 } // namespace gfx 373 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/platform_font_win.h ('k') | ui/gfx/platform_font_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698