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
3 // found in the LICENSE file.
5 #include "ui/gfx/win/direct_write.h"
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/win/registry.h"
13 #include "base/win/scoped_comptr.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"
18 #include "ui/gfx/switches.h"
19 #include "ui/gfx/win/dpi.h"
26 static bool dwrite_enabled
= false;
30 bool ShouldUseDirectWrite() {
31 // If the flag is currently on, and we're on Win7 or above, we enable
32 // DirectWrite. Skia does not require the additions to DirectWrite in QFE
33 // 2670838, but a simple 'better than XP' check is not enough.
34 if (base::win::GetVersion() < base::win::VERSION_WIN7
)
37 base::win::OSInfo::VersionNumber os_version
=
38 base::win::OSInfo::GetInstance()->version_number();
39 if ((os_version
.major
== 6) && (os_version
.minor
== 1)) {
40 // We can't use DirectWrite for pre-release versions of Windows 7.
41 if (os_version
.build
< 7600)
44 // If forced off, don't use it.
45 const base::CommandLine
& command_line
=
46 *base::CommandLine::ForCurrentProcess();
47 if (command_line
.HasSwitch(switches::kDisableDirectWrite
))
50 // Can't use GDI on HiDPI.
51 if (gfx::GetDPIScale() > 1.0f
)
54 // Otherwise, check the field trial.
55 const std::string group_name
=
56 base::FieldTrialList::FindFullName("DirectWrite");
57 return group_name
!= "Disabled";
60 void MaybeInitializeDirectWrite() {
61 static bool tried_dwrite_initialize
= false;
62 if (tried_dwrite_initialize
)
64 tried_dwrite_initialize
= true;
66 if (!ShouldUseDirectWrite() ||
67 base::CommandLine::ForCurrentProcess()->HasSwitch(
68 switches::kDisableDirectWriteForUI
) ||
69 base::CommandLine::ForCurrentProcess()->HasSwitch(
70 switches::kDisableHarfBuzzRenderText
)) {
74 using DWriteCreateFactoryProc
= decltype(DWriteCreateFactory
)*;
75 HMODULE dwrite_dll
= LoadLibraryW(L
"dwrite.dll");
79 DWriteCreateFactoryProc dwrite_create_factory_proc
=
80 reinterpret_cast<DWriteCreateFactoryProc
>(
81 GetProcAddress(dwrite_dll
, "DWriteCreateFactory"));
82 // Not finding the DWriteCreateFactory function indicates a corrupt dll.
83 if (!dwrite_create_factory_proc
)
86 base::win::ScopedComPtr
<IDWriteFactory
> factory
;
88 // Failure to create the DirectWrite factory indicates a corrupt dll.
89 if (FAILED(dwrite_create_factory_proc(
90 DWRITE_FACTORY_TYPE_SHARED
,
91 __uuidof(IDWriteFactory
),
92 reinterpret_cast<IUnknown
**>(factory
.Receive()))))
95 // The skia call to create a new DirectWrite font manager instance can fail
96 // if we are unable to get the system font collection from the DirectWrite
97 // factory. The GetSystemFontCollection method in the IDWriteFactory
98 // interface fails with E_INVALIDARG on certain Windows 7 gold versions
99 // (6.1.7600.*). We should just use GDI in these cases.
100 SkFontMgr
* direct_write_font_mgr
= SkFontMgr_New_DirectWrite(factory
.get());
101 if (!direct_write_font_mgr
)
103 dwrite_enabled
= true;
104 SetDefaultSkiaFactory(direct_write_font_mgr
);
105 gfx::PlatformFontWin::SetDirectWriteFactory(factory
.get());
108 bool IsDirectWriteEnabled() {
109 return dwrite_enabled
;