Skip to main content

cairo/
lib.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#![cfg_attr(feature = "dox", feature(doc_cfg))]
4#![allow(clippy::missing_safety_doc)]
5#![allow(clippy::wrong_self_convention)]
6#![doc = include_str!("../README.md")]
7
8pub use ffi;
9#[cfg(feature = "freetype")]
10pub use freetype_crate as freetype;
11#[cfg(feature = "use_glib")]
12pub use glib;
13
14// Helper macro for our GValue related trait impls
15#[cfg(feature = "use_glib")]
16macro_rules! gvalue_impl {
17    ($name:ty, $ffi_name:ty, $get_type:expr) => {
18        #[allow(unused_imports)]
19        use glib::translate::*;
20
21        impl glib::types::StaticType for $name {
22            fn static_type() -> glib::types::Type {
23                unsafe { from_glib($get_type()) }
24            }
25        }
26
27        impl glib::value::ValueType for $name {
28            type Type = Self;
29        }
30
31        unsafe impl<'a> glib::value::FromValue<'a> for $name {
32            type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;
33
34            unsafe fn from_value(value: &'a glib::Value) -> Self {
35                let ptr = glib::gobject_ffi::g_value_get_boxed(
36                    glib::translate::ToGlibPtr::to_glib_none(value).0,
37                );
38                assert!(!ptr.is_null());
39                <$name as glib::translate::FromGlibPtrNone<*mut $ffi_name>>::from_glib_none(
40                    ptr as *mut $ffi_name,
41                )
42            }
43        }
44
45        impl glib::value::ToValue for $name {
46            fn to_value(&self) -> glib::Value {
47                unsafe {
48                    let mut value =
49                        glib::Value::from_type(<$name as glib::StaticType>::static_type());
50                    glib::gobject_ffi::g_value_set_boxed(
51                        value.to_glib_none_mut().0,
52                        self.to_glib_none().0 as *mut _,
53                    );
54                    value
55                }
56            }
57
58            fn value_type(&self) -> glib::Type {
59                <$name as glib::StaticType>::static_type()
60            }
61        }
62
63        impl glib::value::ToValueOptional for $name {
64            fn to_value_optional(s: Option<&Self>) -> glib::Value {
65                let mut value = glib::Value::for_value_type::<Self>();
66                unsafe {
67                    glib::gobject_ffi::g_value_take_boxed(
68                        value.to_glib_none_mut().0,
69                        glib::translate::ToGlibPtr::to_glib_full(&s) as *mut _,
70                    );
71                }
72
73                value
74            }
75        }
76    };
77}
78
79pub use crate::user_data::UserDataKey;
80
81pub use crate::context::{Context, RectangleList};
82
83pub use crate::paths::{Path, PathSegment, PathSegments};
84
85pub use crate::device::Device;
86
87pub use crate::enums::*;
88
89pub use crate::error::{BorrowError, Error, IoError, Result};
90
91pub use crate::patterns::{
92    Gradient, LinearGradient, Mesh, Pattern, RadialGradient, SolidPattern, SurfacePattern,
93};
94
95pub use crate::font::{
96    FontExtents, FontFace, FontOptions, FontSlant, FontType, FontWeight, Glyph, ScaledFont,
97    TextCluster, TextExtents,
98};
99
100pub use crate::matrices::Matrix;
101
102pub use crate::recording_surface::RecordingSurface;
103pub use crate::rectangle::Rectangle;
104pub use crate::rectangle_int::RectangleInt;
105
106pub use crate::region::Region;
107
108pub use crate::surface::{MappedImageSurface, Surface};
109
110pub use crate::image_surface::{ImageSurface, ImageSurfaceData, ImageSurfaceDataOwned};
111
112#[cfg(any(feature = "pdf", feature = "svg", feature = "ps", feature = "dox"))]
113pub use stream::StreamWithError;
114
115#[cfg(any(feature = "pdf", feature = "dox"))]
116pub use pdf::PdfSurface;
117
118#[cfg(any(feature = "ps", feature = "dox"))]
119pub use ps::PsSurface;
120
121#[cfg(any(feature = "svg", feature = "dox"))]
122pub use svg::SvgSurface;
123
124#[cfg(any(feature = "xcb", feature = "dox"))]
125pub use xcb::{
126    XCBConnection, XCBDrawable, XCBPixmap, XCBRenderPictFormInfo, XCBScreen, XCBSurface,
127    XCBVisualType,
128};
129
130#[macro_use]
131mod surface_macros;
132#[macro_use]
133mod user_data;
134mod constants;
135pub use crate::constants::*;
136mod utils;
137pub use crate::utils::{debug_reset_static_data, version_string, Version};
138mod context;
139mod device;
140mod enums;
141mod error;
142mod font;
143mod image_surface;
144#[cfg(any(feature = "png", feature = "dox"))]
145mod image_surface_png;
146mod matrices;
147mod paths;
148mod patterns;
149mod recording_surface;
150mod rectangle;
151mod rectangle_int;
152mod region;
153mod surface;
154#[cfg(any(feature = "xcb", feature = "dox"))]
155mod xcb;
156
157#[cfg(any(feature = "pdf", feature = "svg", feature = "ps", feature = "dox"))]
158#[macro_use]
159mod stream;
160#[cfg(any(feature = "pdf", feature = "dox"))]
161mod pdf;
162#[cfg(any(feature = "ps", feature = "dox"))]
163mod ps;
164#[cfg(any(feature = "svg", feature = "dox"))]
165mod svg;
166
167#[cfg(any(target_os = "macos", target_os = "ios", feature = "dox"))]
168mod quartz_surface;
169#[cfg(any(target_os = "macos", target_os = "ios", feature = "dox"))]
170pub use quartz_surface::QuartzSurface;
171
172#[cfg(any(all(windows, feature = "win32-surface"), feature = "dox"))]
173mod win32_surface;
174
175#[cfg(any(all(windows, feature = "win32-surface"), feature = "dox"))]
176pub use win32_surface::Win32Surface;
177
178#[cfg(not(feature = "use_glib"))]
179mod borrowed {
180    use std::mem;
181
182    /// Wrapper around values representing borrowed C memory.
183    ///
184    /// This is returned by `from_glib_borrow()` and ensures that the wrapped value
185    /// is never dropped when going out of scope.
186    ///
187    /// Borrowed values must never be passed by value or mutable reference to safe Rust code and must
188    /// not leave the C scope in which they are valid.
189    #[derive(Debug)]
190    pub struct Borrowed<T>(mem::ManuallyDrop<T>);
191
192    impl<T> Borrowed<T> {
193        /// Creates a new borrowed value.
194        pub fn new(val: T) -> Self {
195            Self(mem::ManuallyDrop::new(val))
196        }
197
198        /// Extracts the contained value.
199        ///
200        /// The returned value must never be dropped and instead has to be passed to `mem::forget()` or
201        /// be directly wrapped in `mem::ManuallyDrop` or another `Borrowed` wrapper.
202        pub unsafe fn into_inner(self) -> T {
203            mem::ManuallyDrop::into_inner(self.0)
204        }
205    }
206
207    impl<T> AsRef<T> for Borrowed<T> {
208        fn as_ref(&self) -> &T {
209            &*self.0
210        }
211    }
212
213    impl<T> std::ops::Deref for Borrowed<T> {
214        type Target = T;
215
216        fn deref(&self) -> &T {
217            &*self.0
218        }
219    }
220}
221
222#[cfg(not(feature = "use_glib"))]
223pub use borrowed::Borrowed;
224
225#[cfg(feature = "use_glib")]
226pub(crate) use glib::translate::Borrowed;