use std::collections::HashMap;
use cosmic_text::{FontSystem, LayoutGlyph, SwashCache, SwashContent, SwashImage};
use crabshell_ui::{
Affine, CaptureView, Color, Content, IconView, ImageView, InputView, NodeView, Radii, Rect,
Shadow, Size, Span, Style, TextSystem, TextView, WindowView,
};
use tiny_skia::{
FillRule, FilterQuality, LineCap, LineJoin, Mask, Paint, PathBuilder, PathSegment, Pixmap,
PixmapMut, PixmapPaint, Stroke, StrokeDash, Transform,
};
use crate::damage::{BufferAge, DamageHistory, PixelRect};
use crate::icons::{Drawn, Icons};
use crate::theme::IconLookup;
mod canvas;
mod effects;
mod gradient;
mod transition;
const TILE: u32 = 64;
const GLYPH_CACHE_LIMIT: usize = 2048;
const LAYER_LIMIT: u64 = 1 << 22;
const LAYER_MARGIN: f32 = 2.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PixelFormat {
Argb8888,
Abgr8888,
}
pub struct Canvas<'a> {
pub pixels: &'a mut [u8],
pub width: u32,
pub height: u32,
pub scale: f32,
pub format: PixelFormat,
pub age: &'a mut BufferAge,
}
pub struct Renderer {
glyphs: SwashCache,
icons: Icons,
scratch: Vec<u8>,
work: Work,
}
#[derive(Default)]
#[cfg_attr(not(test), allow(dead_code, reason = "only tests read it"))]
struct Work {
layer_pixels: u64,
nodes: u64,
blurred: u64,
gradient_pixels: std::cell::Cell<u64>,
}
impl Renderer {
pub fn new() -> Renderer {
Renderer {
glyphs: SwashCache::new(),
icons: Icons::new(),
scratch: Vec::new(),
work: Work::default(),
}
}
#[cfg(test)]
pub(crate) fn image_lookups(&self) -> usize {
self.icons.lookups()
}
#[cfg(test)]
pub(crate) fn cached_glyphs(&self) -> usize {
self.glyphs.image_cache.len()
}
#[cfg(test)]
pub(crate) fn work(&self) -> (u64, u64, u64) {
(self.work.layer_pixels, self.work.nodes, self.work.blurred)
}
#[cfg(test)]
pub(crate) fn gradient_pixels(&self) -> u64 {
self.work.gradient_pixels.get()
}
#[cfg(test)]
pub(crate) fn with_icon_dirs(bases: Vec<std::path::PathBuf>) -> Renderer {
Renderer {
icons: Icons::with_themes(crate::theme::Themes::new(bases)),
..Renderer::new()
}
}
pub fn set_icon_theme(&mut self, theme: impl Into<String>) {
self.icons.set_theme(theme.into());
}
pub fn icon_lookup(&self) -> IconLookup {
self.icons.lookup()
}
#[cfg(test)]
pub(crate) fn cached_icons(&self) -> usize {
self.icons.cached()
}
#[cfg(test)]
pub(crate) fn rasterized_icons(&self) -> usize {
self.icons.rasterized()
}
pub fn forget_captures(&mut self, gone: &[u64]) {
self.icons.forget_captures(gone);
}
pub fn decode_off_thread(&mut self) {
self.icons.decode_off_thread();
}
pub fn load_images(&mut self, view: &WindowView<'_>, scale: f32) {
self.icons.load_unseen(&view.loading_images(), scale);
}
pub fn decoding(&self) -> bool {
self.icons.decoding()
}
pub fn wake_when_decoded(&mut self, wake: impl FnOnce() + Send + 'static) {
self.icons.wake_when_decoded(Box::new(wake));
}
pub fn waits_for_pictures(&self) -> bool {
self.icons.waited()
}
pub fn take_decoded(&mut self) -> Vec<u64> {
self.icons.take_decoded()
}
pub fn next_image_frame(&mut self, now: std::time::Instant) -> Option<std::time::Instant> {
self.icons.next_frame(now)
}
pub fn advance_images(&mut self, now: std::time::Instant) -> Vec<u64> {
self.icons.advance(now)
}
#[cfg(test)]
pub(crate) fn decoded_off_thread(&self) -> usize {
self.icons.worker_work().0
}
#[cfg(test)]
pub(crate) fn decoder_running(&self) -> bool {
self.icons.worker_work().1
}
#[cfg(test)]
pub(crate) fn cached_captures(&self) -> usize {
self.icons.captures()
}
pub fn draw(
&mut self,
view: &WindowView<'_>,
damage: &[Rect],
text: &mut TextSystem,
history: &mut DamageHistory,
canvas: Canvas<'_>,
) -> Result<Vec<PixelRect>, CanvasError> {
let Canvas {
pixels,
width,
height,
scale,
format,
age,
} = canvas;
if pixels.len() as u64 != u64::from(width) * u64::from(height) * 4
|| !(scale.is_finite() && scale > 0.0)
{
return Err(CanvasError {
width,
height,
scale,
bytes: pixels.len(),
});
}
self.icons.start_frame(history.id());
let plan = history.plan(damage, scale, width, height, age);
let mut tiles: Vec<(u32, u32)> = Vec::new();
for area in plan.repaint.iter().filter(|area| area.area() > 0) {
for row in area.y / TILE..=(area.y + area.height - 1) / TILE {
tiles
.extend((area.x / TILE..=(area.x + area.width - 1) / TILE).map(|column| (row, column)));
}
}
tiles.sort_unstable();
tiles.dedup();
let mut casts = HashMap::new();
let mut fonts = text.font_system();
for &(row, column) in &tiles {
self.icons.start_tile((row, column));
let (x, y) = (column * TILE, row * TILE);
let size = (width.saturating_sub(x), height.saturating_sub(y));
let area = PixelRect::new(x, y, TILE.min(size.0), TILE.min(size.1));
self.scratch.clear();
self.scratch.resize(area.area() as usize * 4, 0);
let Some(mut pixmap) = PixmapMut::from_bytes(&mut self.scratch, area.width, area.height)
else {
continue;
};
let mut painter = Painter {
grid: Grid {
scale,
zoom: 1.0,
linear: Affine {
sx: 1.0,
ky: 0.0,
kx: 0.0,
sy: 1.0,
tx: 0.0,
ty: 0.0,
},
window: view.size(),
width,
height,
left: area.x as f32,
top: area.y as f32,
},
glyphs: &mut self.glyphs,
icons: &mut self.icons,
fonts: &mut fonts,
work: &mut self.work,
casts: &mut casts,
};
painter.node(&mut pixmap, view.root());
copy(&self.scratch, area, pixels, width, format);
}
self.icons.repainted(&tiles);
self.icons.end_frame();
if self.glyphs.image_cache.len() > GLYPH_CACHE_LIMIT {
self.glyphs.image_cache.clear();
self.glyphs.outline_command_cache.clear();
}
Ok(plan.report)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CanvasError {
pub width: u32,
pub height: u32,
pub scale: f32,
pub bytes: usize,
}
impl std::fmt::Display for CanvasError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"a {}x{} canvas at scale {} needs {} bytes, but has {}",
self.width,
self.height,
self.scale,
u64::from(self.width) * u64::from(self.height) * 4,
self.bytes
)
}
}
impl std::error::Error for CanvasError {}
impl Default for Renderer {
fn default() -> Self {
Self::new()
}
}
fn copy(scratch: &[u8], area: PixelRect, pixels: &mut [u8], width: u32, format: PixelFormat) {
let stride = width as usize * 4;
let row_bytes = area.width as usize * 4;
for (row, source) in scratch.chunks_exact(row_bytes).enumerate() {
let start = (area.y as usize + row) * stride + area.x as usize * 4;
let target = &mut pixels[start..start + row_bytes];
match format {
PixelFormat::Abgr8888 => target.copy_from_slice(source),
PixelFormat::Argb8888 => {
let (to, _) = target.as_chunks_mut::<4>();
let (from, _) = source.as_chunks::<4>();
for (to, from) in to.iter_mut().zip(from) {
let pixel = u32::from_le_bytes(*from);
*to = (pixel & 0xff00_ff00 | (pixel >> 16) & 0xff | (pixel & 0xff) << 16).to_le_bytes();
}
}
}
}
}
#[derive(Clone, Copy)]
struct Grid {
scale: f32,
zoom: f32,
linear: Affine,
window: Size,
width: u32,
height: u32,
left: f32,
top: f32,
}
impl Grid {
fn x(&self, x: f32) -> f32 {
let pixel = if x >= self.window.width {
self.width as f32
} else {
(x * self.scale).round()
};
pixel - self.left
}
fn y(&self, y: f32) -> f32 {
let pixel = if y >= self.window.height {
self.height as f32
} else {
(y * self.scale).round()
};
pixel - self.top
}
fn cover(&self, rect: Rect, width: u32, height: u32) -> Option<(u32, u32, u32, u32)> {
if !(rect.width > 0.0 && rect.height > 0.0) {
return None;
}
let left = ((rect.x * self.scale).floor() - self.left - 1.0).max(0.0) as u32;
let top = ((rect.y * self.scale).floor() - self.top - 1.0).max(0.0) as u32;
let right = (((rect.right() * self.scale).ceil() - self.left + 1.0).max(0.0) as u32).min(width);
let bottom =
(((rect.bottom() * self.scale).ceil() - self.top + 1.0).max(0.0) as u32).min(height);
(left < right && top < bottom).then(|| (left, top, right - left, bottom - top))
}
fn moved(&self, x: u32, y: u32) -> Grid {
Grid {
left: self.left + x as f32,
top: self.top + y as f32,
..*self
}
}
fn rect(&self, rect: Rect) -> Option<tiny_skia::Rect> {
tiny_skia::Rect::from_ltrb(
self.x(rect.x),
self.y(rect.y),
self.x(rect.right()),
self.y(rect.bottom()),
)
.filter(|rect| rect.width() >= 1.0 && rect.height() >= 1.0)
}
}
struct Painter<'a> {
grid: Grid,
glyphs: &'a mut SwashCache,
icons: &'a mut Icons,
fonts: &'a mut FontSystem,
work: &'a mut Work,
casts: &'a mut HashMap<CastKey, effects::Cast>,
}
type CastKey = (String, i64, i64);
impl Painter<'_> {
fn on(&mut self, grid: Grid) -> Painter<'_> {
Painter {
grid,
glyphs: &mut *self.glyphs,
icons: &mut *self.icons,
fonts: &mut *self.fonts,
work: &mut *self.work,
casts: &mut *self.casts,
}
}
fn node(&mut self, pixmap: &mut PixmapMut<'_>, node: NodeView<'_>) {
let Some((x, y, width, height)) =
self
.grid
.cover(node.bounds(), pixmap.width(), pixmap.height())
else {
return;
};
let style = node.style();
let opacity = style.opacity.unwrap_or(1.0);
let opacity = if opacity.is_nan() {
1.0
} else {
opacity.clamp(0.0, 1.0)
};
if opacity == 0.0 {
return;
}
let shadow = effects::cast_by_content(&style);
let region = node
.mask_region()
.filter(|_| node.transform().is_none() && shadow.is_none())
.map(|region| {
let inner = self.on(self.grid.moved(x, y));
inner.region_cover(width, height, node.rect(), region)
});
if matches!(region, Some(effects::Cover::Clear)) {
return;
}
if let Some(transform) = node.transform() {
self.transformed(pixmap, node, &style, transform, opacity);
} else if opacity < 1.0
|| style.mask.is_some()
|| matches!(region, Some(effects::Cover::Part(_)))
|| shadow.is_some()
{
let Some(mut layer) = Pixmap::new(width, height) else {
return;
};
self.work.layer_pixels += u64::from(layer.width()) * u64::from(layer.height());
let mut inner = self.on(self.grid.moved(x, y));
inner.layer_contents(&mut layer.as_mut(), node, &style, shadow, region);
let paint = PixmapPaint {
opacity,
..PixmapPaint::default()
};
let (x, y) = (x as i32, y as i32);
pixmap.draw_pixmap(x, y, layer.as_ref(), &paint, Transform::identity(), None);
} else {
self.node_contents(pixmap, node, &style);
}
}
fn layer_contents(
&mut self,
layer: &mut PixmapMut<'_>,
node: NodeView<'_>,
style: &Style,
shadow: Option<Shadow>,
cover: Option<effects::Cover>,
) {
self.node_contents(layer, node, style);
if let Some(mask) = &style.mask {
self.mask(layer, node.rect(), mask);
}
let (width, height) = (layer.width(), layer.height());
let cover = cover.or_else(|| {
let region = node.mask_region()?;
Some(self.region_cover(width, height, node.rect(), region))
});
match cover {
Some(effects::Cover::Clear) => layer.fill(tiny_skia::Color::TRANSPARENT),
Some(effects::Cover::Part(cut)) => layer.apply_mask(&cut),
Some(effects::Cover::Whole) | None => {}
}
if let Some(shadow) = shadow {
self.content_shadow(layer, node, &shadow);
}
}
fn transformed(
&mut self,
pixmap: &mut PixmapMut<'_>,
node: NodeView<'_>,
style: &Style,
transform: Affine,
opacity: f32,
) {
let Affine {
sx,
ky,
kx,
sy,
tx,
ty,
} = transform;
let Grid {
scale, left, top, ..
} = self.grid;
let outer = self.grid.linear;
let linear = Transform::from_row(outer.sx, outer.ky, outer.kx, outer.sy, 0.0, 0.0)
.pre_concat(Transform::from_row(sx, ky, kx, sy, 0.0, 0.0));
let linear = Affine {
sx: linear.sx,
ky: linear.ky,
kx: linear.kx,
sy: linear.sy,
tx: 0.0,
ty: 0.0,
};
let zoom = linear.layer_zoom();
let layer_scale = scale / self.grid.zoom * zoom;
let device = Transform::from_scale(scale, scale)
.pre_concat(Transform::from_row(sx, ky, kx, sy, tx, ty))
.pre_scale(1.0 / layer_scale, 1.0 / layer_scale);
let to_pixmap = device.post_translate(-left, -top);
let Some(back) = to_pixmap.invert() else {
return;
};
let inner = node.inner_bounds();
let shown = tiny_skia::Rect::from_xywh(0.0, 0.0, pixmap.width() as f32, pixmap.height() as f32)
.and_then(|rect| rect.transform(back));
let drawn = tiny_skia::Rect::from_xywh(
inner.x * layer_scale,
inner.y * layer_scale,
inner.width * layer_scale,
inner.height * layer_scale,
);
let Some(area) = shown
.zip(drawn)
.and_then(|(shown, drawn)| shown.intersect(&drawn))
.and_then(|area| area.outset(LAYER_MARGIN, LAYER_MARGIN))
.and_then(|area| area.round_out())
else {
return;
};
let pixels = u64::from(area.width()) * u64::from(area.height());
if pixels > LAYER_LIMIT {
tracing::debug!(
"{} is transformed into a layer too large to draw",
node.path()
);
return;
}
let Some(mut layer) = Pixmap::new(area.width(), area.height()) else {
return;
};
self.work.layer_pixels += pixels;
let (x, y) = (area.x() as f32, area.y() as f32);
let (width, height) = if zoom == self.grid.zoom {
(self.grid.width, self.grid.height)
} else {
let window = self.grid.window;
(
(window.width * layer_scale).round() as u32,
(window.height * layer_scale).round() as u32,
)
};
let mut inner = self.on(Grid {
scale: layer_scale,
zoom,
linear,
width,
height,
left: x,
top: y,
..self.grid
});
let shadow = effects::cast_by_content(style);
inner.layer_contents(&mut layer.as_mut(), node, style, shadow, None);
let paint = PixmapPaint {
opacity,
quality: FilterQuality::Bilinear,
..PixmapPaint::default()
};
pixmap.draw_pixmap(
0,
0,
layer.as_ref(),
&paint,
to_pixmap.pre_translate(x, y),
None,
);
}
fn node_contents(&mut self, pixmap: &mut PixmapMut<'_>, node: NodeView<'_>, style: &Style) {
self.work.nodes += 1;
let outside = |shadow: &Shadow| !shadow.follow_content && !shadow.inset;
if let Some(shadow) = style.shadow.filter(outside) {
self.shadow(pixmap, node.rect(), style, &shadow);
}
self.draw_box(pixmap, node.rect(), style);
match node.content() {
Content::Text(text) => {
let color = text.color();
let tint = Color::rgba(color.r, color.g, color.b, color.a / 3);
self.selection(pixmap, &text.selection(), tint);
self.text(pixmap, &text);
}
Content::Icon(icon) => self.icon(pixmap, &icon),
Content::Image(image) => self.image(pixmap, &image),
Content::Capture(capture) => self.capture(pixmap, &capture),
Content::Input(input) => self.input(pixmap, &input),
Content::Canvas(canvas) => self.canvas(pixmap, &canvas, style),
Content::None => {}
}
if node.clips() {
self.clipped_children(pixmap, node, style);
} else {
for child in node.children() {
self.node(pixmap, child);
}
}
}
fn clipped_children(&mut self, pixmap: &mut PixmapMut<'_>, node: NodeView<'_>, style: &Style) {
let Some((area, corners)) = self.box_shape(node.rect(), style) else {
return;
};
let width = self.border_width(style);
let Some(inside) = tiny_skia::Rect::from_ltrb(
area.left() + width,
area.top() + width,
area.right() - width,
area.bottom() - width,
) else {
return;
};
let corners = inset(corners, width);
self.through_mask(pixmap, node.rect(), (inside, corners), |painter, layer| {
for child in node.children() {
painter.node(layer, child);
}
for (handle, bar) in node.scroll_bars() {
painter.draw_box(layer, handle, &bar);
}
});
}
fn through_mask(
&mut self,
pixmap: &mut PixmapMut<'_>,
cover: Rect,
(shape, corners): (tiny_skia::Rect, Corners),
draw: impl FnOnce(&mut Painter<'_>, &mut PixmapMut<'_>),
) {
if !visible(pixmap, shape) {
return;
}
let (Some(mut mask), Some((x, y, layer_width, layer_height))) = (
Mask::new(pixmap.width(), pixmap.height()),
self.grid.cover(cover, pixmap.width(), pixmap.height()),
) else {
return;
};
let Some(mut layer) = Pixmap::new(layer_width, layer_height) else {
return;
};
self.work.layer_pixels += u64::from(layer_width) * u64::from(layer_height);
let mut inner = self.on(self.grid.moved(x, y));
draw(&mut inner, &mut layer.as_mut());
let mut path = PathBuilder::new();
rounded_rect(&mut path, shape, corners);
let Some(path) = path.finish() else {
return;
};
mask.fill_path(&path, FillRule::Winding, true, Transform::identity());
pixmap.draw_pixmap(
x as i32,
y as i32,
layer.as_ref(),
&PixmapPaint::default(),
Transform::identity(),
Some(&mask),
);
}
fn box_shape(&self, rect: Rect, style: &Style) -> Option<(tiny_skia::Rect, Corners)> {
let area = self.grid.rect(rect)?;
let corners = style.radius.unwrap_or_default().corners();
let corners = fit(corners.map(|radius| radius * self.grid.scale), area);
Some((area, corners))
}
fn border_width(&self, style: &Style) -> f32 {
let Some(border) = style.border else {
return 0.0;
};
let width = (border.width * self.grid.scale).floor();
if border.width > 0.0 {
width.max(1.0)
} else {
width.max(0.0)
}
}
fn draw_box(&self, pixmap: &mut PixmapMut<'_>, rect: Rect, style: &Style) {
let inset_shadow = style.shadow.filter(|shadow| shadow.inset);
if style.background.is_none()
&& style.gradient.is_none()
&& style.border.is_none()
&& inset_shadow.is_none()
{
return;
}
let Some((area, corners)) = self.box_shape(rect, style) else {
return;
};
if !visible(pixmap, area) {
return;
}
if let Some(color) = style.background
&& !(corners == [0.0; 4] && fill_opaque(pixmap, area, color))
{
let mut path = PathBuilder::new();
rounded_rect(&mut path, area, corners);
fill(pixmap, path, color, FillRule::Winding);
}
if let Some(gradient) = style.gradient {
let mut path = PathBuilder::new();
rounded_rect(&mut path, area, corners);
let size = (pixmap.width(), pixmap.height());
if let (Some(path), Some(ramp)) = (path.finish(), self.ramp(&gradient, area, area, size)) {
ramp.fill(pixmap, &path);
}
}
if let Some(shadow) = inset_shadow {
self.inset_shadow(pixmap, (area, corners), self.border_width(style), &shadow);
}
if let Some(border) = style.border {
let width = self.border_width(style);
let dashed = width >= 1.0
&& style.border_dash.is_some_and(|dash| {
self.dashed_border(pixmap, (area, corners), width, border.color, dash)
});
if width >= 1.0 && !dashed {
let mut path = PathBuilder::new();
rounded_rect(&mut path, area, corners);
if let Some(inner) = tiny_skia::Rect::from_ltrb(
area.left() + width,
area.top() + width,
area.right() - width,
area.bottom() - width,
) {
rounded_rect(&mut path, inner, inset(corners, width));
}
fill(pixmap, path, border.color, FillRule::EvenOdd);
}
}
}
fn dashed_border(
&self,
pixmap: &mut PixmapMut<'_>,
(area, corners): (tiny_skia::Rect, Corners),
width: f32,
color: Color,
(on, off): (f32, f32),
) -> bool {
let Grid {
scale, left, top, ..
} = self.grid;
let half = width / 2.0;
let Some(middle) = tiny_skia::Rect::from_ltrb(
area.left() + left + half,
area.top() + top + half,
area.right() + left - half,
area.bottom() + top - half,
) else {
return false;
};
let corners = inset(corners, half);
let (on, off) = (on * scale, off * scale);
let mut all = PathBuilder::new();
let square: Vec<usize> = (0..4).filter(|&index| corners[index] <= 0.0).collect();
if square.is_empty() {
let rounding: f32 = corners
.iter()
.map(|radius| (2.0 - std::f32::consts::FRAC_PI_2) * radius)
.sum();
let perimeter = 2.0 * (middle.width() + middle.height()) - rounding;
let mut path = PathBuilder::new();
rounded_rect(&mut path, middle, corners);
if !dash_into(&mut all, path, perimeter, on, off) {
return false;
}
} else {
for (at, &from) in square.iter().enumerate() {
let to = square[(at + 1) % square.len()];
let steps = (to + 4 - from - 1) % 4 + 1;
let [.., mut was] = corner(middle, corners, from);
let mut path = PathBuilder::new();
path.move_to(was.x, was.y);
let mut length = 0.0;
for index in (from + 1..=from + steps).map(|index| index % 4) {
let [start, one, two, end] = corner(middle, corners, index);
path.line_to(start.x, start.y);
length += was.distance(start);
if corners[index] > 0.0 {
path.cubic_to(one.x, one.y, two.x, two.y, end.x, end.y);
length += std::f32::consts::FRAC_PI_2 * corners[index];
}
was = end;
}
if !dash_into(&mut all, path, length, on, off) {
return false;
}
}
if on > 0.0 {
const IN: [(f32, f32); 4] = [(0.0, -1.0), (1.0, 0.0), (0.0, 1.0), (-1.0, 0.0)];
for &index in &square {
let [point, ..] = corner(middle, corners, index);
let ((ax, ay), (bx, by)) = (IN[index], IN[(index + 1) % 4]);
all.move_to(point.x - half * ax, point.y - half * ay);
all.line_to(point.x, point.y);
all.line_to(point.x + half * bx, point.y + half * by);
}
}
}
let Some(dashes) = all.finish() else {
return true;
};
let reach = 2.0 * width + 1.0;
let near = [
left - reach,
top - reach,
left + pixmap.width() as f32 + reach,
top + pixmap.height() as f32 + reach,
];
let mut kept = PathBuilder::new();
let mut contour = Vec::new();
for segment in dashes.segments() {
if matches!(segment, PathSegment::MoveTo(_)) {
keep_near(&mut kept, &contour, near);
contour.clear();
}
contour.push(segment);
}
keep_near(&mut kept, &contour, near);
let Some(kept) = kept.finish() else {
return true;
};
let stroke = Stroke {
width,
line_cap: if on == 0.0 {
LineCap::Round
} else {
LineCap::Butt
},
line_join: LineJoin::Miter,
..Stroke::default()
};
let mut paint = Paint::default();
paint.set_color_rgba8(color.r, color.g, color.b, color.a);
paint.anti_alias = true;
let moved = Transform::from_translate(-left, -top);
pixmap.stroke_path(&kept, &paint, &stroke, moved, None);
true
}
fn shadow(&self, pixmap: &mut PixmapMut<'_>, rect: Rect, style: &Style, shadow: &Shadow) {
let Grid {
scale, left, top, ..
} = self.grid;
let numbers = [shadow.x, shadow.y, shadow.blur, shadow.spread];
if shadow.color.a == 0 || !numbers.iter().all(|n| n.is_finite()) {
return;
}
let Some((area, corners)) = self.box_shape(rect, style) else {
return;
};
let spread = shadow.spread * scale;
let Some(shape) = tiny_skia::Rect::from_ltrb(
area.left() + left + shadow.x * scale - spread,
area.top() + top + shadow.y * scale - spread,
area.right() + left + shadow.x * scale + spread,
area.bottom() + top + shadow.y * scale + spread,
) else {
return;
};
let corners = fit(spread_corners(corners, spread), shape);
let cut = if shadow.cut_out {
self.box_mask(pixmap, rect, style)
} else {
None
};
let sigma = shadow.blur.max(0.0) * scale / 2.0;
if sigma < 0.25 {
let mut path = PathBuilder::new();
let Some(shape) = shape.transform(Transform::from_translate(-left, -top)) else {
return;
};
rounded_rect(&mut path, shape, corners);
let Some(path) = path.finish() else {
return;
};
let mut paint = Paint::default();
let color = shadow.color;
paint.set_color_rgba8(color.r, color.g, color.b, color.a);
paint.anti_alias = true;
let outside = cut.map(|mut cut| {
cut.invert();
cut
});
let (rule, at) = (FillRule::Winding, Transform::identity());
pixmap.fill_path(&path, &paint, rule, at, outside.as_ref());
return;
}
let reach = 3.0 * sigma;
let columns = (shape.left() - reach - left).floor().max(0.0) as u32
..((shape.right() + reach - left).ceil().max(0.0) as u32).min(pixmap.width());
let rows = (shape.top() - reach - top).floor().max(0.0) as u32
..((shape.bottom() + reach - top).ceil().max(0.0) as u32).min(pixmap.height());
if columns.is_empty() || rows.is_empty() {
return;
}
let blurred = BlurredBox::new(shape, corners, sigma);
let mut mask = Vec::with_capacity(columns.len() * rows.len());
for row in rows.clone() {
let xs = columns.clone().map(|column| left + column as f32 + 0.5);
blurred.row(top + row as f32 + 0.5, xs, &mut mask);
}
if let Some(cut) = cut {
let stride = pixmap.width() as usize;
let under = rows.clone().flat_map(|row| {
columns
.clone()
.map(move |column| row as usize * stride + column as usize)
});
for (alpha, at) in mask.iter_mut().zip(under) {
*alpha = mul(*alpha, 255 - cut.data()[at]);
}
}
let width = columns.len() as u32;
let height = rows.len() as u32;
let (x, y) = (columns.start as i32, rows.start as i32);
blend_mask(pixmap, x, y, width, height, &mask, shadow.color);
}
fn inset_shadow(
&self,
pixmap: &mut PixmapMut<'_>,
(area, corners): (tiny_skia::Rect, Corners),
border: f32,
shadow: &Shadow,
) {
let Grid {
scale, left, top, ..
} = self.grid;
let numbers = [shadow.x, shadow.y, shadow.blur, shadow.spread];
if shadow.color.a == 0 || !numbers.iter().all(|n| n.is_finite()) {
return;
}
let Some(inside) = tiny_skia::Rect::from_ltrb(
area.left() + border,
area.top() + border,
area.right() - border,
area.bottom() - border,
) else {
return;
};
let inside_corners = inset(corners, border);
if !visible(pixmap, inside) {
return;
}
let mut path = PathBuilder::new();
rounded_rect(&mut path, inside, inside_corners);
let Some(path) = path.finish() else {
return;
};
let (x, y, spread) = (shadow.x * scale, shadow.y * scale, shadow.spread * scale);
let hole = tiny_skia::Rect::from_ltrb(
inside.left() + left + x + spread,
inside.top() + top + y + spread,
inside.right() + left + x - spread,
inside.bottom() + top + y - spread,
);
let hole = hole.map(|hole| (hole, fit(spread_corners(inside_corners, -spread), hole)));
let sigma = shadow.blur.max(0.0) * scale / 2.0;
let color = shadow.color;
if sigma < 0.25 {
let mut outside = None;
if let Some((hole, corners)) = hole {
let mut cut = PathBuilder::new();
let Some(moved) = hole.transform(Transform::from_translate(-left, -top)) else {
return;
};
rounded_rect(&mut cut, moved, corners);
let Some(mut mask) = Mask::new(pixmap.width(), pixmap.height()) else {
return;
};
let (rule, at) = (FillRule::Winding, Transform::identity());
let Some(cut) = cut.finish() else {
return;
};
mask.fill_path(&cut, rule, true, at);
mask.invert();
outside = Some(mask);
}
let mut paint = Paint::default();
paint.set_color_rgba8(color.r, color.g, color.b, color.a);
paint.anti_alias = true;
let (rule, at) = (FillRule::Winding, Transform::identity());
pixmap.fill_path(&path, &paint, rule, at, outside.as_ref());
return;
}
let Some(mut mask) = Mask::new(pixmap.width(), pixmap.height()) else {
return;
};
mask.fill_path(&path, FillRule::Winding, true, Transform::identity());
let columns = inside.left().floor().max(0.0) as u32
..(inside.right().ceil().max(0.0) as u32).min(pixmap.width());
let rows = inside.top().floor().max(0.0) as u32
..(inside.bottom().ceil().max(0.0) as u32).min(pixmap.height());
if columns.is_empty() || rows.is_empty() {
return;
}
let blurred = hole.map(|(hole, corners)| BlurredBox::new(hole, corners, sigma));
let mut alpha = Vec::with_capacity(columns.len() * rows.len());
for row in rows.clone() {
let start = alpha.len();
match &blurred {
Some(blurred) => {
let xs = columns.clone().map(|column| left + column as f32 + 0.5);
blurred.row(top + row as f32 + 0.5, xs, &mut alpha);
}
None => alpha.resize(start + columns.len(), 0),
}
let stride = pixmap.width() as usize;
let row = &mask.data()[row as usize * stride..];
let covered = &row[columns.start as usize..columns.end as usize];
for (value, &covered) in alpha[start..].iter_mut().zip(covered) {
*value = mul(255 - *value, covered);
}
}
let (width, height) = (columns.len() as u32, rows.len() as u32);
let (x, y) = (columns.start as i32, rows.start as i32);
blend_mask(pixmap, x, y, width, height, &alpha, color);
}
fn box_mask(&self, pixmap: &PixmapMut<'_>, rect: Rect, style: &Style) -> Option<Mask> {
let (area, corners) = self.box_shape(rect, style)?;
let mut path = PathBuilder::new();
rounded_rect(&mut path, area, corners);
let mut mask = Mask::new(pixmap.width(), pixmap.height())?;
let (rule, at) = (FillRule::Winding, Transform::identity());
mask.fill_path(&path.finish()?, rule, true, at);
Some(mask)
}
fn text(&mut self, pixmap: &mut PixmapMut<'_>, text: &TextView<'_>) {
let Grid {
scale, left, top, ..
} = self.grid;
let origin = text.origin();
let x = origin.x * scale - left;
for run in text.runs() {
let baseline = ((origin.y + run.line_y) * scale).round() - top;
if baseline + run.line_height * scale < 0.0
|| baseline - run.line_height * scale > pixmap.height() as f32
{
continue;
}
let span_color = |span: &Span, glyph: &LayoutGlyph| match text.glyph_lit(run.line_i, glyph) {
true => text.link_hover_color().or(span.color),
false => span.color,
};
for glyph in run.glyphs.iter() {
let span = text.span(run.line_i, glyph);
let color = text.selected_color(run.line_i, glyph);
let color = color.or_else(|| span.and_then(|span| span_color(span, glyph)));
let color = color.unwrap_or(text.color());
if color.a == 0 {
continue;
}
let physical = glyph.physical((x, baseline), scale);
let Some(image) = self.glyphs.get_image(self.fonts, physical.cache_key) else {
continue;
};
let left = physical.x + image.placement.left;
let top = physical.y - image.placement.top;
let (width, height) = (image.placement.width, image.placement.height);
match image.content {
SwashContent::Mask => {
blend_mask(pixmap, left, top, width, height, &image.data, color);
}
SwashContent::Color => {
let premultiplied = is_color_outline(image);
let rgba = (image.data.as_slice(), premultiplied);
blend_rgba(pixmap, left, top, width, height, rgba, color.a);
}
SwashContent::SubpixelMask => {}
}
}
let origin = text.origin().x;
let own = (text.underline(), text.strikethrough());
if own != (false, false) {
self.decorations(pixmap, origin, &run.glyphs, baseline, own, text.color());
}
let same_span = |a: &LayoutGlyph, b: &LayoutGlyph| match (
text.span(run.line_i, a),
text.span(run.line_i, b),
) {
(Some(a), Some(b)) => std::ptr::eq(a, b),
(a, b) => a.is_none() && b.is_none(),
};
for glyphs in run.glyphs.chunk_by(same_span) {
let Some(span) = text.span(run.line_i, &glyphs[0]) else {
continue;
};
let lit = text.glyph_lit(run.line_i, &glyphs[0]);
let lines = (span.underline || lit, span.strikethrough);
if lines != (false, false) {
let color = span_color(span, &glyphs[0]).unwrap_or(text.color());
self.decorations(pixmap, origin, glyphs, baseline, lines, color);
}
}
}
}
fn decorations(
&mut self,
pixmap: &mut PixmapMut<'_>,
origin: f32,
glyphs: &[LayoutGlyph],
baseline: f32,
lines: (bool, bool),
color: Color,
) {
let same_font = |a: &LayoutGlyph, b: &LayoutGlyph| {
(a.font_id, a.font_weight, a.font_size) == (b.font_id, b.font_weight, b.font_size)
};
for glyphs in glyphs.chunk_by(same_font) {
self.font_decorations(pixmap, origin, glyphs, baseline, lines, color);
}
}
fn font_decorations(
&mut self,
pixmap: &mut PixmapMut<'_>,
origin: f32,
glyphs: &[LayoutGlyph],
baseline: f32,
(underline, strikethrough): (bool, bool),
color: Color,
) {
let Some(first) = glyphs.first() else {
return;
};
let Some(font) = self.fonts.get_font(first.font_id, first.font_weight) else {
return;
};
let metrics = font.metrics();
let em = f32::from(metrics.units_per_em);
if em == 0.0 {
return;
}
let Grid { scale, left, .. } = self.grid;
let (start, end) = glyphs
.iter()
.fold((f32::MAX, f32::MIN), |(start, end), glyph| {
(start.min(glyph.x), end.max(glyph.x + glyph.w))
});
let x0 = ((origin + start) * scale).round() - left;
let x1 = ((origin + end) * scale).round() - left;
let size = first.font_size * scale;
let lines = [
(underline, metrics.underline, -0.125),
(strikethrough, metrics.strikeout, 0.3),
];
for (on, decoration, offset) in lines {
if !on {
continue;
}
let (offset, thickness) =
decoration.map_or((offset, 1.0 / 14.0), |d| (d.offset / em, d.thickness / em));
let thickness = (thickness.clamp(0.0, 0.25) * size).round().max(1.0);
let y = baseline - (offset.clamp(-0.25, 0.5) * size).round();
if let Some(area) = tiny_skia::Rect::from_ltrb(x0, y, x1, y + thickness) {
let mut path = PathBuilder::new();
path.push_rect(area);
fill(pixmap, path, color, FillRule::Winding);
}
}
}
fn input(&mut self, pixmap: &mut PixmapMut<'_>, input: &InputView<'_>) {
let Some(area) = self.grid.rect(input.area) else {
return;
};
self.through_mask(pixmap, input.area, (area, [0.0; 4]), |painter, layer| {
painter.input_contents(layer, input);
});
}
fn input_contents(&mut self, pixmap: &mut PixmapMut<'_>, input: &InputView<'_>) {
let color = input.text.color();
self.selection(pixmap, input.selection, input.selection_color);
self.text(pixmap, &input.text);
for line in input.underline {
if let Some(area) = self.grid.rect(*line) {
let mut path = PathBuilder::new();
path.push_rect(area);
fill(pixmap, path, color, FillRule::Winding);
}
}
if let Some(caret) = input.caret {
let left = self.grid.x(caret.x);
let right = self.grid.x(caret.right()).max(left + 1.0);
if let Some(area) = tiny_skia::Rect::from_ltrb(
left,
self.grid.y(caret.y),
right,
self.grid.y(caret.bottom()),
) {
let mut path = PathBuilder::new();
path.push_rect(area);
fill(pixmap, path, color, FillRule::Winding);
}
}
}
fn selection(&mut self, pixmap: &mut PixmapMut<'_>, spans: &[Rect], tint: Color) {
for span in spans {
if let Some(area) = self.grid.rect(*span) {
let mut path = PathBuilder::new();
path.push_rect(area);
fill(pixmap, path, tint, FillRule::Winding);
}
}
}
fn image(&mut self, pixmap: &mut PixmapMut<'_>, image: &ImageView<'_>) {
let Some((x, y, width, height)) = self.picture_area(pixmap, image.rect) else {
return;
};
match self.icons.image(image, (width, height), self.grid.scale) {
Some(Drawn::One(pixels)) if (pixels.width(), pixels.height()) == (width, height) => {
transition::blit(pixmap, (x, y), pixels);
}
Some(Drawn::One(pixels)) => {
let paint = PixmapPaint {
quality: FilterQuality::Bilinear,
..PixmapPaint::default()
};
let stretch = Transform::from_scale(
width as f32 / pixels.width() as f32,
height as f32 / pixels.height() as f32,
)
.post_translate(x as f32, y as f32);
pixmap.draw_pixmap(0, 0, pixels.as_ref(), &paint, stretch, None);
}
Some(Drawn::Two { from, to, t }) => {
transition::draw(pixmap, (x, y), from, to, t, image.transition);
}
None => {}
}
}
fn capture(&mut self, pixmap: &mut PixmapMut<'_>, capture: &CaptureView<'_>) {
let Some((x, y, width, height)) = self.picture_area(pixmap, capture.rect) else {
return;
};
let frame = capture.frame;
let Some([left, top, part_width, part_height]) = frame.drawn_part(capture.crop) else {
return;
};
let crop = capture.crop.and_then(|_| {
let sides = [left, top, part_width, part_height].map(|side| side as i32);
tiny_skia::IntRect::from_xywh(sides[0], sides[1], part_width, part_height)
});
let size = (width, height);
if let Some(pixels) = self
.icons
.capture(capture.id, frame, size, capture.fit, crop)
{
draw_at(pixmap, x, y, pixels);
}
}
fn picture_area(&self, pixmap: &PixmapMut<'_>, rect: Rect) -> Option<(i32, i32, u32, u32)> {
let Grid {
scale, left, top, ..
} = self.grid;
let (width, height) = ((rect.width * scale).round(), (rect.height * scale).round());
let (x, y) = (
(rect.x * scale).round() - left,
(rect.y * scale).round() - top,
);
if !(width >= 1.0 && height >= 1.0) {
return None;
}
let area = tiny_skia::Rect::from_xywh(x, y, width, height)?;
visible(pixmap, area).then_some((x as i32, y as i32, width as u32, height as u32))
}
fn icon(&mut self, pixmap: &mut PixmapMut<'_>, icon: &IconView<'_>) {
let Grid {
scale, left, top, ..
} = self.grid;
let side = (icon.rect.width * scale).round();
if side.is_nan() || side < 1.0 {
return;
}
let x = (icon.rect.x * scale).round() - left;
let y = (icon.rect.y * scale).round() - top;
let size = icon.rect.width.round() as u32;
let theme_scale = scale.ceil().max(1.0) as u32;
let sizes = (side as u32, size, theme_scale);
let source = match icon.fallback {
Some(fallback) if self.icons.get(icon.source, sizes, icon.tint).is_none() => fallback,
_ => icon.source,
};
if let Some(image) = self.icons.get(source, sizes, icon.tint) {
pixmap.draw_pixmap(
x as i32,
y as i32,
image.as_ref(),
&PixmapPaint::default(),
Transform::identity(),
None,
);
}
}
}
fn draw_at(pixmap: &mut PixmapMut<'_>, x: i32, y: i32, pixels: &Pixmap) {
let paint = PixmapPaint::default();
pixmap.draw_pixmap(x, y, pixels.as_ref(), &paint, Transform::identity(), None);
}
fn visible(pixmap: &PixmapMut<'_>, area: tiny_skia::Rect) -> bool {
area.right() > 0.0
&& area.bottom() > 0.0
&& area.left() < pixmap.width() as f32
&& area.top() < pixmap.height() as f32
}
fn fill(pixmap: &mut PixmapMut<'_>, path: PathBuilder, color: Color, rule: FillRule) {
let Some(path) = path.finish() else {
return;
};
let mut paint = Paint::default();
paint.set_color_rgba8(color.r, color.g, color.b, color.a);
paint.anti_alias = true;
pixmap.fill_path(&path, &paint, rule, Transform::identity(), None);
}
type Corners = [f32; 4];
fn dash_into(all: &mut PathBuilder, path: PathBuilder, length: f32, on: f32, off: f32) -> bool {
let Some((on, off)) = fit_dash(length, on, off) else {
return false;
};
let (Some(path), Some(dash)) = (path.finish(), StrokeDash::new(vec![on, off], on / 2.0)) else {
return false;
};
if let Some(dashes) = path.dash(&dash, 1.0) {
all.push_path(&dashes);
}
true
}
fn corner(rect: tiny_skia::Rect, corners: Corners, index: usize) -> [tiny_skia::Point; 4] {
const KAPPA: f32 = 0.552_284_8;
let (l, t, r, b) = (rect.left(), rect.top(), rect.right(), rect.bottom());
let radius = corners[index];
let k = radius * (1.0 - KAPPA);
let points = match index {
0 => [(l, t + radius), (l, t + k), (l + k, t), (l + radius, t)],
1 => [(r - radius, t), (r - k, t), (r, t + k), (r, t + radius)],
2 => [(r, b - radius), (r, b - k), (r - k, b), (r - radius, b)],
_ => [(l + radius, b), (l + k, b), (l, b - k), (l, b - radius)],
};
points.map(|(x, y)| tiny_skia::Point::from_xy(x, y))
}
fn spread_corners(corners: Corners, spread: f32) -> Corners {
corners.map(|radius| {
if spread > 0.0 && radius < spread {
radius + spread * (1.0 + (radius / spread - 1.0).powi(3))
} else {
(radius + spread).max(0.0)
}
})
}
fn fit(
[top_left, top_right, bottom_right, bottom_left]: Corners,
area: tiny_skia::Rect,
) -> Corners {
let radii = Radii {
top_left,
top_right,
bottom_right,
bottom_left,
};
radii.fit(area.width(), area.height()).corners()
}
fn inset(corners: Corners, by: f32) -> Corners {
corners.map(|radius| (radius - by).max(0.0))
}
fn rounded_rect(path: &mut PathBuilder, rect: tiny_skia::Rect, corners: Corners) {
if corners.iter().all(|&radius| radius <= 0.0) {
path.push_rect(rect);
return;
}
let [.., start] = corner(rect, corners, 0);
path.move_to(start.x, start.y);
for index in [1, 2, 3, 0] {
let [start, one, two, end] = corner(rect, corners, index);
path.line_to(start.x, start.y);
if corners[index] > 0.0 {
path.cubic_to(one.x, one.y, two.x, two.y, end.x, end.y);
}
}
path.close();
}
fn mul(a: u8, b: u8) -> u8 {
((u16::from(a) * u16::from(b) + 127) / 255) as u8
}
fn over(data: &mut [u8], index: usize, source: [u8; 4]) {
let rest = 255 - source[3];
for (channel, value) in data[index..index + 4].iter_mut().zip(source) {
*channel = value.saturating_add(mul(*channel, rest));
}
}
fn overlap(
pixmap: &PixmapMut<'_>,
left: i32,
top: i32,
width: u32,
height: u32,
) -> Option<(usize, usize, usize, usize)> {
let column_start = (-i64::from(left)).max(0);
let row_start = (-i64::from(top)).max(0);
let column_end = i64::from(width).min(i64::from(pixmap.width()) - i64::from(left));
let row_end = i64::from(height).min(i64::from(pixmap.height()) - i64::from(top));
(column_start < column_end && row_start < row_end).then_some((
column_start as usize,
column_end as usize,
row_start as usize,
row_end as usize,
))
}
fn blend_mask(
pixmap: &mut PixmapMut<'_>,
left: i32,
top: i32,
width: u32,
height: u32,
mask: &[u8],
color: Color,
) {
if mask.len() < width as usize * height as usize {
return;
}
let Some((columns, columns_end, rows, rows_end)) = overlap(pixmap, left, top, width, height)
else {
return;
};
let stride = pixmap.width() as usize;
let data = pixmap.data_mut();
for row in rows..rows_end {
for column in columns..columns_end {
let coverage = mask[row * width as usize + column];
if coverage == 0 {
continue;
}
let alpha = mul(color.a, coverage);
let source = [
mul(color.r, alpha),
mul(color.g, alpha),
mul(color.b, alpha),
alpha,
];
let x = (i64::from(left) + column as i64) as usize;
let y = (i64::from(top) + row as i64) as usize;
over(data, (y * stride + x) * 4, source);
}
}
}
const SAMPLES: usize = 8;
struct BlurredBox {
middle: (f32, f32),
half: (f32, f32),
corners: Corners,
largest: f32,
sigma: f32,
whole: f32,
}
impl BlurredBox {
fn new(shape: tiny_skia::Rect, corners: Corners, sigma: f32) -> BlurredBox {
let step = 6.0 * sigma / SAMPLES as f32;
let whole = (0..SAMPLES)
.map(|sample| gaussian(step * (sample as f32 + 0.5) - 3.0 * sigma, sigma) * step)
.sum();
BlurredBox {
whole,
middle: (
(shape.left() + shape.right()) / 2.0,
(shape.top() + shape.bottom()) / 2.0,
),
half: (shape.width() / 2.0, shape.height() / 2.0),
corners,
largest: corners.into_iter().fold(0.0, f32::max),
sigma,
}
}
fn row(&self, y: f32, xs: impl Iterator<Item = f32>, mask: &mut Vec<u8>) {
let y = y - self.middle.1;
let (half_width, half_height) = self.half;
let reach = 3.0 * self.sigma;
let down = self.along(y, half_height);
let clear_of_corners = y.abs() <= half_height - self.largest - reach;
let rows = if clear_of_corners {
[(0.0, 0.0, 0.0); SAMPLES]
} else {
self.rows_near(y)
};
for x in xs {
let x = x - self.middle.0;
let coverage = if clear_of_corners || x.abs() <= half_width - self.largest - reach {
self.along(x, half_width) * down
} else {
let sum: f32 = rows
.iter()
.map(|&(left, right, weight)| self.between(x, left, right) * weight)
.sum();
(sum / self.whole).clamp(0.0, 1.0)
};
mask.push((coverage * 255.0).round() as u8);
}
}
fn along(&self, p: f32, half: f32) -> f32 {
self.between(p, half, half)
}
fn between(&self, p: f32, left: f32, right: f32) -> f32 {
let reach = 3.0 * self.sigma;
if -(left - reach) <= p && p <= right - reach {
return 1.0;
}
let scale = std::f32::consts::FRAC_1_SQRT_2 / self.sigma;
0.5 * (erf((p + left) * scale) - erf((p - right) * scale))
}
fn rows_near(&self, y: f32) -> [(f32, f32, f32); SAMPLES] {
let (half_width, half_height) = self.half;
let reach = 3.0 * self.sigma;
let start = (y - half_height).clamp(-reach, reach);
let end = (y + half_height).clamp(-reach, reach);
let step = (end - start) / SAMPLES as f32;
std::array::from_fn(|sample| {
let offset = start + step * (sample as f32 + 0.5);
let row = y - offset;
let [top_left, top_right, bottom_right, bottom_left] = self.corners;
let (left, right) = if row < 0.0 {
(top_left, top_right)
} else {
(bottom_left, bottom_right)
};
let span = |radius: f32| {
let into_corner = (row.abs() - (half_height - radius)).max(0.0);
half_width - radius
+ (radius * radius - into_corner * into_corner)
.max(0.0)
.sqrt()
};
(span(left), span(right), gaussian(offset, self.sigma) * step)
})
}
}
fn gaussian(x: f32, sigma: f32) -> f32 {
let root_two_pi = (2.0 * std::f32::consts::PI).sqrt();
(-(x * x) / (2.0 * sigma * sigma)).exp() / (root_two_pi * sigma)
}
fn erf(x: f32) -> f32 {
let a = x.abs();
let d = 1.0 + a * (0.278_393 + a * (0.230_389 + a * (0.000_972 + a * 0.078_108)));
let d = d * d;
(1.0 - 1.0 / (d * d)).copysign(x)
}
fn is_color_outline(image: &SwashImage) -> bool {
format!("{:?}", image.source).starts_with("ColorOutline")
}
fn blend_rgba(
pixmap: &mut PixmapMut<'_>,
left: i32,
top: i32,
width: u32,
height: u32,
(rgba, premultiplied): (&[u8], bool),
opacity: u8,
) {
if rgba.len() < width as usize * height as usize * 4 {
return;
}
let Some((columns, columns_end, rows, rows_end)) = overlap(pixmap, left, top, width, height)
else {
return;
};
let stride = pixmap.width() as usize;
let data = pixmap.data_mut();
for row in rows..rows_end {
for column in columns..columns_end {
let index = (row * width as usize + column) * 4;
let alpha = mul(rgba[index + 3], opacity);
if alpha == 0 {
continue;
}
let factor = if premultiplied { opacity } else { alpha };
let source = [
mul(rgba[index], factor),
mul(rgba[index + 1], factor),
mul(rgba[index + 2], factor),
alpha,
];
let x = (i64::from(left) + column as i64) as usize;
let y = (i64::from(top) + row as i64) as usize;
over(data, (y * stride + x) * 4, source);
}
}
}
fn keep_near(
kept: &mut PathBuilder,
contour: &[PathSegment],
[left, top, right, bottom]: [f32; 4],
) {
let (mut low, mut high) = (
(f32::INFINITY, f32::INFINITY),
(f32::NEG_INFINITY, f32::NEG_INFINITY),
);
let points = contour.iter().flat_map(|segment| match *segment {
PathSegment::MoveTo(a) | PathSegment::LineTo(a) => [Some(a), None, None],
PathSegment::QuadTo(a, b) => [Some(a), Some(b), None],
PathSegment::CubicTo(a, b, c) => [Some(a), Some(b), Some(c)],
PathSegment::Close => [None; 3],
});
for point in points.flatten() {
low = (low.0.min(point.x), low.1.min(point.y));
high = (high.0.max(point.x), high.1.max(point.y));
}
if low.0 > right || low.1 > bottom || high.0 < left || high.1 < top {
return;
}
for segment in contour {
match *segment {
PathSegment::MoveTo(a) => kept.move_to(a.x, a.y),
PathSegment::LineTo(a) => kept.line_to(a.x, a.y),
PathSegment::QuadTo(a, b) => kept.quad_to(a.x, a.y, b.x, b.y),
PathSegment::CubicTo(a, b, c) => kept.cubic_to(a.x, a.y, b.x, b.y, c.x, c.y),
PathSegment::Close => kept.close(),
}
}
}
pub(crate) fn fit_dash(perimeter: f32, on: f32, off: f32) -> Option<(f32, f32)> {
let period = on + off;
if !(on >= 0.0 && off > 0.0 && period.is_finite() && period >= 1.0 && perimeter > 0.0) {
return None;
}
let count = (perimeter / period).round().max(1.0);
let stretch = perimeter / (count * period);
Some((on * stretch, off * stretch))
}
pub(crate) fn fill_opaque(pixmap: &mut PixmapMut<'_>, area: tiny_skia::Rect, color: Color) -> bool {
let edges = [area.left(), area.top(), area.right(), area.bottom()];
if color.a != 255 || edges.iter().any(|edge| edge.fract() != 0.0) {
return false;
}
let (width, height) = (pixmap.width() as f32, pixmap.height() as f32);
let [left, top, right, bottom] = [
area.left().clamp(0.0, width),
area.top().clamp(0.0, height),
area.right().clamp(0.0, width),
area.bottom().clamp(0.0, height),
]
.map(|edge| edge as usize);
let stride = pixmap.width() as usize * 4;
let pixel = [color.r, color.g, color.b, 255];
for row in pixmap
.data_mut()
.chunks_exact_mut(stride)
.take(bottom)
.skip(top)
{
row[left * 4..right * 4].as_chunks_mut::<4>().0.fill(pixel);
}
true
}