use crate::paint::rounded::{Radii, inset, rounded_rect};
use crate::paint::{BoxPaint, Color, Contour, CornerShape, Rect};
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;
const SIGMA_PER_BLUR: f32 = 0.5;
const BLUR_EXTENT_SIGMAS: f32 = 3.0;
const MASK_SIGMA_PIXELS: f32 = 6.0;
const MAX_MASK_PIXELS_PER_PT: f32 = 4.0;
const MAX_MASK_PIXELS: f32 = 4.0e6;
const COVERAGE_SUBROWS: usize = 4;
const ALPHA_MAX: f32 = 255.0;
const MAX_CACHED_MASK_BYTES: usize = 4 << 20;
const ROUNDED_KEY: u32 = 0;
const CONTOURS_KEY: u32 = 1;
const LINE_KEY: u32 = 0;
const CUBIC_KEY: u32 = 1;
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub struct BoxShadow {
pub offset: [f32; 2],
pub blur: f32,
pub spread: f32,
pub color: Color,
pub inset: bool,
}
impl BoxShadow {
pub(crate) fn sigma(&self) -> f32 {
self.blur * SIGMA_PER_BLUR
}
pub(crate) fn extent(&self) -> f32 {
self.sigma() * BLUR_EXTENT_SIGMAS
}
}
#[derive(Debug, PartialEq)]
pub struct ShadowMask {
pub rect: Rect,
pub size: [u32; 2],
pub alpha: Vec<u8>,
}
#[derive(Default)]
pub(crate) struct ShadowMasks {
masks: HashMap<Vec<u32>, Arc<ShadowMask>>,
bytes: usize,
}
impl ShadowMasks {
fn blurred(
&mut self,
region: Rect,
shape: Option<ShadowArea>,
sigma: f32,
invert: bool,
) -> Arc<ShadowMask> {
let mut key: Vec<u32> = [region.x, region.y, region.width, region.height, sigma]
.map(f32::to_bits)
.into();
key.push(u32::from(invert));
if let Some(shape) = &shape {
shape.push_key(&mut key);
}
if let Some(mask) = self.masks.get(&key) {
return mask.clone();
}
let mask = Arc::new(blurred_mask(region, shape, sigma, invert));
if self.bytes + mask.alpha.len() > MAX_CACHED_MASK_BYTES {
self.masks.clear();
self.bytes = 0;
}
self.bytes += mask.alpha.len();
self.masks.insert(key, mask.clone());
mask
}
}
#[derive(Clone, Debug)]
pub enum ShadowShape {
Sharp(Vec<Contour>),
Blurred(Arc<ShadowMask>),
}
#[derive(Clone, Debug)]
pub struct ShadowPaint {
pub bounds: Rect,
pub clip: Vec<Contour>,
pub shape: ShadowShape,
pub color: Color,
}
pub(crate) fn spread_radii(radii: Radii, growth: f32) -> Radii {
radii.map(|corner| corner.map(|radius| crate::paint::rounded::spread_radius(radius, growth)))
}
pub(crate) fn outset_radii(radii: Radii, [top, right, bottom, left]: [f32; 4]) -> Radii {
let growth = [[left, top], [right, top], [right, bottom], [left, bottom]];
std::array::from_fn(|corner| {
std::array::from_fn(|axis| {
crate::paint::rounded::spread_radius(radii[corner][axis], growth[corner][axis])
})
})
}
pub(crate) fn outset(rect: Rect, distance: [f32; 4]) -> Rect {
let [top, right, bottom, left] = distance;
Rect {
x: rect.x - left,
y: rect.y - top,
width: (rect.width + left + right).max(0.0),
height: (rect.height + top + bottom).max(0.0),
}
}
fn is_empty(rect: Rect) -> bool {
rect.width <= 0.0 || rect.height <= 0.0
}
#[derive(Clone)]
enum ShadowArea {
Rounded(Rect, Radii),
Contours(Vec<Contour>),
}
impl ShadowArea {
fn is_empty(&self) -> bool {
match self {
Self::Rounded(rect, _) => is_empty(*rect),
Self::Contours(contours) => contours.is_empty(),
}
}
fn translated(self, offset: [f32; 2]) -> Self {
match self {
Self::Rounded(rect, radii) => Self::Rounded(rect.translated(offset), radii),
Self::Contours(contours) => Self::Contours(
contours
.into_iter()
.map(|contour| contour.map(|[x, y]| [x + offset[0], y + offset[1]]))
.collect(),
),
}
}
fn push_key(&self, key: &mut Vec<u32>) {
let length = |length: usize| u32::try_from(length).unwrap_or(u32::MAX);
let points = |points: &[crate::paint::Point], key: &mut Vec<u32>| {
key.extend(points.iter().flatten().map(|value| value.to_bits()));
};
match self {
Self::Rounded(rect, radii) => {
key.push(ROUNDED_KEY);
key.extend(
[rect.x, rect.y, rect.width, rect.height]
.iter()
.chain(radii.as_flattened())
.map(|value| value.to_bits()),
);
}
Self::Contours(contours) => {
key.extend([CONTOURS_KEY, length(contours.len())]);
for contour in contours {
key.extend([u32::from(contour.closed), length(contour.segments.len())]);
points(&[contour.start], key);
for segment in &contour.segments {
match segment {
crate::paint::Segment::Line(end) => {
key.push(LINE_KEY);
points(&[*end], key);
}
crate::paint::Segment::Cubic(controls) => {
key.push(CUBIC_KEY);
points(controls, key);
}
}
}
}
}
}
}
fn contours(self) -> Vec<Contour> {
match self {
Self::Rounded(rect, radii) => vec![rounded_rect(rect, radii)],
Self::Contours(contours) => contours,
}
}
fn coverage(self, region: Rect, size: [usize; 2], pitch: [f32; 2]) -> Vec<f32> {
let contours = match self {
Self::Rounded(rect, radii) => return coverage(region, size, pitch, (rect, radii)),
Self::Contours(contours) => contours,
};
let mut target = tiny_skia::Pixmap::new(size[0] as u32, size[1] as u32)
.expect("shadow grid dimensions fit the pixel budget");
let bounds = [
[f64::from(region.x), f64::from(region.x + region.width)],
[f64::from(region.y), f64::from(region.y + region.height)],
];
let tolerance = f64::from(pitch[0].min(pitch[1])) / COVERAGE_SUBROWS as f64;
let contours = contours
.into_iter()
.filter_map(|contour| {
let polygon =
crate::paint::clip_polygon(&contour.polygon_points(tolerance), bounds);
(polygon.len() >= 3).then(|| {
Contour::polygon(
&polygon
.into_iter()
.map(|point| {
std::array::from_fn(|axis| {
((point[axis] - bounds[axis][0]) / f64::from(pitch[axis]))
as f32
})
})
.collect::<Vec<_>>(),
)
})
})
.collect();
let local = Rect {
x: 0.0,
y: 0.0,
width: size[0] as f32,
height: size[1] as f32,
};
crate::paint::raster::cover(
&crate::paint::Shape::Path {
contours,
even_odd: false,
clip: None,
},
local,
tiny_skia::Transform::identity(),
&mut target,
None,
);
target
.pixels()
.iter()
.map(|pixel| f32::from(pixel.alpha()) / ALPHA_MAX)
.collect()
}
}
impl<I> BoxPaint<I> {
pub fn shadows(&self, inset_shadows: bool) -> Vec<ShadowPaint> {
self.cached_shadows(inset_shadows, &mut ShadowMasks::default())
}
pub(crate) fn cached_shadows(
&self,
inset_shadows: bool,
masks: &mut ShadowMasks,
) -> Vec<ShadowPaint> {
let rect = self.decoration_rect();
if is_empty(rect) {
return Vec::new();
}
let radii = self.radii();
self.cast_shadows(inset_shadows)
.filter_map(|shadow| {
if shadow.inset {
self.inset_shadow(shadow, rect, radii, masks)
} else {
self.outer_shadow(shadow, rect, radii, masks)
}
})
.collect()
}
pub(crate) fn outer_shadow_bounds(&self) -> Vec<Rect> {
let rect = self.decoration_rect();
if is_empty(rect) {
return Vec::new();
}
let radii = self.radii();
self.cast_shadows(false)
.filter_map(|shadow| self.outer_shadow_area(shadow, rect, radii))
.map(|(_, _, bounds)| self.slice_bounds(bounds))
.collect()
}
fn cast_shadows(&self, inset_shadows: bool) -> impl Iterator<Item = &BoxShadow> {
self.decoration
.shadows
.iter()
.rev()
.filter(move |shadow| shadow.inset == inset_shadows && shadow.color.alpha > 0.0)
}
fn inset_shadow(
&self,
shadow: &BoxShadow,
unbroken: Rect,
radii: Radii,
masks: &mut ShadowMasks,
) -> Option<ShadowPaint> {
let (unbroken_padding, padding_radii) = inset(unbroken, radii, self.inner_widths());
let bounds = self.slice_bounds(unbroken_padding);
if is_empty(bounds) {
return None;
}
let clip = self.inner_clip([0.0; 4], [true; 2]).contours().into_owned();
if clip.is_empty() {
return None;
}
let hole = if *self.decoration.corner_shapes == [CornerShape::ROUND; 4] {
ShadowArea::Rounded(
outset(unbroken_padding, [-shadow.spread; 4]),
spread_radii(padding_radii, -shadow.spread),
)
} else {
ShadowArea::Contours(crate::paint::rounded::box_contours(
unbroken,
radii,
self.decoration.corner_shapes.map(CornerShape::curvature),
self.inner_widths().map(|width| width + shadow.spread),
))
}
.translated(shadow.offset);
let hole = (!hole.is_empty()).then_some(hole);
let shape = if shadow.blur > 0.0 {
let region = outset(unbroken_padding, [shadow.extent(); 4]);
ShadowShape::Blurred(masks.blurred(region, hole, shadow.sigma(), true))
} else {
let mut contours = vec![Contour::polygon(&corners(unbroken_padding))];
contours.extend(hole.into_iter().flat_map(ShadowArea::contours));
ShadowShape::Sharp(contours)
};
Some(ShadowPaint {
bounds,
clip,
shape,
color: shadow.color,
})
}
fn outer_shadow(
&self,
shadow: &BoxShadow,
rect: Rect,
radii: Radii,
masks: &mut ShadowMasks,
) -> Option<ShadowPaint> {
let (ink, area, bounds) = self.outer_shadow_area(shadow, rect, radii)?;
let shape = if shadow.blur > 0.0 {
ShadowShape::Blurred(masks.blurred(ink, Some(area), shadow.sigma(), false))
} else {
ShadowShape::Sharp(area.contours())
};
let mut clip = vec![Contour::polygon(&corners(bounds))];
clip.extend(
self.box_area(crate::paint::BoxArea::BorderBox)
.contours()
.iter()
.cloned(),
);
Some(ShadowPaint {
bounds: self.slice_bounds(bounds),
clip,
shape,
color: shadow.color,
})
}
fn outer_shadow_area(
&self,
shadow: &BoxShadow,
rect: Rect,
radii: Radii,
) -> Option<(Rect, ShadowArea, Rect)> {
let shape = outset(rect, [shadow.spread; 4]).translated(shadow.offset);
if is_empty(shape) {
return None;
}
let area = if *self.decoration.corner_shapes == [CornerShape::ROUND; 4] {
ShadowArea::Rounded(
outset(rect, [shadow.spread; 4]),
spread_radii(radii, shadow.spread),
)
} else {
ShadowArea::Contours(crate::paint::rounded::box_contours(
rect,
radii,
self.decoration.corner_shapes.map(CornerShape::curvature),
[-shadow.spread; 4],
))
}
.translated(shadow.offset);
if area.is_empty() {
return None;
}
let ink = outset(shape, [shadow.extent(); 4]);
let visible = self.slice_bounds(ink);
if is_empty(visible) {
return None;
}
Some((ink, area, ink.union(rect)))
}
}
fn corners(rect: Rect) -> [crate::paint::Point; 4] {
[
[rect.x, rect.y],
[rect.x + rect.width, rect.y],
[rect.x + rect.width, rect.y + rect.height],
[rect.x, rect.y + rect.height],
]
}
fn row_span(rect: Rect, radii: Radii, y: f32) -> Option<[f32; 2]> {
let below_top = y - rect.y;
let above_bottom = rect.y + rect.height - y;
if below_top < 0.0 || above_bottom < 0.0 {
return None;
}
let corner = |[rx, ry]: [f32; 2], depth: f32| {
if rx <= 0.0 || ry <= 0.0 || depth >= ry {
return 0.0;
}
let along = (ry - depth) / ry;
rx * (1.0 - (1.0 - along * along).max(0.0).sqrt())
};
let left = corner(radii[0], below_top).max(corner(radii[3], above_bottom));
let right = corner(radii[1], below_top).max(corner(radii[2], above_bottom));
let span = [rect.x + left, rect.x + rect.width - right];
(span[0] < span[1]).then_some(span)
}
fn coverage(region: Rect, size: [usize; 2], pitch: [f32; 2], shape: (Rect, Radii)) -> Vec<f32> {
let [columns, rows] = size;
let mut values = vec![0.0; columns * rows];
let weight = 1.0 / COVERAGE_SUBROWS as f32;
let mut previous = None;
for row in 0..rows {
let spans: [_; COVERAGE_SUBROWS] = std::array::from_fn(|sample| {
let y = region.y + (row as f32 + (sample as f32 + 0.5) * weight) * pitch[1];
row_span(shape.0, shape.1, y)
});
let (done, rest) = values.split_at_mut(row * columns);
let line = &mut rest[..columns];
let bits = spans.map(|span| span.map(|ends| ends.map(f32::to_bits)));
if previous.replace(bits) == Some(bits) {
line.copy_from_slice(&done[(row - 1) * columns..]);
continue;
}
for [x0, x1] in spans.into_iter().flatten() {
let from = ((x0 - region.x) / pitch[0]).max(0.0);
let to = ((x1 - region.x) / pitch[0]).min(columns as f32);
if from >= to {
continue;
}
for (column, value) in line
.iter_mut()
.enumerate()
.take(to.ceil() as usize)
.skip(from.floor() as usize)
{
let column = column as f32;
*value += (to.min(column + 1.0) - from.max(column)) * weight;
}
}
}
values
}
fn gaussian_kernel(sigma: f32) -> Vec<f32> {
let radius = (sigma * BLUR_EXTENT_SIGMAS).ceil() as i32;
let weights: Vec<f32> = (-radius..=radius)
.map(|offset| (-(offset * offset) as f32 / (2.0 * sigma * sigma)).exp())
.collect();
let total: f32 = weights.iter().sum();
weights.into_iter().map(|weight| weight / total).collect()
}
fn convolve(values: &[f32], size: [usize; 2], kernel: &[f32], vertical: bool) -> Vec<f32> {
let [columns, rows] = size;
let radius = kernel.len() / 2;
let reach = if vertical { radius } else { 0 };
let line = |row: usize| &values[row * columns..][..columns];
let mut repeats = vec![0; rows];
for row in 1..rows {
if line(row) == line(row - 1) {
repeats[row] = repeats[row - 1] + 1;
}
}
let mut output = vec![0.0; values.len()];
let accumulate = |output: &mut [f32], input: &[f32], weight: f32| {
for (sum, value) in output.iter_mut().zip(input) {
*sum += weight * value;
}
};
for row in 0..rows {
let (done, rest) = output.split_at_mut(row * columns);
let sums = &mut rest[..columns];
if row > reach && row + reach < rows && repeats[row + reach] > 2 * reach {
sums.copy_from_slice(&done[(row - 1) * columns..]);
continue;
}
for (tap, &weight) in kernel.iter().enumerate() {
if vertical {
if let Some(source) = (row + tap).checked_sub(radius).filter(|&at| at < rows) {
accumulate(sums, line(source), weight);
}
} else {
let (start, source) = (radius.saturating_sub(tap), tap.saturating_sub(radius));
if let (Some(target), Some(input)) =
(sums.get_mut(start..), line(row).get(source..))
{
accumulate(target, input, weight);
}
}
}
}
output
}
fn blurred_mask(region: Rect, shape: Option<ShadowArea>, sigma: f32, invert: bool) -> ShadowMask {
blurred(region, sigma, invert, |size, pitch| {
shape.map_or_else(
|| vec![0.0; size[0] * size[1]],
|shape| shape.coverage(region, size, pitch),
)
})
}
fn mask_grid(region: Rect, sigma: f32) -> ([usize; 2], [f32; 2]) {
let mut density = (MASK_SIGMA_PIXELS / sigma).min(MAX_MASK_PIXELS_PER_PT);
let area = f64::from(region.width) * f64::from(region.height);
if area * f64::from(density).powi(2) > f64::from(MAX_MASK_PIXELS) {
density = (f64::from(MAX_MASK_PIXELS) / area).sqrt() as f32;
}
let limit = MAX_MASK_PIXELS as usize;
let mut size = [region.width, region.height]
.map(|extent| ((extent * density).ceil() as usize).clamp(1, limit));
if size[0] > limit / size[1] {
let larger = usize::from(size[1] > size[0]);
size[larger] = limit / size[1 - larger];
}
let pitch = [
region.width / size[0] as f32,
region.height / size[1] as f32,
];
(size, pitch)
}
pub(crate) fn blurred_shadow(
region: Rect,
sigma: f32,
coverage: impl FnOnce([usize; 2], [f32; 2]) -> Vec<f32>,
) -> ShadowMask {
blurred(region, sigma, false, coverage)
}
fn blurred(
region: Rect,
sigma: f32,
invert: bool,
coverage: impl FnOnce([usize; 2], [f32; 2]) -> Vec<f32>,
) -> ShadowMask {
crate::work::count(crate::work::Work::ShadowMask, 1);
let (size, pitch) = mask_grid(region, sigma);
let mut values = coverage(size, pitch);
for (axis, vertical) in [(0, false), (1, true)] {
values = convolve(
&values,
size,
&gaussian_kernel(sigma / pitch[axis]),
vertical,
);
}
ShadowMask {
rect: region,
size: size.map(|extent| extent as u32),
alpha: values
.into_iter()
.map(|value| {
let value = value.clamp(0.0, 1.0);
((if invert { 1.0 - value } else { value }) * ALPHA_MAX).round() as u8
})
.collect(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn phi(x: f32) -> f32 {
let z = x.abs() / std::f32::consts::SQRT_2;
let t = 1.0 / (1.0 + 0.327_591_1 * z);
let poly = t
* (0.254_829_6
+ t * (-0.284_496_7 + t * (1.421_413_8 + t * (-1.453_152 + t * 1.061_405_4))));
let erf = 1.0 - poly * (-(z * z)).exp();
f32::midpoint(1.0, erf.copysign(x))
}
fn pixel_at(mask: &ShadowMask, point: [f32; 2]) -> (f32, [f32; 2]) {
let pitch = [
mask.rect.width / mask.size[0] as f32,
mask.rect.height / mask.size[1] as f32,
];
let column = ((point[0] - mask.rect.x) / pitch[0]) as usize;
let row = ((point[1] - mask.rect.y) / pitch[1]) as usize;
(
f32::from(mask.alpha[row * mask.size[0] as usize + column]) / ALPHA_MAX,
[
mask.rect.x + (column as f32 + 0.5) * pitch[0],
mask.rect.y + (row as f32 + 0.5) * pitch[1],
],
)
}
fn notched_shadow(inset: bool, blur: f32, spread: f32) -> BoxPaint {
use crate::paint::{Border, BorderStyle, BoxDecoration, CornerShape, LengthPercentage};
BoxPaint::whole(
Rect {
x: 0.0,
y: 0.0,
width: 100.0,
height: 80.0,
},
BoxDecoration {
radii: Arc::new(std::array::from_fn(|corner| {
std::array::from_fn(|_| {
LengthPercentage::Length(if corner == 0 { 30.0 } else { 0.0 })
})
})),
corner_shapes: Arc::new([
CornerShape::NOTCH,
CornerShape::ROUND,
CornerShape::ROUND,
CornerShape::ROUND,
]),
borders: [4.0, 8.0, 12.0, 16.0].map(|width| Border {
width,
style: BorderStyle::Solid,
color: Color::BLACK,
}),
shadows: Arc::new(vec![BoxShadow {
offset: [0.0; 2],
blur,
spread,
color: Color::BLACK,
inset,
}]),
..Default::default()
},
)
}
fn inside(contours: &[Contour], point: [f64; 2]) -> bool {
use kurbo::Shape as _;
contours
.iter()
.map(|c| {
kurbo::BezPath::from_iter(c.path_elements())
.winding(kurbo::Point::new(point[0], point[1]))
})
.sum::<i32>()
.abs()
% 2
== 1
}
#[test]
fn sharp_notch_shadows_keep_the_cutout_and_total_inset_distances() {
let paint = notched_shadow(false, 0.0, 0.0);
let outer = paint.shadows(false).pop().unwrap();
let ShadowShape::Sharp(contours) = &outer.shape else {
panic!("sharp shadow")
};
assert!(
!inside(contours, [15.0, 15.0]),
"outer shadow retains the notch cutout"
);
assert!(inside(contours, [35.0, 15.0]));
assert!(
inside(&outer.clip, [15.0, 15.0]),
"shadow is allowed in the box's missing corner"
);
assert!(
!inside(&outer.clip, [35.0, 15.0]),
"shadow must not cover the box itself"
);
let mut paint = notched_shadow(true, 0.0, 3.0);
Arc::make_mut(&mut paint.decoration.shadows)[0].offset = [5.0, -2.0];
let inner = paint.shadows(true).pop().unwrap();
assert!(
!inside(&inner.clip, [32.0, 20.0]),
"padding notch ends at x34: the top border offsets its vertical segment"
);
assert!(inside(&inner.clip, [36.0, 20.0]));
let ShadowShape::Sharp(contours) = &inner.shape else {
panic!("sharp shadow")
};
assert!(
inside(contours, [40.0, 20.0]),
"hole notch ends at 30+4+3+5=42"
);
assert!(
!inside(contours, [44.0, 20.0]),
"the translated inset hole is transparent"
);
}
#[test]
fn blurred_notch_alpha_matches_the_difference_of_two_rectangle_gaussians() {
let paint = notched_shadow(false, 4.0, 0.0);
let shadow = paint.shadows(false).pop().unwrap();
let ShadowShape::Blurred(mask) = &shadow.shape else {
panic!("blurred shadow")
};
let sigma = 2.0;
for point in [
[15.0, 15.0],
[28.0, 28.0],
[30.0, 30.0],
[32.0, 30.0],
[35.0, 15.0],
[0.0, 40.0],
] {
let (actual, [x, y]) = pixel_at(mask, point);
let outer = (phi(x / sigma) - phi((x - 100.0) / sigma))
* (phi(y / sigma) - phi((y - 80.0) / sigma));
let cutout = (phi(x / sigma) - phi((x - 30.0) / sigma))
* (phi(y / sigma) - phi((y - 30.0) / sigma));
assert!(
(actual - (outer - cutout)).abs() < 0.015,
"notch blur at {point:?}: {actual}, expected {}",
outer - cutout
);
}
}
#[test]
fn outer_notch_spread_and_offset_move_the_shadow_before_box_knockout() {
for spread in [-4.0, 4.0] {
let mut paint = notched_shadow(false, 0.0, spread);
Arc::make_mut(&mut paint.decoration.shadows)[0].offset = [7.0, -3.0];
let shadow = paint.shadows(false).pop().unwrap();
let ShadowShape::Sharp(contours) = &shadow.shape else {
panic!("sharp shadow")
};
let vertex = [30.0 - spread + 7.0, 30.0 - spread - 3.0].map(f64::from);
assert!(
!inside(contours, [vertex[0] - 1.0, 15.0]),
"spread {spread}: translated vertical notch edge excludes its cutout"
);
assert!(inside(contours, [vertex[0] + 1.0, 15.0]));
assert!(!inside(contours, [20.0, vertex[1] - 1.0]));
assert!(inside(contours, [20.0, vertex[1] + 1.0]));
assert!(
inside(&shadow.clip, [15.0, 15.0]),
"knockout remains at the original notch, independent of spread and offset"
);
assert!(!inside(&shadow.clip, [50.0, 50.0]));
}
}
#[test]
fn blurred_inset_notch_uses_the_complement_of_the_translated_hole() {
let mut paint = notched_shadow(true, 4.0, 3.0);
Arc::make_mut(&mut paint.decoration.shadows)[0].offset = [5.0, -2.0];
let shadow = paint.shadows(true).pop().unwrap();
let ShadowShape::Blurred(mask) = &shadow.shape else {
panic!("blurred shadow")
};
let sigma = 2.0;
for point in [
[40.0, 20.0],
[42.0, 20.0],
[44.0, 20.0],
[42.0, 47.0],
[50.0, 47.0],
] {
let (actual, [x, y]) = pixel_at(mask, point);
let outer = (phi((x - 24.0) / sigma) - phi((x - 94.0) / sigma))
* (phi((y - 5.0) / sigma) - phi((y - 63.0) / sigma));
let cutout = (phi((x - 24.0) / sigma) - phi((x - 42.0) / sigma))
* (phi((y - 5.0) / sigma) - phi((y - 47.0) / sigma));
assert!(
(actual - (1.0 - outer + cutout)).abs() < 0.015,
"inset notch at {point:?}: {actual}, expected {}",
1.0 - outer + cutout
);
}
}
#[test]
fn disconnected_shadow_islands_blur_independently_and_slices_keep_the_unbroken_mask() {
use crate::paint::LengthPercentage;
let mut paint = notched_shadow(false, 4.0, -4.0);
paint.decoration.corner_shapes = Arc::new([
CornerShape::NOTCH,
CornerShape::ROUND,
CornerShape::NOTCH,
CornerShape::ROUND,
]);
paint.decoration.radii = Arc::new(std::array::from_fn(|corner| {
std::array::from_fn(|_| {
if corner % 2 == 0 {
LengthPercentage::Percentage(0.5)
} else {
LengthPercentage::ZERO
}
})
}));
let whole = paint.shadows(false).pop().unwrap();
let ShadowShape::Blurred(mask) = &whole.shape else {
panic!("blurred shadow")
};
let sigma = 2.0;
for point in [
[50.0, 40.0],
[54.0, 30.0],
[40.0, 44.0],
[60.0, 20.0],
[30.0, 50.0],
] {
let (actual, [x, y]) = pixel_at(mask, point);
let top_right = (phi((x - 54.0) / sigma) - phi((x - 96.0) / sigma))
* (phi((y - 4.0) / sigma) - phi((y - 36.0) / sigma));
let bottom_left = (phi((x - 4.0) / sigma) - phi((x - 46.0) / sigma))
* (phi((y - 44.0) / sigma) - phi((y - 76.0) / sigma));
assert!(
(actual - top_right - bottom_left).abs() < 0.015,
"disconnected shadow at {point:?}: {actual}, expected {}",
top_right + bottom_left
);
}
paint.background_box = Some(paint.rect);
paint.rect.y = 10.0;
paint.rect.height = 60.0;
paint.first_fragment = false;
paint.last_fragment = false;
let sliced = paint.shadows(false).pop().unwrap();
let ShadowShape::Blurred(sliced_mask) = &sliced.shape else {
panic!("blurred shadow")
};
assert_eq!(
sliced_mask, mask,
"slicing preserves the complete unbroken Gaussian mask"
);
assert_eq!(
[sliced.bounds.y, sliced.bounds.height],
[10.0, 60.0],
"only the visible fragment bounds crop the shadow"
);
}
#[test]
fn empty_concave_padding_produces_no_inset_shadow() {
let mut paint = notched_shadow(true, 4.0, 0.0);
paint.decoration.corner_shapes = Arc::new([CornerShape::NOTCH; 4]);
paint.decoration.radii = Arc::new(std::array::from_fn(|_| {
std::array::from_fn(|_| crate::paint::LengthPercentage::Percentage(0.5))
}));
assert!(
paint.shadows(true).is_empty(),
"four half-box notches consume the whole padding region"
);
}
#[test]
fn spread_sharpens_radii_smaller_than_the_spread() {
let grown = spread_radii([[10.0, 30.0], [0.0, 0.0], [20.0, 20.0], [5.0, 40.0]], 20.0);
assert_eq!(
grown,
[[27.5, 50.0], [0.0, 0.0], [40.0, 40.0], [16.5625, 60.0]],
"specification example r = 10, spread = 20 gives 27.5; r = 5 grows by \
20 × (1 + (0.25 − 1)³); zero stays sharp; r ≥ spread adds the spread"
);
assert_eq!(
spread_radii([[10.0, 3.0]; 4], -4.0),
[[6.0, 0.0]; 4],
"a negative spread subtracts and floors at zero"
);
}
#[test]
fn blurred_edges_follow_the_gaussian_of_half_the_blur_radius() {
let sigma = 8.0 * SIGMA_PER_BLUR;
let shape = Rect {
x: 0.0,
y: 0.0,
width: 100.0,
height: 60.0,
};
let region = outset(shape, [sigma * BLUR_EXTENT_SIGMAS; 4]);
let square = Some(ShadowArea::Rounded(shape, [[0.0; 2]; 4]));
let mask = blurred_mask(region, square.clone(), sigma, false);
let inverted = blurred_mask(region, square, sigma, true);
for distance in [-2.0 * sigma, -sigma, 0.0, sigma, 2.0 * sigma] {
let point = [shape.x + distance, shape.y + shape.height / 2.0];
let (actual, centre) = pixel_at(&mask, point);
let expected = phi((centre[0] - shape.x) / sigma);
assert!(
(actual - expected).abs() < 0.01,
"pixel centred {}pt inside the edge: alpha {actual}, Gaussian {expected}",
centre[0] - shape.x
);
let (complement, _) = pixel_at(&inverted, point);
assert!(
(complement - (1.0 - expected)).abs() < 0.01,
"the same pixel around an inset hole: alpha {complement}, expected {}",
1.0 - expected
);
}
let (corner, centre) = pixel_at(&mask, [shape.x, shape.y]);
let expected = phi((centre[0] - shape.x) / sigma) * phi((centre[1] - shape.y) / sigma);
assert!(
(corner - expected).abs() < 0.01,
"a square corner is the product of the two edge profiles: {corner}, expected {expected}"
);
}
#[test]
fn mask_grid_keeps_extreme_aspect_ratios_within_the_allocation_budget() {
for [width, height] in [[1.0e12, 1.0], [1.0, 1.0e12], [1.0e30, 1.0e30]] {
let (size, pitch) = mask_grid(
Rect {
x: 0.0,
y: 0.0,
width,
height,
},
0.5,
);
assert!(
size[0] as f64 * size[1] as f64 <= f64::from(MAX_MASK_PIXELS),
"{width}x{height}: rounded grid {size:?} exceeds the shadow pixel budget"
);
assert!(
pitch.iter().all(|value| value.is_finite() && *value > 0.0),
"finite positive region needs finite positive pitch: {pitch:?}"
);
if width == height {
let expected = MAX_MASK_PIXELS.sqrt() as usize;
assert!(
size.iter().all(|&extent| extent >= expected - 1),
"overflow in physical area must not collapse a large square to a single pixel: {size:?}"
);
}
}
}
#[test]
fn huge_blurs_of_huge_regions_sample_each_deviation_with_a_bounded_kernel() {
const SIGMA: f32 = 5.0e29;
let (size, pitch) = mask_grid(
Rect {
x: 0.0,
y: 0.0,
width: 5.0e30,
height: 5.0e30,
},
SIGMA,
);
assert!(
pitch
.iter()
.all(|pitch| SIGMA / pitch <= MASK_SIGMA_PIXELS * (1.0 + f32::EPSILON)),
"a region whose area overflows f32 but holds few deviations must not be sampled \
finer than {MASK_SIGMA_PIXELS} px per deviation, which widens the kernel: \
{size:?} pixels of pitch {pitch:?}"
);
}
#[test]
fn mask_density_tracks_the_deviation_within_limits() {
let region = Rect {
x: 0.0,
y: 0.0,
width: 50.0,
height: 20.0,
};
assert_eq!(
blurred_mask(region, None, 0.5, false).size,
[200, 80],
"small blurs sample at the {MAX_MASK_PIXELS_PER_PT} px/pt cap"
);
assert_eq!(
blurred_mask(region, None, 12.0, false).size,
[25, 10],
"a 12pt deviation needs only {MASK_SIGMA_PIXELS} px per deviation"
);
let huge = Rect {
width: 10_000.0,
height: 10_000.0,
..region
};
let capped = blurred_mask(huge, None, 0.5, false);
let pixels = capped.size[0] as f32 * capped.size[1] as f32;
assert!(
pixels <= MAX_MASK_PIXELS * 1.01,
"pixel budget exceeded: {:?} has {pixels} pixels",
capped.size
);
}
#[test]
fn convolving_repeated_rows_matches_every_pixel_summed_in_kernel_order() {
let [columns, rows] = [7, 40];
let pattern = |row: usize| match row {
0..4 | 36.. => 0.0,
4..6 => 0.25,
6..25 => 1.0,
25..27 => 0.5,
_ => 0.75,
};
let values: Vec<f32> = (0..rows)
.flat_map(|row| (0..columns).map(move |column| pattern(row) * (column + 1) as f32))
.collect();
let kernel = gaussian_kernel(1.7);
let radius = kernel.len() / 2;
for vertical in [false, true] {
let actual = convolve(&values, [columns, rows], &kernel, vertical);
for row in 0..rows {
for column in 0..columns {
let mut expected = 0.0;
for (tap, weight) in kernel.iter().enumerate() {
let [x, y] = if vertical {
[Some(column), (row + tap).checked_sub(radius)]
} else {
[(column + tap).checked_sub(radius), Some(row)]
};
if let (Some(x), Some(y)) = (x.filter(|&x| x < columns), y)
&& y < rows
{
expected += weight * values[y * columns + x];
}
}
let index = row * columns + column;
assert_eq!(
actual[index].to_bits(),
expected.to_bits(),
"vertical {vertical}, row {row}, column {column}: {} but summing the \
taps in order gives {expected}",
actual[index]
);
}
}
}
}
#[test]
fn rounded_rows_span_the_elliptical_corners() {
let rect = Rect {
x: 10.0,
y: 0.0,
width: 100.0,
height: 40.0,
};
let radii = [[20.0, 10.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]];
let top = row_span(rect, radii, 0.0).unwrap();
assert!(
(top[0] - 30.0).abs() < 1e-4 && top[1] == 110.0,
"the top row starts where the corner arc meets the top edge: {top:?}"
);
let middle = row_span(rect, radii, 5.0).unwrap();
let expected = 10.0 + 20.0 * (1.0 - 0.75_f32.sqrt());
assert!(
(middle[0] - expected).abs() < 1e-4,
"half-way down the corner: {middle:?}, expected {expected}"
);
assert_eq!(row_span(rect, radii, 41.0), None, "below the box");
}
}