Debian Codegraph @

third_party/rust/dwrote/src/font_file.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use std::cell::UnsafeCell;
6 use std::ffi::OsString;
7 use std::os::windows::ffi::{OsStrExt, OsStringExt};
8 use std::path::Path;
9 use std::path::PathBuf;
10 use std::ptr;
11 use std::slice;
12 use std::sync::Arc;
13 use winapi::ctypes::c_void;
14 use winapi::um::dwrite::{IDWriteFontFace, IDWriteFontFile, IDWriteFontFileStream};
15 use winapi::um::dwrite::{IDWriteFontFileLoader, IDWriteLocalFontFileLoader};
16 use winapi::um::dwrite::{DWRITE_FONT_FACE_TYPE, DWRITE_FONT_FILE_TYPE_UNKNOWN};
17 use winapi::um::dwrite::{DWRITE_FONT_FACE_TYPE_UNKNOWN, DWRITE_FONT_SIMULATIONS};
18 use winapi::um::winnt::HRESULT;
19 use wio::com::ComPtr;
20
21 use super::DWriteFactory;
22 use crate::font_face::FontFace;
23 use crate::font_file_loader_impl::DataFontHelper;
24
25 pub struct FontFile {
26 native: UnsafeCell<ComPtr<IDWriteFontFile>>,
27 stream: UnsafeCell<Option<ComPtr<IDWriteFontFileStream>>>,
28 data_key: usize,
29 face_type: DWRITE_FONT_FACE_TYPE,
30 }
31
32 impl FontFile {
33 pub fn new_from_path<P>(path: P) -> Option<FontFile>
34 where
35 P: AsRef<Path>,
36 {
37 unsafe {
38 let mut path: Vec<u16> = path.as_ref().as_os_str().encode_wide().collect();
39 path.push(0);
40
41 let mut font_file: *mut IDWriteFontFile = ptr::null_mut();
42 let hr = (*DWriteFactory()).CreateFontFileReference(
43 path.as_ptr(),
44 ptr::null(),
45 &mut font_file,
46 );
47 if hr != 0 || font_file.is_null() {
48 return None;
49 }
50
51 let mut ff = FontFile {
52 native: UnsafeCell::new(ComPtr::from_raw(font_file)),
53 stream: UnsafeCell::new(None),
54 data_key: 0,
55 face_type: DWRITE_FONT_FACE_TYPE_UNKNOWN,
56 };
57
58 if ff.analyze() == 0 {
59 None
60 } else {
61 Some(ff)
62 }
63 }
64 }
65
66 pub fn new_from_data(data: Arc<Vec<u8>>) -> Option<FontFile> {
67 let (font_file, font_file_stream, key) = DataFontHelper::register_font_data(data);
68
69 let mut ff = FontFile {
70 native: UnsafeCell::new(font_file),
71 stream: UnsafeCell::new(Some(font_file_stream)),
72 data_key: key,
73 face_type: DWRITE_FONT_FACE_TYPE_UNKNOWN,
74 };
75
76 if ff.analyze() == 0 {
77 None
78 } else {
79 Some(ff)
80 }
81 }
82
83 pub fn analyze_data(data: Arc<Vec<u8>>) -> u32 {
84 let (font_file, font_file_stream, key) = DataFontHelper::register_font_data(data);
85
86 let mut ff = FontFile {
87 native: UnsafeCell::new(font_file),
88 stream: UnsafeCell::new(Some(font_file_stream)),
89 data_key: key,
90 face_type: DWRITE_FONT_FACE_TYPE_UNKNOWN,
91 };
92
93 ff.analyze()
94 }
95
96 fn analyze(&mut self) -> u32 {
97 let mut face_type = DWRITE_FONT_FACE_TYPE_UNKNOWN;
98 let mut num_faces = 0;
99 unsafe {
100 let mut supported = 0;
101 let mut _file_type = DWRITE_FONT_FILE_TYPE_UNKNOWN;
102
103 let hr = (*self.native.get()).Analyze(
104 &mut supported,
105 &mut _file_type,
106 &mut face_type,
107 &mut num_faces,
108 );
109 if hr != 0 || supported == 0 {
110 return 0;
111 }
112 }
113 self.face_type = face_type;
114 num_faces as u32
115 }
116
117 pub fn take(native: ComPtr<IDWriteFontFile>) -> FontFile {
118 let mut ff = FontFile {
119 native: UnsafeCell::new(native),
120 stream: UnsafeCell::new(None),
121 data_key: 0,
122 face_type: DWRITE_FONT_FACE_TYPE_UNKNOWN,
123 };
124 ff.analyze();
125 ff
126 }
127
128 pub fn data_key(&self) -> Option<usize> {
129 if self.data_key != 0 {
130 Some(self.data_key)
131 } else {
132 None
133 }
134 }
135
136 pub(crate) unsafe fn as_com_ptr(&self) -> ComPtr<IDWriteFontFile> {
137 (*self.native.get()).clone()
138 }
139
140 // This is a helper to read the contents of this FontFile,
141 // without requiring callers to deal with loaders, keys,
142 // or streams.
143 pub fn get_font_file_bytes(&self) -> Vec<u8> {
144 unsafe {
145 let mut ref_key: *const c_void = ptr::null();
146 let mut ref_key_size: u32 = 0;
147 let hr = (*self.native.get()).GetReferenceKey(&mut ref_key, &mut ref_key_size);
148 assert!(hr == 0);
149
150 let mut loader: *mut IDWriteFontFileLoader = ptr::null_mut();
151 let hr = (*self.native.get()).GetLoader(&mut loader);
152 assert!(hr == 0);
153 let loader = ComPtr::from_raw(loader);
154
155 let mut stream: *mut IDWriteFontFileStream = ptr::null_mut();
156 let hr = loader.CreateStreamFromKey(ref_key, ref_key_size, &mut stream);
157 assert!(hr == 0);
158 let stream = ComPtr::from_raw(stream);
159
160 let mut file_size: u64 = 0;
161 let hr = stream.GetFileSize(&mut file_size);
162 assert!(hr == 0);
163
164 let mut fragment_start: *const c_void = ptr::null();
165 let mut fragment_context: *mut c_void = ptr::null_mut();
166 let hr =
167 stream.ReadFileFragment(&mut fragment_start, 0, file_size, &mut fragment_context);
168 assert!(hr == 0);
169
170 let in_ptr = slice::from_raw_parts(fragment_start as *const u8, file_size as usize);
171 let bytes = in_ptr.to_vec();
172
173 stream.ReleaseFileFragment(fragment_context);
174
175 bytes
176 }
177 }
178
179 // This is a helper to get the path of a font file,
180 // without requiring callers to deal with loaders.
181 pub fn get_font_file_path(&self) -> Option<PathBuf> {
182 unsafe {
183 let mut ref_key: *const c_void = ptr::null();
184 let mut ref_key_size: u32 = 0;
185 let hr = (*self.native.get()).GetReferenceKey(&mut ref_key, &mut ref_key_size);
186 assert!(hr == 0);
187
188 let mut loader: *mut IDWriteFontFileLoader = ptr::null_mut();
189 let hr = (*self.native.get()).GetLoader(&mut loader);
190 assert!(hr == 0);
191 let loader = ComPtr::from_raw(loader);
192
193 let local_loader: ComPtr<IDWriteLocalFontFileLoader> = match loader.cast() {
194 Ok(local_loader) => local_loader,
195 Err(_) => return None,
196 };
197
198 let mut file_path_len = 0;
199 let hr =
200 local_loader.GetFilePathLengthFromKey(ref_key, ref_key_size, &mut file_path_len);
201 assert_eq!(hr, 0);
202
203 let mut file_path_buf = vec![0; file_path_len as usize + 1];
204 let hr = local_loader.GetFilePathFromKey(
205 ref_key,
206 ref_key_size,
207 file_path_buf.as_mut_ptr(),
208 file_path_len + 1,
209 );
210 assert_eq!(hr, 0);
211
212 if let Some(&0) = file_path_buf.last() {
213 file_path_buf.pop();
214 }
215
216 Some(PathBuf::from(OsString::from_wide(&file_path_buf)))
217 }
218 }
219
220 pub fn create_face(
221 &self,
222 face_index: u32,
223 simulations: DWRITE_FONT_SIMULATIONS,
224 ) -> Result<FontFace, HRESULT> {
225 unsafe {
226 let mut face: *mut IDWriteFontFace = ptr::null_mut();
227 let ptr = self.as_com_ptr();
228 let hr = (*DWriteFactory()).CreateFontFace(
229 self.face_type,
230 1,
231 &ptr.as_raw(),
232 face_index,
233 simulations,
234 &mut face,
235 );
236 if hr != 0 {
237 Err(hr)
238 } else {
239 Ok(FontFace::take(ComPtr::from_raw(face)))
240 }
241 }
242 }
243 }
244
245 impl Clone for FontFile {
246 fn clone(&self) -> FontFile {
247 unsafe {
248 FontFile {
249 native: UnsafeCell::new((*self.native.get()).clone()),
250 stream: UnsafeCell::new((*self.stream.get()).clone()),
251 data_key: self.data_key,
252 face_type: self.face_type,
253 }
254 }
255 }
256 }