-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add fallible font methods to FontCollection #63
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -108,62 +109,87 @@ 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() | ||
} | ||
|
||
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 font collection. | ||
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() | ||
} | ||
|
||
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() | ||
} | ||
|
||
pub fn font_family_by_name(&self, family_name: &str) -> Result<Option<FontFamily>, HRESULT> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that we have a An alternative I considered was creating a new error enum, e.g. enum GetError {
Win32Error(HRESULT),
NotFound
} And we could return |
||
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)))) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.