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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::cell::UnsafeCell;
use std::mem;
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use winapi::shared::minwindef::{BOOL, FALSE, TRUE};
use winapi::shared::winerror::S_OK;
use winapi::um::dwrite::IDWriteFontCollectionLoader;
use winapi::um::dwrite::{IDWriteFont, IDWriteFontCollection, IDWriteFontFamily};
use wio::com::ComPtr;
use crate::helpers::*;
use super::{DWriteFactory, Font, FontDescriptor, FontFace, FontFamily};
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
pub struct FontCollectionFamilyIterator {
collection: ComPtr<IDWriteFontCollection>,
curr: u32,
count: u32,
}
impl Iterator for FontCollectionFamilyIterator {
type Item = FontFamily;
fn next(&mut self) -> Option<FontFamily> {
if self.curr == self.count {
return None;
}
unsafe {
let mut family: *mut IDWriteFontFamily = ptr::null_mut();
let hr = self.collection.GetFontFamily(self.curr, &mut family);
assert!(hr == 0);
self.curr += 1;
Some(FontFamily::take(ComPtr::from_raw(family)))
}
}
}
pub struct FontCollection {
native: UnsafeCell<ComPtr<IDWriteFontCollection>>,
}
impl FontCollection {
pub fn get_system(update: bool) -> FontCollection {
unsafe {
let mut native: *mut IDWriteFontCollection = ptr::null_mut();
let hr = (*DWriteFactory()).GetSystemFontCollection(
&mut native,
if update { TRUE } else { FALSE },
);
assert!(hr == 0);
FontCollection {
native: UnsafeCell::new(ComPtr::from_raw(native)),
}
}
}
pub fn system() -> FontCollection {
FontCollection::get_system(false)
}
pub fn take(native: ComPtr<IDWriteFontCollection>) -> FontCollection {
FontCollection {
native: UnsafeCell::new(native),
}
}
pub fn from_loader(collection_loader: ComPtr<IDWriteFontCollectionLoader>) -> FontCollection {
unsafe {
let factory = DWriteFactory();
assert_eq!(
(*factory).RegisterFontCollectionLoader(collection_loader.clone().into_raw()),
S_OK
);
let mut collection: *mut IDWriteFontCollection = ptr::null_mut();
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
assert_eq!(
(*factory).CreateCustomFontCollection(
collection_loader.clone().into_raw(),
&id as *const usize as *const _,
mem::size_of::<AtomicUsize>() as u32,
&mut collection
),
S_OK
);
FontCollection::take(ComPtr::from_raw(collection))
}
}
pub unsafe fn as_ptr(&self) -> *mut IDWriteFontCollection {
(*self.native.get()).as_raw()
}
pub fn families_iter(&self) -> FontCollectionFamilyIterator {
unsafe {
FontCollectionFamilyIterator {
collection: (*self.native.get()).clone(),
curr: 0,
count: (*self.native.get()).GetFontFamilyCount(),
}
}
}
pub fn get_font_family_count(&self) -> u32 {
unsafe { (*self.native.get()).GetFontFamilyCount() }
}
pub fn get_font_family(&self, index: u32) -> FontFamily {
unsafe {
let mut family: *mut IDWriteFontFamily = ptr::null_mut();
let hr = (*self.native.get()).GetFontFamily(index, &mut family);
assert!(hr == 0);
FontFamily::take(ComPtr::from_raw(family))
}
}
// Find a font matching the given font descriptor in this
// font collection.
pub fn get_font_from_descriptor(&self, desc: &FontDescriptor) -> Option<Font> {
if let Some(family) = self.get_font_family_by_name(&desc.family_name) {
let font = family.get_first_matching_font(desc.weight, desc.stretch, desc.style);
// Exact matches only here
if font.weight() == desc.weight
&& font.stretch() == desc.stretch
&& font.style() == desc.style
{
return Some(font);
}
}
None
}
pub fn get_font_from_face(&self, face: &FontFace) -> Option<Font> {
unsafe {
let mut font: *mut IDWriteFont = ptr::null_mut();
let hr = (*self.native.get()).GetFontFromFontFace(face.as_ptr(), &mut font);
if hr != 0 {
return None;
}
Some(Font::take(ComPtr::from_raw(font)))
}
}
pub fn get_font_family_by_name(&self, family_name: &str) -> Option<FontFamily> {
unsafe {
let mut index: u32 = 0;
let mut exists: BOOL = FALSE;
let hr = (*self.native.get()).FindFamilyName(
family_name.to_wide_null().as_ptr(),
&mut index,
&mut exists,
);
assert!(hr == 0);
if exists == FALSE {
return None;
}
let mut family: *mut IDWriteFontFamily = ptr::null_mut();
let hr = (*self.native.get()).GetFontFamily(index, &mut family);
assert!(hr == 0);
Some(FontFamily::take(ComPtr::from_raw(family)))
}
}
}
|