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