From 9507834395458a81874a2ccd77e4486da53932a3 Mon Sep 17 00:00:00 2001 From: Andy Carlson <2yinyang2@gmail.com> Date: Tue, 25 Feb 2025 10:51:05 -0500 Subject: [PATCH 1/2] add fallible font methods on Font struct --- src/font.rs | 61 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/src/font.rs b/src/font.rs index 37b915d..a755077 100644 --- a/src/font.rs +++ b/src/font.rs @@ -87,49 +87,80 @@ impl Font { unsafe { mem::transmute::((*self.native.get()).GetSimulations()) } } + #[deprecated(note = "Use `try_family_name` instead.")] pub fn family_name(&self) -> String { + self.try_family_name().unwrap() + } + + pub fn try_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() } } + #[deprecated(note = "Use `try_face_name` instead.")] pub fn face_name(&self) -> String { + self.try_face_name().unwrap() + } + + pub fn try_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))) } } + #[deprecated(note = "Use `try_informational_string` instead.")] pub fn informational_string(&self, id: InformationalStringId) -> Option { + self.try_informational_string(id).unwrap() + } + + pub fn try_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) } } } + #[deprecated(note = "Use `try_create_font_face` instead.")] pub fn create_font_face(&self) -> FontFace { + self.try_create_font_face().unwrap() + } + + pub fn try_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))) } } From 5f206f64f74fd8a8ba93497d566ec212243a7d74 Mon Sep 17 00:00:00 2001 From: Andy Carlson <2yinyang2@gmail.com> Date: Sun, 23 Mar 2025 09:46:08 -0400 Subject: [PATCH 2/2] make naming consistent with other modules --- src/font.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/font.rs b/src/font.rs index a755077..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,12 +87,12 @@ impl Font { unsafe { mem::transmute::((*self.native.get()).GetSimulations()) } } - #[deprecated(note = "Use `try_family_name` instead.")] - pub fn family_name(&self) -> String { - self.try_family_name().unwrap() + #[deprecated(note = "Use `family_name` instead.")] + pub fn get_family_name(&self) -> String { + self.family_name().unwrap() } - pub fn try_family_name(&self) -> Result { + pub fn family_name(&self) -> Result { let mut family: *mut IDWriteFontFamily = ptr::null_mut(); unsafe { let hr = (*self.native.get()).GetFontFamily(&mut family); @@ -104,12 +104,12 @@ impl Font { } } - #[deprecated(note = "Use `try_face_name` instead.")] - pub fn face_name(&self) -> String { - self.try_face_name().unwrap() + #[deprecated(note = "Use `face_name` instead.")] + pub fn get_face_name(&self) -> String { + self.face_name().unwrap() } - pub fn try_face_name(&self) -> Result { + pub fn face_name(&self) -> Result { let mut names: *mut IDWriteLocalizedStrings = ptr::null_mut(); unsafe { let hr = (*self.native.get()).GetFaceNames(&mut names); @@ -121,12 +121,12 @@ impl Font { } } - #[deprecated(note = "Use `try_informational_string` instead.")] - pub fn informational_string(&self, id: InformationalStringId) -> Option { - self.try_informational_string(id).unwrap() + #[deprecated(note = "Use `informational_string` instead.")] + pub fn get_informational_string(&self, id: InformationalStringId) -> Option { + self.informational_string(id).unwrap() } - pub fn try_informational_string( + pub fn informational_string( &self, id: InformationalStringId, ) -> Result, HRESULT> { @@ -146,12 +146,12 @@ impl Font { } } - #[deprecated(note = "Use `try_create_font_face` instead.")] - pub fn create_font_face(&self) -> FontFace { - self.try_create_font_face().unwrap() + #[deprecated(note = "Use `create_font_face` instead.")] + pub fn get_create_font_face(&self) -> FontFace { + self.create_font_face().unwrap() } - pub fn try_create_font_face(&self) -> Result { + 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