cairo/font/
font_options.rs

1use ffi;
2#[cfg(feature = "use_glib")]
3use glib::translate::*;
4use std::cmp::PartialEq;
5use std::hash;
6
7#[cfg(any(feature = "v1_16", feature = "dox"))]
8use font::font_face::to_optional_string;
9#[cfg(any(feature = "v1_16", feature = "dox"))]
10use std::ffi::CString;
11
12use enums::{Antialias, HintMetrics, HintStyle, Status, SubpixelOrder};
13
14#[cfg(feature = "use_glib")]
15glib_wrapper! {
16    #[derive(Debug)]
17    pub struct FontOptions(Boxed<ffi::cairo_font_options_t>);
18
19    match fn {
20        copy => |ptr| {
21            let ptr = ffi::cairo_font_options_copy(ptr);
22            let status = ffi::cairo_font_options_status(ptr);
23            Status::from(status).ensure_valid();
24            ptr
25        },
26        free => |ptr| ffi::cairo_font_options_destroy(ptr),
27        get_type => || ffi::gobject::cairo_gobject_font_options_get_type(),
28    }
29}
30
31#[cfg(not(feature = "use_glib"))]
32#[derive(Debug)]
33pub struct FontOptions(*mut ffi::cairo_font_options_t);
34
35impl FontOptions {
36    pub fn new() -> FontOptions {
37        let font_options: FontOptions =
38            unsafe { FontOptions::from_raw_full(ffi::cairo_font_options_create()) };
39        font_options.ensure_status();
40        font_options
41    }
42
43    #[cfg(feature = "use_glib")]
44    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_options_t) -> FontOptions {
45        from_glib_full(ptr)
46    }
47
48    #[cfg(not(feature = "use_glib"))]
49    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_options_t) -> FontOptions {
50        assert!(!ptr.is_null());
51        FontOptions(ptr)
52    }
53
54    #[cfg(feature = "use_glib")]
55    pub fn to_raw_none(&self) -> *mut ffi::cairo_font_options_t {
56        mut_override(self.to_glib_none().0)
57    }
58
59    #[cfg(not(feature = "use_glib"))]
60    pub fn to_raw_none(&self) -> *mut ffi::cairo_font_options_t {
61        self.0
62    }
63
64    pub fn ensure_status(&self) {
65        let status = unsafe { ffi::cairo_font_options_status(self.to_raw_none()) };
66        Status::from(status).ensure_valid()
67    }
68
69    pub fn merge(&mut self, other: &FontOptions) {
70        unsafe { ffi::cairo_font_options_merge(self.to_raw_none(), other.to_raw_none()) }
71    }
72
73    pub fn set_antialias(&mut self, antialias: Antialias) {
74        unsafe { ffi::cairo_font_options_set_antialias(self.to_raw_none(), antialias.into()) }
75    }
76
77    pub fn get_antialias(&self) -> Antialias {
78        unsafe { Antialias::from(ffi::cairo_font_options_get_antialias(self.to_raw_none())) }
79    }
80
81    pub fn set_subpixel_order(&mut self, order: SubpixelOrder) {
82        unsafe { ffi::cairo_font_options_set_subpixel_order(self.to_raw_none(), order.into()) }
83    }
84
85    pub fn get_subpixel_order(&self) -> SubpixelOrder {
86        unsafe {
87            SubpixelOrder::from(ffi::cairo_font_options_get_subpixel_order(
88                self.to_raw_none(),
89            ))
90        }
91    }
92
93    pub fn set_hint_style(&mut self, hint_style: HintStyle) {
94        unsafe { ffi::cairo_font_options_set_hint_style(self.to_raw_none(), hint_style.into()) }
95    }
96
97    pub fn get_hint_style(&self) -> HintStyle {
98        unsafe { HintStyle::from(ffi::cairo_font_options_get_hint_style(self.to_raw_none())) }
99    }
100
101    pub fn set_hint_metrics(&mut self, hint_metrics: HintMetrics) {
102        unsafe { ffi::cairo_font_options_set_hint_metrics(self.to_raw_none(), hint_metrics.into()) }
103    }
104
105    pub fn get_hint_metrics(&self) -> HintMetrics {
106        unsafe { HintMetrics::from(ffi::cairo_font_options_get_hint_metrics(self.to_raw_none())) }
107    }
108
109    #[cfg(any(feature = "v1_16", feature = "dox"))]
110    pub fn get_variations(&self) -> Option<String> {
111        unsafe { to_optional_string(ffi::cairo_font_options_get_variations(self.to_raw_none())) }
112    }
113
114    #[cfg(any(feature = "v1_16", feature = "dox"))]
115    pub fn set_variations<'a, T: Into<Option<&'a str>>>(&self, variations: T) {
116        unsafe {
117            let variations = variations.into();
118            match variations {
119                Some(ref v) => {
120                    let v = CString::new(*v).unwrap();
121                    ffi::cairo_font_options_set_variations(self.to_raw_none(), v.as_ptr())
122                }
123                None => ffi::cairo_font_options_set_variations(self.to_raw_none(), 0 as *const _),
124            }
125        }
126    }
127}
128
129impl PartialEq for FontOptions {
130    fn eq(&self, other: &FontOptions) -> bool {
131        unsafe { ffi::cairo_font_options_equal(self.to_raw_none(), other.to_raw_none()).as_bool() }
132    }
133}
134
135impl Eq for FontOptions {}
136
137impl hash::Hash for FontOptions {
138    fn hash<H>(&self, state: &mut H)
139    where
140        H: hash::Hasher,
141    {
142        unsafe { hash::Hash::hash(&ffi::cairo_font_options_hash(self.to_raw_none()), state) }
143    }
144}
145
146impl Default for FontOptions {
147    fn default() -> Self {
148        Self::new()
149    }
150}
151
152#[cfg(not(feature = "use_glib"))]
153impl Drop for FontOptions {
154    fn drop(&mut self) {
155        unsafe {
156            ffi::cairo_font_options_destroy(self.to_raw_none());
157        }
158    }
159}
160
161#[cfg(not(feature = "use_glib"))]
162impl Clone for FontOptions {
163    fn clone(&self) -> FontOptions {
164        unsafe { FontOptions::from_raw_full(ffi::cairo_font_options_copy(self.to_raw_none())) }
165    }
166}