[librsvg: 1/45] New CairoContextState struct to capture the state of a passed-in Cairo context




commit d839c73e14272c6ec246336e37d05ccb6ef17c78
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed May 11 13:40:23 2022 -0500

    New CairoContextState struct to capture the state of a passed-in Cairo context
    
    The idea is to log all "transactions" in the public API, including the
    state of the Cairo context that was passed to those calls.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/731>

 src/log.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
---
diff --git a/src/log.rs b/src/log.rs
index 346fe25e7..505ae0862 100644
--- a/src/log.rs
+++ b/src/log.rs
@@ -18,3 +18,53 @@ pub fn log_enabled() -> bool {
 
     *ENABLED
 }
+
+/// Captures the basic state of a [`cairo::Context`] for logging purposes.
+///
+/// A librsvg "transaction" like rendering a
+/// [`crate::api::SvgHandle`], which takes a Cairo context, depends on the state of the
+/// context as it was passed in by the caller.  For example, librsvg may decide to
+/// operate differently depending on the context's target surface type, or its current
+/// transformation matrix.  This struct captures that sort of information.
+#[derive(Copy, Clone, Debug, PartialEq)]
+struct CairoContextState {
+    surface_type: cairo::SurfaceType,
+}
+
+impl CairoContextState {
+    fn new(cr: &cairo::Context) -> Self {
+        let surface_type = cr.target().type_();
+
+        Self { surface_type }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn captures_cr_surface_type() {
+        let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 10, 10).unwrap();
+        let cr = cairo::Context::new(&surface).unwrap();
+        let state = CairoContextState::new(&cr);
+
+        assert_eq!(
+            CairoContextState {
+                surface_type: cairo::SurfaceType::Image,
+            },
+            state,
+        );
+
+        let surface = cairo::RecordingSurface::create(cairo::Content::ColorAlpha, None).unwrap();
+        let cr = cairo::Context::new(&surface).unwrap();
+        let state = CairoContextState::new(&cr);
+
+        assert_eq!(
+            CairoContextState {
+                surface_type: cairo::SurfaceType::Recording,
+            },
+            state,
+        );
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]