diff --git a/src/font_family.rs b/src/font_family.rs index 72f13b6..78dadaa 100644 --- a/src/font_family.rs +++ b/src/font_family.rs @@ -26,41 +26,66 @@ impl FontFamily { (*self.native.get()).as_raw() } + #[deprecated(note = "Use `family_name` instead.")] pub fn name(&self) -> String { + self.family_name().unwrap() + } + + pub fn family_name(&self) -> Result { + let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut(); unsafe { - let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut(); let hr = (*self.native.get()).GetFamilyNames(&mut family_names); - assert!(hr == 0); - - get_locale_string(&mut ComPtr::from_raw(family_names)) + if hr != 0 { + return Err(hr); + } + Ok(get_locale_string(&mut ComPtr::from_raw(family_names))) } } + #[deprecated(note = "Use `first_matching_font` instead.")] pub fn get_first_matching_font( &self, weight: FontWeight, stretch: FontStretch, style: FontStyle, ) -> Font { + self.first_matching_font(weight, stretch, style).unwrap() + } + + pub fn first_matching_font( + &self, + weight: FontWeight, + stretch: FontStretch, + style: FontStyle, + ) -> Result { + let mut font: *mut IDWriteFont = ptr::null_mut(); unsafe { - let mut font: *mut IDWriteFont = ptr::null_mut(); let hr = (*self.native.get()).GetFirstMatchingFont( weight.t(), stretch.t(), style.t(), &mut font, ); - assert!(hr == 0); - Font::take(ComPtr::from_raw(font)) + if hr != 0 { + return Err(hr); + } + Ok(Font::take(ComPtr::from_raw(font))) } } + #[deprecated(note = "Use `font_collection` instead.")] pub fn get_font_collection(&self) -> FontCollection { + self.font_collection().unwrap() + } + + pub fn font_collection(&self) -> Result { + let mut collection: *mut IDWriteFontCollection = ptr::null_mut(); unsafe { - let mut collection: *mut IDWriteFontCollection = ptr::null_mut(); let hr = (*self.native.get()).GetFontCollection(&mut collection); - assert!(hr == 0); - FontCollection::take(ComPtr::from_raw(collection)) + if hr != 0 { + return Err(hr); + } + Ok(FontCollection::take(ComPtr::from_raw(collection))) } } @@ -68,12 +93,19 @@ impl FontFamily { unsafe { (*self.native.get()).GetFontCount() } } + #[deprecated(note = "Use `font` instead.")] pub fn get_font(&self, index: u32) -> Font { + self.font(index).unwrap() + } + + pub fn font(&self, index: u32) -> Result { + let mut font: *mut IDWriteFont = ptr::null_mut(); unsafe { - let mut font: *mut IDWriteFont = ptr::null_mut(); let hr = (*self.native.get()).GetFont(index, &mut font); - assert!(hr == 0); - Font::take(ComPtr::from_raw(font)) + if hr != 0 { + return Err(hr); + } + Ok(Font::take(ComPtr::from_raw(font))) } } }