subsdec: add a special "local system" character set
[vlc/asuraparaju-public.git] / modules / codec / subtitles / subsdec.c
blobb128859b0317b5729e4bf1c3798db3e6e70dfab1
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 "subsdec.h"
35 #include <vlc_plugin.h>
37 /*****************************************************************************
38 * Local prototypes
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 /*****************************************************************************
50 * Module descriptor.
51 *****************************************************************************/
52 static const char *const ppsz_encodings[] = {
53 "",
54 "system",
55 "UTF-8",
56 "UTF-16",
57 "UTF-16BE",
58 "UTF-16LE",
59 "GB18030",
60 "ISO-8859-15",
61 "Windows-1252",
62 "ISO-8859-2",
63 "Windows-1250",
64 "ISO-8859-3",
65 "ISO-8859-10",
66 "Windows-1251",
67 "KOI8-R",
68 "KOI8-U",
69 "ISO-8859-6",
70 "Windows-1256",
71 "ISO-8859-7",
72 "Windows-1253",
73 "ISO-8859-8",
74 "Windows-1255",
75 "ISO-8859-9",
76 "Windows-1254",
77 "ISO-8859-11",
78 "Windows-874",
79 "ISO-8859-13",
80 "Windows-1257",
81 "ISO-8859-14",
82 "ISO-8859-16",
83 "ISO-2022-CN-EXT",
84 "EUC-CN",
85 "ISO-2022-JP-2",
86 "EUC-JP",
87 "Shift_JIS",
88 "CP949",
89 "ISO-2022-KR",
90 "Big5",
91 "ISO-2022-TW",
92 "Big5-HKSCS",
93 "VISCII",
94 "Windows-1258",
97 static const char *const ppsz_encoding_names[] = {
98 /* xgettext:
99 The character encoding name in parenthesis corresponds to that used for
100 the GetACP translation. "Windows-1252" applies to Western European
101 languages using the Latin alphabet. */
102 N_("Default (Windows-1252)"),
103 N_("System codeset"),
104 N_("Universal (UTF-8)"),
105 N_("Universal (UTF-16)"),
106 N_("Universal (big endian UTF-16)"),
107 N_("Universal (little endian UTF-16)"),
108 N_("Universal, Chinese (GB18030)"),
110 /* ISO 8859 and the likes */
111 /* 1 */
112 N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
113 N_("Western European (Windows-1252)"),
114 /* 2 */
115 N_("Eastern European (Latin-2)"),
116 N_("Eastern European (Windows-1250)"),
117 /* 3 */
118 N_("Esperanto (Latin-3)"),
119 /* 4 */
120 N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
121 /* 5 */
122 N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
123 N_("Russian (KOI8-R)"),
124 N_("Ukrainian (KOI8-U)"),
125 /* 6 */
126 N_("Arabic (ISO 8859-6)"),
127 N_("Arabic (Windows-1256)"),
128 /* 7 */
129 N_("Greek (ISO 8859-7)"),
130 N_("Greek (Windows-1253)"),
131 /* 8 */
132 N_("Hebrew (ISO 8859-8)"),
133 N_("Hebrew (Windows-1255)"),
134 /* 9 */
135 N_("Turkish (ISO 8859-9)"),
136 N_("Turkish (Windows-1254)"),
137 /* 10 -> 4 */
138 /* 11 */
139 N_("Thai (TIS 620-2533/ISO 8859-11)"),
140 N_("Thai (Windows-874)"),
141 /* 13 */
142 N_("Baltic (Latin-7)"),
143 N_("Baltic (Windows-1257)"),
144 /* 12 -> /dev/null */
145 /* 14 */
146 N_("Celtic (Latin-8)"),
147 /* 15 -> 1 */
148 /* 16 */
149 N_("South-Eastern European (Latin-10)"),
150 /* CJK families */
151 N_("Simplified Chinese (ISO-2022-CN-EXT)"),
152 N_("Simplified Chinese Unix (EUC-CN)"),
153 N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
154 N_("Japanese Unix (EUC-JP)"),
155 N_("Japanese (Shift JIS)"),
156 N_("Korean (EUC-KR/CP949)"),
157 N_("Korean (ISO-2022-KR)"),
158 N_("Traditional Chinese (Big5)"),
159 N_("Traditional Chinese Unix (EUC-TW)"),
160 N_("Hong-Kong Supplementary (HKSCS)"),
161 /* Other */
162 N_("Vietnamese (VISCII)"),
163 N_("Vietnamese (Windows-1258)"),
166 SSA supports charset selection.
167 The following known charsets are used:
169 0 = Ansi - Western European
170 1 = default
171 2 = symbol
172 3 = invalid
173 77 = Mac
174 128 = Japanese (Shift JIS)
175 129 = Hangul
176 130 = Johab
177 134 = GB2312 Simplified Chinese
178 136 = Big5 Traditional Chinese
179 161 = Greek
180 162 = Turkish
181 163 = Vietnamese
182 177 = Hebrew
183 178 = Arabic
184 186 = Baltic
185 204 = Russian (Cyrillic)
186 222 = Thai
187 238 = Eastern European
188 254 = PC 437
191 static const int pi_justification[] = { 0, 1, 2 };
192 static const char *const ppsz_justification_text[] = {
193 N_("Center"),N_("Left"),N_("Right")};
195 #define ENCODING_TEXT N_("Subtitles text encoding")
196 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
197 #define ALIGN_TEXT N_("Subtitles justification")
198 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
199 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
200 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
201 "UTF-8 encoding within subtitles files.")
202 #define FORMAT_TEXT N_("Formatted Subtitles")
203 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
204 "VLC partly implements this, but you can choose to disable all formatting.")
207 vlc_module_begin ()
208 set_shortname( N_("Subtitles"))
209 set_description( N_("Text subtitles decoder") )
210 set_capability( "decoder", 50 )
211 set_callbacks( OpenDecoder, CloseDecoder )
212 set_category( CAT_INPUT )
213 set_subcategory( SUBCAT_INPUT_SCODEC )
215 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
216 false )
217 change_integer_list( pi_justification, ppsz_justification_text, NULL )
218 add_string( "subsdec-encoding", "", NULL,
219 ENCODING_TEXT, ENCODING_LONGTEXT, false )
220 change_string_list( ppsz_encodings, ppsz_encoding_names, 0 )
221 add_bool( "subsdec-autodetect-utf8", true, NULL,
222 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
223 add_bool( "subsdec-formatted", true, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
224 false )
225 vlc_module_end ()
227 /*****************************************************************************
228 * OpenDecoder: probe the decoder and return score
229 *****************************************************************************
230 * Tries to launch a decoder and return score so that the interface is able
231 * to chose.
232 *****************************************************************************/
233 static int OpenDecoder( vlc_object_t *p_this )
235 decoder_t *p_dec = (decoder_t*)p_this;
236 decoder_sys_t *p_sys;
238 switch( p_dec->fmt_in.i_codec )
240 case VLC_CODEC_SUBT:
241 case VLC_CODEC_SSA:
242 case VLC_CODEC_ITU_T140:
243 break;
244 default:
245 return VLC_EGENERIC;
248 p_dec->pf_decode_sub = DecodeBlock;
249 p_dec->fmt_out.i_cat = SPU_ES;
250 p_dec->fmt_out.i_codec = 0;
252 /* Allocate the memory needed to store the decoder's structure */
253 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
254 if( p_sys == NULL )
255 return VLC_ENOMEM;
257 /* init of p_sys */
258 p_sys->i_align = 0;
259 p_sys->iconv_handle = (vlc_iconv_t)-1;
260 p_sys->b_autodetect_utf8 = false;
261 p_sys->b_ass = false;
262 p_sys->i_original_height = -1;
263 p_sys->i_original_width = -1;
264 TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
265 TAB_INIT( p_sys->i_images, p_sys->pp_images );
267 char *psz_charset = NULL;
269 /* First try demux-specified encoding */
270 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
271 psz_charset = strdup( "UTF-8" ); /* IUT T.140 is always using UTF-8 */
272 else
273 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
275 psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
276 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
277 p_dec->fmt_in.subs.psz_encoding ?
278 p_dec->fmt_in.subs.psz_encoding : "not specified");
281 /* Second, try configured encoding */
282 if (psz_charset == NULL)
284 psz_charset = var_InheritString (p_dec, "subsdec-encoding");
285 msg_Dbg (p_dec, "trying configured character encoding: %s",
286 psz_charset ? psz_charset : "not specified");
287 if (!strcmp (psz_charset, "system"))
289 free (psz_charset);
290 psz_charset = strdup ("");
291 /* ^ iconv() treats "" as nl_langinfo(CODESET) */
295 /* Third, try "local" encoding with optional UTF-8 autodetection */
296 if (psz_charset == NULL)
298 /* xgettext:
299 The Windows ANSI code page most commonly used for this language.
300 VLC uses this as a guess of the subtitle files character set
301 (if UTF-8 and UTF-16 autodetection fails).
302 Western European languages normally use "CP1252", which is a
303 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
304 Other scripts use other code pages.
306 This MUST be a valid iconv character set. If unsure, please refer
307 the VideoLAN translators mailing list. */
308 const char *acp = vlc_pgettext("GetACP", "CP1252");
310 psz_charset = strdup (acp);
311 msg_Dbg (p_dec, "trying default character encoding: %s",
312 psz_charset ? psz_charset : "not specified");
314 if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
316 msg_Dbg (p_dec, "using automatic UTF-8 detection");
317 p_sys->b_autodetect_utf8 = true;
321 /* Forth, don't do character decoding, i.e. assume UTF-8 */
322 if (psz_charset == NULL)
324 psz_charset = strdup ("UTF-8");
325 msg_Dbg (p_dec, "using UTF-8 character encoding" );
328 if ((psz_charset != NULL)
329 && strcasecmp (psz_charset, "UTF-8")
330 && strcasecmp (psz_charset, "utf8"))
332 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
333 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
334 msg_Err (p_dec, "cannot convert from %s: %m", psz_charset);
336 free (psz_charset);
338 p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
340 if( p_dec->fmt_in.i_codec == VLC_CODEC_SSA
341 && var_InheritBool( p_dec, "subsdec-formatted" ) )
343 if( p_dec->fmt_in.i_extra > 0 )
344 ParseSSAHeader( p_dec );
347 return VLC_SUCCESS;
350 /****************************************************************************
351 * DecodeBlock: the whole thing
352 ****************************************************************************
353 * This function must be fed with complete subtitles units.
354 ****************************************************************************/
355 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
357 subpicture_t *p_spu;
358 block_t *p_block;
360 if( !pp_block || *pp_block == NULL )
361 return NULL;
363 p_block = *pp_block;
364 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
366 block_Release( p_block );
367 return NULL;
370 p_spu = ParseText( p_dec, p_block );
372 block_Release( p_block );
373 *pp_block = NULL;
375 return p_spu;
378 /*****************************************************************************
379 * CloseDecoder: clean up the decoder
380 *****************************************************************************/
381 static void CloseDecoder( vlc_object_t *p_this )
383 decoder_t *p_dec = (decoder_t *)p_this;
384 decoder_sys_t *p_sys = p_dec->p_sys;
386 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
387 vlc_iconv_close( p_sys->iconv_handle );
389 if( p_sys->pp_ssa_styles )
391 int i;
392 for( i = 0; i < p_sys->i_ssa_styles; i++ )
394 if( !p_sys->pp_ssa_styles[i] )
395 continue;
397 free( p_sys->pp_ssa_styles[i]->psz_stylename );
398 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
399 free( p_sys->pp_ssa_styles[i] );
401 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
403 if( p_sys->pp_images )
405 int i;
406 for( i = 0; i < p_sys->i_images; i++ )
408 if( !p_sys->pp_images[i] )
409 continue;
411 if( p_sys->pp_images[i]->p_pic )
412 picture_Release( p_sys->pp_images[i]->p_pic );
413 free( p_sys->pp_images[i]->psz_filename );
415 free( p_sys->pp_images[i] );
417 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
420 free( p_sys );
423 /*****************************************************************************
424 * ParseText: parse an text subtitle packet and send it to the video output
425 *****************************************************************************/
426 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
428 decoder_sys_t *p_sys = p_dec->p_sys;
429 subpicture_t *p_spu = NULL;
430 char *psz_subtitle = NULL;
431 video_format_t fmt;
433 /* We cannot display a subpicture with no date */
434 if( p_block->i_pts <= VLC_TS_INVALID )
436 msg_Warn( p_dec, "subtitle without a date" );
437 return NULL;
440 /* Check validity of packet data */
441 /* An "empty" line containing only \0 can be used to force
442 and ephemer picture from the screen */
443 if( p_block->i_buffer < 1 )
445 msg_Warn( p_dec, "no subtitle data" );
446 return NULL;
449 /* Should be resiliant against bad subtitles */
450 psz_subtitle = malloc( p_block->i_buffer + 1 );
451 if( psz_subtitle == NULL )
452 return NULL;
453 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
454 psz_subtitle[p_block->i_buffer] = '\0';
456 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
458 if (EnsureUTF8( psz_subtitle ) == NULL)
460 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
461 "Try manually setting a character-encoding "
462 "before you open the file." );
465 else
468 if( p_sys->b_autodetect_utf8 )
470 if( IsUTF8( psz_subtitle ) == NULL )
472 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
473 "disabling UTF-8 subtitles autodetection" );
474 p_sys->b_autodetect_utf8 = false;
478 if( !p_sys->b_autodetect_utf8 )
480 size_t inbytes_left = strlen( psz_subtitle );
481 size_t outbytes_left = 6 * inbytes_left;
482 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
483 char *psz_convert_buffer_out = psz_new_subtitle;
484 const char *psz_convert_buffer_in = psz_subtitle;
486 size_t ret = vlc_iconv( p_sys->iconv_handle,
487 &psz_convert_buffer_in, &inbytes_left,
488 &psz_convert_buffer_out, &outbytes_left );
490 *psz_convert_buffer_out++ = '\0';
491 free( psz_subtitle );
493 if( ( ret == (size_t)(-1) ) || inbytes_left )
495 free( psz_new_subtitle );
496 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
497 "Try manually setting a character-encoding "
498 "before you open the file." );
499 return NULL;
502 psz_subtitle = realloc( psz_new_subtitle,
503 psz_convert_buffer_out - psz_new_subtitle );
504 if( !psz_subtitle )
505 psz_subtitle = psz_new_subtitle;
509 /* Create the subpicture unit */
510 p_spu = decoder_NewSubpicture( p_dec, NULL );
511 if( !p_spu )
513 msg_Warn( p_dec, "can't get spu buffer" );
514 free( psz_subtitle );
515 return NULL;
518 /* Create a new subpicture region */
519 memset( &fmt, 0, sizeof(video_format_t) );
520 fmt.i_chroma = VLC_CODEC_TEXT;
521 fmt.i_width = fmt.i_height = 0;
522 fmt.i_x_offset = fmt.i_y_offset = 0;
523 p_spu->p_region = subpicture_region_New( &fmt );
524 if( !p_spu->p_region )
526 msg_Err( p_dec, "cannot allocate SPU region" );
527 free( psz_subtitle );
528 decoder_DeleteSubpicture( p_dec, p_spu );
529 return NULL;
532 /* Decode and format the subpicture unit */
533 if( p_dec->fmt_in.i_codec != VLC_CODEC_SSA )
535 /* Normal text subs, easy markup */
536 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
537 p_spu->p_region->i_x = p_sys->i_align ? 20 : 0;
538 p_spu->p_region->i_y = 10;
540 /* Remove formatting from string */
542 p_spu->p_region->psz_text = StripTags( psz_subtitle );
543 if( var_InheritBool( p_dec, "subsdec-formatted" ) )
545 p_spu->p_region->psz_html = CreateHtmlSubtitle( &p_spu->p_region->i_align, psz_subtitle );
548 p_spu->i_start = p_block->i_pts;
549 p_spu->i_stop = p_block->i_pts + p_block->i_length;
550 p_spu->b_ephemer = (p_block->i_length == 0);
551 p_spu->b_absolute = false;
553 else
555 /* Decode SSA/USF strings */
556 ParseSSAString( p_dec, psz_subtitle, p_spu );
558 p_spu->i_start = p_block->i_pts;
559 p_spu->i_stop = p_block->i_pts + p_block->i_length;
560 p_spu->b_ephemer = (p_block->i_length == 0);
561 p_spu->b_absolute = false;
562 p_spu->i_original_picture_width = p_sys->i_original_width;
563 p_spu->i_original_picture_height = p_sys->i_original_height;
565 free( psz_subtitle );
567 return p_spu;
570 char* GotoNextLine( char *psz_text )
572 char *p_newline = psz_text;
574 while( p_newline[0] != '\0' )
576 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
578 p_newline++;
579 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
580 p_newline++;
581 break;
583 else p_newline++;
585 return p_newline;
588 /* Function now handles tags with attribute values, and tries
589 * to deal with &' commands too. It no longer modifies the string
590 * in place, so that the original text can be reused
592 static char *StripTags( char *psz_subtitle )
594 char *psz_text_start;
595 char *psz_text;
597 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
598 if( !psz_text_start )
599 return NULL;
601 while( *psz_subtitle )
603 if( *psz_subtitle == '<' )
605 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
606 *psz_text++ = '\n';
608 psz_subtitle += strcspn( psz_subtitle, ">" );
610 else if( *psz_subtitle == '&' )
612 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
614 *psz_text++ = '<';
615 psz_subtitle += strcspn( psz_subtitle, ";" );
617 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
619 *psz_text++ = '>';
620 psz_subtitle += strcspn( psz_subtitle, ";" );
622 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
624 *psz_text++ = '&';
625 psz_subtitle += strcspn( psz_subtitle, ";" );
627 else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
629 *psz_text++ = '\"';
630 psz_subtitle += strcspn( psz_subtitle, ";" );
632 else
634 /* Assume it is just a normal ampersand */
635 *psz_text++ = '&';
638 else
640 *psz_text++ = *psz_subtitle;
643 psz_subtitle++;
645 *psz_text = '\0';
646 char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
647 if( psz ) psz_text_start = psz;
649 return psz_text_start;
652 /* Try to respect any style tags present in the subtitle string. The main
653 * problem here is a lack of adequate specs for the subtitle formats.
654 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
655 * SAMI has a detailed spec, but extensive rework is needed in the demux
656 * code to prevent all this style information being excised, as it presently
657 * does.
658 * That leaves the others - none of which were (I guess) originally intended
659 * to be carrying style information. Over time people have used them that way.
660 * In the absence of specifications from which to work, the tags supported
661 * have been restricted to the simple set permitted by the USF DTD, ie. :
662 * Basic: <br>, <i>, <b>, <u>, <s>
663 * Extended: <font>
664 * Attributes: face
665 * family
666 * size
667 * color
668 * outline-color
669 * shadow-color
670 * outline-level
671 * shadow-level
672 * back-color
673 * alpha
674 * There is also the further restriction that the subtitle be well-formed
675 * as an XML entity, ie. the HTML sentence:
676 * <b><i>Bold and Italics</b></i>
677 * doesn't qualify because the tags aren't nested one inside the other.
678 * <text> tags are automatically added to the output to ensure
679 * well-formedness.
680 * If the text doesn't qualify for any reason, a NULL string is
681 * returned, and the rendering engine will fall back to the
682 * plain text version of the subtitle.
684 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
686 const int i_len = strlen(psz_text);
688 strncpy( *ppsz_html, psz_text, i_max );
689 *ppsz_html += __MIN(i_max,i_len);
692 static void HtmlPut( char **ppsz_html, const char *psz_text )
694 strcpy( *ppsz_html, psz_text );
695 *ppsz_html += strlen(psz_text);
697 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
699 HtmlPut( ppsz_html, psz_text );
700 *ppsz_subtitle += strlen(psz_text);
703 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
705 /* */
706 char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
707 if( !psz_tag )
708 return NULL;
709 psz_tag[ 0 ] = '\0';
711 /* */
712 //Oo + 100 ???
713 size_t i_buf_size = strlen( psz_subtitle ) + 100;
714 char *psz_html_start = malloc( i_buf_size );
715 char *psz_html = psz_html_start;
716 if( psz_html_start == NULL )
718 free( psz_tag );
719 return NULL;
721 psz_html[0] = '\0';
723 bool b_has_align = false;
725 HtmlPut( &psz_html, "<text>" );
727 /* */
728 while( *psz_subtitle )
730 if( *psz_subtitle == '\n' )
732 HtmlPut( &psz_html, "<br/>" );
733 psz_subtitle++;
735 else if( *psz_subtitle == '<' )
737 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
739 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
741 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
743 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
744 strcat( psz_tag, "b" );
746 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
748 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
749 strcat( psz_tag, "i" );
751 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
753 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
754 strcat( psz_tag, "u" );
756 else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
758 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
759 strcat( psz_tag, "s" );
761 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
763 const char *psz_attribs[] = { "face=", "family=", "size=",
764 "color=", "outline-color=", "shadow-color=",
765 "outline-level=", "shadow-level=", "back-color=",
766 "alpha=", NULL };
768 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
769 strcat( psz_tag, "f" );
771 while( *psz_subtitle != '>' )
773 int k;
775 for( k=0; psz_attribs[ k ]; k++ )
777 int i_len = strlen( psz_attribs[ k ] );
779 if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
781 /* */
782 HtmlPut( &psz_html, psz_attribs[k] );
783 psz_subtitle += i_len;
785 /* */
786 if( *psz_subtitle == '"' )
788 psz_subtitle++;
789 i_len = strcspn( psz_subtitle, "\"" );
791 else
793 i_len = strcspn( psz_subtitle, " \t>" );
795 HtmlPut( &psz_html, "\"" );
796 if( !strcmp( psz_attribs[ k ], "color=" ) && *psz_subtitle >= '0' && *psz_subtitle <= '9' )
797 HtmlPut( &psz_html, "#" );
798 HtmlNPut( &psz_html, psz_subtitle, i_len );
799 HtmlPut( &psz_html, "\"" );
801 psz_subtitle += i_len;
802 if( *psz_subtitle == '\"' )
803 psz_subtitle++;
804 break;
807 if( psz_attribs[ k ] == NULL )
809 /* Jump over unrecognised tag */
810 int i_len = strcspn( psz_subtitle, "\"" );
811 if( psz_subtitle[i_len] == '\"' )
813 i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
814 if( psz_subtitle[i_len] == '\"' )
815 i_len++;
817 psz_subtitle += i_len;
819 while (*psz_subtitle == ' ')
820 *psz_html++ = *psz_subtitle++;
822 *psz_html++ = *psz_subtitle++;
824 else if( !strncmp( psz_subtitle, "</", 2 ))
826 bool b_match = false;
827 bool b_ignore = false;
828 int i_len = strlen( psz_tag ) - 1;
829 char *psz_lastTag = NULL;
831 if( i_len >= 0 )
833 psz_lastTag = psz_tag + i_len;
834 i_len = 0;
836 switch( *psz_lastTag )
838 case 'b':
839 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
840 i_len = 4;
841 break;
842 case 'i':
843 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
844 i_len = 4;
845 break;
846 case 'u':
847 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
848 i_len = 4;
849 break;
850 case 's':
851 b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
852 i_len = 4;
853 break;
854 case 'f':
855 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
856 i_len = 7;
857 break;
858 case 'I':
859 i_len = strcspn( psz_subtitle, ">" );
860 b_match = psz_subtitle[i_len] == '>';
861 b_ignore = true;
862 if( b_match )
863 i_len++;
864 break;
867 if( !b_match )
869 /* Not well formed -- kill everything */
870 free( psz_html_start );
871 psz_html_start = NULL;
872 break;
874 *psz_lastTag = '\0';
875 if( !b_ignore )
876 HtmlNPut( &psz_html, psz_subtitle, i_len );
878 psz_subtitle += i_len;
880 else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
881 ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
883 /* We have a single < */
884 HtmlPut( &psz_html, "&lt;" );
885 psz_subtitle++;
887 else
889 /* We have an unknown tag or a single < */
891 /* Search for the next tag or end of tag or end of string */
892 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
893 char *psz_closing = strstr( psz_subtitle, "/>" );
895 if( psz_closing && psz_closing < psz_stop )
897 /* We have a self closed tag, remove it */
898 psz_subtitle = &psz_closing[2];
900 else if( *psz_stop == '>' )
902 char psz_match[256];
904 snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
905 psz_match[strcspn( psz_match, " \t>" )] = '\0';
907 if( strstr( psz_subtitle, psz_match ) )
909 /* We have the closing tag, ignore it TODO */
910 psz_subtitle = &psz_stop[1];
911 strcat( psz_tag, "I" );
913 else
915 int i_len = psz_stop + 1 - psz_subtitle;
917 /* Copy the whole data */
918 for( ; i_len > 0; i_len--, psz_subtitle++ )
920 if( *psz_subtitle == '<' )
921 HtmlPut( &psz_html, "&lt;" );
922 else if( *psz_subtitle == '>' )
923 HtmlPut( &psz_html, "&gt;" );
924 else
925 *psz_html++ = *psz_subtitle;
929 else
931 /* We have a single < */
932 HtmlPut( &psz_html, "&lt;" );
933 psz_subtitle++;
937 else if( *psz_subtitle == '&' )
939 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
941 HtmlCopy( &psz_html, &psz_subtitle, "&lt;" );
943 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
945 HtmlCopy( &psz_html, &psz_subtitle, "&gt;" );
947 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
949 HtmlCopy( &psz_html, &psz_subtitle, "&amp;" );
951 else
953 HtmlPut( &psz_html, "&amp;" );
954 psz_subtitle++;
957 else if( *psz_subtitle == '>' )
959 HtmlPut( &psz_html, "&gt;" );
960 psz_subtitle++;
962 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
963 strchr( psz_subtitle, '}' ) )
965 /* Check for forced alignment */
966 if( !b_has_align &&
967 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
969 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
970 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
971 const int i_id = psz_subtitle[4] - '1';
973 b_has_align = true;
974 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
976 /* TODO fr -> rotation */
978 /* Hide {\stupidity} */
979 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
981 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == 'Y'
982 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
984 /* Hide {Y:stupidity} */
985 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
987 else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
989 if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
991 HtmlPut( &psz_html, "<br/>" );
992 psz_subtitle += 2;
994 else if( psz_subtitle[1] == 'h' )
996 /* Non breakable space */
997 HtmlPut( &psz_html, NO_BREAKING_SPACE );
998 psz_subtitle += 2;
1000 else
1002 HtmlPut( &psz_html, "\\" );
1003 psz_subtitle++;
1006 else
1008 *psz_html = *psz_subtitle;
1009 if( psz_html > psz_html_start )
1011 /* Check for double whitespace */
1012 if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
1013 ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
1015 HtmlPut( &psz_html, NO_BREAKING_SPACE );
1016 psz_html--;
1019 psz_html++;
1020 psz_subtitle++;
1023 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
1025 const int i_len = psz_html - psz_html_start;
1027 i_buf_size += 200;
1028 char *psz_new = realloc( psz_html_start, i_buf_size );
1029 if( !psz_new )
1030 break;
1031 psz_html_start = psz_new;
1032 psz_html = &psz_new[i_len];
1035 if( psz_html_start )
1037 static const char *psz_text_close = "</text>";
1038 static const char *psz_tag_long = "/font>";
1040 /* Realloc for closing tags and shrink memory */
1041 const size_t i_length = (size_t)( psz_html - psz_html_start );
1043 const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
1044 char *psz_new = realloc( psz_html_start, i_size );
1045 if( psz_new )
1047 psz_html_start = psz_new;
1048 psz_html = &psz_new[i_length];
1050 /* Close not well formed subtitle */
1051 while( *psz_tag )
1053 /* */
1054 char *psz_last = &psz_tag[strlen(psz_tag)-1];
1055 switch( *psz_last )
1057 case 'b':
1058 HtmlPut( &psz_html, "</b>" );
1059 break;
1060 case 'i':
1061 HtmlPut( &psz_html, "</i>" );
1062 break;
1063 case 'u':
1064 HtmlPut( &psz_html, "</u>" );
1065 break;
1066 case 's':
1067 HtmlPut( &psz_html, "</s>" );
1068 break;
1069 case 'f':
1070 HtmlPut( &psz_html, "/font>" );
1071 break;
1072 case 'I':
1073 break;
1076 *psz_last = '\0';
1078 HtmlPut( &psz_html, psz_text_close );
1081 free( psz_tag );
1083 return psz_html_start;