Chromium Code Reviews| 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_COMMON_DWRITE_FONT_PROXY_WIN_H_ | |
| 6 #define CONTENT_COMMON_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/strings/string16.h" | |
| 17 #include "content/common/content_export.h" | |
| 18 | |
| 19 namespace mswr = Microsoft::WRL; | |
| 20 | |
| 21 namespace IPC { | |
| 22 class Sender; | |
| 23 } | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 typedef IPC::Sender* (*GetSenderFn)(); | |
| 28 | |
| 29 class DWriteFontFamilyProxy; | |
| 30 | |
| 31 // Implements a direct write font collection that uses IPC to the browser to do | |
|
jam
2015/11/03 00:18:54
this should be in content/child
content/common is
Ilya Kulshin
2015/11/03 18:56:22
Done.
| |
| 32 // font enumeration. If a matching family is found, it will be loaded locally | |
| 33 // into a custom font collection. | |
| 34 // This is needed because the sandbox interferes with direct write's | |
| 35 // communication with the system font service. | |
| 36 class CONTENT_EXPORT DWriteFontCollectionProxy | |
| 37 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | |
| 38 IDWriteFontCollection, | |
| 39 IDWriteFontCollectionLoader, | |
| 40 IDWriteFontFileLoader> { | |
| 41 public: | |
| 42 HRESULT STDMETHODCALLTYPE FindFamilyName(const WCHAR* familyName, | |
| 43 UINT32* index, | |
| 44 BOOL* exists) override; | |
| 45 | |
| 46 HRESULT STDMETHODCALLTYPE | |
| 47 GetFontFamily(UINT32 index, IDWriteFontFamily** fontFamily) override; | |
| 48 | |
| 49 UINT32 STDMETHODCALLTYPE GetFontFamilyCount() override; | |
| 50 | |
| 51 HRESULT STDMETHODCALLTYPE GetFontFromFontFace(IDWriteFontFace* fontFace, | |
| 52 IDWriteFont** font) override; | |
| 53 | |
| 54 HRESULT STDMETHODCALLTYPE CreateEnumeratorFromKey( | |
| 55 IDWriteFactory* factory, | |
| 56 const void* collectionKey, | |
| 57 UINT32 collectionKeySize, | |
| 58 IDWriteFontFileEnumerator** fontFileEnumerator) override; | |
| 59 | |
| 60 HRESULT STDMETHODCALLTYPE | |
| 61 CreateStreamFromKey(const void* fontFileReferenceKey, | |
| 62 uint32 fontFileReferenceKeySize, | |
| 63 IDWriteFontFileStream** fontFileStream) override; | |
| 64 | |
| 65 HRESULT STDMETHODCALLTYPE | |
| 66 RuntimeClassInitialize(IDWriteFactory* factory, | |
| 67 const base::Callback<IPC::Sender*(void)>& getSender); | |
| 68 | |
| 69 void Unregister(); | |
| 70 | |
| 71 bool LoadFamily(unsigned int familyIndex, | |
| 72 IDWriteFontCollection** containingCollection); | |
| 73 | |
| 74 bool LoadFamilyNames(unsigned int familyIndex, | |
| 75 IDWriteLocalizedStrings** strings); | |
| 76 | |
| 77 bool CreateFamily(unsigned int familyIndex); | |
| 78 | |
| 79 private: | |
| 80 mswr::ComPtr<IDWriteFactory> factory_; | |
| 81 std::vector<mswr::ComPtr<DWriteFontFamilyProxy>> families_; | |
| 82 std::map<base::string16, unsigned int> familyNames_; | |
| 83 UINT32 familyCount_ = UINT_MAX; | |
| 84 base::Callback<IPC::Sender*(void)> getSender_; | |
| 85 }; | |
| 86 | |
| 87 // Implements the direct write font family interface. This class is just a | |
| 88 // stub, until something calls a method that requires actual font data. At that | |
| 89 // point this will load the font files into a custom collection and | |
| 90 // subsequently calls will be proxied to the resulting direct write object. | |
| 91 class CONTENT_EXPORT DWriteFontFamilyProxy | |
| 92 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | |
| 93 IDWriteFontFamily> { | |
| 94 public: | |
| 95 HRESULT STDMETHODCALLTYPE | |
| 96 GetFontCollection(IDWriteFontCollection** fontCollection) override; | |
| 97 | |
| 98 UINT32 STDMETHODCALLTYPE GetFontCount() override; | |
| 99 | |
| 100 HRESULT STDMETHODCALLTYPE GetFont(UINT32 index, IDWriteFont** font) override; | |
| 101 | |
| 102 HRESULT STDMETHODCALLTYPE | |
| 103 GetFamilyNames(IDWriteLocalizedStrings** names) override; | |
| 104 | |
| 105 HRESULT STDMETHODCALLTYPE | |
| 106 GetFirstMatchingFont(DWRITE_FONT_WEIGHT weight, | |
| 107 DWRITE_FONT_STRETCH stretch, | |
| 108 DWRITE_FONT_STYLE style, | |
| 109 IDWriteFont** matchingFont) override; | |
| 110 | |
| 111 HRESULT STDMETHODCALLTYPE | |
| 112 GetMatchingFonts(DWRITE_FONT_WEIGHT weight, | |
| 113 DWRITE_FONT_STRETCH stretch, | |
| 114 DWRITE_FONT_STYLE style, | |
| 115 IDWriteFontList** matchingFonts) override; | |
| 116 | |
| 117 HRESULT STDMETHODCALLTYPE | |
| 118 RuntimeClassInitialize(DWriteFontCollectionProxy* collection, | |
| 119 unsigned int index); | |
| 120 | |
| 121 bool GetFontFromFontFace(IDWriteFontFace* fontFace, IDWriteFont** font); | |
| 122 | |
| 123 void SetName(base::string16 familyName) { familyName_.assign(familyName); } | |
| 124 | |
| 125 bool IsLoaded() { return family_ != nullptr; } | |
| 126 | |
| 127 protected: | |
| 128 bool LoadFamily(); | |
| 129 | |
| 130 private: | |
| 131 unsigned int familyIndex_; | |
| 132 base::string16 familyName_; | |
| 133 mswr::ComPtr<DWriteFontCollectionProxy> proxyCollection_; | |
| 134 mswr::ComPtr<IDWriteFontFamily> family_; | |
| 135 mswr::ComPtr<IDWriteLocalizedStrings> familyNames_; | |
| 136 }; | |
| 137 | |
| 138 // Implements the direct write font file enumerator interface, backed by a list | |
| 139 // of font files. | |
| 140 class CONTENT_EXPORT FontFileEnumerator | |
| 141 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | |
| 142 IDWriteFontFileEnumerator> { | |
| 143 public: | |
| 144 HRESULT STDMETHODCALLTYPE GetCurrentFontFile(IDWriteFontFile** file) override; | |
| 145 | |
| 146 HRESULT STDMETHODCALLTYPE MoveNext(BOOL* hasCurrentFile) override; | |
| 147 | |
| 148 HRESULT STDMETHODCALLTYPE | |
| 149 RuntimeClassInitialize(IDWriteFactory* factory, | |
| 150 IDWriteFontFileLoader* loader, | |
| 151 std::vector<base::string16>* fileNames); | |
| 152 | |
| 153 private: | |
| 154 mswr::ComPtr<IDWriteFactory> factory_; | |
| 155 mswr::ComPtr<IDWriteFontFileLoader> loader_; | |
| 156 std::vector<base::string16> fileNames_; | |
| 157 std::vector<mswr::ComPtr<IDWriteFontFileStream>> fileStreams_; | |
| 158 unsigned int nextFile_ = 0; | |
| 159 unsigned int currentFile_ = UINT_MAX; | |
| 160 }; | |
| 161 | |
| 162 // Implements the direct write font file stream interface that maps the file to | |
| 163 // be loaded as a memory mapped file, and subsequently returns pointers into | |
| 164 // the mapped memory block. | |
| 165 // TODO(kulshin): confirm that using custom streams is actually an improvement | |
| 166 class CONTENT_EXPORT FontFileStream | |
| 167 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | |
| 168 IDWriteFontFileStream> { | |
| 169 public: | |
| 170 HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* fileSize) override; | |
| 171 HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* lastWriteTime) override; | |
| 172 HRESULT STDMETHODCALLTYPE ReadFileFragment(const void** fragmentStart, | |
| 173 UINT64 fileOffset, | |
| 174 UINT64 fragmentSize, | |
| 175 void** fragmentContext) override; | |
| 176 void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext) override {} | |
| 177 | |
| 178 HRESULT STDMETHODCALLTYPE | |
| 179 RuntimeClassInitialize(const base::string16& fileName); | |
| 180 | |
| 181 private: | |
| 182 base::MemoryMappedFile data_; | |
| 183 }; | |
| 184 | |
| 185 } // namespace content | |
| 186 #endif // CONTENT_COMMON_DWRITE_FONT_PROXY_WIN_H_ | |
| OLD | NEW |