Skip to content

Commit dbce1fb

Browse files
authored
add analogous fallible font fetching methods (#62)
* add analogous fallible font fetching methods * return entire HRESULT (severity, facility, code) instead of just the code * clean up new method names
1 parent 771b2e1 commit dbce1fb

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/font_family.rs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,86 @@ impl FontFamily {
2626
(*self.native.get()).as_raw()
2727
}
2828

29+
#[deprecated(note = "Use `family_name` instead.")]
2930
pub fn name(&self) -> String {
31+
self.family_name().unwrap()
32+
}
33+
34+
pub fn family_name(&self) -> Result<String, HRESULT> {
35+
let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut();
3036
unsafe {
31-
let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut();
3237
let hr = (*self.native.get()).GetFamilyNames(&mut family_names);
33-
assert!(hr == 0);
34-
35-
get_locale_string(&mut ComPtr::from_raw(family_names))
38+
if hr != 0 {
39+
return Err(hr);
40+
}
41+
Ok(get_locale_string(&mut ComPtr::from_raw(family_names)))
3642
}
3743
}
3844

45+
#[deprecated(note = "Use `first_matching_font` instead.")]
3946
pub fn get_first_matching_font(
4047
&self,
4148
weight: FontWeight,
4249
stretch: FontStretch,
4350
style: FontStyle,
4451
) -> Font {
52+
self.first_matching_font(weight, stretch, style).unwrap()
53+
}
54+
55+
pub fn first_matching_font(
56+
&self,
57+
weight: FontWeight,
58+
stretch: FontStretch,
59+
style: FontStyle,
60+
) -> Result<Font, HRESULT> {
61+
let mut font: *mut IDWriteFont = ptr::null_mut();
4562
unsafe {
46-
let mut font: *mut IDWriteFont = ptr::null_mut();
4763
let hr = (*self.native.get()).GetFirstMatchingFont(
4864
weight.t(),
4965
stretch.t(),
5066
style.t(),
5167
&mut font,
5268
);
53-
assert!(hr == 0);
54-
Font::take(ComPtr::from_raw(font))
69+
if hr != 0 {
70+
return Err(hr);
71+
}
72+
Ok(Font::take(ComPtr::from_raw(font)))
5573
}
5674
}
5775

76+
#[deprecated(note = "Use `font_collection` instead.")]
5877
pub fn get_font_collection(&self) -> FontCollection {
78+
self.font_collection().unwrap()
79+
}
80+
81+
pub fn font_collection(&self) -> Result<FontCollection, HRESULT> {
82+
let mut collection: *mut IDWriteFontCollection = ptr::null_mut();
5983
unsafe {
60-
let mut collection: *mut IDWriteFontCollection = ptr::null_mut();
6184
let hr = (*self.native.get()).GetFontCollection(&mut collection);
62-
assert!(hr == 0);
63-
FontCollection::take(ComPtr::from_raw(collection))
85+
if hr != 0 {
86+
return Err(hr);
87+
}
88+
Ok(FontCollection::take(ComPtr::from_raw(collection)))
6489
}
6590
}
6691

6792
pub fn get_font_count(&self) -> u32 {
6893
unsafe { (*self.native.get()).GetFontCount() }
6994
}
7095

96+
#[deprecated(note = "Use `font` instead.")]
7197
pub fn get_font(&self, index: u32) -> Font {
98+
self.font(index).unwrap()
99+
}
100+
101+
pub fn font(&self, index: u32) -> Result<Font, HRESULT> {
102+
let mut font: *mut IDWriteFont = ptr::null_mut();
72103
unsafe {
73-
let mut font: *mut IDWriteFont = ptr::null_mut();
74104
let hr = (*self.native.get()).GetFont(index, &mut font);
75-
assert!(hr == 0);
76-
Font::take(ComPtr::from_raw(font))
105+
if hr != 0 {
106+
return Err(hr);
107+
}
108+
Ok(Font::take(ComPtr::from_raw(font)))
77109
}
78110
}
79111
}

0 commit comments

Comments
 (0)