1use ffi;
2#[cfg(feature = "use_glib")]
3use glib::translate::*;
4use libc::c_char;
5use std::ffi::{CStr, CString};
6
7use enums::{FontSlant, FontType, FontWeight, FtSynthesize, Status};
8
9#[cfg(feature = "use_glib")]
10glib_wrapper! {
11 #[derive(Debug)]
12 pub struct FontFace(Shared<ffi::cairo_font_face_t>);
13
14 match fn {
15 ref => |ptr| ffi::cairo_font_face_reference(ptr),
16 unref => |ptr| ffi::cairo_font_face_destroy(ptr),
17 get_type => || ffi::gobject::cairo_gobject_font_face_get_type(),
18 }
19}
20
21#[cfg(not(feature = "use_glib"))]
22#[derive(Debug)]
23pub struct FontFace(*mut ffi::cairo_font_face_t);
24
25impl FontFace {
26 pub fn toy_create(family: &str, slant: FontSlant, weight: FontWeight) -> FontFace {
27 let font_face: FontFace = unsafe {
28 let family = CString::new(family).unwrap();
29 FontFace::from_raw_full(ffi::cairo_toy_font_face_create(
30 family.as_ptr(),
31 slant.into(),
32 weight.into(),
33 ))
34 };
35 font_face.ensure_status();
36 font_face
37 }
38
39 #[cfg(feature = "use_glib")]
40 pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
41 from_glib_full(ptr)
42 }
43
44 #[cfg(not(feature = "use_glib"))]
45 pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
46 assert!(!ptr.is_null());
47 FontFace(ptr)
48 }
49
50 #[cfg(feature = "use_glib")]
51 pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
52 from_glib_none(ptr)
53 }
54
55 #[cfg(not(feature = "use_glib"))]
56 pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_font_face_t) -> FontFace {
57 assert!(!ptr.is_null());
58 FontFace(ptr)
59 }
60
61 #[cfg(feature = "use_glib")]
62 pub fn to_raw_none(&self) -> *mut ffi::cairo_font_face_t {
63 self.to_glib_none().0
64 }
65
66 #[cfg(not(feature = "use_glib"))]
67 pub fn to_raw_none(&self) -> *mut ffi::cairo_font_face_t {
68 self.0
69 }
70
71 pub fn toy_get_family(&self) -> Option<String> {
72 unsafe { to_optional_string(ffi::cairo_toy_font_face_get_family(self.to_raw_none())) }
73 }
74
75 pub fn toy_get_slant(&self) -> FontSlant {
76 unsafe { FontSlant::from(ffi::cairo_toy_font_face_get_slant(self.to_raw_none())) }
77 }
78
79 pub fn toy_get_weight(&self) -> FontWeight {
80 unsafe { FontWeight::from(ffi::cairo_toy_font_face_get_weight(self.to_raw_none())) }
81 }
82
83 pub fn ensure_status(&self) {
84 let status = unsafe { ffi::cairo_font_face_status(self.to_raw_none()) };
85 Status::from(status).ensure_valid()
86 }
87
88 pub fn get_type(&self) -> FontType {
89 unsafe { FontType::from(ffi::cairo_font_face_get_type(self.to_raw_none())) }
90 }
91
92 pub fn get_reference_count(&self) -> usize {
93 unsafe { ffi::cairo_font_face_get_reference_count(self.to_raw_none()) as usize }
94 }
95
96 pub fn get_synthesize(&self) -> FtSynthesize {
97 unsafe { FtSynthesize::from(ffi::cairo_ft_font_face_get_synthesize(self.to_raw_none())) }
98 }
99
100 pub fn set_synthesize(&self, synth_flags: FtSynthesize) {
101 unsafe { ffi::cairo_ft_font_face_set_synthesize(self.to_raw_none(), synth_flags.into()) }
102 }
103
104 pub fn unset_synthesize(&self, synth_flags: FtSynthesize) {
105 unsafe { ffi::cairo_ft_font_face_unset_synthesize(self.to_raw_none(), synth_flags.into()) }
106 }
107
108 user_data_methods! {
109 ffi::cairo_font_face_get_user_data,
110 ffi::cairo_font_face_set_user_data,
111 }
112}
113
114#[cfg(not(feature = "use_glib"))]
115impl Drop for FontFace {
116 fn drop(&mut self) {
117 unsafe {
118 ffi::cairo_font_face_destroy(self.to_raw_none());
119 }
120 }
121}
122
123#[cfg(not(feature = "use_glib"))]
124impl Clone for FontFace {
125 fn clone(&self) -> FontFace {
126 unsafe { FontFace::from_raw_none(self.to_raw_none()) }
127 }
128}
129
130pub(crate) unsafe fn to_optional_string(str: *const c_char) -> Option<String> {
131 if str.is_null() {
132 None
133 } else {
134 Some(String::from_utf8_lossy(CStr::from_ptr(str).to_bytes()).into_owned())
135 }
136}