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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::TransformCategory;
use glib::translate::*;

glib::wrapper! {
    /// [`Transform`][crate::Transform] is an object to describe transform matrices.
    ///
    /// Unlike [`graphene::Matrix`][crate::graphene::Matrix], [`Transform`][crate::Transform] retains the steps in how
    /// a transform was constructed, and allows inspecting them. It is modeled
    /// after the way CSS describes transforms.
    ///
    /// [`Transform`][crate::Transform] objects are immutable and cannot be changed after creation.
    /// This means code can safely expose them as properties of objects without
    /// having to worry about others changing them.
    #[derive(Debug, PartialOrd, Ord, Hash)]
    pub struct Transform(Shared<ffi::GskTransform>);

    match fn {
        ref => |ptr| ffi::gsk_transform_ref(ptr),
        unref => |ptr| ffi::gsk_transform_unref(ptr),
        type_ => || ffi::gsk_transform_get_type(),
    }
}

impl Transform {
    #[doc(alias = "gsk_transform_new")]
    pub fn new() -> Transform {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gsk_transform_new()) }
    }

    #[doc(alias = "gsk_transform_equal")]
    fn equal(&self, second: &Transform) -> bool {
        unsafe {
            from_glib(ffi::gsk_transform_equal(
                self.to_glib_none().0,
                second.to_glib_none().0,
            ))
        }
    }

    /// Returns the category this transform belongs to.
    ///
    /// # Returns
    ///
    /// The category of the transform
    #[doc(alias = "gsk_transform_get_category")]
    #[doc(alias = "get_category")]
    pub fn category(&self) -> TransformCategory {
        unsafe { from_glib(ffi::gsk_transform_get_category(self.to_glib_none().0)) }
    }

    /// Multiplies @self with the given @matrix.
    /// ## `matrix`
    /// the matrix to multiply @self with
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_matrix")]
    #[must_use]
    pub fn matrix(self, matrix: &graphene::Matrix) -> Transform {
        unsafe {
            from_glib_full(ffi::gsk_transform_matrix(
                self.into_glib_ptr(),
                matrix.to_glib_none().0,
            ))
        }
    }

    /// Applies a perspective projection transform.
    ///
    /// This transform scales points in X and Y based on their Z value,
    /// scaling points with positive Z values away from the origin, and
    /// those with negative Z values towards the origin. Points
    /// on the z=0 plane are unchanged.
    /// ## `depth`
    /// distance of the z=0 plane. Lower values give a more
    ///   flattened pyramid and therefore a more pronounced
    ///   perspective effect.
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_perspective")]
    #[must_use]
    pub fn perspective(self, depth: f32) -> Transform {
        unsafe { from_glib_full(ffi::gsk_transform_perspective(self.into_glib_ptr(), depth)) }
    }

    /// Converts a [`Transform`][crate::Transform] to a 2D transformation matrix.
    ///
    /// @self must be a 2D transformation. If you are not
    /// sure, use gsk_transform_get_category() >=
    /// [`TransformCategory::_2d`][crate::TransformCategory::_2d] to check.
    ///
    /// The returned values have the following layout:
    ///
    /// ```text
    ///   | xx yx |   |  a  b  0 |
    ///   | xy yy | = |  c  d  0 |
    ///   | dx dy |   | tx ty  1 |
    /// ```
    ///
    /// This function can be used to convert between a [`Transform`][crate::Transform]
    /// and a matrix type from other 2D drawing libraries, in particular
    /// Cairo.
    ///
    /// # Returns
    ///
    ///
    /// ## `out_xx`
    /// return location for the xx member
    ///
    /// ## `out_yx`
    /// return location for the yx member
    ///
    /// ## `out_xy`
    /// return location for the xy member
    ///
    /// ## `out_yy`
    /// return location for the yy member
    ///
    /// ## `out_dx`
    /// return location for the x0 member
    ///
    /// ## `out_dy`
    /// return location for the y0 member
    #[doc(alias = "gsk_transform_to_2d")]
    pub fn to_2d(&self) -> (f32, f32, f32, f32, f32, f32) {
        unsafe {
            let mut out_xx = std::mem::MaybeUninit::uninit();
            let mut out_yx = std::mem::MaybeUninit::uninit();
            let mut out_xy = std::mem::MaybeUninit::uninit();
            let mut out_yy = std::mem::MaybeUninit::uninit();
            let mut out_dx = std::mem::MaybeUninit::uninit();
            let mut out_dy = std::mem::MaybeUninit::uninit();
            ffi::gsk_transform_to_2d(
                self.to_glib_none().0,
                out_xx.as_mut_ptr(),
                out_yx.as_mut_ptr(),
                out_xy.as_mut_ptr(),
                out_yy.as_mut_ptr(),
                out_dx.as_mut_ptr(),
                out_dy.as_mut_ptr(),
            );
            (
                out_xx.assume_init(),
                out_yx.assume_init(),
                out_xy.assume_init(),
                out_yy.assume_init(),
                out_dx.assume_init(),
                out_dy.assume_init(),
            )
        }
    }

    /// Converts a [`Transform`][crate::Transform] to 2D transformation factors.
    ///
    /// To recreate an equivalent transform from the factors returned
    /// by this function, use
    ///
    ///     gsk_transform_skew (
    ///         gsk_transform_scale (
    ///             gsk_transform_rotate (
    ///                 gsk_transform_translate (NULL, &GRAPHENE_POINT_T (dx, dy)),
    ///                 angle),
    ///             scale_x, scale_y),
    ///         skew_x, skew_y)
    ///
    /// @self must be a 2D transformation. If you are not sure, use
    ///
    ///     gsk_transform_get_category() >= [`TransformCategory::_2d`][crate::TransformCategory::_2d]
    ///
    /// to check.
    ///
    /// # Returns
    ///
    ///
    /// ## `out_skew_x`
    /// return location for the skew factor
    ///   in the  x direction
    ///
    /// ## `out_skew_y`
    /// return location for the skew factor
    ///   in the  y direction
    ///
    /// ## `out_scale_x`
    /// return location for the scale
    ///   factor in the x direction
    ///
    /// ## `out_scale_y`
    /// return location for the scale
    ///   factor in the y direction
    ///
    /// ## `out_angle`
    /// return location for the rotation angle
    ///
    /// ## `out_dx`
    /// return location for the translation
    ///   in the x direction
    ///
    /// ## `out_dy`
    /// return location for the translation
    ///   in the y direction
    #[cfg(feature = "v4_6")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
    #[doc(alias = "gsk_transform_to_2d_components")]
    pub fn to_2d_components(&self) -> (f32, f32, f32, f32, f32, f32, f32) {
        unsafe {
            let mut out_skew_x = std::mem::MaybeUninit::uninit();
            let mut out_skew_y = std::mem::MaybeUninit::uninit();
            let mut out_scale_x = std::mem::MaybeUninit::uninit();
            let mut out_scale_y = std::mem::MaybeUninit::uninit();
            let mut out_angle = std::mem::MaybeUninit::uninit();
            let mut out_dx = std::mem::MaybeUninit::uninit();
            let mut out_dy = std::mem::MaybeUninit::uninit();
            ffi::gsk_transform_to_2d_components(
                self.to_glib_none().0,
                out_skew_x.as_mut_ptr(),
                out_skew_y.as_mut_ptr(),
                out_scale_x.as_mut_ptr(),
                out_scale_y.as_mut_ptr(),
                out_angle.as_mut_ptr(),
                out_dx.as_mut_ptr(),
                out_dy.as_mut_ptr(),
            );
            (
                out_skew_x.assume_init(),
                out_skew_y.assume_init(),
                out_scale_x.assume_init(),
                out_scale_y.assume_init(),
                out_angle.assume_init(),
                out_dx.assume_init(),
                out_dy.assume_init(),
            )
        }
    }

    /// Converts a [`Transform`][crate::Transform] to 2D affine transformation factors.
    ///
    /// To recreate an equivalent transform from the factors returned
    /// by this function, use
    ///
    ///     gsk_transform_scale (gsk_transform_translate (NULL,
    ///                                                   &GRAPHENE_POINT_T (dx, dy)),
    ///                          sx, sy)
    ///
    /// @self must be a 2D affine transformation. If you are not
    /// sure, use
    ///
    ///     gsk_transform_get_category() >= [`TransformCategory::_2dAffine`][crate::TransformCategory::_2dAffine]
    ///
    /// to check.
    ///
    /// # Returns
    ///
    ///
    /// ## `out_scale_x`
    /// return location for the scale
    ///   factor in the x direction
    ///
    /// ## `out_scale_y`
    /// return location for the scale
    ///   factor in the y direction
    ///
    /// ## `out_dx`
    /// return location for the translation
    ///   in the x direction
    ///
    /// ## `out_dy`
    /// return location for the translation
    ///   in the y direction
    #[doc(alias = "gsk_transform_to_affine")]
    pub fn to_affine(&self) -> (f32, f32, f32, f32) {
        unsafe {
            let mut out_scale_x = std::mem::MaybeUninit::uninit();
            let mut out_scale_y = std::mem::MaybeUninit::uninit();
            let mut out_dx = std::mem::MaybeUninit::uninit();
            let mut out_dy = std::mem::MaybeUninit::uninit();
            ffi::gsk_transform_to_affine(
                self.to_glib_none().0,
                out_scale_x.as_mut_ptr(),
                out_scale_y.as_mut_ptr(),
                out_dx.as_mut_ptr(),
                out_dy.as_mut_ptr(),
            );
            (
                out_scale_x.assume_init(),
                out_scale_y.assume_init(),
                out_dx.assume_init(),
                out_dy.assume_init(),
            )
        }
    }

    /// Computes the actual value of @self and stores it in @out_matrix.
    ///
    /// The previous value of @out_matrix will be ignored.
    ///
    /// # Returns
    ///
    ///
    /// ## `out_matrix`
    /// The matrix to set
    #[doc(alias = "gsk_transform_to_matrix")]
    pub fn to_matrix(&self) -> graphene::Matrix {
        unsafe {
            let mut out_matrix = graphene::Matrix::uninitialized();
            ffi::gsk_transform_to_matrix(self.to_glib_none().0, out_matrix.to_glib_none_mut().0);
            out_matrix
        }
    }

    /// Converts a matrix into a string that is suitable for printing.
    ///
    /// The resulting string can be parsed with [`parse()`][Self::parse()].
    ///
    /// This is a wrapper around `Gsk::Transform::print()`.
    ///
    /// # Returns
    ///
    /// A new string for @self
    #[doc(alias = "gsk_transform_to_string")]
    #[doc(alias = "to_string")]
    pub fn to_str(&self) -> glib::GString {
        unsafe { from_glib_full(ffi::gsk_transform_to_string(self.to_glib_none().0)) }
    }

    /// Converts a [`Transform`][crate::Transform] to a translation operation.
    ///
    /// @self must be a 2D transformation. If you are not
    /// sure, use
    ///
    ///     gsk_transform_get_category() >= [`TransformCategory::_2dTranslate`][crate::TransformCategory::_2dTranslate]
    ///
    /// to check.
    ///
    /// # Returns
    ///
    ///
    /// ## `out_dx`
    /// return location for the translation
    ///   in the x direction
    ///
    /// ## `out_dy`
    /// return location for the translation
    ///   in the y direction
    #[doc(alias = "gsk_transform_to_translate")]
    pub fn to_translate(&self) -> (f32, f32) {
        unsafe {
            let mut out_dx = std::mem::MaybeUninit::uninit();
            let mut out_dy = std::mem::MaybeUninit::uninit();
            ffi::gsk_transform_to_translate(
                self.to_glib_none().0,
                out_dx.as_mut_ptr(),
                out_dy.as_mut_ptr(),
            );
            (out_dx.assume_init(), out_dy.assume_init())
        }
    }

    /// Transforms a [`graphene::Rect`][crate::graphene::Rect] using the given transform @self.
    ///
    /// The result is the bounding box containing the coplanar quad.
    /// ## `rect`
    /// a [`graphene::Rect`][crate::graphene::Rect]
    ///
    /// # Returns
    ///
    ///
    /// ## `out_rect`
    /// return location for the bounds
    ///   of the transformed rectangle
    #[doc(alias = "gsk_transform_transform_bounds")]
    pub fn transform_bounds(&self, rect: &graphene::Rect) -> graphene::Rect {
        unsafe {
            let mut out_rect = graphene::Rect::uninitialized();
            ffi::gsk_transform_transform_bounds(
                self.to_glib_none().0,
                rect.to_glib_none().0,
                out_rect.to_glib_none_mut().0,
            );
            out_rect
        }
    }

    /// Transforms a [`graphene::Point`][crate::graphene::Point] using the given transform @self.
    /// ## `point`
    /// a [`graphene::Point`][crate::graphene::Point]
    ///
    /// # Returns
    ///
    ///
    /// ## `out_point`
    /// return location for
    ///   the transformed point
    #[doc(alias = "gsk_transform_transform_point")]
    pub fn transform_point(&self, point: &graphene::Point) -> graphene::Point {
        unsafe {
            let mut out_point = graphene::Point::uninitialized();
            ffi::gsk_transform_transform_point(
                self.to_glib_none().0,
                point.to_glib_none().0,
                out_point.to_glib_none_mut().0,
            );
            out_point
        }
    }
}

impl Default for Transform {
    fn default() -> Self {
        Self::new()
    }
}

impl PartialEq for Transform {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.equal(other)
    }
}

impl Eq for Transform {}

impl std::fmt::Display for Transform {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&self.to_str())
    }
}