Skip to content
Merged
Changes from all 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
68 changes: 49 additions & 19 deletions src/font_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use winapi::shared::minwindef::{BOOL, FALSE, TRUE};
use winapi::shared::winerror::S_OK;
use winapi::um::dwrite::IDWriteFontCollectionLoader;
use winapi::um::dwrite::{IDWriteFont, IDWriteFontCollection, IDWriteFontFamily};
use winapi::um::winnt::HRESULT;
use wio::com::ComPtr;

use super::{DWriteFactory, Font, FontDescriptor, FontFace, FontFamily};
Expand Down Expand Up @@ -108,62 +109,91 @@ impl FontCollection {
unsafe { (*self.native.get()).GetFontFamilyCount() }
}

#[deprecated(note = "Use `font_family` instead.")]
pub fn get_font_family(&self, index: u32) -> FontFamily {
self.font_family(index).unwrap()
}

/// Returns the [`FontFamily`] at the given index.
pub fn font_family(&self, index: u32) -> Result<FontFamily, HRESULT> {
let mut family: *mut IDWriteFontFamily = ptr::null_mut();
unsafe {
let mut family: *mut IDWriteFontFamily = ptr::null_mut();
let hr = (*self.native.get()).GetFontFamily(index, &mut family);
assert!(hr == 0);
FontFamily::take(ComPtr::from_raw(family))
if hr != S_OK {
return Err(hr);
}
Ok(FontFamily::take(ComPtr::from_raw(family)))
}
}

// Find a font matching the given font descriptor in this
// font collection.
#[deprecated(note = "Use `font_from_descriptor` instead.")]
pub fn get_font_from_descriptor(&self, desc: &FontDescriptor) -> Option<Font> {
if let Some(family) = self.get_font_family_by_name(&desc.family_name) {
let font = family.get_first_matching_font(desc.weight, desc.stretch, desc.style);
self.font_from_descriptor(desc).unwrap()
}

/// Find a font matching the given font descriptor in this [`FontCollection`].
pub fn font_from_descriptor(&self, desc: &FontDescriptor) -> Result<Option<Font>, HRESULT> {
if let Some(family) = self.font_family_by_name(&desc.family_name)? {
let font = family.first_matching_font(desc.weight, desc.stretch, desc.style)?;
// Exact matches only here
if font.weight() == desc.weight
&& font.stretch() == desc.stretch
&& font.style() == desc.style
{
return Some(font);
return Ok(Some(font));
}
}

None
Ok(None)
}

#[deprecated(note = "Use `font_from_face` instead.")]
pub fn get_font_from_face(&self, face: &FontFace) -> Option<Font> {
self.font_from_face(face).ok()
}

/// Get a [`Font`] from the given [`FontFace`].
pub fn font_from_face(&self, face: &FontFace) -> 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()).GetFontFromFontFace(face.as_ptr(), &mut font);
if hr != 0 {
return None;
if hr != S_OK {
return Err(hr);
}
Some(Font::take(ComPtr::from_raw(font)))
Ok(Font::take(ComPtr::from_raw(font)))
}
}

#[deprecated(note = "Use `font_family_by_name` instead.")]
pub fn get_font_family_by_name(&self, family_name: &str) -> Option<FontFamily> {
self.font_family_by_name(family_name).unwrap()
}

/// Find a [`FontFamily`] with the given name. Returns `None` if no family
/// with that name is found.
pub fn font_family_by_name(&self, family_name: &str) -> Result<Option<FontFamily>, HRESULT> {
Copy link
Author

@acarl005 acarl005 Feb 25, 2025

Choose a reason for hiding this comment

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

Note that we have a Result<Option<FontFamily>, HRESULT> instead of just a Result<FontFamily, HRESULT>. I think this makes sense, as there may be no font family with the given name, so Ok(None) is returned to designate that the name doesn't exist, and no errors occurred.

An alternative I considered was creating a new error enum, e.g.

enum GetError {
    Win32Error(HRESULT),
    NotFound
}

And we could return Result<FontFamily, GetError> instead.

let mut index: u32 = 0;
let mut exists: BOOL = FALSE;
unsafe {
let mut index: u32 = 0;
let mut exists: BOOL = FALSE;
let hr = (*self.native.get()).FindFamilyName(
family_name.to_wide_null().as_ptr(),
&mut index,
&mut exists,
);
assert!(hr == 0);
if hr != S_OK {
return Err(hr);
}
if exists == FALSE {
return None;
return Ok(None);
}

let mut family: *mut IDWriteFontFamily = ptr::null_mut();
let hr = (*self.native.get()).GetFontFamily(index, &mut family);
assert!(hr == 0);
if hr != S_OK {
return Err(hr);
}

Some(FontFamily::take(ComPtr::from_raw(family)))
Ok(Some(FontFamily::take(ComPtr::from_raw(family))))
}
}
}