use crate::display::PixelFormat;
use crate::element::{Element, IntoElement};
use crate::pod::FrameScope;
use crate::renderers::software::rasterize_text;
use crate::sdf::SurfacePainter;
use crate::solver::Region;
use crate::style::Color;
use crate::svg::math::{floor_f32, sqrt_f32};
#[inline(always)]
fn canon_f32(v: f32) -> u32 {
if v == 0.0 {
0
} else {
v.to_bits()
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
pub struct StaticSceneId(pub u32);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
pub struct CustomShaderId(pub u32);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
pub enum CustomShaderLayer {
#[default]
Background,
Foreground,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Viewport2D {
pub offset_x: f32,
pub offset_y: f32,
pub zoom: f32,
}
impl Default for Viewport2D {
#[inline(always)]
fn default() -> Self {
Self {
offset_x: 0.0,
offset_y: 0.0,
zoom: 1.0,
}
}
}
impl PartialEq for Viewport2D {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
canon_f32(self.offset_x) == canon_f32(other.offset_x)
&& canon_f32(self.offset_y) == canon_f32(other.offset_y)
&& canon_f32(self.zoom) == canon_f32(other.zoom)
}
}
impl Eq for Viewport2D {}
impl Viewport2D {
#[inline(always)]
pub const fn new(offset_x: f32, offset_y: f32, zoom: f32) -> Self {
Self {
offset_x,
offset_y,
zoom,
}
}
#[inline(always)]
pub fn world_to_local(&self, wx: f32, wy: f32) -> (f32, f32) {
(wx * self.zoom + self.offset_x, wy * self.zoom + self.offset_y)
}
#[inline(always)]
pub fn local_to_world(&self, lx: f32, ly: f32) -> (f32, f32) {
let inv = if self.zoom.abs() > 1e-6 {
1.0 / self.zoom
} else {
1.0
};
((lx - self.offset_x) * inv, (ly - self.offset_y) * inv)
}
#[inline(always)]
pub fn projected_px(&self, world_size: f32) -> f32 {
world_size * self.zoom
}
#[inline]
pub fn visible_world_bounds(&self, canvas_w: f32, canvas_h: f32) -> [f32; 4] {
let (x0, y0) = self.local_to_world(0.0, 0.0);
let (x1, y1) = self.local_to_world(canvas_w, canvas_h);
[x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)]
}
#[inline]
pub fn world_aabb_to_screen_region(&self, aabb: [f32; 4], canvas_bounds: Region) -> Region {
let (lx0, ly0) = self.world_to_local(aabb[0], aabb[1]);
let (lx1, ly1) = self.world_to_local(aabb[2], aabb[3]);
let min_x = floor_f32(lx0.min(lx1)) as i32 - 2;
let min_y = floor_f32(ly0.min(ly1)) as i32 - 2;
let max_x = floor_f32(lx0.max(lx1)) as i32 + 3;
let max_y = floor_f32(ly0.max(ly1)) as i32 + 3;
let sx = (canvas_bounds.x as i32 + min_x).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
let sy = (canvas_bounds.y as i32 + min_y).clamp(i16::MIN as i32, i16::MAX as i32) as i16;
let sw = (max_x - min_x).clamp(0, i16::MAX as i32) as i16;
let sh = (max_y - min_y).clamp(0, i16::MAX as i32) as i16;
Region::new(sx, sy, sw, sh).intersect(canvas_bounds)
}
#[inline]
pub fn camera_hash(&self) -> u32 {
let mut h: u32 = 0x811c_9dc5;
for bits in [
canon_f32(self.offset_x),
canon_f32(self.offset_y),
canon_f32(self.zoom),
] {
for b in bits.to_le_bytes() {
h ^= b as u32;
h = h.wrapping_mul(0x0100_0193);
}
}
h
}
}
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum CanvasPrimitiveKind {
#[default]
RoundedRect = 0,
Circle = 1,
Capsule = 2,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct CanvasStaticGeom {
pub cx: f32,
pub cy: f32,
pub w: f32,
pub h: f32,
pub cos: f32,
pub sin: f32,
pub corner_r: f32,
pub kind_flags: u32,
}
const _: () = assert!(core::mem::size_of::<CanvasStaticGeom>() == 32);
impl Default for CanvasStaticGeom {
#[inline(always)]
fn default() -> Self {
Self {
cx: 0.0,
cy: 0.0,
w: 16.0,
h: 16.0,
cos: 1.0,
sin: 0.0,
corner_r: 3.0,
kind_flags: 0,
}
}
}
impl CanvasStaticGeom {
#[inline(always)]
pub const fn rounded_rect_rotated(
cx: f32,
cy: f32,
w: f32,
h: f32,
cos: f32,
sin: f32,
corner_r: f32,
) -> Self {
Self {
cx,
cy,
w,
h,
cos,
sin,
corner_r,
kind_flags: CanvasPrimitiveKind::RoundedRect as u32,
}
}
#[inline(always)]
pub const fn circle(cx: f32, cy: f32, diameter: f32) -> Self {
Self {
cx,
cy,
w: diameter,
h: diameter,
cos: 1.0,
sin: 0.0,
corner_r: diameter * 0.5,
kind_flags: CanvasPrimitiveKind::Circle as u32,
}
}
#[inline]
pub fn capsule(x0: f32, y0: f32, x1: f32, y1: f32, thickness: f32) -> Self {
let dx = x1 - x0;
let dy = y1 - y0;
let len = sqrt_f32(dx * dx + dy * dy);
let (cos, sin) = if len > 1e-5 {
(dx / len, dy / len)
} else {
(1.0, 0.0)
};
Self {
cx: (x0 + x1) * 0.5,
cy: (y0 + y1) * 0.5,
w: len,
h: thickness.max(1.0),
cos,
sin,
corner_r: thickness * 0.5,
kind_flags: CanvasPrimitiveKind::Capsule as u32,
}
}
#[inline]
pub fn world_aabb(&self) -> [f32; 4] {
let hw = if self.kind_flags == CanvasPrimitiveKind::Capsule as u32 {
(self.w + self.h) * 0.5
} else {
self.w * 0.5
};
let hh = self.h * 0.5;
let ac = self.cos.abs();
let as_ = self.sin.abs();
let ex = hw * ac + hh * as_;
let ey = hw * as_ + hh * ac;
[self.cx - ex, self.cy - ey, self.cx + ex, self.cy + ey]
}
}
pub const CANVAS_STATE_HOVERED: u8 = 1 << 0;
pub const CANVAS_STATE_SELECTED: u8 = 1 << 1;
pub const CANVAS_STATE_DISABLED: u8 = 1 << 2;
pub const CANVAS_STATE_LOCKED: u8 = 1 << 3;
pub const CANVAS_STATE_FOCUSED: u8 = 1 << 4;
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct CanvasItemState {
pub palette_idx: u8,
pub flags: u8,
pub badge_mask: u8,
pub lod_group: u8,
}
const _: () = assert!(core::mem::size_of::<CanvasItemState>() == 4);
impl CanvasItemState {
#[inline(always)]
pub const fn new(palette_idx: u8) -> Self {
Self {
palette_idx,
flags: 0,
badge_mask: 0,
lod_group: 0,
}
}
#[inline(always)]
pub const fn with_flags(mut self, flags: u8) -> Self {
self.flags = flags;
self
}
#[inline(always)]
pub const fn with_badge(mut self, badge_mask: u8) -> Self {
self.badge_mask = badge_mask;
self
}
#[inline(always)]
pub const fn is_selected(self) -> bool {
(self.flags & CANVAS_STATE_SELECTED) != 0
}
#[inline(always)]
pub const fn is_hovered(self) -> bool {
(self.flags & CANVAS_STATE_HOVERED) != 0
}
#[inline(always)]
pub const fn is_disabled(self) -> bool {
(self.flags & CANVAS_STATE_DISABLED) != 0
}
#[inline(always)]
pub const fn is_locked(self) -> bool {
(self.flags & CANVAS_STATE_LOCKED) != 0
}
#[inline(always)]
pub const fn is_focused(self) -> bool {
(self.flags & CANVAS_STATE_FOCUSED) != 0
}
#[inline(always)]
pub const fn pack_u32(self) -> u32 {
(self.palette_idx as u32)
| ((self.flags as u32) << 8)
| ((self.badge_mask as u32) << 16)
| ((self.lod_group as u32) << 24)
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Default)]
pub struct CanvasMeshVertex {
pub x: f32,
pub y: f32,
pub color: u32,
}
const _: () = assert!(core::mem::size_of::<CanvasMeshVertex>() == 12);
#[derive(Copy, Clone, Debug)]
pub struct CanvasAsciiLabel<'a> {
pub wx: f32,
pub wy: f32,
pub font_px: f32,
pub color: Color,
pub text: &'a str,
}
#[derive(Copy, Clone)]
pub struct CustomShaderCall<'a> {
pub id: CustomShaderId,
pub world_bounds: [f32; 4],
pub state_hash: u64,
pub uniform_bytes: &'a [u8],
}
impl<'a> PartialEq for CustomShaderCall<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.state_hash == other.state_hash
&& canon_f32(self.world_bounds[0]) == canon_f32(other.world_bounds[0])
&& canon_f32(self.world_bounds[1]) == canon_f32(other.world_bounds[1])
&& canon_f32(self.world_bounds[2]) == canon_f32(other.world_bounds[2])
&& canon_f32(self.world_bounds[3]) == canon_f32(other.world_bounds[3])
&& self.uniform_bytes == other.uniform_bytes
}
}
impl<'a> Eq for CustomShaderCall<'a> {}
#[derive(Copy, Clone)]
pub struct SceneCanvasSpec<'a> {
pub id: u32,
pub width: i16,
pub height: i16,
pub opacity: u8,
pub bg: Color,
pub viewport: Viewport2D,
pub zooming: bool,
pub scene_id: StaticSceneId,
pub geom_revision: u32,
pub state_revision: u32,
pub mesh_revision: u32,
pub palette: &'a [Color],
pub geoms: &'a [CanvasStaticGeom],
pub states: &'a [CanvasItemState],
pub visible_ranges: &'a [(u32, u32)],
pub mesh_verts: &'a [CanvasMeshVertex],
pub mesh_indices: &'a [u32],
pub labels: &'a [CanvasAsciiLabel<'a>],
pub marquee: Option<[f32; 4]>,
pub marquee_color: Color,
pub dirty_screen_rects: &'a [Region],
pub bg_shaders: &'a [CustomShaderCall<'a>],
pub fg_shaders: &'a [CustomShaderCall<'a>],
pub software_bg_fallback: Option<&'a dyn Fn(&mut dyn SurfacePainter, Viewport2D)>,
pub software_fg_fallback: Option<&'a dyn Fn(&mut dyn SurfacePainter, Viewport2D)>,
}
impl<'a> SceneCanvasSpec<'a> {
pub fn content_hash(&self) -> u32 {
let mut h: u32 = 0x811c_9dc5;
let mut mix = |v: u32| {
for b in v.to_le_bytes() {
h ^= b as u32;
h = h.wrapping_mul(0x0100_0193);
}
};
mix(self.id);
mix(self.scene_id.0);
mix(self.geom_revision);
mix(self.state_revision);
mix(self.mesh_revision);
mix(self.viewport.camera_hash());
mix(self.bg.0);
mix(self.opacity as u32 | ((self.zooming as u32) << 8));
mix(self.labels.len() as u32);
for c in self.palette.iter().take(16) {
mix(c.0);
}
if let Some(m) = self.marquee {
for v in m {
mix(canon_f32(v));
}
}
for s in self.bg_shaders {
mix(s.id.0);
mix(s.state_hash as u32);
mix((s.state_hash >> 32) as u32);
}
for s in self.fg_shaders {
mix(s.id.0);
mix(s.state_hash as u32);
mix((s.state_hash >> 32) as u32);
}
h
}
}
impl<'a> PartialEq for SceneCanvasSpec<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.width == other.width
&& self.height == other.height
&& self.opacity == other.opacity
&& self.bg == other.bg
&& self.viewport == other.viewport
&& self.zooming == other.zooming
&& self.scene_id == other.scene_id
&& self.geom_revision == other.geom_revision
&& self.state_revision == other.state_revision
&& self.mesh_revision == other.mesh_revision
&& self.palette == other.palette
&& self.labels.len() == other.labels.len()
&& self.marquee.map(|m| m.map(canon_f32)) == other.marquee.map(|m| m.map(canon_f32))
&& self.bg_shaders == other.bg_shaders
&& self.fg_shaders == other.fg_shaders
}
}
impl<'a> Eq for SceneCanvasSpec<'a> {}
pub struct SceneCanvas<'a> {
spec: SceneCanvasSpec<'a>,
}
impl<'a> SceneCanvas<'a> {
#[inline(always)]
pub const fn from_spec(spec: SceneCanvasSpec<'a>) -> Self {
Self { spec }
}
}
impl<'a> IntoElement<'a> for SceneCanvas<'a> {
#[inline]
fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
let r = cx.arena().alloc(self.spec);
Element::SceneCanvas(r)
}
}
#[inline(always)]
pub const fn scene_canvas<'a>(spec: SceneCanvasSpec<'a>) -> SceneCanvas<'a> {
SceneCanvas::from_spec(spec)
}
struct BufferPainter<'a, P: PixelFormat> {
bounds: Region,
clip: Region,
origin_x: i16,
origin_y: i16,
stride: usize,
target: &'a mut [P],
}
impl<'a, P: PixelFormat> SurfacePainter for BufferPainter<'a, P> {
#[inline(always)]
fn bounds(&self) -> Region {
self.bounds
}
#[inline(always)]
fn clip(&self) -> Region {
self.clip
}
#[inline(always)]
fn draw_text(&mut self, bounds: Region, text: &str, color: Color, font_size: u8) {
rasterize_text(
bounds,
self.clip,
text,
color,
font_size,
0x10,
self.origin_x,
self.origin_y,
self.stride,
self.target,
);
}
#[inline(always)]
fn blend_pixel(&mut self, x: i16, y: i16, color: Color, alpha: u8) {
if alpha == 0 || !self.clip.contains(x, y) {
return;
}
let rx = (x - self.origin_x) as usize;
let ry = (y - self.origin_y) as usize;
let idx = ry * self.stride + rx;
if idx < self.target.len() {
let eff_a = ((color.a() as u16 * alpha as u16) / 255) as u8;
if eff_a == 0 {
return;
}
if eff_a == 255 {
self.target[idx] = P::from_color(color);
} else {
let dst = self.target[idx].to_color();
let inv = 255u16 - eff_a as u16;
let a16 = eff_a as u16;
let r = ((color.r() as u16 * a16 + dst.r() as u16 * inv) / 255) as u8;
let g = ((color.g() as u16 * a16 + dst.g() as u16 * inv) / 255) as u8;
let b = ((color.b() as u16 * a16 + dst.b() as u16 * inv) / 255) as u8;
self.target[idx] = P::from_color(Color::rgb(r, g, b));
}
}
}
}
#[inline(always)]
fn blend_px<P: PixelFormat>(target: &mut [P], idx: usize, color: Color, alpha: u8) {
if alpha == 0 || idx >= target.len() {
return;
}
if alpha == 255 {
target[idx] = P::from_color(color);
return;
}
let dst = target[idx].to_color();
let inv = 255u16 - alpha as u16;
let a16 = alpha as u16;
let r = ((color.r() as u16 * a16 + dst.r() as u16 * inv) / 255) as u8;
let g = ((color.g() as u16 * a16 + dst.g() as u16 * inv) / 255) as u8;
let b = ((color.b() as u16 * a16 + dst.b() as u16 * inv) / 255) as u8;
target[idx] = P::from_color(Color::rgb(r, g, b));
}
#[inline]
fn resolve_item_colors(state: CanvasItemState, palette: &[Color]) -> (Color, Color, bool) {
let base = palette
.get(state.palette_idx as usize)
.copied()
.unwrap_or(Color::rgb(59, 130, 246));
if state.is_disabled() {
return (base.scale_alpha(90), Color::TRANSPARENT, false);
}
let mut fill = base;
let mut border = Color::TRANSPARENT;
if state.is_selected() {
fill = base.lerp(Color::WHITE, 46);
border = Color::WHITE;
} else if state.is_hovered() {
fill = base.lerp(Color::WHITE, 28);
border = base.lerp(Color::WHITE, 140);
}
if state.is_focused() {
border = Color::rgb(96, 165, 250);
}
(fill, border, state.badge_mask != 0 || state.is_locked())
}
#[inline]
fn rasterize_triangle<P: PixelFormat>(
x0: f32,
y0: f32,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
color: Color,
eff_alpha: u8,
clip: Region,
origin_x: i16,
origin_y: i16,
stride: usize,
target: &mut [P],
) {
if eff_alpha == 0 {
return;
}
let min_x = (floor_f32(x0.min(x1).min(x2)) as i32).max(clip.x as i32);
let max_x = (floor_f32(x0.max(x1).max(x2)) as i32 + 1).min(clip.right() as i32);
let min_y = (floor_f32(y0.min(y1).min(y2)) as i32).max(clip.y as i32);
let max_y = (floor_f32(y0.max(y1).max(y2)) as i32 + 1).min(clip.bottom() as i32);
if min_x >= max_x || min_y >= max_y {
return;
}
let area = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
if area.abs() < 1e-4 {
return;
}
let sign = if area > 0.0 { 1.0 } else { -1.0 };
for py in min_y..max_y {
let fy = py as f32 + 0.5;
let row_off = ((py as i16 - origin_y) as usize) * stride;
for px in min_x..max_x {
let fx = px as f32 + 0.5;
let e0 = ((x1 - x0) * (fy - y0) - (y1 - y0) * (fx - x0)) * sign;
let e1 = ((x2 - x1) * (fy - y1) - (y2 - y1) * (fx - x1)) * sign;
let e2 = ((x0 - x2) * (fy - y2) - (y0 - y2) * (fx - x2)) * sign;
if e0 >= 0.0 && e1 >= 0.0 && e2 >= 0.0 {
let idx = row_off + ((px as i16 - origin_x) as usize);
blend_px(target, idx, color, eff_alpha);
}
}
}
}
#[allow(clippy::too_many_arguments)]
pub fn rasterize_scene_canvas<P: PixelFormat>(
bounds: Region,
clip: Region,
spec: &SceneCanvasSpec<'_>,
scale: f32,
origin_x: i16,
origin_y: i16,
stride: usize,
target: &mut [P],
) {
let eff = bounds.intersect(clip);
if eff.is_empty() || spec.opacity == 0 {
return;
}
let s = if scale > 0.0 { scale } else { 1.0 };
let vp_zoom = spec.viewport.zoom * s;
let vp_ox = bounds.x as f32 + spec.viewport.offset_x * s;
let vp_oy = bounds.y as f32 + spec.viewport.offset_y * s;
if spec.bg.a() > 0 {
let a = ((spec.bg.a() as u16 * spec.opacity as u16) / 255) as u8;
for py in eff.y..eff.bottom() {
let row_off = ((py - origin_y) as usize) * stride;
for px in eff.x..eff.right() {
blend_px(target, row_off + ((px - origin_x) as usize), spec.bg, a);
}
}
}
if let Some(bg_cb) = spec.software_bg_fallback {
let mut painter = BufferPainter {
bounds,
clip: eff,
origin_x,
origin_y,
stride,
target,
};
bg_cb(&mut painter, spec.viewport);
}
let mut t_idx = 0usize;
while t_idx + 2 < spec.mesh_indices.len() {
let i0 = spec.mesh_indices[t_idx] as usize;
let i1 = spec.mesh_indices[t_idx + 1] as usize;
let i2 = spec.mesh_indices[t_idx + 2] as usize;
t_idx += 3;
if i0 >= spec.mesh_verts.len()
|| i1 >= spec.mesh_verts.len()
|| i2 >= spec.mesh_verts.len()
{
continue;
}
let v0 = spec.mesh_verts[i0];
let v1 = spec.mesh_verts[i1];
let v2 = spec.mesh_verts[i2];
let col = Color(v0.color);
let a = ((col.a() as u16 * spec.opacity as u16) / 255) as u8;
rasterize_triangle(
v0.x * vp_zoom + vp_ox,
v0.y * vp_zoom + vp_oy,
v1.x * vp_zoom + vp_ox,
v1.y * vp_zoom + vp_oy,
v2.x * vp_zoom + vp_ox,
v2.y * vp_zoom + vp_oy,
col,
a,
eff,
origin_x,
origin_y,
stride,
target,
);
}
let full_range = [(0u32, spec.geoms.len() as u32)];
let ranges: &[(u32, u32)] = if spec.visible_ranges.is_empty() {
&full_range
} else {
spec.visible_ranges
};
for &(r_start, r_end) in ranges {
let start = (r_start as usize).min(spec.geoms.len());
let end = (r_end as usize).min(spec.geoms.len());
for i in start..end {
let g = &spec.geoms[i];
let st = spec
.states
.get(i)
.copied()
.unwrap_or_default();
let (fill_col, border_col, has_badge) = resolve_item_colors(st, spec.palette);
let scx = g.cx * vp_zoom + vp_ox;
let scy = g.cy * vp_zoom + vp_oy;
let sw = g.w * vp_zoom;
let sh = g.h * vp_zoom;
let is_capsule = g.kind_flags == CanvasPrimitiveKind::Capsule as u32;
let is_circle = g.kind_flags == CanvasPrimitiveKind::Circle as u32;
let hw = if is_capsule { (sw + sh) * 0.5 } else { sw * 0.5 };
let hh = sh * 0.5;
let ext_x = hw * g.cos.abs() + hh * g.sin.abs() + 2.0;
let ext_y = hw * g.sin.abs() + hh * g.cos.abs() + 2.0;
let min_x = (floor_f32(scx - ext_x) as i32).max(eff.x as i32);
let max_x = (floor_f32(scx + ext_x) as i32 + 1).min(eff.right() as i32);
let min_y = (floor_f32(scy - ext_y) as i32).max(eff.y as i32);
let max_y = (floor_f32(scy + ext_y) as i32 + 1).min(eff.bottom() as i32);
if min_x >= max_x || min_y >= max_y {
continue;
}
let corner_r = (g.corner_r * vp_zoom).min(hw.min(hh)).max(0.0);
let border_px = if border_col.a() > 0 {
(1.5 * s).clamp(1.0, 3.0)
} else {
0.0
};
let fill_a = ((fill_col.a() as u16 * spec.opacity as u16) / 255) as u8;
let border_a = ((border_col.a() as u16 * spec.opacity as u16) / 255) as u8;
for py in min_y..max_y {
let dy = py as f32 + 0.5 - scy;
let row_off = ((py as i16 - origin_y) as usize) * stride;
for px in min_x..max_x {
let dx = px as f32 + 0.5 - scx;
let lx = dx * g.cos + dy * g.sin;
let ly = -dx * g.sin + dy * g.cos;
let dist = if is_circle {
sqrt_f32(lx * lx + ly * ly) - hw.min(hh)
} else if is_capsule {
let half_seg = (sw * 0.5).max(0.0);
let qx = (lx.abs() - half_seg).max(0.0);
sqrt_f32(qx * qx + ly * ly) - hh
} else {
let qx = lx.abs() - (hw - corner_r);
let qy = ly.abs() - (hh - corner_r);
let ox = qx.max(0.0);
let oy = qy.max(0.0);
sqrt_f32(ox * ox + oy * oy) + qx.max(qy).min(0.0) - corner_r
};
if dist <= 0.75 {
let cov = (0.75 - dist).clamp(0.0, 1.0);
let idx = row_off + ((px as i16 - origin_x) as usize);
if border_px > 0.0 && dist > -border_px {
let a = ((border_a as f32) * cov) as u8;
blend_px(target, idx, border_col, a);
} else {
let a = ((fill_a as f32) * cov) as u8;
blend_px(target, idx, fill_col, a);
}
if has_badge && hw >= 6.0 && hh >= 6.0 {
let bx = lx - (hw - 3.5);
let by = ly + (hh - 3.5);
if bx * bx + by * by <= 4.5 {
blend_px(target, idx, Color::rgb(250, 204, 21), spec.opacity);
}
}
}
}
}
}
}
for lbl in spec.labels {
if lbl.text.is_empty() {
continue;
}
let sx = (lbl.wx * vp_zoom + vp_ox) as i32;
let sy = (lbl.wy * vp_zoom + vp_oy) as i32;
let fpx = (lbl.font_px * s).clamp(7.0, 32.0) as u8;
let est_w = ((lbl.text.len() as i32) * (fpx as i32) * 3 / 4).max(16);
let est_h = (fpx as i32) + 4;
let tb = Region::new(
(sx - est_w / 2).clamp(i16::MIN as i32, i16::MAX as i32) as i16,
(sy - est_h / 2).clamp(i16::MIN as i32, i16::MAX as i32) as i16,
est_w.min(i16::MAX as i32) as i16,
est_h.min(i16::MAX as i32) as i16,
);
if !tb.intersect(eff).is_empty() {
rasterize_text(
tb,
eff,
lbl.text,
lbl.color,
fpx,
1 | 0x10,
origin_x,
origin_y,
stride,
target,
);
}
}
if let Some([mx0, my0, mx1, my1]) = spec.marquee {
let rx0 = (bounds.x as f32 + mx0.min(mx1) * s) as i32;
let ry0 = (bounds.y as f32 + my0.min(my1) * s) as i32;
let rx1 = (bounds.x as f32 + mx0.max(mx1) * s) as i32;
let ry1 = (bounds.y as f32 + my0.max(my1) * s) as i32;
let mr = Region::new(
rx0.clamp(i16::MIN as i32, i16::MAX as i32) as i16,
ry0.clamp(i16::MIN as i32, i16::MAX as i32) as i16,
(rx1 - rx0).clamp(0, i16::MAX as i32) as i16,
(ry1 - ry0).clamp(0, i16::MAX as i32) as i16,
)
.intersect(eff);
if !mr.is_empty() {
let fill_a = ((spec.marquee_color.a() as u16 * 56) / 255) as u8;
let edge_a = ((spec.marquee_color.a() as u16 * 210) / 255) as u8;
for py in mr.y..mr.bottom() {
let row_off = ((py - origin_y) as usize) * stride;
let is_y_edge = py == mr.y || py == mr.bottom() - 1;
for px in mr.x..mr.right() {
let is_edge = is_y_edge || px == mr.x || px == mr.right() - 1;
blend_px(
target,
row_off + ((px - origin_x) as usize),
spec.marquee_color,
if is_edge { edge_a } else { fill_a },
);
}
}
}
}
if let Some(fg_cb) = spec.software_fg_fallback {
let mut painter = BufferPainter {
bounds,
clip: eff,
origin_x,
origin_y,
stride,
target,
};
fg_cb(&mut painter, spec.viewport);
}
}