Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Fit typewriter transition grid and caret to the layer's content (#3229)

NetterSilver19d0a78b3860
diff --git a/crates/render/src/transitions/special.rs b/crates/render/src/transitions/special.rs
index f214f41af..fe506aeb4 100644
--- a/crates/render/src/transitions/special.rs
+++ b/crates/render/src/transitions/special.rs
@@ -18,6 +18,19 @@ fn rot(x: f32, y: f32, deg: f32) -> (f32, f32) {
(x * c + y * s, -x * s + y * c)
}
+/// Pixel bounds `(x0, y0, x1, y1)` of everything not fully transparent in either frame.
+fn content_bounds(a: &Image, b: &Image) -> Option<(f32, f32, f32, f32)> {
+ let (mut x0, mut y0, mut x1, mut y1) = (usize::MAX, usize::MAX, 0, 0);
+ for (y, (ra, rb)) in a.px.chunks(a.w * 4).zip(b.px.chunks(b.w * 4)).enumerate() {
+ for (x, (pa, pb)) in ra.chunks(4).zip(rb.chunks(4)).enumerate() {
+ if pa.get(3).is_some_and(|v| *v > 0.0) || pb.get(3).is_some_and(|v| *v > 0.0) {
+ (x0, y0, x1, y1) = (x0.min(x), y0.min(y), x1.max(x + 1), y1.max(y + 1));
+ }
+ }
+ }
+ (x0 < x1 && y0 < y1).then_some((x0 as f32, y0 as f32, x1 as f32, y1 as f32))
+}
+
pub(super) fn apply(id: &str, t: &Tx) -> Option<Image> {
let p = t.p;
let (w, h) = (t.wf, t.hf);
@@ -165,15 +178,22 @@ pub(super) fn apply(id: &str, t: &Tx) -> Option<Image> {
let cursor = t.flag("cursor");
let n = cols * rows;
let cur = (p * n).floor();
- let (cw, ch) = (w / cols, h / rows);
+ // lay the grid over the visible content (a text layer is mostly transparent)
+ let Some((bx0, by0, bx1, by1)) = content_bounds(t.a, t.b) else {
+ return Some(t.a.clone().lerp(t.b, p));
+ };
+ let (cw, ch) = ((bx1 - bx0) / cols, (by1 - by0) / rows);
paint(t.w, t.h, |x, y| {
- let (ci, ri) = ((x / cw).floor().min(cols - 1.0), (y / ch).floor().min(rows - 1.0));
+ if x < bx0 || x >= bx1 || y < by0 || y >= by1 {
+ return if p < 0.5 { t.pa(x, y) } else { t.pb(x, y) };
+ }
+ let (ci, ri) = (((x - bx0) / cw).floor().min(cols - 1.0), ((y - by0) / ch).floor().min(rows - 1.0));
let i = ri * cols + ci;
if i < cur {
t.pb(x, y)
} else if i == cur && cursor {
// a caret block in the lower part of the cell
- let ly = (y - ri * ch) / ch;
+ let ly = (y - by0 - ri * ch) / ch;
if ly > 0.15 { over(t.pa(x, y), scale4([1.0, 1.0, 1.0, 1.0], 0.75)) } else { t.pa(x, y) }
} else {
t.pa(x, y)
diff --git a/crates/render/src/transitions/tests.rs b/crates/render/src/transitions/tests.rs
index e2f25a184..e4d9a295f 100644
--- a/crates/render/src/transitions/tests.rs
+++ b/crates/render/src/transitions/tests.rs
@@ -479,3 +479,40 @@ fn lens_blur_frame_time() {
before / after
);
}
+
+/// #3086: on a text layer the typewriter grid and caret follow the text, not the whole frame.
+#[test]
+fn typewriter_cursor_stays_on_the_text_block() {
+ let (x0, x1, y0, y1) = (40usize, 88usize, 30usize, 42usize);
+ let layer = paint(W, H, |x, y| {
+ let (xi, yi) = (x as usize, y as usize);
+ if (x0..x1).contains(&xi) && (y0..y1).contains(&yi) { [0.3, 0.3, 0.3, 1.0] } else { [0.0; 4] }
+ });
+ let def = transitions().find(|d| d.id == "typewriter").unwrap();
+ for rows in [1.0, 3.0] {
+ let mut e = def.instance();
+ e.params.get_mut("columns").unwrap().value = ParamValue::Float(8.0);
+ e.params.get_mut("rows").unwrap().value = ParamValue::Float(rows);
+ e.params.get_mut("cursor").unwrap().value = ParamValue::Bool(true);
+ for p in [0.3, 0.6] {
+ let m = apply(&e, &layer, &layer, p);
+ let (mut cx0, mut cx1, mut cy0, mut cy1) = (usize::MAX, 0, usize::MAX, 0);
+ for y in 0..H {
+ for x in 0..W {
+ let i = (y * W + x) * 4;
+ let (o, s) = (&m.px[i..i + 4], &layer.px[i..i + 4]);
+ if s[3] == 0.0 {
+ assert!(o[3] == 0.0, "rows={rows} p={p}: transparent pixel ({x}, {y}) painted");
+ }
+ if o.iter().zip(s).any(|(a, b)| (a - b).abs() > 1e-4) {
+ (cx0, cx1, cy0, cy1) = (cx0.min(x), cx1.max(x + 1), cy0.min(y), cy1.max(y + 1));
+ }
+ }
+ }
+ assert!(cx0 < cx1, "rows={rows} p={p}: no cursor visible");
+ assert!(cx0 >= x0 && cx1 <= x1 && cy0 >= y0 && cy1 <= y1, "rows={rows} p={p}: cursor x{cx0}..{cx1} y{cy0}..{cy1} outside block");
+ let max_h = ((y1 - y0) as f64 / rows).ceil() as usize;
+ assert!(cy1 - cy0 <= max_h, "rows={rows} p={p}: cursor {} px tall, row is {max_h}", cy1 - cy0);
+ }
+ }
+}