1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#![allow(non_snake_case, non_upper_case_globals)]
use std::collections::HashMap;
use std::marker::Send;
use std::sync::atomic::AtomicUsize;
use std::sync::{atomic, Arc, Mutex};
use std::{mem, ptr};
use winapi::ctypes::c_void;
use winapi::shared::basetsd::{UINT32, UINT64};
use winapi::shared::guiddef::REFIID;
use winapi::shared::minwindef::ULONG;
use winapi::shared::winerror::{E_FAIL, E_INVALIDARG, E_NOTIMPL, S_OK};
use winapi::um::dwrite::IDWriteFontFile;
use winapi::um::dwrite::{IDWriteFontFileLoader, IDWriteFontFileLoaderVtbl};
use winapi::um::dwrite::{IDWriteFontFileStream, IDWriteFontFileStreamVtbl};
use winapi::um::unknwnbase::{IUnknown, IUnknownVtbl};
use winapi::um::winnt::HRESULT;
use wio::com::ComPtr;
use super::DWriteFactory;
use crate::com_helpers::*;
struct FontFileLoader;
const FontFileLoaderVtbl: &'static IDWriteFontFileLoaderVtbl = &IDWriteFontFileLoaderVtbl {
parent: implement_iunknown!(static IDWriteFontFileLoader, FontFileLoader),
CreateStreamFromKey: {
unsafe extern "system" fn CreateStreamFromKey(
_This: *mut IDWriteFontFileLoader,
fontFileReferenceKey: *const c_void,
fontFileReferenceKeySize: UINT32,
fontFileStream: *mut *mut IDWriteFontFileStream,
) -> HRESULT {
if fontFileReferenceKey.is_null() || fontFileStream.is_null() {
return E_INVALIDARG;
}
assert!(fontFileReferenceKeySize == mem::size_of::<usize>() as UINT32);
let key = *(fontFileReferenceKey as *const usize);
let stream = match FONT_FILE_STREAM_MAP.lock().unwrap().get(&key) {
None => {
*fontFileStream = ptr::null_mut();
return E_FAIL;
}
Some(&FontFileStreamPtr(file_stream)) => file_stream,
};
// This is an addref getter, so make sure to do that!
(*stream).AddRef();
*fontFileStream = stream;
S_OK
}
CreateStreamFromKey
},
};
impl Com<IDWriteFontFileLoader> for FontFileLoader {
type Vtbl = IDWriteFontFileLoaderVtbl;
fn vtbl() -> &'static IDWriteFontFileLoaderVtbl {
FontFileLoaderVtbl
}
}
impl Com<IUnknown> for FontFileLoader {
type Vtbl = IUnknownVtbl;
fn vtbl() -> &'static IUnknownVtbl {
&FontFileLoaderVtbl.parent
}
}
impl FontFileLoader {
pub fn new() -> FontFileLoader {
FontFileLoader
}
}
unsafe impl Send for FontFileLoader {}
unsafe impl Sync for FontFileLoader {}
struct FontFileStream {
refcount: atomic::AtomicUsize,
key: usize,
data: Arc<Vec<u8>>,
}
const FontFileStreamVtbl: &'static IDWriteFontFileStreamVtbl = &IDWriteFontFileStreamVtbl {
parent: implement_iunknown!(IDWriteFontFileStream, FontFileStream),
ReadFileFragment: {
unsafe extern "system" fn ReadFileFragment(
This: *mut IDWriteFontFileStream,
fragmentStart: *mut *const c_void,
fileOffset: UINT64,
fragmentSize: UINT64,
fragmentContext: *mut *mut c_void,
) -> HRESULT {
let this = FontFileStream::from_interface(This);
*fragmentContext = ptr::null_mut();
if (fileOffset + fragmentSize) as usize > this.data.len() {
return E_INVALIDARG;
}
let index = fileOffset as usize;
*fragmentStart = this.data[index..].as_ptr() as *const c_void;
S_OK
}
ReadFileFragment
},
ReleaseFileFragment: {
unsafe extern "system" fn ReleaseFileFragment(
_This: *mut IDWriteFontFileStream,
_fragmentContext: *mut c_void,
) {
}
ReleaseFileFragment
},
GetFileSize: {
unsafe extern "system" fn GetFileSize(
This: *mut IDWriteFontFileStream,
fileSize: *mut UINT64,
) -> HRESULT {
let this = FontFileStream::from_interface(This);
*fileSize = this.data.len() as UINT64;
S_OK
}
GetFileSize
},
GetLastWriteTime: {
unsafe extern "system" fn GetLastWriteTime(
_This: *mut IDWriteFontFileStream,
_lastWriteTime: *mut UINT64,
) -> HRESULT {
E_NOTIMPL
}
GetLastWriteTime
},
};
impl FontFileStream {
pub fn new(key: usize, data: Arc<Vec<u8>>) -> FontFileStream {
FontFileStream {
refcount: AtomicUsize::new(1),
key,
data,
}
}
}
impl Drop for FontFileStream {
fn drop(&mut self) {
DataFontHelper::unregister_font_data(self.key);
}
}
impl Com<IDWriteFontFileStream> for FontFileStream {
type Vtbl = IDWriteFontFileStreamVtbl;
fn vtbl() -> &'static IDWriteFontFileStreamVtbl {
FontFileStreamVtbl
}
}
impl Com<IUnknown> for FontFileStream {
type Vtbl = IUnknownVtbl;
fn vtbl() -> &'static IUnknownVtbl {
&FontFileStreamVtbl.parent
}
}
struct FontFileStreamPtr(*mut IDWriteFontFileStream);
unsafe impl Send for FontFileStreamPtr {}
static mut FONT_FILE_KEY: atomic::AtomicUsize = AtomicUsize::new(0);
#[derive(Clone)]
struct FontFileLoaderWrapper(ComPtr<IDWriteFontFileLoader>);
unsafe impl Send for FontFileLoaderWrapper {}
unsafe impl Sync for FontFileLoaderWrapper {}
lazy_static! {
static ref FONT_FILE_STREAM_MAP: Mutex<HashMap<usize, FontFileStreamPtr>> =
{ Mutex::new(HashMap::new()) };
static ref FONT_FILE_LOADER: Mutex<FontFileLoaderWrapper> = {
unsafe {
let ffl_native = FontFileLoader::new();
let ffl = ComPtr::<IDWriteFontFileLoader>::from_raw(ffl_native.into_interface());
let hr = (*DWriteFactory()).RegisterFontFileLoader(ffl.as_raw());
assert!(hr == 0);
Mutex::new(FontFileLoaderWrapper(ffl))
}
};
}
pub struct DataFontHelper;
impl DataFontHelper {
pub fn register_font_data(
font_data: Arc<Vec<u8>>,
) -> (
ComPtr<IDWriteFontFile>,
ComPtr<IDWriteFontFileStream>,
usize,
) {
unsafe {
let key = FONT_FILE_KEY.fetch_add(1, atomic::Ordering::Relaxed);
let font_file_stream_native = FontFileStream::new(key, font_data);
let font_file_stream: ComPtr<IDWriteFontFileStream> =
ComPtr::from_raw(font_file_stream_native.into_interface());
{
let mut map = FONT_FILE_STREAM_MAP.lock().unwrap();
map.insert(key, FontFileStreamPtr(font_file_stream.as_raw()));
}
let mut font_file: *mut IDWriteFontFile = ptr::null_mut();
{
let loader = FONT_FILE_LOADER.lock().unwrap();
let hr = (*DWriteFactory()).CreateCustomFontFileReference(
mem::transmute(&key),
mem::size_of::<usize>() as UINT32,
loader.0.as_raw(),
&mut font_file,
);
assert!(hr == S_OK);
}
let font_file = ComPtr::from_raw(font_file);
(font_file, font_file_stream, key)
}
}
fn unregister_font_data(key: usize) {
let mut map = FONT_FILE_STREAM_MAP.lock().unwrap();
if map.remove(&key).is_none() {
panic!("unregister_font_data: trying to unregister key that is no longer registered");
}
}
}
|