use crate::color::Color;
use crate::geom::{Point, Rect, pt, rect};
use crate::path::Path;
pub type Edges<'a> = &'a mut dyn FnMut(&mut dyn FnMut(Point, Point));
pub(crate) const FINE: usize = 2;
const GROUP: usize = 8 / FINE;
pub(crate) const SOFT: f32 = 0.75;
pub struct Canvas<'a> {
pixels: &'a mut [u32],
width: i32,
height: i32,
origin: (i32, i32),
scale: f32,
clip: [i32; 4],
visible: Rect,
cover: Vec<f32>,
cells: Vec<u8>,
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
list: Option<&'a mut crate::gpu_draw::DrawList>,
}
impl std::fmt::Debug for Canvas<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Canvas").field("width", &self.width).field("height", &self.height).field("origin", &self.origin).field("scale", &self.scale).finish_non_exhaustive()
}
}
impl<'a> Canvas<'a> {
pub fn new(pixels: &'a mut [u32], width: usize, height: usize, origin: (i32, i32), scale: f32) -> Self {
assert!(pixels.len() >= width * height, "the block is smaller than it says");
let (width, height) = (width as i32, height as i32);
let mut canvas = Canvas {
pixels,
width,
height,
origin,
scale,
clip: [0; 4],
visible: Rect::EMPTY,
cover: Vec::new(),
cells: Vec::new(),
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
list: None,
};
canvas.restore_clip([origin.0, origin.1, origin.0 + width, origin.1 + height]);
canvas
}
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
#[inline]
pub(crate) fn recording(list: &'a mut crate::gpu_draw::DrawList, width: usize, height: usize, origin: (i32, i32), scale: f32) -> Self {
let mut canvas = Canvas::new(&mut [], 0, 0, origin, scale);
(canvas.width, canvas.height, canvas.list) = (width as i32, height as i32, Some(list));
canvas.restore_clip([origin.0, origin.1, origin.0 + width as i32, origin.1 + height as i32]);
canvas
}
#[cfg(all(windows, feature = "gpu-draw"))]
#[inline]
pub(crate) fn list(&mut self) -> Option<&mut crate::gpu_draw::DrawList> {
self.list.as_deref_mut()
}
pub fn scale(&self) -> f32 {
self.scale
}
pub fn origin(&self) -> (i32, i32) {
self.origin
}
pub fn clip_pixels(&self) -> [i32; 4] {
self.clip
}
pub fn area(&self) -> Rect {
rect(self.origin.0 as f32 / self.scale, self.origin.1 as f32 / self.scale, self.width as f32 / self.scale, self.height as f32 / self.scale)
}
pub fn visible(&self) -> Rect {
self.visible
}
pub fn clip(&mut self, area: Rect) -> [i32; 4] {
let before = self.clip;
let s = self.scale;
let want = [whole(area.x * s), whole(area.y * s), whole(area.right() * s), whole(area.bottom() * s)];
self.restore_clip([want[0].max(before[0]), want[1].max(before[1]), want[2].min(before[2]), want[3].min(before[3])]);
before
}
pub fn restore_clip(&mut self, clip: [i32; 4]) {
let ([left, top, right, bottom], s) = (clip, self.scale);
(self.clip, self.visible) = (clip, rect(left as f32 / s, top as f32 / s, (right - left) as f32 / s, (bottom - top) as f32 / s));
}
pub fn with_clip(&mut self, area: Rect, draw: impl FnOnce(&mut Self)) {
let before = self.clip(area);
draw(self);
self.restore_clip(before);
}
#[deprecated(since = "0.2.1", note = "renamed to `with_clip`")]
pub fn held_to(&mut self, area: Rect, draw: impl FnOnce(&mut Self)) {
self.with_clip(area, draw);
}
pub(crate) fn keep_under(&mut self, area: Rect, kept: &mut Vec<u32>) -> [i32; 4] {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
list.failed = true;
return [0; 4];
}
let (s, [cx0, cy0, cx1, cy1]) = (self.scale, self.clip);
let part = [whole(area.x * s - 1.0).max(cx0), whole(area.y * s - 1.0).max(cy0), whole(area.right() * s + 1.0).min(cx1), whole(area.bottom() * s + 1.0).min(cy1)];
let [left, top, right, bottom] = part;
if left < right {
(top..bottom).for_each(|y| kept.extend_from_slice(self.row(y, left, right)));
}
part
}
pub(crate) fn let_through(&mut self, [left, top, right, bottom]: [i32; 4], kept: &mut Vec<u32>, fade: u8) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if self.list.is_some() {
return;
}
let wide = if left < right && top < bottom { (right - left) as usize } else { return };
let from = kept.len().saturating_sub(wide * (bottom - top) as usize);
for (y, under) in (top..bottom).zip(kept[from..].chunks_exact(wide)) {
self.row(y, left, right).iter_mut().zip(under).for_each(|(drawn, under)| *drawn = between(*under, *drawn, 255 - fade as u32));
}
kept.truncate(from);
}
#[inline(always)]
fn row(&mut self, y: i32, x0: i32, x1: i32) -> &mut [u32] {
let at = ((y - self.origin.1) * self.width + (x0 - self.origin.0)) as usize;
&mut self.pixels[at..at + (x1 - x0) as usize]
}
pub fn clear(&mut self, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.failed = true;
}
spread(self.pixels, color.over(0, 255));
}
pub fn erase(&mut self) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.failed = true;
}
let [left, top, right, bottom] = self.clip;
for y in top..bottom.max(top) {
spread(self.row(y, left, right.max(left)), 0);
}
}
pub fn rect(&mut self, r: Rect, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.rect(r, color, self.scale, self.clip);
}
let (s, [cx0, cy0, cx1, cy1]) = (self.scale, self.clip);
let (left, top) = (((r.x * s).round() as i32).max(cx0), ((r.y * s).round() as i32).max(cy0));
let (right, bottom) = (((r.right() * s).round() as i32).min(cx1), ((r.bottom() * s).round() as i32).min(cy1));
if color.is_none() || left >= right {
return;
}
let (solid, fill) = (color.a() == 255, color.0 | 0xff00_0000);
for y in top..bottom {
match solid {
true => spread(self.row(y, left, right), fill),
false => self.row(y, left, right).iter_mut().for_each(|p| *p = color.over(*p, 255)),
}
}
}
pub fn rounded(&mut self, r: Rect, radius: f32, color: Color) {
match radius > 0.0 {
true => self.ring(r, radius, f32::INFINITY, color),
false => self.rect(r, color),
}
}
pub fn border(&mut self, r: Rect, radius: f32, width: f32, color: Color) {
if width > 0.0 {
self.ring(r, radius, width, color);
}
}
pub fn shadow(&mut self, r: Rect, radius: f32, reach: f32, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.shadow(r, radius, reach, color, self.scale, self.clip);
}
if color.is_none() || r.is_empty() || reach <= 0.0 {
return;
}
let s = self.scale;
let (reach, drop) = (reach * s, reach * s / 4.0);
let (x0, y0, x1, y1) = (r.x * s, r.y * s + drop, r.right() * s, r.bottom() * s + drop);
let radius = (radius * s).min((x1 - x0) / 2.0).min((y1 - y0) / 2.0).max(0.0);
let [cx0, cy0, cx1, cy1] = self.clip;
let (left, top) = (((x0 - reach).floor() as i32).max(cx0), ((y0 - reach).floor() as i32).max(cy0));
let (right, bottom) = (((x1 + reach).ceil() as i32).min(cx1), ((y1 + reach).ceil() as i32).min(cy1));
if left >= right || top >= bottom {
return;
}
for y in top..bottom {
let py = y as f32 + 0.5;
let dy = (y0 + radius - py).max(py - (y1 - radius)).max(0.0);
for (i, p) in self.row(y, left, right).iter_mut().enumerate() {
let px = (left + i as i32) as f32 + 0.5;
let dx = (x0 + radius - px).max(px - (x1 - radius)).max(0.0);
let away = (dx * dx + dy * dy).sqrt() - radius;
let near = if away > -1.0 { (1.0 - away / reach).clamp(0.0, 1.0) } else { (py >= y1 - drop - radius - 1.0) as u8 as f32 };
*p = color.over(*p, (near * near * 255.0) as u32);
}
}
}
fn ring(&mut self, r: Rect, radius: f32, thick: f32, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.ring(r, radius, thick, color, self.scale, self.clip);
}
if color.is_none() || r.is_empty() {
return;
}
let s = self.scale;
let (x0, y0, x1, y1) = ((r.x * s).round(), (r.y * s).round(), (r.right() * s).round(), (r.bottom() * s).round());
let radius = (radius * s).min((x1 - x0) / 2.0).min((y1 - y0) / 2.0).max(0.0);
let thick = if thick.is_finite() { (thick * s).round().max(1.0) } else { f32::INFINITY };
let [cx0, cy0, cx1, cy1] = self.clip;
let (left, top, right, bottom) = ((x0 as i32).max(cx0), (y0 as i32).max(cy0), (x1 as i32).min(cx1), (y1 as i32).min(cy1));
if left >= right || top >= bottom {
return;
}
let hollow = thick.is_finite();
let cover_at = |x: i32, y: i32| -> f32 {
let outer = inside(x, y, x0, y0, x1, y1, radius);
let (px, py) = (x as f32 + 0.5, y as f32 + 0.5);
if !hollow || outer <= 0.0 || px < x0 + thick || px > x1 - thick || py < y0 + thick || py > y1 - thick {
return outer;
}
outer - inside(x, y, x0 + thick, y0 + thick, x1 - thick, y1 - thick, (radius - thick).max(0.0)).min(outer)
};
let (solid, fill) = (color.a() == 255, color.0 | 0xff00_0000);
let lay = |stretch: &mut [u32]| match solid {
true => spread(stretch, fill),
false => stretch.iter_mut().for_each(|p| *p = color.over(*p, 255)),
};
let edge = if hollow { thick as i32 } else { 0 };
let ends = (radius.ceil() as i32).max(edge).min(1 << 24);
let far = |at: f32| at.clamp(-16_777_216.0, 16_777_216.0) as i32;
let (x0, y0, x1, y1) = (far(x0), far(y0), far(x1), far(y1));
let (mut corner, kept) = ([0u8; 256], ends <= 16);
let shows = (top < y0 + ends || bottom > y1 - ends) && (left < x0 + ends || right > x1 - ends);
for at in 0..if kept && shows { ends * ends } else { 0 } {
corner[at as usize] = (cover_at(x0 + at % ends, y0 + at / ends) * 255.0) as u8;
}
let covered = |x: i32, y: i32| match kept {
true => corner[((y - y0).min(y1 - 1 - y) * ends + (x - x0).min(x1 - 1 - x)) as usize] as u32,
false => (cover_at(x, y) * 255.0) as u32,
};
for y in top..bottom {
let cornered = y < y0 + ends || y >= y1 - ends;
let side = if cornered { ends } else { edge };
let from = (x0 + side).clamp(left, right);
let to = (x1 - side).clamp(from, right);
let whole = cover_at((x0 + x1) / 2, y) >= 1.0;
let (near, rest) = self.row(y, left, right).split_at_mut((from - left) as usize);
let (middle, far) = rest.split_at_mut((to - from) as usize);
match cornered {
true => near.iter_mut().enumerate().for_each(|(i, p)| *p = color.over(*p, covered(left + i as i32, y))),
false => lay(near),
}
match cornered {
true => far.iter_mut().enumerate().for_each(|(i, p)| *p = color.over(*p, covered(to + i as i32, y))),
false => lay(far),
}
if whole {
lay(middle);
}
}
}
pub fn gradient(&mut self, r: Rect, radius: f32, colors: &[(f32, Color)], turn: Option<f32>) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.gradient(r, radius, colors, turn, self.scale, self.clip);
}
let s = self.scale;
let (x0, y0, x1, y1) = ((r.x * s).round(), (r.y * s).round(), (r.right() * s).round(), (r.bottom() * s).round());
let radius = (radius * s).min((x1 - x0) / 2.0).min((y1 - y0) / 2.0).max(0.0);
let [cx0, cy0, cx1, cy1] = self.clip;
let (left, top, right, bottom) = ((x0 as i32).max(cx0), (y0 as i32).max(cy0), (x1 as i32).min(cx1), (y1 as i32).min(cy1));
if left >= right || top >= bottom {
return;
}
let (mid_x, mid_y, half_x, half_y) = ((x0 + x1) / 2.0, (y0 + y1) / 2.0, ((x1 - x0 - 1.0) / 2.0).max(0.5), ((y1 - y0 - 1.0) / 2.0).max(0.5));
let (sin, cos) = turn.map_or((0.0, 0.0), |turn| turn.to_radians().sin_cos());
let reach = (sin.abs() * half_x + cos.abs() * half_y).max(0.5);
for y in top..bottom {
for (i, p) in self.row(y, left, right).iter_mut().enumerate() {
let x = left + i as i32;
let (dx, dy) = (x as f32 + 0.5 - mid_x, y as f32 + 0.5 - mid_y);
let along = if turn.is_some() { 0.5 + (dx * sin - dy * cos) / (2.0 * reach) } else { (dx / half_x).hypot(dy / half_y) };
*p = color_at(colors, along).over(*p, (inside(x, y, x0, y0, x1, y1, radius) * 255.0) as u32);
}
}
}
pub fn fill(&mut self, path: &Path, at: Point, size: f32, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
let (s, to) = (size * self.scale, pt(at.x * self.scale, at.y * self.scale));
return list.fill_one(&mut |each| path.flatten(s, to, 1.0, each), color, self.scale, self.clip);
}
let (s, to) = (size * self.scale, pt(at.x * self.scale, at.y * self.scale));
if !color.is_none() {
self.fill_with(&mut |each| path.flatten(s, to, 1.0, each), false, |_, _| color);
}
}
pub fn stroke(&mut self, path: &Path, at: Point, size: f32, width: f32, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.stroke(path, at, size, width, color, self.scale, self.clip);
}
let (fine, [cx0, cy0, cx1, cy1]) = (FINE as f32, self.clip);
let (s, to) = (size * self.scale * fine, pt(at.x * self.scale * fine, at.y * self.scale * fine));
let reach = (width * s / 2.0).max(0.5 * fine) + SOFT;
let (mut x0, mut y0, mut x1, mut y1) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
path.flatten(s, to, fine, |a, b| {
(x0, y0) = (x0.min(a.x).min(b.x), y0.min(a.y).min(b.y));
(x1, y1) = (x1.max(a.x).max(b.x), y1.max(a.y).max(b.y));
});
let (left, top) = ((((x0 - reach) / fine).floor() as i32).max(cx0), (((y0 - reach) / fine).floor() as i32).max(cy0));
let (right, bottom) = ((((x1 + reach) / fine).ceil() as i32).min(cx1), (((y1 + reach) / fine).ceil() as i32).min(cy1));
if color.is_none() || left >= right || top >= bottom {
return;
}
let (stride, mut cells) = ((right - left) as usize * FINE, std::mem::take(&mut self.cells));
let used = stride * (bottom - top) as usize * FINE;
cells.resize(cells.len().max(used), 0);
path.flatten(s, to, fine, |a, b| thick(&mut cells[..used], stride, (left * FINE as i32, top * FINE as i32), a, b, reach));
for (y, rows) in cells[..used].chunks_exact_mut(stride * FINE).enumerate() {
lay(self.row(top + y as i32, left, right), rows, stride, color);
}
self.cells = cells;
}
#[inline(always)]
pub(crate) fn fill_one(&mut self, edges: Edges, color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.fill_one(edges, color, self.scale, self.clip);
}
self.fill_with(edges, false, |_, _| color);
}
pub fn fill_with(&mut self, edges: Edges, even_odd: bool, mut ink: impl FnMut(i32, i32) -> Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.fill_with(edges, even_odd, ink, self.scale, self.clip);
}
let (mut x0, mut y0, mut x1, mut y1) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
edges(&mut |a, b| {
(x0, y0) = (x0.min(a.x).min(b.x), y0.min(a.y).min(b.y));
(x1, y1) = (x1.max(a.x).max(b.x), y1.max(a.y).max(b.y));
});
let [cx0, cy0, cx1, cy1] = self.clip;
let (left, right) = ((x0.floor() as i32).max(self.origin.0), (x1.ceil() as i32).saturating_add(1).min(self.origin.0 + self.width));
let (top, bottom) = ((y0.floor() as i32).max(cy0), (y1.ceil() as i32).min(cy1));
let (from, to) = (left.max(cx0), right.min(cx1));
if from >= to || top >= bottom {
return;
}
let (stride, h) = ((right - left) as usize + 2, (bottom - top) as usize);
let mut cover = std::mem::take(&mut self.cover);
cover.clear();
cover.resize(stride * h, 0.0);
let near = |p: Point| pt(p.x - left as f32, p.y - top as f32);
edges(&mut |a, b| edge(&mut cover, stride, h, near(a), near(b)));
for (y, cells) in cover.chunks_exact(stride).enumerate() {
let skipped = (from - left) as usize;
let mut sum: f32 = cells[..skipped].iter().sum();
for ((p, cell), x) in self.row(top + y as i32, from, to).iter_mut().zip(&cells[skipped..]).zip(from..) {
sum += cell;
let share = if even_odd { (sum - 2.0 * (sum / 2.0).round()).abs() } else { sum.abs().min(1.0) };
let color = if share > 0.0 { ink(x, top + y as i32) } else { Color::NONE };
*p = color.over(*p, (share * 255.0 + 0.5) as u32);
}
}
self.cover = cover;
}
pub fn picture(&mut self, pixels: &[u32], width: usize, r: Rect) {
self.rounded_picture(pixels, width, r, 0.0);
}
pub fn rounded_picture(&mut self, pixels: &[u32], width: usize, r: Rect, radius: f32) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.picture(pixels, width, r, radius, self.scale, self.clip);
}
let (s, height) = (self.scale, pixels.len() / width.max(1));
let (x0, y0, x1, y1) = (whole(r.x * s), whole(r.y * s), whole(r.right() * s), whole(r.bottom() * s));
let [cx0, cy0, cx1, cy1] = self.clip;
let (from, to) = (x0.max(cx0), x1.min(cx1));
if from >= to || y1 <= y0 || height == 0 {
return;
}
let (step_x, step_y) = (width as f32 / (x1 - x0) as f32, height as f32 / (y1 - y0) as f32);
if step_x > 1.0 || step_y > 1.0 {
return self.shrunk(pixels, width, [x0, y0, x1, y1], radius * s);
}
let covered = corners([x0, y0, x1, y1], radius * s);
let mix = |a: u32, b: u32, t: u32| ((((a & 0xff00ff) * (256 - t) + (b & 0xff00ff) * t) >> 8) & 0xff00ff) | (((a >> 8 & 0xff00ff) * (256 - t) + (b >> 8 & 0xff00ff) * t) & 0xff00_ff00);
for y in y0.max(cy0)..y1.min(cy1) {
let v = (((y - y0) as f32 + 0.5) * step_y - 0.5).clamp(0.0, (height - 1) as f32);
let (above, below, down) = (v as usize * width, (v as usize + 1).min(height - 1) * width, (v.fract() * 256.0) as u32);
for (i, p) in self.row(y, from, to).iter_mut().enumerate() {
let u = (((from - x0) as f32 + i as f32 + 0.5) * step_x - 0.5).clamp(0.0, (width - 1) as f32);
let (left, right, along) = (u as usize, (u as usize + 1).min(width - 1), (u.fract() * 256.0) as u32);
let color = mix(mix(pixels[above + left], pixels[above + right], along), mix(pixels[below + left], pixels[below + right], along), down);
*p = Color(color).over(*p, covered(from + i as i32, y));
}
}
}
fn shrunk(&mut self, pixels: &[u32], width: usize, [x0, y0, x1, y1]: [i32; 4], radius: f32) {
let height = pixels.len() / width;
let (step_x, step_y) = (width as f32 / (x1 - x0) as f32, height as f32 / (y1 - y0) as f32);
let on = |i: i32, step: f32, size: usize| {
let from = (i as f32 + 0.5) * step - step.max(1.0) / 2.0;
[from.max(0.0), (from + step.max(1.0)).min(size as f32)]
};
let [cx0, cy0, cx1, cy1] = self.clip;
let (first, covered) = (x0.max(cx0), corners([x0, y0, x1, y1], radius));
for y in y0.max(cy0)..y1.min(cy1) {
let [top, bottom] = on(y - y0, step_y, height);
for (i, p) in self.row(y, first, x1.min(cx1)).iter_mut().enumerate() {
let [left, right] = on(first - x0 + i as i32, step_x, width);
*p = Color(mean(pixels, width, [left, top, right, bottom])).over(*p, covered(first + i as i32, y));
}
}
}
pub fn pixels(&mut self, pixels: &[u32], width: usize, [x, y]: [i32; 2], solid: bool) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.pixels(pixels, width, [x, y], self.clip);
}
let ([cx0, cy0, cx1, cy1], high) = (self.clip, (pixels.len() / width.max(1)) as i32);
let (from, to) = (x.max(cx0), x.saturating_add(width as i32).min(cx1));
for row in y.max(cy0)..y.saturating_add(high).min(cy1) {
if from >= to {
return;
}
let start = (row - y) as usize * width;
let (under, block) = (self.row(row, from, to), &pixels[start + (from - x) as usize..start + (to - x) as usize]);
match solid {
true => under.copy_from_slice(block),
false => under.iter_mut().zip(block).for_each(|(under, over)| *under = laid(*over, *under)),
}
}
}
pub fn fill_mask(&mut self, block: &[u32], width: usize, [x, y]: [i32; 2], mut color: impl FnMut(i32, i32) -> Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
let area = [x, y, x.saturating_add(width as i32), y.saturating_add((block.len() / width.max(1)) as i32)];
return list.patch(area, self.clip, self.scale, |canvas| canvas.fill_mask(block, width, [x, y], color));
}
let ([cx0, cy0, cx1, cy1], high) = (self.clip, (block.len() / width.max(1)) as i32);
let (from, to) = (x.max(cx0), x.saturating_add(width as i32).min(cx1));
for row in y.max(cy0)..y.saturating_add(high).min(cy1) {
if from >= to {
return;
}
let start = (row - y) as usize * width + (from - x) as usize;
let covered = self.row(row, from, to).iter_mut().zip(&block[start..]).zip(from..).filter(|((_, cover), _)| **cover >> 24 != 0);
covered.for_each(|((pixel, cover), x)| *pixel = color(x, row).over(*pixel, cover >> 24));
}
}
#[deprecated(since = "0.2.1", note = "renamed to `fill_mask`")]
pub fn ink(&mut self, block: &[u32], width: usize, at: [i32; 2], color: impl FnMut(i32, i32) -> Color) {
self.fill_mask(block, width, at, color);
}
pub fn cover(&mut self, x: i32, y: i32, width: usize, cover: &[u8], color: Color) {
#[cfg(all(feature = "gpu-draw", not(target_os = "macos")))]
if let Some(list) = self.list.as_deref_mut() {
return list.mask(x, y, width, cover, color, self.clip);
}
if width == 0 || color.is_none() {
return;
}
let [cx0, cy0, cx1, cy1] = self.clip;
for (j, line) in cover.chunks_exact(width).enumerate() {
let py = y.saturating_add(j as i32);
if py < cy0 || py >= cy1 {
continue;
}
let (from, to) = (x.max(cx0), x.saturating_add(width as i32).min(cx1));
if from >= to {
return;
}
let dest = self.row(py, from, to);
let covered = dest.iter_mut().zip(&line[(from - x) as usize..]).filter(|(_, cover)| **cover > 0);
covered.for_each(|(p, cover)| *p = color.over(*p, *cover as u32));
}
}
}
#[inline]
fn between(from: u32, to: u32, along: u32) -> u32 {
let part = |from: u32, to: u32| {
let sum = from * (255 - along) + to * along + 0x0080_0080;
(sum + (sum >> 8 & 0x00ff_00ff)) >> 8 & 0x00ff_00ff
};
part(from & 0x00ff_00ff, to & 0x00ff_00ff) | part(from >> 8 & 0x00ff_00ff, to >> 8 & 0x00ff_00ff) << 8
}
#[inline]
fn laid(over: u32, under: u32) -> u32 {
let clear = 255 - (over >> 24);
if clear == 0 || clear == 255 {
return if clear == 0 { over } else { under };
}
let part = |pair: u32| (pair * clear + 0x0080_0080 + ((pair * clear + 0x0080_0080) >> 8 & 0x00ff_00ff)) >> 8 & 0x00ff_00ff;
over + (part(under & 0x00ff_00ff) | part(under >> 8 & 0x00ff_00ff) << 8)
}
pub fn coverage(path: &Path, size: f32, stroke: f32, origin: (i32, i32), width: usize, height: usize) -> Vec<u8> {
if stroke > 0.0 {
let (fine, stride, mut cells) = (FINE as f32, width * FINE, vec![0u8; width * FINE * height * FINE]);
let reach = (stroke * size / 2.0).max(0.5) * fine + SOFT;
path.flatten(size * fine, pt(0.0, 0.0), fine, |a, b| thick(&mut cells, stride, (origin.0 * FINE as i32, origin.1 * FINE as i32), a, b, reach));
return cells.chunks_exact((stride * FINE).max(1)).flat_map(|rows| (0..width).map(move |x| covered(rows, stride, x) as u8)).collect();
}
let (stride, mut cells) = (width + 2, vec![0.0f32; (width + 2) * height]);
let mut add = |a: Point, b: Point| edge(&mut cells, stride, height, pt(a.x - origin.0 as f32, a.y - origin.1 as f32), pt(b.x - origin.0 as f32, b.y - origin.1 as f32));
path.flatten(size, pt(0.0, 0.0), 1.0, &mut add);
let mut covers = Vec::with_capacity(width * height);
for row in cells.chunks_exact(stride) {
let mut sum = 0.0;
covers.extend(row[..width].iter().map(|cell| {
sum += cell;
(sum.abs().min(1.0) * 255.0) as u8
}));
}
covers
}
#[inline(always)]
fn spread(stretch: &mut [u32], pixel: u32) {
let mut done = stretch.len().min(16);
stretch[..done].fill(pixel);
while done < stretch.len() {
let more = done.min(stretch.len() - done);
stretch.copy_within(..more, done);
done += more;
}
}
fn whole(at: f32) -> i32 {
at.round().clamp(-16_777_216.0, 16_777_216.0) as i32
}
fn corners([x0, y0, x1, y1]: [i32; 4], radius: f32) -> impl Fn(i32, i32) -> u32 {
let radius = radius.min((x1 - x0) as f32 / 2.0).min((y1 - y0) as f32 / 2.0).max(0.0);
let (left, top, right, bottom) = (x0 as f32, y0 as f32, x1 as f32, y1 as f32);
move |x, y| if radius > 0.0 { (inside(x, y, left, top, right, bottom, radius) * 255.0) as u32 } else { 255 }
}
#[inline(always)]
fn inside(px: i32, py: i32, x0: f32, y0: f32, x1: f32, y1: f32, radius: f32) -> f32 {
let (x, y) = (px as f32 + 0.5, py as f32 + 0.5);
let (dx, dy) = ((x0 + radius - x).max(x - (x1 - radius)), (y0 + radius - y).max(y - (y1 - radius)));
if dx > 0.0 && dy > 0.0 { (radius - (dx * dx + dy * dy).sqrt() + 0.5).clamp(0.0, 1.0) } else { 1.0 }
}
pub(crate) fn color_at(colors: &[(f32, Color)], along: f32) -> Color {
let after = colors.partition_point(|color| color.0 <= along);
match (after.checked_sub(1).map(|before| colors[before]), colors.get(after)) {
(Some(before), Some(after)) => before.1.lerp(after.1, (along - before.0) / (after.0 - before.0)),
(Some(only), None) | (None, Some(&only)) => only.1,
(None, None) => Color::NONE,
}
}
fn mean(pixels: &[u32], width: usize, [left, top, right, bottom]: [f32; 4]) -> u32 {
let mut sum = [0.0f32; 4];
for y in top as usize..bottom.ceil() as usize {
let high = bottom.min(y as f32 + 1.0) - top.max(y as f32);
for x in left as usize..right.ceil() as usize {
let (pixel, part) = (pixels[y * width + x], high * (right.min(x as f32 + 1.0) - left.max(x as f32)));
let solid = (pixel >> 24) as f32 * part;
sum = [sum[0] + solid, sum[1] + (pixel >> 16 & 255) as f32 * solid, sum[2] + (pixel >> 8 & 255) as f32 * solid, sum[3] + (pixel & 255) as f32 * solid];
}
}
let (area, solid) = ((right - left) * (bottom - top), sum[0].max(1e-6));
let [how_solid, red, green, blue] = [sum[0] / area, sum[1] / solid, sum[2] / solid, sum[3] / solid].map(|part| (part + 0.5).min(255.0) as u32);
how_solid << 24 | red << 16 | green << 8 | blue
}
pub(crate) fn halve(pixels: &[u32], width: usize) -> (usize, Vec<u32>) {
let (height, wide) = (pixels.len() / width.max(1), width.div_ceil(2));
let at = |x: usize, y: usize| pixels[y.min(height - 1) * width + x.min(width - 1)];
let four = |x: usize, y: usize| [at(x, y), at(x + 1, y), at(x, y + 1), at(x + 1, y + 1)];
let mean = |four: [u32; 4]| {
let solid: u32 = four.iter().map(|pixel| pixel >> 24).sum();
let part = |shift: u32| (four.iter().map(|pixel| (pixel >> shift & 255) * (pixel >> 24)).sum::<u32>() + solid / 2) / solid.max(1);
((solid + 2) / 4) << 24 | part(16) << 16 | part(8) << 8 | part(0)
};
(wide, (0..wide * height.div_ceil(2)).map(|i| mean(four(i % wide * 2, i / wide * 2))).collect())
}
fn thick(cells: &mut [u8], stride: usize, origin: (i32, i32), a: Point, b: Point, reach: f32) {
let ((dx, dy), rows) = ((b.x - a.x, b.y - a.y), (cells.len() / stride.max(1)) as i32);
let (per_long, per_down) = (1.0 / (dx * dx + dy * dy).max(1e-12), if dy == 0.0 { 0.0 } else { 1.0 / dy });
let square = Square::of(dx, dy, reach - SOFT);
let reach = square.deep;
let first = ((a.y.min(b.y) - reach).floor() as i32).saturating_sub(origin.1).max(0);
let last = ((a.y.max(b.y) + reach).ceil() as i32).saturating_sub(origin.1).min(rows);
for y in first..last {
let down = (y + origin.1) as f32 + 0.5 - a.y;
let (u, v) = if dy == 0.0 { (0.0, 1.0) } else { ((down - reach) * per_down, (down + reach) * per_down) };
let (p, q) = (a.x + u.clamp(0.0, 1.0) * dx, a.x + v.clamp(0.0, 1.0) * dx);
let from = ((p.min(q) - reach).floor() as i32).saturating_sub(origin.0).clamp(0, stride as i32);
let to = ((p.max(q) + reach).ceil() as i32).saturating_sub(origin.0).min(stride as i32).max(from);
let row = &mut cells[y as usize * stride..][from as usize..to as usize];
for (cell, x) in row.iter_mut().zip(from..) {
let across = (x + origin.0) as f32 + 0.5 - a.x;
let along = ((across * dx + down * dy) * per_long).clamp(0.0, 1.0);
let away = ((across - along * dx).powi(2) + (down - along * dy).powi(2)).sqrt();
*cell = (*cell).max(square.cover(away));
}
}
}
struct Square {
a: f32,
b: f32,
deep: f32,
per: f32,
}
impl Square {
fn of(dx: f32, dy: f32, half: f32) -> Square {
let long = dx.hypot(dy);
let (p, q) = if long > 0.0 { ((dy / long).abs(), (dx / long).abs()) } else { (0.0, 1.0) };
let (a, b) = (p.min(q).max(1.0 / 1024.0), p.max(q).max(std::f32::consts::FRAC_1_SQRT_2));
Square { a, b, deep: half + (a + b) / 2.0, per: 1.0 / (2.0 * a * b) }
}
#[inline(always)]
fn cover(&self, away: f32) -> u8 {
let (Square { a, b, per, .. }, deep) = (self, self.deep - away);
let corner = |past: f32| past.max(0.0) * past.max(0.0);
let cover = (corner(deep) - corner(deep - *a) - corner(deep - *b) + corner(deep - *a - *b)) * *per;
(cover.clamp(0.0, 1.0) * 255.0 + 0.5) as u8
}
}
fn lay(pixels: &mut [u32], rows: &mut [u8], stride: usize, color: Color) {
for (group, x) in pixels.chunks_mut(GROUP).zip((0..).step_by(GROUP)) {
let eight = |row: &[u8]| row[x * FINE..].first_chunk::<8>().map_or(1, |cells| u64::from_ne_bytes(*cells));
if rows.chunks_exact(stride).all(|row| eight(row) == 0) {
continue;
}
let covers = group.iter_mut().enumerate().map(|(i, p)| (p, covered(rows, stride, x + i)));
covers.filter(|(_, cover)| *cover > 0).for_each(|(p, cover)| *p = color.over(*p, cover));
rows.chunks_exact_mut(stride).for_each(|row| row[x * FINE..(x * FINE + 8).min(stride)].fill(0));
}
}
#[inline(always)]
fn covered(cells: &[u8], stride: usize, x: usize) -> u32 {
cells.chunks_exact(stride).map(|row| row[x * FINE..(x + 1) * FINE].iter().map(|cell| *cell as u32).sum::<u32>()).sum::<u32>() / (FINE * FINE) as u32
}
fn edge(cover: &mut [f32], stride: usize, h: usize, a: Point, b: Point) {
if a.y == b.y {
return;
}
let (dir, a, b) = if a.y < b.y { (1.0, a, b) } else { (-1.0, b, a) };
let (slope, wide) = ((b.x - a.x) / (b.y - a.y), (stride - 2) as f32);
for y in (a.y.max(0.0) as usize)..h.min(b.y.ceil().max(0.0) as usize) {
let (from, to) = ((y as f32).max(a.y), ((y + 1) as f32).min(b.y));
let d = (to - from) * dir;
let (p, q) = ((a.x + (from - a.y) * slope).clamp(0.0, wide), (a.x + (to - a.y) * slope).clamp(0.0, wide));
let (x0, x1) = if p < q { (p, q) } else { (q, p) };
let (first, last) = (x0.floor() as usize, x1.ceil() as usize);
let (row, start, end) = (y * stride, x0 - first as f32, x1 - first as f32);
if last <= first + 1 {
let mid = 0.5 * (start + end);
cover[row + first] += d - d * mid;
cover[row + first + 1] += d * mid;
continue;
}
let run = 1.0 / (end - start);
let head = 0.5 * run * (1.0 - start) * (1.0 - start);
let into_last = end - (last - first) as f32 + 1.0;
let tail = 0.5 * run * into_last * into_last;
cover[row + first] += d * head;
if last == first + 2 {
cover[row + first + 1] += d * (1.0 - head - tail);
} else {
let second = run * (1.5 - start);
cover[row + first + 1] += d * (second - head);
for i in first + 2..last - 1 {
cover[row + i] += d * run;
}
let before = second + (last - first - 3) as f32 * run;
cover[row + last - 1] += d * (1.0 - before - tail);
}
cover[row + last] += d * tail;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn block(w: usize, h: usize) -> Vec<u32> {
vec![0; w * h]
}
#[test]
fn a_rectangle_fills_exactly_its_pixels() {
let mut px = block(8, 8);
Canvas::new(&mut px, 8, 8, (0, 0), 1.0).rect(rect(2.0, 3.0, 4.0, 2.0), Color::WHITE);
let lit: Vec<usize> = px.iter().enumerate().filter(|(_, p)| **p != 0).map(|(i, _)| i).collect();
assert_eq!(lit, [26, 27, 28, 29, 34, 35, 36, 37]);
}
#[test]
fn a_picture_drawn_smaller_shows_the_mean_of_what_falls_on_each_pixel() {
let checks: Vec<u32> = (0..64).map(|i| if (i % 8 + i / 8) % 2 == 0 { 0xff00_0000 } else { 0xffff_ffff }).collect();
for (side, greys) in [(4usize, 0x7f..=0x80), (2, 0x7f..=0x80), (1, 0x7f..=0x80), (3, 0x78..=0x87)] {
let mut px = block(side, side);
Canvas::new(&mut px, side, side, (0, 0), 1.0).picture(&checks, 8, rect(0.0, 0.0, side as f32, side as f32));
assert!(px.iter().all(|p| greys.contains(&(p & 255)) && p >> 24 == 255), "drawn {side} wide: {px:x?}");
}
let (red, clear, blue) = (0xffff_0000, 0x0000_ff00, 0xff00_00ff);
assert_eq!(halve(&[red, clear, blue, red, clear, blue], 3), (2, vec![0x80ff_0000, blue]));
}
#[test]
fn a_shadow_is_dark_right_from_the_lower_edge_of_its_box() {
let mut px = vec![0xffff_ffffu32; 48 * 48];
Canvas::new(&mut px, 48, 48, (0, 0), 1.0).shadow(rect(10.0, 10.0, 20.0, 20.0), 6.0, 8.0, Color::BLACK.with_alpha(0.4));
let grey = |y: usize| px[y * 48 + 20] & 255;
assert!(grey(30) == grey(31) && grey(31) < grey(34) && grey(34) < grey(38), "{:?}", [30, 31, 32, 34, 38, 44].map(grey));
assert_eq!((grey(44), grey(20)), (255, 255), "white past its reach, and under the box (which covers that)");
}
#[test]
fn nothing_is_drawn_outside_the_clip_or_the_block() {
let mut px = block(8, 8);
let mut c = Canvas::new(&mut px, 8, 8, (0, 0), 1.0);
c.clip(rect(4.0, 0.0, 100.0, 100.0));
c.rounded(rect(-10.0, -10.0, 100.0, 100.0), 3.0, Color::WHITE);
c.stroke(&Path::svg("M-5 4h30"), pt(0.0, 0.0), 1.0, 2.0, Color::WHITE);
assert!(px.chunks(8).all(|row| row[..4].iter().all(|p| *p == 0) && row[4..].iter().all(|p| *p != 0)));
}
#[test]
#[allow(deprecated)]
fn the_old_names_draw_as_the_new_ones() {
let mask: Vec<u32> = (0..16).map(|at| (at * 16) << 24 | 0x00ff_ffff).collect();
let draw = |old: bool| {
let mut px = block(8, 8);
let mut c = Canvas::new(&mut px, 8, 8, (0, 0), 1.0);
let fill = |c: &mut Canvas| c.rounded(rect(0.0, 0.0, 8.0, 8.0), 0.0, Color::WHITE);
let color = |x: i32, y: i32| Color::rgb(0x10_2030 + (x * 8 + y) as u32);
match old {
true => (c.held_to(rect(2.0, 2.0, 3.0, 3.0), fill), c.ink(&mask, 4, [5, 4], color)),
false => (c.with_clip(rect(2.0, 2.0, 3.0, 3.0), fill), c.fill_mask(&mask, 4, [5, 4], color)),
};
px
};
let new = draw(false);
assert_eq!(draw(true), new);
assert_eq!(new.iter().filter(|p| **p == Color::WHITE.0).count(), 9, "the clip held the fill to 3 by 3 pixels");
assert!(new[5 * 8 + 6] != 0, "the mask laid down its colours");
}
#[test]
fn a_strip_of_a_window_holds_the_same_pixels_as_the_whole() {
let draw = |c: &mut Canvas| {
c.rounded(rect(1.0, 1.0, 14.0, 14.0), 5.0, Color::rgb(0x8b93ff));
c.border(rect(1.0, 1.0, 14.0, 14.0), 5.0, 1.0, Color::WHITE);
c.fill(&Path::svg("M8 1.8 9.4 6.6 14.2 8 9.4 9.4 8 14.2 6.6 9.4 1.8 8 6.6 6.6z"), pt(0.0, 0.0), 1.0, Color::BLACK);
c.stroke(&Path::svg("M2.5 7a4.5 4.5 0 1 0 9 0a4.5 4.5 0 1 0-9 0M10.5 10.5 14 14"), pt(0.3, 0.6), 1.0, 1.5, Color::WHITE);
};
let mut whole = block(16, 16);
draw(&mut Canvas::new(&mut whole, 16, 16, (0, 0), 1.0));
let mut strip = block(16, 4);
draw(&mut Canvas::new(&mut strip, 16, 4, (0, 8), 1.0));
assert_eq!(strip[..], whole[8 * 16..12 * 16]);
}
fn counted(d: &str, px: usize, stroke: f32) -> Vec<f32> {
let (scale, mut pieces) = (px as f32 / 16.0, Vec::new());
Path::svg(d).flatten(scale * 4.0, pt(0.0, 0.0), 4.0, |a, b| pieces.push((pt(a.x / 4.0, a.y / 4.0), pt(b.x / 4.0, b.y / 4.0))));
let half = (stroke * scale / 2.0).max(0.5);
let near = |x: f32, y: f32| {
pieces.iter().any(|(a, b)| {
let (dx, dy) = (b.x - a.x, b.y - a.y);
let t = (((x - a.x) * dx + (y - a.y) * dy) / (dx * dx + dy * dy).max(1e-12)).clamp(0.0, 1.0);
(x - a.x - t * dx).powi(2) + (y - a.y - t * dy).powi(2) <= half * half
})
};
let inside = |i: usize, sub: usize| near((i % px) as f32 + (sub % 16) as f32 / 16.0 + 1.0 / 32.0, (i / px) as f32 + (sub / 16) as f32 / 16.0 + 1.0 / 32.0);
(0..px * px).map(|i| (0..256).filter(|sub| inside(i, *sub)).count() as f32 / 256.0).collect()
}
#[test]
fn an_outline_covers_each_pixel_by_as_much_as_lies_on_it() {
let drawings = [
"M1.8 4.3a1 1 0 0 1 1-1h3l1.4 1.5h6a1 1 0 0 1 1 1v6.4a1 1 0 0 1-1 1H2.8a1 1 0 0 1-1-1z",
"M8 1.8 9.4 6.6 14.2 8 9.4 9.4 8 14.2 6.6 9.4 1.8 8 6.6 6.6z",
"M2.5 7a4.5 4.5 0 1 0 9 0a4.5 4.5 0 1 0-9 0M10.5 10.5 14 14",
"M3 8.5 6.5 12 13 4.5",
"M2.7 8a0.8 0.8 0 1 0 1.6 0a0.8 0.8 0 1 0-1.6 0M7.2 8a0.8 0.8 0 1 0 1.6 0a0.8 0.8 0 1 0-1.6 0M11.7 8a0.8 0.8 0 1 0 1.6 0a0.8 0.8 0 1 0-1.6 0",
];
let off = |d: &str, px: usize| {
let (have, want) = (coverage(&Path::svg(d), px as f32 / 16.0, 1.5, (0, 0), px, px), counted(d, px, 1.5));
let off = have.iter().zip(&want).map(|(have, want)| (*have as f32 / 255.0 - want).abs());
let (worst, all) = off.fold((0.0f32, 0.0), |(worst, all), off| (worst.max(off), all + off));
(worst, all / want.iter().sum::<f32>())
};
for (d, px) in drawings.iter().flat_map(|d| [12usize, 13, 16, 18, 24].map(|px| (*d, px))) {
let (worst, all) = off(d, px);
assert!(worst < 0.085 && all < 0.05, "{px} pixels: the worst pixel is off by {worst:.3}, all by {all:.3} of the drawing, of {d}");
}
for px in [12, 13, 16, 18, 24] {
let (worst, all) = off("M3 4h10M3 6h10M3 9.5h10M3 11h10", px);
assert!(worst < 0.26 && all < 0.07, "{px} pixels: the worst pixel is off by {worst:.3}, all by {all:.3} of the drawing");
}
}
#[test]
fn a_circle_is_round_at_any_size() {
let inside = |[r, inner]: [f32; 2], middle: f32, x: i32, y: i32| {
let points = (0..32 * 32).filter(|at| {
let (px, py) = (x as f32 + (at % 32) as f32 / 32.0 + 1.0 / 64.0 - middle, y as f32 + (at / 32) as f32 / 32.0 + 1.0 / 64.0 - middle);
(inner..=r).contains(&px.hypot(py))
});
points.count() as f32 / 1024.0
};
for (r, width) in [2.0f32, 4.5, 10.0, 40.0, 600.0].into_iter().flat_map(|r| [0.0f32, 1.0, 2.0, 8.0].map(|width| (r, width))) {
let side = (2.0 * r + width + 6.0).ceil() as usize;
let (middle, mut px) = (side as f32 / 2.0, block(side, side));
let mut ring = Path::new();
ring.circle(middle, middle, r);
let mut canvas = Canvas::new(&mut px, side, side, (0, 0), 1.0);
match width {
0.0 => canvas.fill(&ring, pt(0.0, 0.0), 1.0, Color::WHITE),
_ => canvas.stroke(&ring, pt(0.0, 0.0), 1.0, width, Color::WHITE),
}
let edges = if width == 0.0 { [r, -1.0] } else { [r + width / 2.0, r - width / 2.0] };
let (mut worst, mut all, mut count) = (0.0f32, 0.0, 0);
for (x, y) in (0..side as i32).flat_map(|y| (0..side as i32).map(move |x| (x, y))) {
let away = (x as f32 + 0.5 - middle).hypot(y as f32 + 0.5 - middle);
if edges.iter().all(|edge| (away - edge).abs() > 1.5) {
continue;
}
let off = ((px[y as usize * side + x as usize] >> 24) as f32 / 255.0 - inside(edges, middle, x, y)).abs();
(worst, all, count) = (worst.max(off), all + off, count + 1);
}
assert!(worst < 0.04 && all / (count as f32) < 1.5 / 255.0, "radius {r}, line {width}: the worst pixel is off by {worst:.4}, the mean one by {:.4}", all / count as f32);
}
}
#[test]
fn a_round_corner_is_smooth() {
let mut px = block(12, 12);
Canvas::new(&mut px, 12, 12, (0, 0), 1.0).rounded(rect(0.0, 0.0, 12.0, 12.0), 6.0, Color::WHITE);
assert_eq!(px[0], 0, "the very corner is outside");
assert_eq!(px[6 * 12 + 6], 0xffffffff, "the middle is inside");
assert!(px.iter().any(|p| *p != 0 && *p != 0xffffffff), "the edge has in-between pixels");
}
#[test]
fn a_filled_square_shape_covers_its_area() {
let mut px = block(10, 10);
Canvas::new(&mut px, 10, 10, (0, 0), 1.0).fill(&Path::svg("M2 2h6v6h-6z"), pt(0.0, 0.0), 1.0, Color::WHITE);
let lit = px.iter().filter(|p| **p == 0xffffffff).count();
assert_eq!(lit, 36);
assert_eq!(px.iter().filter(|p| **p != 0).count(), 36, "its edges fall on whole pixels, so nothing is half covered");
}
}