diff --git a/src/font.rs b/src/font.rs index 37b915d..033627d 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.get_family_name(), 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)) + Ok(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))) } }