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

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: better font search optimization 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 110 }
110 return font; 111 return font;
111 } 112 }
112 113
113 LOGFONT font_info; 114 LOGFONT font_info;
114 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); 115 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
115 font_info.lfHeight = height; 116 font_info.lfHeight = height;
116 SetLogFontStyle(style, &font_info); 117 SetLogFontStyle(style, &font_info);
117 118
118 HFONT hfont = CreateFontIndirect(&font_info); 119 HFONT hfont = CreateFontIndirect(&font_info);
119 return Font(new PlatformFontWin(CreateHFontRef(hfont))); 120 return DeriveWithCorrectedSize(hfont);
120 } 121 }
121 122
122 //////////////////////////////////////////////////////////////////////////////// 123 ////////////////////////////////////////////////////////////////////////////////
123 // PlatformFontWin, PlatformFont implementation: 124 // PlatformFontWin, PlatformFont implementation:
124 125
125 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { 126 Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
126 LOGFONT font_info; 127 LOGFONT font_info;
127 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); 128 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
128 const int requested_font_size = font_ref_->requested_font_size(); 129 const int requested_font_size = font_ref_->requested_font_size();
129 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta); 130 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { 229 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
229 TEXTMETRIC font_metrics; 230 TEXTMETRIC font_metrics;
230 231
231 { 232 {
232 base::win::ScopedGetDC screen_dc(NULL); 233 base::win::ScopedGetDC screen_dc(NULL);
233 base::win::ScopedSelectObject scoped_font(screen_dc, font); 234 base::win::ScopedSelectObject scoped_font(screen_dc, font);
234 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); 235 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT);
235 GetTextMetrics(screen_dc, &font_metrics); 236 GetTextMetrics(screen_dc, &font_metrics);
236 } 237 }
237 238
239 return CreateHFontRef(font, font_metrics);
240 }
241
242 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(
243 HFONT font,
244 const TEXTMETRIC& font_metrics) {
238 const int height = std::max<int>(1, font_metrics.tmHeight); 245 const int height = std::max<int>(1, font_metrics.tmHeight);
239 const int baseline = std::max<int>(1, font_metrics.tmAscent); 246 const int baseline = std::max<int>(1, font_metrics.tmAscent);
240 const int cap_height = 247 const int cap_height =
241 std::max<int>(1, font_metrics.tmAscent - font_metrics.tmInternalLeading); 248 std::max<int>(1, font_metrics.tmAscent - font_metrics.tmInternalLeading);
242 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); 249 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth);
243 const int font_size = 250 const int font_size =
244 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); 251 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading);
245 int style = 0; 252 int style = 0;
246 if (font_metrics.tmItalic) 253 if (font_metrics.tmItalic)
247 style |= Font::ITALIC; 254 style |= Font::ITALIC;
248 if (font_metrics.tmUnderlined) 255 if (font_metrics.tmUnderlined)
249 style |= Font::UNDERLINE; 256 style |= Font::UNDERLINE;
250 if (font_metrics.tmWeight >= kTextMetricWeightBold) 257 if (font_metrics.tmWeight >= kTextMetricWeightBold)
251 style |= Font::BOLD; 258 style |= Font::BOLD;
252 259
253 return new HFontRef(font, font_size, height, baseline, cap_height, 260 return new HFontRef(font, font_size, height, baseline, cap_height,
254 ave_char_width, style); 261 ave_char_width, style);
255 } 262 }
256 263
264 Font PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font) {
Alexei Svitkine (slow) 2014/06/02 15:42:55 Make this a free-standing function in the anon nam
Tomasz Moniuszko 2014/06/11 13:11:56 This method calls PlatformFontWin::CreateHFontRef
265 base::win::ScopedGetDC screen_dc(NULL);
266 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT);
267
268 base::win::ScopedGDIObject<HFONT> best_font(base_font);
269 TEXTMETRIC best_font_metrics;
270 {
271 base::win::ScopedSelectObject scoped_font(screen_dc, best_font);
272 GetTextMetrics(screen_dc, &best_font_metrics);
Alexei Svitkine (slow) 2014/06/02 15:42:55 Nit: Make a helper function in the anon namespace
Tomasz Moniuszko 2014/06/11 13:11:55 Done.
273 }
274
275 LOGFONT font_info;
276 GetObject(base_font, sizeof(LOGFONT), &font_info);
277
278 do {
279 font_info.lfHeight =
280 -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading + 1);
Alexei Svitkine (slow) 2014/06/02 15:42:55 Nit: Add a comment explaining this logic.
Tomasz Moniuszko 2014/06/11 13:11:55 Done.
281 base::win::ScopedGDIObject<HFONT> font(CreateFontIndirect(&font_info));
282 TEXTMETRIC font_metrics;
283 {
284 base::win::ScopedSelectObject scoped_font(screen_dc, font);
285 GetTextMetrics(screen_dc, &font_metrics);
286 }
287 if (font_metrics.tmHeight > best_font_metrics.tmHeight) {
Alexei Svitkine (slow) 2014/06/02 15:42:55 Nit: No {}'s.
Tomasz Moniuszko 2014/06/11 13:11:56 Done.
288 break;
289 }
290 best_font.Set(font.release());
291 best_font_metrics = font_metrics;
292 } while (true);
293
294 return Font(new PlatformFontWin(
295 CreateHFontRef(best_font.release())));
Alexei Svitkine (slow) 2014/06/02 15:42:55 Nit: Does this fit on the previous line?
Tomasz Moniuszko 2014/06/11 13:11:56 Done.
296 }
297
257 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { 298 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
258 } 299 }
259 300
260 //////////////////////////////////////////////////////////////////////////////// 301 ////////////////////////////////////////////////////////////////////////////////
261 // PlatformFontWin::HFontRef: 302 // PlatformFontWin::HFontRef:
262 303
263 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, 304 PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
264 int font_size, 305 int font_size,
265 int height, 306 int height,
266 int baseline, 307 int baseline,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 return new PlatformFontWin(native_font); 363 return new PlatformFontWin(native_font);
323 } 364 }
324 365
325 // static 366 // static
326 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 367 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
327 int font_size) { 368 int font_size) {
328 return new PlatformFontWin(font_name, font_size); 369 return new PlatformFontWin(font_name, font_size);
329 } 370 }
330 371
331 } // namespace gfx 372 } // 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