| 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::ptr; |
| 7 |
use winapi::um::dwrite::IDWriteLocalizedStrings; |
| 8 |
use winapi::um::dwrite::{IDWriteFont, IDWriteFontCollection, IDWriteFontFamily}; |
| 9 |
use wio::com::ComPtr; |
| 10 |
|
| 11 |
use super::*; |
| 12 |
use helpers::*; |
| 13 |
|
| 14 |
pub struct FontFamily { |
| 15 |
native: UnsafeCell<ComPtr<IDWriteFontFamily>>, |
| 16 |
} |
| 17 |
|
| 18 |
impl FontFamily { |
| 19 |
pub fn take(native: ComPtr<IDWriteFontFamily>) -> FontFamily { |
| 20 |
FontFamily { |
| 21 |
native: UnsafeCell::new(native), |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
pub unsafe fn as_ptr(&self) -> *mut IDWriteFontFamily { |
| 26 |
(*self.native.get()).as_raw() |
| 27 |
} |
| 28 |
|
| 29 |
pub fn name(&self) -> String { |
| 30 |
unsafe { |
| 31 |
let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut(); |
| 32 |
let hr = (*self.native.get()).GetFamilyNames(&mut family_names); |
| 33 |
assert!(hr == 0); |
| 34 |
|
| 35 |
get_locale_string(&mut ComPtr::from_raw(family_names)) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
pub fn get_first_matching_font( |
| 40 |
&self, |
| 41 |
weight: FontWeight, |
| 42 |
stretch: FontStretch, |
| 43 |
style: FontStyle, |
| 44 |
) -> Font { |
| 45 |
unsafe { |
| 46 |
let mut font: *mut IDWriteFont = ptr::null_mut(); |
| 47 |
let hr = (*self.native.get()).GetFirstMatchingFont( |
| 48 |
weight.t(), |
| 49 |
stretch.t(), |
| 50 |
style.t(), |
| 51 |
&mut font, |
| 52 |
); |
| 53 |
assert!(hr == 0); |
| 54 |
Font::take(ComPtr::from_raw(font)) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
pub fn get_font_collection(&self) -> FontCollection { |
| 59 |
unsafe { |
| 60 |
let mut collection: *mut IDWriteFontCollection = ptr::null_mut(); |
| 61 |
let hr = (*self.native.get()).GetFontCollection(&mut collection); |
| 62 |
assert!(hr == 0); |
| 63 |
FontCollection::take(ComPtr::from_raw(collection)) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub fn get_font_count(&self) -> u32 { |
| 68 |
unsafe { (*self.native.get()).GetFontCount() } |
| 69 |
} |
| 70 |
|
| 71 |
pub fn get_font(&self, index: u32) -> Font { |
| 72 |
unsafe { |
| 73 |
let mut font: *mut IDWriteFont = ptr::null_mut(); |
| 74 |
let hr = (*self.native.get()).GetFont(index, &mut font); |
| 75 |
assert!(hr == 0); |
| 76 |
Font::take(ComPtr::from_raw(font)) |
| 77 |
} |
| 78 |
} |
| 79 |
} |