| 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::mem; |
| 7 |
use std::ptr; |
| 8 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 9 |
use winapi::shared::minwindef::{BOOL, FALSE, TRUE}; |
| 10 |
use winapi::shared::winerror::S_OK; |
| 11 |
use winapi::um::dwrite::IDWriteFontCollectionLoader; |
| 12 |
use winapi::um::dwrite::{IDWriteFont, IDWriteFontCollection, IDWriteFontFamily}; |
| 13 |
use wio::com::ComPtr; |
| 14 |
|
| 15 |
use crate::helpers::*; |
| 16 |
use super::{DWriteFactory, Font, FontDescriptor, FontFace, FontFamily}; |
| 17 |
|
| 18 |
static NEXT_ID: AtomicUsize = AtomicUsize::new(0); |
| 19 |
|
| 20 |
pub struct FontCollectionFamilyIterator { |
| 21 |
collection: ComPtr<IDWriteFontCollection>, |
| 22 |
curr: u32, |
| 23 |
count: u32, |
| 24 |
} |
| 25 |
|
| 26 |
impl Iterator for FontCollectionFamilyIterator { |
| 27 |
type Item = FontFamily; |
| 28 |
fn next(&mut self) -> Option<FontFamily> { |
| 29 |
if self.curr == self.count { |
| 30 |
return None; |
| 31 |
} |
| 32 |
|
| 33 |
unsafe { |
| 34 |
let mut family: *mut IDWriteFontFamily = ptr::null_mut(); |
| 35 |
let hr = self.collection.GetFontFamily(self.curr, &mut family); |
| 36 |
assert!(hr == 0); |
| 37 |
self.curr += 1; |
| 38 |
Some(FontFamily::take(ComPtr::from_raw(family))) |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
pub struct FontCollection { |
| 44 |
native: UnsafeCell<ComPtr<IDWriteFontCollection>>, |
| 45 |
} |
| 46 |
|
| 47 |
impl FontCollection { |
| 48 |
pub fn get_system(update: bool) -> FontCollection { |
| 49 |
unsafe { |
| 50 |
let mut native: *mut IDWriteFontCollection = ptr::null_mut(); |
| 51 |
let hr = (*DWriteFactory()).GetSystemFontCollection( |
| 52 |
&mut native, |
| 53 |
if update { TRUE } else { FALSE }, |
| 54 |
); |
| 55 |
assert!(hr == 0); |
| 56 |
|
| 57 |
FontCollection { |
| 58 |
native: UnsafeCell::new(ComPtr::from_raw(native)), |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub fn system() -> FontCollection { |
| 64 |
FontCollection::get_system(false) |
| 65 |
} |
| 66 |
|
| 67 |
pub fn take(native: ComPtr<IDWriteFontCollection>) -> FontCollection { |
| 68 |
FontCollection { |
| 69 |
native: UnsafeCell::new(native), |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
pub fn from_loader(collection_loader: ComPtr<IDWriteFontCollectionLoader>) -> FontCollection { |
| 74 |
unsafe { |
| 75 |
let factory = DWriteFactory(); |
| 76 |
assert_eq!( |
| 77 |
(*factory).RegisterFontCollectionLoader(collection_loader.clone().into_raw()), |
| 78 |
S_OK |
| 79 |
); |
| 80 |
let mut collection: *mut IDWriteFontCollection = ptr::null_mut(); |
| 81 |
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst); |
| 82 |
assert_eq!( |
| 83 |
(*factory).CreateCustomFontCollection( |
| 84 |
collection_loader.clone().into_raw(), |
| 85 |
&id as *const usize as *const _, |
| 86 |
mem::size_of::<AtomicUsize>() as u32, |
| 87 |
&mut collection |
| 88 |
), |
| 89 |
S_OK |
| 90 |
); |
| 91 |
FontCollection::take(ComPtr::from_raw(collection)) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
pub unsafe fn as_ptr(&self) -> *mut IDWriteFontCollection { |
| 96 |
(*self.native.get()).as_raw() |
| 97 |
} |
| 98 |
|
| 99 |
pub fn families_iter(&self) -> FontCollectionFamilyIterator { |
| 100 |
unsafe { |
| 101 |
FontCollectionFamilyIterator { |
| 102 |
collection: (*self.native.get()).clone(), |
| 103 |
curr: 0, |
| 104 |
count: (*self.native.get()).GetFontFamilyCount(), |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
pub fn get_font_family_count(&self) -> u32 { |
| 110 |
unsafe { (*self.native.get()).GetFontFamilyCount() } |
| 111 |
} |
| 112 |
|
| 113 |
pub fn get_font_family(&self, index: u32) -> FontFamily { |
| 114 |
unsafe { |
| 115 |
let mut family: *mut IDWriteFontFamily = ptr::null_mut(); |
| 116 |
let hr = (*self.native.get()).GetFontFamily(index, &mut family); |
| 117 |
assert!(hr == 0); |
| 118 |
FontFamily::take(ComPtr::from_raw(family)) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Find a font matching the given font descriptor in this |
| 123 |
// font collection. |
| 124 |
pub fn get_font_from_descriptor(&self, desc: &FontDescriptor) -> Option<Font> { |
| 125 |
if let Some(family) = self.get_font_family_by_name(&desc.family_name) { |
| 126 |
let font = family.get_first_matching_font(desc.weight, desc.stretch, desc.style); |
| 127 |
// Exact matches only here |
| 128 |
if font.weight() == desc.weight |
| 129 |
&& font.stretch() == desc.stretch |
| 130 |
&& font.style() == desc.style |
| 131 |
{ |
| 132 |
return Some(font); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
None |
| 137 |
} |
| 138 |
|
| 139 |
pub fn get_font_from_face(&self, face: &FontFace) -> Option<Font> { |
| 140 |
unsafe { |
| 141 |
let mut font: *mut IDWriteFont = ptr::null_mut(); |
| 142 |
let hr = (*self.native.get()).GetFontFromFontFace(face.as_ptr(), &mut font); |
| 143 |
if hr != 0 { |
| 144 |
return None; |
| 145 |
} |
| 146 |
Some(Font::take(ComPtr::from_raw(font))) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
pub fn get_font_family_by_name(&self, family_name: &str) -> Option<FontFamily> { |
| 151 |
unsafe { |
| 152 |
let mut index: u32 = 0; |
| 153 |
let mut exists: BOOL = FALSE; |
| 154 |
let hr = (*self.native.get()).FindFamilyName( |
| 155 |
family_name.to_wide_null().as_ptr(), |
| 156 |
&mut index, |
| 157 |
&mut exists, |
| 158 |
); |
| 159 |
assert!(hr == 0); |
| 160 |
if exists == FALSE { |
| 161 |
return None; |
| 162 |
} |
| 163 |
|
| 164 |
let mut family: *mut IDWriteFontFamily = ptr::null_mut(); |
| 165 |
let hr = (*self.native.get()).GetFontFamily(index, &mut family); |
| 166 |
assert!(hr == 0); |
| 167 |
|
| 168 |
Some(FontFamily::take(ComPtr::from_raw(family))) |
| 169 |
} |
| 170 |
} |
| 171 |
} |