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