1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::mem;
use libc::c_void;
#[cfg(feature = "use_glib")]
use glib::translate::*;
use ffi;
use ffi::enums::{
Content,
Status,
SurfaceType,
};
#[derive(Debug)]
pub struct Surface(*mut ffi::cairo_surface_t, bool);
impl Surface {
pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_surface_t) -> Surface {
assert!(!ptr.is_null());
ffi::cairo_surface_reference(ptr);
Surface(ptr, false)
}
pub unsafe fn from_raw_borrow(ptr: *mut ffi::cairo_surface_t) -> Surface {
assert!(!ptr.is_null());
Surface(ptr, true)
}
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Surface {
assert!(!ptr.is_null());
Surface(ptr, false)
}
pub fn to_raw_none(&self) -> *mut ffi::cairo_surface_t {
self.0
}
pub fn create_similar(&self, content: Content, width: i32, height: i32) -> Surface {
unsafe { Self::from_raw_full(ffi::cairo_surface_create_similar(self.0, content, width, height)) }
}
}
#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for Surface {
type Storage = &'a Surface;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_surface_t, Self> {
Stash(self.to_raw_none(), self)
}
#[inline]
fn to_glib_full(&self) -> *mut ffi::cairo_surface_t {
unsafe {
ffi::cairo_surface_reference(self.to_raw_none())
}
}
}
#[cfg(feature = "use_glib")]
impl FromGlibPtrNone<*mut ffi::cairo_surface_t> for Surface {
#[inline]
unsafe fn from_glib_none(ptr: *mut ffi::cairo_surface_t) -> Surface {
Self::from_raw_none(ptr)
}
}
#[cfg(feature = "use_glib")]
impl FromGlibPtrBorrow<*mut ffi::cairo_surface_t> for Surface {
#[inline]
unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_surface_t) -> Surface {
Self::from_raw_borrow(ptr)
}
}
#[cfg(feature = "use_glib")]
impl FromGlibPtrFull<*mut ffi::cairo_surface_t> for Surface {
#[inline]
unsafe fn from_glib_full(ptr: *mut ffi::cairo_surface_t) -> Surface {
Self::from_raw_full(ptr)
}
}
#[cfg(feature = "use_glib")]
gvalue_impl!(Surface, ffi::cairo_surface_t, ffi::gobject::cairo_gobject_surface_get_type);
impl AsRef<Surface> for Surface {
fn as_ref(&self) -> &Surface {
self
}
}
impl Clone for Surface {
fn clone(&self) -> Surface {
unsafe { Self::from_raw_none(self.0) }
}
}
impl Drop for Surface {
fn drop(&mut self) {
if !self.1 {
unsafe { ffi::cairo_surface_destroy(self.0); }
}
}
}
pub trait SurfaceExt {
fn flush(&self);
fn finish(&self);
fn get_type(&self) -> SurfaceType;
fn status(&self) -> Status;
}
impl<O: AsRef<Surface>> SurfaceExt for O {
fn flush(&self) {
unsafe { ffi::cairo_surface_flush(self.as_ref().0); }
}
fn finish(&self) {
unsafe { ffi::cairo_surface_finish(self.as_ref().0); }
}
fn get_type(&self) -> SurfaceType {
unsafe { ffi::cairo_surface_get_type(self.as_ref().0) }
}
fn status(&self) -> Status {
unsafe { ffi::cairo_surface_status(self.as_ref().0) }
}
}
pub(crate) trait SurfacePriv {
unsafe fn set_user_data<K, T>(&self, key: &K, data: Box<T>) -> Result<(), Status>;
}
impl<O: AsRef<Surface>> SurfacePriv for O {
unsafe fn set_user_data<K, T>(&self, key: &K, data: Box<T>) -> Result<(), Status> {
let ptr: *mut T = Box::into_raw(data);
assert_eq!(mem::size_of::<*mut c_void>(), mem::size_of_val(&ptr));
let status = ffi::cairo_surface_set_user_data(self.as_ref().0, key as *const _ as *mut _,
ptr as *mut c_void, Some(unbox::<T>));
match status {
Status::Success => Ok(()),
x => Err(x),
}
}
}
unsafe extern "C" fn unbox<T>(data: *mut c_void) {
let data: Box<T> = Box::from_raw(data as *mut T);
drop(data);
}