Chromium Code Reviews| 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/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/win/scoped_comptr.h" | |
| 18 #include "base/win/scoped_gdi_object.h" | 19 #include "base/win/scoped_gdi_object.h" |
| 19 #include "base/win/scoped_hdc.h" | 20 #include "base/win/scoped_hdc.h" |
| 20 #include "base/win/scoped_select_object.h" | 21 #include "base/win/scoped_select_object.h" |
| 21 #include "base/win/win_util.h" | 22 #include "base/win/win_util.h" |
| 22 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/font.h" | 24 #include "ui/gfx/font.h" |
| 24 #include "ui/gfx/font_render_params.h" | 25 #include "ui/gfx/font_render_params.h" |
| 25 #include "ui/gfx/win/scoped_set_map_mode.h" | 26 #include "ui/gfx/win/scoped_set_map_mode.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 | 75 |
| 75 // static | 76 // static |
| 76 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; | 77 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; |
| 77 | 78 |
| 78 // static | 79 // static |
| 79 PlatformFontWin::AdjustFontCallback | 80 PlatformFontWin::AdjustFontCallback |
| 80 PlatformFontWin::adjust_font_callback = NULL; | 81 PlatformFontWin::adjust_font_callback = NULL; |
| 81 PlatformFontWin::GetMinimumFontSizeCallback | 82 PlatformFontWin::GetMinimumFontSizeCallback |
| 82 PlatformFontWin::get_minimum_font_size_callback = NULL; | 83 PlatformFontWin::get_minimum_font_size_callback = NULL; |
| 83 | 84 |
| 85 IDWriteFactory* PlatformFontWin::direct_write_factory_ = NULL; | |
| 86 | |
| 84 //////////////////////////////////////////////////////////////////////////////// | 87 //////////////////////////////////////////////////////////////////////////////// |
| 85 // PlatformFontWin, public | 88 // PlatformFontWin, public |
| 86 | 89 |
| 87 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { | 90 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { |
| 88 } | 91 } |
| 89 | 92 |
| 90 PlatformFontWin::PlatformFontWin(NativeFont native_font) { | 93 PlatformFontWin::PlatformFontWin(NativeFont native_font) { |
| 91 InitWithCopyOfHFONT(native_font); | 94 InitWithCopyOfHFONT(native_font); |
| 92 } | 95 } |
| 93 | 96 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 base_font_ref_ = PlatformFontWin::CreateHFontRef(font); | 243 base_font_ref_ = PlatformFontWin::CreateHFontRef(font); |
| 241 // base_font_ref_ is global, up the ref count so it's never deleted. | 244 // base_font_ref_ is global, up the ref count so it's never deleted. |
| 242 base_font_ref_->AddRef(); | 245 base_font_ref_->AddRef(); |
| 243 } | 246 } |
| 244 return base_font_ref_; | 247 return base_font_ref_; |
| 245 } | 248 } |
| 246 | 249 |
| 247 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { | 250 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { |
| 248 TEXTMETRIC font_metrics; | 251 TEXTMETRIC font_metrics; |
| 249 | 252 |
| 253 if (direct_write_factory_) { | |
| 254 base::win::ScopedGDIObject<HFONT> gdi_font(font); | |
| 255 font = ConvertGDIFontToDirectWriteFont(gdi_font); | |
| 256 } | |
| 257 | |
| 250 { | 258 { |
| 251 base::win::ScopedGetDC screen_dc(NULL); | 259 base::win::ScopedGetDC screen_dc(NULL); |
| 252 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); | 260 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
| 253 GetTextMetricsForFont(screen_dc, font, &font_metrics); | 261 GetTextMetricsForFont(screen_dc, font, &font_metrics); |
| 254 } | 262 } |
| 255 | 263 |
| 256 return CreateHFontRef(font, font_metrics); | 264 return CreateHFontRef(font, font_metrics); |
| 257 } | 265 } |
| 258 | 266 |
| 259 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef( | 267 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 GetTextMetricsForFont(screen_dc, font, &font_metrics); | 310 GetTextMetricsForFont(screen_dc, font, &font_metrics); |
| 303 if (font_metrics.tmHeight > best_font_metrics.tmHeight) | 311 if (font_metrics.tmHeight > best_font_metrics.tmHeight) |
| 304 break; | 312 break; |
| 305 best_font.Set(font.release()); | 313 best_font.Set(font.release()); |
| 306 best_font_metrics = font_metrics; | 314 best_font_metrics = font_metrics; |
| 307 } while (true); | 315 } while (true); |
| 308 | 316 |
| 309 return Font(new PlatformFontWin(CreateHFontRef(best_font.release()))); | 317 return Font(new PlatformFontWin(CreateHFontRef(best_font.release()))); |
| 310 } | 318 } |
| 311 | 319 |
| 320 // static | |
| 321 HFONT PlatformFontWin::ConvertGDIFontToDirectWriteFont(HFONT gdi_font) { | |
|
scottmg
2014/10/29 21:33:32
seems a bit weird to me that that round trip actua
ananta
2014/10/29 21:56:18
Yes. I had a hunch though :)
| |
| 322 // This function uses the DirectWrite Gdi interop interfaces to convert the | |
| 323 // gdi font passed in to a HFONT which is compatible with DirectWrite font | |
| 324 // metrics which could be different from GDI font metrics. This ensures that | |
| 325 // widgets like labels which use font metrics to calculate bounds have the | |
| 326 // same calculations as skia which uses DirectWrite. | |
| 327 DCHECK(direct_write_factory_); | |
| 328 base::win::ScopedComPtr<IDWriteGdiInterop> gdi_interop; | |
| 329 HRESULT hr = direct_write_factory_->GetGdiInterop(gdi_interop.Receive()); | |
| 330 if (FAILED(hr)) { | |
| 331 CHECK(false); | |
|
scottmg
2014/10/29 21:33:32
check(false); return NULL; doesn't make any sense,
ananta
2014/10/29 21:56:18
Added the returns for semantics only.
| |
| 332 return NULL; | |
| 333 } | |
| 334 | |
| 335 base::win::ScopedGetDC screen_dc(NULL); | |
| 336 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); | |
| 337 | |
| 338 base::win::ScopedSelectObject scoped_font(screen_dc, gdi_font); | |
| 339 | |
| 340 base::win::ScopedComPtr<IDWriteFontFace> font_face_gdi; | |
| 341 hr = gdi_interop->CreateFontFaceFromHdc(screen_dc, font_face_gdi.Receive()); | |
| 342 if (FAILED(hr)) { | |
| 343 CHECK(false); | |
| 344 return NULL; | |
| 345 } | |
| 346 | |
| 347 LOGFONT dwrite_to_gdi_log_font = {0}; | |
| 348 hr = gdi_interop->ConvertFontFaceToLOGFONT(font_face_gdi, | |
| 349 &dwrite_to_gdi_log_font); | |
| 350 if (FAILED(hr)) { | |
| 351 CHECK(false); | |
| 352 return NULL; | |
| 353 } | |
| 354 return CreateFontIndirect(&dwrite_to_gdi_log_font); | |
| 355 } | |
| 356 | |
| 312 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { | 357 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { |
| 313 } | 358 } |
| 314 | 359 |
| 315 //////////////////////////////////////////////////////////////////////////////// | 360 //////////////////////////////////////////////////////////////////////////////// |
| 316 // PlatformFontWin::HFontRef: | 361 // PlatformFontWin::HFontRef: |
| 317 | 362 |
| 318 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, | 363 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, |
| 319 int font_size, | 364 int font_size, |
| 320 int height, | 365 int height, |
| 321 int baseline, | 366 int baseline, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 return new PlatformFontWin(native_font); | 422 return new PlatformFontWin(native_font); |
| 378 } | 423 } |
| 379 | 424 |
| 380 // static | 425 // static |
| 381 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 426 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 382 int font_size) { | 427 int font_size) { |
| 383 return new PlatformFontWin(font_name, font_size); | 428 return new PlatformFontWin(font_name, font_size); |
| 384 } | 429 } |
| 385 | 430 |
| 386 } // namespace gfx | 431 } // namespace gfx |
| OLD | NEW |