directwrite/font_file/
builder.rs

1use error::DWriteError;
2use font_file::FontFile;
3
4use std::ptr;
5
6use winapi::shared::minwindef::FILETIME;
7use winapi::shared::winerror::SUCCEEDED;
8use winapi::um::dwrite::{IDWriteFactory, IDWriteFontFile};
9use wio::com::ComPtr;
10use wio::wide::ToWide;
11
12pub struct FontFileBuilder<'a> {
13    factory: &'a IDWriteFactory,
14    file_path: Option<&'a str>,
15    last_write_time: Option<FILETIME>,
16}
17
18impl<'a> FontFileBuilder<'a> {
19    pub fn new(factory: &'a IDWriteFactory) -> FontFileBuilder<'a> {
20        FontFileBuilder {
21            factory,
22            file_path: None,
23            last_write_time: None,
24        }
25    }
26
27    pub fn build(self) -> Result<FontFile, DWriteError> {
28        unsafe {
29            let file_path = self
30                .file_path
31                .expect("`file_path` must be specified")
32                .to_wide_null();
33            let last_write_time = match self.last_write_time {
34                Some(t) => &t,
35                None => ptr::null(),
36            };
37
38            let mut ptr: *mut IDWriteFontFile = ptr::null_mut();
39            let result =
40                self.factory
41                    .CreateFontFileReference(file_path.as_ptr(), last_write_time, &mut ptr);
42
43            if SUCCEEDED(result) {
44                let ptr = ComPtr::from_raw(ptr);
45                Ok(FontFile { ptr: ptr })
46            } else {
47                Err(From::from(result))
48            }
49        }
50    }
51
52    pub fn with_file_path(mut self, file_path: &'a str) -> Self {
53        self.file_path = Some(file_path);
54        self
55    }
56}