| 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/public/common/dwrite_font_platform_win.h" | 5 #include "content/public/common/dwrite_font_platform_win.h" |
| 6 | 6 |
| 7 #include <dwrite.h> | 7 #include <dwrite.h> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // collection as specified by DirectWrite. We also use this class for storing | 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. | 147 // some state information as this is one of the centralized entity. |
| 148 class FontCollectionLoader | 148 class FontCollectionLoader |
| 149 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | 149 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 150 IDWriteFontCollectionLoader> { | 150 IDWriteFontCollectionLoader> { |
| 151 public: | 151 public: |
| 152 FontCollectionLoader() | 152 FontCollectionLoader() |
| 153 : in_collection_building_mode_(false), | 153 : in_collection_building_mode_(false), |
| 154 create_static_cache_(false) {} | 154 create_static_cache_(false) {} |
| 155 | 155 |
| 156 virtual ~FontCollectionLoader(); | 156 ~FontCollectionLoader() override; |
| 157 | 157 |
| 158 HRESULT RuntimeClassInitialize() { | 158 HRESULT RuntimeClassInitialize() { |
| 159 return S_OK; | 159 return S_OK; |
| 160 } | 160 } |
| 161 | 161 |
| 162 // IDWriteFontCollectionLoader methods. | 162 // IDWriteFontCollectionLoader methods. |
| 163 virtual HRESULT STDMETHODCALLTYPE | 163 HRESULT STDMETHODCALLTYPE |
| 164 CreateEnumeratorFromKey( | 164 CreateEnumeratorFromKey(IDWriteFactory* factory, |
| 165 IDWriteFactory* factory, | 165 void const* key, |
| 166 void const* key, | 166 UINT32 key_size, |
| 167 UINT32 key_size, | 167 IDWriteFontFileEnumerator** file_enumerator) override; |
| 168 IDWriteFontFileEnumerator** file_enumerator) override; | |
| 169 | 168 |
| 170 // Does all the initialization for required loading fonts from registry. | 169 // Does all the initialization for required loading fonts from registry. |
| 171 static HRESULT Initialize(IDWriteFactory* factory); | 170 static HRESULT Initialize(IDWriteFactory* factory); |
| 172 | 171 |
| 173 // Returns font cache map size. | 172 // Returns font cache map size. |
| 174 UINT32 GetFontMapSize(); | 173 UINT32 GetFontMapSize(); |
| 175 | 174 |
| 176 // Returns font name string when given font index. | 175 // Returns font name string when given font index. |
| 177 base::string16 GetFontNameFromKey(UINT32 idx); | 176 base::string16 GetFontNameFromKey(UINT32 idx); |
| 178 | 177 |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 | 485 |
| 487 DISALLOW_COPY_AND_ASSIGN(FontCacheWriter); | 486 DISALLOW_COPY_AND_ASSIGN(FontCacheWriter); |
| 488 }; | 487 }; |
| 489 | 488 |
| 490 // Class implements IDWriteFontFileStream interface as required by direct write. | 489 // Class implements IDWriteFontFileStream interface as required by direct write. |
| 491 class FontFileStream | 490 class FontFileStream |
| 492 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | 491 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 493 IDWriteFontFileStream> { | 492 IDWriteFontFileStream> { |
| 494 public: | 493 public: |
| 495 // IDWriteFontFileStream methods. | 494 // IDWriteFontFileStream methods. |
| 496 virtual HRESULT STDMETHODCALLTYPE ReadFileFragment( | 495 HRESULT STDMETHODCALLTYPE ReadFileFragment( |
| 497 void const** fragment_start, | 496 void const** fragment_start, |
| 498 UINT64 file_offset, | 497 UINT64 file_offset, |
| 499 UINT64 fragment_size, | 498 UINT64 fragment_size, |
| 500 void** context) override { | 499 void** context) override { |
| 501 if (cached_data_) { | 500 if (cached_data_) { |
| 502 *fragment_start = g_font_loader->GetCachedFragment(font_key_, | 501 *fragment_start = g_font_loader->GetCachedFragment(font_key_, |
| 503 file_offset, | 502 file_offset, |
| 504 fragment_size); | 503 fragment_size); |
| 505 if (*fragment_start == NULL) { | 504 if (*fragment_start == NULL) { |
| 506 DCHECK(false); | 505 DCHECK(false); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 519 if (g_font_loader->IsBuildStaticCacheMode()) { | 518 if (g_font_loader->IsBuildStaticCacheMode()) { |
| 520 FontCacheWriter* cache_writer = g_font_loader->GetFontCacheWriter(); | 519 FontCacheWriter* cache_writer = g_font_loader->GetFontCacheWriter(); |
| 521 cache_writer->AddRegion(writer_cookie_, | 520 cache_writer->AddRegion(writer_cookie_, |
| 522 file_offset, | 521 file_offset, |
| 523 fragment_size, | 522 fragment_size, |
| 524 static_cast<const BYTE*>(*fragment_start)); | 523 static_cast<const BYTE*>(*fragment_start)); |
| 525 } | 524 } |
| 526 return S_OK; | 525 return S_OK; |
| 527 } | 526 } |
| 528 | 527 |
| 529 virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* context) override {} | 528 void STDMETHODCALLTYPE ReleaseFileFragment(void* context) override {} |
| 530 | 529 |
| 531 virtual HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override { | 530 HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override { |
| 532 if (cached_data_) { | 531 if (cached_data_) { |
| 533 *file_size = g_font_loader->GetCachedFileSize(font_key_); | 532 *file_size = g_font_loader->GetCachedFileSize(font_key_); |
| 534 return S_OK; | 533 return S_OK; |
| 535 } | 534 } |
| 536 | 535 |
| 537 if (!memory_.get() || !memory_->IsValid()) | 536 if (!memory_.get() || !memory_->IsValid()) |
| 538 return E_FAIL; | 537 return E_FAIL; |
| 539 | 538 |
| 540 *file_size = memory_->length(); | 539 *file_size = memory_->length(); |
| 541 return S_OK; | 540 return S_OK; |
| 542 } | 541 } |
| 543 | 542 |
| 544 virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime( | 543 HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* last_write_time) override { |
| 545 UINT64* last_write_time) override { | |
| 546 if (cached_data_) { | 544 if (cached_data_) { |
| 547 *last_write_time = 0; | 545 *last_write_time = 0; |
| 548 return S_OK; | 546 return S_OK; |
| 549 } | 547 } |
| 550 | 548 |
| 551 if (!memory_.get() || !memory_->IsValid()) | 549 if (!memory_.get() || !memory_->IsValid()) |
| 552 return E_FAIL; | 550 return E_FAIL; |
| 553 | 551 |
| 554 // According to MSDN article http://goo.gl/rrSYzi the "last modified time" | 552 // According to MSDN article http://goo.gl/rrSYzi the "last modified time" |
| 555 // is used by DirectWrite font selection algorithms to determine whether | 553 // is used by DirectWrite font selection algorithms to determine whether |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 | 614 |
| 617 DISALLOW_COPY_AND_ASSIGN(FontFileStream); | 615 DISALLOW_COPY_AND_ASSIGN(FontFileStream); |
| 618 }; | 616 }; |
| 619 | 617 |
| 620 // Implements IDWriteFontFileLoader as required by FontFileLoader. | 618 // Implements IDWriteFontFileLoader as required by FontFileLoader. |
| 621 class FontFileLoader | 619 class FontFileLoader |
| 622 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | 620 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 623 IDWriteFontFileLoader> { | 621 IDWriteFontFileLoader> { |
| 624 public: | 622 public: |
| 625 // IDWriteFontFileLoader methods. | 623 // IDWriteFontFileLoader methods. |
| 626 virtual HRESULT STDMETHODCALLTYPE | 624 HRESULT STDMETHODCALLTYPE |
| 627 CreateStreamFromKey(void const* ref_key, | 625 CreateStreamFromKey(void const* ref_key, |
| 628 UINT32 ref_key_size, | 626 UINT32 ref_key_size, |
| 629 IDWriteFontFileStream** stream) override { | 627 IDWriteFontFileStream** stream) override { |
| 630 if (ref_key_size != sizeof(UINT32)) | 628 if (ref_key_size != sizeof(UINT32)) |
| 631 return E_FAIL; | 629 return E_FAIL; |
| 632 | 630 |
| 633 UINT32 font_key = *static_cast<const UINT32*>(ref_key); | 631 UINT32 font_key = *static_cast<const UINT32*>(ref_key); |
| 634 mswr::ComPtr<FontFileStream> font_stream; | 632 mswr::ComPtr<FontFileStream> font_stream; |
| 635 HRESULT hr = mswr::MakeAndInitialize<FontFileStream>(&font_stream, | 633 HRESULT hr = mswr::MakeAndInitialize<FontFileStream>(&font_stream, |
| 636 font_key); | 634 font_key); |
| 637 if (SUCCEEDED(hr)) { | 635 if (SUCCEEDED(hr)) { |
| 638 *stream = font_stream.Detach(); | 636 *stream = font_stream.Detach(); |
| 639 return S_OK; | 637 return S_OK; |
| 640 } | 638 } |
| 641 return E_FAIL; | 639 return E_FAIL; |
| 642 } | 640 } |
| 643 | 641 |
| 644 FontFileLoader() {} | 642 FontFileLoader() {} |
| 645 virtual ~FontFileLoader() {} | 643 ~FontFileLoader() override {} |
| 646 | 644 |
| 647 private: | 645 private: |
| 648 DISALLOW_COPY_AND_ASSIGN(FontFileLoader); | 646 DISALLOW_COPY_AND_ASSIGN(FontFileLoader); |
| 649 }; | 647 }; |
| 650 | 648 |
| 651 // Implements IDWriteFontFileEnumerator as required by direct write. | 649 // Implements IDWriteFontFileEnumerator as required by direct write. |
| 652 class FontFileEnumerator | 650 class FontFileEnumerator |
| 653 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | 651 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 654 IDWriteFontFileEnumerator> { | 652 IDWriteFontFileEnumerator> { |
| 655 public: | 653 public: |
| 656 // IDWriteFontFileEnumerator methods. | 654 // IDWriteFontFileEnumerator methods. |
| 657 virtual HRESULT STDMETHODCALLTYPE MoveNext(BOOL* has_current_file) override { | 655 HRESULT STDMETHODCALLTYPE MoveNext(BOOL* has_current_file) override { |
| 658 *has_current_file = FALSE; | 656 *has_current_file = FALSE; |
| 659 | 657 |
| 660 if (current_file_) | 658 if (current_file_) |
| 661 current_file_.ReleaseAndGetAddressOf(); | 659 current_file_.ReleaseAndGetAddressOf(); |
| 662 | 660 |
| 663 if (font_idx_ < g_font_loader->GetFontMapSize()) { | 661 if (font_idx_ < g_font_loader->GetFontMapSize()) { |
| 664 HRESULT hr = | 662 HRESULT hr = |
| 665 factory_->CreateCustomFontFileReference(&font_idx_, | 663 factory_->CreateCustomFontFileReference(&font_idx_, |
| 666 sizeof(UINT32), | 664 sizeof(UINT32), |
| 667 file_loader_.Get(), | 665 file_loader_.Get(), |
| 668 current_file_.GetAddressOf()); | 666 current_file_.GetAddressOf()); |
| 669 DCHECK(SUCCEEDED(hr)); | 667 DCHECK(SUCCEEDED(hr)); |
| 670 *has_current_file = TRUE; | 668 *has_current_file = TRUE; |
| 671 font_idx_++; | 669 font_idx_++; |
| 672 } | 670 } |
| 673 return S_OK; | 671 return S_OK; |
| 674 } | 672 } |
| 675 | 673 |
| 676 virtual HRESULT STDMETHODCALLTYPE | 674 HRESULT STDMETHODCALLTYPE |
| 677 GetCurrentFontFile(IDWriteFontFile** font_file) override { | 675 GetCurrentFontFile(IDWriteFontFile** font_file) override { |
| 678 if (!current_file_) { | 676 if (!current_file_) { |
| 679 *font_file = NULL; | 677 *font_file = NULL; |
| 680 return E_FAIL; | 678 return E_FAIL; |
| 681 } | 679 } |
| 682 | 680 |
| 683 *font_file = current_file_.Detach(); | 681 *font_file = current_file_.Detach(); |
| 684 return S_OK; | 682 return S_OK; |
| 685 } | 683 } |
| 686 | 684 |
| 687 FontFileEnumerator(const void* keys, | 685 FontFileEnumerator(const void* keys, |
| 688 UINT32 buffer_size, | 686 UINT32 buffer_size, |
| 689 IDWriteFactory* factory, | 687 IDWriteFactory* factory, |
| 690 IDWriteFontFileLoader* file_loader) | 688 IDWriteFontFileLoader* file_loader) |
| 691 : factory_(factory), file_loader_(file_loader), font_idx_(0) {} | 689 : factory_(factory), file_loader_(file_loader), font_idx_(0) {} |
| 692 | 690 |
| 693 virtual ~FontFileEnumerator() {} | 691 ~FontFileEnumerator() override {} |
| 694 | 692 |
| 695 mswr::ComPtr<IDWriteFactory> factory_; | 693 mswr::ComPtr<IDWriteFactory> factory_; |
| 696 mswr::ComPtr<IDWriteFontFile> current_file_; | 694 mswr::ComPtr<IDWriteFontFile> current_file_; |
| 697 mswr::ComPtr<IDWriteFontFileLoader> file_loader_; | 695 mswr::ComPtr<IDWriteFontFileLoader> file_loader_; |
| 698 UINT32 font_idx_; | 696 UINT32 font_idx_; |
| 699 | 697 |
| 700 private: | 698 private: |
| 701 DISALLOW_COPY_AND_ASSIGN(FontFileEnumerator); | 699 DISALLOW_COPY_AND_ASSIGN(FontFileEnumerator); |
| 702 }; | 700 }; |
| 703 | 701 |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1220 g_shared_font_cache.Set(mapping); | 1218 g_shared_font_cache.Set(mapping); |
| 1221 | 1219 |
| 1222 return true; | 1220 return true; |
| 1223 } | 1221 } |
| 1224 | 1222 |
| 1225 bool BuildFontCache(const base::FilePath& file) { | 1223 bool BuildFontCache(const base::FilePath& file) { |
| 1226 return BuildFontCacheInternal(file.value().c_str()); | 1224 return BuildFontCacheInternal(file.value().c_str()); |
| 1227 } | 1225 } |
| 1228 | 1226 |
| 1229 } // namespace content | 1227 } // namespace content |
| OLD | NEW |