cairo/font/
font_face.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::{CStr, CString};
4#[cfg(not(feature = "use_glib"))]
5use std::ptr;
6#[cfg(feature = "freetype")]
7use std::rc::Rc;
8
9#[cfg(feature = "use_glib")]
10use glib::translate::*;
11
12#[cfg(feature = "freetype")]
13use crate::FtSynthesize;
14use crate::{ffi, utils::status_to_result, Error, FontSlant, FontType, FontWeight};
15
16#[cfg(feature = "freetype")]
17static FT_FACE_KEY: crate::UserDataKey<freetype::face::Face> = crate::UserDataKey::new();
18
19#[cfg(feature = "use_glib")]
20glib::wrapper! {
21    #[derive(Debug)]
22    #[doc(alias = "cairo_font_face_t")]
23    pub struct FontFace(Shared<ffi::cairo_font_face_t>);
24
25    match fn {
26        ref => |ptr| ffi::cairo_font_face_reference(ptr),
27        unref => |ptr| ffi::cairo_font_face_destroy(ptr),
28        type_ => || ffi::gobject::cairo_gobject_font_face_get_type(),
29    }
30}
31
32#[cfg(not(feature = "use_glib"))]
33#[cfg_attr(docsrs, doc(cfg(not(feature = "use_glib"))))]
34#[derive(Debug)]
35#[doc(alias = "cairo_font_face_t")]
36pub struct FontFace(ptr::NonNull<ffi::cairo_font_face_t>);
37
38impl FontFace {
39    #[doc(alias = "cairo_toy_font_face_create")]
40    pub fn toy_create(
41        family: &str,
42        slant: FontSlant,
43        weight: FontWeight,
44    ) -> Result<FontFace, Error> {
45        let font_face: FontFace = unsafe {
46            let family = CString::new(family).unwrap();
47            FontFace::from_raw_full(ffi::cairo_toy_font_face_create(
48                family.as_ptr(),
49                slant.into(),
50                weight.into(),
51            ))
52        };
53        let status = unsafe { ffi::cairo_font_face_status(font_face.to_raw_none()) };
54        status_to_result(status)?;
55
56        Ok(font_face)
57    }
58
59    // rustdoc-stripper-ignore-next
60    /// Creates a new font face for the FreeType backend from an already opened FreeType face.
61    #[cfg(feature = "freetype")]
62    #[cfg_attr(docsrs, doc(cfg(feature = "freetype")))]
63    #[doc(alias = "cairo_ft_font_face_create_for_ft_face")]
64    pub fn create_from_ft(face: &freetype::face::Face) -> Result<FontFace, Error> {
65        // Increase reference count of `FT_Face`.
66        let mut face = face.clone();
67
68        // SAFETY: The user data entry keeps `freetype::face::Face` alive
69        // until the FontFace is dropped.
70        let font_face = unsafe {
71            FontFace::from_raw_full(ffi::cairo_ft_font_face_create_for_ft_face(
72                face.raw_mut() as freetype::ffi::FT_Face as *mut _,
73                0,
74            ))
75        };
76        font_face.set_user_data(&FT_FACE_KEY, Rc::new(face))?;
77        let status = unsafe { ffi::cairo_font_face_status(font_face.to_raw_none()) };
78        status_to_result(status)?;
79
80        Ok(font_face)
81    }
82
83    // rustdoc-stripper-ignore-next
84    /// Creates a new font face for the FreeType backend from an already opened FreeType face,
85    /// additionally allowing you to pass flags to the underlying C API.
86    #[cfg(feature = "freetype")]
87    #[cfg_attr(docsrs, doc(cfg(feature = "freetype")))]
88    #[doc(alias = "cairo_ft_font_face_create_for_ft_face")]
89    pub fn create_from_ft_with_flags(
90        face: &freetype::face::Face,
91        load_flags: libc::c_int,
92    ) -> Result<FontFace, Error> {
93        // Increase reference count of `FT_Face`.
94        let mut face = face.clone();
95
96        // SAFETY: The user data entry keeps `freetype::face::Face` alive
97        // until the FontFace is dropped.
98        let font_face = unsafe {
99            FontFace::from_raw_full(ffi::cairo_ft_font_face_create_for_ft_face(
100                face.raw_mut() as freetype::ffi::FT_Face as *mut _,
101                load_flags,
102            ))
103        };
104        font_face.set_user_data(&FT_FACE_KEY, Rc::new(face))?;
105        let status = unsafe { ffi::cairo_font_face_status(font_face.to_raw_none()) };
106        status_to_result(status)?;
107
108        Ok(font_face)
109    }
110
111    #[cfg(feature = "use_glib")]
112    #[inline]
113    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
114        from_glib_full(ptr)
115    }
116
117    #[cfg(not(feature = "use_glib"))]
118    #[inline]
119    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
120        debug_assert!(!ptr.is_null());
121        FontFace(ptr::NonNull::new_unchecked(ptr))
122    }
123
124    #[cfg(feature = "use_glib")]
125    #[inline]
126    pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
127        from_glib_none(ptr)
128    }
129
130    #[cfg(not(feature = "use_glib"))]
131    #[inline]
132    pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
133        debug_assert!(!ptr.is_null());
134        FontFace(ptr::NonNull::new_unchecked(ptr))
135    }
136
137    #[cfg(feature = "use_glib")]
138    #[inline]
139    pub fn to_raw_none(&self) -> *mut ffi::cairo_font_face_t {
140        self.to_glib_none().0
141    }
142
143    #[cfg(not(feature = "use_glib"))]
144    #[inline]
145    pub fn to_raw_none(&self) -> *mut ffi::cairo_font_face_t {
146        self.0.as_ptr()
147    }
148
149    #[doc(alias = "cairo_toy_font_face_get_family")]
150    pub fn toy_get_family(&self) -> Option<String> {
151        unsafe { to_optional_string(ffi::cairo_toy_font_face_get_family(self.to_raw_none())) }
152    }
153
154    #[doc(alias = "cairo_toy_font_face_get_slant")]
155    pub fn toy_get_slant(&self) -> FontSlant {
156        unsafe { FontSlant::from(ffi::cairo_toy_font_face_get_slant(self.to_raw_none())) }
157    }
158
159    #[doc(alias = "cairo_toy_font_face_get_weight")]
160    pub fn toy_get_weight(&self) -> FontWeight {
161        unsafe { FontWeight::from(ffi::cairo_toy_font_face_get_weight(self.to_raw_none())) }
162    }
163
164    #[doc(alias = "cairo_font_face_get_type")]
165    #[doc(alias = "get_type")]
166    pub fn type_(&self) -> FontType {
167        unsafe { FontType::from(ffi::cairo_font_face_get_type(self.to_raw_none())) }
168    }
169
170    #[doc(alias = "cairo_font_face_get_reference_count")]
171    #[doc(alias = "get_reference_count")]
172    pub fn reference_count(&self) -> usize {
173        unsafe { ffi::cairo_font_face_get_reference_count(self.to_raw_none()) as usize }
174    }
175
176    #[cfg(feature = "freetype")]
177    #[doc(alias = "cairo_ft_font_face_get_synthesize")]
178    #[doc(alias = "get_synthesize")]
179    pub fn synthesize(&self) -> FtSynthesize {
180        unsafe { FtSynthesize::from(ffi::cairo_ft_font_face_get_synthesize(self.to_raw_none())) }
181    }
182
183    #[cfg(feature = "freetype")]
184    #[doc(alias = "cairo_ft_font_face_set_synthesize")]
185    pub fn set_synthesize(&self, synth_flags: FtSynthesize) {
186        unsafe { ffi::cairo_ft_font_face_set_synthesize(self.to_raw_none(), synth_flags.into()) }
187    }
188
189    #[cfg(feature = "freetype")]
190    #[doc(alias = "cairo_ft_font_face_unset_synthesize")]
191    pub fn unset_synthesize(&self, synth_flags: FtSynthesize) {
192        unsafe { ffi::cairo_ft_font_face_unset_synthesize(self.to_raw_none(), synth_flags.into()) }
193    }
194
195    #[doc(alias = "cairo_font_face_status")]
196    pub fn status(&self) -> Result<(), Error> {
197        let status = unsafe { ffi::cairo_font_face_status(self.to_raw_none()) };
198        status_to_result(status)
199    }
200
201    user_data_methods! {
202        ffi::cairo_font_face_get_user_data,
203        ffi::cairo_font_face_set_user_data,
204    }
205}
206
207#[cfg(not(feature = "use_glib"))]
208impl Drop for FontFace {
209    #[inline]
210    fn drop(&mut self) {
211        unsafe {
212            ffi::cairo_font_face_destroy(self.to_raw_none());
213        }
214    }
215}
216
217#[cfg(not(feature = "use_glib"))]
218impl Clone for FontFace {
219    #[inline]
220    fn clone(&self) -> FontFace {
221        unsafe { FontFace::from_raw_none(self.to_raw_none()) }
222    }
223}
224
225pub(crate) unsafe fn to_optional_string(str: *const libc::c_char) -> Option<String> {
226    if str.is_null() {
227        None
228    } else {
229        Some(String::from_utf8_lossy(CStr::from_ptr(str).to_bytes()).into_owned())
230    }
231}