OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #ifndef CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ |
| 6 #define CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ |
| 7 |
| 8 #include <dwrite.h> |
| 9 #include <wrl.h> |
| 10 |
| 11 #include <map> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/callback.h" |
| 15 #include "base/files/memory_mapped_file.h" |
| 16 #include "base/macros.h" |
| 17 #include "base/strings/string16.h" |
| 18 #include "content/common/content_export.h" |
| 19 |
| 20 namespace mswr = Microsoft::WRL; |
| 21 |
| 22 namespace IPC { |
| 23 class Sender; |
| 24 } |
| 25 |
| 26 namespace content { |
| 27 |
| 28 class DWriteFontFamilyProxy; |
| 29 |
| 30 // Implements a DirectWrite font collection that uses IPC to the browser to do |
| 31 // font enumeration. If a matching family is found, it will be loaded locally |
| 32 // into a custom font collection. |
| 33 // This is needed because the sandbox interferes with DirectWrite's |
| 34 // communication with the system font service. |
| 35 class CONTENT_EXPORT DWriteFontCollectionProxy |
| 36 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 37 IDWriteFontCollection, |
| 38 IDWriteFontCollectionLoader, |
| 39 IDWriteFontFileLoader> { |
| 40 public: |
| 41 // IDWriteFontCollection: |
| 42 HRESULT STDMETHODCALLTYPE FindFamilyName(const WCHAR* family_name, |
| 43 UINT32* index, |
| 44 BOOL* exists) override; |
| 45 HRESULT STDMETHODCALLTYPE |
| 46 GetFontFamily(UINT32 index, IDWriteFontFamily** font_family) override; |
| 47 UINT32 STDMETHODCALLTYPE GetFontFamilyCount() override; |
| 48 HRESULT STDMETHODCALLTYPE GetFontFromFontFace(IDWriteFontFace* font_face, |
| 49 IDWriteFont** font) override; |
| 50 |
| 51 // IDWriteFontCollectionLoader: |
| 52 HRESULT STDMETHODCALLTYPE CreateEnumeratorFromKey( |
| 53 IDWriteFactory* factory, |
| 54 const void* collection_key, |
| 55 UINT32 collection_key_size, |
| 56 IDWriteFontFileEnumerator** font_file_enumerator) override; |
| 57 |
| 58 // IDWriteFontFileLoader: |
| 59 HRESULT STDMETHODCALLTYPE |
| 60 CreateStreamFromKey(const void* font_file_reference_key, |
| 61 uint32 font_file_reference_key_size, |
| 62 IDWriteFontFileStream** font_file_stream) override; |
| 63 |
| 64 HRESULT STDMETHODCALLTYPE |
| 65 RuntimeClassInitialize(IDWriteFactory* factory, |
| 66 const base::Callback<IPC::Sender*(void)>& sender); |
| 67 |
| 68 void Unregister(); |
| 69 |
| 70 bool LoadFamily(unsigned int family_index, |
| 71 IDWriteFontCollection** containing_collection); |
| 72 |
| 73 bool LoadFamilyNames(unsigned int family_index, |
| 74 IDWriteLocalizedStrings** strings); |
| 75 |
| 76 bool CreateFamily(unsigned int family_index); |
| 77 |
| 78 private: |
| 79 mswr::ComPtr<IDWriteFactory> factory_; |
| 80 std::vector<mswr::ComPtr<DWriteFontFamilyProxy>> families_; |
| 81 std::map<base::string16, unsigned int> family_names_; |
| 82 UINT32 family_count_ = UINT_MAX; |
| 83 base::Callback<IPC::Sender*(void)> sender_; |
| 84 |
| 85 DISALLOW_ASSIGN(DWriteFontCollectionProxy); |
| 86 }; |
| 87 |
| 88 // Implements the DirectWrite font family interface. This class is just a |
| 89 // stub, until something calls a method that requires actual font data. At that |
| 90 // point this will load the font files into a custom collection and |
| 91 // subsequently calls will be proxied to the resulting DirectWrite object. |
| 92 class CONTENT_EXPORT DWriteFontFamilyProxy |
| 93 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 94 IDWriteFontFamily> { |
| 95 public: |
| 96 // IDWriteFontFamily: |
| 97 HRESULT STDMETHODCALLTYPE |
| 98 GetFontCollection(IDWriteFontCollection** font_collection) override; |
| 99 UINT32 STDMETHODCALLTYPE GetFontCount() override; |
| 100 HRESULT STDMETHODCALLTYPE GetFont(UINT32 index, IDWriteFont** font) override; |
| 101 HRESULT STDMETHODCALLTYPE |
| 102 GetFamilyNames(IDWriteLocalizedStrings** names) override; |
| 103 HRESULT STDMETHODCALLTYPE |
| 104 GetFirstMatchingFont(DWRITE_FONT_WEIGHT weight, |
| 105 DWRITE_FONT_STRETCH stretch, |
| 106 DWRITE_FONT_STYLE style, |
| 107 IDWriteFont** matching_font) override; |
| 108 HRESULT STDMETHODCALLTYPE |
| 109 GetMatchingFonts(DWRITE_FONT_WEIGHT weight, |
| 110 DWRITE_FONT_STRETCH stretch, |
| 111 DWRITE_FONT_STYLE style, |
| 112 IDWriteFontList** matching_fonts) override; |
| 113 |
| 114 HRESULT STDMETHODCALLTYPE |
| 115 RuntimeClassInitialize(DWriteFontCollectionProxy* collection, |
| 116 unsigned int index); |
| 117 |
| 118 bool GetFontFromFontFace(IDWriteFontFace* font_face, IDWriteFont** font); |
| 119 |
| 120 void SetName(base::string16 family_name) { family_name_.assign(family_name); } |
| 121 |
| 122 bool IsLoaded() { return family_ != nullptr; } |
| 123 |
| 124 protected: |
| 125 bool LoadFamily(); |
| 126 |
| 127 private: |
| 128 unsigned int family_index_; |
| 129 base::string16 family_name_; |
| 130 mswr::ComPtr<DWriteFontCollectionProxy> proxy_collection_; |
| 131 mswr::ComPtr<IDWriteFontFamily> family_; |
| 132 mswr::ComPtr<IDWriteLocalizedStrings> family_names_; |
| 133 |
| 134 DISALLOW_ASSIGN(DWriteFontFamilyProxy); |
| 135 }; |
| 136 |
| 137 // Implements the DirectWrite font file enumerator interface, backed by a list |
| 138 // of font files. |
| 139 class CONTENT_EXPORT FontFileEnumerator |
| 140 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 141 IDWriteFontFileEnumerator> { |
| 142 public: |
| 143 // IDWriteFontFileEnumerator: |
| 144 HRESULT STDMETHODCALLTYPE GetCurrentFontFile(IDWriteFontFile** file) override; |
| 145 HRESULT STDMETHODCALLTYPE MoveNext(BOOL* has_current_file) override; |
| 146 |
| 147 HRESULT STDMETHODCALLTYPE |
| 148 RuntimeClassInitialize(IDWriteFactory* factory, |
| 149 IDWriteFontFileLoader* loader, |
| 150 std::vector<base::string16>* file_names); |
| 151 |
| 152 private: |
| 153 mswr::ComPtr<IDWriteFactory> factory_; |
| 154 mswr::ComPtr<IDWriteFontFileLoader> loader_; |
| 155 std::vector<base::string16> file_names_; |
| 156 std::vector<mswr::ComPtr<IDWriteFontFileStream>> file_streams_; |
| 157 unsigned int next_file_ = 0; |
| 158 unsigned int current_file_ = UINT_MAX; |
| 159 |
| 160 DISALLOW_ASSIGN(FontFileEnumerator); |
| 161 }; |
| 162 |
| 163 // Implements the DirectWrite font file stream interface that maps the file to |
| 164 // be loaded as a memory mapped file, and subsequently returns pointers into |
| 165 // the mapped memory block. |
| 166 // TODO(kulshin): confirm that using custom streams is actually an improvement |
| 167 class CONTENT_EXPORT FontFileStream |
| 168 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
| 169 IDWriteFontFileStream> { |
| 170 public: |
| 171 // IDWriteFontFileStream: |
| 172 HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override; |
| 173 HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* last_write_time) override; |
| 174 HRESULT STDMETHODCALLTYPE ReadFileFragment(const void** fragment_start, |
| 175 UINT64 file_offset, |
| 176 UINT64 fragment_size, |
| 177 void** fragment_context) override; |
| 178 void STDMETHODCALLTYPE ReleaseFileFragment(void* fragment_context) override {} |
| 179 |
| 180 HRESULT STDMETHODCALLTYPE |
| 181 RuntimeClassInitialize(const base::string16& file_name); |
| 182 |
| 183 private: |
| 184 base::MemoryMappedFile data_; |
| 185 |
| 186 DISALLOW_ASSIGN(FontFileStream); |
| 187 }; |
| 188 |
| 189 } // namespace content |
| 190 #endif // CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ |
OLD | NEW |