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; | |
Alexei Svitkine (slow)
2015/11/25 20:17:45
No namespace decls in header files.
Ilya Kulshin
2015/12/02 02:23:01
Done.
| |
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 ~DWriteFontCollectionProxy(); | |
42 // IDWriteFontCollection: | |
Alexei Svitkine (slow)
2015/11/25 20:17:45
Add an empty line above.
Ilya Kulshin
2015/12/02 02:23:01
Done.
| |
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(unsigned int family_index, | |
72 IDWriteFontCollection** containing_collection); | |
73 | |
74 bool LoadFamilyNames(unsigned int family_index, | |
75 IDWriteLocalizedStrings** strings); | |
76 | |
77 bool CreateFamily(unsigned int family_index); | |
78 | |
79 private: | |
80 mswr::ComPtr<IDWriteFactory> factory_; | |
81 std::vector<mswr::ComPtr<DWriteFontFamilyProxy>> families_; | |
82 std::map<base::string16, unsigned int> 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 mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::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, | |
119 unsigned int index); | |
120 | |
121 bool GetFontFromFontFace(IDWriteFontFace* font_face, IDWriteFont** font); | |
122 | |
123 void SetName(base::string16 family_name) { family_name_.assign(family_name); } | |
Alexei Svitkine (slow)
2015/11/25 20:17:45
Trivial functions should be named hacker_style; no
Ilya Kulshin
2015/12/02 02:23:01
Done.
| |
124 | |
125 bool IsLoaded() { return family_ != nullptr; } | |
126 | |
127 protected: | |
128 bool LoadFamily(); | |
129 | |
130 private: | |
131 unsigned int family_index_; | |
Alexei Svitkine (slow)
2015/11/25 20:17:45
Nit: size_t?
Ilya Kulshin
2015/12/02 02:23:01
Switched to UINT32, since these indexes usually st
| |
132 base::string16 family_name_; | |
133 mswr::ComPtr<DWriteFontCollectionProxy> proxy_collection_; | |
134 mswr::ComPtr<IDWriteFontFamily> family_; | |
135 mswr::ComPtr<IDWriteLocalizedStrings> family_names_; | |
136 | |
137 DISALLOW_ASSIGN(DWriteFontFamilyProxy); | |
138 }; | |
139 | |
140 // Implements the DirectWrite font file enumerator interface, backed by a list | |
141 // of font files. | |
142 class CONTENT_EXPORT FontFileEnumerator | |
143 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::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 mswr::ComPtr<IDWriteFactory> factory_; | |
159 mswr::ComPtr<IDWriteFontFileLoader> loader_; | |
160 std::vector<base::string16> file_names_; | |
161 std::vector<mswr::ComPtr<IDWriteFontFileStream>> file_streams_; | |
162 unsigned int next_file_ = 0; | |
163 unsigned int 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 mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, | |
174 IDWriteFontFileStream> { | |
175 public: | |
176 ~FontFileStream(); | |
177 | |
178 // IDWriteFontFileStream: | |
179 HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override; | |
180 HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* last_write_time) override; | |
181 HRESULT STDMETHODCALLTYPE ReadFileFragment(const void** fragment_start, | |
182 UINT64 file_offset, | |
183 UINT64 fragment_size, | |
184 void** fragment_context) override; | |
185 void STDMETHODCALLTYPE ReleaseFileFragment(void* fragment_context) override {} | |
186 | |
187 HRESULT STDMETHODCALLTYPE | |
188 RuntimeClassInitialize(const base::string16& file_name); | |
189 | |
190 private: | |
191 base::MemoryMappedFile data_; | |
192 | |
193 DISALLOW_ASSIGN(FontFileStream); | |
194 }; | |
195 | |
196 } // namespace content | |
197 #endif // CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ | |
OLD | NEW |