x11 factory: use vlc_readdir
[vlc/solaris.git] / modules / codec / subsdec.c
blob5c32d4030f5c6bd05572707c34ce0c210f8aa69f
1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_charset.h>
39 #include "substext.h"
41 /*****************************************************************************
42 * Module descriptor.
43 *****************************************************************************/
44 static const char *const ppsz_encodings[] = {
45 "",
46 "system",
47 "UTF-8",
48 "UTF-16",
49 "UTF-16BE",
50 "UTF-16LE",
51 "GB18030",
52 "ISO-8859-15",
53 "Windows-1252",
54 "ISO-8859-2",
55 "Windows-1250",
56 "ISO-8859-3",
57 "ISO-8859-10",
58 "Windows-1251",
59 "KOI8-R",
60 "KOI8-U",
61 "ISO-8859-6",
62 "Windows-1256",
63 "ISO-8859-7",
64 "Windows-1253",
65 "ISO-8859-8",
66 "Windows-1255",
67 "ISO-8859-9",
68 "Windows-1254",
69 "ISO-8859-11",
70 "Windows-874",
71 "ISO-8859-13",
72 "Windows-1257",
73 "ISO-8859-14",
74 "ISO-8859-16",
75 "ISO-2022-CN-EXT",
76 "EUC-CN",
77 "ISO-2022-JP-2",
78 "EUC-JP",
79 "Shift_JIS",
80 "CP949",
81 "ISO-2022-KR",
82 "Big5",
83 "ISO-2022-TW",
84 "Big5-HKSCS",
85 "VISCII",
86 "Windows-1258",
89 static const char *const ppsz_encoding_names[] = {
90 /* xgettext:
91 The character encoding name in parenthesis corresponds to that used for
92 the GetACP translation. "Windows-1252" applies to Western European
93 languages using the Latin alphabet. */
94 N_("Default (Windows-1252)"),
95 N_("System codeset"),
96 N_("Universal (UTF-8)"),
97 N_("Universal (UTF-16)"),
98 N_("Universal (big endian UTF-16)"),
99 N_("Universal (little endian UTF-16)"),
100 N_("Universal, Chinese (GB18030)"),
102 /* ISO 8859 and the likes */
103 /* 1 */
104 N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
105 N_("Western European (Windows-1252)"),
106 /* 2 */
107 N_("Eastern European (Latin-2)"),
108 N_("Eastern European (Windows-1250)"),
109 /* 3 */
110 N_("Esperanto (Latin-3)"),
111 /* 4 */
112 N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
113 /* 5 */
114 N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
115 N_("Russian (KOI8-R)"),
116 N_("Ukrainian (KOI8-U)"),
117 /* 6 */
118 N_("Arabic (ISO 8859-6)"),
119 N_("Arabic (Windows-1256)"),
120 /* 7 */
121 N_("Greek (ISO 8859-7)"),
122 N_("Greek (Windows-1253)"),
123 /* 8 */
124 N_("Hebrew (ISO 8859-8)"),
125 N_("Hebrew (Windows-1255)"),
126 /* 9 */
127 N_("Turkish (ISO 8859-9)"),
128 N_("Turkish (Windows-1254)"),
129 /* 10 -> 4 */
130 /* 11 */
131 N_("Thai (TIS 620-2533/ISO 8859-11)"),
132 N_("Thai (Windows-874)"),
133 /* 13 */
134 N_("Baltic (Latin-7)"),
135 N_("Baltic (Windows-1257)"),
136 /* 12 -> /dev/null */
137 /* 14 */
138 N_("Celtic (Latin-8)"),
139 /* 15 -> 1 */
140 /* 16 */
141 N_("South-Eastern European (Latin-10)"),
142 /* CJK families */
143 N_("Simplified Chinese (ISO-2022-CN-EXT)"),
144 N_("Simplified Chinese Unix (EUC-CN)"),
145 N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
146 N_("Japanese Unix (EUC-JP)"),
147 N_("Japanese (Shift JIS)"),
148 N_("Korean (EUC-KR/CP949)"),
149 N_("Korean (ISO-2022-KR)"),
150 N_("Traditional Chinese (Big5)"),
151 N_("Traditional Chinese Unix (EUC-TW)"),
152 N_("Hong-Kong Supplementary (HKSCS)"),
153 /* Other */
154 N_("Vietnamese (VISCII)"),
155 N_("Vietnamese (Windows-1258)"),
158 static const int pi_justification[] = { 0, 1, 2 };
159 static const char *const ppsz_justification_text[] = {
160 N_("Center"),N_("Left"),N_("Right")};
162 #define ENCODING_TEXT N_("Subtitles text encoding")
163 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
164 #define ALIGN_TEXT N_("Subtitles justification")
165 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
166 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
167 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
168 "UTF-8 encoding within subtitles files.")
169 #define FORMAT_TEXT N_("Formatted Subtitles")
170 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
171 "VLC partly implements this, but you can choose to disable all formatting.")
173 static int OpenDecoder ( vlc_object_t * );
174 static void CloseDecoder ( vlc_object_t * );
176 vlc_module_begin ()
177 set_shortname( N_("Subtitles"))
178 set_description( N_("Text subtitles decoder") )
179 set_capability( "decoder", 50 )
180 set_callbacks( OpenDecoder, CloseDecoder )
181 set_category( CAT_INPUT )
182 set_subcategory( SUBCAT_INPUT_SCODEC )
184 add_integer( "subsdec-align", 0, ALIGN_TEXT, ALIGN_LONGTEXT,
185 false )
186 change_integer_list( pi_justification, ppsz_justification_text )
187 add_string( "subsdec-encoding", "",
188 ENCODING_TEXT, ENCODING_LONGTEXT, false )
189 change_string_list( ppsz_encodings, ppsz_encoding_names, 0 )
190 add_bool( "subsdec-autodetect-utf8", true,
191 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
192 add_bool( "subsdec-formatted", true, FORMAT_TEXT, FORMAT_LONGTEXT,
193 false )
194 vlc_module_end ()
196 /*****************************************************************************
197 * Local prototypes
198 *****************************************************************************/
199 #define NO_BREAKING_SPACE "&#160;"
201 struct decoder_sys_t
203 int i_align; /* Subtitles alignment on the vout */
205 vlc_iconv_t iconv_handle; /* handle to iconv instance */
206 bool b_autodetect_utf8;
210 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
211 static subpicture_t *ParseText ( decoder_t *, block_t * );
212 static char *StripTags ( char * );
213 static char *CreateHtmlSubtitle( int *pi_align, char * );
215 /*****************************************************************************
216 * OpenDecoder: probe the decoder and return score
217 *****************************************************************************
218 * Tries to launch a decoder and return score so that the interface is able
219 * to chose.
220 *****************************************************************************/
221 static int OpenDecoder( vlc_object_t *p_this )
223 decoder_t *p_dec = (decoder_t*)p_this;
224 decoder_sys_t *p_sys;
226 switch( p_dec->fmt_in.i_codec )
228 case VLC_CODEC_SUBT:
229 case VLC_CODEC_ITU_T140:
230 break;
231 default:
232 return VLC_EGENERIC;
235 p_dec->pf_decode_sub = DecodeBlock;
236 p_dec->fmt_out.i_cat = SPU_ES;
237 p_dec->fmt_out.i_codec = 0;
239 /* Allocate the memory needed to store the decoder's structure */
240 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
241 if( p_sys == NULL )
242 return VLC_ENOMEM;
244 /* init of p_sys */
245 p_sys->i_align = 0;
246 p_sys->iconv_handle = (vlc_iconv_t)-1;
247 p_sys->b_autodetect_utf8 = false;
249 const char *encoding;
250 char *var = NULL;
252 /* First try demux-specified encoding */
253 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
254 encoding = "UTF-8"; /* IUT T.140 is always using UTF-8 */
255 else
256 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
258 encoding = p_dec->fmt_in.subs.psz_encoding;
259 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
260 encoding);
262 else
263 /* Second, try configured encoding */
264 if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
266 msg_Dbg (p_dec, "trying configured character encoding: %s", var);
267 if (!strcmp (var, "system"))
269 free (var);
270 var = NULL;
271 encoding = "";
272 /* ^ iconv() treats "" as nl_langinfo(CODESET) */
274 else
275 encoding = var;
277 else
278 /* Third, try "local" encoding with optional UTF-8 autodetection */
280 /* xgettext:
281 The Windows ANSI code page most commonly used for this language.
282 VLC uses this as a guess of the subtitle files character set
283 (if UTF-8 and UTF-16 autodetection fails).
284 Western European languages normally use "CP1252", which is a
285 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
286 Other scripts use other code pages.
288 This MUST be a valid iconv character set. If unsure, please refer
289 the VideoLAN translators mailing list. */
290 encoding = vlc_pgettext("GetACP", "CP1252");
291 msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
292 if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
294 msg_Dbg (p_dec, "using automatic UTF-8 detection");
295 p_sys->b_autodetect_utf8 = true;
299 if (strcasecmp (encoding, "UTF-8") && strcasecmp (encoding, "utf8"))
301 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", encoding);
302 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
303 msg_Err (p_dec, "cannot convert from %s: %m", encoding);
305 free (var);
307 p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
309 return VLC_SUCCESS;
312 /****************************************************************************
313 * DecodeBlock: the whole thing
314 ****************************************************************************
315 * This function must be fed with complete subtitles units.
316 ****************************************************************************/
317 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
319 subpicture_t *p_spu;
320 block_t *p_block;
322 if( !pp_block || *pp_block == NULL )
323 return NULL;
325 p_block = *pp_block;
326 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
328 block_Release( p_block );
329 return NULL;
332 p_spu = ParseText( p_dec, p_block );
334 block_Release( p_block );
335 *pp_block = NULL;
337 return p_spu;
340 /*****************************************************************************
341 * CloseDecoder: clean up the decoder
342 *****************************************************************************/
343 static void CloseDecoder( vlc_object_t *p_this )
345 decoder_t *p_dec = (decoder_t *)p_this;
346 decoder_sys_t *p_sys = p_dec->p_sys;
348 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
349 vlc_iconv_close( p_sys->iconv_handle );
351 free( p_sys );
354 /*****************************************************************************
355 * ParseText: parse an text subtitle packet and send it to the video output
356 *****************************************************************************/
357 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
359 decoder_sys_t *p_sys = p_dec->p_sys;
360 subpicture_t *p_spu = NULL;
361 char *psz_subtitle = NULL;
363 /* We cannot display a subpicture with no date */
364 if( p_block->i_pts <= VLC_TS_INVALID )
366 msg_Warn( p_dec, "subtitle without a date" );
367 return NULL;
370 /* Check validity of packet data */
371 /* An "empty" line containing only \0 can be used to force
372 and ephemer picture from the screen */
373 if( p_block->i_buffer < 1 )
375 msg_Warn( p_dec, "no subtitle data" );
376 return NULL;
379 /* Should be resiliant against bad subtitles */
380 psz_subtitle = malloc( p_block->i_buffer + 1 );
381 if( psz_subtitle == NULL )
382 return NULL;
383 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
384 psz_subtitle[p_block->i_buffer] = '\0';
386 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
388 if (EnsureUTF8( psz_subtitle ) == NULL)
390 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
391 "Try manually setting a character-encoding "
392 "before you open the file." );
395 else
398 if( p_sys->b_autodetect_utf8 )
400 if( IsUTF8( psz_subtitle ) == NULL )
402 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
403 "disabling UTF-8 subtitles autodetection" );
404 p_sys->b_autodetect_utf8 = false;
408 if( !p_sys->b_autodetect_utf8 )
410 size_t inbytes_left = strlen( psz_subtitle );
411 size_t outbytes_left = 6 * inbytes_left;
412 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
413 char *psz_convert_buffer_out = psz_new_subtitle;
414 const char *psz_convert_buffer_in = psz_subtitle;
416 size_t ret = vlc_iconv( p_sys->iconv_handle,
417 &psz_convert_buffer_in, &inbytes_left,
418 &psz_convert_buffer_out, &outbytes_left );
420 *psz_convert_buffer_out++ = '\0';
421 free( psz_subtitle );
423 if( ( ret == (size_t)(-1) ) || inbytes_left )
425 free( psz_new_subtitle );
426 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
427 "Try manually setting a character-encoding "
428 "before you open the file." );
429 return NULL;
432 psz_subtitle = realloc( psz_new_subtitle,
433 psz_convert_buffer_out - psz_new_subtitle );
434 if( !psz_subtitle )
435 psz_subtitle = psz_new_subtitle;
439 /* Create the subpicture unit */
440 p_spu = decoder_NewSubpictureText( p_dec );
441 if( !p_spu )
443 free( psz_subtitle );
444 return NULL;
446 p_spu->i_start = p_block->i_pts;
447 p_spu->i_stop = p_block->i_pts + p_block->i_length;
448 p_spu->b_ephemer = (p_block->i_length == 0);
449 p_spu->b_absolute = false;
451 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
453 p_spu_sys->align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
454 p_spu_sys->text = StripTags( psz_subtitle );
455 if( var_InheritBool( p_dec, "subsdec-formatted" ) )
456 p_spu_sys->html = CreateHtmlSubtitle( &p_spu_sys->align, psz_subtitle );
458 free( psz_subtitle );
460 return p_spu;
463 /* Function now handles tags with attribute values, and tries
464 * to deal with &' commands too. It no longer modifies the string
465 * in place, so that the original text can be reused
467 static char *StripTags( char *psz_subtitle )
469 char *psz_text_start;
470 char *psz_text;
472 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
473 if( !psz_text_start )
474 return NULL;
476 while( *psz_subtitle )
478 if( *psz_subtitle == '<' )
480 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
481 *psz_text++ = '\n';
483 psz_subtitle += strcspn( psz_subtitle, ">" );
485 else if( *psz_subtitle == '&' )
487 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
489 *psz_text++ = '<';
490 psz_subtitle += strcspn( psz_subtitle, ";" );
492 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
494 *psz_text++ = '>';
495 psz_subtitle += strcspn( psz_subtitle, ";" );
497 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
499 *psz_text++ = '&';
500 psz_subtitle += strcspn( psz_subtitle, ";" );
502 else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
504 *psz_text++ = '\"';
505 psz_subtitle += strcspn( psz_subtitle, ";" );
507 else
509 /* Assume it is just a normal ampersand */
510 *psz_text++ = '&';
513 else
515 *psz_text++ = *psz_subtitle;
518 /* Security fix: Account for the case where input ends early */
519 if( *psz_subtitle == '\0' ) break;
521 psz_subtitle++;
523 *psz_text = '\0';
524 char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
525 if( psz ) psz_text_start = psz;
527 return psz_text_start;
530 /* Try to respect any style tags present in the subtitle string. The main
531 * problem here is a lack of adequate specs for the subtitle formats.
532 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
533 * SAMI has a detailed spec, but extensive rework is needed in the demux
534 * code to prevent all this style information being excised, as it presently
535 * does.
536 * That leaves the others - none of which were (I guess) originally intended
537 * to be carrying style information. Over time people have used them that way.
538 * In the absence of specifications from which to work, the tags supported
539 * have been restricted to the simple set permitted by the USF DTD, ie. :
540 * Basic: <br>, <i>, <b>, <u>, <s>
541 * Extended: <font>
542 * Attributes: face
543 * family
544 * size
545 * color
546 * outline-color
547 * shadow-color
548 * outline-level
549 * shadow-level
550 * back-color
551 * alpha
552 * There is also the further restriction that the subtitle be well-formed
553 * as an XML entity, ie. the HTML sentence:
554 * <b><i>Bold and Italics</b></i>
555 * doesn't qualify because the tags aren't nested one inside the other.
556 * <text> tags are automatically added to the output to ensure
557 * well-formedness.
558 * If the text doesn't qualify for any reason, a NULL string is
559 * returned, and the rendering engine will fall back to the
560 * plain text version of the subtitle.
562 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
564 const int i_len = strlen(psz_text);
566 strncpy( *ppsz_html, psz_text, i_max );
567 *ppsz_html += __MIN(i_max,i_len);
570 static void HtmlPut( char **ppsz_html, const char *psz_text )
572 strcpy( *ppsz_html, psz_text );
573 *ppsz_html += strlen(psz_text);
575 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
577 HtmlPut( ppsz_html, psz_text );
578 *ppsz_subtitle += strlen(psz_text);
581 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
583 /* */
584 char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
585 if( !psz_tag )
586 return NULL;
587 psz_tag[ 0 ] = '\0';
589 /* */
590 //Oo + 100 ???
591 size_t i_buf_size = strlen( psz_subtitle ) + 100;
592 char *psz_html_start = malloc( i_buf_size );
593 char *psz_html = psz_html_start;
594 if( psz_html_start == NULL )
596 free( psz_tag );
597 return NULL;
599 psz_html[0] = '\0';
601 bool b_has_align = false;
603 HtmlPut( &psz_html, "<text>" );
605 /* */
606 while( *psz_subtitle )
608 if( *psz_subtitle == '\n' )
610 HtmlPut( &psz_html, "<br/>" );
611 psz_subtitle++;
613 else if( *psz_subtitle == '<' )
615 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
617 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
619 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
621 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
622 strcat( psz_tag, "b" );
624 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
626 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
627 strcat( psz_tag, "i" );
629 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
631 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
632 strcat( psz_tag, "u" );
634 else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
636 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
637 strcat( psz_tag, "s" );
639 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
641 const char *psz_attribs[] = { "face=", "family=", "size=",
642 "color=", "outline-color=", "shadow-color=",
643 "outline-level=", "shadow-level=", "back-color=",
644 "alpha=", NULL };
646 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
647 strcat( psz_tag, "f" );
649 while( *psz_subtitle != '>' )
651 int k;
653 for( k=0; psz_attribs[ k ]; k++ )
655 int i_len = strlen( psz_attribs[ k ] );
657 if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
659 /* */
660 HtmlPut( &psz_html, psz_attribs[k] );
661 psz_subtitle += i_len;
663 /* */
664 if( *psz_subtitle == '"' )
666 psz_subtitle++;
667 i_len = strcspn( psz_subtitle, "\"" );
669 else
671 i_len = strcspn( psz_subtitle, " \t>" );
673 HtmlPut( &psz_html, "\"" );
674 HtmlNPut( &psz_html, psz_subtitle, i_len );
675 HtmlPut( &psz_html, "\"" );
677 psz_subtitle += i_len;
678 if( *psz_subtitle == '\"' )
679 psz_subtitle++;
680 break;
683 if( psz_attribs[ k ] == NULL )
685 /* Jump over unrecognised tag */
686 int i_len = strcspn( psz_subtitle, "\"" );
687 if( psz_subtitle[i_len] == '\"' )
689 i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
690 if( psz_subtitle[i_len] == '\"' )
691 i_len++;
693 psz_subtitle += i_len;
695 while (*psz_subtitle == ' ')
696 *psz_html++ = *psz_subtitle++;
698 *psz_html++ = *psz_subtitle++;
700 else if( !strncmp( psz_subtitle, "</", 2 ))
702 bool b_match = false;
703 bool b_ignore = false;
704 int i_len = strlen( psz_tag ) - 1;
705 char *psz_lastTag = NULL;
707 if( i_len >= 0 )
709 psz_lastTag = psz_tag + i_len;
710 i_len = 0;
712 switch( *psz_lastTag )
714 case 'b':
715 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
716 i_len = 4;
717 break;
718 case 'i':
719 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
720 i_len = 4;
721 break;
722 case 'u':
723 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
724 i_len = 4;
725 break;
726 case 's':
727 b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
728 i_len = 4;
729 break;
730 case 'f':
731 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
732 i_len = 7;
733 break;
734 case 'I':
735 i_len = strcspn( psz_subtitle, ">" );
736 b_match = psz_subtitle[i_len] == '>';
737 b_ignore = true;
738 if( b_match )
739 i_len++;
740 break;
743 if( !b_match )
745 /* Not well formed -- kill everything */
746 free( psz_html_start );
747 psz_html_start = NULL;
748 break;
750 *psz_lastTag = '\0';
751 if( !b_ignore )
752 HtmlNPut( &psz_html, psz_subtitle, i_len );
754 psz_subtitle += i_len;
756 else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
757 ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
759 /* We have a single < */
760 HtmlPut( &psz_html, "&lt;" );
761 psz_subtitle++;
763 else
765 /* We have an unknown tag or a single < */
767 /* Search for the next tag or end of tag or end of string */
768 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
769 char *psz_closing = strstr( psz_subtitle, "/>" );
771 if( psz_closing && psz_closing < psz_stop )
773 /* We have a self closed tag, remove it */
774 psz_subtitle = &psz_closing[2];
776 else if( *psz_stop == '>' )
778 char psz_match[256];
780 snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
781 psz_match[strcspn( psz_match, " \t>" )] = '\0';
783 if( strstr( psz_subtitle, psz_match ) )
785 /* We have the closing tag, ignore it TODO */
786 psz_subtitle = &psz_stop[1];
787 strcat( psz_tag, "I" );
789 else
791 int i_len = psz_stop + 1 - psz_subtitle;
793 /* Copy the whole data */
794 for( ; i_len > 0; i_len--, psz_subtitle++ )
796 if( *psz_subtitle == '<' )
797 HtmlPut( &psz_html, "&lt;" );
798 else if( *psz_subtitle == '>' )
799 HtmlPut( &psz_html, "&gt;" );
800 else
801 *psz_html++ = *psz_subtitle;
805 else
807 /* We have a single < */
808 HtmlPut( &psz_html, "&lt;" );
809 psz_subtitle++;
813 else if( *psz_subtitle == '&' )
815 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
817 HtmlCopy( &psz_html, &psz_subtitle, "&lt;" );
819 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
821 HtmlCopy( &psz_html, &psz_subtitle, "&gt;" );
823 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
825 HtmlCopy( &psz_html, &psz_subtitle, "&amp;" );
827 else
829 HtmlPut( &psz_html, "&amp;" );
830 psz_subtitle++;
833 else if( *psz_subtitle == '>' )
835 HtmlPut( &psz_html, "&gt;" );
836 psz_subtitle++;
838 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
839 strchr( psz_subtitle, '}' ) )
841 /* Check for forced alignment */
842 if( !b_has_align &&
843 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
845 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
846 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
847 const int i_id = psz_subtitle[4] - '1';
849 b_has_align = true;
850 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
852 /* TODO fr -> rotation */
854 /* Hide {\stupidity} */
855 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
857 else if( psz_subtitle[0] == '{' &&
858 ( psz_subtitle[1] == 'Y' || psz_subtitle[1] == 'y' )
859 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
861 // FIXME: We don't do difference between Y and y, and we should.
862 if( psz_subtitle[3] == 'i' )
864 HtmlPut( &psz_html, "<i>" );
865 strcat( psz_tag, "i" );
867 if( psz_subtitle[3] == 'b' )
869 HtmlPut( &psz_html, "<b>" );
870 strcat( psz_tag, "b" );
872 if( psz_subtitle[3] == 'u' )
874 HtmlPut( &psz_html, "<u>" );
875 strcat( psz_tag, "u" );
877 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
879 else if( psz_subtitle[0] == '{' && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
881 // Hide other {x:y} atrocities, like {c:$bbggrr} or {P:x}
882 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
884 else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
886 if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
888 HtmlPut( &psz_html, "<br/>" );
889 psz_subtitle += 2;
891 else if( psz_subtitle[1] == 'h' )
893 /* Non breakable space */
894 HtmlPut( &psz_html, NO_BREAKING_SPACE );
895 psz_subtitle += 2;
897 else
899 HtmlPut( &psz_html, "\\" );
900 psz_subtitle++;
903 else
905 *psz_html = *psz_subtitle;
906 if( psz_html > psz_html_start )
908 /* Check for double whitespace */
909 if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
910 ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
912 HtmlPut( &psz_html, NO_BREAKING_SPACE );
913 psz_html--;
916 psz_html++;
917 psz_subtitle++;
920 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
922 const int i_len = psz_html - psz_html_start;
924 i_buf_size += 200;
925 char *psz_new = realloc( psz_html_start, i_buf_size );
926 if( !psz_new )
927 break;
928 psz_html_start = psz_new;
929 psz_html = &psz_new[i_len];
932 if( psz_html_start )
934 static const char *psz_text_close = "</text>";
935 static const char *psz_tag_long = "/font>";
937 /* Realloc for closing tags and shrink memory */
938 const size_t i_length = (size_t)( psz_html - psz_html_start );
940 const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
941 char *psz_new = realloc( psz_html_start, i_size );
942 if( psz_new )
944 psz_html_start = psz_new;
945 psz_html = &psz_new[i_length];
947 /* Close not well formed subtitle */
948 while( *psz_tag )
950 /* */
951 char *psz_last = &psz_tag[strlen(psz_tag)-1];
952 switch( *psz_last )
954 case 'b':
955 HtmlPut( &psz_html, "</b>" );
956 break;
957 case 'i':
958 HtmlPut( &psz_html, "</i>" );
959 break;
960 case 'u':
961 HtmlPut( &psz_html, "</u>" );
962 break;
963 case 's':
964 HtmlPut( &psz_html, "</s>" );
965 break;
966 case 'f':
967 HtmlPut( &psz_html, "/font>" );
968 break;
969 case 'I':
970 break;
973 *psz_last = '\0';
975 HtmlPut( &psz_html, psz_text_close );
978 free( psz_tag );
980 return psz_html_start;