OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/win/direct_write.h" | 5 #include "ui/gfx/win/direct_write.h" |
6 | 6 |
| 7 #include <dwrite.h> |
| 8 |
7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
9 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
10 #include "base/win/registry.h" | 12 #include "base/win/registry.h" |
| 13 #include "base/win/scoped_comptr.h" |
11 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
| 15 #include "skia/ext/fontmgr_default_win.h" |
| 16 #include "third_party/skia/include/ports/SkTypeface_win.h" |
| 17 #include "ui/gfx/platform_font_win.h" |
12 #include "ui/gfx/switches.h" | 18 #include "ui/gfx/switches.h" |
13 #include "ui/gfx/win/dpi.h" | 19 #include "ui/gfx/win/dpi.h" |
14 | 20 |
15 namespace gfx { | 21 namespace gfx { |
16 namespace win { | 22 namespace win { |
17 | 23 |
18 bool ShouldUseDirectWrite() { | 24 bool ShouldUseDirectWrite() { |
19 // If the flag is currently on, and we're on Win7 or above, we enable | 25 // If the flag is currently on, and we're on Win7 or above, we enable |
20 // DirectWrite. Skia does not require the additions to DirectWrite in QFE | 26 // DirectWrite. Skia does not require the additions to DirectWrite in QFE |
21 // 2670838, but a simple 'better than XP' check is not enough. | 27 // 2670838, but a simple 'better than XP' check is not enough. |
(...skipping 30 matching lines...) Expand all Loading... |
52 const DWORD kMaxAllowedFontsBeforeFallbackToGDI = 1750; | 58 const DWORD kMaxAllowedFontsBeforeFallbackToGDI = 1750; |
53 if (reg_iterator.ValueCount() >= kMaxAllowedFontsBeforeFallbackToGDI) | 59 if (reg_iterator.ValueCount() >= kMaxAllowedFontsBeforeFallbackToGDI) |
54 return false; | 60 return false; |
55 | 61 |
56 // Otherwise, check the field trial. | 62 // Otherwise, check the field trial. |
57 const std::string group_name = | 63 const std::string group_name = |
58 base::FieldTrialList::FindFullName("DirectWrite"); | 64 base::FieldTrialList::FindFullName("DirectWrite"); |
59 return group_name != "Disabled"; | 65 return group_name != "Disabled"; |
60 } | 66 } |
61 | 67 |
| 68 void MaybeInitializeDirectWrite() { |
| 69 static bool tried_dwrite_initialize = false; |
| 70 if (tried_dwrite_initialize) |
| 71 return; |
| 72 tried_dwrite_initialize = true; |
| 73 |
| 74 if (!ShouldUseDirectWrite() || |
| 75 CommandLine::ForCurrentProcess()->HasSwitch( |
| 76 switches::kDisableDirectWriteForUI) || |
| 77 CommandLine::ForCurrentProcess()->HasSwitch( |
| 78 switches::kDisableHarfBuzzRenderText)) { |
| 79 return; |
| 80 } |
| 81 |
| 82 using DWriteCreateFactoryProc = decltype(DWriteCreateFactory)*; |
| 83 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll"); |
| 84 if (!dwrite_dll) |
| 85 return; |
| 86 |
| 87 DWriteCreateFactoryProc dwrite_create_factory_proc = |
| 88 reinterpret_cast<DWriteCreateFactoryProc>( |
| 89 GetProcAddress(dwrite_dll, "DWriteCreateFactory")); |
| 90 // Not finding the DWriteCreateFactory function indicates a corrupt dll. |
| 91 CHECK(dwrite_create_factory_proc); |
| 92 |
| 93 base::win::ScopedComPtr<IDWriteFactory> factory; |
| 94 |
| 95 CHECK(SUCCEEDED( |
| 96 dwrite_create_factory_proc( |
| 97 DWRITE_FACTORY_TYPE_SHARED, |
| 98 __uuidof(IDWriteFactory), |
| 99 reinterpret_cast<IUnknown**>(factory.Receive())))); |
| 100 // The skia call to create a new DirectWrite font manager instance can fail |
| 101 // if we are unable to get the system font collection from the DirectWrite |
| 102 // factory. The GetSystemFontCollection method in the IDWriteFactory |
| 103 // interface fails with E_INVALIDARG on certain Windows 7 gold versions |
| 104 // (6.1.7600.*). We should just use GDI in these cases. |
| 105 SkFontMgr* direct_write_font_mgr = SkFontMgr_New_DirectWrite(factory.get()); |
| 106 if (direct_write_font_mgr) { |
| 107 SetDefaultSkiaFactory(direct_write_font_mgr); |
| 108 gfx::PlatformFontWin::SetDirectWriteFactory(factory.get()); |
| 109 } |
| 110 } |
| 111 |
62 } // namespace win | 112 } // namespace win |
63 } // namespace gfx | 113 } // namespace gfx |
OLD | NEW |