Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve: avoid caption word copies and empty zoom allocations

Richie McIlroyf613212d6bc9
diff --git a/crates/rendering/src/layers/captions.rs b/crates/rendering/src/layers/captions.rs
index a20a65ab0..6f6906194 100644
--- a/crates/rendering/src/layers/captions.rs
+++ b/crates/rendering/src/layers/captions.rs
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
-use cap_project::XY;
+use cap_project::{CaptionWord, XY};
use glyphon::cosmic_text::LayoutRunIter;
use glyphon::{
Attrs, Buffer, Cache, Color, Family, FontSystem, Metrics, Resolution, Shaping, SwashCache,
@@ -10,13 +10,6 @@ use wgpu::{Device, Queue, include_wgsl, util::DeviceExt};
use crate::{DecodedSegmentFrames, ProjectUniforms, RenderVideoConstants, parse_color_component};
-#[derive(Debug, Clone)]
-pub struct CaptionWord {
- pub text: String,
- pub start: f32,
- pub end: f32,
-}
-
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable, Debug)]
pub struct CaptionSettings {
@@ -518,16 +511,7 @@ impl CaptionsLayer {
} else {
joined_caption_text
};
- let caption_words: Vec<CaptionWord> = active
- .segment
- .words
- .iter()
- .map(|w| CaptionWord {
- text: w.text.clone(),
- start: w.start,
- end: w.end,
- })
- .collect();
+ let caption_words = &active.segment.words;
let fade_opacity = calculate_caption_fade(
current_time,
@@ -572,8 +556,8 @@ impl CaptionsLayer {
active_word_highlight_enabled && !caption_words.is_empty() && !use_pill_highlight;
let active_word_byte_range = if use_pill_highlight {
- find_active_word_index(current_time as f32, &caption_words)
- .and_then(|idx| word_byte_range(&caption_text, &caption_words, idx, uppercase))
+ find_active_word_index(current_time as f32, caption_words)
+ .and_then(|idx| word_byte_range(&caption_text, caption_words, idx, uppercase))
} else {
None
};
@@ -677,7 +661,7 @@ impl CaptionsLayer {
current_time as f32,
word,
idx,
- &caption_words,
+ caption_words,
word_transition_duration,
);
@@ -1180,7 +1164,10 @@ fn calculate_caption_bounce(current_time: f64, start: f64, end: f64, fade_durati
#[cfg(test)]
mod tests {
- use super::{caption_segment_effective_end, find_active_caption_segment};
+ use super::{
+ caption_segment_effective_end, find_active_caption_segment, find_active_word_index,
+ word_byte_range,
+ };
use cap_project::{CaptionTrackSegment, CaptionWord};
fn segment(start: f64, end: f64, words: Vec<CaptionWord>) -> CaptionTrackSegment {
@@ -1230,4 +1217,44 @@ mod tests {
// Still active while the (capped) word is on screen.
assert!(find_active_caption_segment(37.0, &segments, 0.2).is_some());
}
+
+ #[test]
+ fn active_word_selection_preserves_boundaries_and_gaps() {
+ let words = [word(0.1, 0.4), word(0.5, 0.9)];
+
+ for (time, expected) in [
+ (-0.1, 0),
+ (0.1, 0),
+ (0.4, 0),
+ (0.49, 0),
+ (0.5, 1),
+ (0.9, 1),
+ (1.2, 1),
+ ] {
+ assert_eq!(find_active_word_index(time, &words), Some(expected));
+ }
+ assert_eq!(find_active_word_index(0.5, &[]), None);
+ }
+
+ #[test]
+ fn word_ranges_preserve_repeated_words_and_unicode_uppercase() {
+ let words = ["ʼn", "Straße", "ʼn"].map(|text| CaptionWord {
+ text: text.to_string(),
+ start: 0.0,
+ end: 1.0,
+ });
+ let text = "ʼn Straße ʼn";
+
+ for (index, expected) in [(0, (0, 2)), (1, (3, 10)), (2, (11, 13))] {
+ assert_eq!(word_byte_range(text, &words, index, false), Some(expected));
+ }
+ for (index, expected) in [(0, (0, 3)), (1, (4, 11)), (2, (12, 15))] {
+ assert_eq!(
+ word_byte_range(&text.to_uppercase(), &words, index, true),
+ Some(expected)
+ );
+ }
+ assert_eq!(word_byte_range(text, &words, 3, false), None);
+ assert_eq!(word_byte_range("missing", &words, 0, false), None);
+ }
}
diff --git a/crates/rendering/src/zoom_spring.rs b/crates/rendering/src/zoom_spring.rs
index 76aa4763e..5904a0d7b 100644
--- a/crates/rendering/src/zoom_spring.rs
+++ b/crates/rendering/src/zoom_spring.rs
@@ -478,7 +478,11 @@ impl ZoomTransformTimeline {
let mut zoom_segments = zoom_segments.to_vec();
zoom_segments.sort_by(|a, b| a.start.total_cmp(&b.start).then(a.end.total_cmp(&b.end)));
- let time_map = build_time_map(timeline);
+ let time_map = if zoom_segments.is_empty() {
+ Vec::new()
+ } else {
+ build_time_map(timeline)
+ };
let clusters = zoom_segments
.iter()
.map(|segment| match segment.mode {
@@ -518,12 +522,7 @@ impl ZoomTransformTimeline {
let total_samples = (duration_secs * 1000.0 / STEP_MS).ceil() as usize + 2;
if zoom_segments.is_empty() {
return Self {
- samples: vec![TimelineSample {
- amount: 1.0,
- center: XY::new(0.5, 0.5),
- activity: 0.0,
- snapped: false,
- }],
+ samples: Vec::new(),
state: None,
zoom_segments,
clusters,
@@ -946,11 +945,35 @@ mod tests {
#[test]
fn empty_zoom_timeline_stays_constant_without_precompute_work() {
let mut timeline = timeline_for(&[], &CursorEvents::default(), 60.0 * 60.0);
- timeline.ensure_precomputed_until(60.0 * 60.0);
+ for seconds in [
+ f32::NEG_INFINITY,
+ -1.0,
+ -0.0,
+ 0.0,
+ 0.5,
+ 60.0 * 60.0,
+ f32::MAX,
+ f32::INFINITY,
+ f32::NAN,
+ ] {
+ timeline.ensure_precomputed_until(seconds);
+ timeline.precompute();
+ let sample = timeline.sample(seconds);
+ assert_eq!(
+ [
+ sample.t,
+ sample.bounds.top_left.x,
+ sample.bounds.top_left.y,
+ sample.bounds.bottom_right.x,
+ sample.bounds.bottom_right.y,
+ ]
+ .map(f64::to_bits),
+ [0.0, 0.0, 0.0, 1.0, 1.0].map(f64::to_bits)
+ );
+ assert!(!timeline.snapped_within(seconds, 0.0));
+ }
assert!(timeline.state.is_none());
- assert_eq!(timeline.samples.len(), 1);
- assert_eq!(timeline.sample(60.0 * 60.0).display_amount(), 1.0);
}
/// Max |value delta| and |slope delta| between adjacent 8ms sample