Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: content/child/dwrite_font_proxy/dwrite_font_proxy_win.h

Issue 1438603002: Create direct write font proxy classes and unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
21
22 namespace IPC {
23 class Sender;
24 }
25
26 namespace content {
27
28 class DWriteFontFamilyProxy;
29
30 // Implements a direct write 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 direct write'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 // IDWriteFontCollection:
42 HRESULT STDMETHODCALLTYPE FindFamilyName(const WCHAR* family_name,
43 UINT32* index,
44 BOOL* exists) override;
45 HRESULT STDMETHODCALLTYPE
46 GetFontFamily(UINT32 index, IDWriteFontFamily** font_family) override;
47 UINT32 STDMETHODCALLTYPE GetFontFamilyCount() override;
48 HRESULT STDMETHODCALLTYPE GetFontFromFontFace(IDWriteFontFace* font_face,
49 IDWriteFont** font) override;
50
51 // IDWriteFontCollectionLoader:
52 HRESULT STDMETHODCALLTYPE CreateEnumeratorFromKey(
53 IDWriteFactory* factory,
54 const void* collection_key,
55 UINT32 collection_key_size,
56 IDWriteFontFileEnumerator** font_file_enumerator) override;
57
58 // IDWriteFontFileLoader:
59 HRESULT STDMETHODCALLTYPE
60 CreateStreamFromKey(const void* font_file_reference_key,
61 uint32 font_file_reference_key_size,
62 IDWriteFontFileStream** font_file_stream) override;
63
64 HRESULT STDMETHODCALLTYPE
65 RuntimeClassInitialize(IDWriteFactory* factory,
66 const base::Callback<IPC::Sender*(void)>& sender);
67
68 void Unregister();
69
70 bool LoadFamily(unsigned int family_index,
71 IDWriteFontCollection** containing_collection);
72
73 bool LoadFamilyNames(unsigned int family_index,
74 IDWriteLocalizedStrings** strings);
75
76 bool CreateFamily(unsigned int family_index);
77
78 private:
79 mswr::ComPtr<IDWriteFactory> factory_;
80 std::vector<mswr::ComPtr<DWriteFontFamilyProxy>> families_;
81 std::map<base::string16, unsigned int> family_names_;
82 UINT32 family_count_ = UINT_MAX;
83 base::Callback<IPC::Sender*(void)> sender_;
84 DISALLOW_ASSIGN(DWriteFontCollectionProxy);
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 // IDWriteFontFamily:
96 HRESULT STDMETHODCALLTYPE
97 GetFontCollection(IDWriteFontCollection** font_collection) override;
ananta 2015/11/13 00:08:10 new line after each method and fix alignment
Ilya Kulshin 2015/11/13 00:41:11 Alignment and line wrapping are done by "git cl fo
98 UINT32 STDMETHODCALLTYPE GetFontCount() override;
99 HRESULT STDMETHODCALLTYPE GetFont(UINT32 index, IDWriteFont** font) override;
100 HRESULT STDMETHODCALLTYPE
101 GetFamilyNames(IDWriteLocalizedStrings** names) override;
102 HRESULT STDMETHODCALLTYPE
103 GetFirstMatchingFont(DWRITE_FONT_WEIGHT weight,
104 DWRITE_FONT_STRETCH stretch,
105 DWRITE_FONT_STYLE style,
106 IDWriteFont** matching_font) override;
107 HRESULT STDMETHODCALLTYPE
108 GetMatchingFonts(DWRITE_FONT_WEIGHT weight,
109 DWRITE_FONT_STRETCH stretch,
110 DWRITE_FONT_STYLE style,
111 IDWriteFontList** matching_fonts) override;
112
113 HRESULT STDMETHODCALLTYPE
114 RuntimeClassInitialize(DWriteFontCollectionProxy* collection,
115 unsigned int index);
116
117 bool GetFontFromFontFace(IDWriteFontFace* font_face, IDWriteFont** font);
118
119 void SetName(base::string16 family_name) { family_name_.assign(family_name); }
120
121 bool IsLoaded() { return family_ != nullptr; }
122
123 protected:
124 bool LoadFamily();
125
126 private:
127 unsigned int family_index_;
128 base::string16 family_name_;
129 mswr::ComPtr<DWriteFontCollectionProxy> proxy_collection_;
130 mswr::ComPtr<IDWriteFontFamily> family_;
131 mswr::ComPtr<IDWriteLocalizedStrings> family_names_;
132 DISALLOW_ASSIGN(DWriteFontFamilyProxy);
133 };
134
135 // Implements the direct write font file enumerator interface, backed by a list
136 // of font files.
137 class CONTENT_EXPORT FontFileEnumerator
138 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>,
139 IDWriteFontFileEnumerator> {
140 public:
141 // IDWriteFontFileEnumerator:
142 HRESULT STDMETHODCALLTYPE GetCurrentFontFile(IDWriteFontFile** file) override;
143 HRESULT STDMETHODCALLTYPE MoveNext(BOOL* has_current_file) override;
144
145 HRESULT STDMETHODCALLTYPE
146 RuntimeClassInitialize(IDWriteFactory* factory,
147 IDWriteFontFileLoader* loader,
148 std::vector<base::string16>* file_names);
149
150 private:
151 mswr::ComPtr<IDWriteFactory> factory_;
152 mswr::ComPtr<IDWriteFontFileLoader> loader_;
153 std::vector<base::string16> file_names_;
154 std::vector<mswr::ComPtr<IDWriteFontFileStream>> file_streams_;
155 unsigned int next_file_ = 0;
156 unsigned int current_file_ = UINT_MAX;
157 DISALLOW_ASSIGN(FontFileEnumerator);
158 };
159
160 // Implements the direct write font file stream interface that maps the file to
161 // be loaded as a memory mapped file, and subsequently returns pointers into
162 // the mapped memory block.
163 // TODO(kulshin): confirm that using custom streams is actually an improvement
164 class CONTENT_EXPORT FontFileStream
165 : public mswr::RuntimeClass<mswr::RuntimeClassFlags<mswr::ClassicCom>,
166 IDWriteFontFileStream> {
167 public:
168 // IDWriteFontFileStream:
169 HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* file_size) override;
170 HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* last_write_time) override;
171 HRESULT STDMETHODCALLTYPE ReadFileFragment(const void** fragment_start,
172 UINT64 file_offset,
173 UINT64 fragment_size,
174 void** fragment_context) override;
175 void STDMETHODCALLTYPE ReleaseFileFragment(void* fragment_context) override {}
176
177 HRESULT STDMETHODCALLTYPE
178 RuntimeClassInitialize(const base::string16& file_name);
179
180 private:
181 base::MemoryMappedFile data_;
182 DISALLOW_ASSIGN(FontFileStream);
183 };
184
185 } // namespace content
186 #endif // CONTENT_CHILD_DWRITE_FONT_PROXY_DWRITE_FONT_PROXY_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698