Skip to content
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 on Font struct #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
61 changes: 46 additions & 15 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,49 +87,80 @@ impl Font {
unsafe { mem::transmute::<u32, FontSimulations>((*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<String, HRESULT> {
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<String, HRESULT> {
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<String> {
self.try_informational_string(id).unwrap()
}

pub fn try_informational_string(
&self,
id: InformationalStringId,
) -> Result<Option<String>, 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<FontFace, HRESULT> {
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)))
}
}

Expand Down