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
use std::mem;
use libc::c_void;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use ffi::enums::Format;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use ffi::CGContextRef;
#[cfg(feature = "glib")]
use glib::translate::*;
use ffi;
use ffi::enums::{
Content,
Status,
SurfaceType,
};
#[derive(Debug)]
pub struct Surface(*mut ffi::cairo_surface_t);
impl Surface {
#[doc(hidden)]
pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_surface_t) -> Surface {
assert!(!ptr.is_null());
ffi::cairo_surface_reference(ptr);
Surface(ptr)
}
#[doc(hidden)]
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_surface_t) -> Surface {
assert!(!ptr.is_null());
Surface(ptr)
}
#[doc(hidden)]
pub fn to_raw_none(&self) -> *mut ffi::cairo_surface_t {
self.0
}
pub fn status(&self) -> Status {
unsafe { ffi::cairo_surface_status(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(any(target_os = "macos", target_os = "ios"))]
pub fn quartz_create(format: Format, width: u32, height: u32) -> Surface {
unsafe { Self::from_raw_full(ffi::cairo_quartz_surface_create(format, width, height)) }
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub fn quartz_create_for_cg_context(cg_context: CGContextRef, width: u32, height: u32) -> Surface {
unsafe { Self::from_raw_full(ffi::cairo_quartz_surface_create_for_cg_context(cg_context, width, height)) }
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub fn quartz_get_cg_context(&self) -> CGContextRef {
unsafe { ffi::cairo_quartz_surface_get_cg_context(self.to_raw_none()) }
}
}
#[cfg(feature = "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)
}
}
#[cfg(feature = "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 = "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)
}
}
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) {
unsafe { ffi::cairo_surface_destroy(self.0); }
}
}
pub trait SurfaceExt {
fn flush(&self);
fn finish(&self);
fn get_type(&self) -> SurfaceType;
}
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) }
}
}
pub 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 status = ffi::cairo_surface_set_user_data(self.as_ref().0, mem::transmute(key),
mem::transmute(data), 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> = mem::transmute(data);
drop(data);
}