[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / common / dwrite_font_platform_win.cc
blobfad6f61a2f213e2378f81ea5c245b231d1514f1a
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 "content/public/common/dwrite_font_platform_win.h"
7 #include <dwrite.h>
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 #include <wrl/implements.h>
13 #include <wrl/wrappers/corewrappers.h>
15 #include "base/command_line.h"
16 #include "base/debug/alias.h"
17 #include "base/debug/crash_logging.h"
18 #include "base/files/file_enumerator.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/files/memory_mapped_file.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/scoped_vector.h"
24 #include "base/memory/shared_memory.h"
25 #include "base/metrics/histogram.h"
26 #include "base/path_service.h"
27 #include "base/process/process_handle.h"
28 #include "base/stl_util.h"
29 #include "base/strings/string_number_conversions.h"
30 #include "base/strings/utf_string_conversions.h"
31 #include "base/synchronization/lock.h"
32 #include "base/time/time.h"
33 #include "base/win/registry.h"
34 #include "base/win/scoped_comptr.h"
35 #include "content/public/common/content_switches.h"
37 namespace {
39 // Font Cache implementation short story:
40 // Due to our sandboxing restrictions, we cannot connect to Windows font cache
41 // service from Renderer and need to use DirectWrite isolated font loading
42 // mechanism.
43 // DirectWrite needs to be initialized before any of the API could be used.
44 // During initialization DirectWrite loads all font files and populates
45 // internal cache, we refer this phase as enumeration and we are trying
46 // to optimize this phase in our cache approach. Using cache during
47 // initialization will help improve on startup latency in each renderer
48 // instance.
49 // During enumeration DirectWrite reads various fragments from .ttf/.ttc
50 // font files. Our assumption is that these fragments are being read to
51 // cache information such as font families, supported sizes etc.
52 // For reading fragments DirectWrite calls ReadFragment of our FontFileStream
53 // implementation with parameters start_offset and length. We cache these
54 // parameters along with associated data chunk.
55 // Here is small example of how segments are read
56 // start_offset: 0, length: 16
57 // start_offset: 0, length: 12
58 // start_offset: 0, length: 117
59 // For better cache management we collapse segments if they overlap or are
60 // adjacent.
62 namespace mswr = Microsoft::WRL;
64 const char kFontKeyName[] = "font_key_name";
66 // We use this value to determine whether to cache file fragments
67 // or not. In our trials we observed that for some font files
68 // direct write ends up reading almost entire file during enumeration
69 // phase. If we don't use this percentile formula we will end up
70 // increasing significant cache size by caching entire file contents
71 // for some of the font files.
72 const double kMaxPercentileOfFontFileSizeToCache = 0.6;
74 // With current implementation we map entire shared section into memory during
75 // renderer startup. This causes increase in working set of Chrome. As first
76 // step we want to see if caching is really improving any performance for our
77 // users, so we are putting arbitrary limit on cache file size. There are
78 // multiple ways we can tune our working size, like mapping only required part
79 // of section at any given time.
80 const double kArbitraryCacheFileSizeLimit = (30 * 1024 * 1024);
82 // We have chosen current font file length arbitrarily. In our logic
83 // if we don't find file we are looking for in cache we end up loading
84 // that file directly from system fonts folder.
85 const unsigned int kMaxFontFileNameLength = 34;
87 const DWORD kCacheFileVersion = 103;
88 const DWORD kFileSignature = 0x4D4F5243; // CROM
89 const DWORD kMagicCompletionSignature = 0x454E4F44; // DONE
91 const DWORD kUndefinedDWORDS = 36;
93 // Make sure that all structure sizes align with 8 byte boundary otherwise
94 // dr. memory test may complain.
95 #pragma pack(push, 8)
96 // Cache file header, includes signature, completion bits and version.
97 struct CacheFileHeader {
98 CacheFileHeader() {
99 file_signature = kFileSignature;
100 magic_completion_signature = 0;
101 version = kCacheFileVersion;
102 ::ZeroMemory(undefined, sizeof(undefined));
105 DWORD file_signature;
106 DWORD magic_completion_signature;
107 DWORD version;
108 BYTE undefined[kUndefinedDWORDS];
111 // Entry for a particular font file within this cache.
112 struct CacheFileEntry {
113 CacheFileEntry() {
114 file_size = 0;
115 entry_count = 0;
116 ::ZeroMemory(file_name, sizeof(file_name));
119 UINT64 file_size;
120 DWORD entry_count;
121 wchar_t file_name[kMaxFontFileNameLength];
124 // Offsets or data chunks that are cached for particular font file.
125 struct CacheFileOffsetEntry {
126 CacheFileOffsetEntry() {
127 start_offset = 0;
128 length = 0;
131 UINT64 start_offset;
132 UINT64 length;
133 /* BYTE blob_[]; // Place holder for the blob that follows. */
135 #pragma pack(pop)
137 bool ValidateFontCacheHeader(CacheFileHeader* header) {
138 return (header->file_signature == kFileSignature &&
139 header->magic_completion_signature == kMagicCompletionSignature &&
140 header->version == kCacheFileVersion);
143 class FontCacheWriter;
145 // This class implements main interface required for loading custom font
146 // collection as specified by DirectWrite. We also use this class for storing
147 // some state information as this is one of the centralized entity.
148 class FontCollectionLoader
149 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>,
150 IDWriteFontCollectionLoader> {
151 public:
152 FontCollectionLoader()
153 : in_collection_building_mode_(false),
154 create_static_cache_(false) {}
156 ~FontCollectionLoader() override;
158 HRESULT RuntimeClassInitialize() {
159 return S_OK;
162 // IDWriteFontCollectionLoader methods.
163 HRESULT STDMETHODCALLTYPE
164 CreateEnumeratorFromKey(IDWriteFactory* factory,
165 void const* key,
166 UINT32 key_size,
167 IDWriteFontFileEnumerator** file_enumerator) override;
169 // Does all the initialization for required loading fonts from registry.
170 static HRESULT Initialize(IDWriteFactory* factory);
172 // Returns font cache map size.
173 UINT32 GetFontMapSize();
175 // Returns font name string when given font index.
176 base::string16 GetFontNameFromKey(UINT32 idx);
178 // Loads internal structure with fonts from registry.
179 bool LoadFontListFromRegistry();
181 // Loads restricted web safe fonts as fallback method to registry fonts.
182 bool LoadRestrictedFontList();
184 // Puts class in collection building mode. In collection building mode
185 // we use static cache if it is available as a look aside buffer.
186 void EnableCollectionBuildingMode(bool enable);
188 // Returns current state of collection building.
189 bool InCollectionBuildingMode();
191 // Loads static cache file.
192 bool LoadCacheFile();
194 // Unloads cache file and related data.
195 void UnloadCacheFile();
197 // Puts class in static cache creating mode. In this mode we record all
198 // direct write requests and store chunks of font data.
199 void EnterStaticCacheMode(const WCHAR* file_name);
201 // Gets out of static cache building mode.
202 void LeaveStaticCacheMode();
204 // Returns if class is currently in static cache building mode.
205 bool IsBuildStaticCacheMode();
207 // Validates cache file for consistency.
208 bool ValidateCacheFile(base::File* file);
210 private:
211 // Structure to represent each chunk within font file that we load in memory.
212 struct CacheTableOffsetEntry {
213 UINT64 start_offset;
214 UINT64 length;
215 BYTE* inside_file_ptr;
218 typedef std::vector<CacheTableOffsetEntry> OffsetVector;
220 // Structure representing each font entry with cache.
221 struct CacheTableEntry {
222 UINT64 file_size;
223 OffsetVector offset_entries;
226 public:
227 // Returns whether file we have particular font entry within cache or not.
228 bool IsFileCached(UINT32 font_key);
229 // Returns cache fragment corresponding to specific font key.
230 void* GetCachedFragment(UINT32 font_key, UINT64 start_offset, UINT64 length);
231 // Returns actual font file size at the time of caching.
232 UINT64 GetCachedFileSize(UINT32 font_key);
234 // Returns instance of font cache writer. This class manages actual font
235 // file format.
236 FontCacheWriter* GetFontCacheWriter();
238 private:
239 // Functions validates and loads cache into internal map.
240 bool ValidateAndLoadCacheMap();
242 mswr::ComPtr<IDWriteFontFileLoader> file_loader_;
244 std::vector<base::string16> reg_fonts_;
245 bool in_collection_building_mode_;
246 bool create_static_cache_;
247 scoped_ptr<base::SharedMemory> cache_;
248 scoped_ptr<FontCacheWriter> cache_writer_;
250 typedef std::map<base::string16, CacheTableEntry*> CacheMap;
251 CacheMap cache_map_;
253 DISALLOW_COPY_AND_ASSIGN(FontCollectionLoader);
256 mswr::ComPtr<FontCollectionLoader> g_font_loader;
257 base::win::ScopedHandle g_shared_font_cache;
259 // Class responsible for handling font cache file format details as well as
260 // tracking various cache region requests by direct write.
261 class FontCacheWriter {
262 public:
263 FontCacheWriter() : count_font_entries_ignored_(0), cookie_counter_(0) {}
265 ~FontCacheWriter() {
266 if (static_cache_.get()) {
267 static_cache_->Close();
271 public:
272 // Holds data related to individual region as requested by direct write.
273 struct CacheRegion {
274 UINT64 start_offset;
275 UINT64 length;
276 const BYTE* ptr;
277 /* BYTE blob_[]; // Place holder for the blob that follows. */
280 // Function to create static font cache file.
281 bool Create(const wchar_t* file_name) {
282 static_cache_.reset(new base::File(base::FilePath(file_name),
283 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE |
284 base::File::FLAG_EXCLUSIVE_WRITE));
285 if (!static_cache_->IsValid()) {
286 static_cache_.reset();
287 return false;
289 CacheFileHeader header;
291 // At offset 0 write cache version
292 static_cache_->Write(0,
293 reinterpret_cast<const char*>(&header),
294 sizeof(header));
296 static_cache_->Flush();
297 return true;
300 // Closes static font cache file. Also writes completion signature to mark
301 // it as completely written.
302 void Close() {
303 if (static_cache_.get()) {
304 CacheFileHeader header;
305 header.magic_completion_signature = kMagicCompletionSignature;
306 // At offset 0 write cache version
307 int bytes_written = static_cache_->Write(0,
308 reinterpret_cast<const char*>(&header),
309 sizeof(header));
310 DCHECK_NE(bytes_written, -1);
312 UMA_HISTOGRAM_MEMORY_KB("DirectWrite.Fonts.BuildCache.File.Size",
313 static_cache_->GetLength() / 1024);
315 UMA_HISTOGRAM_COUNTS("DirectWrite.Fonts.BuildCache.Ignored",
316 count_font_entries_ignored_);
318 static_cache_->Close();
319 static_cache_.reset(NULL);
323 private:
324 typedef std::vector<CacheRegion> RegionVector;
326 // Structure to track various regions requested by direct write for particular
327 // font file.
328 struct FontEntryInternal {
329 FontEntryInternal(const wchar_t* name, UINT64 size)
330 : file_name(name),
331 file_size(size) {
334 base::string16 file_name;
335 UINT64 file_size;
336 RegionVector regions;
339 public:
340 // Starts up new font entry to be tracked, returns cookie to identify this
341 // particular entry.
342 UINT NewFontEntry(const wchar_t* file_name, UINT64 file_size) {
343 base::AutoLock lock(lock_);
344 UINT old_counter = cookie_counter_;
345 FontEntryInternal* font_entry = new FontEntryInternal(file_name, file_size);
346 cookie_map_[cookie_counter_].reset(font_entry);
347 cookie_counter_++;
348 return old_counter;
351 // AddRegion function lets caller add various regions to be cached for
352 // particular font file. Once enumerating that particular font file is done
353 // (based on uniquely identifying cookie) changes could be committed using
354 // CommitFontEntry
355 bool AddRegion(UINT64 cookie, UINT64 start, UINT64 length, const BYTE* ptr) {
356 base::AutoLock lock(lock_);
357 if (cookie_map_.find(cookie) == cookie_map_.end())
358 return false;
359 RegionVector& regions = cookie_map_[cookie].get()->regions;
360 CacheRegion region;
361 region.start_offset = start;
362 region.length = length;
363 region.ptr = ptr;
364 regions.push_back(region);
365 return true;
368 // Function which commits after merging all collected regions into cache file.
369 bool CommitFontEntry(UINT cookie) {
370 base::AutoLock lock(lock_);
371 if (cookie_map_.find(cookie) == cookie_map_.end())
372 return false;
374 // We will skip writing entries beyond allowed limit. Following condition
375 // doesn't enforce hard file size. We need to write complete font entry.
376 int64 length = static_cache_->GetLength();
377 if (length == -1 || length >= kArbitraryCacheFileSizeLimit) {
378 count_font_entries_ignored_++;
379 return false;
382 FontEntryInternal* font_entry = cookie_map_[cookie].get();
383 RegionVector& regions = font_entry->regions;
384 std::sort(regions.begin(), regions.end(), SortCacheRegions);
386 // At this point, we have collected all regions to be cached. These regions
387 // are tuples of start, length, data for particular data segment.
388 // These tuples can overlap.
389 // e.g. (0, 12, data), (0, 117, data), (21, 314, data), (335, 15, data)
390 // In this case as you can see first three segments overlap and
391 // 4th is adjacent. If we cache them individually then we will end up
392 // caching duplicate data, so we merge these segments together to find
393 // superset for the cache. In above example our algorithm should
394 // produce (cache) single segment starting at offset 0 with length 350.
395 RegionVector merged_regions;
396 RegionVector::iterator iter;
397 int idx = 0;
398 for (iter = regions.begin(); iter != regions.end(); iter++) {
399 if (iter == regions.begin()) {
400 merged_regions.push_back(*iter);
401 continue;
403 CacheRegion& base_region = merged_regions[idx];
404 if (IsOverlap(&base_region, &(*iter))) {
405 UINT64 end1 = base_region.start_offset + base_region.length;
406 UINT64 end2 = iter->start_offset + iter->length;
407 if (base_region.start_offset > iter->start_offset) {
408 base_region.start_offset = iter->start_offset;
409 base_region.ptr = iter->ptr;
411 base_region.length = std::max(end1, end2) - base_region.start_offset;
412 } else {
413 merged_regions.push_back(*iter);
414 idx++;
418 UINT64 total_merged_cache_in_bytes = 0;
419 for (iter = merged_regions.begin(); iter != merged_regions.end(); iter++) {
420 total_merged_cache_in_bytes += iter->length;
423 // We want to adjust following parameter based on experiments. But general
424 // logic here is that if we are going to end up caching most of the contents
425 // for a file (e.g. simsunb.ttf > 90%) then we should avoid caching that
426 // file.
427 double percentile = static_cast<double>(total_merged_cache_in_bytes) /
428 font_entry->file_size;
429 if (percentile > kMaxPercentileOfFontFileSizeToCache) {
430 count_font_entries_ignored_++;
431 return false;
434 CacheFileEntry entry;
435 wcsncpy_s(entry.file_name, kMaxFontFileNameLength,
436 font_entry->file_name.c_str(), _TRUNCATE);
437 entry.file_size = font_entry->file_size;
438 entry.entry_count = merged_regions.size();
439 static_cache_->WriteAtCurrentPos(
440 reinterpret_cast<const char*>(&entry),
441 sizeof(entry));
442 for (iter = merged_regions.begin(); iter != merged_regions.end(); iter++) {
443 CacheFileOffsetEntry offset_entry;
444 offset_entry.start_offset = iter->start_offset;
445 offset_entry.length = iter->length;
446 static_cache_->WriteAtCurrentPos(
447 reinterpret_cast<const char*>(&offset_entry),
448 sizeof(offset_entry));
449 static_cache_->WriteAtCurrentPos(
450 reinterpret_cast<const char*>(iter->ptr),
451 iter->length);
453 return true;
456 private:
457 // This is the count of font entries that we reject based on size to be
458 // cached.
459 unsigned int count_font_entries_ignored_;
460 scoped_ptr<base::File> static_cache_;
461 std::map<UINT, scoped_ptr<FontEntryInternal>> cookie_map_;
462 UINT cookie_counter_;
464 // Lock is required to protect internal data structures and access to file,
465 // According to MSDN documentation on ReadFileFragment and based on our
466 // experiments so far, there is possibility of ReadFileFragment getting called
467 // from multiple threads.
468 base::Lock lock_;
470 // Function checks if two regions overlap or are adjacent.
471 bool IsOverlap(CacheRegion* region1, CacheRegion* region2) {
472 return
473 !((region1->start_offset + region1->length) < region2->start_offset ||
474 region1->start_offset > (region2->start_offset + region2->length));
477 // Function to sort cached regions.
478 static bool SortCacheRegions(const CacheRegion& region1,
479 const CacheRegion& region2) {
480 return
481 region1.start_offset == region2.start_offset ?
482 region1.length < region2.length :
483 region1.start_offset < region2.start_offset;
486 DISALLOW_COPY_AND_ASSIGN(FontCacheWriter);
489 // Class implements IDWriteFontFileStream interface as required by direct write.
490 class FontFileStream
491 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>,
492 IDWriteFontFileStream> {
493 public:
494 // IDWriteFontFileStream methods.
495 HRESULT STDMETHODCALLTYPE ReadFileFragment(
496 void const** fragment_start,
497 UINT64 file_offset,
498 UINT64 fragment_size,
499 void** context) override {
500 if (cached_data_) {
501 *fragment_start = g_font_loader->GetCachedFragment(font_key_,
502 file_offset,
503 fragment_size);
504 if (*fragment_start == NULL) {
505 DCHECK(false);
507 *context = NULL;
508 return *fragment_start != NULL ? S_OK : E_FAIL;
510 if (!memory_.get() || !memory_->IsValid() ||
511 file_offset >= memory_->length() ||
512 (file_offset + fragment_size) > memory_->length())
513 return E_FAIL;
515 *fragment_start = static_cast<BYTE const*>(memory_->data()) +
516 static_cast<size_t>(file_offset);
517 *context = NULL;
518 if (g_font_loader->IsBuildStaticCacheMode()) {
519 FontCacheWriter* cache_writer = g_font_loader->GetFontCacheWriter();
520 cache_writer->AddRegion(writer_cookie_,
521 file_offset,
522 fragment_size,
523 static_cast<const BYTE*>(*fragment_start));
525 return S_OK;
528 void STDMETHODCALLTYPE ReleaseFileFragment(void* context) override {}
530 HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override {
531 if (cached_data_) {
532 *file_size = g_font_loader->GetCachedFileSize(font_key_);
533 return S_OK;
536 if (!memory_.get() || !memory_->IsValid())
537 return E_FAIL;
539 *file_size = memory_->length();
540 return S_OK;
543 HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* last_write_time) override {
544 if (cached_data_) {
545 *last_write_time = 0;
546 return S_OK;
549 if (!memory_.get() || !memory_->IsValid())
550 return E_FAIL;
552 // According to MSDN article http://goo.gl/rrSYzi the "last modified time"
553 // is used by DirectWrite font selection algorithms to determine whether
554 // one font resource is more up to date than another one.
555 // So by returning 0 we are assuming that it will treat all fonts to be
556 // equally up to date.
557 // TODO(shrikant): We should further investigate this.
558 *last_write_time = 0;
559 return S_OK;
562 FontFileStream() : font_key_(0), cached_data_(false) {}
564 HRESULT RuntimeClassInitialize(UINT32 font_key) {
565 if (g_font_loader->InCollectionBuildingMode() &&
566 g_font_loader->IsFileCached(font_key)) {
567 cached_data_ = true;
568 font_key_ = font_key;
569 return S_OK;
572 base::FilePath path;
573 PathService::Get(base::DIR_WINDOWS_FONTS, &path);
574 base::string16 font_key_name(g_font_loader->GetFontNameFromKey(font_key));
575 path = path.Append(font_key_name.c_str());
576 memory_.reset(new base::MemoryMappedFile());
578 // Put some debug information on stack.
579 WCHAR font_name[MAX_PATH];
580 path.value().copy(font_name, arraysize(font_name));
581 base::debug::Alias(font_name);
583 if (!memory_->Initialize(path)) {
584 memory_.reset();
585 return E_FAIL;
588 font_key_ = font_key;
590 base::debug::SetCrashKeyValue(kFontKeyName,
591 base::WideToUTF8(font_key_name));
593 if (g_font_loader->IsBuildStaticCacheMode()) {
594 FontCacheWriter* cache_writer = g_font_loader->GetFontCacheWriter();
595 writer_cookie_ = cache_writer->NewFontEntry(font_key_name.c_str(),
596 memory_->length());
598 return S_OK;
601 ~FontFileStream() override {
602 if (g_font_loader->IsBuildStaticCacheMode()) {
603 FontCacheWriter* cache_writer = g_font_loader->GetFontCacheWriter();
604 cache_writer->CommitFontEntry(writer_cookie_);
608 private:
609 UINT32 font_key_;
610 scoped_ptr<base::MemoryMappedFile> memory_;
611 bool cached_data_;
612 UINT writer_cookie_;
614 DISALLOW_COPY_AND_ASSIGN(FontFileStream);
617 // Implements IDWriteFontFileLoader as required by FontFileLoader.
618 class FontFileLoader
619 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>,
620 IDWriteFontFileLoader> {
621 public:
622 // IDWriteFontFileLoader methods.
623 HRESULT STDMETHODCALLTYPE
624 CreateStreamFromKey(void const* ref_key,
625 UINT32 ref_key_size,
626 IDWriteFontFileStream** stream) override {
627 if (ref_key_size != sizeof(UINT32))
628 return E_FAIL;
630 UINT32 font_key = *static_cast<const UINT32*>(ref_key);
631 mswr::ComPtr<FontFileStream> font_stream;
632 HRESULT hr = mswr::MakeAndInitialize<FontFileStream>(&font_stream,
633 font_key);
634 if (SUCCEEDED(hr)) {
635 *stream = font_stream.Detach();
636 return S_OK;
638 return E_FAIL;
641 FontFileLoader() {}
642 ~FontFileLoader() override {}
644 private:
645 DISALLOW_COPY_AND_ASSIGN(FontFileLoader);
648 // Implements IDWriteFontFileEnumerator as required by direct write.
649 class FontFileEnumerator
650 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>,
651 IDWriteFontFileEnumerator> {
652 public:
653 // IDWriteFontFileEnumerator methods.
654 HRESULT STDMETHODCALLTYPE MoveNext(BOOL* has_current_file) override {
655 *has_current_file = FALSE;
657 if (current_file_)
658 current_file_.ReleaseAndGetAddressOf();
660 if (font_idx_ < g_font_loader->GetFontMapSize()) {
661 HRESULT hr =
662 factory_->CreateCustomFontFileReference(&font_idx_,
663 sizeof(UINT32),
664 file_loader_.Get(),
665 current_file_.GetAddressOf());
666 DCHECK(SUCCEEDED(hr));
667 *has_current_file = TRUE;
668 font_idx_++;
670 return S_OK;
673 HRESULT STDMETHODCALLTYPE
674 GetCurrentFontFile(IDWriteFontFile** font_file) override {
675 if (!current_file_) {
676 *font_file = NULL;
677 return E_FAIL;
680 *font_file = current_file_.Detach();
681 return S_OK;
684 FontFileEnumerator(const void* keys,
685 UINT32 buffer_size,
686 IDWriteFactory* factory,
687 IDWriteFontFileLoader* file_loader)
688 : factory_(factory), file_loader_(file_loader), font_idx_(0) {}
690 ~FontFileEnumerator() override {}
692 mswr::ComPtr<IDWriteFactory> factory_;
693 mswr::ComPtr<IDWriteFontFile> current_file_;
694 mswr::ComPtr<IDWriteFontFileLoader> file_loader_;
695 UINT32 font_idx_;
697 private:
698 DISALLOW_COPY_AND_ASSIGN(FontFileEnumerator);
701 // IDWriteFontCollectionLoader methods.
702 HRESULT STDMETHODCALLTYPE FontCollectionLoader::CreateEnumeratorFromKey(
703 IDWriteFactory* factory,
704 void const* key,
705 UINT32 key_size,
706 IDWriteFontFileEnumerator** file_enumerator) {
707 *file_enumerator = mswr::Make<FontFileEnumerator>(
708 key, key_size, factory, file_loader_.Get()).Detach();
709 return S_OK;
712 // static
713 HRESULT FontCollectionLoader::Initialize(IDWriteFactory* factory) {
714 DCHECK(g_font_loader == NULL);
716 HRESULT result;
717 result = mswr::MakeAndInitialize<FontCollectionLoader>(&g_font_loader);
718 if (FAILED(result) || !g_font_loader) {
719 DCHECK(false);
720 return E_FAIL;
723 CHECK(g_font_loader->LoadFontListFromRegistry());
725 g_font_loader->file_loader_ = mswr::Make<FontFileLoader>().Detach();
727 factory->RegisterFontFileLoader(g_font_loader->file_loader_.Get());
728 factory->RegisterFontCollectionLoader(g_font_loader.Get());
730 return S_OK;
733 FontCollectionLoader::~FontCollectionLoader() {
734 STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end());
737 UINT32 FontCollectionLoader::GetFontMapSize() {
738 return reg_fonts_.size();
741 base::string16 FontCollectionLoader::GetFontNameFromKey(UINT32 idx) {
742 DCHECK(idx < reg_fonts_.size());
743 return reg_fonts_[idx];
746 const base::FilePath::CharType* kFontExtensionsToIgnore[] {
747 FILE_PATH_LITERAL(".FON"), // Bitmap or vector
748 FILE_PATH_LITERAL(".PFM"), // Adobe Type 1
749 FILE_PATH_LITERAL(".PFB"), // Adobe Type 1
752 const wchar_t* kFontsToIgnore[] = {
753 // "Gill Sans Ultra Bold" turns into an Ultra Bold weight "Gill Sans" in
754 // DirectWrite, but most users don't have any other weights. The regular
755 // weight font is named "Gill Sans MT", but that ends up in a different
756 // family with that name. On Mac, there's a "Gill Sans" with various weights,
757 // so CSS authors use { 'font-family': 'Gill Sans', 'Gill Sans MT', ... } and
758 // because of the DirectWrite family futzing, they end up with an Ultra Bold
759 // font, when they just wanted "Gill Sans". Mozilla implemented a more
760 // complicated hack where they effectively rename the Ultra Bold font to
761 // "Gill Sans MT Ultra Bold", but because the Ultra Bold font is so ugly
762 // anyway, we simply ignore it. See
763 // http://www.microsoft.com/typography/fonts/font.aspx?FMID=978 for a picture
764 // of the font, and the file name. We also ignore "Gill Sans Ultra Bold
765 // Condensed".
766 L"gilsanub.ttf",
767 L"gillubcd.ttf",
770 bool FontCollectionLoader::LoadFontListFromRegistry() {
771 const wchar_t kFontsRegistry[] =
772 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
773 CHECK(reg_fonts_.empty());
774 base::win::RegKey regkey;
775 if (regkey.Open(HKEY_LOCAL_MACHINE, kFontsRegistry, KEY_READ) !=
776 ERROR_SUCCESS) {
777 return false;
780 base::FilePath system_font_path;
781 PathService::Get(base::DIR_WINDOWS_FONTS, &system_font_path);
783 base::string16 name;
784 base::string16 value;
785 for (DWORD idx = 0; idx < regkey.GetValueCount(); idx++) {
786 if (regkey.GetValueNameAt(idx, &name) == ERROR_SUCCESS &&
787 regkey.ReadValue(name.c_str(), &value) == ERROR_SUCCESS) {
788 base::FilePath path(value.c_str());
789 // We need to check if path in registry is absolute, if it is then
790 // we check if it is same as DIR_WINDOWS_FONTS otherwise we ignore.
791 bool absolute = path.IsAbsolute();
792 if (absolute &&
793 !base::FilePath::CompareEqualIgnoreCase(system_font_path.value(),
794 path.DirName().value())) {
795 continue;
798 // Ignore if path ends with a separator.
799 if (path.EndsWithSeparator())
800 continue;
802 if (absolute)
803 value = path.BaseName().value();
805 bool should_ignore = false;
806 for (const auto& ignore : kFontsToIgnore) {
807 if (base::FilePath::CompareEqualIgnoreCase(value, ignore)) {
808 should_ignore = true;
809 break;
812 // DirectWrite doesn't support bitmap/vector fonts and Adobe type 1
813 // fonts, we will ignore those font extensions.
814 // MSDN article: http://goo.gl/TfCOA
815 if (!should_ignore) {
816 for (const auto& ignore : kFontExtensionsToIgnore) {
817 if (path.MatchesExtension(ignore)) {
818 should_ignore = true;
819 break;
824 if (!should_ignore)
825 reg_fonts_.push_back(value.c_str());
828 UMA_HISTOGRAM_COUNTS("DirectWrite.Fonts.Loaded", reg_fonts_.size());
829 UMA_HISTOGRAM_COUNTS("DirectWrite.Fonts.Ignored",
830 regkey.GetValueCount() - reg_fonts_.size());
831 return true;
834 // This list is mainly based on prefs/prefs_tab_helper.cc kFontDefaults.
835 const wchar_t* kRestrictedFontSet[] = {
836 // These are the "Web Safe" fonts.
837 L"times.ttf", // IDS_STANDARD_FONT_FAMILY
838 L"timesbd.ttf", // IDS_STANDARD_FONT_FAMILY
839 L"timesbi.ttf", // IDS_STANDARD_FONT_FAMILY
840 L"timesi.ttf", // IDS_STANDARD_FONT_FAMILY
841 L"cour.ttf", // IDS_FIXED_FONT_FAMILY
842 L"courbd.ttf", // IDS_FIXED_FONT_FAMILY
843 L"courbi.ttf", // IDS_FIXED_FONT_FAMILY
844 L"couri.ttf", // IDS_FIXED_FONT_FAMILY
845 L"consola.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN
846 L"consolab.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN
847 L"consolai.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN
848 L"consolaz.ttf", // IDS_FIXED_FONT_FAMILY_ALT_WIN
849 L"arial.ttf", // IDS_SANS_SERIF_FONT_FAMILY
850 L"arialbd.ttf", // IDS_SANS_SERIF_FONT_FAMILY
851 L"arialbi.ttf", // IDS_SANS_SERIF_FONT_FAMILY
852 L"ariali.ttf", // IDS_SANS_SERIF_FONT_FAMILY
853 L"comic.ttf", // IDS_CURSIVE_FONT_FAMILY
854 L"comicbd.ttf", // IDS_CURSIVE_FONT_FAMILY
855 L"comici.ttf", // IDS_CURSIVE_FONT_FAMILY
856 L"comicz.ttf", // IDS_CURSIVE_FONT_FAMILY
857 L"impact.ttf", // IDS_FANTASY_FONT_FAMILY
858 L"georgia.ttf",
859 L"georgiab.ttf",
860 L"georgiai.ttf",
861 L"georgiaz.ttf",
862 L"trebuc.ttf",
863 L"trebucbd.ttf",
864 L"trebucbi.ttf",
865 L"trebucit.ttf",
866 L"verdana.ttf",
867 L"verdanab.ttf",
868 L"verdanai.ttf",
869 L"verdanaz.ttf",
870 L"segoeui.ttf", // IDS_PICTOGRAPH_FONT_FAMILY
871 L"segoeuib.ttf", // IDS_PICTOGRAPH_FONT_FAMILY
872 L"segoeuii.ttf", // IDS_PICTOGRAPH_FONT_FAMILY
873 L"msgothic.ttc", // IDS_STANDARD_FONT_FAMILY_JAPANESE
874 L"msmincho.ttc", // IDS_SERIF_FONT_FAMILY_JAPANESE
875 L"gulim.ttc", // IDS_FIXED_FONT_FAMILY_KOREAN
876 L"batang.ttc", // IDS_SERIF_FONT_FAMILY_KOREAN
877 L"simsun.ttc", // IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN
878 L"mingliu.ttc", // IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN
880 // These are from the Blink fallback list.
881 L"david.ttf", // USCRIPT_HEBREW
882 L"davidbd.ttf", // USCRIPT_HEBREW
883 L"euphemia.ttf", // USCRIPT_CANADIAN_ABORIGINAL
884 L"gautami.ttf", // USCRIPT_TELUGU
885 L"gautamib.ttf", // USCRIPT_TELUGU
886 L"latha.ttf", // USCRIPT_TAMIL
887 L"lathab.ttf", // USCRIPT_TAMIL
888 L"mangal.ttf", // USCRIPT_DEVANAGARI
889 L"mangalb.ttf", // USCRIPT_DEVANAGARI
890 L"monbaiti.ttf", // USCRIPT_MONGOLIAN
891 L"mvboli.ttf", // USCRIPT_THAANA
892 L"plantc.ttf", // USCRIPT_CHEROKEE
893 L"raavi.ttf", // USCRIPT_GURMUKHI
894 L"raavib.ttf", // USCRIPT_GURMUKHI
895 L"shruti.ttf", // USCRIPT_GUJARATI
896 L"shrutib.ttf", // USCRIPT_GUJARATI
897 L"sylfaen.ttf", // USCRIPT_GEORGIAN and USCRIPT_ARMENIAN
898 L"tahoma.ttf", // USCRIPT_ARABIC,
899 L"tahomabd.ttf", // USCRIPT_ARABIC,
900 L"tunga.ttf", // USCRIPT_KANNADA
901 L"tungab.ttf", // USCRIPT_KANNADA
902 L"vrinda.ttf", // USCRIPT_BENGALI
903 L"vrindab.ttf", // USCRIPT_BENGALI
906 bool FontCollectionLoader::LoadRestrictedFontList() {
907 reg_fonts_.clear();
908 reg_fonts_.assign(kRestrictedFontSet,
909 kRestrictedFontSet + _countof(kRestrictedFontSet));
910 return true;
913 void FontCollectionLoader::EnableCollectionBuildingMode(bool enable) {
914 in_collection_building_mode_ = enable;
917 bool FontCollectionLoader::InCollectionBuildingMode() {
918 return in_collection_building_mode_;
921 bool FontCollectionLoader::IsFileCached(UINT32 font_key) {
922 if (!cache_.get() || cache_->memory() == NULL) {
923 return false;
925 CacheMap::iterator iter = cache_map_.find(
926 GetFontNameFromKey(font_key).c_str());
927 return iter != cache_map_.end();;
930 bool FontCollectionLoader::LoadCacheFile() {
931 std::string font_cache_handle_string =
932 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
933 switches::kFontCacheSharedHandle);
934 if (font_cache_handle_string.empty())
935 return false;
937 base::SharedMemoryHandle font_cache_handle = NULL;
938 base::StringToUint(font_cache_handle_string,
939 reinterpret_cast<unsigned int*>(&font_cache_handle));
940 DCHECK(font_cache_handle);
942 base::SharedMemory* shared_mem = new base::SharedMemory(
943 font_cache_handle, true);
944 // Map while file
945 shared_mem->Map(0);
947 cache_.reset(shared_mem);
949 if (!ValidateAndLoadCacheMap()) {
950 cache_.reset();
951 return false;
954 return true;
957 void FontCollectionLoader::UnloadCacheFile() {
958 cache_.reset();
959 STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end());
960 cache_map_.clear();
963 void FontCollectionLoader::EnterStaticCacheMode(const WCHAR* file_name) {
964 cache_writer_.reset(new FontCacheWriter());
965 if (cache_writer_->Create(file_name))
966 create_static_cache_ = true;
969 void FontCollectionLoader::LeaveStaticCacheMode() {
970 cache_writer_->Close();
971 cache_writer_.reset(NULL);
972 create_static_cache_ = false;
975 bool FontCollectionLoader::IsBuildStaticCacheMode() {
976 return create_static_cache_;
979 bool FontCollectionLoader::ValidateAndLoadCacheMap() {
980 BYTE* mem_file_start = static_cast<BYTE*>(cache_->memory());
981 BYTE* mem_file_end = mem_file_start + cache_->mapped_size();
983 BYTE* current_ptr = mem_file_start;
984 CacheFileHeader* file_header =
985 reinterpret_cast<CacheFileHeader*>(current_ptr);
986 if (!ValidateFontCacheHeader(file_header))
987 return false;
989 current_ptr = current_ptr + sizeof(CacheFileHeader);
990 if (current_ptr >= mem_file_end)
991 return false;
993 while ((current_ptr + sizeof(CacheFileEntry)) < mem_file_end) {
994 CacheFileEntry* entry = reinterpret_cast<CacheFileEntry*>(current_ptr);
995 current_ptr += sizeof(CacheFileEntry);
996 WCHAR file_name[kMaxFontFileNameLength];
997 wcsncpy_s(file_name,
998 kMaxFontFileNameLength,
999 entry->file_name,
1000 _TRUNCATE);
1001 CacheTableEntry* table_entry = NULL;
1002 CacheMap::iterator iter = cache_map_.find(file_name);
1003 if (iter == cache_map_.end()) {
1004 table_entry = new CacheTableEntry();
1005 cache_map_[file_name] = table_entry;
1006 } else {
1007 table_entry = iter->second;
1009 table_entry->file_size = entry->file_size;
1010 for (DWORD idx = 0;
1011 (current_ptr + sizeof(CacheFileOffsetEntry)) < mem_file_end &&
1012 idx < entry->entry_count;
1013 idx++) {
1014 CacheFileOffsetEntry* offset_entry =
1015 reinterpret_cast<CacheFileOffsetEntry*>(current_ptr);
1016 CacheTableOffsetEntry table_offset_entry;
1017 table_offset_entry.start_offset = offset_entry->start_offset;
1018 table_offset_entry.length = offset_entry->length;
1019 table_offset_entry.inside_file_ptr =
1020 current_ptr + sizeof(CacheFileOffsetEntry);
1021 table_entry->offset_entries.push_back(table_offset_entry);
1022 current_ptr += sizeof(CacheFileOffsetEntry);
1023 current_ptr += offset_entry->length;
1027 return true;
1030 void* FontCollectionLoader::GetCachedFragment(UINT32 font_key,
1031 UINT64 start_offset,
1032 UINT64 length) {
1033 UINT64 just_past_end = start_offset + length;
1034 CacheMap::iterator iter = cache_map_.find(
1035 GetFontNameFromKey(font_key).c_str());
1036 if (iter != cache_map_.end()) {
1037 CacheTableEntry* entry = iter->second;
1038 OffsetVector::iterator offset_iter = entry->offset_entries.begin();
1039 while (offset_iter != entry->offset_entries.end()) {
1040 UINT64 available_just_past_end =
1041 offset_iter->start_offset + offset_iter->length;
1042 if (offset_iter->start_offset <= start_offset &&
1043 just_past_end <= available_just_past_end) {
1044 return offset_iter->inside_file_ptr +
1045 (start_offset - offset_iter->start_offset);
1047 offset_iter++;
1050 return NULL;
1053 UINT64 FontCollectionLoader::GetCachedFileSize(UINT32 font_key) {
1054 CacheMap::iterator iter = cache_map_.find(
1055 GetFontNameFromKey(font_key).c_str());
1056 if (iter != cache_map_.end()) {
1057 return iter->second->file_size;
1059 return 0;
1062 FontCacheWriter* FontCollectionLoader::GetFontCacheWriter() {
1063 return cache_writer_.get();
1066 } // namespace
1068 namespace content {
1070 const char kFontCacheSharedSectionName[] = "ChromeDWriteFontCache";
1072 mswr::ComPtr<IDWriteFontCollection> g_font_collection;
1074 IDWriteFontCollection* GetCustomFontCollection(IDWriteFactory* factory) {
1075 if (g_font_collection.Get() != NULL)
1076 return g_font_collection.Get();
1078 base::TimeTicks start_tick = base::TimeTicks::Now();
1080 FontCollectionLoader::Initialize(factory);
1082 bool cache_file_loaded = g_font_loader->LoadCacheFile();
1084 // Arbitrary threshold to stop loading enormous number of fonts. Usual
1085 // side effect of loading large number of fonts results in renderer getting
1086 // killed as it appears to hang.
1087 const UINT32 kMaxFontThreshold = 1750;
1088 HRESULT hr = E_FAIL;
1089 if (cache_file_loaded ||
1090 g_font_loader->GetFontMapSize() < kMaxFontThreshold) {
1091 g_font_loader->EnableCollectionBuildingMode(true);
1092 hr = factory->CreateCustomFontCollection(
1093 g_font_loader.Get(), NULL, 0, g_font_collection.GetAddressOf());
1094 g_font_loader->UnloadCacheFile();
1095 g_font_loader->EnableCollectionBuildingMode(false);
1097 bool loading_restricted = false;
1098 if (FAILED(hr) || !g_font_collection.Get()) {
1099 loading_restricted = true;
1100 // We will try here just one more time with restricted font set.
1101 g_font_loader->LoadRestrictedFontList();
1102 hr = factory->CreateCustomFontCollection(
1103 g_font_loader.Get(), NULL, 0, g_font_collection.GetAddressOf());
1106 base::TimeDelta time_delta = base::TimeTicks::Now() - start_tick;
1107 int64 delta = time_delta.ToInternalValue();
1108 base::debug::Alias(&delta);
1109 UINT32 size = g_font_loader->GetFontMapSize();
1110 base::debug::Alias(&size);
1111 base::debug::Alias(&loading_restricted);
1113 CHECK(SUCCEEDED(hr));
1114 CHECK(g_font_collection.Get() != NULL);
1116 if (cache_file_loaded)
1117 UMA_HISTOGRAM_TIMES("DirectWrite.Fonts.LoadTime.Cached", time_delta);
1118 else
1119 UMA_HISTOGRAM_TIMES("DirectWrite.Fonts.LoadTime", time_delta);
1121 base::debug::ClearCrashKey(kFontKeyName);
1123 return g_font_collection.Get();
1126 bool BuildFontCacheInternal(const WCHAR* file_name) {
1127 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc;
1128 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll");
1129 if (!dwrite_dll) {
1130 DWORD load_library_get_last_error = GetLastError();
1131 base::debug::Alias(&dwrite_dll);
1132 base::debug::Alias(&load_library_get_last_error);
1133 CHECK(false);
1136 DWriteCreateFactoryProc dwrite_create_factory_proc =
1137 reinterpret_cast<DWriteCreateFactoryProc>(
1138 GetProcAddress(dwrite_dll, "DWriteCreateFactory"));
1140 if (!dwrite_create_factory_proc) {
1141 DWORD get_proc_address_get_last_error = GetLastError();
1142 base::debug::Alias(&dwrite_create_factory_proc);
1143 base::debug::Alias(&get_proc_address_get_last_error);
1144 CHECK(false);
1147 mswr::ComPtr<IDWriteFactory> factory;
1149 CHECK(SUCCEEDED(
1150 dwrite_create_factory_proc(
1151 DWRITE_FACTORY_TYPE_ISOLATED,
1152 __uuidof(IDWriteFactory),
1153 reinterpret_cast<IUnknown**>(factory.GetAddressOf()))));
1155 base::TimeTicks start_tick = base::TimeTicks::Now();
1157 FontCollectionLoader::Initialize(factory.Get());
1159 g_font_loader->EnterStaticCacheMode(file_name);
1161 mswr::ComPtr<IDWriteFontCollection> font_collection;
1163 HRESULT hr = E_FAIL;
1164 g_font_loader->EnableCollectionBuildingMode(true);
1165 hr = factory->CreateCustomFontCollection(
1166 g_font_loader.Get(), NULL, 0, font_collection.GetAddressOf());
1167 g_font_loader->EnableCollectionBuildingMode(false);
1169 bool loading_restricted = false;
1170 if (FAILED(hr) || !font_collection.Get()) {
1171 loading_restricted = true;
1172 // We will try here just one more time with restricted font set.
1173 g_font_loader->LoadRestrictedFontList();
1174 hr = factory->CreateCustomFontCollection(
1175 g_font_loader.Get(), NULL, 0, font_collection.GetAddressOf());
1178 g_font_loader->LeaveStaticCacheMode();
1180 base::TimeDelta time_delta = base::TimeTicks::Now() - start_tick;
1181 int64 delta = time_delta.ToInternalValue();
1182 base::debug::Alias(&delta);
1183 UINT32 size = g_font_loader->GetFontMapSize();
1184 base::debug::Alias(&size);
1185 base::debug::Alias(&loading_restricted);
1187 CHECK(SUCCEEDED(hr));
1188 CHECK(font_collection.Get() != NULL);
1190 base::debug::ClearCrashKey(kFontKeyName);
1192 return true;
1195 bool ValidateFontCacheFile(base::File* file) {
1196 DCHECK(file != NULL);
1197 CacheFileHeader file_header;
1198 if (file->Read(0, reinterpret_cast<char*>(&file_header), sizeof(file_header))
1199 == -1) {
1200 return false;
1202 return ValidateFontCacheHeader(&file_header);
1205 bool LoadFontCache(const base::FilePath& path) {
1206 scoped_ptr<base::File> file(new base::File(path,
1207 base::File::FLAG_OPEN | base::File::FLAG_READ));
1208 if (!file->IsValid())
1209 return false;
1211 if (!ValidateFontCacheFile(file.get()))
1212 return false;
1214 base::string16 name(base::ASCIIToUTF16(content::kFontCacheSharedSectionName));
1215 name.append(base::UintToString16(base::GetCurrentProcId()));
1216 HANDLE mapping = ::CreateFileMapping(
1217 file->GetPlatformFile(),
1218 NULL,
1219 PAGE_READONLY,
1222 name.c_str());
1223 if (mapping == INVALID_HANDLE_VALUE)
1224 return false;
1226 if (::GetLastError() == ERROR_ALREADY_EXISTS) {
1227 CloseHandle(mapping);
1228 // We crash here, as no one should have created this mapping except Chrome.
1229 CHECK(false);
1230 return false;
1233 DCHECK(!g_shared_font_cache.IsValid());
1234 g_shared_font_cache.Set(mapping);
1236 return true;
1239 bool BuildFontCache(const base::FilePath& file) {
1240 return BuildFontCacheInternal(file.value().c_str());
1243 } // namespace content