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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::TransformCategory;
use glib::translate::*;
use std::fmt;
use std::mem;

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)) }
    }

    /// Inverts the given transform.
    ///
    /// If @self is not invertible, [`None`] is returned.
    /// Note that inverting [`None`] also returns [`None`], which is
    /// the correct inverse of [`None`]. If you need to differentiate
    /// between those cases, you should check @self is not [`None`]
    /// before calling this function.
    ///
    /// # Returns
    ///
    /// The inverted transform
    #[doc(alias = "gsk_transform_invert")]
    #[must_use]
    pub fn invert(&self) -> Option<Transform> {
        unsafe { from_glib_full(ffi::gsk_transform_invert(self.to_glib_full())) }
    }

    /// 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.to_glib_full(),
                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.to_glib_full(), depth)) }
    }

    /// Rotates @self @angle degrees in 2D - or in 3D-speak, around the z axis.
    /// ## `angle`
    /// the rotation angle, in degrees (clockwise)
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_rotate")]
    #[must_use]
    pub fn rotate(&self, angle: f32) -> Option<Transform> {
        unsafe { from_glib_full(ffi::gsk_transform_rotate(self.to_glib_full(), angle)) }
    }

    /// Rotates @self @angle degrees around @axis.
    ///
    /// For a rotation in 2D space, use [`rotate()`][Self::rotate()]
    /// ## `angle`
    /// the rotation angle, in degrees (clockwise)
    /// ## `axis`
    /// The rotation axis
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_rotate_3d")]
    #[must_use]
    pub fn rotate_3d(&self, angle: f32, axis: &graphene::Vec3) -> Option<Transform> {
        unsafe {
            from_glib_full(ffi::gsk_transform_rotate_3d(
                self.to_glib_full(),
                angle,
                axis.to_glib_none().0,
            ))
        }
    }

    /// Scales @self in 2-dimensional space by the given factors.
    ///
    /// Use [`scale_3d()`][Self::scale_3d()] to scale in all 3 dimensions.
    /// ## `factor_x`
    /// scaling factor on the X axis
    /// ## `factor_y`
    /// scaling factor on the Y axis
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_scale")]
    #[must_use]
    pub fn scale(&self, factor_x: f32, factor_y: f32) -> Option<Transform> {
        unsafe {
            from_glib_full(ffi::gsk_transform_scale(
                self.to_glib_full(),
                factor_x,
                factor_y,
            ))
        }
    }

    /// Scales @self by the given factors.
    /// ## `factor_x`
    /// scaling factor on the X axis
    /// ## `factor_y`
    /// scaling factor on the Y axis
    /// ## `factor_z`
    /// scaling factor on the Z axis
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_scale_3d")]
    #[must_use]
    pub fn scale_3d(&self, factor_x: f32, factor_y: f32, factor_z: f32) -> Option<Transform> {
        unsafe {
            from_glib_full(ffi::gsk_transform_scale_3d(
                self.to_glib_full(),
                factor_x,
                factor_y,
                factor_z,
            ))
        }
    }

    /// Applies a skew transform.
    /// ## `skew_x`
    /// skew factor, in degrees, on the X axis
    /// ## `skew_y`
    /// skew factor, in degrees, on the Y axis
    ///
    /// # Returns
    ///
    /// The new transform
    #[cfg(any(feature = "v4_6", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v4_6")))]
    #[doc(alias = "gsk_transform_skew")]
    #[must_use]
    pub fn skew(&self, skew_x: f32, skew_y: f32) -> Option<Transform> {
        unsafe { from_glib_full(ffi::gsk_transform_skew(self.to_glib_full(), skew_x, skew_y)) }
    }

    /// 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 = mem::MaybeUninit::uninit();
            let mut out_yx = mem::MaybeUninit::uninit();
            let mut out_xy = mem::MaybeUninit::uninit();
            let mut out_yy = mem::MaybeUninit::uninit();
            let mut out_dx = mem::MaybeUninit::uninit();
            let mut out_dy = 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(any(feature = "v4_6", feature = "dox"))]
    #[cfg_attr(feature = "dox", 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 = mem::MaybeUninit::uninit();
            let mut out_skew_y = mem::MaybeUninit::uninit();
            let mut out_scale_x = mem::MaybeUninit::uninit();
            let mut out_scale_y = mem::MaybeUninit::uninit();
            let mut out_angle = mem::MaybeUninit::uninit();
            let mut out_dx = mem::MaybeUninit::uninit();
            let mut out_dy = 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 = mem::MaybeUninit::uninit();
            let mut out_scale_y = mem::MaybeUninit::uninit();
            let mut out_dx = mem::MaybeUninit::uninit();
            let mut out_dy = 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 = mem::MaybeUninit::uninit();
            let mut out_dy = 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())
        }
    }

    /// Applies all the operations from @other to @self.
    /// ## `other`
    /// Transform to apply
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_transform")]
    #[must_use]
    pub fn transform(&self, other: Option<&Transform>) -> Option<Transform> {
        unsafe {
            from_glib_full(ffi::gsk_transform_transform(
                self.to_glib_full(),
                other.to_glib_none().0,
            ))
        }
    }

    /// 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
        }
    }

    /// Translates @self in 2-dimensional space by @point.
    /// ## `point`
    /// the point to translate the transform by
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_translate")]
    #[must_use]
    pub fn translate(&self, point: &graphene::Point) -> Option<Transform> {
        unsafe {
            from_glib_full(ffi::gsk_transform_translate(
                self.to_glib_full(),
                point.to_glib_none().0,
            ))
        }
    }

    /// Translates @self by @point.
    /// ## `point`
    /// the point to translate the transform by
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_translate_3d")]
    #[must_use]
    pub fn translate_3d(&self, point: &graphene::Point3D) -> Option<Transform> {
        unsafe {
            from_glib_full(ffi::gsk_transform_translate_3d(
                self.to_glib_full(),
                point.to_glib_none().0,
            ))
        }
    }
}

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 fmt::Display for Transform {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.to_str())
    }
}