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> | |
8 | 9 |
9 #include <set> | 10 #include <set> |
10 | 11 |
12 #include "base/debug/alias.h" | |
13 #include "base/logging.h" | |
11 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
12 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "content/common/sandbox_win.h" | |
17 #include "content/public/renderer/render_font_warmup_win.h" | |
13 | 18 |
14 namespace content { | 19 namespace content { |
15 | 20 |
16 static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEXW* logical_font, | 21 static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEXW* logical_font, |
17 NEWTEXTMETRICEXW* physical_font, | 22 NEWTEXTMETRICEXW* physical_font, |
18 DWORD font_type, | 23 DWORD font_type, |
19 LPARAM lparam) { | 24 LPARAM lparam) { |
20 std::set<base::string16>* font_names = | 25 std::set<base::string16>* font_names = |
21 reinterpret_cast<std::set<base::string16>*>(lparam); | 26 reinterpret_cast<std::set<base::string16>*>(lparam); |
22 if (font_names) { | 27 if (font_names) { |
23 const LOGFONTW& lf = logical_font->elfLogFont; | 28 const LOGFONTW& lf = logical_font->elfLogFont; |
24 if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@') { | 29 if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@') { |
25 base::string16 face_name(lf.lfFaceName); | 30 base::string16 face_name(lf.lfFaceName); |
26 font_names->insert(face_name); | 31 font_names->insert(face_name); |
27 } | 32 } |
28 } | 33 } |
29 return 1; | 34 return 1; |
30 } | 35 } |
31 | 36 |
32 scoped_ptr<base::ListValue> GetFontList_SlowBlocking() { | 37 static inline void GetFontListInternal_GDI( |
38 scoped_ptr<base::ListValue>& font_list) { | |
33 std::set<base::string16> font_names; | 39 std::set<base::string16> font_names; |
34 | 40 |
35 LOGFONTW logfont; | 41 LOGFONTW logfont; |
36 memset(&logfont, 0, sizeof(logfont)); | 42 memset(&logfont, 0, sizeof(logfont)); |
37 logfont.lfCharSet = DEFAULT_CHARSET; | 43 logfont.lfCharSet = DEFAULT_CHARSET; |
38 | 44 |
39 HDC hdc = ::GetDC(NULL); | 45 HDC hdc = ::GetDC(NULL); |
40 ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamExProc, | 46 ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamExProc, |
41 (LPARAM)&font_names, 0); | 47 (LPARAM)&font_names, 0); |
42 ::ReleaseDC(NULL, hdc); | 48 ::ReleaseDC(NULL, hdc); |
43 | 49 |
44 scoped_ptr<base::ListValue> font_list(new base::ListValue); | |
45 std::set<base::string16>::iterator iter; | 50 std::set<base::string16>::iterator iter; |
46 for (iter = font_names.begin(); iter != font_names.end(); ++iter) { | 51 for (iter = font_names.begin(); iter != font_names.end(); ++iter) { |
47 base::ListValue* font_item = new base::ListValue(); | 52 base::ListValue* font_item = new base::ListValue(); |
48 font_item->Append(new base::StringValue(*iter)); | 53 font_item->Append(new base::StringValue(*iter)); |
49 font_item->Append(new base::StringValue(*iter)); | 54 font_item->Append(new base::StringValue(*iter)); |
50 font_list->Append(font_item); | 55 font_list->Append(font_item); |
51 } | 56 } |
57 } | |
58 | |
59 static inline void AddLocalizedFontFamily( | |
scottmg
2014/06/26 20:13:38
nit; no 'inline's necessary
| |
60 scoped_ptr<base::ListValue>& font_list, | |
61 IDWriteFontFamily* font_family, | |
62 wchar_t* user_locale) { | |
63 IDWriteLocalizedStrings* family_names = NULL; | |
64 if (SUCCEEDED(font_family->GetFamilyNames(&family_names))) { | |
65 UINT32 index = 0; | |
66 BOOL exists = false; | |
67 | |
68 family_names->FindLocaleName(user_locale, &index, &exists); | |
69 if (!exists) { | |
70 family_names->FindLocaleName(L"en-us", &index, &exists); | |
71 } | |
72 if (!exists) { | |
73 index = 0; | |
74 } | |
75 | |
76 UINT32 len = 0; | |
77 if (SUCCEEDED(family_names->GetStringLength(index, &len))) { | |
78 wchar_t* name = new wchar_t[len + 1]; | |
79 if (name) { | |
80 if (SUCCEEDED(family_names->GetString(index, name, len + 1))) { | |
81 base::ListValue* font_item = new base::ListValue(); | |
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 if (family_names) { | |
91 family_names->Release(); | |
92 } | |
93 } | |
94 | |
95 static inline void GetFontListInternal_DirectWrite( | |
96 scoped_ptr<base::ListValue>& font_list) { | |
97 wchar_t user_locale[LOCALE_NAME_MAX_LENGTH]; | |
98 GetUserDefaultLocaleName(user_locale, LOCALE_NAME_MAX_LENGTH); | |
99 | |
100 IDWriteFactory* factory = NULL; | |
101 CreateDirectWriteFactory(&factory); | |
102 | |
103 IDWriteFontCollection* font_collection; | |
104 BOOL checkForUpdates = false; | |
105 if (SUCCEEDED(factory->GetSystemFontCollection(&font_collection, | |
106 checkForUpdates))) { | |
107 UINT32 family_count = font_collection->GetFontFamilyCount(); | |
108 for (UINT32 i = 0; i < family_count; i++) { | |
109 IDWriteFontFamily* font_family = NULL; | |
110 if (SUCCEEDED(font_collection->GetFontFamily(i, &font_family))) { | |
111 AddLocalizedFontFamily(font_list, font_family, user_locale); | |
scottmg
2014/06/26 20:13:38
I'm not sure how this changes the actual names ret
eae
2014/06/26 20:58:24
For fonts with multiple names (e.x. SimSun which i
scottmg
2014/06/26 21:05:17
OK, I just noticed e.g. in https://src.chromium.or
| |
112 } | |
113 if (font_family) { | |
114 font_family->Release(); | |
115 } | |
116 } | |
117 } | |
118 if (font_collection) { | |
119 font_collection->Release(); | |
120 } | |
121 if (factory) { | |
122 factory->Release(); | |
123 } | |
124 } | |
125 | |
126 scoped_ptr<base::ListValue> GetFontList_SlowBlocking() { | |
scottmg
2014/06/26 20:13:38
I'm a little unclear on where this happens. If it'
eae
2014/06/26 20:58:24
Yeah, the way I get the factory from the sandbox c
| |
127 scoped_ptr<base::ListValue> font_list(new base::ListValue); | |
128 if (ShouldUseDirectWrite()) { | |
129 GetFontListInternal_DirectWrite(font_list); | |
130 } | |
131 if (font_list->GetSize() < 1) { | |
132 GetFontListInternal_GDI(font_list); | |
133 } | |
52 return font_list.Pass(); | 134 return font_list.Pass(); |
53 } | 135 } |
54 | 136 |
55 } // namespace content | 137 } // namespace content |
OLD | NEW |