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

use std::fmt;

use glib::translate::*;

use crate::{Matrix, Rectangle};

impl Matrix {
    pub fn new(xx: f64, xy: f64, yx: f64, yy: f64, x0: f64, y0: f64) -> Self {
        unsafe {
            Self::unsafe_from(ffi::PangoMatrix {
                xx,
                xy,
                yx,
                yy,
                x0,
                y0,
            })
        }
    }

    /// First transforms the @rect using @self, then calculates the bounding box
    /// of the transformed rectangle.
    ///
    /// This function is useful for example when you want to draw a rotated
    /// @PangoLayout to an image buffer, and want to know how large the image
    /// should be and how much you should shift the layout when rendering.
    ///
    /// For better accuracy, you should use [`transform_rectangle()`][Self::transform_rectangle()]
    /// on original rectangle in Pango units and convert to pixels afterward
    /// using [`extents_to_pixels()`][crate::extents_to_pixels()]'s first argument.
    /// ## `rect`
    /// in/out bounding box in device units
    #[doc(alias = "pango_matrix_transform_pixel_rectangle")]
    pub fn transform_pixel_rectangle(&self, rect: &mut Rectangle) {
        unsafe {
            ffi::pango_matrix_transform_pixel_rectangle(
                self.to_glib_none().0,
                rect.to_glib_none_mut().0,
            )
        }
    }

    /// First transforms @rect using @self, then calculates the bounding box
    /// of the transformed rectangle.
    ///
    /// This function is useful for example when you want to draw a rotated
    /// @PangoLayout to an image buffer, and want to know how large the image
    /// should be and how much you should shift the layout when rendering.
    ///
    /// If you have a rectangle in device units (pixels), use
    /// [`transform_pixel_rectangle()`][Self::transform_pixel_rectangle()].
    ///
    /// If you have the rectangle in Pango units and want to convert to
    /// transformed pixel bounding box, it is more accurate to transform it first
    /// (using this function) and pass the result to pango_extents_to_pixels(),
    /// first argument, for an inclusive rounded rectangle.
    /// However, there are valid reasons that you may want to convert
    /// to pixels first and then transform, for example when the transformed
    /// coordinates may overflow in Pango units (large matrix translation for
    /// example).
    /// ## `rect`
    /// in/out bounding box in Pango units
    #[doc(alias = "pango_matrix_transform_rectangle")]
    pub fn transform_rectangle(&self, rect: &mut Rectangle) {
        unsafe {
            ffi::pango_matrix_transform_rectangle(self.to_glib_none().0, rect.to_glib_none_mut().0)
        }
    }

    pub fn xx(&self) -> f64 {
        self.inner.xx
    }

    pub fn xy(&self) -> f64 {
        self.inner.xy
    }

    pub fn yx(&self) -> f64 {
        self.inner.yx
    }

    pub fn yy(&self) -> f64 {
        self.inner.yy
    }

    pub fn x0(&self) -> f64 {
        self.inner.x0
    }

    pub fn y0(&self) -> f64 {
        self.inner.y0
    }
}

impl fmt::Debug for Matrix {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Matrix")
            .field("xx", &self.xx())
            .field("xy", &self.xy())
            .field("yx", &self.yx())
            .field("yy", &self.yy())
            .field("x0", &self.x0())
            .field("y0", &self.y0())
            .finish()
    }
}