OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/font_list.h" | 5 #include "content/common/font_list.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <dwrite.h> | |
9 | 8 |
10 #include <set> | 9 #include <set> |
11 | 10 |
12 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
13 #include "base/values.h" | 12 #include "base/values.h" |
14 #include "base/win/scoped_comptr.h" | |
15 #include "content/common/sandbox_win.h" | |
16 | 13 |
17 namespace content { | 14 namespace content { |
18 | 15 |
19 static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEXW* logical_font, | 16 static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEXW* logical_font, |
20 NEWTEXTMETRICEXW* physical_font, | 17 NEWTEXTMETRICEXW* physical_font, |
21 DWORD font_type, | 18 DWORD font_type, |
22 LPARAM lparam) { | 19 LPARAM lparam) { |
23 std::set<base::string16>* font_names = | 20 std::set<base::string16>* font_names = |
24 reinterpret_cast<std::set<base::string16>*>(lparam); | 21 reinterpret_cast<std::set<base::string16>*>(lparam); |
25 if (font_names) { | 22 if (font_names) { |
26 const LOGFONTW& lf = logical_font->elfLogFont; | 23 const LOGFONTW& lf = logical_font->elfLogFont; |
27 if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@') { | 24 if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@') { |
28 base::string16 face_name(lf.lfFaceName); | 25 base::string16 face_name(lf.lfFaceName); |
29 font_names->insert(face_name); | 26 font_names->insert(face_name); |
30 } | 27 } |
31 } | 28 } |
32 return 1; | 29 return 1; |
33 } | 30 } |
34 | 31 |
35 static void GetFontListInternal_GDI(base::ListValue* font_list) { | 32 scoped_ptr<base::ListValue> GetFontList_SlowBlocking() { |
36 std::set<base::string16> font_names; | 33 std::set<base::string16> font_names; |
37 | 34 |
38 LOGFONTW logfont; | 35 LOGFONTW logfont; |
39 memset(&logfont, 0, sizeof(logfont)); | 36 memset(&logfont, 0, sizeof(logfont)); |
40 logfont.lfCharSet = DEFAULT_CHARSET; | 37 logfont.lfCharSet = DEFAULT_CHARSET; |
41 | 38 |
42 HDC hdc = ::GetDC(NULL); | 39 HDC hdc = ::GetDC(NULL); |
43 ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamExProc, | 40 ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamExProc, |
44 (LPARAM)&font_names, 0); | 41 (LPARAM)&font_names, 0); |
45 ::ReleaseDC(NULL, hdc); | 42 ::ReleaseDC(NULL, hdc); |
46 | 43 |
| 44 scoped_ptr<base::ListValue> font_list(new base::ListValue); |
47 std::set<base::string16>::iterator iter; | 45 std::set<base::string16>::iterator iter; |
48 for (iter = font_names.begin(); iter != font_names.end(); ++iter) { | 46 for (iter = font_names.begin(); iter != font_names.end(); ++iter) { |
49 base::ListValue* font_item = new base::ListValue(); | 47 base::ListValue* font_item = new base::ListValue(); |
50 font_item->Append(new base::StringValue(*iter)); | 48 font_item->Append(new base::StringValue(*iter)); |
51 font_item->Append(new base::StringValue(*iter)); | 49 font_item->Append(new base::StringValue(*iter)); |
52 font_list->Append(font_item); | 50 font_list->Append(font_item); |
53 } | 51 } |
54 } | |
55 | |
56 static void AddLocalizedFontFamily(base::ListValue* font_list, | |
57 IDWriteFontFamily* font_family, | |
58 wchar_t* user_locale) { | |
59 base::win::ScopedComPtr<IDWriteLocalizedStrings> family_names; | |
60 if (SUCCEEDED(font_family->GetFamilyNames(family_names.Receive()))) { | |
61 UINT32 index = 0; | |
62 BOOL exists = false; | |
63 | |
64 // Try to get name of font in the users locale first, failing that fall back | |
65 // on US English and then the first name listed. | |
66 family_names->FindLocaleName(user_locale, &index, &exists); | |
67 if (!exists) | |
68 family_names->FindLocaleName(L"en-us", &index, &exists); | |
69 if (!exists) | |
70 index = 0; | |
71 | |
72 UINT32 len = 0; | |
73 if (SUCCEEDED(family_names->GetStringLength(index, &len))) { | |
74 wchar_t* name = new wchar_t[len + 1]; | |
75 if (name) { | |
76 if (SUCCEEDED(family_names->GetString(index, name, len + 1)) && | |
77 name[0] != '@') { | |
78 base::ListValue* font_item = new base::ListValue(); | |
79 // First field is the name displayed to the user, second is the name | |
80 // used internally and passed to the font system. For fonts the | |
81 // localized name is used for both. | |
82 font_item->Append(new base::StringValue(name)); | |
83 font_item->Append(new base::StringValue(name)); | |
84 font_list->Append(font_item); | |
85 } | |
86 delete[] name; | |
87 } | |
88 } | |
89 } | |
90 } | |
91 | |
92 static base::win::ScopedComPtr<IDWriteFactory> CreateDirectWriteFactory() { | |
93 base::win::ScopedComPtr<IDWriteFactory> factory; | |
94 typedef decltype(DWriteCreateFactory) * DWriteCreateFactoryProc; | |
95 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll"); | |
96 if (dwrite_dll) { | |
97 DWriteCreateFactoryProc dwrite_create_factory_proc = | |
98 reinterpret_cast<DWriteCreateFactoryProc>( | |
99 GetProcAddress(dwrite_dll, "DWriteCreateFactory")); | |
100 if (dwrite_create_factory_proc) { | |
101 dwrite_create_factory_proc( | |
102 DWRITE_FACTORY_TYPE_ISOLATED, | |
103 __uuidof(IDWriteFactory), | |
104 reinterpret_cast<IUnknown**>(factory.Receive())); | |
105 } | |
106 } | |
107 return factory; | |
108 } | |
109 | |
110 static void GetFontListInternal_DirectWrite(base::ListValue* font_list) { | |
111 wchar_t user_locale[LOCALE_NAME_MAX_LENGTH]; | |
112 GetUserDefaultLocaleName(user_locale, LOCALE_NAME_MAX_LENGTH); | |
113 | |
114 base::win::ScopedComPtr<IDWriteFactory> factory = CreateDirectWriteFactory(); | |
115 | |
116 base::win::ScopedComPtr<IDWriteFontCollection> font_collection; | |
117 BOOL check_for_updates = false; | |
118 if (factory && SUCCEEDED(factory->GetSystemFontCollection( | |
119 font_collection.Receive(), check_for_updates))) { | |
120 UINT32 family_count = font_collection->GetFontFamilyCount(); | |
121 for (UINT32 i = 0; i < family_count; i++) { | |
122 base::win::ScopedComPtr<IDWriteFontFamily> font_family; | |
123 if (SUCCEEDED(font_collection->GetFontFamily(i, font_family.Receive()))) | |
124 AddLocalizedFontFamily(font_list, font_family, user_locale); | |
125 } | |
126 } | |
127 } | |
128 | |
129 scoped_ptr<base::ListValue> GetFontList_SlowBlocking() { | |
130 scoped_ptr<base::ListValue> font_list(new base::ListValue); | |
131 | |
132 if (ShouldUseDirectWrite()) | |
133 GetFontListInternal_DirectWrite(font_list.get()); | |
134 | |
135 // Fallback on GDI if DirectWrite isn't enabled or if it failed to populate | |
136 // the font list. | |
137 if (font_list->GetSize() == 0) | |
138 GetFontListInternal_GDI(font_list.get()); | |
139 | |
140 return font_list.Pass(); | 52 return font_list.Pass(); |
141 } | 53 } |
142 | 54 |
143 } // namespace content | 55 } // namespace content |
OLD | NEW |