diff --git a/src/font_collection.rs b/src/font_collection.rs index 90b9121..3e4974f 100644 --- a/src/font_collection.rs +++ b/src/font_collection.rs @@ -10,6 +10,7 @@ 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 winapi::um::winnt::HRESULT; use wio::com::ComPtr; use super::{DWriteFactory, Font, FontDescriptor, FontFace, FontFamily}; @@ -108,62 +109,87 @@ impl FontCollection { unsafe { (*self.native.get()).GetFontFamilyCount() } } + #[deprecated(note = "Use `font_family` instead.")] pub fn get_font_family(&self, index: u32) -> FontFamily { + self.font_family(index).unwrap() + } + + pub fn font_family(&self, index: u32) -> Result { + let mut family: *mut IDWriteFontFamily = ptr::null_mut(); 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)) + if hr != S_OK { + return Err(hr); + } + Ok(FontFamily::take(ComPtr::from_raw(family))) } } - // Find a font matching the given font descriptor in this - // font collection. + #[deprecated(note = "Use `font_from_descriptor` instead.")] pub fn get_font_from_descriptor(&self, desc: &FontDescriptor) -> Option { - 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); + self.font_from_descriptor(desc).unwrap() + } + + // Find a font matching the given font descriptor in this font collection. + pub fn font_from_descriptor(&self, desc: &FontDescriptor) -> Result, HRESULT> { + if let Some(family) = self.font_family_by_name(&desc.family_name)? { + let font = family.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); + return Ok(Some(font)); } } - None + Ok(None) } + #[deprecated(note = "Use `font_from_face` instead.")] pub fn get_font_from_face(&self, face: &FontFace) -> Option { + self.font_from_face(face).ok() + } + + pub fn font_from_face(&self, face: &FontFace) -> Result { + let mut font: *mut IDWriteFont = ptr::null_mut(); 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; + if hr != S_OK { + return Err(hr); } - Some(Font::take(ComPtr::from_raw(font))) + Ok(Font::take(ComPtr::from_raw(font))) } } + #[deprecated(note = "Use `font_family_by_name` instead.")] pub fn get_font_family_by_name(&self, family_name: &str) -> Option { + self.font_family_by_name(family_name).unwrap() + } + + pub fn font_family_by_name(&self, family_name: &str) -> Result, HRESULT> { + let mut index: u32 = 0; + let mut exists: BOOL = FALSE; 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 hr != S_OK { + return Err(hr); + } if exists == FALSE { - return None; + return Ok(None); } let mut family: *mut IDWriteFontFamily = ptr::null_mut(); let hr = (*self.native.get()).GetFontFamily(index, &mut family); - assert!(hr == 0); + if hr != S_OK { + return Err(hr); + } - Some(FontFamily::take(ComPtr::from_raw(family))) + Ok(Some(FontFamily::take(ComPtr::from_raw(family)))) } } }