rsvg/
log.rs

1//! Utilities for logging messages from the library.
2
3#[doc(hidden)]
4#[macro_export]
5macro_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#[derive(Copy, Clone, Debug, PartialEq)]
25struct CairoContextState {
26    surface_type: cairo::SurfaceType,
27    matrix: cairo::Matrix,
28}
29
30impl CairoContextState {
31    #[cfg(test)]
32    fn new(cr: &cairo::Context) -> Self {
33        let surface_type = cr.target().type_();
34        let matrix = cr.matrix();
35
36        Self {
37            surface_type,
38            matrix,
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn captures_cr_state() {
49        let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 10, 10).unwrap();
50        let cr = cairo::Context::new(&surface).unwrap();
51        let state = CairoContextState::new(&cr);
52
53        assert_eq!(
54            CairoContextState {
55                surface_type: cairo::SurfaceType::Image,
56                matrix: cairo::Matrix::identity(),
57            },
58            state,
59        );
60
61        let surface = cairo::RecordingSurface::create(cairo::Content::ColorAlpha, None).unwrap();
62        let cr = cairo::Context::new(&surface).unwrap();
63        cr.scale(2.0, 3.0);
64        let state = CairoContextState::new(&cr);
65
66        let mut matrix = cairo::Matrix::identity();
67        matrix.scale(2.0, 3.0);
68
69        assert_eq!(
70            CairoContextState {
71                surface_type: cairo::SurfaceType::Recording,
72                matrix,
73            },
74            state,
75        );
76    }
77}