Debian Codegraph @

third_party/rust/dwrote/src/font_file_loader_impl.rs

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