use crate::render::painter::ClipInfo;
use crate::style::{CornerRadii, Rect};
pub(crate) fn clip_world_rect(clip: &ClipInfo) -> Rect {
Rect::new(
clip.rect.x + clip.scroll_offset[0],
clip.rect.y + clip.scroll_offset[1],
clip.rect.width,
clip.rect.height,
)
}
pub(crate) enum ClipMask {
None,
Skip,
Mask(tiny_skia::Mask),
}
pub(crate) fn build_clip_mask_rect(
abs: Rect,
radius: CornerRadii,
xform: glam::Affine2,
intersect_raw: Option<Rect>,
phys_w: u32,
phys_h: u32,
sf: f32,
) -> ClipMask {
if abs.width <= 0.0 || abs.height <= 0.0 {
return ClipMask::Skip;
}
let mut mask = match tiny_skia::Mask::new(phys_w.max(1), phys_h.max(1)) {
Some(m) => m,
None => return ClipMask::None,
};
let logical_rect =
tiny_skia::Rect::from_xywh(abs.x, abs.y, abs.width.max(0.01), abs.height.max(0.01))
.unwrap_or_else(|| tiny_skia::Rect::from_xywh(0.0, 0.0, 1.0, 1.0).unwrap());
let path = super::rounded_skia_rect(logical_rect, radius, 1.0);
let dev_xform = super::glam_to_skia_transform_logical(xform, sf);
mask.fill_path(&path, tiny_skia::FillRule::Winding, true, dev_xform);
if let Some(ir) = intersect_raw {
let Some(sr) =
tiny_skia::Rect::from_xywh(ir.x, ir.y, ir.width.max(0.01), ir.height.max(0.01))
else {
return ClipMask::Skip;
};
let mut pb = tiny_skia::PathBuilder::new();
pb.push_rect(sr);
if let Some(p) = pb.finish() {
mask.intersect_path(&p, tiny_skia::FillRule::Winding, false, dev_xform);
}
}
ClipMask::Mask(mask)
}
pub(crate) fn build_clip_mask(
clip: &ClipInfo,
xform: glam::Affine2,
phys_w: u32,
phys_h: u32,
logical_w: f32,
logical_h: f32,
sf: f32,
) -> ClipMask {
let abs = clip_world_rect(clip);
if abs.width <= 0.0 || abs.height <= 0.0 {
return ClipMask::Skip;
}
let is_identity = xform == glam::Affine2::IDENTITY;
if clip.radius.is_zero()
&& is_identity
&& abs.x <= 0.5
&& abs.y <= 0.5
&& abs.x + abs.width >= logical_w - 0.5
&& abs.y + abs.height >= logical_h - 0.5
{
return ClipMask::None;
}
build_clip_mask_rect(abs, clip.radius, xform, None, phys_w, phys_h, sf)
}