diff --git a/src/font.rs b/src/font.rs index 37b915d..dfa980b 100644 --- a/src/font.rs +++ b/src/font.rs @@ -57,7 +57,7 @@ impl Font { pub fn to_descriptor(&self) -> FontDescriptor { FontDescriptor { - family_name: self.family_name(), + family_name: self.family_name().unwrap_or_default(), stretch: self.stretch(), style: self.style(), weight: self.weight(), @@ -87,49 +87,80 @@ impl Font { unsafe { mem::transmute::((*self.native.get()).GetSimulations()) } } - pub fn family_name(&self) -> String { + #[deprecated(note = "Use `family_name` instead.")] + pub fn get_family_name(&self) -> String { + self.family_name().unwrap() + } + + pub fn family_name(&self) -> Result { + let mut family: *mut IDWriteFontFamily = ptr::null_mut(); unsafe { - let mut family: *mut IDWriteFontFamily = ptr::null_mut(); let hr = (*self.native.get()).GetFontFamily(&mut family); - assert!(hr == 0); + if hr != S_OK { + return Err(hr); + } - FontFamily::take(ComPtr::from_raw(family)).name() + FontFamily::take(ComPtr::from_raw(family)).family_name() } } - pub fn face_name(&self) -> String { + #[deprecated(note = "Use `face_name` instead.")] + pub fn get_face_name(&self) -> String { + self.face_name().unwrap() + } + + pub fn face_name(&self) -> Result { + let mut names: *mut IDWriteLocalizedStrings = ptr::null_mut(); unsafe { - let mut names: *mut IDWriteLocalizedStrings = ptr::null_mut(); let hr = (*self.native.get()).GetFaceNames(&mut names); - assert!(hr == 0); + if hr != S_OK { + return Err(hr); + } get_locale_string(&mut ComPtr::from_raw(names)) } } - pub fn informational_string(&self, id: InformationalStringId) -> Option { + #[deprecated(note = "Use `informational_string` instead.")] + pub fn get_informational_string(&self, id: InformationalStringId) -> Option { + self.informational_string(id).unwrap() + } + + pub fn informational_string( + &self, + id: InformationalStringId, + ) -> Result, HRESULT> { + let mut names: *mut IDWriteLocalizedStrings = ptr::null_mut(); + let mut exists = FALSE; + let id = id as DWRITE_INFORMATIONAL_STRING_ID; unsafe { - let mut names: *mut IDWriteLocalizedStrings = ptr::null_mut(); - let mut exists = FALSE; - let id = id as DWRITE_INFORMATIONAL_STRING_ID; let hr = (*self.native.get()).GetInformationalStrings(id, &mut names, &mut exists); - assert!(hr == S_OK); + if hr != S_OK { + return Err(hr); + } if exists == TRUE { - Some(get_locale_string(&mut ComPtr::from_raw(names))) + Ok(Some(get_locale_string(&mut ComPtr::from_raw(names))?)) } else { - None + Ok(None) } } } - pub fn create_font_face(&self) -> FontFace { + #[deprecated(note = "Use `create_font_face` instead.")] + pub fn get_create_font_face(&self) -> FontFace { + self.create_font_face().unwrap() + } + + pub fn create_font_face(&self) -> Result { + let mut face: *mut IDWriteFontFace = ptr::null_mut(); // FIXME create_font_face should cache the FontFace and return it, // there's a 1:1 relationship unsafe { - let mut face: *mut IDWriteFontFace = ptr::null_mut(); let hr = (*self.native.get()).CreateFontFace(&mut face); - assert!(hr == 0); - FontFace::take(ComPtr::from_raw(face)) + if hr != S_OK { + return Err(hr); + } + Ok(FontFace::take(ComPtr::from_raw(face))) } } diff --git a/src/font_family.rs b/src/font_family.rs index 78dadaa..786b0ce 100644 --- a/src/font_family.rs +++ b/src/font_family.rs @@ -38,7 +38,7 @@ impl FontFamily { if hr != 0 { return Err(hr); } - Ok(get_locale_string(&mut ComPtr::from_raw(family_names))) + get_locale_string(&mut ComPtr::from_raw(family_names)) } } diff --git a/src/helpers.rs b/src/helpers.rs index 45bf005..3f5c154 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -6,9 +6,10 @@ use std::ffi::OsStr; use std::os::windows::ffi::OsStrExt; use winapi::ctypes::wchar_t; use winapi::shared::minwindef::{BOOL, FALSE}; -use winapi::shared::winerror::S_OK; +use winapi::shared::winerror::{E_FAIL, S_OK}; use winapi::um::dwrite::IDWriteLocalizedStrings; use winapi::um::winnls::GetUserDefaultLocaleName; +use winapi::um::winnt::HRESULT; use wio::com::ComPtr; lazy_static! { @@ -22,7 +23,7 @@ lazy_static! { static ref EN_US_LOCALE: Vec = OsStr::new("en-us").to_wide_null(); } -pub fn get_locale_string(strings: &mut ComPtr) -> String { +pub fn get_locale_string(strings: &mut ComPtr) -> Result { unsafe { let mut index: u32 = 0; let mut exists: BOOL = FALSE; @@ -37,14 +38,18 @@ pub fn get_locale_string(strings: &mut ComPtr) -> Strin let mut length: u32 = 0; let hr = strings.GetStringLength(index, &mut length); - assert!(hr == 0); + if hr != S_OK { + return Err(hr); + } let mut name: Vec = Vec::with_capacity(length as usize + 1); let hr = strings.GetString(index, name.as_mut_ptr(), length + 1); - assert!(hr == 0); + if hr != S_OK { + return Err(hr); + } name.set_len(length as usize); - String::from_utf16(&name).ok().unwrap() + String::from_utf16(&name).map_err(|_| E_FAIL) } }