use crate::page_ir::DeviceRect;
use crate::render_cpu::kernels::mul_div_255;
use crate::render_cpu::prepared::{ClipKind, CpuPreparedPage, PreparedClip};
use crate::render_cpu::raster::RasterKernel;
#[derive(Clone, Copy)]
pub struct ClipGeom<'a> {
pub clips: &'a [PreparedClip],
pub subpaths: &'a [(usize, usize)],
pub points: &'a [[f32; 2]],
pub width: usize,
pub height: usize,
}
impl<'a> ClipGeom<'a> {
pub fn of(page: &'a CpuPreparedPage) -> Self {
Self {
clips: &page.clips,
subpaths: &page.subpaths,
points: &page.points,
width: page.size.width as usize,
height: page.size.height as usize,
}
}
}
#[derive(Debug)]
pub struct ClipMask {
pub bounds: DeviceRect,
pub data: Vec<u8>,
pub outside: u8,
pub all_opaque: bool,
}
impl ClipMask {
#[inline]
pub fn stride(&self) -> usize {
self.bounds.width as usize
}
#[inline]
pub fn clip_window(&self) -> MaskWindow<'_> {
MaskWindow::new(self, 0)
}
#[inline]
pub fn soft_window(&self) -> MaskWindow<'_> {
MaskWindow::new(self, self.outside)
}
}
#[derive(Debug, Clone, Copy)]
pub struct MaskWindow<'a> {
data: &'a [u8],
x0: usize,
y0: usize,
width: usize,
height: usize,
outside: u16,
}
impl<'a> MaskWindow<'a> {
#[inline]
fn new(mask: &'a ClipMask, outside: u8) -> Self {
Self {
data: &mask.data,
x0: mask.bounds.x as usize,
y0: mask.bounds.y as usize,
width: mask.bounds.width as usize,
height: mask.bounds.height as usize,
outside: outside as u16,
}
}
#[inline]
pub fn coverage(&self, x: usize, y: usize) -> u16 {
let (Some(lx), Some(ly)) = (x.checked_sub(self.x0), y.checked_sub(self.y0)) else {
return self.outside;
};
if lx < self.width && ly < self.height {
self.data[ly * self.width + lx] as u16
} else {
self.outside
}
}
}
pub fn build_clip_mask(raster: &mut RasterKernel, geom: ClipGeom, cid: u32) -> ClipMask {
if let Some(mask) = build_clip_mask_cancellable(raster, geom, cid, None) {
return mask;
}
let bounds = geom.clips[cid as usize].bounds;
ClipMask {
bounds,
data: vec![0; bounds.width as usize * bounds.height as usize],
outside: 0,
all_opaque: false,
}
}
pub fn build_clip_mask_cancellable(
raster: &mut RasterKernel,
geom: ClipGeom,
cid: u32,
should_cancel: Option<&(dyn Fn() -> bool + Send + Sync)>,
) -> Option<ClipMask> {
let cb = geom.clips[cid as usize].bounds;
let w = cb.width as usize;
let h = cb.height as usize;
let (bx, by) = (cb.x as usize, cb.y as usize);
let ow = geom.width;
let oh = geom.height;
let mut acc = vec![0u8; w * h];
let mut tmp: Vec<u8> = Vec::new();
let mut path_seen = 0u32;
let mut cur = Some(cid);
while let Some(id) = cur {
let clip = &geom.clips[id as usize];
if let ClipKind::Path {
subpath_range,
rule,
} = clip.kind
{
let subs = &geom.subpaths[subpath_range.0 as usize..subpath_range.1 as usize];
path_seen += 1;
if path_seen == 1 {
if !raster.fill_cancellable_in(
&geom.points,
subs,
ow,
oh,
rule,
by..by + h,
should_cancel,
|y, x0, x1, cov| {
if y < by || y >= by + h {
return;
}
let ly = y - by;
let lx0 = x0.max(bx);
let lx1 = x1.min(bx + w - 1);
if lx0 <= lx1 {
acc[ly * w + (lx0 - bx)..=ly * w + (lx1 - bx)]
.copy_from_slice(&cov[lx0..=lx1]);
}
},
) {
return None;
}
} else {
if tmp.is_empty() {
tmp = vec![0u8; w * h];
} else {
tmp.iter_mut().for_each(|t| *t = 0);
}
if !raster.fill_cancellable_in(
&geom.points,
subs,
ow,
oh,
rule,
by..by + h,
should_cancel,
|y, x0, x1, cov| {
if y < by || y >= by + h {
return;
}
let ly = y - by;
let lx0 = x0.max(bx);
let lx1 = x1.min(bx + w - 1);
if lx0 <= lx1 {
tmp[ly * w + (lx0 - bx)..=ly * w + (lx1 - bx)]
.copy_from_slice(&cov[lx0..=lx1]);
}
},
) {
return None;
}
for (index, (a, &t)) in acc.iter_mut().zip(&tmp).enumerate() {
if index & 0xffff == 0 && should_cancel.is_some_and(|cancel| cancel()) {
return None;
}
*a = mul_div_255(*a as u16, t as u16) as u8;
}
}
}
cur = clip.parent;
}
let all_opaque = acc.iter().all(|&v| v == 255);
Some(ClipMask {
bounds: cb,
data: acc,
outside: 0,
all_opaque,
})
}