FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#1542: pin the captions.setStyle margin contract with a regression test; name a bad track in the error
notdezzi130633974966
diff --git a/crates/captions/src/burn.rs b/crates/captions/src/burn.rsindex 77c9bf848..463e9737b 100644--- a/crates/captions/src/burn.rs+++ b/crates/captions/src/burn.rs@@ -286,6 +286,29 @@ mod tests {assert!(o.x < 120, "{}", o.x);}+ #[test]+ fn margin_is_a_fraction_of_frame_height() {+ // #1542: on a portrait sequence, a bottom-anchored caption with margin 0.31 sits with+ // its bottom edge 0.31 of the frame height above the bottom; the top anchor mirrors it.+ let st = CaptionStyle { anchor: CaptionAnchor::Bottom, margin: 0.31, size: 78.0, ..Default::default() };+ let o = render_caption(&cap("Hello world"), &st, 1080, 1920).unwrap();+ // the overlay pads the content by the outline grow (the default outline is 0, so by 1 px)+ let bottom = (o.y as usize + o.h) as f32;+ let expected = 1920.0 - 0.31 * 1920.0;+ assert!((bottom - expected).abs() <= 2.0, "bottom edge {bottom}, expected {expected}");+ let st = CaptionStyle { anchor: CaptionAnchor::Top, margin: 0.31, size: 78.0, ..Default::default() };+ let o = render_caption(&cap("Hello world"), &st, 1080, 1920).unwrap();+ assert!((o.y as f32 - 0.31 * 1920.0).abs() <= 2.0, "top edge {}, expected {}", o.y, 0.31 * 1920.0);+ // margin 0 and the 0.45 clamp stay inside the frame+ for margin in [0.0, 0.31, 0.45, 9.0] {+ let st = CaptionStyle { anchor: CaptionAnchor::Bottom, margin, size: 78.0, ..Default::default() };+ let o = render_caption(&cap("Hello world"), &st, 1080, 1920).unwrap();+ // the outline-grow padding plus ceil rounding may reach 2 px past the edge; the+ // composite clips it+ assert!(o.y >= 0 && o.y as usize + o.h <= 1922, "margin {margin}: y {} h {}", o.y, o.h);+ }+ }+#[test]fn wraps_and_composites() {let long = "word ".repeat(60);diff --git a/crates/engine/src/captions.rs b/crates/engine/src/captions.rsindex 1566a596d..d048a5677 100644--- a/crates/engine/src/captions.rs+++ b/crates/engine/src/captions.rs@@ -299,7 +299,15 @@ pub fn commands() -> Vec<CommandSpec> {r##"{"track":id|"C1","font":str?,"size":f32?,"color":"#rrggbb[aa]"?,"background":bool?,"backgroundColor":"#rrggbbaa"?,"align":"left|center|right"?,"anchor":"top|middle|bottom"?,"margin":0..0.45 (fraction of frame height)?,"lineSpacing":f32?,"outline":f32?,"outlineColor":str?,"reset":bool?}"##,has_caption_track,|s, p| {- let tid = track_param(s, p).ok_or_else(|| bad("captions.setStyle", "no such caption track"))?;+ let tid = track_param(s, p).ok_or_else(|| {+ bad(+ "captions.setStyle",+ format!(+ "no such caption track ({}) — pass the track id from captions.list, or \"C1\"…",+ p.get("track").map(|v| v.to_string()).unwrap_or_else(|| "no `track` given".into())+ ),+ )+ })?;let p = p.clone();s.edit("Caption Style", |pr, st| {let seq = st.active_sequence.ok_or(EngineError::NoSequence)?;