1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
41 /*****************************************************************************
43 *****************************************************************************/
44 static const char *const ppsz_encodings[] = {
90 static const char *const ppsz_encoding_names[] = {
92 The character encoding name in parenthesis corresponds to that used for
93 the GetACP translation. "Windows-1252" applies to Western European
94 languages using the Latin alphabet. */
95 N_("Default (Windows-1252)"),
97 N_("Universal (UTF-8)"),
98 N_("Universal (UTF-16)"),
99 N_("Universal (big endian UTF-16)"),
100 N_("Universal (little endian UTF-16)"),
101 N_("Universal, Chinese (GB18030)"),
103 /* ISO 8859 and the likes */
105 N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
106 N_("Western European (Windows-1252)"),
107 N_("Western European (IBM 00850)"),
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 static const int pi_justification[] = { 0, 1, 2 };
161 static const char *const ppsz_justification_text[] = {
162 N_("Center"),N_("Left"),N_("Right")};
164 #define ENCODING_TEXT N_("Subtitles text encoding")
165 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
166 #define ALIGN_TEXT N_("Subtitles justification")
167 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
168 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
169 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
170 "UTF-8 encoding within subtitles files.")
171 #define FORMAT_TEXT N_("Formatted Subtitles")
172 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
173 "VLC partly implements this, but you can choose to disable all formatting.")
175 static int OpenDecoder ( vlc_object_t * );
176 static void CloseDecoder ( vlc_object_t * );
179 set_shortname( N_("Subtitles"))
180 set_description( N_("Text subtitles decoder") )
181 set_capability( "decoder", 50 )
182 set_callbacks( OpenDecoder, CloseDecoder )
183 set_category( CAT_INPUT )
184 set_subcategory( SUBCAT_INPUT_SCODEC )
186 add_integer( "subsdec-align", 0, ALIGN_TEXT, ALIGN_LONGTEXT,
188 change_integer_list( pi_justification, ppsz_justification_text )
189 add_string( "subsdec-encoding", "",
190 ENCODING_TEXT, ENCODING_LONGTEXT, false )
191 change_string_list( ppsz_encodings, ppsz_encoding_names )
192 add_bool( "subsdec-autodetect-utf8", true,
193 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
194 add_bool( "subsdec-formatted", true, FORMAT_TEXT, FORMAT_LONGTEXT,
198 /*****************************************************************************
200 *****************************************************************************/
201 #define NO_BREAKING_SPACE " "
205 int i_align; /* Subtitles alignment on the vout */
207 vlc_iconv_t iconv_handle; /* handle to iconv instance */
208 bool b_autodetect_utf8;
212 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
213 static subpicture_t *ParseText ( decoder_t *, block_t * );
214 static char *StripTags ( char * );
215 static char *CreateHtmlSubtitle( int *pi_align, char * );
217 /*****************************************************************************
218 * OpenDecoder: probe the decoder and return score
219 *****************************************************************************
220 * Tries to launch a decoder and return score so that the interface is able
222 *****************************************************************************/
223 static int OpenDecoder( vlc_object_t *p_this )
225 decoder_t *p_dec = (decoder_t*)p_this;
226 decoder_sys_t *p_sys;
228 switch( p_dec->fmt_in.i_codec )
231 case VLC_CODEC_ITU_T140:
237 p_dec->pf_decode_sub = DecodeBlock;
238 p_dec->fmt_out.i_cat = SPU_ES;
239 p_dec->fmt_out.i_codec = 0;
241 /* Allocate the memory needed to store the decoder's structure */
242 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
248 p_sys->iconv_handle = (vlc_iconv_t)-1;
249 p_sys->b_autodetect_utf8 = false;
251 const char *encoding;
254 /* First try demux-specified encoding */
255 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
256 encoding = "UTF-8"; /* IUT T.140 is always using UTF-8 */
258 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
260 encoding = p_dec->fmt_in.subs.psz_encoding;
261 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
266 /* Second, try configured encoding */
267 if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
269 msg_Dbg (p_dec, "trying configured character encoding: %s", var);
270 if (!strcmp (var, "system"))
275 /* ^ iconv() treats "" as nl_langinfo(CODESET) */
281 /* Third, try "local" encoding */
284 The Windows ANSI code page most commonly used for this language.
285 VLC uses this as a guess of the subtitle files character set
286 (if UTF-8 and UTF-16 autodetection fails).
287 Western European languages normally use "CP1252", which is a
288 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
289 Other scripts use other code pages.
291 This MUST be a valid iconv character set. If unsure, please refer
292 the VideoLAN translators mailing list. */
293 encoding = vlc_pgettext("GetACP", "CP1252");
294 msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
297 /* Check UTF-8 autodetection */
298 if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
300 msg_Dbg (p_dec, "using automatic UTF-8 detection");
301 p_sys->b_autodetect_utf8 = true;
305 if (strcasecmp (encoding, "UTF-8") && strcasecmp (encoding, "utf8"))
307 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", encoding);
308 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
309 msg_Err (p_dec, "cannot convert from %s: %m", encoding);
313 p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
318 /****************************************************************************
319 * DecodeBlock: the whole thing
320 ****************************************************************************
321 * This function must be fed with complete subtitles units.
322 ****************************************************************************/
323 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
328 if( !pp_block || *pp_block == NULL )
332 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
334 block_Release( p_block );
338 p_spu = ParseText( p_dec, p_block );
340 block_Release( p_block );
346 /*****************************************************************************
347 * CloseDecoder: clean up the decoder
348 *****************************************************************************/
349 static void CloseDecoder( vlc_object_t *p_this )
351 decoder_t *p_dec = (decoder_t *)p_this;
352 decoder_sys_t *p_sys = p_dec->p_sys;
354 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
355 vlc_iconv_close( p_sys->iconv_handle );
360 /*****************************************************************************
361 * ParseText: parse an text subtitle packet and send it to the video output
362 *****************************************************************************/
363 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
365 decoder_sys_t *p_sys = p_dec->p_sys;
366 subpicture_t *p_spu = NULL;
367 char *psz_subtitle = NULL;
369 /* We cannot display a subpicture with no date */
370 if( p_block->i_pts <= VLC_TS_INVALID )
372 msg_Warn( p_dec, "subtitle without a date" );
376 /* Check validity of packet data */
377 /* An "empty" line containing only \0 can be used to force
378 and ephemer picture from the screen */
379 if( p_block->i_buffer < 1 )
381 msg_Warn( p_dec, "no subtitle data" );
385 /* Should be resiliant against bad subtitles */
386 psz_subtitle = malloc( p_block->i_buffer + 1 );
387 if( psz_subtitle == NULL )
389 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
390 psz_subtitle[p_block->i_buffer] = '\0';
392 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
394 if (EnsureUTF8( psz_subtitle ) == NULL)
396 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
397 "Try manually setting a character-encoding "
398 "before you open the file." );
404 if( p_sys->b_autodetect_utf8 )
406 if( IsUTF8( psz_subtitle ) == NULL )
408 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
409 "disabling UTF-8 subtitles autodetection" );
410 p_sys->b_autodetect_utf8 = false;
414 if( !p_sys->b_autodetect_utf8 )
416 size_t inbytes_left = strlen( psz_subtitle );
417 size_t outbytes_left = 6 * inbytes_left;
418 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
419 char *psz_convert_buffer_out = psz_new_subtitle;
420 const char *psz_convert_buffer_in = psz_subtitle;
422 size_t ret = vlc_iconv( p_sys->iconv_handle,
423 &psz_convert_buffer_in, &inbytes_left,
424 &psz_convert_buffer_out, &outbytes_left );
426 *psz_convert_buffer_out++ = '\0';
427 free( psz_subtitle );
429 if( ( ret == (size_t)(-1) ) || inbytes_left )
431 free( psz_new_subtitle );
432 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
433 "Try manually setting a character-encoding "
434 "before you open the file." );
438 psz_subtitle = realloc( psz_new_subtitle,
439 psz_convert_buffer_out - psz_new_subtitle );
441 psz_subtitle = psz_new_subtitle;
445 /* Create the subpicture unit */
446 p_spu = decoder_NewSubpictureText( p_dec );
449 free( psz_subtitle );
452 p_spu->i_start = p_block->i_pts;
453 p_spu->i_stop = p_block->i_pts + p_block->i_length;
454 p_spu->b_ephemer = (p_block->i_length == 0);
455 p_spu->b_absolute = false;
457 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
459 p_spu_sys->align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
460 p_spu_sys->text = StripTags( psz_subtitle );
461 if( var_InheritBool( p_dec, "subsdec-formatted" ) )
462 p_spu_sys->html = CreateHtmlSubtitle( &p_spu_sys->align, psz_subtitle );
464 free( psz_subtitle );
469 /* Function now handles tags with attribute values, and tries
470 * to deal with &' commands too. It no longer modifies the string
471 * in place, so that the original text can be reused
473 static char *StripTags( char *psz_subtitle )
475 char *psz_text_start;
478 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
479 if( !psz_text_start )
482 while( *psz_subtitle )
484 if( *psz_subtitle == '<' )
486 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
489 psz_subtitle += strcspn( psz_subtitle, ">" );
491 else if( *psz_subtitle == '&' )
493 if( !strncasecmp( psz_subtitle, "<", 4 ))
496 psz_subtitle += strcspn( psz_subtitle, ";" );
498 else if( !strncasecmp( psz_subtitle, ">", 4 ))
501 psz_subtitle += strcspn( psz_subtitle, ";" );
503 else if( !strncasecmp( psz_subtitle, "&", 5 ))
506 psz_subtitle += strcspn( psz_subtitle, ";" );
508 else if( !strncasecmp( psz_subtitle, """, 6 ))
511 psz_subtitle += strcspn( psz_subtitle, ";" );
515 /* Assume it is just a normal ampersand */
521 *psz_text++ = *psz_subtitle;
524 /* Security fix: Account for the case where input ends early */
525 if( *psz_subtitle == '\0' ) break;
530 char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
531 if( psz ) psz_text_start = psz;
533 return psz_text_start;
536 /* Try to respect any style tags present in the subtitle string. The main
537 * problem here is a lack of adequate specs for the subtitle formats.
538 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
539 * SAMI has a detailed spec, but extensive rework is needed in the demux
540 * code to prevent all this style information being excised, as it presently
542 * That leaves the others - none of which were (I guess) originally intended
543 * to be carrying style information. Over time people have used them that way.
544 * In the absence of specifications from which to work, the tags supported
545 * have been restricted to the simple set permitted by the USF DTD, ie. :
546 * Basic: <br>, <i>, <b>, <u>, <s>
558 * There is also the further restriction that the subtitle be well-formed
559 * as an XML entity, ie. the HTML sentence:
560 * <b><i>Bold and Italics</b></i>
561 * doesn't qualify because the tags aren't nested one inside the other.
562 * <text> tags are automatically added to the output to ensure
564 * If the text doesn't qualify for any reason, a NULL string is
565 * returned, and the rendering engine will fall back to the
566 * plain text version of the subtitle.
568 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
570 const int i_len = strlen(psz_text);
572 strncpy( *ppsz_html, psz_text, i_max );
573 *ppsz_html += __MIN(i_max,i_len);
576 static void HtmlPut( char **ppsz_html, const char *psz_text )
578 strcpy( *ppsz_html, psz_text );
579 *ppsz_html += strlen(psz_text);
581 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
583 HtmlPut( ppsz_html, psz_text );
584 *ppsz_subtitle += strlen(psz_text);
587 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
590 char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
597 size_t i_buf_size = strlen( psz_subtitle ) + 100;
598 char *psz_html_start = malloc( i_buf_size );
599 char *psz_html = psz_html_start;
600 if( psz_html_start == NULL )
607 bool b_has_align = false;
609 HtmlPut( &psz_html, "<text>" );
612 while( *psz_subtitle )
614 if( *psz_subtitle == '\n' )
616 HtmlPut( &psz_html, "<br/>" );
619 else if( *psz_subtitle == '<' )
621 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
623 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
625 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
627 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
628 strcat( psz_tag, "b" );
630 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
632 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
633 strcat( psz_tag, "i" );
635 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
637 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
638 strcat( psz_tag, "u" );
640 else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
642 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
643 strcat( psz_tag, "s" );
645 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
647 const char *psz_attribs[] = { "face=", "family=", "size=",
648 "color=", "outline-color=", "shadow-color=",
649 "outline-level=", "shadow-level=", "back-color=",
652 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
653 strcat( psz_tag, "f" );
656 while (*psz_subtitle == ' ')
659 while( *psz_subtitle != '>' )
663 for( k=0; psz_attribs[ k ]; k++ )
665 int i_len = strlen( psz_attribs[ k ] );
667 if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
670 HtmlPut( &psz_html, psz_attribs[k] );
671 psz_subtitle += i_len;
673 /* <font color= red */
674 while (*psz_subtitle == ' ')
678 if( *psz_subtitle == '"' )
681 i_len = strcspn( psz_subtitle, "\"" );
683 else if( *psz_subtitle == '\'' )
686 i_len = strcspn( psz_subtitle, "'" );
690 i_len = strcspn( psz_subtitle, " \t>" );
692 HtmlPut( &psz_html, "\"" );
693 HtmlNPut( &psz_html, psz_subtitle, i_len );
694 HtmlPut( &psz_html, "\"" );
696 psz_subtitle += i_len;
697 if( *psz_subtitle == '\"' || *psz_subtitle == '\'' )
702 if( psz_attribs[ k ] == NULL )
704 /* Jump over unrecognised tag */
705 int i_len = strcspn( psz_subtitle, "\"" );
706 if( psz_subtitle[i_len] == '\"' )
708 i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
709 if( psz_subtitle[i_len] == '\"' )
712 /* Not a tag, something else we do not understand */
716 psz_subtitle += i_len;
719 while (*psz_subtitle == ' ')
720 *psz_html++ = *psz_subtitle++;
725 else if( !strncmp( psz_subtitle, "</", 2 ))
727 bool b_match = false;
728 bool b_ignore = false;
729 int i_len = strlen( psz_tag ) - 1;
730 char *psz_lastTag = NULL;
734 psz_lastTag = psz_tag + i_len;
737 switch( *psz_lastTag )
740 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
744 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
748 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
752 b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
756 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
760 i_len = strcspn( psz_subtitle, ">" );
761 b_match = psz_subtitle[i_len] == '>';
770 /* Not well formed -- kill everything */
771 free( psz_html_start );
772 psz_html_start = NULL;
777 HtmlNPut( &psz_html, psz_subtitle, i_len );
779 psz_subtitle += i_len;
781 else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
782 ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
784 /* We have a single < */
785 HtmlPut( &psz_html, "<" );
790 /* We have an unknown tag or a single < */
792 /* Search for the next tag or end of tag or end of string */
793 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
794 char *psz_closing = strstr( psz_subtitle, "/>" );
796 if( psz_closing && psz_closing < psz_stop )
798 /* We have a self closed tag, remove it */
799 psz_subtitle = &psz_closing[2];
801 else if( *psz_stop == '>' )
805 snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
806 psz_match[strcspn( psz_match, " \t>" )] = '\0';
808 if( strstr( psz_subtitle, psz_match ) )
810 /* We have the closing tag, ignore it TODO */
811 psz_subtitle = &psz_stop[1];
812 strcat( psz_tag, "I" );
816 int i_len = psz_stop + 1 - psz_subtitle;
818 /* Copy the whole data */
819 for( ; i_len > 0; i_len--, psz_subtitle++ )
821 if( *psz_subtitle == '<' )
822 HtmlPut( &psz_html, "<" );
823 else if( *psz_subtitle == '>' )
824 HtmlPut( &psz_html, ">" );
826 *psz_html++ = *psz_subtitle;
832 /* We have a single < */
833 HtmlPut( &psz_html, "<" );
838 else if( *psz_subtitle == '&' )
840 if( !strncasecmp( psz_subtitle, "<", 4 ))
842 HtmlCopy( &psz_html, &psz_subtitle, "<" );
844 else if( !strncasecmp( psz_subtitle, ">", 4 ))
846 HtmlCopy( &psz_html, &psz_subtitle, ">" );
848 else if( !strncasecmp( psz_subtitle, "&", 5 ))
850 HtmlCopy( &psz_html, &psz_subtitle, "&" );
854 HtmlPut( &psz_html, "&" );
858 else if( *psz_subtitle == '>' )
860 HtmlPut( &psz_html, ">" );
863 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
864 strchr( psz_subtitle, '}' ) )
866 /* Check for forced alignment */
868 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
870 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
871 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
872 const int i_id = psz_subtitle[4] - '1';
875 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
877 /* TODO fr -> rotation */
879 /* Hide {\stupidity} */
880 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
882 else if( psz_subtitle[0] == '{' &&
883 ( psz_subtitle[1] == 'Y' || psz_subtitle[1] == 'y' )
884 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
886 // FIXME: We don't do difference between Y and y, and we should.
887 if( psz_subtitle[3] == 'i' )
889 HtmlPut( &psz_html, "<i>" );
890 strcat( psz_tag, "i" );
892 if( psz_subtitle[3] == 'b' )
894 HtmlPut( &psz_html, "<b>" );
895 strcat( psz_tag, "b" );
897 if( psz_subtitle[3] == 'u' )
899 HtmlPut( &psz_html, "<u>" );
900 strcat( psz_tag, "u" );
902 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
904 else if( psz_subtitle[0] == '{' && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
906 // Hide other {x:y} atrocities, like {c:$bbggrr} or {P:x}
907 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
909 else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
911 if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
913 HtmlPut( &psz_html, "<br/>" );
916 else if( psz_subtitle[1] == 'h' )
918 /* Non breakable space */
919 HtmlPut( &psz_html, NO_BREAKING_SPACE );
924 HtmlPut( &psz_html, "\\" );
930 *psz_html = *psz_subtitle;
931 if( psz_html > psz_html_start )
933 /* Check for double whitespace */
934 if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
935 ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
937 HtmlPut( &psz_html, NO_BREAKING_SPACE );
945 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
947 const int i_len = psz_html - psz_html_start;
950 char *psz_new = realloc( psz_html_start, i_buf_size );
953 psz_html_start = psz_new;
954 psz_html = &psz_new[i_len];
959 static const char *psz_text_close = "</text>";
960 static const char *psz_tag_long = "/font>";
962 /* Realloc for closing tags and shrink memory */
963 const size_t i_length = (size_t)( psz_html - psz_html_start );
965 const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
966 char *psz_new = realloc( psz_html_start, i_size );
969 psz_html_start = psz_new;
970 psz_html = &psz_new[i_length];
972 /* Close not well formed subtitle */
976 char *psz_last = &psz_tag[strlen(psz_tag)-1];
980 HtmlPut( &psz_html, "</b>" );
983 HtmlPut( &psz_html, "</i>" );
986 HtmlPut( &psz_html, "</u>" );
989 HtmlPut( &psz_html, "</s>" );
992 HtmlPut( &psz_html, "</font>" );
1000 HtmlPut( &psz_html, psz_text_close );
1005 return psz_html_start;