From 75e258280290f6b766261293d7237efc349fda6b Mon Sep 17 00:00:00 2001 From: Mathieu Duponchelle Date: Fri, 4 Jul 2025 00:29:23 +0200 Subject: [PATCH] tttocea708/translate: improve logic for prepending spaces No space needs to be prepended after inserting a carriage return or clearing the screen. Part-of: --- .../closedcaption/src/tttocea708/translate.rs | 24 +++++++++++++++---- video/closedcaption/tests/tttocea708.rs | 6 ++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/video/closedcaption/src/tttocea708/translate.rs b/video/closedcaption/src/tttocea708/translate.rs index cbafb1028..65d739fcb 100644 --- a/video/closedcaption/src/tttocea708/translate.rs +++ b/video/closedcaption/src/tttocea708/translate.rs @@ -391,6 +391,7 @@ impl TextToCea708 { } self.pen_location.column = origin_column as u8; need_pen_location = true; + cleared = true; } } @@ -437,12 +438,17 @@ impl TextToCea708 { for (i, chunk) in line.chunks.iter().enumerate() { let (cr, mut prepend_space) = if i == 0 { - (line.carriage_return, true) + ( + line.carriage_return, + line.carriage_return.map(|cr| !cr).unwrap_or(!cleared), + ) } else { (Some(false), false) }; self.open_line(chunk, cr); + cleared = false; + if is_punctuation(&chunk.text) { prepend_space = false; } @@ -463,9 +469,7 @@ impl TextToCea708 { continue; } - let code = Code::from_char(c).unwrap_or(Code::Space); - self.service_writer.push_codes(&[code]); - self.pen_location.column += 1; + let mut push_code = true; if self.mode == Cea708Mode::RollUp { /* In roll-up mode, we introduce carriage returns automatically. @@ -488,8 +492,18 @@ impl TextToCea708 { self.pen_location.column = self.origin_column as u8; self.open_line(chunk, Some(true)); + + push_code = !c.is_ascii_whitespace(); } - } else if self.pen_location.column > 31 { + } + + if push_code { + let code = Code::from_char(c).unwrap_or(Code::Space); + self.service_writer.push_codes(&[code]); + self.pen_location.column += 1; + } + + if self.mode != Cea708Mode::RollUp && self.pen_location.column > 31 { if chars.peek().is_some() { gst::warning!(CAT, "Dropping characters after 32nd column: {}", c); } diff --git a/video/closedcaption/tests/tttocea708.rs b/video/closedcaption/tests/tttocea708.rs index 5a24223f7..ab98dd80e 100644 --- a/video/closedcaption/tests/tttocea708.rs +++ b/video/closedcaption/tests/tttocea708.rs @@ -56,7 +56,7 @@ static PADDING: [u8; 60] = [ ]; static POP_ON_PREAMBLE: [u8; 24] = [ - 0xff, 0x0d, 0x37, 0xfe, 0x88, 0x01, 0xfe, 0x8c, 0xfe, 0xfe, 0x99, 0x18, 0xfe, 0xe4, 0x32, 0xfe, + 0xff, 0x0c, 0x36, 0xfe, 0x88, 0x01, 0xfe, 0x8c, 0xfe, 0xfe, 0x99, 0x18, 0xfe, 0xe4, 0x32, 0xfe, 0x7e, 0x1f, 0xfe, 0x11, 0x92, 0xfe, 0x0e, 0x00, ]; @@ -81,8 +81,8 @@ fn test_tttocea708_one_timed_buffer_and_eos() { expected0[..3].copy_from_slice(&[0xfc, 0x94, 0x20]); expected0[6..30].copy_from_slice(&POP_ON_PREAMBLE); // Hello ETX - expected0[30..45].copy_from_slice(&[ - 0xfe, 0x20, 0x48, 0xfe, 0x65, 0x6c, 0xfe, 0x6c, 0x6f, 0xfe, 0x8b, 0x03, 0xfe, 0x03, 0x00, + expected0[30..42].copy_from_slice(&[ + 0xfe, 0x48, 0x65, 0xfe, 0x6c, 0x6c, 0xfe, 0x6f, 0x8b, 0xfe, 0x03, 0x03, ]); let mut expected1 = PADDING; expected1[..3].copy_from_slice(&[0xfc, 0x94, 0xae]);