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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_charset.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static const char *const ppsz_encodings[] = {
87 static const char *const ppsz_encoding_names[] = {
89 The character encoding name in parenthesis corresponds to that used for
90 the GetACP translation. "Windows-1252" applies to Western European
91 languages using the Latin alphabet. */
92 N_("Default (Windows-1252)"),
94 N_("Universal (UTF-8)"),
95 N_("Universal (UTF-16)"),
96 N_("Universal (big endian UTF-16)"),
97 N_("Universal (little endian UTF-16)"),
98 N_("Universal, Chinese (GB18030)"),
100 /* ISO 8859 and the likes */
102 N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
103 N_("Western European (Windows-1252)"),
105 N_("Eastern European (Latin-2)"),
106 N_("Eastern European (Windows-1250)"),
108 N_("Esperanto (Latin-3)"),
110 N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
112 N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
113 N_("Russian (KOI8-R)"),
114 N_("Ukrainian (KOI8-U)"),
116 N_("Arabic (ISO 8859-6)"),
117 N_("Arabic (Windows-1256)"),
119 N_("Greek (ISO 8859-7)"),
120 N_("Greek (Windows-1253)"),
122 N_("Hebrew (ISO 8859-8)"),
123 N_("Hebrew (Windows-1255)"),
125 N_("Turkish (ISO 8859-9)"),
126 N_("Turkish (Windows-1254)"),
129 N_("Thai (TIS 620-2533/ISO 8859-11)"),
130 N_("Thai (Windows-874)"),
132 N_("Baltic (Latin-7)"),
133 N_("Baltic (Windows-1257)"),
134 /* 12 -> /dev/null */
136 N_("Celtic (Latin-8)"),
139 N_("South-Eastern European (Latin-10)"),
141 N_("Simplified Chinese (ISO-2022-CN-EXT)"),
142 N_("Simplified Chinese Unix (EUC-CN)"),
143 N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
144 N_("Japanese Unix (EUC-JP)"),
145 N_("Japanese (Shift JIS)"),
146 N_("Korean (EUC-KR/CP949)"),
147 N_("Korean (ISO-2022-KR)"),
148 N_("Traditional Chinese (Big5)"),
149 N_("Traditional Chinese Unix (EUC-TW)"),
150 N_("Hong-Kong Supplementary (HKSCS)"),
152 N_("Vietnamese (VISCII)"),
153 N_("Vietnamese (Windows-1258)"),
156 static const int pi_justification[] = { 0, 1, 2 };
157 static const char *const ppsz_justification_text[] = {
158 N_("Center"),N_("Left"),N_("Right")};
160 #define ENCODING_TEXT N_("Subtitles text encoding")
161 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
162 #define ALIGN_TEXT N_("Subtitles justification")
163 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
164 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
165 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
166 "UTF-8 encoding within subtitles files.")
167 #define FORMAT_TEXT N_("Formatted Subtitles")
168 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
169 "VLC partly implements this, but you can choose to disable all formatting.")
171 static int OpenDecoder ( vlc_object_t * );
172 static void CloseDecoder ( vlc_object_t * );
175 set_shortname( N_("Subtitles"))
176 set_description( N_("Text subtitles decoder") )
177 set_capability( "decoder", 50 )
178 set_callbacks( OpenDecoder, CloseDecoder )
179 set_category( CAT_INPUT )
180 set_subcategory( SUBCAT_INPUT_SCODEC )
182 add_integer( "subsdec-align", 0, ALIGN_TEXT, ALIGN_LONGTEXT,
184 change_integer_list( pi_justification, ppsz_justification_text )
185 add_string( "subsdec-encoding", "",
186 ENCODING_TEXT, ENCODING_LONGTEXT, false )
187 change_string_list( ppsz_encodings, ppsz_encoding_names, 0 )
188 add_bool( "subsdec-autodetect-utf8", true,
189 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
190 add_bool( "subsdec-formatted", true, FORMAT_TEXT, FORMAT_LONGTEXT,
194 /*****************************************************************************
196 *****************************************************************************/
197 #define NO_BREAKING_SPACE " "
201 int i_align; /* Subtitles alignment on the vout */
203 vlc_iconv_t iconv_handle; /* handle to iconv instance */
204 bool b_autodetect_utf8;
208 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
209 static subpicture_t *ParseText ( decoder_t *, block_t * );
210 static char *StripTags ( char * );
211 static char *CreateHtmlSubtitle( int *pi_align, char * );
213 /*****************************************************************************
214 * OpenDecoder: probe the decoder and return score
215 *****************************************************************************
216 * Tries to launch a decoder and return score so that the interface is able
218 *****************************************************************************/
219 static int OpenDecoder( vlc_object_t *p_this )
221 decoder_t *p_dec = (decoder_t*)p_this;
222 decoder_sys_t *p_sys;
224 switch( p_dec->fmt_in.i_codec )
227 case VLC_CODEC_ITU_T140:
233 p_dec->pf_decode_sub = DecodeBlock;
234 p_dec->fmt_out.i_cat = SPU_ES;
235 p_dec->fmt_out.i_codec = 0;
237 /* Allocate the memory needed to store the decoder's structure */
238 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
244 p_sys->iconv_handle = (vlc_iconv_t)-1;
245 p_sys->b_autodetect_utf8 = false;
247 char *psz_charset = NULL;
249 /* First try demux-specified encoding */
250 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
251 psz_charset = strdup( "UTF-8" ); /* IUT T.140 is always using UTF-8 */
253 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
255 psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
256 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
257 p_dec->fmt_in.subs.psz_encoding ?
258 p_dec->fmt_in.subs.psz_encoding : "not specified");
261 /* Second, try configured encoding */
262 if (psz_charset == NULL)
264 psz_charset = var_InheritString (p_dec, "subsdec-encoding");
265 msg_Dbg (p_dec, "trying configured character encoding: %s",
266 psz_charset ? psz_charset : "not specified");
267 if (psz_charset != NULL && !strcmp (psz_charset, "system"))
270 psz_charset = strdup ("");
271 /* ^ iconv() treats "" as nl_langinfo(CODESET) */
275 /* Third, try "local" encoding with optional UTF-8 autodetection */
276 if (psz_charset == NULL)
279 The Windows ANSI code page most commonly used for this language.
280 VLC uses this as a guess of the subtitle files character set
281 (if UTF-8 and UTF-16 autodetection fails).
282 Western European languages normally use "CP1252", which is a
283 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
284 Other scripts use other code pages.
286 This MUST be a valid iconv character set. If unsure, please refer
287 the VideoLAN translators mailing list. */
288 const char *acp = vlc_pgettext("GetACP", "CP1252");
290 psz_charset = strdup (acp);
291 msg_Dbg (p_dec, "trying default character encoding: %s",
292 psz_charset ? psz_charset : "not specified");
294 if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
296 msg_Dbg (p_dec, "using automatic UTF-8 detection");
297 p_sys->b_autodetect_utf8 = true;
301 /* Forth, don't do character decoding, i.e. assume UTF-8 */
302 if (psz_charset == NULL)
304 psz_charset = strdup ("UTF-8");
305 msg_Dbg (p_dec, "using UTF-8 character encoding" );
308 if ((psz_charset != NULL)
309 && strcasecmp (psz_charset, "UTF-8")
310 && strcasecmp (psz_charset, "utf8"))
312 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
313 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
314 msg_Err (p_dec, "cannot convert from %s: %m", psz_charset);
318 p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
323 /****************************************************************************
324 * DecodeBlock: the whole thing
325 ****************************************************************************
326 * This function must be fed with complete subtitles units.
327 ****************************************************************************/
328 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
333 if( !pp_block || *pp_block == NULL )
337 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
339 block_Release( p_block );
343 p_spu = ParseText( p_dec, p_block );
345 block_Release( p_block );
351 /*****************************************************************************
352 * CloseDecoder: clean up the decoder
353 *****************************************************************************/
354 static void CloseDecoder( vlc_object_t *p_this )
356 decoder_t *p_dec = (decoder_t *)p_this;
357 decoder_sys_t *p_sys = p_dec->p_sys;
359 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
360 vlc_iconv_close( p_sys->iconv_handle );
365 /*****************************************************************************
366 * ParseText: parse an text subtitle packet and send it to the video output
367 *****************************************************************************/
368 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
370 decoder_sys_t *p_sys = p_dec->p_sys;
371 subpicture_t *p_spu = NULL;
372 char *psz_subtitle = NULL;
375 /* We cannot display a subpicture with no date */
376 if( p_block->i_pts <= VLC_TS_INVALID )
378 msg_Warn( p_dec, "subtitle without a date" );
382 /* Check validity of packet data */
383 /* An "empty" line containing only \0 can be used to force
384 and ephemer picture from the screen */
385 if( p_block->i_buffer < 1 )
387 msg_Warn( p_dec, "no subtitle data" );
391 /* Should be resiliant against bad subtitles */
392 psz_subtitle = malloc( p_block->i_buffer + 1 );
393 if( psz_subtitle == NULL )
395 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
396 psz_subtitle[p_block->i_buffer] = '\0';
398 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
400 if (EnsureUTF8( psz_subtitle ) == NULL)
402 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
403 "Try manually setting a character-encoding "
404 "before you open the file." );
410 if( p_sys->b_autodetect_utf8 )
412 if( IsUTF8( psz_subtitle ) == NULL )
414 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
415 "disabling UTF-8 subtitles autodetection" );
416 p_sys->b_autodetect_utf8 = false;
420 if( !p_sys->b_autodetect_utf8 )
422 size_t inbytes_left = strlen( psz_subtitle );
423 size_t outbytes_left = 6 * inbytes_left;
424 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
425 char *psz_convert_buffer_out = psz_new_subtitle;
426 const char *psz_convert_buffer_in = psz_subtitle;
428 size_t ret = vlc_iconv( p_sys->iconv_handle,
429 &psz_convert_buffer_in, &inbytes_left,
430 &psz_convert_buffer_out, &outbytes_left );
432 *psz_convert_buffer_out++ = '\0';
433 free( psz_subtitle );
435 if( ( ret == (size_t)(-1) ) || inbytes_left )
437 free( psz_new_subtitle );
438 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
439 "Try manually setting a character-encoding "
440 "before you open the file." );
444 psz_subtitle = realloc( psz_new_subtitle,
445 psz_convert_buffer_out - psz_new_subtitle );
447 psz_subtitle = psz_new_subtitle;
451 /* Create the subpicture unit */
452 p_spu = decoder_NewSubpicture( p_dec, NULL );
455 msg_Warn( p_dec, "can't get spu buffer" );
456 free( psz_subtitle );
460 /* Create a new subpicture region */
461 memset( &fmt, 0, sizeof(video_format_t) );
462 fmt.i_chroma = VLC_CODEC_TEXT;
463 fmt.i_width = fmt.i_height = 0;
464 fmt.i_x_offset = fmt.i_y_offset = 0;
465 p_spu->p_region = subpicture_region_New( &fmt );
466 if( !p_spu->p_region )
468 msg_Err( p_dec, "cannot allocate SPU region" );
469 free( psz_subtitle );
470 decoder_DeleteSubpicture( p_dec, p_spu );
474 /* Normal text subs, easy markup */
475 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
476 p_spu->p_region->i_x = p_sys->i_align ? 20 : 0;
477 p_spu->p_region->i_y = 10;
479 /* Remove formatting from string */
481 p_spu->p_region->psz_text = StripTags( psz_subtitle );
482 if( var_InheritBool( p_dec, "subsdec-formatted" ) )
484 p_spu->p_region->psz_html = CreateHtmlSubtitle( &p_spu->p_region->i_align, psz_subtitle );
487 p_spu->i_start = p_block->i_pts;
488 p_spu->i_stop = p_block->i_pts + p_block->i_length;
489 p_spu->b_ephemer = (p_block->i_length == 0);
490 p_spu->b_absolute = false;
491 free( psz_subtitle );
496 /* Function now handles tags with attribute values, and tries
497 * to deal with &' commands too. It no longer modifies the string
498 * in place, so that the original text can be reused
500 static char *StripTags( char *psz_subtitle )
502 char *psz_text_start;
505 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
506 if( !psz_text_start )
509 while( *psz_subtitle )
511 if( *psz_subtitle == '<' )
513 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
516 psz_subtitle += strcspn( psz_subtitle, ">" );
518 else if( *psz_subtitle == '&' )
520 if( !strncasecmp( psz_subtitle, "<", 4 ))
523 psz_subtitle += strcspn( psz_subtitle, ";" );
525 else if( !strncasecmp( psz_subtitle, ">", 4 ))
528 psz_subtitle += strcspn( psz_subtitle, ";" );
530 else if( !strncasecmp( psz_subtitle, "&", 5 ))
533 psz_subtitle += strcspn( psz_subtitle, ";" );
535 else if( !strncasecmp( psz_subtitle, """, 6 ))
538 psz_subtitle += strcspn( psz_subtitle, ";" );
542 /* Assume it is just a normal ampersand */
548 *psz_text++ = *psz_subtitle;
551 /* Security fix: Account for the case where input ends early */
552 if( *psz_subtitle == '\0' ) break;
557 char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
558 if( psz ) psz_text_start = psz;
560 return psz_text_start;
563 /* Try to respect any style tags present in the subtitle string. The main
564 * problem here is a lack of adequate specs for the subtitle formats.
565 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
566 * SAMI has a detailed spec, but extensive rework is needed in the demux
567 * code to prevent all this style information being excised, as it presently
569 * That leaves the others - none of which were (I guess) originally intended
570 * to be carrying style information. Over time people have used them that way.
571 * In the absence of specifications from which to work, the tags supported
572 * have been restricted to the simple set permitted by the USF DTD, ie. :
573 * Basic: <br>, <i>, <b>, <u>, <s>
585 * There is also the further restriction that the subtitle be well-formed
586 * as an XML entity, ie. the HTML sentence:
587 * <b><i>Bold and Italics</b></i>
588 * doesn't qualify because the tags aren't nested one inside the other.
589 * <text> tags are automatically added to the output to ensure
591 * If the text doesn't qualify for any reason, a NULL string is
592 * returned, and the rendering engine will fall back to the
593 * plain text version of the subtitle.
595 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
597 const int i_len = strlen(psz_text);
599 strncpy( *ppsz_html, psz_text, i_max );
600 *ppsz_html += __MIN(i_max,i_len);
603 static void HtmlPut( char **ppsz_html, const char *psz_text )
605 strcpy( *ppsz_html, psz_text );
606 *ppsz_html += strlen(psz_text);
608 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
610 HtmlPut( ppsz_html, psz_text );
611 *ppsz_subtitle += strlen(psz_text);
614 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
617 char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
624 size_t i_buf_size = strlen( psz_subtitle ) + 100;
625 char *psz_html_start = malloc( i_buf_size );
626 char *psz_html = psz_html_start;
627 if( psz_html_start == NULL )
634 bool b_has_align = false;
636 HtmlPut( &psz_html, "<text>" );
639 while( *psz_subtitle )
641 if( *psz_subtitle == '\n' )
643 HtmlPut( &psz_html, "<br/>" );
646 else if( *psz_subtitle == '<' )
648 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
650 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
652 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
654 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
655 strcat( psz_tag, "b" );
657 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
659 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
660 strcat( psz_tag, "i" );
662 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
664 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
665 strcat( psz_tag, "u" );
667 else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
669 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
670 strcat( psz_tag, "s" );
672 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
674 const char *psz_attribs[] = { "face=", "family=", "size=",
675 "color=", "outline-color=", "shadow-color=",
676 "outline-level=", "shadow-level=", "back-color=",
679 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
680 strcat( psz_tag, "f" );
682 while( *psz_subtitle != '>' )
686 for( k=0; psz_attribs[ k ]; k++ )
688 int i_len = strlen( psz_attribs[ k ] );
690 if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
693 HtmlPut( &psz_html, psz_attribs[k] );
694 psz_subtitle += i_len;
697 if( *psz_subtitle == '"' )
700 i_len = strcspn( psz_subtitle, "\"" );
704 i_len = strcspn( psz_subtitle, " \t>" );
706 HtmlPut( &psz_html, "\"" );
707 if( !strcmp( psz_attribs[ k ], "color=" ) && *psz_subtitle >= '0' && *psz_subtitle <= '9' )
708 HtmlPut( &psz_html, "#" );
709 HtmlNPut( &psz_html, psz_subtitle, i_len );
710 HtmlPut( &psz_html, "\"" );
712 psz_subtitle += i_len;
713 if( *psz_subtitle == '\"' )
718 if( psz_attribs[ k ] == NULL )
720 /* Jump over unrecognised tag */
721 int i_len = strcspn( psz_subtitle, "\"" );
722 if( psz_subtitle[i_len] == '\"' )
724 i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
725 if( psz_subtitle[i_len] == '\"' )
728 psz_subtitle += i_len;
730 while (*psz_subtitle == ' ')
731 *psz_html++ = *psz_subtitle++;
733 *psz_html++ = *psz_subtitle++;
735 else if( !strncmp( psz_subtitle, "</", 2 ))
737 bool b_match = false;
738 bool b_ignore = false;
739 int i_len = strlen( psz_tag ) - 1;
740 char *psz_lastTag = NULL;
744 psz_lastTag = psz_tag + i_len;
747 switch( *psz_lastTag )
750 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
754 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
758 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
762 b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
766 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
770 i_len = strcspn( psz_subtitle, ">" );
771 b_match = psz_subtitle[i_len] == '>';
780 /* Not well formed -- kill everything */
781 free( psz_html_start );
782 psz_html_start = NULL;
787 HtmlNPut( &psz_html, psz_subtitle, i_len );
789 psz_subtitle += i_len;
791 else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
792 ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
794 /* We have a single < */
795 HtmlPut( &psz_html, "<" );
800 /* We have an unknown tag or a single < */
802 /* Search for the next tag or end of tag or end of string */
803 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
804 char *psz_closing = strstr( psz_subtitle, "/>" );
806 if( psz_closing && psz_closing < psz_stop )
808 /* We have a self closed tag, remove it */
809 psz_subtitle = &psz_closing[2];
811 else if( *psz_stop == '>' )
815 snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
816 psz_match[strcspn( psz_match, " \t>" )] = '\0';
818 if( strstr( psz_subtitle, psz_match ) )
820 /* We have the closing tag, ignore it TODO */
821 psz_subtitle = &psz_stop[1];
822 strcat( psz_tag, "I" );
826 int i_len = psz_stop + 1 - psz_subtitle;
828 /* Copy the whole data */
829 for( ; i_len > 0; i_len--, psz_subtitle++ )
831 if( *psz_subtitle == '<' )
832 HtmlPut( &psz_html, "<" );
833 else if( *psz_subtitle == '>' )
834 HtmlPut( &psz_html, ">" );
836 *psz_html++ = *psz_subtitle;
842 /* We have a single < */
843 HtmlPut( &psz_html, "<" );
848 else if( *psz_subtitle == '&' )
850 if( !strncasecmp( psz_subtitle, "<", 4 ))
852 HtmlCopy( &psz_html, &psz_subtitle, "<" );
854 else if( !strncasecmp( psz_subtitle, ">", 4 ))
856 HtmlCopy( &psz_html, &psz_subtitle, ">" );
858 else if( !strncasecmp( psz_subtitle, "&", 5 ))
860 HtmlCopy( &psz_html, &psz_subtitle, "&" );
864 HtmlPut( &psz_html, "&" );
868 else if( *psz_subtitle == '>' )
870 HtmlPut( &psz_html, ">" );
873 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
874 strchr( psz_subtitle, '}' ) )
876 /* Check for forced alignment */
878 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
880 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
881 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
882 const int i_id = psz_subtitle[4] - '1';
885 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
887 /* TODO fr -> rotation */
889 /* Hide {\stupidity} */
890 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
892 else if( psz_subtitle[0] == '{' &&
893 ( psz_subtitle[1] == 'Y' || psz_subtitle[1] == 'y' )
894 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
896 // FIXME: We don't do difference between Y and y, and we should.
897 if( psz_subtitle[3] == 'i' )
899 HtmlPut( &psz_html, "<i>" );
900 strcat( psz_tag, "i" );
902 if( psz_subtitle[3] == 'b' )
904 HtmlPut( &psz_html, "<b>" );
905 strcat( psz_tag, "b" );
907 if( psz_subtitle[3] == 'u' )
909 HtmlPut( &psz_html, "<u>" );
910 strcat( psz_tag, "u" );
912 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
914 else if( psz_subtitle[0] == '{' && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
916 // Hide other {x:y} atrocities, like {c:$bbggrr} or {P:x}
917 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
919 else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
921 if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
923 HtmlPut( &psz_html, "<br/>" );
926 else if( psz_subtitle[1] == 'h' )
928 /* Non breakable space */
929 HtmlPut( &psz_html, NO_BREAKING_SPACE );
934 HtmlPut( &psz_html, "\\" );
940 *psz_html = *psz_subtitle;
941 if( psz_html > psz_html_start )
943 /* Check for double whitespace */
944 if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
945 ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
947 HtmlPut( &psz_html, NO_BREAKING_SPACE );
955 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
957 const int i_len = psz_html - psz_html_start;
960 char *psz_new = realloc( psz_html_start, i_buf_size );
963 psz_html_start = psz_new;
964 psz_html = &psz_new[i_len];
969 static const char *psz_text_close = "</text>";
970 static const char *psz_tag_long = "/font>";
972 /* Realloc for closing tags and shrink memory */
973 const size_t i_length = (size_t)( psz_html - psz_html_start );
975 const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
976 char *psz_new = realloc( psz_html_start, i_size );
979 psz_html_start = psz_new;
980 psz_html = &psz_new[i_length];
982 /* Close not well formed subtitle */
986 char *psz_last = &psz_tag[strlen(psz_tag)-1];
990 HtmlPut( &psz_html, "</b>" );
993 HtmlPut( &psz_html, "</i>" );
996 HtmlPut( &psz_html, "</u>" );
999 HtmlPut( &psz_html, "</s>" );
1002 HtmlPut( &psz_html, "/font>" );
1010 HtmlPut( &psz_html, psz_text_close );
1015 return psz_html_start;