Skip to content

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

fix(captions): keep literal angle-bracket text in EBU STL exports

Nigel Ihza Firdausyf9f0268b6594
diff --git a/crates/captions/src/stl.rs b/crates/captions/src/stl.rs
index ad0123951..cc5d1e37d 100644
--- a/crates/captions/src/stl.rs
+++ b/crates/captions/src/stl.rs
@@ -215,15 +215,23 @@ fn encode_text(text: &str) -> Vec<u8> {
if ch == '<'
&& let Some(end) = rest.find('>')
{
- match rest[1..end].trim().to_ascii_lowercase().as_str() {
- "i" => out.push(0x80),
- "/i" => out.push(0x81),
- "u" => out.push(0x82),
- "/u" => out.push(0x83),
- _ => {}
+ let tag = &rest[1..end];
+ // Only a syntactically plausible tag is markup. Comparisons such as
+ // "2 < 3 > 1" and "x < y > z" must not lose their middle text.
+ let name = tag.strip_prefix('/').unwrap_or(tag);
+ let is_tag = name.as_bytes().first().is_some_and(u8::is_ascii_alphabetic)
+ && tag.trim() == tag;
+ if is_tag {
+ match tag.to_ascii_lowercase().as_str() {
+ "i" => out.push(0x80),
+ "/i" => out.push(0x81),
+ "u" => out.push(0x82),
+ "/u" => out.push(0x83),
+ _ => {}
+ }
+ rest = &rest[end + 1..];
+ continue;
}
- rest = &rest[end + 1..];
- continue;
}
if ch == '\n' {
out.push(0x8a);
@@ -451,6 +459,26 @@ mod tests {
assert!(parse(&b[..500]).is_err());
}
+ #[test]
+ fn literal_angle_brackets_are_not_discarded_as_markup() {
+ let literal = "2 < 3 > 1, x < y > z, and 5 <7> 6";
+ let doc = Document {
+ cues: vec![Cue {
+ start: FrameRate::FPS_25.tick_of(25),
+ end: FrameRate::FPS_25.tick_of(75),
+ text: literal.into(),
+ ..Default::default()
+ }],
+ ..Default::default()
+ };
+ let encoded = write(&doc, Some(FrameRate::FPS_25));
+ let decoded = parse(&encoded).expect("valid EBU STL");
+ assert_eq!(decoded.cues.len(), 1);
+ assert_eq!(decoded.cues[0].text, literal);
+ // Recognized styling still becomes STL control bytes.
+ assert_eq!(encode_text("<i>Hi</i>"), vec![0x80, b'H', b'i', 0x81]);
+ }
+
#[test]
fn programme_start_is_subtracted() {
let doc = Document {