cairo: don't panic when constructing a FontFace
@@ -34,7 +34,11 @@ pub struct FontFace(ptr::NonNull<ffi::cairo_font_face_t>);
impl FontFace {
#[doc(alias = "cairo_toy_font_face_create")]
- pub fn toy_create(family: &str, slant: FontSlant, weight: FontWeight) -> FontFace {
+ pub fn toy_create(
+ family: &str,
+ slant: FontSlant,
+ weight: FontWeight,
+ ) -> Result<FontFace, Error> {
let font_face: FontFace = unsafe {
let family = CString::new(family).unwrap();
FontFace::from_raw_full(ffi::cairo_toy_font_face_create(
@@ -44,21 +48,25 @@ impl FontFace {
))
};
let status = unsafe { ffi::cairo_font_face_status(font_face.to_raw_none()) };
- status_to_result(status).expect("Failed to create a FontFace");
- font_face
+ status_to_result(status)?;
+
+ Ok(font_face)
}
// Safety: the FT_Face must be valid and not be freed until the `FontFace` is dropped.
#[cfg(any(feature = "freetype", feature = "dox"))]
#[doc(alias = "cairo_ft_font_face_create_for_ft_face")]
- pub unsafe fn create_from_ft(face: freetype_crate::freetype::FT_Face) -> FontFace {
+ pub unsafe fn create_from_ft(
+ face: freetype_crate::freetype::FT_Face,
+ ) -> Result<FontFace, Error> {
let font_face = FontFace::from_raw_full(ffi::cairo_ft_font_face_create_for_ft_face(
face as *mut _,
0,
));
let status = ffi::cairo_font_face_status(font_face.to_raw_none());
- status_to_result(status).expect("Failed to create a FontFace");
- font_face
+ status_to_result(status)?;
+
+ Ok(font_face)
}
// Safety: the FT_Face must be valid and not be freed until the `FontFace` is dropped.
@@ -67,14 +75,15 @@ impl FontFace {
pub unsafe fn create_from_ft_with_flags(
face: freetype_crate::freetype::FT_Face,
load_flags: libc::c_int,
- ) -> FontFace {
+ ) -> Result<FontFace, Error> {
let font_face = FontFace::from_raw_full(ffi::cairo_ft_font_face_create_for_ft_face(
face as *mut _,
load_flags,
));
let status = ffi::cairo_font_face_status(font_face.to_raw_none());
- status_to_result(status).expect("Failed to create a FontFace");
- font_face
+ status_to_result(status)?;
+
+ Ok(font_face)
}
#[cfg(feature = "use_glib")]
