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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use std::ffi::CString;
use std::path::Path;
use enums::{Content, DeviceType, ScriptMode, Status};
use ffi;
#[cfg(feature = "use_glib")]
use glib::translate::*;
use recording_surface::RecordingSurface;
use std::fmt;
use surface::Surface;
#[derive(Debug)]
pub struct Device(*mut ffi::cairo_device_t, bool);
impl Device {
pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_device_t) -> Device {
assert!(!ptr.is_null());
ffi::cairo_device_reference(ptr);
Device(ptr, false)
}
pub unsafe fn from_raw_borrow(ptr: *mut ffi::cairo_device_t) -> Device {
assert!(!ptr.is_null());
Device(ptr, true)
}
pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_device_t) -> Device {
assert!(!ptr.is_null());
Device(ptr, false)
}
pub fn to_raw_none(&self) -> *mut ffi::cairo_device_t {
self.0
}
pub fn create<P: AsRef<Path>>(filename: P) -> Option<Device> {
unsafe {
let filename = filename.as_ref().to_string_lossy().into_owned();
let filename = CString::new(filename).unwrap();
let p = ffi::cairo_script_create(filename.as_ptr());
if p.is_null() {
None
} else {
Some(Self::from_raw_full(p))
}
}
}
pub fn from_recording_surface(&self, surface: &RecordingSurface) -> Status {
unsafe {
Status::from(ffi::cairo_script_from_recording_surface(
self.to_raw_none(),
surface.to_raw_none(),
))
}
}
pub fn get_mode(&self) -> ScriptMode {
unsafe { ScriptMode::from(ffi::cairo_script_get_mode(self.to_raw_none())) }
}
pub fn set_mode(&self, mode: ScriptMode) {
unsafe { ffi::cairo_script_set_mode(self.to_raw_none(), mode.into()) }
}
pub fn surface_create(
&self,
content: Content,
width: f64,
height: f64,
) -> Result<Surface, Status> {
unsafe {
Ok(Surface::from_raw_full(ffi::cairo_script_surface_create(
self.to_raw_none(),
content.into(),
width,
height,
))?)
}
}
pub fn surface_create_for_target(&self, target: &Surface) -> Result<Surface, Status> {
unsafe {
Ok(Surface::from_raw_full(
ffi::cairo_script_surface_create_for_target(
self.to_raw_none(),
target.to_raw_none(),
),
)?)
}
}
pub fn write_comment(&self, comment: &str) {
unsafe {
let len = comment.len();
let comment = CString::new(comment).unwrap();
ffi::cairo_script_write_comment(self.to_raw_none(), comment.as_ptr(), len as i32)
}
}
pub fn status(&self) -> Status {
unsafe { Status::from(ffi::cairo_device_status(self.to_raw_none())) }
}
pub fn finish(&self) {
unsafe { ffi::cairo_device_finish(self.to_raw_none()) }
}
pub fn flush(&self) {
unsafe { ffi::cairo_device_flush(self.to_raw_none()) }
}
pub fn get_type(&self) -> DeviceType {
unsafe { DeviceType::from(ffi::cairo_device_get_type(self.to_raw_none())) }
}
pub fn acquire(&self) -> Status {
unsafe { Status::from(ffi::cairo_device_acquire(self.to_raw_none())) }
}
pub fn release(&self) {
unsafe { ffi::cairo_device_release(self.to_raw_none()) }
}
pub fn observer_elapsed(&self) -> f64 {
unsafe { ffi::cairo_device_observer_elapsed(self.to_raw_none()) }
}
pub fn observer_fill_elapsed(&self) -> f64 {
unsafe { ffi::cairo_device_observer_fill_elapsed(self.to_raw_none()) }
}
pub fn observer_glyphs_elapsed(&self) -> f64 {
unsafe { ffi::cairo_device_observer_glyphs_elapsed(self.to_raw_none()) }
}
pub fn observer_mask_elapsed(&self) -> f64 {
unsafe { ffi::cairo_device_observer_mask_elapsed(self.to_raw_none()) }
}
pub fn observer_paint_elapsed(&self) -> f64 {
unsafe { ffi::cairo_device_observer_paint_elapsed(self.to_raw_none()) }
}
pub fn observer_stroke_elapsed(&self) -> f64 {
unsafe { ffi::cairo_device_observer_stroke_elapsed(self.to_raw_none()) }
}
#[cfg(any(feature = "xlib", feature = "xcb", feature = "dox"))]
pub fn debug_cap_xrender_version(&self, major_version: i32, minor_version: i32) {
unsafe {
match self.get_type() {
DeviceType::Xlib => {
#[cfg(feature = "xlib")]
{
ffi::cairo_xlib_device_debug_cap_xrender_version(
self.to_raw_none(),
major_version,
minor_version,
)
}
#[cfg(not(feature = "xlib"))]
{
panic!("you need to enable \"xlib\" feature")
}
}
DeviceType::Xcb => {
#[cfg(feature = "xcb")]
{
ffi::cairo_xcb_device_debug_cap_xrender_version(
self.to_raw_none(),
major_version,
minor_version,
)
}
#[cfg(not(feature = "xcb"))]
{
panic!("you need to enable \"xcb\" feature")
}
}
d => panic!("invalid device type: {}", d),
}
}
}
#[cfg(any(feature = "xlib", feature = "xcb", feature = "dox"))]
pub fn debug_get_precision(&self) -> i32 {
unsafe {
match self.get_type() {
DeviceType::Xlib => {
#[cfg(feature = "xlib")]
{
ffi::cairo_xlib_device_debug_get_precision(self.to_raw_none())
}
#[cfg(not(feature = "xlib"))]
{
panic!("you need to enable \"xlib\" feature")
}
}
DeviceType::Xcb => {
#[cfg(feature = "xcb")]
{
ffi::cairo_xcb_device_debug_get_precision(self.to_raw_none())
}
#[cfg(not(feature = "xcb"))]
{
panic!("you need to enable \"xcb\" feature")
}
}
d => panic!("invalid device type: {}", d),
}
}
}
#[cfg(any(feature = "xlib", feature = "xcb", feature = "dox"))]
pub fn debug_set_precision(&self, precision: i32) {
unsafe {
match self.get_type() {
DeviceType::Xlib => {
#[cfg(feature = "xlib")]
{
ffi::cairo_xlib_device_debug_set_precision(self.to_raw_none(), precision)
}
#[cfg(not(feature = "xlib"))]
{
panic!("you need to enable \"xlib\" feature")
}
}
DeviceType::Xcb => {
#[cfg(feature = "xcb")]
{
ffi::cairo_xcb_device_debug_set_precision(self.to_raw_none(), precision)
}
#[cfg(not(feature = "xcb"))]
{
panic!("you need to enable \"xcb\" feature")
}
}
d => panic!("invalid device type: {}", d),
}
}
}
user_data_methods! {
ffi::cairo_device_get_user_data,
ffi::cairo_device_set_user_data,
}
}
#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_device_t> for Device {
type Storage = &'a Device;
#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_device_t, Self> {
Stash(self.to_raw_none(), self)
}
#[inline]
fn to_glib_full(&self) -> *mut ffi::cairo_device_t {
unsafe { ffi::cairo_device_reference(self.to_raw_none()) }
}
}
#[cfg(feature = "use_glib")]
impl FromGlibPtrNone<*mut ffi::cairo_device_t> for Device {
#[inline]
unsafe fn from_glib_none(ptr: *mut ffi::cairo_device_t) -> Device {
Self::from_raw_none(ptr)
}
}
#[cfg(feature = "use_glib")]
impl FromGlibPtrBorrow<*mut ffi::cairo_device_t> for Device {
#[inline]
unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_device_t) -> Device {
Self::from_raw_borrow(ptr)
}
}
#[cfg(feature = "use_glib")]
impl FromGlibPtrFull<*mut ffi::cairo_device_t> for Device {
#[inline]
unsafe fn from_glib_full(ptr: *mut ffi::cairo_device_t) -> Device {
Self::from_raw_full(ptr)
}
}
#[cfg(feature = "use_glib")]
gvalue_impl!(
Device,
ffi::cairo_device_t,
ffi::gobject::cairo_gobject_device_get_type
);
impl Clone for Device {
fn clone(&self) -> Device {
unsafe { Self::from_raw_none(ffi::cairo_device_reference(self.0)) }
}
}
impl Drop for Device {
fn drop(&mut self) {
if !self.1 {
unsafe {
ffi::cairo_device_destroy(self.0);
}
}
}
}
impl fmt::Display for Device {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Device")
}
}