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 "content/renderer/renderer_font_platform_win.h" | 5 #include "content/renderer/renderer_font_platform_win.h" |
6 | 6 |
7 #include <dwrite.h> | 7 #include <dwrite.h> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 #include <wrl/implements.h> | 10 #include <wrl/implements.h> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 UINT32 key_size, | 42 UINT32 key_size, |
43 IDWriteFontFileEnumerator** file_enumerator); | 43 IDWriteFontFileEnumerator** file_enumerator); |
44 | 44 |
45 static HRESULT Initialize(IDWriteFactory* factory); | 45 static HRESULT Initialize(IDWriteFactory* factory); |
46 | 46 |
47 UINT32 GetFontMapSize(); | 47 UINT32 GetFontMapSize(); |
48 | 48 |
49 std::wstring GetFontNameFromKey(UINT32 idx); | 49 std::wstring GetFontNameFromKey(UINT32 idx); |
50 | 50 |
51 bool LoadFontListFromRegistry(); | 51 bool LoadFontListFromRegistry(); |
52 bool LoadRestrictedFontList(); | |
52 | 53 |
53 FontCollectionLoader() {}; | 54 FontCollectionLoader() {}; |
54 virtual ~FontCollectionLoader() {}; | 55 virtual ~FontCollectionLoader() {}; |
55 | 56 |
56 private: | 57 private: |
57 mswr::ComPtr<IDWriteFontFileLoader> file_loader_; | 58 mswr::ComPtr<IDWriteFontFileLoader> file_loader_; |
58 | 59 |
59 std::vector<std::wstring> reg_fonts_; | 60 std::vector<std::wstring> reg_fonts_; |
60 }; | 61 }; |
61 | 62 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 std::vector<base::FilePath::StringType> components; | 277 std::vector<base::FilePath::StringType> components; |
277 path.GetComponents(&components); | 278 path.GetComponents(&components); |
278 if (components.size() == 1) { | 279 if (components.size() == 1) { |
279 reg_fonts_.push_back(value.c_str()); | 280 reg_fonts_.push_back(value.c_str()); |
280 } | 281 } |
281 } | 282 } |
282 } | 283 } |
283 return true; | 284 return true; |
284 } | 285 } |
285 | 286 |
287 // This list is mainly based on prefs/prefs_tab_helper.cc kFontDefaults. | |
288 const wchar_t* kRestrictedFontSet[] = { | |
289 L"times.ttf", // IDS_STANDARD_FONT_FAMILY | |
290 L"timesbd.ttf", // IDS_STANDARD_FONT_FAMILY | |
291 L"timesbi.ttf", // IDS_STANDARD_FONT_FAMILY | |
292 L"timesi.ttf", // IDS_STANDARD_FONT_FAMILY | |
293 L"cour.ttf", // IDS_FIXED_FONT_FAMILY | |
294 L"courbd.ttf", // IDS_FIXED_FONT_FAMILY | |
295 L"courbi.ttf", // IDS_FIXED_FONT_FAMILY | |
296 L"couri.ttf", // IDS_FIXED_FONT_FAMILY | |
297 L"consola.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN | |
298 L"consolab.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN | |
299 L"consolai.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN | |
300 L"consolaz.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN | |
301 L"arial.ttf", // IDS_SANS_SERIF_FONT_FAMILY | |
302 L"arialbd.ttf", // IDS_SANS_SERIF_FONT_FAMILY | |
303 L"arialbi.ttf", // IDS_SANS_SERIF_FONT_FAMILY | |
304 L"ariali.ttf", // IDS_SANS_SERIF_FONT_FAMILY | |
305 L"comic.ttf", // IDS_CURSIVE_FONT_FAMILY | |
306 L"comicbd.ttf", // IDS_CURSIVE_FONT_FAMILY | |
307 L"comici.ttf", // IDS_CURSIVE_FONT_FAMILY | |
308 L"comicz.ttf", // IDS_CURSIVE_FONT_FAMILY | |
309 L"impact.ttf", // IDS_FANTASY_FONT_FAMILY | |
310 L"segoeui.ttf", // IDS_PICTOGRAPH_FONT_FAMILY | |
311 L"segoeuib.ttf", // IDS_PICTOGRAPH_FONT_FAMILY | |
312 L"segoeuii.ttf", // IDS_PICTOGRAPH_FONT_FAMILY | |
313 L"msgothic.ttc", // IDS_STANDARD_FONT_FAMILY_JAPANESE | |
314 L"msmincho.ttc", // IDS_SERIF_FONT_FAMILY_JAPANESE | |
315 L"gulim.ttc", // IDS_FIXED_FONT_FAMILY_KOREAN | |
316 L"batang.ttc", // IDS_SERIF_FONT_FAMILY_KOREAN | |
317 L"simsun.ttc", // IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN | |
318 L"mingliu.ttc", // IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN | |
319 }; | |
320 | |
321 bool FontCollectionLoader::LoadRestrictedFontList() { | |
322 reg_fonts_.clear(); | |
323 reg_fonts_.assign(kRestrictedFontSet, | |
324 kRestrictedFontSet + _countof(kRestrictedFontSet)); | |
325 return true; | |
scottmg
2014/08/29 02:51:05
no need for return value here
| |
326 } | |
327 | |
286 } // namespace | 328 } // namespace |
287 | 329 |
288 namespace content { | 330 namespace content { |
289 | 331 |
290 mswr::ComPtr<IDWriteFontCollection> g_font_collection; | 332 mswr::ComPtr<IDWriteFontCollection> g_font_collection; |
291 | 333 |
292 IDWriteFontCollection* GetCustomFontCollection(IDWriteFactory* factory) { | 334 IDWriteFontCollection* GetCustomFontCollection(IDWriteFactory* factory) { |
293 if (g_font_collection.Get() != NULL) | 335 if (g_font_collection.Get() != NULL) |
294 return g_font_collection.Get(); | 336 return g_font_collection.Get(); |
295 | 337 |
296 base::TimeTicks start_tick = base::TimeTicks::Now(); | 338 base::TimeTicks start_tick = base::TimeTicks::Now(); |
297 | 339 |
298 FontCollectionLoader::Initialize(factory); | 340 FontCollectionLoader::Initialize(factory); |
299 | 341 |
300 HRESULT hr = factory->CreateCustomFontCollection( | 342 // We try here to put arbitrary limit on max number of fonts that could |
301 g_font_loader.Get(), NULL, 0, g_font_collection.GetAddressOf()); | 343 // be loaded, otherwise we fallback to restricted set of fonts. |
344 const UINT32 kMaxFontThreshold = 1000; | |
345 HRESULT hr = E_FAIL; | |
346 if (g_font_loader->GetFontMapSize() < kMaxFontThreshold) { | |
347 hr = factory->CreateCustomFontCollection( | |
348 g_font_loader.Get(), NULL, 0, g_font_collection.GetAddressOf()); | |
349 } | |
350 | |
351 bool loadingRestricted = false; | |
scottmg
2014/08/29 02:51:05
nit; loading_restricted
| |
352 if (FAILED(hr) || !g_font_collection.Get()) { | |
353 // We will try here just one more time with restricted font set. | |
354 g_font_loader->LoadRestrictedFontList(); | |
355 hr = factory->CreateCustomFontCollection( | |
356 g_font_loader.Get(), NULL, 0, g_font_collection.GetAddressOf()); | |
scottmg
2014/08/29 02:51:05
does this succeed if any one of those fonts don't
Shrikant Kelkar
2014/08/29 23:26:28
IIRC, it will not load that particular font/font f
| |
357 } | |
302 | 358 |
303 base::TimeDelta time_delta = base::TimeTicks::Now() - start_tick; | 359 base::TimeDelta time_delta = base::TimeTicks::Now() - start_tick; |
304 int64 delta = time_delta.ToInternalValue(); | 360 int64 delta = time_delta.ToInternalValue(); |
305 base::debug::Alias(&delta); | 361 base::debug::Alias(&delta); |
306 UINT32 size = g_font_loader->GetFontMapSize(); | 362 UINT32 size = g_font_loader->GetFontMapSize(); |
307 base::debug::Alias(&size); | 363 base::debug::Alias(&size); |
364 base::debug::Alias(&loadingRestricted); | |
308 | 365 |
309 CHECK(SUCCEEDED(hr)); | 366 CHECK(SUCCEEDED(hr)); |
310 CHECK(g_font_collection.Get() != NULL); | 367 CHECK(g_font_collection.Get() != NULL); |
311 | 368 |
312 base::debug::ClearCrashKey(kFontKeyName); | 369 base::debug::ClearCrashKey(kFontKeyName); |
313 | 370 |
314 return g_font_collection.Get(); | 371 return g_font_collection.Get(); |
315 } | 372 } |
316 | 373 |
317 } // namespace content | 374 } // namespace content |
OLD | NEW |