1
//! Utilities for logging messages from the library.
2

            
3
#[doc(hidden)]
4
#[macro_export]
5
macro_rules! rsvg_log {
6
    (
7
        $session:expr,
8
        $($arg:tt)+
9
    ) => {
10
        if $session.log_enabled() {
11
            println!("{}", format_args!($($arg)+));
12
        }
13
    };
14
}
15

            
16
/// Captures the basic state of a [`cairo::Context`] for logging purposes.
17
///
18
/// A librsvg "transaction" like rendering a
19
/// [`crate::api::SvgHandle`], which takes a Cairo context, depends on the state of the
20
/// context as it was passed in by the caller.  For example, librsvg may decide to
21
/// operate differently depending on the context's target surface type, or its current
22
/// transformation matrix.  This struct captures that sort of information.
23
#[allow(dead_code)] // this is never constructed yet; allow building on newer rustc which warns about this
24
4
#[derive(Copy, Clone, Debug, PartialEq)]
25
struct CairoContextState {
26
2
    surface_type: cairo::SurfaceType,
27
2
    matrix: cairo::Matrix,
28
}
29

            
30
impl CairoContextState {
31
    #[cfg(test)]
32
2
    fn new(cr: &cairo::Context) -> Self {
33
2
        let surface_type = cr.target().type_();
34
2
        let matrix = cr.matrix();
35

            
36
2
        Self {
37
            surface_type,
38
            matrix,
39
        }
40
2
    }
41
}
42

            
43
#[cfg(test)]
44
mod tests {
45
    use super::*;
46

            
47
    #[test]
48
2
    fn captures_cr_state() {
49
1
        let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 10, 10).unwrap();
50
1
        let cr = cairo::Context::new(&surface).unwrap();
51
1
        let state = CairoContextState::new(&cr);
52

            
53
1
        assert_eq!(
54
1
            CairoContextState {
55
1
                surface_type: cairo::SurfaceType::Image,
56
1
                matrix: cairo::Matrix::identity(),
57
            },
58
            state,
59
        );
60

            
61
1
        let surface = cairo::RecordingSurface::create(cairo::Content::ColorAlpha, None).unwrap();
62
1
        let cr = cairo::Context::new(&surface).unwrap();
63
1
        cr.scale(2.0, 3.0);
64
1
        let state = CairoContextState::new(&cr);
65

            
66
1
        let mut matrix = cairo::Matrix::identity();
67
1
        matrix.scale(2.0, 3.0);
68

            
69
1
        assert_eq!(
70
1
            CairoContextState {
71
1
                surface_type: cairo::SurfaceType::Recording,
72
1
                matrix,
73
            },
74
            state,
75
        );
76
2
    }
77
}