@@ -13,15 +13,14 @@ use std::{ffi::CStr, fs::File};
1313#[ cfg( windows) ]
1414use crate :: bindings:: _get_osfhandle;
1515
16- use super :: output:: { color_to_hex, is_utf16_charset, write_char, Writer } ;
16+ use super :: output:: { color_to_hex, find_charset_encoding , is_utf16_charset, write_char, Writer } ;
1717use super :: timing:: { get_scc_time_str, get_time_str} ;
1818use super :: { CCX_DTVCC_SCREENGRID_COLUMNS , CCX_DTVCC_SCREENGRID_ROWS } ;
1919use crate :: {
2020 bindings:: * ,
2121 current_fps,
2222 utils:: { is_false, is_true} ,
2323} ;
24- use encoding_rs:: Encoding ;
2524
2625use log:: { debug, warn} ;
2726
@@ -273,7 +272,7 @@ impl dtvcc_tv_screen {
273272 debug ! ( "Charset: {charset}" ) ;
274273
275274 // Look up the encoding by label (name)
276- if let Some ( encoding) = Encoding :: for_label ( charset. as_bytes ( ) ) {
275+ if let Some ( encoding) = find_charset_encoding ( charset) {
277276 let ( cow, _encoding_used, had_errors) = encoding. decode ( & buf) ;
278277 if had_errors {
279278 println ! ( "Warning: Had errors during encoding from {charset} to UTF-8" ) ;
@@ -282,6 +281,18 @@ impl dtvcc_tv_screen {
282281 println ! ( "Warning: Encoding specified ({}) does not match encoding detected ({})" , charset, _encoding_used. name( ) ) ;
283282 }
284283 writer. write_to_file ( cow. as_bytes ( ) ) ?;
284+ } else {
285+ // iconv accepted the name but encoding_rs can't convert from it:
286+ // write the text unconverted instead of dropping it.
287+ static WARNED : std:: sync:: Once = std:: sync:: Once :: new ( ) ;
288+ WARNED . call_once ( || {
289+ if let Some ( logger) = lib_ccxr:: util:: log:: logger ( ) {
290+ logger. log_info ( & format_args ! (
291+ "[CEA-708] Can't convert from charset \" {charset}\" , writing the text unconverted\n "
292+ ) ) ;
293+ }
294+ } ) ;
295+ writer. write_to_file ( & buf) ?;
285296 }
286297 }
287298 } else {
@@ -833,6 +844,78 @@ mod test {
833844 ) ;
834845 }
835846
847+ /// Runs write_row on row 0 of `screen` with a --service charset and
848+ /// returns what was written.
849+ #[ cfg( unix) ]
850+ fn write_row_with_charset ( screen : & dtvcc_tv_screen , charset : & str ) -> Vec < u8 > {
851+ use std:: ffi:: CString ;
852+ use std:: io:: { Read , Seek , SeekFrom } ;
853+ use std:: os:: unix:: io:: AsRawFd ;
854+ use tempfile:: tempfile;
855+
856+ let charset = CString :: new ( charset) . unwrap ( ) ;
857+ let mut writer_ctx = get_zero_allocated_obj :: < dtvcc_writer_ctx > ( ) ;
858+ let transcript_settings = get_zero_allocated_obj :: < ccx_encoders_transcript_format > ( ) ;
859+ let mut temp_file = tempfile ( ) . expect ( "Failed to create temp file" ) ;
860+ writer_ctx. fd = temp_file. as_raw_fd ( ) ;
861+ writer_ctx. filename = std:: ptr:: null_mut ( ) ;
862+ writer_ctx. charset = charset. as_ptr ( ) as * mut _ ;
863+ // dtvcc_writer_init() opened an iconv descriptor for the charset
864+ writer_ctx. cd = 1 as iconv_t ;
865+
866+ let mut counter = 0u32 ;
867+ let mut writer = Writer :: new (
868+ & mut counter,
869+ 0 ,
870+ ccx_output_format:: CCX_OF_SRT ,
871+ & mut writer_ctx,
872+ 1 ,
873+ & transcript_settings,
874+ 0 ,
875+ b"\r \n " ,
876+ ) ;
877+ screen
878+ . write_row ( & mut writer, 0 , false )
879+ . expect ( "write_row should succeed" ) ;
880+
881+ temp_file. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
882+ let mut output = Vec :: new ( ) ;
883+ temp_file. read_to_end ( & mut output) . unwrap ( ) ;
884+ output
885+ }
886+
887+ #[ test]
888+ #[ cfg( unix) ]
889+ fn test_write_row_iconv_charset_name ( ) {
890+ // "Hi 가" in EUC-KR / CP949: 가 is 0xB0A1
891+ let mut screen = get_zero_allocated_obj :: < dtvcc_tv_screen > ( ) ;
892+ screen. chars [ 0 ] [ 0 ] = dtvcc_symbol:: new ( b'H' as u16 ) ;
893+ screen. chars [ 0 ] [ 1 ] = dtvcc_symbol:: new ( b'i' as u16 ) ;
894+ screen. chars [ 0 ] [ 2 ] = dtvcc_symbol:: new ( b' ' as u16 ) ;
895+ screen. chars [ 0 ] [ 3 ] = dtvcc_symbol:: new ( 0xB0A1 ) ;
896+
897+ for charset in [ "EUC-KR" , "CP949" , "UHC" , "EUCKR" ] {
898+ let output = write_row_with_charset ( & screen, charset) ;
899+ assert_eq ! (
900+ String :: from_utf8_lossy( & output) ,
901+ "Hi 가" ,
902+ "charset {charset}"
903+ ) ;
904+ }
905+ }
906+
907+ #[ test]
908+ #[ cfg( unix) ]
909+ fn test_write_row_unknown_charset_keeps_text ( ) {
910+ let mut screen = get_zero_allocated_obj :: < dtvcc_tv_screen > ( ) ;
911+ screen. chars [ 0 ] [ 0 ] = dtvcc_symbol:: new ( b'H' as u16 ) ;
912+ screen. chars [ 0 ] [ 1 ] = dtvcc_symbol:: new ( b'i' as u16 ) ;
913+
914+ // iconv knows JOHAB, encoding_rs doesn't
915+ let output = write_row_with_charset ( & screen, "JOHAB" ) ;
916+ assert_eq ! ( output, b"Hi" ) ;
917+ }
918+
836919 #[ test]
837920 #[ cfg( unix) ]
838921 fn test_writer_output_with_valid_fd ( ) {
0 commit comments