use std::sync::Arc;
use crate::{
app::render_engine::{CapturedFrame, RenderCaptureTarget},
layout::{quad::ClipRect, render_list::GlyphCmd},
};
pub(crate) const REDACTION_FILL: [u8; 4] = [128, 128, 128, 255];
const GLYPH_EDGE_REACH: f32 = 2.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct RedactedArea {
pub(crate) x0: u32,
pub(crate) y0: u32,
pub(crate) x1: u32,
pub(crate) y1: u32,
}
impl RedactedArea {
pub(crate) fn of_glyph_run(glyphs: &[GlyphCmd]) -> Option<Self> {
let mut bounds: Option<[f32; 4]> = None;
for glyph in glyphs {
let Some(glyph_bounds) = clip_bounds(glyph_bounds(glyph), glyph.clip) else {
continue;
};
bounds = Some(match bounds {
Some(b) => [
b[0].min(glyph_bounds[0]),
b[1].min(glyph_bounds[1]),
b[2].max(glyph_bounds[2]),
b[3].max(glyph_bounds[3]),
],
None => glyph_bounds,
});
}
let [x0, y0, x1, y1] = bounds?;
let area = Self {
x0: x0.floor().max(0.0) as u32,
y0: y0.floor().max(0.0) as u32,
x1: x1.ceil().max(0.0) as u32,
y1: y1.ceil().max(0.0) as u32,
};
(area.x0 < area.x1 && area.y0 < area.y1).then_some(area)
}
}
fn glyph_bounds(glyph: &GlyphCmd) -> [f32; 4] {
let pad = if glyph.shadow_sigma > 0.0 {
(glyph.shadow_sigma * 2.0).ceil() + 1.0
} else {
0.0
} + GLYPH_EDGE_REACH;
let [a, b, c, d] = glyph.transform;
let [origin_x, origin_y] = glyph.transform_origin;
let corners = [
(-pad, -pad),
(glyph.width + pad, -pad),
(-pad, glyph.height + pad),
(glyph.width + pad, glyph.height + pad),
];
let mut bounds = [f32::INFINITY, f32::INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY];
for (local_x, local_y) in corners {
let (cx, cy) = (local_x - origin_x, local_y - origin_y);
let x = glyph.x + a * cx + c * cy + origin_x;
let y = glyph.y + b * cx + d * cy + origin_y;
bounds = [bounds[0].min(x), bounds[1].min(y), bounds[2].max(x), bounds[3].max(y)];
}
bounds
}
fn clip_bounds(bounds: [f32; 4], clip: ClipRect) -> Option<[f32; 4]> {
let bounds = if clip.active {
[
bounds[0].max(clip.x),
bounds[1].max(clip.y),
bounds[2].min(clip.x + clip.width),
bounds[3].min(clip.y + clip.height),
]
} else {
bounds
};
(bounds[0] < bounds[2] && bounds[1] < bounds[3]).then_some(bounds)
}
pub(crate) fn redact_pixels(pixels: &mut [u8], width: u32, height: u32, origin: (u32, u32), areas: &[RedactedArea]) {
let (origin_x, origin_y) = origin;
for area in areas {
let x0 = area.x0.saturating_sub(origin_x).min(width);
let x1 = area.x1.saturating_sub(origin_x).min(width);
let y0 = area.y0.saturating_sub(origin_y).min(height);
let y1 = area.y1.saturating_sub(origin_y).min(height);
for y in y0..y1 {
let row = (y * width) as usize * 4;
for pixel in pixels[row + x0 as usize * 4..row + x1 as usize * 4].chunks_exact_mut(4) {
pixel.copy_from_slice(&REDACTION_FILL);
}
}
}
}
pub(crate) fn redacting_target(
target: RenderCaptureTarget,
origin: (u32, u32),
areas: Vec<RedactedArea>,
) -> RenderCaptureTarget {
if areas.is_empty() {
return target;
}
RenderCaptureTarget::Bytes(Arc::new(move |outcome: Result<CapturedFrame, String>| {
let outcome = outcome.map(|mut frame| {
redact_pixels(&mut frame.rgba, frame.width, frame.height, origin, &areas);
frame
});
match (&target, outcome) {
(RenderCaptureTarget::Bytes(callback), outcome) => callback(outcome),
(RenderCaptureTarget::Path(path), Ok(frame)) => {
crate::app::frame_capture::save_capture_png(path, &frame.rgba, frame.width, frame.height);
}
(RenderCaptureTarget::Path(_), Err(reason)) => target.fail(reason),
}
}))
}
#[cfg(test)]
mod tests;