Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions src/font_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ impl FontFamily {
}

pub fn name(&self) -> String {
self.try_name().unwrap()
}

pub fn try_name(&self) -> Result<String, HRESULT> {
let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut();
unsafe {
let mut family_names: *mut IDWriteLocalizedStrings = ptr::null_mut();
let hr = (*self.native.get()).GetFamilyNames(&mut family_names);
assert!(hr == 0);

get_locale_string(&mut ComPtr::from_raw(family_names))
if hr != 0 {
return Err(hr);
}
Ok(get_locale_string(&mut ComPtr::from_raw(family_names)))
}
}

Expand All @@ -42,25 +47,43 @@ impl FontFamily {
stretch: FontStretch,
style: FontStyle,
) -> Font {
self.try_get_first_matching_font(weight, stretch, style)
.unwrap()
}

pub fn try_get_first_matching_font(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Rust idiomatically doesn't use the word get_ in method names, I wonder if a way to do this would be to make the fallible ones names like first_matching_font and then to deprecate the non-fallible versions. That way we could gradually transition to a more idiomatically-Rust-like interface. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me! Will update.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've updated the names and #[deprecated] the old methods.

&self,
weight: FontWeight,
stretch: FontStretch,
style: FontStyle,
) -> Result<Font, HRESULT> {
let mut font: *mut IDWriteFont = ptr::null_mut();
unsafe {
let mut font: *mut IDWriteFont = ptr::null_mut();
let hr = (*self.native.get()).GetFirstMatchingFont(
weight.t(),
stretch.t(),
style.t(),
&mut font,
);
assert!(hr == 0);
Font::take(ComPtr::from_raw(font))
if hr != 0 {
return Err(hr);
}
Ok(Font::take(ComPtr::from_raw(font)))
}
}

pub fn get_font_collection(&self) -> FontCollection {
self.try_get_font_collection().unwrap()
}

pub fn try_get_font_collection(&self) -> Result<FontCollection, HRESULT> {
let mut collection: *mut IDWriteFontCollection = ptr::null_mut();
unsafe {
let mut collection: *mut IDWriteFontCollection = ptr::null_mut();
let hr = (*self.native.get()).GetFontCollection(&mut collection);
assert!(hr == 0);
FontCollection::take(ComPtr::from_raw(collection))
if hr != 0 {
return Err(hr);
}
Ok(FontCollection::take(ComPtr::from_raw(collection)))
}
}

Expand All @@ -69,11 +92,17 @@ impl FontFamily {
}

pub fn get_font(&self, index: u32) -> Font {
self.try_get_font(index).unwrap()
}

pub fn try_get_font(&self, index: u32) -> Result<Font, HRESULT> {
let mut font: *mut IDWriteFont = ptr::null_mut();
unsafe {
let mut font: *mut IDWriteFont = ptr::null_mut();
let hr = (*self.native.get()).GetFont(index, &mut font);
assert!(hr == 0);
Font::take(ComPtr::from_raw(font))
if hr != 0 {
return Err(hr);
}
Ok(Font::take(ComPtr::from_raw(font)))
}
}
}