gdk4/
cairo_interaction.rs

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
// Take a look at the license at the top of the repository in the LICENSE file.

use cairo::{Context, Region};
use gdk_pixbuf::Pixbuf;
use glib::translate::*;

use crate::{ffi, Rectangle, Surface, RGBA};

// rustdoc-stripper-ignore-next
/// Trait containing integration methods with [`cairo::Surface`].
pub trait GdkCairoSurfaceExt {
    /// Creates region that covers the area where the given
    /// @surface is more than 50% opaque.
    ///
    /// This function takes into account device offsets that might be
    /// set with cairo_surface_set_device_offset().
    /// ## `surface`
    /// a cairo surface
    ///
    /// # Returns
    ///
    /// A [`cairo::Region`][crate::cairo::Region]
    #[doc(alias = "gdk_cairo_region_create_from_surface")]
    fn create_region(&self) -> Region;
}

impl GdkCairoSurfaceExt for cairo::Surface {
    fn create_region(&self) -> Region {
        unsafe {
            from_glib_full(ffi::gdk_cairo_region_create_from_surface(
                self.to_glib_none().0,
            ))
        }
    }
}

// rustdoc-stripper-ignore-next
/// Trait containing integration methods with [`cairo::Context`].
pub trait GdkCairoContextExt: sealed::Sealed {
    // rustdoc-stripper-ignore-next
    /// # Safety
    ///
    /// It's the responsibility of the caller to ensure that source
    /// is a valid GL resource.
    // rustdoc-stripper-ignore-next-stop
    /// The main way to not draw GL content in GTK.
    ///
    /// It takes a render buffer ID (@source_type == GL_RENDERBUFFER) or a texture
    /// id (@source_type == GL_TEXTURE) and draws it onto @cr with an OVER operation,
    /// respecting the current clip. The top left corner of the rectangle specified
    /// by @x, @y, @width and @height will be drawn at the current (0,0) position of
    /// the [`cairo::Context`][crate::cairo::Context].
    ///
    /// This will work for *all* [`cairo::Context`][crate::cairo::Context], as long as @surface is realized, but the
    /// fallback implementation that reads back the pixels from the buffer may be
    /// used in the general case. In the case of direct drawing to a surface with
    /// no special effects applied to @cr it will however use a more efficient
    /// approach.
    ///
    /// For GL_RENDERBUFFER the code will always fall back to software for buffers
    /// with alpha components, so make sure you use GL_TEXTURE if using alpha.
    ///
    /// Calling this may change the current GL context.
    ///
    /// # Deprecated since 4.6
    ///
    /// The function is overly complex and produces broken output
    ///   in various combinations of arguments. If you want to draw with GL textures
    ///   in GTK, use [`GLTexture::new()`][crate::GLTexture::new()]; if you want to use that texture in
    ///   Cairo, use [`TextureExtManual::download()`][crate::prelude::TextureExtManual::download()] to download the data into a Cairo
    ///   image surface.
    /// ## `cr`
    /// a cairo context
    /// ## `surface`
    /// The surface we're rendering for (not necessarily into)
    /// ## `source`
    /// The GL ID of the source buffer
    /// ## `source_type`
    /// The type of the @source
    /// ## `buffer_scale`
    /// The scale-factor that the @source buffer is allocated for
    /// ## `x`
    /// The source x position in @source to start copying from in GL coordinates
    /// ## `y`
    /// The source y position in @source to start copying from in GL coordinates
    /// ## `width`
    /// The width of the region to draw
    /// ## `height`
    /// The height of the region to draw
    #[doc(alias = "gdk_cairo_draw_from_gl")]
    #[allow(clippy::too_many_arguments)]
    unsafe fn draw_from_gl(
        &self,
        surface: &Surface,
        source: i32,
        source_type: i32,
        buffer_scale: i32,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
    ) {
        skip_assert_initialized!();
        ffi::gdk_cairo_draw_from_gl(
            self.to_raw(),
            surface.to_glib_none().0,
            source,
            source_type,
            buffer_scale,
            x,
            y,
            width,
            height,
        );
    }

    /// Sets the specified [`RGBA`][crate::RGBA] as the source color of @cr.
    /// ## `cr`
    /// a cairo context
    /// ## `rgba`
    /// a [`RGBA`][crate::RGBA]
    #[doc(alias = "gdk_cairo_set_source_rgba")]
    #[doc(alias = "set_source_rgba")]
    fn set_source_color(&self, rgba: &RGBA) {
        unsafe {
            ffi::gdk_cairo_set_source_rgba(self.to_raw(), rgba.to_glib_none().0);
        }
    }

    /// Sets the given pixbuf as the source pattern for @cr.
    ///
    /// The pattern has an extend mode of `CAIRO_EXTEND_NONE` and is aligned
    /// so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
    /// ## `cr`
    /// a cairo context
    /// ## `pixbuf`
    /// a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
    /// ## `pixbuf_x`
    /// X coordinate of location to place upper left corner of @pixbuf
    /// ## `pixbuf_y`
    /// Y coordinate of location to place upper left corner of @pixbuf
    #[doc(alias = "gdk_cairo_set_source_pixbuf")]
    fn set_source_pixbuf(&self, pixbuf: &Pixbuf, x: f64, y: f64) {
        unsafe {
            ffi::gdk_cairo_set_source_pixbuf(self.to_raw(), pixbuf.to_glib_none().0, x, y);
        }
    }

    /// Adds the given rectangle to the current path of @cr.
    /// ## `cr`
    /// a cairo context
    /// ## `rectangle`
    /// a [`Rectangle`][crate::Rectangle]
    #[doc(alias = "gdk_cairo_rectangle")]
    fn add_rectangle(&self, rectangle: &Rectangle) {
        unsafe {
            ffi::gdk_cairo_rectangle(self.to_raw(), rectangle.to_glib_none().0);
        }
    }

    /// Adds the given region to the current path of @cr.
    /// ## `cr`
    /// a cairo context
    /// ## `region`
    /// a [`cairo::Region`][crate::cairo::Region]
    #[doc(alias = "gdk_cairo_region")]
    fn add_region(&self, region: &Region) {
        unsafe {
            ffi::gdk_cairo_region(self.to_raw(), region.to_glib_none().0);
        }
    }
}

impl GdkCairoContextExt for Context {}

mod sealed {
    use cairo::{ffi::cairo_t, Context};

    pub trait Sealed {
        fn to_raw(&self) -> *mut cairo_t;
    }

    impl Sealed for Context {
        fn to_raw(&self) -> *mut cairo_t {
            self.to_raw_none()
        }
    }
}