cairo/
recording_surface.rs

1// Copyright 2019, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use std::convert::TryFrom;
6use std::fmt;
7use std::ops::Deref;
8
9use enums::{Content, SurfaceType};
10use ffi;
11#[cfg(feature = "use_glib")]
12use glib::translate::*;
13use rectangle::Rectangle;
14
15use surface::Surface;
16
17#[derive(Debug)]
18pub struct RecordingSurface(Surface);
19
20impl TryFrom<Surface> for RecordingSurface {
21    type Error = Surface;
22
23    fn try_from(surface: Surface) -> Result<RecordingSurface, Surface> {
24        if surface.get_type() == SurfaceType::Recording {
25            Ok(RecordingSurface(surface))
26        } else {
27            Err(surface)
28        }
29    }
30}
31
32impl RecordingSurface {
33    pub fn create<T: Into<Option<Rectangle>>>(
34        content: Content,
35        extends: T,
36    ) -> Option<RecordingSurface> {
37        unsafe {
38            let extends = extends.into();
39            let extends = match extends {
40                Some(c) => c.to_raw_none(),
41                None => ::std::ptr::null(),
42            };
43            let p = ffi::cairo_recording_surface_create(content.into(), extends);
44            if p.is_null() {
45                None
46            } else {
47                Some(RecordingSurface(Surface::from_raw_full(p)))
48            }
49        }
50    }
51
52    pub fn get_extents(&self) -> Option<Rectangle> {
53        unsafe {
54            let rectangle: Rectangle = ::std::mem::zeroed();
55            if ffi::cairo_recording_surface_get_extents(self.to_raw_none(), rectangle.to_raw_none())
56                .as_bool()
57            {
58                Some(rectangle)
59            } else {
60                None
61            }
62        }
63    }
64
65    pub fn ink_extents(&self) -> (f64, f64, f64, f64) {
66        let mut x0 = 0.;
67        let mut y0 = 0.;
68        let mut width = 0.;
69        let mut height = 0.;
70
71        unsafe {
72            ffi::cairo_recording_surface_ink_extents(
73                self.to_raw_none(),
74                &mut x0,
75                &mut y0,
76                &mut width,
77                &mut height,
78            );
79        }
80        (x0, y0, width, height)
81    }
82}
83
84#[cfg(feature = "use_glib")]
85impl FromGlibPtrNone<*mut ffi::cairo_surface_t> for RecordingSurface {
86    #[inline]
87    unsafe fn from_glib_none(ptr: *mut ffi::cairo_surface_t) -> RecordingSurface {
88        RecordingSurface(Surface::from_glib_none(ptr))
89    }
90}
91
92#[cfg(feature = "use_glib")]
93impl FromGlibPtrBorrow<*mut ffi::cairo_surface_t> for RecordingSurface {
94    #[inline]
95    unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_surface_t) -> RecordingSurface {
96        RecordingSurface(Surface::from_glib_borrow(ptr))
97    }
98}
99
100#[cfg(feature = "use_glib")]
101impl FromGlibPtrFull<*mut ffi::cairo_surface_t> for RecordingSurface {
102    #[inline]
103    unsafe fn from_glib_full(ptr: *mut ffi::cairo_surface_t) -> RecordingSurface {
104        RecordingSurface(Surface::from_raw_full(ptr))
105    }
106}
107
108#[cfg(feature = "use_glib")]
109gvalue_impl!(
110    RecordingSurface,
111    ffi::cairo_surface_t,
112    ffi::gobject::cairo_gobject_surface_get_type
113);
114
115impl Deref for RecordingSurface {
116    type Target = Surface;
117
118    fn deref(&self) -> &Surface {
119        &self.0
120    }
121}
122
123impl Clone for RecordingSurface {
124    fn clone(&self) -> RecordingSurface {
125        RecordingSurface(self.0.clone())
126    }
127}
128
129impl fmt::Display for RecordingSurface {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        write!(f, "RecordingSurface")
132    }
133}