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

use glib::translate::*;

use crate::Transform;

impl Transform {
    /// Parses the given @string into a transform and puts it in
    /// @out_transform.
    ///
    /// Strings printed via [`to_str()`][Self::to_str()]
    /// can be read in again successfully using this function.
    ///
    /// If @string does not describe a valid transform, [`false`] is
    /// returned and [`None`] is put in @out_transform.
    /// ## `string`
    /// the string to parse
    ///
    /// # Returns
    ///
    /// [`true`] if @string described a valid transform.
    ///
    /// ## `out_transform`
    /// The location to put the transform in
    #[doc(alias = "gsk_transform_parse")]
    pub fn parse(string: impl IntoGStr) -> Result<Self, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            string.run_with_gstr(|string| {
                let mut out_transform = std::ptr::null_mut();
                let ret = from_glib(ffi::gsk_transform_parse(
                    string.as_ptr(),
                    &mut out_transform,
                ));
                if ret {
                    Ok(from_glib_full(out_transform))
                } else {
                    Err(glib::bool_error!("Can't parse Transform"))
                }
            })
        }
    }

    /// 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")]
    pub fn invert(self) -> Result<Self, glib::BoolError> {
        unsafe {
            let matrix = self.to_matrix();
            if matrix == graphene::Matrix::new_identity() {
                return Ok(self);
            }

            let res: Option<Self> = from_glib_full(ffi::gsk_transform_invert(self.into_glib_ptr()));
            res.ok_or_else(|| glib::bool_error!("Failed to invert the transform"))
        }
    }

    /// Rotates @self @angle degrees in 2D - or in 3D-speak, around the Z axis.
    /// The rotation happens around the origin point of (0, 0).
    /// ## `angle`
    /// the rotation angle, in degrees (clockwise)
    ///
    /// # Returns
    ///
    /// The new transform
    #[doc(alias = "gsk_transform_rotate")]
    #[must_use]
    pub fn rotate(self, angle: f32) -> Self {
        unsafe {
            let res: Option<Self> =
                from_glib_full(ffi::gsk_transform_rotate(self.into_glib_ptr(), angle));
            res.unwrap_or_default()
        }
    }

    /// 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) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_rotate_3d(
                self.into_glib_ptr(),
                angle,
                axis.to_glib_none().0,
            ));
            res.unwrap_or_default()
        }
    }

    /// 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) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_scale(
                self.into_glib_ptr(),
                factor_x,
                factor_y,
            ));
            res.unwrap_or_default()
        }
    }

    /// 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) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_scale_3d(
                self.into_glib_ptr(),
                factor_x,
                factor_y,
                factor_z,
            ));
            res.unwrap_or_default()
        }
    }

    /// 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(feature = "v4_6")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
    #[doc(alias = "gsk_transform_skew")]
    #[must_use]
    pub fn skew(self, skew_x: f32, skew_y: f32) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_skew(
                self.into_glib_ptr(),
                skew_x,
                skew_y,
            ));
            res.unwrap_or_default()
        }
    }

    /// 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<&Self>) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_transform(
                self.into_glib_ptr(),
                other.to_glib_none().0,
            ));
            res.unwrap_or_default()
        }
    }

    /// 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) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_translate(
                self.into_glib_ptr(),
                point.to_glib_none().0,
            ));
            res.unwrap_or_default()
        }
    }

    /// 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) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_translate_3d(
                self.into_glib_ptr(),
                point.to_glib_none().0,
            ));
            res.unwrap_or_default()
        }
    }
}

impl std::str::FromStr for Transform {
    type Err = glib::BoolError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        skip_assert_initialized!();
        Self::parse(s)
    }
}

#[test]
fn invert_identity_is_identity() {
    let transform = Transform::new();
    let output = transform.clone().invert();
    assert_eq!(output.unwrap(), transform);
}