cairo/font/
font_options.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(feature = "v1_16")]
4use std::ffi::CString;
5use std::hash;
6#[cfg(not(feature = "use_glib"))]
7use std::ptr;
8
9#[cfg(feature = "use_glib")]
10use glib::translate::*;
11
12#[cfg(feature = "v1_16")]
13use crate::font::font_face::to_optional_string;
14use crate::{
15    ffi, utils::status_to_result, Antialias, Error, HintMetrics, HintStyle, SubpixelOrder,
16};
17
18#[cfg(feature = "use_glib")]
19glib::wrapper! {
20    #[derive(Debug)]
21    #[doc(alias = "cairo_font_options_t")]
22    pub struct FontOptions(Boxed<ffi::cairo_font_options_t>);
23
24    match fn {
25        copy => |ptr| {
26            let ptr = ffi::cairo_font_options_copy(ptr);
27            let status = ffi::cairo_font_options_status(ptr);
28            status_to_result(status).expect("Failed to create a copy of FontOptions");
29            ptr
30        },
31        free => |ptr| ffi::cairo_font_options_destroy(ptr),
32        type_ => || ffi::gobject::cairo_gobject_font_options_get_type(),
33    }
34}
35
36#[cfg(not(feature = "use_glib"))]
37#[cfg_attr(docsrs, doc(cfg(not(feature = "use_glib"))))]
38#[derive(Debug)]
39#[doc(alias = "cairo_font_options_t")]
40pub struct FontOptions(ptr::NonNull<ffi::cairo_font_options_t>);
41
42unsafe impl Send for FontOptions {}
43unsafe impl Sync for FontOptions {}
44
45impl FontOptions {
46    #[doc(alias = "cairo_font_options_create")]
47    pub fn new() -> Result<FontOptions, Error> {
48        let font_options: FontOptions =
49            unsafe { FontOptions::from_raw_full(ffi::cairo_font_options_create()) };
50
51        let status = unsafe { ffi::cairo_font_options_status(font_options.to_raw_none()) };
52        status_to_result(status)?;
53
54        Ok(font_options)
55    }
56
57    #[cfg(feature = "use_glib")]
58    #[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
59    #[inline]
60    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_options_t) -> Self {
61        from_glib_full(ptr)
62    }
63
64    #[cfg(not(feature = "use_glib"))]
65    #[cfg_attr(docsrs, doc(cfg(not(feature = "use_glib"))))]
66    #[inline]
67    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_options_t) -> Self {
68        debug_assert!(!ptr.is_null());
69        Self(ptr::NonNull::new_unchecked(ptr))
70    }
71
72    #[cfg(feature = "use_glib")]
73    #[cfg_attr(docsrs, doc(cfg(feature = "use_glib")))]
74    #[inline]
75    pub fn to_raw_none(&self) -> *mut ffi::cairo_font_options_t {
76        mut_override(self.to_glib_none().0)
77    }
78
79    #[cfg(not(feature = "use_glib"))]
80    #[cfg_attr(docsrs, doc(cfg(not(feature = "use_glib"))))]
81    #[inline]
82    pub fn to_raw_none(&self) -> *mut ffi::cairo_font_options_t {
83        self.0.as_ptr()
84    }
85
86    #[doc(alias = "cairo_font_options_merge")]
87    pub fn merge(&mut self, other: &FontOptions) {
88        unsafe { ffi::cairo_font_options_merge(self.to_raw_none(), other.to_raw_none()) }
89    }
90
91    #[doc(alias = "cairo_font_options_set_antialias")]
92    pub fn set_antialias(&mut self, antialias: Antialias) {
93        unsafe { ffi::cairo_font_options_set_antialias(self.to_raw_none(), antialias.into()) }
94    }
95
96    #[doc(alias = "cairo_font_options_get_antialias")]
97    #[doc(alias = "get_antialias")]
98    pub fn antialias(&self) -> Antialias {
99        unsafe { Antialias::from(ffi::cairo_font_options_get_antialias(self.to_raw_none())) }
100    }
101
102    #[doc(alias = "cairo_font_options_set_subpixel_order")]
103    pub fn set_subpixel_order(&mut self, order: SubpixelOrder) {
104        unsafe { ffi::cairo_font_options_set_subpixel_order(self.to_raw_none(), order.into()) }
105    }
106
107    #[doc(alias = "cairo_font_options_get_subpixel_order")]
108    #[doc(alias = "get_subpixel_order")]
109    pub fn subpixel_order(&self) -> SubpixelOrder {
110        unsafe {
111            SubpixelOrder::from(ffi::cairo_font_options_get_subpixel_order(
112                self.to_raw_none(),
113            ))
114        }
115    }
116
117    #[doc(alias = "cairo_font_options_set_hint_style")]
118    pub fn set_hint_style(&mut self, hint_style: HintStyle) {
119        unsafe { ffi::cairo_font_options_set_hint_style(self.to_raw_none(), hint_style.into()) }
120    }
121
122    #[doc(alias = "cairo_font_options_get_hint_style")]
123    #[doc(alias = "get_hint_style")]
124    pub fn hint_style(&self) -> HintStyle {
125        unsafe { HintStyle::from(ffi::cairo_font_options_get_hint_style(self.to_raw_none())) }
126    }
127
128    #[doc(alias = "cairo_font_options_set_hint_metrics")]
129    pub fn set_hint_metrics(&mut self, hint_metrics: HintMetrics) {
130        unsafe { ffi::cairo_font_options_set_hint_metrics(self.to_raw_none(), hint_metrics.into()) }
131    }
132
133    #[doc(alias = "cairo_font_options_get_hint_metrics")]
134    #[doc(alias = "get_hint_metrics")]
135    pub fn hint_metrics(&self) -> HintMetrics {
136        unsafe { HintMetrics::from(ffi::cairo_font_options_get_hint_metrics(self.to_raw_none())) }
137    }
138
139    #[cfg(feature = "v1_16")]
140    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
141    #[doc(alias = "cairo_font_options_get_variations")]
142    #[doc(alias = "get_variations")]
143    pub fn variations(&self) -> Option<String> {
144        unsafe { to_optional_string(ffi::cairo_font_options_get_variations(self.to_raw_none())) }
145    }
146
147    #[cfg(feature = "v1_16")]
148    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
149    #[doc(alias = "cairo_font_options_set_variations")]
150    pub fn set_variations(&self, variations: Option<&str>) {
151        unsafe {
152            match variations {
153                Some(v) => {
154                    let v = CString::new(v).unwrap();
155                    ffi::cairo_font_options_set_variations(self.to_raw_none(), v.as_ptr())
156                }
157                None => {
158                    ffi::cairo_font_options_set_variations(self.to_raw_none(), std::ptr::null())
159                }
160            }
161        }
162    }
163
164    #[doc(alias = "cairo_font_options_status")]
165    pub fn status(&self) -> Result<(), Error> {
166        let status = unsafe { ffi::cairo_font_options_status(self.to_raw_none()) };
167        status_to_result(status)
168    }
169}
170
171impl PartialEq for FontOptions {
172    #[doc(alias = "cairo_font_options_equal")]
173    fn eq(&self, other: &FontOptions) -> bool {
174        unsafe { ffi::cairo_font_options_equal(self.to_raw_none(), other.to_raw_none()).as_bool() }
175    }
176}
177
178impl Eq for FontOptions {}
179
180impl hash::Hash for FontOptions {
181    #[doc(alias = "cairo_font_options_hash")]
182    fn hash<H>(&self, state: &mut H)
183    where
184        H: hash::Hasher,
185    {
186        unsafe { hash::Hash::hash(&ffi::cairo_font_options_hash(self.to_raw_none()), state) }
187    }
188}
189
190#[cfg(not(feature = "use_glib"))]
191#[cfg_attr(docsrs, doc(cfg(not(feature = "use_glib"))))]
192impl Drop for FontOptions {
193    #[inline]
194    fn drop(&mut self) {
195        unsafe {
196            ffi::cairo_font_options_destroy(self.to_raw_none());
197        }
198    }
199}
200
201#[cfg(not(feature = "use_glib"))]
202#[cfg_attr(docsrs, doc(cfg(not(feature = "use_glib"))))]
203impl Clone for FontOptions {
204    #[inline]
205    fn clone(&self) -> FontOptions {
206        unsafe { FontOptions::from_raw_full(ffi::cairo_font_options_copy(self.to_raw_none())) }
207    }
208}