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