tttocea708/translate: improve logic for prepending spaces

No space needs to be prepended after inserting a carriage return or
clearing the screen.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/2345>
This commit is contained in:
Mathieu Duponchelle 2025-07-04 00:29:23 +02:00 committed by GStreamer Marge Bot
parent 4ad8295ae3
commit 75e2582802
2 changed files with 22 additions and 8 deletions

View file

@ -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);
}

View file

@ -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]);