| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | use std::hash::{Hash, Hasher}; |
| 21 | |
| 22 | use ec_subs::KaraokeMode; |
| 23 | |
| 24 | use crate::subtitle::{Cue, Syllable}; |
| 25 | use crate::text::{Block, Syl, TextStyle, WordFx}; |
| 26 | |
| 27 | |
| 28 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 29 | pub enum CaptionAnim { |
| 30 | |
| 31 | #[default] |
| 32 | None, |
| 33 | |
| 34 | Karaoke, |
| 35 | |
| 36 | Pop, |
| 37 | |
| 38 | Bounce, |
| 39 | |
| 40 | Word, |
| 41 | } |
| 42 | |
| 43 | impl CaptionAnim { |
| 44 | pub const ALL: [CaptionAnim; 5] = [Self::None, Self::Karaoke, Self::Pop, Self::Bounce, Self::Word]; |
| 45 | |
| 46 | |
| 47 | pub fn name(self) -> &'static str { |
| 48 | match self { |
| 49 | Self::None => "none", |
| 50 | Self::Karaoke => "karaoke", |
| 51 | Self::Pop => "pop", |
| 52 | Self::Bounce => "bounce", |
| 53 | Self::Word => "word", |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub fn from_name(name: &str) -> Option<Self> { |
| 58 | Self::ALL.into_iter().find(|a| a.name() == name) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | pub const MAX_WORDS_PER_LINE: u8 = 12; |
| 64 | |
| 65 | |
| 66 | pub const DEFAULT_HIGHLIGHT: [u8; 3] = [255, 214, 0]; |
| 67 | |
| 68 | |
| 69 | pub const POP_FROM: f32 = 1.25; |
| 70 | pub const POP_US: i64 = 120_000; |
| 71 | |
| 72 | |
| 73 | const PAUSE_US: i64 = 700_000; |
| 74 | |
| 75 | |
| 76 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 77 | pub struct CaptionStyle { |
| 78 | pub anim: CaptionAnim, |
| 79 | |
| 80 | pub highlight: [u8; 3], |
| 81 | |
| 82 | pub hl_box: bool, |
| 83 | pub uppercase: bool, |
| 84 | |
| 85 | |
| 86 | pub words_per_line: u8, |
| 87 | } |
| 88 | |
| 89 | impl Default for CaptionStyle { |
| 90 | fn default() -> Self { |
| 91 | Self { |
| 92 | anim: CaptionAnim::None, |
| 93 | highlight: DEFAULT_HIGHLIGHT, |
| 94 | hl_box: false, |
| 95 | uppercase: false, |
| 96 | words_per_line: 0, |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl CaptionStyle { |
| 102 | |
| 103 | pub fn is_plain(&self) -> bool { |
| 104 | self.anim == CaptionAnim::None && !self.uppercase && self.words_per_line == 0 |
| 105 | } |
| 106 | |
| 107 | pub fn clamped(&self) -> Self { |
| 108 | Self { words_per_line: self.words_per_line.min(MAX_WORDS_PER_LINE), ..*self } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 114 | pub struct TimedWord { |
| 115 | pub start_us: i64, |
| 116 | pub end_us: i64, |
| 117 | pub text: String, |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | pub fn cue_words(cue: &Cue) -> Vec<TimedWord> { |
| 123 | |
| 124 | if let Some(real) = cue.real_words() { |
| 125 | return real |
| 126 | .iter() |
| 127 | .map(|(s, e, w)| TimedWord { start_us: *s, end_us: (*e).max(*s), text: w.clone() }) |
| 128 | .collect(); |
| 129 | } |
| 130 | let words: Vec<&str> = cue.text.split_whitespace().collect(); |
| 131 | let total: usize = words.iter().map(|w| w.chars().count().max(1)).sum(); |
| 132 | let span = (cue.end_us - cue.start_us).max(0); |
| 133 | let mut done = 0usize; |
| 134 | words |
| 135 | .iter() |
| 136 | .map(|w| { |
| 137 | let from = cue.start_us + span * done as i64 / total.max(1) as i64; |
| 138 | done += w.chars().count().max(1); |
| 139 | let to = cue.start_us + span * done as i64 / total.max(1) as i64; |
| 140 | TimedWord { start_us: from, end_us: to, text: (*w).to_string() } |
| 141 | }) |
| 142 | .collect() |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | pub fn chunk_words(words: &[TimedWord], per: usize) -> Vec<Vec<TimedWord>> { |
| 148 | let mut out: Vec<Vec<TimedWord>> = Vec::new(); |
| 149 | for w in words { |
| 150 | let start_new = match out.last() { |
| 151 | None => true, |
| 152 | Some(run) => { |
| 153 | (per > 0 && run.len() >= per) |
| 154 | || run.last().is_some_and(|l| w.start_us - l.end_us > PAUSE_US) |
| 155 | } |
| 156 | }; |
| 157 | if start_new { |
| 158 | out.push(Vec::new()); |
| 159 | } |
| 160 | if let Some(run) = out.last_mut() { |
| 161 | run.push(w.clone()); |
| 162 | } |
| 163 | } |
| 164 | out |
| 165 | } |
| 166 | |
| 167 | fn ease_out(x: f32) -> f32 { |
| 168 | let x = x.clamp(0.0, 1.0); |
| 169 | 1.0 - (1.0 - x).powi(3) |
| 170 | } |
| 171 | |
| 172 | |
| 173 | pub fn pop_scale(since_us: i64) -> f32 { |
| 174 | POP_FROM + (1.0 - POP_FROM) * ease_out(since_us as f32 / POP_US as f32) |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | pub fn bounce_fx(since_us: i64) -> (f32, f32) { |
| 180 | const LIFT_US: f32 = 200_000.0; |
| 181 | (1.12, 0.2 * (1.0 - ease_out(since_us as f32 / LIFT_US))) |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | pub fn plan(style: &TextStyle, cues: &[Cue], on: &[usize], frame: u32, fps: f64) -> Vec<Block> { |
| 187 | let cs = style.caption.clamped(); |
| 188 | let t = (f64::from(frame) / fps * 1e6).round() as i64; |
| 189 | on.iter() |
| 190 | .filter_map(|&i| { |
| 191 | let cue = &cues[i]; |
| 192 | if cs.is_plain() { |
| 193 | let lines = cue.text.split('\n').map(str::to_string).collect(); |
| 194 | let kara = cue.real_karaoke().map(|k| karaoke_at(k, t)).unwrap_or_default(); |
| 195 | return Some(Block { lines, fx: Vec::new(), kara }); |
| 196 | } |
| 197 | block_at(&cs, cue, t) |
| 198 | }) |
| 199 | .collect() |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | fn karaoke_at(syllables: &[Syllable], t: i64) -> Vec<Syl> { |
| 206 | syllables |
| 207 | .iter() |
| 208 | .map(|s| { |
| 209 | let (from, len) = (s.from, s.text.chars().count()); |
| 210 | let begun = t >= s.start_us; |
| 211 | let (left, right, split, bare) = match s.mode { |
| 212 | KaraokeMode::Fill => (s.sung, Some(s.unsung), if begun { 1.0 } else { 0.0 }, false), |
| 213 | KaraokeMode::Sweep => { |
| 214 | let span = (s.end_us - s.start_us).max(1) as f32; |
| 215 | (s.sung, Some(s.unsung), ((t - s.start_us) as f32 / span).clamp(0.0, 1.0), false) |
| 216 | } |
| 217 | KaraokeMode::Outline => (s.sung, s.sung, 1.0, !begun), |
| 218 | }; |
| 219 | Syl { from, len, left, right, split, bare } |
| 220 | }) |
| 221 | .collect() |
| 222 | } |
| 223 | |
| 224 | |
| 225 | fn block_at(cs: &CaptionStyle, cue: &Cue, t: i64) -> Option<Block> { |
| 226 | let words = cue_words(cue); |
| 227 | if words.is_empty() { |
| 228 | return None; |
| 229 | } |
| 230 | let per = if cs.anim == CaptionAnim::Word { 1 } else { usize::from(cs.words_per_line) }; |
| 231 | let chunks = chunk_words(&words, per); |
| 232 | |
| 233 | let run = chunks.iter().rev().find(|c| c[0].start_us <= t).unwrap_or(&chunks[0]); |
| 234 | |
| 235 | |
| 236 | let active = run |
| 237 | .iter() |
| 238 | .rposition(|w| w.start_us <= t && w.end_us > w.start_us) |
| 239 | .or_else(|| run.iter().rposition(|w| w.start_us <= t)); |
| 240 | let case = |s: &str| if cs.uppercase { s.to_uppercase() } else { s.to_string() }; |
| 241 | let lines: Vec<String> = if per == 0 { |
| 242 | |
| 243 | cue.text.split('\n').map(case).collect() |
| 244 | } else { |
| 245 | vec![run.iter().map(|w| case(&w.text)).collect::<Vec<_>>().join(" ")] |
| 246 | }; |
| 247 | let highlight = Some((cs.highlight, cs.hl_box)); |
| 248 | let fx: Vec<WordFx> = run |
| 249 | .iter() |
| 250 | .enumerate() |
| 251 | .map(|(k, w)| { |
| 252 | let is_active = active == Some(k); |
| 253 | let since = t - w.start_us; |
| 254 | match cs.anim { |
| 255 | CaptionAnim::None => WordFx::default(), |
| 256 | CaptionAnim::Karaoke => WordFx { highlight: is_active.then_some(()).and(highlight), ..WordFx::default() }, |
| 257 | CaptionAnim::Pop | CaptionAnim::Word => WordFx { |
| 258 | hidden: since < 0, |
| 259 | scale: pop_scale(since), |
| 260 | highlight: is_active.then_some(()).and(highlight), |
| 261 | ..WordFx::default() |
| 262 | }, |
| 263 | CaptionAnim::Bounce => { |
| 264 | if is_active { |
| 265 | let (scale, lift) = bounce_fx(since); |
| 266 | WordFx { scale, lift, highlight, ..WordFx::default() } |
| 267 | } else { |
| 268 | WordFx::default() |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | }) |
| 273 | .collect(); |
| 274 | |
| 275 | if fx.iter().all(|f| f.hidden) { |
| 276 | return None; |
| 277 | } |
| 278 | Some(Block { lines, fx, kara: Vec::new() }) |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | pub fn blocks_key(blocks: &[Block]) -> u64 { |
| 284 | let mut h = std::collections::hash_map::DefaultHasher::new(); |
| 285 | for b in blocks { |
| 286 | b.lines.hash(&mut h); |
| 287 | for f in &b.fx { |
| 288 | (f.hidden, (f.scale * 1000.0).round() as i32, (f.lift * 1000.0).round() as i32, f.highlight).hash(&mut h); |
| 289 | } |
| 290 | for k in &b.kara { |
| 291 | (k.from, k.len, k.left, k.right, (k.split * 1000.0).round() as i32, k.bare).hash(&mut h); |
| 292 | } |
| 293 | 0xB10Cu16.hash(&mut h); |
| 294 | } |
| 295 | h.finish() |
| 296 | } |
| 297 | |
| 298 | #[cfg(test)] |
| 299 | mod tests { |
| 300 | use super::*; |
| 301 | use crate::text::{overlay_blocks, overlay_stack}; |
| 302 | |
| 303 | const W: u32 = 640; |
| 304 | const H: u32 = 360; |
| 305 | const FPS: f64 = 25.0; |
| 306 | |
| 307 | fn cue(text: &str, start_s: f64, end_s: f64) -> Cue { |
| 308 | Cue { start_us: (start_s * 1e6) as i64, end_us: (end_s * 1e6) as i64, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new() } |
| 309 | } |
| 310 | |
| 311 | fn style(anim: CaptionAnim) -> TextStyle { |
| 312 | TextStyle { |
| 313 | boxed: false, |
| 314 | caption: CaptionStyle { anim, ..CaptionStyle::default() }, |
| 315 | ..TextStyle::default() |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | |
| 320 | fn inked(style: &TextStyle, cues: &[Cue], frame: u32) -> Vec<(u32, bool)> { |
| 321 | let blocks = plan(style, cues, &[0], frame, FPS); |
| 322 | let mut canvas = vec![0u8; (W * H * 4) as usize]; |
| 323 | overlay_blocks(style, W, H, &blocks).blend_bgra(&mut canvas); |
| 324 | let mut out = Vec::new(); |
| 325 | for (i, px) in canvas.chunks(4).enumerate() { |
| 326 | let (b, g, r) = (px[0], px[1], px[2]); |
| 327 | let yellow = r > 200 && g > 150 && b < 80; |
| 328 | let white = r > 200 && g > 200 && b > 200; |
| 329 | if yellow || white { |
| 330 | out.push((i as u32 % W, yellow)); |
| 331 | } |
| 332 | } |
| 333 | out |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn karaoke_colours_the_word_being_spoken_and_no_other() { |
| 338 | |
| 339 | let cues = [cue("alpha beta gamma", 0.0, 3.0)]; |
| 340 | let px = inked(&style(CaptionAnim::Karaoke), &cues, 40); |
| 341 | let yellow: Vec<u32> = px.iter().filter(|p| p.1).map(|p| p.0).collect(); |
| 342 | let white: Vec<u32> = px.iter().filter(|p| !p.1).map(|p| p.0).collect(); |
| 343 | assert!(yellow.len() > 100, "the second word has highlight pixels: {}", yellow.len()); |
| 344 | let (ymin, ymax) = (*yellow.iter().min().unwrap(), *yellow.iter().max().unwrap()); |
| 345 | assert!(white.iter().any(|&x| x < ymin), "the first word is white, left of the highlight"); |
| 346 | assert!(white.iter().any(|&x| x > ymax), "the third word is white, right of the highlight"); |
| 347 | assert!(!white.iter().any(|&x| x > ymin && x < ymax), "no white ink inside the highlighted word"); |
| 348 | |
| 349 | let first = inked(&style(CaptionAnim::Karaoke), &cues, 2); |
| 350 | let first_max = first.iter().filter(|p| p.1).map(|p| p.0).max().unwrap(); |
| 351 | assert!(first_max < ymin, "the highlight moved right from word 1 to word 2"); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn a_highlight_box_puts_the_colour_behind_white_text() { |
| 356 | let mut s = style(CaptionAnim::Karaoke); |
| 357 | s.caption.hl_box = true; |
| 358 | let cues = [cue("alpha beta gamma", 0.0, 3.0)]; |
| 359 | let px = inked(&s, &cues, 40); |
| 360 | let yellow = px.iter().filter(|p| p.1).count(); |
| 361 | let white = px.iter().filter(|p| !p.1).count(); |
| 362 | assert!(yellow > 300, "a box is a lot of colour: {yellow}"); |
| 363 | assert!(white > 100, "the words are white"); |
| 364 | } |
| 365 | |
| 366 | #[test] |
| 367 | fn a_pop_is_biggest_when_the_word_begins_and_settled_after_200_ms() { |
| 368 | assert_eq!(pop_scale(0), POP_FROM); |
| 369 | assert!(pop_scale(0) > pop_scale(200_000)); |
| 370 | assert_eq!(pop_scale(200_000), 1.0); |
| 371 | assert!(pop_scale(60_000) < pop_scale(0) && pop_scale(60_000) > 1.0); |
| 372 | |
| 373 | let cues = [cue("alpha beta gamma", 0.0, 3.0)]; |
| 374 | let blocks = plan(&style(CaptionAnim::Pop), &cues, &[0], 27, FPS); |
| 375 | let fx = &blocks[0].fx; |
| 376 | assert_eq!(fx.len(), 3); |
| 377 | assert!(!fx[0].hidden && fx[0].scale == 1.0); |
| 378 | assert!(!fx[1].hidden && fx[1].scale > 1.2); |
| 379 | assert!(fx[2].hidden, "gamma is not spoken yet"); |
| 380 | } |
| 381 | |
| 382 | #[test] |
| 383 | fn bounce_lifts_the_spoken_word_and_eases_down() { |
| 384 | let (s0, l0) = bounce_fx(0); |
| 385 | let (_, l1) = bounce_fx(100_000); |
| 386 | let (_, l2) = bounce_fx(400_000); |
| 387 | assert!(s0 > 1.0 && l0 > l1 && l1 > l2 && l2 == 0.0); |
| 388 | } |
| 389 | |
| 390 | #[test] |
| 391 | fn one_word_mode_draws_only_the_word_being_spoken() { |
| 392 | let cues = [cue("alpha beta gamma", 0.0, 3.0)]; |
| 393 | let s = style(CaptionAnim::Word); |
| 394 | let blocks = plan(&s, &cues, &[0], 40, FPS); |
| 395 | assert_eq!(blocks.len(), 1); |
| 396 | assert_eq!(blocks[0].lines, ["beta"]); |
| 397 | assert_eq!(blocks[0].fx.len(), 1); |
| 398 | |
| 399 | let px = inked(&s, &cues, 40); |
| 400 | let (min, max) = (px.iter().map(|p| p.0).min().unwrap(), px.iter().map(|p| p.0).max().unwrap()); |
| 401 | let all = inked(&style(CaptionAnim::Karaoke), &cues, 40); |
| 402 | let (amin, amax) = (all.iter().map(|p| p.0).min().unwrap(), all.iter().map(|p| p.0).max().unwrap()); |
| 403 | assert!(max - min < (amax - amin) / 2 + 20, "one word {min}..{max} vs the line {amin}..{amax}"); |
| 404 | } |
| 405 | |
| 406 | #[test] |
| 407 | fn chunks_follow_the_word_times() { |
| 408 | let w = |s: f64, e: f64, t: &str| TimedWord { start_us: (s * 1e6) as i64, end_us: (e * 1e6) as i64, text: t.into() }; |
| 409 | let words = [ |
| 410 | w(0.0, 0.3, "one"), w(0.3, 0.6, "two"), w(0.6, 0.9, "three"), w(0.9, 1.2, "four"), |
| 411 | w(3.0, 3.3, "five"), w(3.3, 3.6, "six"), |
| 412 | ]; |
| 413 | let texts = |c: Vec<Vec<TimedWord>>| -> Vec<String> { |
| 414 | c.iter().map(|r| r.iter().map(|w| w.text.as_str()).collect::<Vec<_>>().join(" ")).collect() |
| 415 | }; |
| 416 | assert_eq!(texts(chunk_words(&words, 3)), ["one two three", "four", "five six"], "a pause ends a chunk early"); |
| 417 | assert_eq!(texts(chunk_words(&words, 0)), ["one two three four", "five six"]); |
| 418 | assert_eq!(texts(chunk_words(&words, 1)).len(), 6); |
| 419 | let chunks = chunk_words(&words, 3); |
| 420 | assert_eq!(chunks[2][0].start_us, 3_000_000, "words keep their own times"); |
| 421 | |
| 422 | let cues = [cue("one two three four five six", 0.0, 3.6)]; |
| 423 | let mut s = style(CaptionAnim::Karaoke); |
| 424 | s.caption.words_per_line = 2; |
| 425 | let at = |f: u32| plan(&s, &cues, &[0], f, FPS)[0].lines.clone(); |
| 426 | assert_eq!(at(0), ["one two"]); |
| 427 | assert_eq!(at(60), ["three four"]); |
| 428 | } |
| 429 | |
| 430 | #[test] |
| 431 | fn a_cue_without_word_times_spreads_its_words_by_letters() { |
| 432 | let words = cue_words(&cue("a bbb cc", 1.0, 2.2)); |
| 433 | assert_eq!(words.len(), 3); |
| 434 | assert_eq!(words[0].start_us, 1_000_000); |
| 435 | assert_eq!(words.last().unwrap().end_us, 2_200_000); |
| 436 | assert!(words.windows(2).all(|p| p[0].end_us == p[1].start_us), "contiguous"); |
| 437 | let span = |k: usize| words[k].end_us - words[k].start_us; |
| 438 | assert!(span(1) > span(2) && span(2) > span(0), "longer words are spoken for longer"); |
| 439 | assert!(cue_words(&cue(" ", 0.0, 1.0)).is_empty()); |
| 440 | |
| 441 | assert_eq!(cue_words(&cue("x y", 1.0, 1.0)).len(), 2); |
| 442 | } |
| 443 | |
| 444 | #[test] |
| 445 | fn karaoke_follows_real_word_times_that_are_uneven() { |
| 446 | |
| 447 | |
| 448 | let mut c = cue("extraordinarily big cat", 0.0, 3.0); |
| 449 | c.words = vec![ |
| 450 | (200_000, 2_500_000, "extraordinarily".into()), |
| 451 | (2_500_000, 2_800_000, "big".into()), |
| 452 | (2_800_000, 3_000_000, "cat".into()), |
| 453 | ]; |
| 454 | assert!(c.real_words().is_some()); |
| 455 | let s = style(CaptionAnim::Karaoke); |
| 456 | let active = |frame: u32| { |
| 457 | let fx = &plan(&s, &[c.clone()], &[0], frame, FPS)[0].fx; |
| 458 | fx.iter().position(|f| f.highlight.is_some()) |
| 459 | }; |
| 460 | assert_eq!(active(58), Some(0), "2.32 s: still the long word (letter spacing would say big)"); |
| 461 | assert_eq!(active(64), Some(1), "2.56 s: big"); |
| 462 | assert_eq!(active(71), Some(2), "2.84 s: cat"); |
| 463 | |
| 464 | let mut retyped = c.clone(); |
| 465 | retyped.text = "extraordinarily small cat".into(); |
| 466 | assert!(retyped.real_words().is_none()); |
| 467 | |
| 468 | retyped.text = "Extraordinarily big cat".into(); |
| 469 | assert!(retyped.real_words().is_some()); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn real_words_are_moved_and_clipped_with_their_cue() { |
| 474 | use crate::subtitle::map_words; |
| 475 | let w = vec![(1_000_000, 2_000_000, "a".to_string()), (2_000_000, 3_000_000, "b".to_string())]; |
| 476 | |
| 477 | let m = map_words(&w, (1_000_000, 3_000_000), (11_000_000, 13_000_000)); |
| 478 | assert_eq!((m[0].0, m[1].1), (11_000_000, 13_000_000)); |
| 479 | |
| 480 | let m = map_words(&w, (1_000_000, 3_000_000), (0, 1_000_000)); |
| 481 | assert_eq!((m[0].1, m[1].1), (500_000, 1_000_000)); |
| 482 | |
| 483 | |
| 484 | let m = map_words(&w, (2_000_000, 3_000_000), (0, 1_000_000)); |
| 485 | assert_eq!(m.len(), 2); |
| 486 | assert_eq!((m[0].0, m[0].1), (0, 0)); |
| 487 | assert_eq!((m[1].0, m[1].1), (0, 1_000_000)); |
| 488 | } |
| 489 | |
| 490 | #[test] |
| 491 | fn a_cue_cut_by_a_range_end_drops_the_words_past_it_and_clamps_the_last() { |
| 492 | use crate::subtitle::map_words; |
| 493 | let w = vec![ |
| 494 | (0, 1_000_000, "a".to_string()), |
| 495 | (1_000_000, 3_000_000, "b".to_string()), |
| 496 | (3_000_000, 4_000_000, "c".to_string()), |
| 497 | ]; |
| 498 | |
| 499 | let m = map_words(&w, (0, 2_000_000), (5_000_000, 7_000_000)); |
| 500 | assert_eq!(m.iter().map(|x| x.2.as_str()).collect::<Vec<_>>(), ["a", "b"]); |
| 501 | assert_eq!((m[1].0, m[1].1), (6_000_000, 7_000_000), "the last word ends at the cue's end"); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn the_plain_style_never_reads_word_times() { |
| 506 | let plain = TextStyle::default(); |
| 507 | let bare = cue("alpha beta gamma", 0.0, 3.0); |
| 508 | let mut timed = bare.clone(); |
| 509 | timed.words = vec![ |
| 510 | (0, 200_000, "alpha".into()), |
| 511 | (200_000, 2_500_000, "beta".into()), |
| 512 | (2_500_000, 3_000_000, "gamma".into()), |
| 513 | ]; |
| 514 | for frame in [1, 20, 60] { |
| 515 | let a = plan(&plain, std::slice::from_ref(&bare), &[0], frame, FPS); |
| 516 | let b = plan(&plain, std::slice::from_ref(&timed), &[0], frame, FPS); |
| 517 | assert_eq!(a, b, "same blocks at frame {frame}"); |
| 518 | assert_eq!(overlay_blocks(&plain, W, H, &a), overlay_blocks(&plain, W, H, &b)); |
| 519 | } |
| 520 | |
| 521 | assert!(plain.caption.is_plain()); |
| 522 | } |
| 523 | |
| 524 | #[test] |
| 525 | fn the_default_style_draws_two_multi_line_cues_exactly_as_the_old_path_did() { |
| 526 | let cues = [cue("alpha beta\ngamma delta", 0.0, 3.0), cue("one\ntwo three", 0.0, 3.0)]; |
| 527 | let s = TextStyle::default(); |
| 528 | let old: Vec<Vec<String>> = |
| 529 | [0usize, 1].iter().map(|&i| cues[i].text.split('\n').map(str::to_string).collect()).collect(); |
| 530 | let blocks = plan(&s, &cues, &[0, 1], 10, FPS); |
| 531 | assert_eq!(overlay_blocks(&s, W, H, &blocks), overlay_stack(&s, W, H, &old)); |
| 532 | } |
| 533 | |
| 534 | #[test] |
| 535 | fn the_default_style_draws_the_static_cue_exactly() { |
| 536 | let cues = [cue("alpha beta\ngamma", 0.0, 3.0)]; |
| 537 | let s = TextStyle::default(); |
| 538 | assert!(s.caption.is_plain()); |
| 539 | let blocks = plan(&s, &cues, &[0], 10, FPS); |
| 540 | let lines = vec![vec!["alpha beta".to_string(), "gamma".to_string()]]; |
| 541 | assert_eq!(overlay_blocks(&s, W, H, &blocks), overlay_stack(&s, W, H, &lines)); |
| 542 | } |
| 543 | |
| 544 | #[test] |
| 545 | fn uppercase_applies_to_a_static_cue_and_keeps_its_lines() { |
| 546 | let cues = [cue("alpha beta\ngamma", 0.0, 3.0)]; |
| 547 | let mut s = style(CaptionAnim::None); |
| 548 | s.caption.uppercase = true; |
| 549 | let blocks = plan(&s, &cues, &[0], 10, FPS); |
| 550 | assert_eq!(blocks[0].lines, ["ALPHA BETA", "GAMMA"]); |
| 551 | } |
| 552 | |
| 553 | #[test] |
| 554 | fn a_pop_with_nothing_spoken_yet_draws_nothing() { |
| 555 | let cues = [cue("alpha", 2.0, 3.0)]; |
| 556 | |
| 557 | |
| 558 | assert!(plan(&style(CaptionAnim::Pop), &cues, &[0], 10, FPS).is_empty()); |
| 559 | } |
| 560 | |
| 561 | #[test] |
| 562 | fn the_key_changes_when_the_picture_would() { |
| 563 | let cues = [cue("alpha beta gamma", 0.0, 3.0)]; |
| 564 | let s = style(CaptionAnim::Karaoke); |
| 565 | let key = |f| blocks_key(&plan(&s, &cues, &[0], f, FPS)); |
| 566 | assert_eq!(key(40), key(41), "the same word, the same picture"); |
| 567 | assert_ne!(key(40), key(60), "another word, another picture"); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | fn painted(style: &TextStyle, cues: &[Cue], frame: u32) -> Vec<u8> { |
| 572 | let blocks = plan(style, cues, &[0], frame, FPS); |
| 573 | let mut canvas = vec![0u8; (W * H * 4) as usize]; |
| 574 | overlay_blocks(style, W, H, &blocks).blend_bgra(&mut canvas); |
| 575 | canvas |
| 576 | } |
| 577 | |
| 578 | #[test] |
| 579 | fn an_animated_caption_is_drawn_in_the_named_family() { |
| 580 | |
| 581 | let serif = ["DejaVu Serif", "Liberation Serif", "Noto Serif", "Tinos", "Times New Roman"] |
| 582 | .into_iter() |
| 583 | .find(|name| crate::text::family_face(name).is_some()); |
| 584 | let Some(serif) = serif else { |
| 585 | eprintln!("no serif family is installed: skipped"); |
| 586 | return; |
| 587 | }; |
| 588 | let cues = [cue("Serif words", 0.0, 3.0)]; |
| 589 | let bundled = painted(&style(CaptionAnim::Karaoke), &cues, 40); |
| 590 | let named = TextStyle { family: Some(serif.to_string()), ..style(CaptionAnim::Karaoke) }; |
| 591 | assert_ne!(painted(&named, &cues, 40), bundled, "{serif} draws its own letters"); |
| 592 | |
| 593 | let missing = TextStyle { family: Some("No Such Family 0451".to_string()), ..style(CaptionAnim::Karaoke) }; |
| 594 | assert_eq!(painted(&missing, &cues, 40), bundled, "a missing family falls back to the bundled face"); |
| 595 | } |
| 596 | } |