use cairo::{Context, Error, Extend, Format, ImageSurface, SurfacePattern}; pub const STITCH: [u8; 64] = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, ]; pub const SHINGLES: [u8; 64] = [ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, ]; pub const SHADOW_GRID: [u8; 64] = [ 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, ]; pub const WICKER: [u8; 64] = [ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, ]; /// Creates a cairo surface pattern from pattern data pub fn create_pattern( pattern_data: &[u8; 64], foreground: (f64, f64, f64), background: (f64, f64, f64), ) -> Result { let surface = ImageSurface::create(Format::Rgb24, 8, 8)?; { let context = Context::new(&surface)?; for y in 0_u32..8 { for x in 0_u32..8 { let value = pattern_data[(y * 8 + x) as usize]; let (r, g, b) = if value == 0 { foreground } else { background }; context.set_source_rgb(r, g, b); context.rectangle(f64::from(x), f64::from(y), 1.0, 1.0); context.fill()?; } } } let pattern = SurfacePattern::create(&surface); pattern.set_extend(Extend::Repeat); Ok(pattern) } #[cfg(test)] mod tests { use super::*; #[test] fn test_pattern_data_lengths() { assert_eq!(STITCH.len(), 64); assert_eq!(SHINGLES.len(), 64); assert_eq!(SHADOW_GRID.len(), 64); assert_eq!(WICKER.len(), 64); } #[test] fn test_pattern_values() { for &pixel in &STITCH { assert!(pixel == 0 || pixel == 1); } for &pixel in &SHINGLES { assert!(pixel == 0 || pixel == 1); } } }