1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Samuel Hocevar <sam@zoy.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Bernie Purcell <bitmap@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_plugin.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int OpenDecoder ( vlc_object_t * );
41 static void CloseDecoder ( vlc_object_t * );
43 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
44 static subpicture_t *ParseText ( decoder_t *, block_t * );
45 static char *StripTags ( char * );
46 static char *CreateHtmlSubtitle( int *pi_align, char * );
49 /*****************************************************************************
51 *****************************************************************************/
52 static const char *const ppsz_encodings[] = {
96 static const char *const ppsz_encoding_names[] = {
98 N_("Universal (UTF-8)"),
99 N_("Universal (UTF-16)"),
100 N_("Universal (big endian UTF-16)"),
101 N_("Universal (little endian UTF-16)"),
102 N_("Universal, Chinese (GB18030)"),
104 /* ISO 8859 and the likes */
106 N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
107 N_("Western European (Windows-1252)"),
109 N_("Eastern European (Latin-2)"),
110 N_("Eastern European (Windows-1250)"),
112 N_("Esperanto (Latin-3)"),
114 N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
116 N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
117 N_("Russian (KOI8-R)"),
118 N_("Ukrainian (KOI8-U)"),
120 N_("Arabic (ISO 8859-6)"),
121 N_("Arabic (Windows-1256)"),
123 N_("Greek (ISO 8859-7)"),
124 N_("Greek (Windows-1253)"),
126 N_("Hebrew (ISO 8859-8)"),
127 N_("Hebrew (Windows-1255)"),
129 N_("Turkish (ISO 8859-9)"),
130 N_("Turkish (Windows-1254)"),
133 N_("Thai (TIS 620-2533/ISO 8859-11)"),
134 N_("Thai (Windows-874)"),
136 N_("Baltic (Latin-7)"),
137 N_("Baltic (Windows-1257)"),
138 /* 12 -> /dev/null */
140 N_("Celtic (Latin-8)"),
143 N_("South-Eastern European (Latin-10)"),
145 N_("Simplified Chinese (ISO-2022-CN-EXT)"),
146 N_("Simplified Chinese Unix (EUC-CN)"),
147 N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
148 N_("Japanese Unix (EUC-JP)"),
149 N_("Japanese (Shift JIS)"),
150 N_("Korean (EUC-KR/CP949)"),
151 N_("Korean (ISO-2022-KR)"),
152 N_("Traditional Chinese (Big5)"),
153 N_("Traditional Chinese Unix (EUC-TW)"),
154 N_("Hong-Kong Supplementary (HKSCS)"),
156 N_("Vietnamese (VISCII)"),
157 N_("Vietnamese (Windows-1258)"),
160 SSA supports charset selection.
161 The following known charsets are used:
163 0 = Ansi - Western European
168 128 = Japanese (Shift JIS)
171 134 = GB2312 Simplified Chinese
172 136 = Big5 Traditional Chinese
179 204 = Russian (Cyrillic)
181 238 = Eastern European
185 static const int pi_justification[] = { 0, 1, 2 };
186 static const char *const ppsz_justification_text[] = {
187 N_("Center"),N_("Left"),N_("Right")};
189 #define ENCODING_TEXT N_("Subtitles text encoding")
190 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
191 #define ALIGN_TEXT N_("Subtitles justification")
192 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
193 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
194 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
195 "UTF-8 encoding within subtitles files.")
196 #define FORMAT_TEXT N_("Formatted Subtitles")
197 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
198 "VLC partly implements this, but you can choose to disable all formatting.")
202 set_shortname( N_("Subtitles"))
203 set_description( N_("Text subtitles decoder") )
204 set_capability( "decoder", 50 )
205 set_callbacks( OpenDecoder, CloseDecoder )
206 set_category( CAT_INPUT )
207 set_subcategory( SUBCAT_INPUT_SCODEC )
209 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
211 change_integer_list( pi_justification, ppsz_justification_text, NULL )
212 add_string( "subsdec-encoding", "", NULL,
213 ENCODING_TEXT, ENCODING_LONGTEXT, false )
214 change_string_list( ppsz_encodings, ppsz_encoding_names, 0 )
215 add_bool( "subsdec-autodetect-utf8", true, NULL,
216 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
217 add_bool( "subsdec-formatted", true, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
221 /*****************************************************************************
222 * OpenDecoder: probe the decoder and return score
223 *****************************************************************************
224 * Tries to launch a decoder and return score so that the interface is able
226 *****************************************************************************/
227 static int OpenDecoder( vlc_object_t *p_this )
229 decoder_t *p_dec = (decoder_t*)p_this;
230 decoder_sys_t *p_sys;
232 switch( p_dec->fmt_in.i_codec )
236 case VLC_CODEC_ITU_T140:
242 p_dec->pf_decode_sub = DecodeBlock;
243 p_dec->fmt_out.i_cat = SPU_ES;
244 p_dec->fmt_out.i_codec = 0;
246 /* Allocate the memory needed to store the decoder's structure */
247 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
253 p_sys->iconv_handle = (vlc_iconv_t)-1;
254 p_sys->b_autodetect_utf8 = false;
255 p_sys->b_ass = false;
256 p_sys->i_original_height = -1;
257 p_sys->i_original_width = -1;
258 TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
259 TAB_INIT( p_sys->i_images, p_sys->pp_images );
261 char *psz_charset = NULL;
263 /* First try demux-specified encoding */
264 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
265 psz_charset = strdup( "UTF-8" ); /* IUT T.140 is always using UTF-8 */
267 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
269 psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
270 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
271 p_dec->fmt_in.subs.psz_encoding ?
272 p_dec->fmt_in.subs.psz_encoding : "not specified");
275 /* Second, try configured encoding */
276 if (psz_charset == NULL)
278 psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
279 msg_Dbg (p_dec, "trying configured character encoding: %s",
280 psz_charset ? psz_charset : "not specified");
283 /* Third, try "local" encoding with optional UTF-8 autodetection */
284 if (psz_charset == NULL)
287 The Windows ANSI code page most commonly used for this language.
288 VLC uses this as a guess of the subtitle files character set
289 (if UTF-8 and UTF-16 autodetection fails).
290 Western European languages normally use "CP1252", which is a
291 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
292 Other scripts use other code pages.
294 This MUST be a valid iconv character set. If unsure, please refer
295 the VideoLAN translators mailing list. */
296 const char *acp = vlc_pgettext("GetACP", "CP1252");
298 psz_charset = strdup (acp);
299 msg_Dbg (p_dec, "trying default character encoding: %s",
300 psz_charset ? psz_charset : "not specified");
302 if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
304 msg_Dbg (p_dec, "using automatic UTF-8 detection");
305 p_sys->b_autodetect_utf8 = true;
309 /* Forth, don't do character decoding, i.e. assume UTF-8 */
310 if (psz_charset == NULL)
312 psz_charset = strdup ("UTF-8");
313 msg_Dbg (p_dec, "using UTF-8 character encoding" );
316 if ((psz_charset != NULL)
317 && strcasecmp (psz_charset, "UTF-8")
318 && strcasecmp (psz_charset, "utf8"))
320 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
321 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
322 msg_Err (p_dec, "cannot convert from %s: %m", psz_charset);
326 p_sys->i_align = var_CreateGetInteger( p_dec, "subsdec-align" );
328 if( p_dec->fmt_in.i_codec == VLC_CODEC_SSA
329 && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
331 if( p_dec->fmt_in.i_extra > 0 )
332 ParseSSAHeader( p_dec );
338 /****************************************************************************
339 * DecodeBlock: the whole thing
340 ****************************************************************************
341 * This function must be fed with complete subtitles units.
342 ****************************************************************************/
343 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
348 if( !pp_block || *pp_block == NULL )
352 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
354 block_Release( p_block );
358 p_spu = ParseText( p_dec, p_block );
360 block_Release( p_block );
366 /*****************************************************************************
367 * CloseDecoder: clean up the decoder
368 *****************************************************************************/
369 static void CloseDecoder( vlc_object_t *p_this )
371 decoder_t *p_dec = (decoder_t *)p_this;
372 decoder_sys_t *p_sys = p_dec->p_sys;
374 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
375 vlc_iconv_close( p_sys->iconv_handle );
377 if( p_sys->pp_ssa_styles )
380 for( i = 0; i < p_sys->i_ssa_styles; i++ )
382 if( !p_sys->pp_ssa_styles[i] )
385 free( p_sys->pp_ssa_styles[i]->psz_stylename );
386 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
387 free( p_sys->pp_ssa_styles[i] );
389 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
391 if( p_sys->pp_images )
394 for( i = 0; i < p_sys->i_images; i++ )
396 if( !p_sys->pp_images[i] )
399 if( p_sys->pp_images[i]->p_pic )
400 picture_Release( p_sys->pp_images[i]->p_pic );
401 free( p_sys->pp_images[i]->psz_filename );
403 free( p_sys->pp_images[i] );
405 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
411 /*****************************************************************************
412 * ParseText: parse an text subtitle packet and send it to the video output
413 *****************************************************************************/
414 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
416 decoder_sys_t *p_sys = p_dec->p_sys;
417 subpicture_t *p_spu = NULL;
418 char *psz_subtitle = NULL;
421 /* We cannot display a subpicture with no date */
422 if( p_block->i_pts <= VLC_TS_INVALID )
424 msg_Warn( p_dec, "subtitle without a date" );
428 /* Check validity of packet data */
429 /* An "empty" line containing only \0 can be used to force
430 and ephemer picture from the screen */
431 if( p_block->i_buffer < 1 )
433 msg_Warn( p_dec, "no subtitle data" );
437 /* Should be resiliant against bad subtitles */
438 psz_subtitle = malloc( p_block->i_buffer + 1 );
439 if( psz_subtitle == NULL )
441 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
442 psz_subtitle[p_block->i_buffer] = '\0';
444 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
446 if (EnsureUTF8( psz_subtitle ) == NULL)
448 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
449 "Try manually setting a character-encoding "
450 "before you open the file." );
456 if( p_sys->b_autodetect_utf8 )
458 if( IsUTF8( psz_subtitle ) == NULL )
460 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
461 "disabling UTF-8 subtitles autodetection" );
462 p_sys->b_autodetect_utf8 = false;
466 if( !p_sys->b_autodetect_utf8 )
468 size_t inbytes_left = strlen( psz_subtitle );
469 size_t outbytes_left = 6 * inbytes_left;
470 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
471 char *psz_convert_buffer_out = psz_new_subtitle;
472 const char *psz_convert_buffer_in = psz_subtitle;
474 size_t ret = vlc_iconv( p_sys->iconv_handle,
475 &psz_convert_buffer_in, &inbytes_left,
476 &psz_convert_buffer_out, &outbytes_left );
478 *psz_convert_buffer_out++ = '\0';
479 free( psz_subtitle );
481 if( ( ret == (size_t)(-1) ) || inbytes_left )
483 free( psz_new_subtitle );
484 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
485 "Try manually setting a character-encoding "
486 "before you open the file." );
490 psz_subtitle = realloc( psz_new_subtitle,
491 psz_convert_buffer_out - psz_new_subtitle );
493 psz_subtitle = psz_new_subtitle;
497 /* Create the subpicture unit */
498 p_spu = decoder_NewSubpicture( p_dec, NULL );
501 msg_Warn( p_dec, "can't get spu buffer" );
502 free( psz_subtitle );
506 /* Create a new subpicture region */
507 memset( &fmt, 0, sizeof(video_format_t) );
508 fmt.i_chroma = VLC_CODEC_TEXT;
509 fmt.i_width = fmt.i_height = 0;
510 fmt.i_x_offset = fmt.i_y_offset = 0;
511 p_spu->p_region = subpicture_region_New( &fmt );
512 if( !p_spu->p_region )
514 msg_Err( p_dec, "cannot allocate SPU region" );
515 free( psz_subtitle );
516 decoder_DeleteSubpicture( p_dec, p_spu );
520 /* Decode and format the subpicture unit */
521 if( p_dec->fmt_in.i_codec != VLC_CODEC_SSA )
523 /* Normal text subs, easy markup */
524 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
525 p_spu->p_region->i_x = p_sys->i_align ? 20 : 0;
526 p_spu->p_region->i_y = 10;
528 /* Remove formatting from string */
530 p_spu->p_region->psz_text = StripTags( psz_subtitle );
531 if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
533 p_spu->p_region->psz_html = CreateHtmlSubtitle( &p_spu->p_region->i_align, psz_subtitle );
536 p_spu->i_start = p_block->i_pts;
537 p_spu->i_stop = p_block->i_pts + p_block->i_length;
538 p_spu->b_ephemer = (p_block->i_length == 0);
539 p_spu->b_absolute = false;
543 /* Decode SSA/USF strings */
544 ParseSSAString( p_dec, psz_subtitle, p_spu );
546 p_spu->i_start = p_block->i_pts;
547 p_spu->i_stop = p_block->i_pts + p_block->i_length;
548 p_spu->b_ephemer = (p_block->i_length == 0);
549 p_spu->b_absolute = false;
550 p_spu->i_original_picture_width = p_sys->i_original_width;
551 p_spu->i_original_picture_height = p_sys->i_original_height;
553 free( psz_subtitle );
558 char* GotoNextLine( char *psz_text )
560 char *p_newline = psz_text;
562 while( p_newline[0] != '\0' )
564 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
567 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
576 /* Function now handles tags with attribute values, and tries
577 * to deal with &' commands too. It no longer modifies the string
578 * in place, so that the original text can be reused
580 static char *StripTags( char *psz_subtitle )
582 char *psz_text_start;
585 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
586 if( !psz_text_start )
589 while( *psz_subtitle )
591 if( *psz_subtitle == '<' )
593 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
596 psz_subtitle += strcspn( psz_subtitle, ">" );
598 else if( *psz_subtitle == '&' )
600 if( !strncasecmp( psz_subtitle, "<", 4 ))
603 psz_subtitle += strcspn( psz_subtitle, ";" );
605 else if( !strncasecmp( psz_subtitle, ">", 4 ))
608 psz_subtitle += strcspn( psz_subtitle, ";" );
610 else if( !strncasecmp( psz_subtitle, "&", 5 ))
613 psz_subtitle += strcspn( psz_subtitle, ";" );
615 else if( !strncasecmp( psz_subtitle, """, 6 ))
618 psz_subtitle += strcspn( psz_subtitle, ";" );
622 /* Assume it is just a normal ampersand */
628 *psz_text++ = *psz_subtitle;
634 char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
635 if( psz ) psz_text_start = psz;
637 return psz_text_start;
640 /* Try to respect any style tags present in the subtitle string. The main
641 * problem here is a lack of adequate specs for the subtitle formats.
642 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
643 * SAMI has a detailed spec, but extensive rework is needed in the demux
644 * code to prevent all this style information being excised, as it presently
646 * That leaves the others - none of which were (I guess) originally intended
647 * to be carrying style information. Over time people have used them that way.
648 * In the absence of specifications from which to work, the tags supported
649 * have been restricted to the simple set permitted by the USF DTD, ie. :
650 * Basic: <br>, <i>, <b>, <u>, <s>
662 * There is also the further restriction that the subtitle be well-formed
663 * as an XML entity, ie. the HTML sentence:
664 * <b><i>Bold and Italics</b></i>
665 * doesn't qualify because the tags aren't nested one inside the other.
666 * <text> tags are automatically added to the output to ensure
668 * If the text doesn't qualify for any reason, a NULL string is
669 * returned, and the rendering engine will fall back to the
670 * plain text version of the subtitle.
672 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
674 const int i_len = strlen(psz_text);
676 strncpy( *ppsz_html, psz_text, i_max );
677 *ppsz_html += __MIN(i_max,i_len);
680 static void HtmlPut( char **ppsz_html, const char *psz_text )
682 strcpy( *ppsz_html, psz_text );
683 *ppsz_html += strlen(psz_text);
685 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
687 HtmlPut( ppsz_html, psz_text );
688 *ppsz_subtitle += strlen(psz_text);
691 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
694 char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
701 size_t i_buf_size = strlen( psz_subtitle ) + 100;
702 char *psz_html_start = malloc( i_buf_size );
703 char *psz_html = psz_html_start;
704 if( psz_html_start == NULL )
711 bool b_has_align = false;
713 HtmlPut( &psz_html, "<text>" );
716 while( *psz_subtitle )
718 if( *psz_subtitle == '\n' )
720 HtmlPut( &psz_html, "<br/>" );
723 else if( *psz_subtitle == '<' )
725 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
727 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
729 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
731 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
732 strcat( psz_tag, "b" );
734 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
736 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
737 strcat( psz_tag, "i" );
739 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
741 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
742 strcat( psz_tag, "u" );
744 else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
746 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
747 strcat( psz_tag, "s" );
749 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
751 const char *psz_attribs[] = { "face=", "family=", "size=",
752 "color=", "outline-color=", "shadow-color=",
753 "outline-level=", "shadow-level=", "back-color=",
756 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
757 strcat( psz_tag, "f" );
759 while( *psz_subtitle != '>' )
763 for( k=0; psz_attribs[ k ]; k++ )
765 int i_len = strlen( psz_attribs[ k ] );
767 if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
770 HtmlPut( &psz_html, psz_attribs[k] );
771 psz_subtitle += i_len;
774 if( *psz_subtitle == '"' )
777 i_len = strcspn( psz_subtitle, "\"" );
781 i_len = strcspn( psz_subtitle, " \t>" );
783 HtmlPut( &psz_html, "\"" );
784 if( !strcmp( psz_attribs[ k ], "color=" ) && *psz_subtitle >= '0' && *psz_subtitle <= '9' )
785 HtmlPut( &psz_html, "#" );
786 HtmlNPut( &psz_html, psz_subtitle, i_len );
787 HtmlPut( &psz_html, "\"" );
789 psz_subtitle += i_len;
790 if( *psz_subtitle == '\"' )
795 if( psz_attribs[ k ] == NULL )
797 /* Jump over unrecognised tag */
798 int i_len = strcspn( psz_subtitle, "\"" );
799 if( psz_subtitle[i_len] == '\"' )
801 i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
802 if( psz_subtitle[i_len] == '\"' )
805 psz_subtitle += i_len;
807 while (*psz_subtitle == ' ')
808 *psz_html++ = *psz_subtitle++;
810 *psz_html++ = *psz_subtitle++;
812 else if( !strncmp( psz_subtitle, "</", 2 ))
814 bool b_match = false;
815 bool b_ignore = false;
816 int i_len = strlen( psz_tag ) - 1;
817 char *psz_lastTag = NULL;
821 psz_lastTag = psz_tag + i_len;
824 switch( *psz_lastTag )
827 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
831 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
835 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
839 b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
843 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
847 i_len = strcspn( psz_subtitle, ">" );
848 b_match = psz_subtitle[i_len] == '>';
857 /* Not well formed -- kill everything */
858 free( psz_html_start );
859 psz_html_start = NULL;
864 HtmlNPut( &psz_html, psz_subtitle, i_len );
866 psz_subtitle += i_len;
868 else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
869 ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
871 /* We have a single < */
872 HtmlPut( &psz_html, "<" );
877 /* We have an unknown tag or a single < */
879 /* Search for the next tag or end of tag or end of string */
880 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
881 char *psz_closing = strstr( psz_subtitle, "/>" );
883 if( psz_closing && psz_closing < psz_stop )
885 /* We have a self closed tag, remove it */
886 psz_subtitle = &psz_closing[2];
888 else if( *psz_stop == '>' )
892 snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
893 psz_match[strcspn( psz_match, " \t>" )] = '\0';
895 if( strstr( psz_subtitle, psz_match ) )
897 /* We have the closing tag, ignore it TODO */
898 psz_subtitle = &psz_stop[1];
899 strcat( psz_tag, "I" );
903 int i_len = psz_stop + 1 - psz_subtitle;
905 /* Copy the whole data */
906 for( ; i_len > 0; i_len--, psz_subtitle++ )
908 if( *psz_subtitle == '<' )
909 HtmlPut( &psz_html, "<" );
910 else if( *psz_subtitle == '>' )
911 HtmlPut( &psz_html, ">" );
913 *psz_html++ = *psz_subtitle;
919 /* We have a single < */
920 HtmlPut( &psz_html, "<" );
925 else if( *psz_subtitle == '&' )
927 if( !strncasecmp( psz_subtitle, "<", 4 ))
929 HtmlCopy( &psz_html, &psz_subtitle, "<" );
931 else if( !strncasecmp( psz_subtitle, ">", 4 ))
933 HtmlCopy( &psz_html, &psz_subtitle, ">" );
935 else if( !strncasecmp( psz_subtitle, "&", 5 ))
937 HtmlCopy( &psz_html, &psz_subtitle, "&" );
941 HtmlPut( &psz_html, "&" );
945 else if( *psz_subtitle == '>' )
947 HtmlPut( &psz_html, ">" );
950 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
951 strchr( psz_subtitle, '}' ) )
953 /* Check for forced alignment */
955 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
957 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
958 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
959 const int i_id = psz_subtitle[4] - '1';
962 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
964 /* TODO fr -> rotation */
966 /* Hide {\stupidity} */
967 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
969 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == 'Y'
970 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
972 /* Hide {Y:stupidity} */
973 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
975 else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
977 if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
979 HtmlPut( &psz_html, "<br/>" );
982 else if( psz_subtitle[1] == 'h' )
984 /* Non breakable space */
985 HtmlPut( &psz_html, NO_BREAKING_SPACE );
990 HtmlPut( &psz_html, "\\" );
996 *psz_html = *psz_subtitle;
997 if( psz_html > psz_html_start )
999 /* Check for double whitespace */
1000 if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
1001 ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
1003 HtmlPut( &psz_html, NO_BREAKING_SPACE );
1011 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
1013 const int i_len = psz_html - psz_html_start;
1016 char *psz_new = realloc( psz_html_start, i_buf_size );
1019 psz_html_start = psz_new;
1020 psz_html = &psz_new[i_len];
1023 if( psz_html_start )
1025 static const char *psz_text_close = "</text>";
1026 static const char *psz_tag_long = "/font>";
1028 /* Realloc for closing tags and shrink memory */
1029 const size_t i_length = (size_t)( psz_html - psz_html_start );
1031 const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
1032 char *psz_new = realloc( psz_html_start, i_size );
1035 psz_html_start = psz_new;
1036 psz_html = &psz_new[i_length];
1038 /* Close not well formed subtitle */
1042 char *psz_last = &psz_tag[strlen(psz_tag)-1];
1046 HtmlPut( &psz_html, "</b>" );
1049 HtmlPut( &psz_html, "</i>" );
1052 HtmlPut( &psz_html, "</u>" );
1055 HtmlPut( &psz_html, "</s>" );
1058 HtmlPut( &psz_html, "/font>" );
1066 HtmlPut( &psz_html, psz_text_close );
1071 return psz_html_start;