use crate::utils::srgb::{linear_to_srgb, srgb_to_linear};
use super::{CaptionPainter, CaptionStyle, CaptionTrack};
#[derive(Debug)]
pub struct CaptionOverlay {
pub track: CaptionTrack,
pub painter: CaptionPainter,
width: u32,
height: u32,
rgba: Vec<u8>,
active: Vec<usize>,
valid: bool,
base_style: CaptionStyle,
auto_scale: bool,
}
impl CaptionOverlay {
pub fn new(track: CaptionTrack) -> Self {
Self::with_painter(track, CaptionPainter::new())
}
pub fn with_painter(track: CaptionTrack, painter: CaptionPainter) -> Self {
let base_style = painter.style.clone();
Self {
track,
painter,
width: 0,
height: 0,
rgba: Vec::new(),
active: Vec::new(),
valid: false,
base_style,
auto_scale: false,
}
}
pub fn auto_scale(mut self, enabled: bool) -> Self {
self.set_auto_scale(enabled);
self
}
pub fn set_auto_scale(&mut self, enabled: bool) {
if self.auto_scale == enabled {
return;
}
self.auto_scale = enabled;
if self.height > 0 {
self.painter.style = if enabled {
self.base_style.for_height(self.height)
} else {
self.base_style.clone()
};
self.valid = false;
}
}
pub fn style(&self) -> &CaptionStyle {
&self.base_style
}
pub fn set_style(&mut self, style: CaptionStyle) {
self.base_style = style.clone();
self.painter.style = if self.auto_scale && self.height > 0 {
style.for_height(self.height)
} else {
style
};
self.valid = false;
}
pub fn set_track(&mut self, track: CaptionTrack) {
self.track = track;
self.valid = false;
}
pub fn set_size(&mut self, width: u32, height: u32) {
if self.width == width && self.height == height {
return;
}
self.width = width;
self.height = height;
self.rgba = vec![0u8; (width as usize) * (height as usize) * 4];
if self.auto_scale && height > 0 {
self.painter.style = self.base_style.for_height(height);
}
self.valid = false;
}
pub fn size(&self) -> (u32, u32) {
(self.width, self.height)
}
pub fn update(&mut self, time: f64) -> bool {
if self.width == 0 || self.height == 0 {
return false;
}
let active: Vec<usize> = self
.track
.cues
.iter()
.enumerate()
.filter(|(_, c)| c.is_active_at(time))
.map(|(i, _)| i)
.collect();
if self.valid && active == self.active {
return false;
}
self.active = active;
self.valid = true;
self.rgba.fill(0);
if !self.active.is_empty() {
self.painter
.burn_in(&mut self.rgba, self.width, self.height, &self.track, time);
}
true
}
pub fn invalidate(&mut self) {
self.valid = false;
}
pub fn is_visible(&self) -> bool {
!self.active.is_empty()
}
pub fn rgba(&self) -> &[u8] {
&self.rgba
}
pub fn composite(&self, frame: &mut [u8]) -> bool {
if !self.is_visible() || frame.len() != self.rgba.len() {
return false;
}
for (dst, src) in frame.chunks_exact_mut(4).zip(self.rgba.chunks_exact(4)) {
let sa = src[3] as u32;
if sa == 0 {
continue;
}
if sa == 255 {
dst.copy_from_slice(src);
continue;
}
let da = dst[3] as u32;
let out_a = sa + da * (255 - sa) / 255;
if out_a == 0 {
continue;
}
let sa_f = sa as f32 / 255.0;
let da_f = da as f32 / 255.0;
let out_a_f = sa_f + da_f * (1.0 - sa_f);
for c in 0..3 {
let s_lin = srgb_to_linear(src[c]);
let d_lin = srgb_to_linear(dst[c]);
let out = (s_lin * sa_f + d_lin * da_f * (1.0 - sa_f)) / out_a_f;
dst[c] = linear_to_srgb(out);
}
dst[3] = out_a.min(255) as u8;
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::captions::CaptionStyle;
fn overlay() -> CaptionOverlay {
let track = CaptionTrack::new()
.cue(0.0, 2.0, "first")
.cue(2.0, 4.0, "second");
let mut o = CaptionOverlay::new(track);
o.set_style(CaptionStyle::default().font_size(12.0).margin(6.0));
o.set_size(240, 135);
o
}
#[test]
fn rasterizes_once_per_cue_change() {
let mut o = overlay();
assert!(o.update(0.5), "first update must rasterize");
assert!(!o.update(1.0), "same cue must reuse the buffer");
assert!(!o.update(1.9));
assert!(o.update(2.5), "cue change must re-rasterize");
assert!(o.update(9.0), "cue ending must clear the buffer");
assert!(!o.update(9.5), "still nothing showing");
}
#[test]
fn buffer_is_transparent_when_nothing_shows() {
let mut o = overlay();
o.update(9.0);
assert!(!o.is_visible());
assert!(o.rgba().iter().all(|&b| b == 0));
o.update(0.5);
assert!(o.is_visible());
assert!(o.rgba().chunks_exact(4).any(|px| px[3] > 0));
}
#[test]
fn resizing_reallocates_and_invalidates() {
let mut o = overlay();
o.update(0.5);
assert_eq!(o.rgba().len(), (240 * 135 * 4) as usize);
o.set_size(320, 180);
assert_eq!(o.rgba().len(), (320 * 180 * 4) as usize);
assert!(o.update(0.5), "resize must force a redraw");
}
#[test]
fn auto_scale_tracks_the_frame_height() {
let mut o = CaptionOverlay::new(CaptionTrack::new().cue(0.0, 1.0, "x")).auto_scale(true);
o.set_size(1920, 1080);
let full = o.painter.style.font_size;
o.set_size(960, 540);
assert!((o.painter.style.font_size - full * 0.5).abs() < 1e-4);
}
#[test]
fn auto_scale_toggles_back_to_the_authored_style() {
let mut o = CaptionOverlay::new(CaptionTrack::new().cue(0.0, 1.0, "x"));
o.set_style(CaptionStyle::default().font_size(40.0));
o.set_size(1920, 540);
assert_eq!(o.painter.style.font_size, 40.0, "off by default");
o.set_auto_scale(true);
assert_eq!(o.painter.style.font_size, 20.0);
o.set_auto_scale(false);
assert_eq!(o.painter.style.font_size, 40.0, "must restore the original");
o.set_auto_scale(true);
assert_eq!(o.style().font_size, 40.0);
assert_eq!(o.painter.style.font_size, 20.0);
}
#[test]
fn style_and_track_changes_invalidate_the_cache() {
let mut o = overlay();
o.update(0.5);
o.set_style(CaptionStyle::default().font_size(20.0));
assert!(o.update(0.5), "style change must redraw");
o.update(0.5);
o.set_track(CaptionTrack::new().cue(0.0, 5.0, "replaced"));
assert!(o.update(0.5), "track change must redraw");
o.update(0.5);
o.invalidate();
assert!(o.update(0.5), "explicit invalidate must redraw");
}
#[test]
fn composite_writes_only_where_the_overlay_is_opaque() {
let mut o = overlay();
o.update(0.5);
let mut frame = vec![0u8; o.rgba().len()];
for px in frame.chunks_exact_mut(4) {
px.copy_from_slice(&[10, 20, 30, 255]);
}
assert!(o.composite(&mut frame));
let untouched = frame
.chunks_exact(4)
.filter(|px| px[..3] == [10, 20, 30])
.count();
let changed = frame.chunks_exact(4).count() - untouched;
assert!(changed > 0, "expected caption pixels");
assert!(untouched > changed, "expected most of the frame untouched");
assert!(frame.chunks_exact(4).all(|px| px[3] == 255));
}
#[test]
fn composite_is_a_noop_on_mismatched_buffers() {
let mut o = overlay();
o.update(0.5);
let mut wrong = vec![0u8; 8];
assert!(!o.composite(&mut wrong));
assert!(wrong.iter().all(|&b| b == 0));
}
#[test]
fn zero_sized_overlay_never_rasterizes() {
let mut o = CaptionOverlay::new(CaptionTrack::new().cue(0.0, 1.0, "x"));
assert!(!o.update(0.5));
assert!(o.rgba().is_empty());
}
}