Index: content/child/dwrite_font_proxy/dwrite_font_proxy_win.h |
diff --git a/content/child/dwrite_font_proxy/dwrite_font_proxy_win.h b/content/child/dwrite_font_proxy/dwrite_font_proxy_win.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5d9c9a239e006fe24a9aeade62d8c716504f509b |
--- /dev/null |
+++ b/content/child/dwrite_font_proxy/dwrite_font_proxy_win.h |
@@ -0,0 +1,186 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ |
+#define CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ |
+ |
+#include <dwrite.h> |
+#include <wrl.h> |
+ |
+#include <map> |
+#include <vector> |
+ |
+#include "base/callback.h" |
+#include "base/files/memory_mapped_file.h" |
+#include "base/macros.h" |
+#include "base/strings/string16.h" |
+#include "content/common/content_export.h" |
+ |
+namespace mswr = Microsoft::WRL; |
+ |
+namespace IPC { |
+class Sender; |
+} |
+ |
+namespace content { |
+ |
+class DWriteFontFamilyProxy; |
+ |
+// Implements a direct write font collection that uses IPC to the browser to do |
+// font enumeration. If a matching family is found, it will be loaded locally |
+// into a custom font collection. |
+// This is needed because the sandbox interferes with direct write's |
+// communication with the system font service. |
+class CONTENT_EXPORT DWriteFontCollectionProxy |
+ : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
+ IDWriteFontCollection, |
+ IDWriteFontCollectionLoader, |
+ IDWriteFontFileLoader> { |
+ public: |
+ // IDWriteFontCollection: |
+ HRESULT STDMETHODCALLTYPE FindFamilyName(const WCHAR* family_name, |
+ UINT32* index, |
+ BOOL* exists) override; |
+ HRESULT STDMETHODCALLTYPE |
+ GetFontFamily(UINT32 index, IDWriteFontFamily** font_family) override; |
+ UINT32 STDMETHODCALLTYPE GetFontFamilyCount() override; |
+ HRESULT STDMETHODCALLTYPE GetFontFromFontFace(IDWriteFontFace* font_face, |
+ IDWriteFont** font) override; |
+ |
+ // IDWriteFontCollectionLoader: |
+ HRESULT STDMETHODCALLTYPE CreateEnumeratorFromKey( |
+ IDWriteFactory* factory, |
+ const void* collection_key, |
+ UINT32 collection_key_size, |
+ IDWriteFontFileEnumerator** font_file_enumerator) override; |
+ |
+ // IDWriteFontFileLoader: |
+ HRESULT STDMETHODCALLTYPE |
+ CreateStreamFromKey(const void* font_file_reference_key, |
+ uint32 font_file_reference_key_size, |
+ IDWriteFontFileStream** font_file_stream) override; |
+ |
+ HRESULT STDMETHODCALLTYPE |
+ RuntimeClassInitialize(IDWriteFactory* factory, |
+ const base::Callback<IPC::Sender*(void)>& sender); |
+ |
+ void Unregister(); |
+ |
+ bool LoadFamily(unsigned int family_index, |
+ IDWriteFontCollection** containing_collection); |
+ |
+ bool LoadFamilyNames(unsigned int family_index, |
+ IDWriteLocalizedStrings** strings); |
+ |
+ bool CreateFamily(unsigned int family_index); |
+ |
+ private: |
+ mswr::ComPtr<IDWriteFactory> factory_; |
+ std::vector<mswr::ComPtr<DWriteFontFamilyProxy>> families_; |
+ std::map<base::string16, unsigned int> family_names_; |
+ UINT32 family_count_ = UINT_MAX; |
+ base::Callback<IPC::Sender*(void)> sender_; |
+ DISALLOW_ASSIGN(DWriteFontCollectionProxy); |
+}; |
+ |
+// Implements the direct write font family interface. This class is just a |
+// stub, until something calls a method that requires actual font data. At that |
+// point this will load the font files into a custom collection and |
+// subsequently calls will be proxied to the resulting direct write object. |
+class CONTENT_EXPORT DWriteFontFamilyProxy |
+ : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
+ IDWriteFontFamily> { |
+ public: |
+ // IDWriteFontFamily: |
+ HRESULT STDMETHODCALLTYPE |
+ GetFontCollection(IDWriteFontCollection** font_collection) override; |
+ UINT32 STDMETHODCALLTYPE GetFontCount() override; |
+ HRESULT STDMETHODCALLTYPE GetFont(UINT32 index, IDWriteFont** font) override; |
+ HRESULT STDMETHODCALLTYPE |
+ GetFamilyNames(IDWriteLocalizedStrings** names) override; |
+ HRESULT STDMETHODCALLTYPE |
+ GetFirstMatchingFont(DWRITE_FONT_WEIGHT weight, |
+ DWRITE_FONT_STRETCH stretch, |
+ DWRITE_FONT_STYLE style, |
+ IDWriteFont** matching_font) override; |
+ HRESULT STDMETHODCALLTYPE |
+ GetMatchingFonts(DWRITE_FONT_WEIGHT weight, |
+ DWRITE_FONT_STRETCH stretch, |
+ DWRITE_FONT_STYLE style, |
+ IDWriteFontList** matching_fonts) override; |
+ |
+ HRESULT STDMETHODCALLTYPE |
+ RuntimeClassInitialize(DWriteFontCollectionProxy* collection, |
+ unsigned int index); |
+ |
+ bool GetFontFromFontFace(IDWriteFontFace* font_face, IDWriteFont** font); |
+ |
+ void SetName(base::string16 family_name) { family_name_.assign(family_name); } |
+ |
+ bool IsLoaded() { return family_ != nullptr; } |
+ |
+ protected: |
+ bool LoadFamily(); |
+ |
+ private: |
+ unsigned int family_index_; |
+ base::string16 family_name_; |
+ mswr::ComPtr<DWriteFontCollectionProxy> proxy_collection_; |
+ mswr::ComPtr<IDWriteFontFamily> family_; |
+ mswr::ComPtr<IDWriteLocalizedStrings> family_names_; |
+ DISALLOW_ASSIGN(DWriteFontFamilyProxy); |
+}; |
+ |
+// Implements the direct write font file enumerator interface, backed by a list |
+// of font files. |
+class CONTENT_EXPORT FontFileEnumerator |
+ : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
+ IDWriteFontFileEnumerator> { |
+ public: |
+ // IDWriteFontFileEnumerator: |
+ HRESULT STDMETHODCALLTYPE GetCurrentFontFile(IDWriteFontFile** file) override; |
+ HRESULT STDMETHODCALLTYPE MoveNext(BOOL* has_current_file) override; |
+ |
+ HRESULT STDMETHODCALLTYPE |
+ RuntimeClassInitialize(IDWriteFactory* factory, |
+ IDWriteFontFileLoader* loader, |
+ std::vector<base::string16>* file_names); |
+ |
+ private: |
+ mswr::ComPtr<IDWriteFactory> factory_; |
+ mswr::ComPtr<IDWriteFontFileLoader> loader_; |
+ std::vector<base::string16> file_names_; |
+ std::vector<mswr::ComPtr<IDWriteFontFileStream>> file_streams_; |
+ unsigned int next_file_ = 0; |
+ unsigned int current_file_ = UINT_MAX; |
+ DISALLOW_ASSIGN(FontFileEnumerator); |
+}; |
+ |
+// Implements the direct write font file stream interface that maps the file to |
+// be loaded as a memory mapped file, and subsequently returns pointers into |
+// the mapped memory block. |
+// TODO(kulshin): confirm that using custom streams is actually an improvement |
+class CONTENT_EXPORT FontFileStream |
+ : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>, |
+ IDWriteFontFileStream> { |
+ public: |
+ // IDWriteFontFileStream: |
+ HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override; |
+ HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* last_write_time) override; |
+ HRESULT STDMETHODCALLTYPE ReadFileFragment(const void** fragment_start, |
+ UINT64 file_offset, |
+ UINT64 fragment_size, |
+ void** fragment_context) override; |
+ void STDMETHODCALLTYPE ReleaseFileFragment(void* fragment_context) override {} |
+ |
+ HRESULT STDMETHODCALLTYPE |
+ RuntimeClassInitialize(const base::string16& file_name); |
+ |
+ private: |
+ base::MemoryMappedFile data_; |
+ DISALLOW_ASSIGN(FontFileStream); |
+}; |
+ |
+} // namespace content |
+#endif // CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_ |